2019-05-13 03:19:17 +02:00
|
|
|
package org.mercury_im.messenger;
|
|
|
|
|
|
|
|
import android.app.Application;
|
|
|
|
import android.app.NotificationChannel;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Build;
|
|
|
|
|
2019-08-19 02:36:31 +02:00
|
|
|
import org.jivesoftware.smack.SmackConfiguration;
|
2019-08-20 01:15:30 +02:00
|
|
|
import org.jivesoftware.smack.SmackInitialization;
|
|
|
|
import org.jivesoftware.smack.im.SmackImInitializer;
|
|
|
|
import org.jivesoftware.smack.provider.ProviderManager;
|
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
2019-08-10 21:50:03 +02:00
|
|
|
import org.mercury_im.messenger.core.ConnectionCenter;
|
2019-08-20 01:15:30 +02:00
|
|
|
import org.mercury_im.messenger.core.MercuryConfiguration;
|
2019-05-13 03:19:17 +02:00
|
|
|
import org.mercury_im.messenger.di.component.AppComponent;
|
|
|
|
import org.mercury_im.messenger.di.component.DaggerAppComponent;
|
|
|
|
import org.mercury_im.messenger.di.module.AppModule;
|
2019-08-03 19:05:50 +02:00
|
|
|
import org.mercury_im.messenger.persistence.room.RoomModule;
|
|
|
|
import org.mercury_im.messenger.persistence.room.RoomRepositoryModule;
|
2019-06-06 23:32:41 +02:00
|
|
|
import org.mercury_im.messenger.service.XmppConnectionService;
|
2019-05-13 03:19:17 +02:00
|
|
|
|
2019-08-12 00:34:19 +02:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-05-13 03:19:17 +02:00
|
|
|
public class MercuryImApplication extends Application {
|
|
|
|
|
|
|
|
public static final String TAG = "Mercury-IM";
|
|
|
|
|
2019-08-20 01:15:30 +02:00
|
|
|
static {
|
|
|
|
// Initialize Smack etc.
|
|
|
|
new MercuryConfiguration();
|
|
|
|
}
|
|
|
|
|
2019-05-13 03:19:17 +02:00
|
|
|
private static MercuryImApplication INSTANCE;
|
|
|
|
AppComponent appComponent;
|
|
|
|
|
|
|
|
public static MercuryImApplication getApplication() {
|
|
|
|
return INSTANCE;
|
|
|
|
}
|
|
|
|
|
2019-08-12 00:34:19 +02:00
|
|
|
@Inject
|
|
|
|
ConnectionCenter connectionCenter;
|
|
|
|
|
2019-05-13 03:19:17 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
2019-08-20 01:15:30 +02:00
|
|
|
|
2019-05-13 03:19:17 +02:00
|
|
|
super.onCreate();
|
|
|
|
|
|
|
|
INSTANCE = this;
|
|
|
|
|
2019-08-03 19:05:50 +02:00
|
|
|
appComponent = createAppComponent();
|
|
|
|
|
2019-05-13 03:19:17 +02:00
|
|
|
initializeNotificationChannels(this);
|
|
|
|
|
2019-06-06 23:32:41 +02:00
|
|
|
Intent serviceIntent = new Intent(getApplicationContext(), XmppConnectionService.class);
|
2019-05-13 03:19:17 +02:00
|
|
|
|
2019-06-06 23:32:41 +02:00
|
|
|
serviceIntent.setAction(XmppConnectionService.ACTION_START);
|
2019-05-13 03:19:17 +02:00
|
|
|
if (Build.VERSION.SDK_INT < 26) {
|
|
|
|
startService(serviceIntent);
|
|
|
|
} else {
|
|
|
|
startForegroundService(serviceIntent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-03 19:05:50 +02:00
|
|
|
/**
|
|
|
|
* Create the Dependency Injection graph.
|
|
|
|
* For testing, overwrite this method with custom modules.
|
|
|
|
*/
|
|
|
|
public AppComponent createAppComponent() {
|
|
|
|
AppComponent appComponent = DaggerAppComponent.builder()
|
|
|
|
.appModule(new AppModule(this))
|
|
|
|
.roomModule(new RoomModule(this))
|
|
|
|
.roomRepositoryModule(new RoomRepositoryModule())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
appComponent.inject(this);
|
|
|
|
|
|
|
|
return appComponent;
|
|
|
|
}
|
|
|
|
|
2019-05-13 03:19:17 +02:00
|
|
|
public void initializeNotificationChannels(Context context) {
|
|
|
|
// Only necessary on Android O and upwards.
|
|
|
|
if (Build.VERSION.SDK_INT < 26) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationManager notificationManager =
|
|
|
|
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
|
|
|
|
// Foreground notification channel
|
|
|
|
String fName = getResources().getString(R.string.channel_name_foreground);
|
|
|
|
String fDescription = getResources().getString(R.string.channel_description_foreground);
|
|
|
|
|
|
|
|
NotificationChannel foreground = new NotificationChannel(Notifications.NOTIFICATION_CHANNEL__FOREGROUND_SERVICE,
|
|
|
|
fName, NotificationManager.IMPORTANCE_DEFAULT);
|
|
|
|
foreground.setDescription(fDescription);
|
|
|
|
foreground.setImportance(NotificationManager.IMPORTANCE_MIN);
|
|
|
|
foreground.setShowBadge(false);
|
|
|
|
notificationManager.createNotificationChannel(foreground);
|
|
|
|
}
|
|
|
|
|
|
|
|
public AppComponent getAppComponent() {
|
|
|
|
return appComponent;
|
|
|
|
}
|
|
|
|
}
|