2019-11-18 23:51:27 +01:00
|
|
|
package org.mercury_im.messenger;
|
2019-11-08 00:47:17 +01:00
|
|
|
|
2019-12-21 03:24:00 +01:00
|
|
|
import org.jivesoftware.smack.SmackException;
|
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2019-12-21 05:34:19 +01:00
|
|
|
import org.jivesoftware.smack.roster.Roster;
|
2019-12-21 03:24:00 +01:00
|
|
|
import org.jivesoftware.smackx.csi.ClientStateIndicationManager;
|
2019-12-09 15:49:59 +01:00
|
|
|
import org.mercury_im.messenger.data.repository.Repositories;
|
2019-11-18 23:51:27 +01:00
|
|
|
import org.mercury_im.messenger.entity.Account;
|
2019-12-21 05:34:19 +01:00
|
|
|
import org.mercury_im.messenger.store.MercuryRosterStore;
|
2019-12-21 00:27:48 +01:00
|
|
|
import org.mercury_im.messenger.usecase.AddAccount;
|
2019-12-22 05:48:07 +01:00
|
|
|
import org.mercury_im.messenger.usecase.ConnectAccountsOnStartup;
|
2019-12-21 00:27:48 +01:00
|
|
|
import org.mercury_im.messenger.xmpp.MercuryConnection;
|
2019-11-12 00:07:57 +01:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2019-12-21 16:30:14 +01:00
|
|
|
import java.util.UUID;
|
2019-12-21 03:24:00 +01:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2019-11-12 00:07:57 +01:00
|
|
|
|
2019-12-02 02:31:32 +01:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-12-22 05:48:07 +01:00
|
|
|
import io.reactivex.Scheduler;
|
|
|
|
import io.reactivex.disposables.CompositeDisposable;
|
|
|
|
import io.reactivex.schedulers.Schedulers;
|
|
|
|
|
2019-12-21 03:24:00 +01:00
|
|
|
public class Messenger implements ClientStateListener {
|
2019-11-12 00:07:57 +01:00
|
|
|
|
2019-12-09 13:50:26 +01:00
|
|
|
public static final String TAG = "MercuryIM";
|
2019-12-21 03:24:00 +01:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(Messenger.class.getName());
|
2019-12-09 13:50:26 +01:00
|
|
|
|
2019-12-21 16:30:14 +01:00
|
|
|
private final Map<UUID, MercuryConnection> connections = new HashMap<>();
|
2019-12-21 00:27:48 +01:00
|
|
|
private Repositories repositories;
|
2019-12-02 02:31:32 +01:00
|
|
|
|
2019-12-22 05:48:07 +01:00
|
|
|
private CompositeDisposable disposable = new CompositeDisposable();
|
|
|
|
|
2019-12-02 02:31:32 +01:00
|
|
|
@Inject
|
2019-12-09 15:49:59 +01:00
|
|
|
public Messenger(Repositories repositories) {
|
|
|
|
this.repositories = repositories;
|
2019-12-22 05:48:07 +01:00
|
|
|
initialLogin();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void initialLogin() {
|
|
|
|
disposable.add(repositories.getAccountRepository().observeAllAccounts().firstOrError()
|
|
|
|
.subscribeOn(Schedulers.newThread())
|
|
|
|
.subscribe(initialAccounts -> ConnectAccountsOnStartup
|
|
|
|
.with(this, initialAccounts)
|
|
|
|
.execute()));
|
2019-12-02 02:31:32 +01:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public void addConnection(MercuryConnection connection) {
|
2019-11-12 00:07:57 +01:00
|
|
|
connections.put(connection.getAccount().getId(), connection);
|
2019-12-21 05:34:19 +01:00
|
|
|
initRosterStore(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initRosterStore(MercuryConnection connection) {
|
|
|
|
Roster.getInstanceFor(connection.getConnection())
|
|
|
|
.setRosterStore(new MercuryRosterStore(connection.getAccount(),
|
|
|
|
repositories.getPeerRepository(), repositories.getAccountRepository()));
|
2019-11-12 00:07:57 +01:00
|
|
|
}
|
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public MercuryConnection getConnection(Account account) {
|
2019-11-18 00:04:28 +01:00
|
|
|
return connections.get(account.getId());
|
|
|
|
}
|
2019-12-09 13:50:26 +01:00
|
|
|
|
2019-12-21 00:27:48 +01:00
|
|
|
public AddAccount addAccount() {
|
|
|
|
return new AddAccount(repositories.getAccountRepository(), this);
|
2019-12-09 13:50:26 +01:00
|
|
|
}
|
2019-12-21 03:24:00 +01:00
|
|
|
|
|
|
|
// CSI
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClientInForeground() {
|
|
|
|
LOGGER.log(Level.INFO, "CSI: active");
|
|
|
|
for (MercuryConnection connection : connections.values()) {
|
|
|
|
tryCsiActive(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClientInBackground() {
|
|
|
|
LOGGER.log(Level.INFO, "CSI: inactive");
|
|
|
|
for (MercuryConnection connection : connections.values()) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 00:47:17 +01:00
|
|
|
}
|