introduce CarbonsManager to enable and maintain carbon state

This commit is contained in:
Daniel Gultsch 2023-01-23 18:01:55 +01:00
parent d2794ccf32
commit 164ac450d4
No known key found for this signature in database
GPG key ID: F43D18AD2A0982C2
7 changed files with 76 additions and 27 deletions

View file

@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableClassToInstanceMap;
import im.conversations.android.xmpp.manager.AbstractManager; import im.conversations.android.xmpp.manager.AbstractManager;
import im.conversations.android.xmpp.manager.BlockingManager; import im.conversations.android.xmpp.manager.BlockingManager;
import im.conversations.android.xmpp.manager.BookmarkManager; 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.DiscoManager;
import im.conversations.android.xmpp.manager.RosterManager; import im.conversations.android.xmpp.manager.RosterManager;
@ -18,6 +19,7 @@ public final class Managers {
return new ImmutableClassToInstanceMap.Builder<AbstractManager>() return new ImmutableClassToInstanceMap.Builder<AbstractManager>()
.put(BlockingManager.class, new BlockingManager(context, connection)) .put(BlockingManager.class, new BlockingManager(context, connection))
.put(BookmarkManager.class, new BookmarkManager(context, connection)) .put(BookmarkManager.class, new BookmarkManager(context, connection))
.put(CarbonsManager.class, new CarbonsManager(context, connection))
.put(DiscoManager.class, new DiscoManager(context, connection)) .put(DiscoManager.class, new DiscoManager(context, connection))
.put(RosterManager.class, new RosterManager(context, connection)) .put(RosterManager.class, new RosterManager(context, connection))
.build(); .build();

View file

@ -55,6 +55,7 @@ import im.conversations.android.database.model.Connection;
import im.conversations.android.database.model.Credential; import im.conversations.android.database.model.Credential;
import im.conversations.android.xml.TagWriter; import im.conversations.android.xml.TagWriter;
import im.conversations.android.xmpp.manager.AbstractManager; 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.manager.DiscoManager;
import im.conversations.android.xmpp.model.StreamElement; import im.conversations.android.xmpp.model.StreamElement;
import im.conversations.android.xmpp.model.csi.Active; import im.conversations.android.xmpp.model.csi.Active;
@ -126,8 +127,6 @@ public class XmppConnection implements Runnable {
private TagWriter tagWriter = new TagWriter(); private TagWriter tagWriter = new TagWriter();
private boolean encryptionEnabled = false; private boolean encryptionEnabled = false;
private boolean carbonsEnabled = false;
private boolean shouldAuthenticate = true; private boolean shouldAuthenticate = true;
private boolean inSmacksSession = false; private boolean inSmacksSession = false;
private boolean quickStartInProgress = false; private boolean quickStartInProgress = false;
@ -780,7 +779,6 @@ public class XmppConnection implements Runnable {
} }
if (carbonsEnabled != null) { if (carbonsEnabled != null) {
Log.d(Config.LOGTAG, account.address + ": successfully enabled carbons"); Log.d(Config.LOGTAG, account.address + ": successfully enabled carbons");
this.carbonsEnabled = true;
} }
sendPostBindInitialization(carbonsEnabled != null); sendPostBindInitialization(carbonsEnabled != null);
processNopStreamFeatures = true; processNopStreamFeatures = true;
@ -1841,7 +1839,7 @@ public class XmppConnection implements Runnable {
} }
private void sendPostBindInitialization(final boolean carbonsEnabled) { private void sendPostBindInitialization(final boolean carbonsEnabled) {
this.carbonsEnabled = carbonsEnabled; getManager(CarbonsManager.class).setEnabled(carbonsEnabled);
Log.d(Config.LOGTAG, account.address + ": starting service discovery"); Log.d(Config.LOGTAG, account.address + ": starting service discovery");
final ArrayList<ListenableFuture<?>> discoFutures = new ArrayList<>(); final ArrayList<ListenableFuture<?>> discoFutures = new ArrayList<>();
final var discoManager = getManager(DiscoManager.class); final var discoManager = getManager(DiscoManager.class);
@ -1896,28 +1894,12 @@ public class XmppConnection implements Runnable {
} }
private void enableAdvancedStreamFeatures() { private void enableAdvancedStreamFeatures() {
if (getManager(DiscoManager.class) if (getManager(CarbonsManager.class).isEnabled()) {
.hasFeature(connectionAddress.getDomain(), Namespace.CARBONS) return;
&& !this.carbonsEnabled) {
sendEnableCarbons();
} }
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 { private void processStreamError(final Tag currentTag) throws IOException {

View file

@ -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;
}
}

View file

@ -157,4 +157,8 @@ public class DiscoManager extends AbstractManager {
public boolean hasFeature(final Jid entity, final String feature) { public boolean hasFeature(final Jid entity, final String feature) {
return getDatabase().discoDao().hasFeature(getAccount().id, entity, feature); return getDatabase().discoDao().hasFeature(getAccount().id, entity, feature);
} }
public boolean hasServerFeature(final String feature) {
return hasFeature(getAccount().address.getDomain(), feature);
}
} }

View file

@ -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);
}
}

View file

@ -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;

View file

@ -36,11 +36,11 @@ public class BindProcessor extends XmppConnection.Delegate implements Consumer<J
final var discoManager = getManager(DiscoManager.class); final var discoManager = getManager(DiscoManager.class);
if (discoManager.hasFeature(account.address.getDomain(), Namespace.BLOCKING)) { if (discoManager.hasServerFeature(Namespace.BLOCKING)) {
getManager(BlockingManager.class).fetch(); getManager(BlockingManager.class).fetch();
} }
if (discoManager.hasFeature(account.address.getDomain(), Namespace.COMMANDS)) { if (discoManager.hasServerFeature(Namespace.COMMANDS)) {
discoManager.items(Entity.discoItem(account.address.getDomain()), Namespace.COMMANDS); discoManager.items(Entity.discoItem(account.address.getDomain()), Namespace.COMMANDS);
} }