2019-05-27 21:34:11 +02:00
|
|
|
package org.mercury_im.messenger.xmpp_core;
|
|
|
|
|
2019-06-21 03:45:33 +02:00
|
|
|
import org.jivesoftware.smack.ConnectionListener;
|
2019-05-27 21:34:11 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
2019-06-21 03:45:33 +02:00
|
|
|
import org.jivesoftware.smack.chat2.ChatManager;
|
2019-05-27 21:34:11 +02:00
|
|
|
import org.jivesoftware.smack.roster.Roster;
|
2019-06-30 13:37:36 +02:00
|
|
|
import org.jivesoftware.smack.sm.packet.StreamManagement;
|
|
|
|
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
|
2019-06-21 03:45:33 +02:00
|
|
|
import org.jivesoftware.smackx.carbons.CarbonManager;
|
2019-06-30 13:37:36 +02:00
|
|
|
import org.jivesoftware.smackx.csi.ClientStateIndicationManager;
|
|
|
|
import org.jivesoftware.smackx.sid.StableUniqueStanzaIdManager;
|
2019-06-24 01:41:17 +02:00
|
|
|
|
|
|
|
import java.util.logging.Logger;
|
2019-05-27 21:34:11 +02:00
|
|
|
|
2019-06-23 02:34:14 +02:00
|
|
|
public abstract class MercuryConnection implements ConnectionListener {
|
2019-06-21 03:45:33 +02:00
|
|
|
|
|
|
|
public static final String TAG = "Mercury";
|
2019-05-27 21:34:11 +02:00
|
|
|
|
2019-06-24 01:41:17 +02:00
|
|
|
private static final Logger LOGGER = Logger.getLogger(MercuryConnection.class.getName());
|
|
|
|
|
2019-06-10 02:52:08 +02:00
|
|
|
protected final XMPPConnection connection;
|
|
|
|
protected final long accountId;
|
2019-06-21 03:45:33 +02:00
|
|
|
|
2019-06-10 02:52:08 +02:00
|
|
|
protected final Roster roster;
|
2019-06-21 03:45:33 +02:00
|
|
|
protected final ChatManager chatManager;
|
|
|
|
protected final CarbonManager carbonManager;
|
2019-06-30 13:37:36 +02:00
|
|
|
protected final StableUniqueStanzaIdManager stanzaIdManager;
|
|
|
|
|
2019-06-23 02:34:14 +02:00
|
|
|
protected ConnectionStateHolder stateHolder;
|
2019-05-27 21:34:11 +02:00
|
|
|
|
|
|
|
public MercuryConnection(XMPPConnection connection, long accountId) {
|
|
|
|
this.connection = connection;
|
2019-06-24 01:41:17 +02:00
|
|
|
connection.addConnectionListener(this);
|
2019-05-27 21:34:11 +02:00
|
|
|
this.accountId = accountId;
|
|
|
|
|
|
|
|
this.roster = Roster.getInstanceFor(connection);
|
2019-06-21 03:45:33 +02:00
|
|
|
this.chatManager = ChatManager.getInstanceFor(connection);
|
|
|
|
this.carbonManager = CarbonManager.getInstanceFor(connection);
|
2019-06-30 13:37:36 +02:00
|
|
|
this.stanzaIdManager = StableUniqueStanzaIdManager.getInstanceFor(connection);
|
|
|
|
stanzaIdManager.enable();
|
|
|
|
|
2019-06-21 03:45:33 +02:00
|
|
|
|
2019-05-27 21:34:11 +02:00
|
|
|
roster.setRosterLoadedAtLogin(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public XMPPConnection getConnection() {
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRosterHandler(RosterHandler handler) {
|
|
|
|
roster.addRosterListener(handler);
|
|
|
|
roster.addRosterLoadedListener(handler);
|
|
|
|
}
|
|
|
|
|
2019-06-21 03:45:33 +02:00
|
|
|
public void setPlainMessageHandler(PlainMessageHandler plainMessageHandler) {
|
|
|
|
carbonManager.addCarbonCopyReceivedListener(plainMessageHandler);
|
|
|
|
chatManager.addIncomingListener(plainMessageHandler);
|
|
|
|
chatManager.addOutgoingListener(plainMessageHandler);
|
|
|
|
}
|
|
|
|
|
2019-06-23 02:34:14 +02:00
|
|
|
public abstract ConnectionStateHolder getState();
|
|
|
|
|
2019-05-27 21:34:11 +02:00
|
|
|
public long getAccountId() {
|
|
|
|
return accountId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Roster getRoster() {
|
|
|
|
return roster;
|
|
|
|
}
|
2019-06-21 03:45:33 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void connected(XMPPConnection connection) {
|
2019-06-23 02:34:14 +02:00
|
|
|
getState().updateConnectionState(ConnectionState.CONNECTED);
|
2019-06-21 03:45:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void authenticated(XMPPConnection connection, boolean resumed) {
|
|
|
|
if (connection == this.connection && !resumed) {
|
2019-06-24 01:41:17 +02:00
|
|
|
LOGGER.info("Enabling carbons!");
|
|
|
|
carbonManager.enableCarbonsAsync(exception -> {
|
|
|
|
LOGGER.severe("Could not enable carbons for connection " + accountId + ": " + exception.getMessage());
|
|
|
|
exception.printStackTrace();
|
|
|
|
});
|
2019-06-21 03:45:33 +02:00
|
|
|
}
|
2019-06-23 02:34:14 +02:00
|
|
|
getState().updateConnectionState(ConnectionState.CONNECTED);
|
2019-06-21 03:45:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void connectionClosed() {
|
2019-06-24 01:41:17 +02:00
|
|
|
LOGGER.fine("Connection closed.");
|
2019-06-23 02:34:14 +02:00
|
|
|
getState().updateConnectionState(ConnectionState.DISCONNECTED);
|
2019-06-21 03:45:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void connectionClosedOnError(Exception e) {
|
2019-06-24 01:41:17 +02:00
|
|
|
LOGGER.severe("Connection closed on error: " + e.getMessage());
|
|
|
|
e.printStackTrace();
|
2019-06-23 02:34:14 +02:00
|
|
|
getState().updateConnectionState(ConnectionState.DISCONNECTED);
|
2019-06-21 03:45:33 +02:00
|
|
|
}
|
2019-05-27 21:34:11 +02:00
|
|
|
}
|