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

121 lines
3.8 KiB
Java
Raw Normal View History

2020-06-06 18:45:20 +02:00
package org.mercury_im.messenger.android;
2019-05-13 03:19:17 +02:00
import android.app.Application;
import android.content.Intent;
import android.os.Build;
import org.jivesoftware.smack.android.AndroidSmackInitializer;
2020-07-09 02:10:47 +02:00
import org.jivesoftware.smackx.ping.android.ServerPingWithAlarmManager;
2020-06-06 18:45:20 +02:00
import org.mercury_im.messenger.android.di.component.DaggerAppComponent;
2020-07-09 02:10:47 +02:00
import org.mercury_im.messenger.android.util.AndroidLoggingHandler;
2020-06-06 18:45:20 +02:00
import org.mercury_im.messenger.core.Messenger;
import org.mercury_im.messenger.core.data.repository.AccountRepository;
import org.mercury_im.messenger.android.di.component.AppComponent;
import org.mercury_im.messenger.android.di.module.AppModule;
import org.mercury_im.messenger.entity.Account;
import org.mercury_im.messenger.android.service.MercuryForegroundService;
2020-07-18 12:47:52 +02:00
import org.mercury_im.messenger.core.connection.CsiManager;
2019-05-13 03:19:17 +02:00
import java.util.List;
import javax.inject.Inject;
import io.reactivex.disposables.CompositeDisposable;
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
private static MercuryImApplication INSTANCE;
private AppComponent appComponent;
private ClientStateHandler clientStateHandler = new ClientStateHandler();
private final CompositeDisposable disposable = new CompositeDisposable();
@Inject
2019-12-21 03:13:00 +01:00
AccountRepository accountRepository;
2019-08-30 15:11:03 +02:00
2019-12-21 03:24:00 +01:00
@Inject
Messenger messenger;
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() {
super.onCreate();
AndroidSmackInitializer.initialize(getApplicationContext());
2020-07-09 02:10:47 +02:00
AndroidLoggingHandler.reset(new AndroidLoggingHandler());
2019-05-13 03:19:17 +02:00
INSTANCE = this;
2019-08-03 19:05:50 +02:00
appComponent = createAppComponent();
appComponent.inject(this);
2019-08-03 19:05:50 +02:00
2019-12-21 03:24:00 +01:00
setupClientStateIndication();
Notifications.initializeNotificationChannels(this);
2019-05-13 03:19:17 +02:00
2019-12-21 03:13:00 +01:00
subscribeForegroundServiceToActiveAccounts();
2019-05-13 03:19:17 +02:00
}
2019-12-21 03:24:00 +01:00
private void setupClientStateIndication() {
2020-05-11 16:31:07 +02:00
clientStateHandler.addClientStateListener(new CsiManager(messenger.getConnectionManager()));
2019-12-21 03:24:00 +01:00
registerActivityLifecycleCallbacks(clientStateHandler);
}
2019-08-03 19:05:50 +02:00
/**
* Create the Dependency Injection graph.
*/
private AppComponent createAppComponent() {
2019-08-03 19:05:50 +02:00
AppComponent appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
appComponent.inject(this);
return appComponent;
}
2019-05-13 03:19:17 +02:00
public AppComponent getAppComponent() {
return appComponent;
}
2019-08-30 15:11:03 +02:00
2019-12-21 03:13:00 +01:00
private void subscribeForegroundServiceToActiveAccounts() {
disposable.add(accountRepository.observeAllAccounts()
.map(this::listContainsActiveAccount)
.distinctUntilChanged()
2019-12-21 03:13:00 +01:00
.subscribe(foregroundServiceNeeded -> {
if (foregroundServiceNeeded) {
startForegroundService();
} else {
stopForegroundService();
}
}));
}
private boolean listContainsActiveAccount(List<Account> accounts) {
for (Account account : accounts) {
if (account.isEnabled()) {
2019-12-21 03:13:00 +01:00
return true;
}
}
2019-12-21 03:13:00 +01:00
return false;
}
private void startForegroundService() {
Intent startIntent = new Intent(getApplicationContext(), MercuryForegroundService.class);
startIntent.setAction(MercuryForegroundService.ACTION_START);
if (Build.VERSION.SDK_INT < 26) {
startService(startIntent);
} else {
startForegroundService(startIntent);
2019-08-30 15:11:03 +02:00
}
}
private void stopForegroundService() {
Intent stopIntent = new Intent(getApplicationContext(), MercuryForegroundService.class);
stopIntent.setAction(MercuryForegroundService.ACTION_STOP);
startService(stopIntent);
}
2019-05-13 03:19:17 +02:00
}