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

134 lines
4.7 KiB
Java
Raw Normal View History

2019-05-13 03:19:17 +02:00
package org.mercury_im.messenger;
import android.annotation.SuppressLint;
2019-08-30 15:11:03 +02:00
import android.app.Activity;
2019-05-13 03:19:17 +02:00
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.mercury_im.messenger.di.component.AppComponent;
2019-12-06 15:52:50 +01:00
import org.mercury_im.messenger.di.component.DaggerAppComponent;
2019-05-13 03:19:17 +02:00
import org.mercury_im.messenger.di.module.AppModule;
2019-12-09 13:50:26 +01:00
import org.mercury_im.messenger.service.MercuryConnectionService;
2019-08-30 15:11:03 +02:00
import org.mercury_im.messenger.util.AbstractActivityLifecycleCallbacks;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
2019-05-13 03:19:17 +02:00
2019-08-12 00:34:19 +02:00
2019-11-18 23:51:27 +01:00
public class MercuryImApplication extends Application {
2019-05-13 03:19:17 +02:00
2019-08-20 01:15:30 +02:00
static {
// Initialize Smack etc.
2019-12-06 15:52:50 +01:00
// new MercuryConfiguration();
2019-08-20 01:15:30 +02:00
}
2019-05-13 03:19:17 +02:00
private static MercuryImApplication INSTANCE;
AppComponent appComponent;
2019-08-30 15:11:03 +02:00
// Keep track of activities in "started" state.
// This will come in handy for CSI
// see https://medium.com/@iamsadesh/android-how-to-detect-when-app-goes-background-foreground-fd5a4d331f8a
2019-08-30 15:11:03 +02:00
private AtomicInteger activityReferences = new AtomicInteger(0);
private AtomicBoolean isActivityChangingConfiguration = new AtomicBoolean(false);
2019-05-13 03:19:17 +02:00
public static MercuryImApplication getApplication() {
return INSTANCE;
}
2019-08-12 00:34:19 +02:00
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();
2019-08-30 15:11:03 +02:00
registerActivityLifecycleCallbacks(lifecycleCallbacks);
2019-05-13 03:19:17 +02:00
INSTANCE = this;
2019-08-03 19:05:50 +02:00
appComponent = createAppComponent();
2019-05-13 03:19:17 +02:00
initializeNotificationChannels(this);
2019-12-09 13:50:26 +01:00
Intent serviceIntent = new Intent(getApplicationContext(), MercuryConnectionService.class);
2019-05-13 03:19:17 +02:00
2019-12-09 13:50:26 +01:00
serviceIntent.setAction(MercuryConnectionService.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))
.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);
@SuppressLint("WrongConstant")
2019-05-13 03:19:17 +02:00
NotificationChannel foreground = new NotificationChannel(Notifications.NOTIFICATION_CHANNEL__FOREGROUND_SERVICE,
2019-09-01 02:02:44 +02:00
fName, NotificationManager.IMPORTANCE_MIN);
2019-05-13 03:19:17 +02:00
foreground.setDescription(fDescription);
foreground.setShowBadge(false);
notificationManager.createNotificationChannel(foreground);
2019-09-01 02:02:44 +02:00
// Incoming Messages notification channel
String mName = getResources().getString(R.string.channel_name_message);
String mDescription = getResources().getString(R.string.channel_description_message);
@SuppressLint("WrongConstant")
2019-09-01 02:02:44 +02:00
NotificationChannel messages = new NotificationChannel(Notifications.NOTIFICATION_CHANNEL__NEW_MESSAGE,
2019-09-23 23:55:57 +02:00
mName, NotificationManager.IMPORTANCE_DEFAULT);
2019-09-01 02:02:44 +02:00
messages.setDescription(mDescription);
notificationManager.createNotificationChannel(messages);
2019-05-13 03:19:17 +02:00
}
public AppComponent getAppComponent() {
return appComponent;
}
2019-08-30 15:11:03 +02:00
private final AbstractActivityLifecycleCallbacks lifecycleCallbacks = new AbstractActivityLifecycleCallbacks() {
@Override
public void onActivityStarted(Activity activity) {
if (activityReferences.incrementAndGet() == 1 && !isActivityChangingConfiguration.get()) {
// App enters foreground
2019-11-22 23:35:14 +01:00
//connectionCenter.clientStateActive();
2019-08-30 15:11:03 +02:00
}
}
@Override
public void onActivityStopped(Activity activity) {
isActivityChangingConfiguration.set(activity.isChangingConfigurations());
if (activityReferences.decrementAndGet() == 0 && !isActivityChangingConfiguration.get()) {
// App enters background
2019-11-22 23:35:14 +01:00
//connectionCenter.clientStateInactive();
2019-08-30 15:11:03 +02:00
}
}
};
2019-05-13 03:19:17 +02:00
}