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 javax.inject.Singleton;
import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer; import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers; import io.reactivex.schedulers.Schedulers;
@ -82,50 +83,54 @@ public class ConnectionCenter {
} }
// otherwise subscribe to accounts and create connections. // otherwise subscribe to accounts and create connections.
disposable.add( Disposable allAccounts = accountRepository.getAllAccounts()
accountRepository.getAllAccounts() .observeOn(Schedulers.io())
.observeOn(Schedulers.io()) .subscribeOn(Schedulers.computation())
.subscribeOn(Schedulers.computation()) .subscribe((Consumer<List<? extends AccountModel>>) accounts -> {
.subscribe((Consumer<List<? extends AccountModel>>) accounts -> { LOGGER.log(Level.INFO, "Accounts changed.");
LOGGER.log(Level.INFO, "Accounts changed."); Set<Long> accountIds = new HashSet<>();
Set<Long> accountIds = new HashSet<>();
// Add missing connections to the map
for (AccountModel account : accounts) {
accountIds.add(account.getId());
if (connectionMap.get(account.getId()) != null) {
continue;
}
LOGGER.log(Level.INFO, "Add new connection " + account.getJid().toString() + " to ConnectionCenter list."); // Add missing connections to the map
MercuryConnection connection = createConnection(account); for (AccountModel account : accounts) {
connectionMap.put(account.getId(), connection); accountIds.add(account.getId());
if (connectionMap.get(account.getId()) != null) {
continue;
}
RosterStore rosterStore = new RosterStore(rosterRepository); LOGGER.log(Level.INFO, "Add new connection " + account.getJid().toString() + " to ConnectionCenter list.");
rosterStore.setAccountId(account.getId()); MercuryConnection connection = createConnection(account);
rosterStore.subscribe(); connectionMap.put(account.getId(), connection);
connection.getRoster().setRosterStore(rosterStore);
messageStore.registerForMercuryConnection(connection); // initialize new connection
RosterStore rosterStore = new RosterStore(rosterRepository);
rosterStore.setAccountId(account.getId());
rosterStore.subscribe();
connection.getRoster().setRosterStore(rosterStore);
if (account.getEnabled()) { messageStore.registerForMercuryConnection(connection);
LOGGER.log(Level.INFO, "Connecting...");
connection.connect(); if (account.getEnabled()) {
LOGGER.log(Level.INFO, "Connected!"); LOGGER.log(Level.INFO, "Connecting...");
} else { connection.connect();
LOGGER.log(Level.INFO, "Account not enabled. Skip."); LOGGER.log(Level.INFO, "Connected!");
} } else {
} LOGGER.log(Level.INFO, "Account not enabled. Skip.");
}
}
// Remove unwanted connections from the map
for (long connectionId : connectionMap.keySet()) {
if (!accountIds.contains(connectionId)) {
AbstractXMPPConnection con =
(AbstractXMPPConnection) connectionMap.get(connectionId).getConnection();
con.disconnect();
connectionMap.remove(connectionId);
}
}
});
disposable.add(allAccounts);
// Remove unwanted connections from the map
for (long connectionId : connectionMap.keySet()) {
if (!accountIds.contains(connectionId)) {
AbstractXMPPConnection con =
(AbstractXMPPConnection) connectionMap.get(connectionId).getConnection();
con.disconnect();
connectionMap.remove(connectionId);
}
}
}));
} }
public MercuryConnection getConnection(AccountModel account) { public MercuryConnection getConnection(AccountModel account) {
@ -140,6 +145,15 @@ public class ConnectionCenter {
connectionMap.put(connection.getAccountId(), connection); 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) { public MercuryConnection createConnection(AccountModel accountModel) {
LOGGER.log(Level.INFO, "Create Connection for " + accountModel.getJid().toString()); LOGGER.log(Level.INFO, "Create Connection for " + accountModel.getJid().toString());
XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder() XMPPTCPConnectionConfiguration configuration = XMPPTCPConnectionConfiguration.builder()
@ -154,6 +168,11 @@ public class ConnectionCenter {
return new MercuryConnection(tcpConnection, accountModel.getId()); 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() { public void clientStateActive() {
LOGGER.log(Level.INFO, "CSI: App is going to foreground -> active"); LOGGER.log(Level.INFO, "CSI: App is going to foreground -> active");
for (MercuryConnection mercuryConnection : connectionMap.values()) { for (MercuryConnection mercuryConnection : connectionMap.values()) {
@ -161,15 +180,18 @@ public class ConnectionCenter {
if (connection.isConnected() && ClientStateIndicationManager.isSupported(connection)) { if (connection.isConnected() && ClientStateIndicationManager.isSupported(connection)) {
try { try {
ClientStateIndicationManager.active(mercuryConnection.getConnection()); ClientStateIndicationManager.active(mercuryConnection.getConnection());
} catch (SmackException.NotConnectedException e) { } catch (SmackException.NotConnectedException | InterruptedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace(); 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() { public void clientStateInactive() {
LOGGER.log(Level.INFO, "CSI: App is going to background -> inactive"); LOGGER.log(Level.INFO, "CSI: App is going to background -> inactive");
for (MercuryConnection mercuryConnection : connectionMap.values()) { for (MercuryConnection mercuryConnection : connectionMap.values()) {
@ -177,9 +199,7 @@ public class ConnectionCenter {
if (connection.isConnected() && ClientStateIndicationManager.isSupported(connection)) { if (connection.isConnected() && ClientStateIndicationManager.isSupported(connection)) {
try { try {
ClientStateIndicationManager.inactive(connection); ClientStateIndicationManager.inactive(connection);
} catch (SmackException.NotConnectedException e) { } catch (SmackException.NotConnectedException | InterruptedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }