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; import org.jivesoftware.smack.SmackConfiguration; import org.jivesoftware.smack.SmackInitialization; import org.jivesoftware.smack.im.SmackImInitializer; import org.jivesoftware.smack.provider.ProviderManager; import org.jivesoftware.smack.util.PacketParserUtils; import org.mercury_im.messenger.core.ConnectionCenter; import org.mercury_im.messenger.core.MercuryConfiguration; import org.mercury_im.messenger.di.component.AppComponent; import org.mercury_im.messenger.di.component.DaggerAppComponent; import org.mercury_im.messenger.di.module.AppModule; import org.mercury_im.messenger.persistence.room.RoomModule; import org.mercury_im.messenger.persistence.room.RoomRepositoryModule; import org.mercury_im.messenger.service.XmppConnectionService; import javax.inject.Inject; public class MercuryImApplication extends Application { public static final String TAG = "Mercury-IM"; static { // Initialize Smack etc. new MercuryConfiguration(); } private static MercuryImApplication INSTANCE; AppComponent appComponent; public static MercuryImApplication getApplication() { return INSTANCE; } @Inject ConnectionCenter connectionCenter; @Override public void onCreate() { super.onCreate(); INSTANCE = this; appComponent = createAppComponent(); initializeNotificationChannels(this); Intent serviceIntent = new Intent(getApplicationContext(), XmppConnectionService.class); serviceIntent.setAction(XmppConnectionService.ACTION_START); if (Build.VERSION.SDK_INT < 26) { startService(serviceIntent); } else { startForegroundService(serviceIntent); } } /** * 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; } 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; } }