87 lines
2.9 KiB
Java
87 lines
2.9 KiB
Java
package org.mercury_im.messenger;
|
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
import org.jivesoftware.smackx.csi.ClientStateIndicationManager;
|
|
import org.mercury_im.messenger.data.repository.Repositories;
|
|
import org.mercury_im.messenger.usecase.AddAccount;
|
|
import org.mercury_im.messenger.usecase.RosterStoreBinder;
|
|
import org.mercury_im.messenger.xmpp.MercuryConnection;
|
|
import org.mercury_im.messenger.xmpp.MercuryConnectionManager;
|
|
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
|
|
import javax.inject.Inject;
|
|
import javax.inject.Singleton;
|
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
@Singleton
|
|
public class Messenger implements ClientStateListener {
|
|
|
|
public static final String TAG = "MercuryIM";
|
|
private static final Logger LOGGER = Logger.getLogger(Messenger.class.getName());
|
|
|
|
private final MercuryConnectionManager connectionManager;
|
|
private final Repositories repositories;
|
|
|
|
private CompositeDisposable disposable = new CompositeDisposable();
|
|
|
|
@Inject
|
|
public Messenger(Repositories repositories, MercuryConnectionManager connectionManager) {
|
|
this.repositories = repositories;
|
|
this.connectionManager = connectionManager;
|
|
performInitialLogin();
|
|
}
|
|
|
|
public MercuryConnectionManager getConnectionManager() {
|
|
return connectionManager;
|
|
}
|
|
|
|
public void performInitialLogin() {
|
|
LOGGER.log(Level.INFO, "Perform initial login.");
|
|
disposable.add(repositories.getAccountRepository().observeAllAccounts().firstOrError()
|
|
.subscribeOn(Schedulers.newThread())
|
|
.subscribe(connectionManager::registerConnections));
|
|
}
|
|
|
|
public AddAccount addAccount() {
|
|
return new AddAccount(repositories.getAccountRepository(), this.connectionManager);
|
|
}
|
|
|
|
// CSI
|
|
|
|
@Override
|
|
public void onClientInForeground() {
|
|
LOGGER.log(Level.INFO, "CSI: active");
|
|
for (MercuryConnection connection : connectionManager.getConnections()) {
|
|
tryCsiActive(connection);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onClientInBackground() {
|
|
LOGGER.log(Level.INFO, "CSI: inactive");
|
|
for (MercuryConnection connection : connectionManager.getConnections()) {
|
|
tryCsiInactive(connection);
|
|
}
|
|
}
|
|
|
|
private void tryCsiActive(MercuryConnection connection) {
|
|
try {
|
|
ClientStateIndicationManager.active(connection.getConnection());
|
|
} catch (SmackException.NotConnectedException | InterruptedException e) {
|
|
LOGGER.log(Level.WARNING, "Sending CSI state 'active' failed.", e);
|
|
}
|
|
}
|
|
|
|
private void tryCsiInactive(MercuryConnection connection) {
|
|
try {
|
|
ClientStateIndicationManager.inactive(connection.getConnection());
|
|
} catch (SmackException.NotConnectedException | InterruptedException e) {
|
|
LOGGER.log(Level.WARNING, "Sending CSI state 'inactive' failed.", e);
|
|
}
|
|
}
|
|
}
|