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

118 lines
3.4 KiB
Java
Raw Normal View History

2019-05-13 03:19:17 +02:00
package org.mercury_im.messenger;
import android.app.Application;
import android.content.Intent;
import android.os.Build;
2019-12-21 03:13:00 +01:00
import org.mercury_im.messenger.data.repository.AccountRepository;
2019-05-13 03:19:17 +02:00
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;
import org.mercury_im.messenger.entity.Account;
2019-12-09 13:50:26 +01:00
import org.mercury_im.messenger.service.MercuryConnectionService;
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
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;
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();
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() {
clientStateHandler.addClientStateListener(messenger);
registerActivityLifecycleCallbacks(clientStateHandler);
}
2019-08-03 19:05:50 +02:00
/**
* Create the Dependency Injection graph.
*/
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 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(), MercuryConnectionService.class);
startIntent.setAction(MercuryConnectionService.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(), MercuryConnectionService.class);
stopIntent.setAction(MercuryConnectionService.ACTION_STOP);
startService(stopIntent);
}
2019-05-13 03:19:17 +02:00
}