Improve last commit
This commit is contained in:
parent
749c4ec89c
commit
7d1714e47a
|
@ -3,9 +3,8 @@ package org.mercury_im.messenger;
|
|||
import android.app.Application;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
|
||||
import org.mercury_im.messenger.data.repository.Repositories;
|
||||
import org.mercury_im.messenger.data.repository.AccountRepository;
|
||||
import org.mercury_im.messenger.di.component.AppComponent;
|
||||
import org.mercury_im.messenger.di.component.DaggerAppComponent;
|
||||
import org.mercury_im.messenger.di.module.AppModule;
|
||||
|
@ -32,7 +31,7 @@ public class MercuryImApplication extends Application {
|
|||
private final CompositeDisposable disposable = new CompositeDisposable();
|
||||
|
||||
@Inject
|
||||
Repositories repositories;
|
||||
AccountRepository accountRepository;
|
||||
|
||||
public static MercuryImApplication getApplication() {
|
||||
return INSTANCE;
|
||||
|
@ -49,7 +48,7 @@ public class MercuryImApplication extends Application {
|
|||
registerActivityLifecycleCallbacks(clientStateHandler);
|
||||
Notifications.initializeNotificationChannels(this);
|
||||
|
||||
observeAccounts();
|
||||
subscribeForegroundServiceToActiveAccounts();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -69,31 +68,26 @@ public class MercuryImApplication extends Application {
|
|||
return appComponent;
|
||||
}
|
||||
|
||||
private void observeAccounts() {
|
||||
disposable.add(repositories.getAccountRepository().observeAllAccounts()
|
||||
private void subscribeForegroundServiceToActiveAccounts() {
|
||||
disposable.add(accountRepository.observeAllAccounts()
|
||||
.map(this::listContainsActiveAccount)
|
||||
.distinctUntilChanged()
|
||||
.subscribe(foregroundServiceNecessary -> {
|
||||
if (foregroundServiceNecessary) {
|
||||
Log.d("MercuryIM", "Enable foreground service.");
|
||||
.subscribe(foregroundServiceNeeded -> {
|
||||
if (foregroundServiceNeeded) {
|
||||
startForegroundService();
|
||||
} else {
|
||||
Log.d("MercuryIM", "Disable foreground service.");
|
||||
stopForegroundService();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
private boolean listContainsActiveAccount(List<Account> accounts) {
|
||||
Log.d("MercuryIM", "listContainsActiveAccount size: " + accounts.size());
|
||||
boolean containsActiveAccount = false;
|
||||
for (Account account : accounts) {
|
||||
if (account.isEnabled()) {
|
||||
containsActiveAccount = true;
|
||||
break;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return containsActiveAccount;
|
||||
return false;
|
||||
}
|
||||
|
||||
private void startForegroundService() {
|
||||
|
|
Loading…
Reference in New Issue