From 164ac450d49705ddfa4aaa051eaeaec10868221f Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 23 Jan 2023 18:01:55 +0100 Subject: [PATCH] introduce CarbonsManager to enable and maintain carbon state --- .../conversations/android/xmpp/Managers.java | 2 + .../android/xmpp/XmppConnection.java | 32 +++----------- .../android/xmpp/manager/CarbonsManager.java | 44 +++++++++++++++++++ .../android/xmpp/manager/DiscoManager.java | 4 ++ .../android/xmpp/model/carbons/Enable.java | 12 +++++ .../xmpp/model/carbons/package-info.java | 5 +++ .../android/xmpp/processor/BindProcessor.java | 4 +- 7 files changed, 76 insertions(+), 27 deletions(-) create mode 100644 src/main/java/im/conversations/android/xmpp/manager/CarbonsManager.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/carbons/Enable.java create mode 100644 src/main/java/im/conversations/android/xmpp/model/carbons/package-info.java diff --git a/src/main/java/im/conversations/android/xmpp/Managers.java b/src/main/java/im/conversations/android/xmpp/Managers.java index 428349567..401b1f7c8 100644 --- a/src/main/java/im/conversations/android/xmpp/Managers.java +++ b/src/main/java/im/conversations/android/xmpp/Managers.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableClassToInstanceMap; import im.conversations.android.xmpp.manager.AbstractManager; import im.conversations.android.xmpp.manager.BlockingManager; import im.conversations.android.xmpp.manager.BookmarkManager; +import im.conversations.android.xmpp.manager.CarbonsManager; import im.conversations.android.xmpp.manager.DiscoManager; import im.conversations.android.xmpp.manager.RosterManager; @@ -18,6 +19,7 @@ public final class Managers { return new ImmutableClassToInstanceMap.Builder() .put(BlockingManager.class, new BlockingManager(context, connection)) .put(BookmarkManager.class, new BookmarkManager(context, connection)) + .put(CarbonsManager.class, new CarbonsManager(context, connection)) .put(DiscoManager.class, new DiscoManager(context, connection)) .put(RosterManager.class, new RosterManager(context, connection)) .build(); diff --git a/src/main/java/im/conversations/android/xmpp/XmppConnection.java b/src/main/java/im/conversations/android/xmpp/XmppConnection.java index 458ee1020..7c884eb44 100644 --- a/src/main/java/im/conversations/android/xmpp/XmppConnection.java +++ b/src/main/java/im/conversations/android/xmpp/XmppConnection.java @@ -55,6 +55,7 @@ import im.conversations.android.database.model.Connection; import im.conversations.android.database.model.Credential; import im.conversations.android.xml.TagWriter; import im.conversations.android.xmpp.manager.AbstractManager; +import im.conversations.android.xmpp.manager.CarbonsManager; import im.conversations.android.xmpp.manager.DiscoManager; import im.conversations.android.xmpp.model.StreamElement; import im.conversations.android.xmpp.model.csi.Active; @@ -126,8 +127,6 @@ public class XmppConnection implements Runnable { private TagWriter tagWriter = new TagWriter(); private boolean encryptionEnabled = false; - - private boolean carbonsEnabled = false; private boolean shouldAuthenticate = true; private boolean inSmacksSession = false; private boolean quickStartInProgress = false; @@ -780,7 +779,6 @@ public class XmppConnection implements Runnable { } if (carbonsEnabled != null) { Log.d(Config.LOGTAG, account.address + ": successfully enabled carbons"); - this.carbonsEnabled = true; } sendPostBindInitialization(carbonsEnabled != null); processNopStreamFeatures = true; @@ -1841,7 +1839,7 @@ public class XmppConnection implements Runnable { } private void sendPostBindInitialization(final boolean carbonsEnabled) { - this.carbonsEnabled = carbonsEnabled; + getManager(CarbonsManager.class).setEnabled(carbonsEnabled); Log.d(Config.LOGTAG, account.address + ": starting service discovery"); final ArrayList> discoFutures = new ArrayList<>(); final var discoManager = getManager(DiscoManager.class); @@ -1896,28 +1894,12 @@ public class XmppConnection implements Runnable { } private void enableAdvancedStreamFeatures() { - if (getManager(DiscoManager.class) - .hasFeature(connectionAddress.getDomain(), Namespace.CARBONS) - && !this.carbonsEnabled) { - sendEnableCarbons(); + if (getManager(CarbonsManager.class).isEnabled()) { + return; + } + if (getManager(DiscoManager.class).hasServerFeature(Namespace.CARBONS)) { + getManager(CarbonsManager.class).enable(); } - } - - private void sendEnableCarbons() { - final IQ iq = new IQ(IQ.Type.SET); - iq.addChild("enable", Namespace.CARBONS); - this.sendIqPacket( - iq, - (packet) -> { - if (packet.getType() == IQ.Type.RESULT) { - Log.d(Config.LOGTAG, account.address + ": successfully enabled carbons"); - this.carbonsEnabled = true; - } else { - Log.d( - Config.LOGTAG, - account.address + ": could not enable carbons " + packet); - } - }); } private void processStreamError(final Tag currentTag) throws IOException { diff --git a/src/main/java/im/conversations/android/xmpp/manager/CarbonsManager.java b/src/main/java/im/conversations/android/xmpp/manager/CarbonsManager.java new file mode 100644 index 000000000..4fc354245 --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/manager/CarbonsManager.java @@ -0,0 +1,44 @@ +package im.conversations.android.xmpp.manager; + +import android.content.Context; +import android.util.Log; +import eu.siacs.conversations.Config; +import im.conversations.android.xmpp.XmppConnection; +import im.conversations.android.xmpp.model.carbons.Enable; +import im.conversations.android.xmpp.model.stanza.IQ; + +public class CarbonsManager extends AbstractManager { + + private boolean enabled = false; + + public CarbonsManager(Context context, XmppConnection connection) { + super(context, connection); + } + + public void enable() { + final var iq = new IQ(IQ.Type.SET); + iq.addExtension(new Enable()); + connection.sendIqPacket( + iq, + result -> { + if (result.getType() == IQ.Type.RESULT) { + Log.d( + Config.LOGTAG, + getAccount().address + ": successfully enabled carbons"); + this.enabled = true; + } else { + Log.d( + Config.LOGTAG, + getAccount().address + ": could not enable carbons " + result); + } + }); + } + + public void setEnabled(final boolean enabled) { + this.enabled = enabled; + } + + public boolean isEnabled() { + return this.enabled; + } +} diff --git a/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java b/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java index c2f9aa7aa..d0510cfd6 100644 --- a/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java +++ b/src/main/java/im/conversations/android/xmpp/manager/DiscoManager.java @@ -157,4 +157,8 @@ public class DiscoManager extends AbstractManager { public boolean hasFeature(final Jid entity, final String feature) { return getDatabase().discoDao().hasFeature(getAccount().id, entity, feature); } + + public boolean hasServerFeature(final String feature) { + return hasFeature(getAccount().address.getDomain(), feature); + } } diff --git a/src/main/java/im/conversations/android/xmpp/model/carbons/Enable.java b/src/main/java/im/conversations/android/xmpp/model/carbons/Enable.java new file mode 100644 index 000000000..38b740e8c --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/carbons/Enable.java @@ -0,0 +1,12 @@ +package im.conversations.android.xmpp.model.carbons; + +import im.conversations.android.annotation.XmlElement; +import im.conversations.android.xmpp.model.Extension; + +@XmlElement +public class Enable extends Extension { + + public Enable() { + super(Enable.class); + } +} diff --git a/src/main/java/im/conversations/android/xmpp/model/carbons/package-info.java b/src/main/java/im/conversations/android/xmpp/model/carbons/package-info.java new file mode 100644 index 000000000..f4c76376a --- /dev/null +++ b/src/main/java/im/conversations/android/xmpp/model/carbons/package-info.java @@ -0,0 +1,5 @@ +@XmlPackage(namespace = Namespace.CARBONS) +package im.conversations.android.xmpp.model.carbons; + +import eu.siacs.conversations.xml.Namespace; +import im.conversations.android.annotation.XmlPackage; diff --git a/src/main/java/im/conversations/android/xmpp/processor/BindProcessor.java b/src/main/java/im/conversations/android/xmpp/processor/BindProcessor.java index 6712a6f8c..80147fffd 100644 --- a/src/main/java/im/conversations/android/xmpp/processor/BindProcessor.java +++ b/src/main/java/im/conversations/android/xmpp/processor/BindProcessor.java @@ -36,11 +36,11 @@ public class BindProcessor extends XmppConnection.Delegate implements Consumer