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

140 lines
4.7 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;
import android.util.Log;
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-07-31 22:59:46 +02:00
public class XmppConnectionService extends Service {
2019-05-27 21:34:11 +02:00
private static final String TAG = MercuryImApplication.TAG;
2019-06-20 17:20:23 +02:00
private static final String APP = "org.mercury-im.messenger";
2019-06-06 23:32:41 +02:00
private static final String SERVICE = APP + ".XmppConnectionService";
2019-05-27 21:34:11 +02:00
private static final String ACTION = SERVICE + ".ACTION";
private static final String EVENT = SERVICE + ".EVENT";
private static final String EXTRA = SERVICE + ".EXTRA";
private static final String STATUS = SERVICE + ".STATUS";
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";
public static final String ACTION_CONNECT = ACTION + ".CONNECT";
public static final String ACTION_DISCONNECT = ACTION + ".DISCONNECT";
public static final String ACTION_PING = ACTION + ".PING";
2019-06-06 23:32:41 +02:00
// EVENTS
2019-05-27 21:34:11 +02:00
public static final String EVENT_INCOMING_MESSAGE = EVENT + ".INCOMING_MESSAGE";
public static final String EVENT_OUTGOING_MESSAGE = EVENT + ".OUTGOING_MESSAGE";
2019-06-06 23:32:41 +02:00
// EXTRAS
2019-05-27 21:34:11 +02:00
public static final String EXTRA_CONFIGURATION = EXTRA + ".CONFIGURATION";
public static final String EXTRA_ACCOUNT_ID = EXTRA + ".ACCOUNT_ID";
2019-06-06 23:32:41 +02:00
// STATUSES
2019-05-27 21:34:11 +02:00
public static final String STATUS_SUCCESS = STATUS + ".SUCCESS";
public static final String STATUS_FAILURE = STATUS + ".FAILURE";
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();
Log.d(TAG, "onCreate()");
MercuryImApplication.getApplication().getAppComponent().inject(this);
2019-06-10 02:52:08 +02:00
// Begin life cycle of Ping Manager.
// The Manager will automatically detect newly created connections and ping the server
// every half hour if necessary.
ServerPingWithAlarmManager.onCreate(this);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy()");
// End life cycle of Ping Manager.
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
Log.d(TAG, "onStartCommand(" + intent + ")");
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;
}
public void startAndDisplayForegroundNotification() {
Log.d(TAG, "startAndDisplayForegroundNotification()");
2019-07-31 22:59:46 +02:00
Notification notification = getForegroundNotification(getApplicationContext());
2019-05-27 21:34:11 +02:00
startForeground(Notifications.FOREGROUND_SERVICE_ID, notification);
}
2019-07-31 22:59:46 +02:00
static Notification getForegroundNotification(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)
.setContentTitle("Mercury")
.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 {
private final XmppConnectionService service;
public Binder(XmppConnectionService service) {
this.service = service;
}
public XmppConnectionService getService() {
return service;
}
}
2019-05-27 21:34:11 +02:00
}