mirror of
https://codeberg.org/Mercury-IM/Mercury-IM
synced 2024-06-16 00:24:51 +02:00
Setup CSI again
This commit is contained in:
parent
7d1714e47a
commit
81aab30d59
|
@ -33,6 +33,9 @@ public class MercuryImApplication extends Application {
|
||||||
@Inject
|
@Inject
|
||||||
AccountRepository accountRepository;
|
AccountRepository accountRepository;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Messenger messenger;
|
||||||
|
|
||||||
public static MercuryImApplication getApplication() {
|
public static MercuryImApplication getApplication() {
|
||||||
return INSTANCE;
|
return INSTANCE;
|
||||||
}
|
}
|
||||||
|
@ -45,12 +48,18 @@ public class MercuryImApplication extends Application {
|
||||||
appComponent = createAppComponent();
|
appComponent = createAppComponent();
|
||||||
appComponent.inject(this);
|
appComponent.inject(this);
|
||||||
|
|
||||||
registerActivityLifecycleCallbacks(clientStateHandler);
|
setupClientStateIndication();
|
||||||
|
|
||||||
Notifications.initializeNotificationChannels(this);
|
Notifications.initializeNotificationChannels(this);
|
||||||
|
|
||||||
subscribeForegroundServiceToActiveAccounts();
|
subscribeForegroundServiceToActiveAccounts();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setupClientStateIndication() {
|
||||||
|
clientStateHandler.addClientStateListener(messenger);
|
||||||
|
registerActivityLifecycleCallbacks(clientStateHandler);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create the Dependency Injection graph.
|
* Create the Dependency Injection graph.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package org.mercury_im.messenger;
|
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.data.repository.Repositories;
|
||||||
import org.mercury_im.messenger.entity.Account;
|
import org.mercury_im.messenger.entity.Account;
|
||||||
import org.mercury_im.messenger.usecase.AddAccount;
|
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.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
public class Messenger {
|
public class Messenger implements ClientStateListener {
|
||||||
|
|
||||||
public static final String TAG = "MercuryIM";
|
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 final Map<Long, MercuryConnection> connections = new HashMap<>();
|
||||||
private Repositories repositories;
|
private Repositories repositories;
|
||||||
|
@ -33,4 +39,38 @@ public class Messenger {
|
||||||
public AddAccount addAccount() {
|
public AddAccount addAccount() {
|
||||||
return new AddAccount(repositories.getAccountRepository(), this);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue