Mercury-IM/app/src/main/java/org/mercury_im/messenger/service/MercuryConnectionService.java

119 lines
3.6 KiB
Java
Raw Normal View History

2019-05-27 21:34:11 +02:00
package org.mercury_im.messenger.service;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
2019-06-10 02:52:08 +02:00
import androidx.annotation.NonNull;
2019-05-27 21:34:11 +02:00
import androidx.core.app.NotificationCompat;
import org.jivesoftware.smack.XMPPConnection;
2019-06-10 02:52:08 +02:00
import org.jivesoftware.smackx.ping.android.ServerPingWithAlarmManager;
2019-05-27 21:34:11 +02:00
import org.mercury_im.messenger.MercuryImApplication;
import org.mercury_im.messenger.Notifications;
import org.mercury_im.messenger.R;
import org.mercury_im.messenger.ui.MainActivity;
/**
2019-06-06 23:32:41 +02:00
* Started, Bound Service, which is responsible for managing {@link XMPPConnection XMPPConnections}
2019-05-27 21:34:11 +02:00
* affiliated with registered accounts.
*/
2019-12-09 13:50:26 +01:00
public class MercuryConnectionService extends Service {
2019-05-27 21:34:11 +02:00
2019-06-20 17:20:23 +02:00
private static final String APP = "org.mercury-im.messenger";
2019-12-09 13:50:26 +01:00
private static final String SERVICE = APP + ".MercuryConnectionService";
2019-05-27 21:34:11 +02:00
private static final String ACTION = SERVICE + ".ACTION";
2019-06-06 23:32:41 +02:00
// ACTIONS
2019-05-27 21:34:11 +02:00
public static final String ACTION_START = ACTION + ".START";
public static final String ACTION_STOP = ACTION + ".STOP";
2019-06-10 02:52:08 +02:00
@NonNull
2019-05-27 21:34:11 +02:00
@Override
public final IBinder onBind(Intent intent) {
2019-06-10 02:52:08 +02:00
return new Binder(this);
2019-05-27 21:34:11 +02:00
}
@Override
public void onCreate() {
super.onCreate();
MercuryImApplication.getApplication().getAppComponent().inject(this);
2019-12-09 13:50:26 +01:00
beginLifecycleOfPingManager();
}
2019-06-10 02:52:08 +02:00
2019-12-09 13:50:26 +01:00
/**
* PingManager will ensure the XMPP connection is kept alive.
*/
private void beginLifecycleOfPingManager() {
2019-06-10 02:52:08 +02:00
ServerPingWithAlarmManager.onCreate(this);
}
@Override
public void onDestroy() {
super.onDestroy();
2019-12-09 13:50:26 +01:00
endLifecycleOfPingManager();
}
private void endLifecycleOfPingManager() {
2019-06-10 02:52:08 +02:00
ServerPingWithAlarmManager.onDestroy();
2019-05-27 21:34:11 +02:00
}
2019-06-20 17:20:23 +02:00
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
2019-05-27 21:34:11 +02:00
if (intent == null) {
startAndDisplayForegroundNotification();
} else {
String action = intent.getAction();
action = action == null ? "" : action;
switch (action) {
case ACTION_START:
startAndDisplayForegroundNotification();
break;
case ACTION_STOP:
stopForeground(true);
2019-06-20 17:20:23 +02:00
stopSelf();
2019-05-27 21:34:11 +02:00
break;
default:
break;
}
}
return START_STICKY_COMPATIBILITY;
}
2019-12-21 03:46:17 +01:00
private void startAndDisplayForegroundNotification() {
Notification notification = buildForegroundNotification(getApplicationContext());
2019-05-27 21:34:11 +02:00
startForeground(Notifications.FOREGROUND_SERVICE_ID, notification);
}
2019-12-21 03:46:17 +01:00
private static Notification buildForegroundNotification(Context context) {
2019-05-27 21:34:11 +02:00
Intent startMainActivityIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
startMainActivityIntent, 0);
return new NotificationCompat.Builder(context, Notifications.NOTIFICATION_CHANNEL__FOREGROUND_SERVICE)
.setSmallIcon(R.drawable.ic_send_black_24dp)
.setContentIntent(pendingIntent)
.build();
}
2019-06-10 02:52:08 +02:00
public class Binder extends android.os.Binder {
2019-12-09 13:50:26 +01:00
private final MercuryConnectionService service;
2019-06-10 02:52:08 +02:00
2019-12-09 13:50:26 +01:00
public Binder(MercuryConnectionService service) {
2019-06-10 02:52:08 +02:00
this.service = service;
}
2019-12-09 13:50:26 +01:00
public MercuryConnectionService getService() {
2019-06-10 02:52:08 +02:00
return service;
}
}
2019-05-27 21:34:11 +02:00
}