Add comments and improve readability

This commit is contained in:
Paul Schaub 2019-08-31 18:56:18 +02:00
parent 9fc62b25f2
commit 1dd669ef14
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
1 changed files with 65 additions and 45 deletions

View File

@ -30,6 +30,7 @@ import javax.inject.Inject;
import javax.inject.Singleton;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
@ -82,13 +83,13 @@ public class ConnectionCenter {
}
// otherwise subscribe to accounts and create connections.
disposable.add(
accountRepository.getAllAccounts()
Disposable allAccounts = accountRepository.getAllAccounts()
.observeOn(Schedulers.io())
.subscribeOn(Schedulers.computation())
.subscribe((Consumer<List<? extends AccountModel>>) accounts -> {
LOGGER.log(Level.INFO, "Accounts changed.");
Set<Long> accountIds = new HashSet<>();
// Add missing connections to the map
for (AccountModel account : accounts) {
accountIds.add(account.getId());
@ -100,6 +101,7 @@ public class ConnectionCenter {
MercuryConnection connection = createConnection(account);
connectionMap.put(account.getId(), connection);
// initialize new connection
RosterStore rosterStore = new RosterStore(rosterRepository);
rosterStore.setAccountId(account.getId());
rosterStore.subscribe();
@ -125,7 +127,10 @@ public class ConnectionCenter {
connectionMap.remove(connectionId);
}
}
}));
});
disposable.add(allAccounts);
}
public MercuryConnection getConnection(AccountModel account) {
@ -140,6 +145,15 @@ public class ConnectionCenter {
connectionMap.put(connection.getAccountId(), connection);
}
/**
* Create a new {@link MercuryConnection} with an underlying {@link XMPPTCPConnection}
* from the credentials of an {@link AccountModel}.
* The new connection will not be connected.
*
* @param accountModel accountModel
*
* @return disconnected mercury connection
*/
public MercuryConnection createConnection(AccountModel accountModel) {
LOGGER.log(Level.INFO, "Create Connection for " + accountModel.getJid().toString());
XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder()
@ -154,6 +168,11 @@ public class ConnectionCenter {
return new MercuryConnection(tcpConnection, accountModel.getId());
}
/**
* Set Client State Indication status to active.
*
* @see <a href="https://xmpp.org/extensions/xep-0352.html">XEP-0352: Client State Indication</a>
*/
public void clientStateActive() {
LOGGER.log(Level.INFO, "CSI: App is going to foreground -> active");
for (MercuryConnection mercuryConnection : connectionMap.values()) {
@ -161,15 +180,18 @@ public class ConnectionCenter {
if (connection.isConnected() && ClientStateIndicationManager.isSupported(connection)) {
try {
ClientStateIndicationManager.active(mercuryConnection.getConnection());
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (SmackException.NotConnectedException | InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* Set Client State Indication status to inactive.
*
* @see <a href="https://xmpp.org/extensions/xep-0352.html">XEP-0352: Client State Indication</a>
*/
public void clientStateInactive() {
LOGGER.log(Level.INFO, "CSI: App is going to background -> inactive");
for (MercuryConnection mercuryConnection : connectionMap.values()) {
@ -177,9 +199,7 @@ public class ConnectionCenter {
if (connection.isConnected() && ClientStateIndicationManager.isSupported(connection)) {
try {
ClientStateIndicationManager.inactive(connection);
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
} catch (SmackException.NotConnectedException | InterruptedException e) {
e.printStackTrace();
}
}