diff options
author | steckbrief <steckbrief@chefmail.de> | 2017-08-02 12:43:47 +0200 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2017-08-02 12:43:47 +0200 |
commit | 76ce6f52062aa802482497bc79aa05ee3e73244b (patch) | |
tree | ae6b23574f544c80c38338fbf0b948756c174ddc /src | |
parent | 8c5208bacd95ebd1ad6a6c192e536fd1ed47bfe2 (diff) |
Using a generator approach for generating PresencePackets
Diffstat (limited to 'src')
7 files changed, 133 insertions, 100 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/generator/AbstractGenerator.java b/src/main/java/de/thedevstack/conversationsplus/generator/AbstractGenerator.java index 2d825f2c..abe137cf 100644 --- a/src/main/java/de/thedevstack/conversationsplus/generator/AbstractGenerator.java +++ b/src/main/java/de/thedevstack/conversationsplus/generator/AbstractGenerator.java @@ -17,7 +17,7 @@ import de.tzur.conversations.Settings; import de.thedevstack.conversationsplus.crypto.axolotl.AxolotlService; public abstract class AbstractGenerator { - private final String[] FEATURES = { + private static final String[] FEATURES = { "urn:xmpp:jingle:1", "urn:xmpp:jingle:apps:file-transfer:3", "urn:xmpp:jingle:transports:s5b:1", @@ -32,15 +32,15 @@ public abstract class AbstractGenerator { "jabber:iq:version", "http://jabber.org/protocol/chatstates", AxolotlService.PEP_DEVICE_LIST+"+notify"}; - private final String[] MESSAGE_CONFIRMATION_FEATURES = { + private static final String[] MESSAGE_CONFIRMATION_FEATURES = { "urn:xmpp:chat-markers:0", "urn:xmpp:receipts" }; - protected final String IDENTITY_TYPE = "phone"; + protected static final String IDENTITY_TYPE = "phone"; private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US); - public String getCapHash() { + public static String getCapHash() { StringBuilder s = new StringBuilder(); s.append("client/" + IDENTITY_TYPE + "//" + ConversationsPlusApplication.getNameAndVersion() + "<"); MessageDigest md; @@ -62,7 +62,7 @@ public abstract class AbstractGenerator { return DATE_FORMAT.format(time); } - public List<String> getFeatures() { + public static List<String> getFeatures() { ArrayList<String> features = new ArrayList<>(); features.addAll(Arrays.asList(FEATURES)); if (Settings.CONFIRM_MESSAGE_RECEIVED) { diff --git a/src/main/java/de/thedevstack/conversationsplus/generator/PresenceGenerator.java b/src/main/java/de/thedevstack/conversationsplus/generator/PresenceGenerator.java index f370c6e0..fbe7c911 100644 --- a/src/main/java/de/thedevstack/conversationsplus/generator/PresenceGenerator.java +++ b/src/main/java/de/thedevstack/conversationsplus/generator/PresenceGenerator.java @@ -2,41 +2,55 @@ package de.thedevstack.conversationsplus.generator; import de.thedevstack.conversationsplus.entities.Account; import de.thedevstack.conversationsplus.entities.Contact; +import de.thedevstack.conversationsplus.entities.MucOptions; import de.thedevstack.conversationsplus.entities.Presence; import de.thedevstack.conversationsplus.xml.Element; +import de.thedevstack.conversationsplus.xmpp.jid.Jid; import de.thedevstack.conversationsplus.xmpp.stanzas.PresencePacket; public class PresenceGenerator extends AbstractGenerator { - private PresencePacket subscription(String type, Contact contact) { - PresencePacket packet = new PresencePacket(); - packet.setAttribute("type", type); - packet.setTo(contact.getJid()); - packet.setFrom(contact.getAccount().getJid().toBareJid()); - return packet; + private static PresencePacket generatePresencePacket(String type, Jid from, Jid to) { + PresencePacket packet = new PresencePacket(); + if (null != type) { + packet.setAttribute("type", type); + } + + if (null != to) { + packet.setTo(to); + } + + if (null != from) { + packet.setFrom(from); + } + + return packet; + + } + + private static PresencePacket subscription(String type, Contact contact) { + return generatePresencePacket(type, contact.getAccount().getJid().toBareJid(), contact.getJid()); } - public PresencePacket requestPresenceUpdatesFrom(Contact contact) { + public static PresencePacket requestPresenceUpdatesFrom(Contact contact) { return subscription("subscribe", contact); } - public PresencePacket stopPresenceUpdatesFrom(Contact contact) { + public static PresencePacket stopPresenceUpdatesFrom(Contact contact) { return subscription("unsubscribe", contact); } - public PresencePacket stopPresenceUpdatesTo(Contact contact) { + public static PresencePacket stopPresenceUpdatesTo(Contact contact) { return subscription("unsubscribed", contact); } - public PresencePacket sendPresenceUpdatesTo(Contact contact) { + public static PresencePacket sendPresenceUpdatesTo(Contact contact) { return subscription("subscribed", contact); } - public PresencePacket selfPresence(Account account, Presence.Status status) { - PresencePacket packet = new PresencePacket(); - if(status.toShowString() != null) { - packet.addChild("show").setContent(status.toShowString()); - } + public static PresencePacket selfPresence(Account account, Presence.Status status) { + PresencePacket packet = new PresencePacket(status); + packet.setFrom(account.getJid()); String sig = account.getPgpSignature(); if (sig != null) { @@ -53,10 +67,51 @@ public class PresenceGenerator extends AbstractGenerator { return packet; } - public PresencePacket sendOfflinePresence(Account account) { - PresencePacket packet = new PresencePacket(); - packet.setFrom(account.getJid()); - packet.setAttribute("type","unavailable"); - return packet; + private static PresencePacket generateOfflinePresencePacket(Jid from, Jid to) { + return generatePresencePacket("unavailable", from, to); + + } + + public static PresencePacket generateOfflinePresencePacketTo(Account account, Jid to) { + return generateOfflinePresencePacket(account.getJid(), to); + } + + public static PresencePacket generateOfflinePresencePacket(Account account) { + return generateOfflinePresencePacket(account.getJid(), null); } + + public static PresencePacket generateMucJoin(Account account, Jid joinJid, MucOptions mucOptions, long lastMessageTransmitted) { + PresencePacket packet = generatePresencePacket(null, account.getJid(), joinJid); + Element x = packet.addChild("x", "http://jabber.org/protocol/muc"); + if (mucOptions.getPassword() != null) { + x.addChild("password").setContent(mucOptions.getPassword()); + } + + Element historyElement = x.addChild("history"); + if (mucOptions.mamSupport()) { + // Use MAM instead of the limited muc history to get history + historyElement.setAttribute("maxchars", "0"); + } else { + // Fallback to muc history + historyElement.setAttribute("since", PresenceGenerator.getTimestamp(lastMessageTransmitted)); + } + String sig = account.getPgpSignature(); + if (sig != null) { + packet.addChild("x", "jabber:x:signed").setContent(sig); + } + + return packet; + } + + public static PresencePacket generateMucRename(Account account, Jid joinJid) { + PresencePacket packet = generatePresencePacket(null, account.getJid(), joinJid); + + String sig = account.getPgpSignature(); + if (sig != null) { + packet.addChild("status").setContent("online"); + packet.addChild("x", "jabber:x:signed").setContent(sig); + } + + return packet; + } } diff --git a/src/main/java/de/thedevstack/conversationsplus/parser/PresenceParser.java b/src/main/java/de/thedevstack/conversationsplus/parser/PresenceParser.java index e86e657e..52e23bce 100644 --- a/src/main/java/de/thedevstack/conversationsplus/parser/PresenceParser.java +++ b/src/main/java/de/thedevstack/conversationsplus/parser/PresenceParser.java @@ -21,6 +21,7 @@ import de.thedevstack.conversationsplus.entities.Presence; import de.thedevstack.conversationsplus.generator.PresenceGenerator; import de.thedevstack.conversationsplus.services.AvatarService; import de.thedevstack.conversationsplus.services.XmppConnectionService; +import de.thedevstack.conversationsplus.utils.XmppSendUtil; import de.thedevstack.conversationsplus.xml.Element; import de.thedevstack.conversationsplus.xmpp.OnPresencePacketReceived; import de.thedevstack.conversationsplus.xmpp.jid.Jid; @@ -172,7 +173,6 @@ public class PresenceParser extends AbstractParser implements } public void parseContactPresence(final PresencePacket packet, final Account account) { - final PresenceGenerator mPresenceGenerator = mXmppConnectionService.getPresenceGenerator(); final Jid from = packet.getFrom(); if (from == null) { return; @@ -229,8 +229,7 @@ public class PresenceParser extends AbstractParser implements mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, false); } else if (type.equals("subscribe")) { if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) { - mXmppConnectionService.sendPresencePacket(account, - mPresenceGenerator.sendPresenceUpdatesTo(contact)); + XmppSendUtil.sendPresencePacket(account, PresenceGenerator.sendPresenceUpdatesTo(contact)); } else { contact.setOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST); final Conversation conversation = mXmppConnectionService.findOrCreateConversation( diff --git a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java index 1d1f0108..a7bd8006 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java @@ -161,7 +161,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } }; private MessageGenerator mMessageGenerator = new MessageGenerator(); - private PresenceGenerator mPresenceGenerator = new PresenceGenerator(); private List<Account> accounts; private JingleConnectionManager mJingleConnectionManager = new JingleConnectionManager(); public OnContactStatusChanged onContactStatusChanged = new OnContactStatusChanged() { @@ -1258,9 +1257,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa conversation.endOtrIfNeeded(); if (conversation.getContact().getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) { Log.d(Config.LOGTAG, "Canceling presence request from " + conversation.getJid().toString()); - sendPresencePacket( + XmppSendUtil.sendPresencePacket( conversation.getAccount(), - mPresenceGenerator.stopPresenceUpdatesTo(conversation.getContact()) + PresenceGenerator.stopPresenceUpdatesTo(conversation.getContact()) ); } } @@ -1662,26 +1661,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa final MucOptions mucOptions = conversation.getMucOptions(); final Jid joinJid = mucOptions.getSelf().getFullJid(); Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": joining conversation " + joinJid.toString()); - PresencePacket packet = new PresencePacket(); - packet.setFrom(conversation.getAccount().getJid()); - packet.setTo(joinJid); - Element x = packet.addChild("x", "http://jabber.org/protocol/muc"); - if (conversation.getMucOptions().getPassword() != null) { - x.addChild("password").setContent(conversation.getMucOptions().getPassword()); - } - - if (mucOptions.mamSupport()) { - // Use MAM instead of the limited muc history to get history - x.addChild("history").setAttribute("maxchars", "0"); - } else { - // Fallback to muc history - x.addChild("history").setAttribute("since", PresenceGenerator.getTimestamp(conversation.getLastMessageTransmitted())); - } - String sig = account.getPgpSignature(); - if (sig != null) { - packet.addChild("x", "jabber:x:signed").setContent(sig); - } - sendPresencePacket(account, packet); + long lastMessageTransmitted = conversation.getLastMessageTransmitted(); + PresencePacket packet = PresenceGenerator.generateMucJoin(account, joinJid, mucOptions, lastMessageTransmitted); + XmppSendUtil.sendPresencePacket(account, packet); if (onConferenceJoined != null) { onConferenceJoined.onConferenceJoined(conversation); } @@ -1787,16 +1769,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } }); - PresencePacket packet = new PresencePacket(); - packet.setTo(joinJid); - packet.setFrom(conversation.getAccount().getJid()); - - String sig = account.getPgpSignature(); - if (sig != null) { - packet.addChild("status").setContent("online"); - packet.addChild("x", "jabber:x:signed").setContent(sig); - } - sendPresencePacket(account, packet); + PresencePacket packet = PresenceGenerator.generateMucRename(account, joinJid); + XmppSendUtil.sendPresencePacket(account, packet); } else { conversation.setContactJid(joinJid); databaseBackend.updateConversation(conversation); @@ -1820,14 +1794,11 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa account.pendingConferenceJoins.remove(conversation); account.pendingConferenceLeaves.remove(conversation); if (account.getStatus() == Account.State.ONLINE || now) { - PresencePacket packet = new PresencePacket(); - packet.setTo(conversation.getMucOptions().getSelf().getFullJid()); - packet.setFrom(conversation.getAccount().getJid()); - packet.setAttribute("type", "unavailable"); - sendPresencePacket(conversation.getAccount(), packet); + PresencePacket packet = PresenceGenerator.generateOfflinePresencePacketTo(account, conversation.getMucOptions().getSelf().getFullJid()); + XmppSendUtil.sendPresencePacket(account, packet); conversation.getMucOptions().setOffline(); conversation.deregisterWithBookmark(); - Logging.d(Config.LOGTAG, conversation.getAccount().getJid().toBareJid() + Logging.d(Config.LOGTAG, account.getJid().toBareJid() + ": leaving muc " + conversation.getJid()); } else { account.pendingConferenceLeaves.add(conversation); @@ -2169,12 +2140,10 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa iq.query(Xmlns.ROSTER).addChild(contact.asElement()); account.getXmppConnection().sendIqPacket(iq, mDefaultIqHandler); if (sendUpdates) { - sendPresencePacket(account, - mPresenceGenerator.sendPresenceUpdatesTo(contact)); + XmppSendUtil.sendPresencePacket(account, PresenceGenerator.sendPresenceUpdatesTo(contact)); } if (ask) { - sendPresencePacket(account, - mPresenceGenerator.requestPresenceUpdatesFrom(contact)); + XmppSendUtil.sendPresencePacket(account, PresenceGenerator.requestPresenceUpdatesFrom(contact)); } } } @@ -2450,11 +2419,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa XmppSendUtil.sendMessagePacket(account, packet); } - @Deprecated - public void sendPresencePacket(Account account, PresencePacket packet) { - XmppSendUtil.sendPresencePacket(account, packet); - } - public void sendCreateAccountWithCaptchaPacket(Account account, String id, Data data) { XmppConnection connection = account.getXmppConnection(); if (connection != null) { @@ -2468,7 +2432,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } public void sendPresence(final Account account) { - XmppSendUtil.sendPresencePacket(account, mPresenceGenerator.selfPresence(account, getTargetPresence())); + XmppSendUtil.sendPresencePacket(account, PresenceGenerator.selfPresence(account, getTargetPresence())); } public void refreshAllPresences() { @@ -2488,17 +2452,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } public void sendOfflinePresence(final Account account) { - XmppSendUtil.sendPresencePacket(account, mPresenceGenerator.sendOfflinePresence(account)); + XmppSendUtil.sendPresencePacket(account, PresenceGenerator.generateOfflinePresencePacket(account)); } public MessageGenerator getMessageGenerator() { return this.mMessageGenerator; } - public PresenceGenerator getPresenceGenerator() { - return this.mPresenceGenerator; - } - public IqGenerator getIqGenerator() { return this.mIqGenerator; } diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/ContactDetailsActivity.java b/src/main/java/de/thedevstack/conversationsplus/ui/ContactDetailsActivity.java index 3ca9fce7..6577c9ce 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/ContactDetailsActivity.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/ContactDetailsActivity.java @@ -33,6 +33,7 @@ import java.security.cert.X509Certificate; import java.util.List; import de.thedevstack.conversationsplus.ConversationsPlusPreferences; +import de.thedevstack.conversationsplus.generator.PresenceGenerator; import de.thedevstack.conversationsplus.ui.listeners.ShowResourcesListDialogListener; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.R; @@ -47,6 +48,7 @@ import de.thedevstack.conversationsplus.services.XmppConnectionService.OnAccount import de.thedevstack.conversationsplus.services.XmppConnectionService.OnRosterUpdate; import de.thedevstack.conversationsplus.utils.CryptoHelper; import de.thedevstack.conversationsplus.utils.UIHelper; +import de.thedevstack.conversationsplus.utils.XmppSendUtil; import de.thedevstack.conversationsplus.xmpp.OnKeyStatusUpdated; import de.thedevstack.conversationsplus.xmpp.OnUpdateBlocklist; import de.thedevstack.conversationsplus.xmpp.XmppConnection; @@ -72,18 +74,13 @@ public class ContactDetailsActivity extends XmppActivity implements OnAccountUpd if (isChecked) { if (contact .getOption(Contact.Options.PENDING_SUBSCRIPTION_REQUEST)) { - xmppConnectionService.sendPresencePacket(contact - .getAccount(), - xmppConnectionService.getPresenceGenerator() - .sendPresenceUpdatesTo(contact)); + XmppSendUtil.sendPresencePacket(contact.getAccount(), PresenceGenerator.sendPresenceUpdatesTo(contact)); } else { contact.setOption(Contact.Options.PREEMPTIVE_GRANT); } } else { contact.resetOption(Contact.Options.PREEMPTIVE_GRANT); - xmppConnectionService.sendPresencePacket(contact.getAccount(), - xmppConnectionService.getPresenceGenerator() - .stopPresenceUpdatesTo(contact)); + XmppSendUtil.sendPresencePacket(contact.getAccount(), PresenceGenerator.stopPresenceUpdatesTo(contact)); } } }; @@ -93,13 +90,9 @@ public class ContactDetailsActivity extends XmppActivity implements OnAccountUpd public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { - xmppConnectionService.sendPresencePacket(contact.getAccount(), - xmppConnectionService.getPresenceGenerator() - .requestPresenceUpdatesFrom(contact)); + XmppSendUtil.sendPresencePacket(contact.getAccount(), PresenceGenerator.requestPresenceUpdatesFrom(contact)); } else { - xmppConnectionService.sendPresencePacket(contact.getAccount(), - xmppConnectionService.getPresenceGenerator() - .stopPresenceUpdatesFrom(contact)); + XmppSendUtil.sendPresencePacket(contact.getAccount(), PresenceGenerator.stopPresenceUpdatesFrom(contact)); } } }; diff --git a/src/main/java/de/thedevstack/conversationsplus/ui/XmppActivity.java b/src/main/java/de/thedevstack/conversationsplus/ui/XmppActivity.java index 6785dd31..85bc2af3 100644 --- a/src/main/java/de/thedevstack/conversationsplus/ui/XmppActivity.java +++ b/src/main/java/de/thedevstack/conversationsplus/ui/XmppActivity.java @@ -72,10 +72,12 @@ import de.thedevstack.conversationsplus.entities.Conversation; import de.thedevstack.conversationsplus.entities.Message; import de.thedevstack.conversationsplus.entities.MucOptions; import de.thedevstack.conversationsplus.entities.Presences; +import de.thedevstack.conversationsplus.generator.PresenceGenerator; import de.thedevstack.conversationsplus.services.XmppConnectionService; import de.thedevstack.conversationsplus.services.XmppConnectionService.XmppConnectionBinder; import de.thedevstack.conversationsplus.utils.CryptoHelper; import de.thedevstack.conversationsplus.utils.ExceptionHelper; +import de.thedevstack.conversationsplus.utils.XmppSendUtil; import de.thedevstack.conversationsplus.xmpp.OnKeyStatusUpdated; import de.thedevstack.conversationsplus.xmpp.OnUpdateBlocklist; import de.thedevstack.conversationsplus.xmpp.jid.InvalidJidException; @@ -562,10 +564,7 @@ public abstract class XmppActivity extends Activity { @Override public void onClick(DialogInterface dialog, int which) { if (xmppConnectionServiceBound) { - xmppConnectionService.sendPresencePacket(contact - .getAccount(), xmppConnectionService - .getPresenceGenerator() - .requestPresenceUpdatesFrom(contact)); + XmppSendUtil.sendPresencePacket(contact.getAccount(), PresenceGenerator.requestPresenceUpdatesFrom(contact)); } } }); diff --git a/src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/PresencePacket.java b/src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/PresencePacket.java index 5fd21b3b..7d5bf43a 100644 --- a/src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/PresencePacket.java +++ b/src/main/java/de/thedevstack/conversationsplus/xmpp/stanzas/PresencePacket.java @@ -1,8 +1,35 @@ package de.thedevstack.conversationsplus.xmpp.stanzas; +import de.thedevstack.conversationsplus.entities.Presence; + public class PresencePacket extends AbstractAcknowledgeableStanza { - public PresencePacket() { + public PresencePacket() { + super("presence"); + } + + public PresencePacket(Presence.Status status) { super("presence"); + String show; + switch(status) { + case CHAT: + show = "chat"; + break; + case AWAY: + show = "away"; + break; + case XA: + show = "xa"; + break; + case DND: + show = "dnd"; + break; + default: + show = null; + } + + if(show != null) { + this.addChild("show").setContent(show); + } } } |