Setup CSI again

This commit is contained in:
Paul Schaub 2019-12-21 03:24:00 +01:00
parent 7d1714e47a
commit 81aab30d59
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 51 additions and 2 deletions

View File

@ -33,6 +33,9 @@ public class MercuryImApplication extends Application {
@Inject
AccountRepository accountRepository;
@Inject
Messenger messenger;
public static MercuryImApplication getApplication() {
return INSTANCE;
}
@ -45,12 +48,18 @@ public class MercuryImApplication extends Application {
appComponent = createAppComponent();
appComponent.inject(this);
registerActivityLifecycleCallbacks(clientStateHandler);
setupClientStateIndication();
Notifications.initializeNotificationChannels(this);
subscribeForegroundServiceToActiveAccounts();
}
private void setupClientStateIndication() {
clientStateHandler.addClientStateListener(messenger);
registerActivityLifecycleCallbacks(clientStateHandler);
}
/**
* Create the Dependency Injection graph.
*/

View File

@ -1,5 +1,8 @@
package org.mercury_im.messenger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smackx.csi.ClientStateIndicationManager;
import org.mercury_im.messenger.data.repository.Repositories;
import org.mercury_im.messenger.entity.Account;
import org.mercury_im.messenger.usecase.AddAccount;
@ -7,12 +10,15 @@ import org.mercury_im.messenger.xmpp.MercuryConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
public class Messenger {
public class Messenger implements ClientStateListener {
public static final String TAG = "MercuryIM";
private static final Logger LOGGER = Logger.getLogger(Messenger.class.getName());
private final Map<Long, MercuryConnection> connections = new HashMap<>();
private Repositories repositories;
@ -33,4 +39,38 @@ public class Messenger {
public AddAccount addAccount() {
return new AddAccount(repositories.getAccountRepository(), this);
}
// 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);
}
}
}