Added presence control, MAM for all messages

This commit is contained in:
ChronosX88 2019-05-24 12:48:49 +04:00
parent 2a29188fed
commit 0168cc5ad8
3 changed files with 22 additions and 0 deletions

View File

@ -82,6 +82,7 @@ dependencies {
implementation 'org.greenrobot:eventbus:3.1.1' implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'net.sourceforge.streamsupport:android-retrofuture:1.7.0' implementation 'net.sourceforge.streamsupport:android-retrofuture:1.7.0'
implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
implementation 'org.igniterealtime.smack:smack-experimental:4.3.3'
} }
repositories { repositories {
mavenCentral() mavenCentral()

View File

@ -31,10 +31,12 @@ import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat2.Chat; import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.chat2.ChatManager; import org.jivesoftware.smack.chat2.ChatManager;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.roster.Roster; import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry; import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.tcp.XMPPTCPConnection; import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration; import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smackx.mam.MamManager;
import org.jivesoftware.smackx.vcardtemp.VCardManager; import org.jivesoftware.smackx.vcardtemp.VCardManager;
import org.jxmpp.jid.EntityBareJid; import org.jxmpp.jid.EntityBareJid;
@ -53,6 +55,7 @@ public class XMPPConnection implements ConnectionListener {
private NetworkHandler networkHandler; private NetworkHandler networkHandler;
private Context context; private Context context;
private Roster roster; private Roster roster;
private MamManager mamManager;
public enum ConnectionState { public enum ConnectionState {
CONNECTED, CONNECTED,
@ -113,6 +116,16 @@ public class XMPPConnection implements ConnectionListener {
ReconnectionManager.setEnabledPerDefault(true); ReconnectionManager.setEnabledPerDefault(true);
reconnectionManager.enableAutomaticReconnection(); reconnectionManager.enableAutomaticReconnection();
roster = roster.getInstanceFor(connection); roster = roster.getInstanceFor(connection);
roster.setSubscriptionMode(Roster.SubscriptionMode.accept_all);
mamManager = MamManager.getInstanceFor(connection);
try {
if(mamManager.isSupported()) {
MamManager.getInstanceFor(connection).enableMamForAllMessages();
}
connection.sendStanza(new Presence(Presence.Type.available));
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
} }

View File

@ -27,6 +27,7 @@ import android.os.Looper;
import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.EventBus;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import java.io.IOException; import java.io.IOException;
@ -74,6 +75,13 @@ public class XMPPConnectionService extends Service {
if(connection != null) { if(connection != null) {
thread.interrupt(); thread.interrupt();
thread = null; thread = null;
try {
connection.getConnection().sendStanza(new Presence(Presence.Type.unavailable));
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
connection.disconnect(); connection.disconnect();
connection = null; connection = null;
} }