From a892bb6f1685b84b74ba37e5a6a24ee8fb45eee7 Mon Sep 17 00:00:00 2001 From: steckbrief Date: Mon, 7 Aug 2017 12:57:57 +0200 Subject: Simplifying introduction of new XEP implementations, implements FS#250 and FS#251 (Privacy and Security settings), refactoring of location of some interfaces --- .../services/XmppConnectionService.java | 178 +++++---------------- 1 file changed, 37 insertions(+), 141 deletions(-) (limited to 'src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java') diff --git a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java index ac6ef464..963adfe4 100644 --- a/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java +++ b/src/main/java/de/thedevstack/conversationsplus/services/XmppConnectionService.java @@ -58,13 +58,28 @@ import de.thedevstack.conversationsplus.ConversationsPlusPreferences; import de.thedevstack.conversationsplus.services.avatar.AvatarCache; import de.thedevstack.conversationsplus.services.avatar.AvatarService; import de.thedevstack.conversationsplus.services.filetransfer.FileTransferManager; +import de.thedevstack.conversationsplus.services.mam.MessageArchiveService; +import de.thedevstack.conversationsplus.services.mam.OnMoreMessagesLoaded; +import de.thedevstack.conversationsplus.services.muc.OnConferenceConfigurationFetched; +import de.thedevstack.conversationsplus.services.muc.OnConferenceJoined; +import de.thedevstack.conversationsplus.services.muc.listener.ConferenceConfigurationFetched; import de.thedevstack.conversationsplus.utils.AccountUtil; import de.thedevstack.conversationsplus.utils.ImageUtil; import de.thedevstack.conversationsplus.utils.MessageUtil; import de.thedevstack.conversationsplus.utils.UiUpdateHelper; import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor; import de.thedevstack.conversationsplus.utils.XmppSendUtil; +import de.thedevstack.conversationsplus.services.muc.listener.ConferenceServiceDiscoveryReceived; +import de.thedevstack.conversationsplus.xmpp.XepRegistry; +import de.thedevstack.conversationsplus.xmpp.disco.ServiceDiscoveryIqPacketGenerator; +import de.thedevstack.conversationsplus.xmpp.disco.ServiceDiscoveryIqPacketParser; +import de.thedevstack.conversationsplus.xmpp.disco.ServiceDiscoveryXep; +import de.thedevstack.conversationsplus.xmpp.iqversion.SoftwareVersionXep; import de.thedevstack.conversationsplus.xmpp.mam.Mam; +import de.thedevstack.conversationsplus.xmpp.openpgp.OpenPgpXep; +import de.thedevstack.conversationsplus.xmpp.ping.PingXep; +import de.thedevstack.conversationsplus.xmpp.stanzas.IqPacketReceiver; +import de.thedevstack.conversationsplus.xmpp.time.EntityTimeXep; import de.tzur.conversations.Settings; import de.thedevstack.conversationsplus.Config; import de.thedevstack.conversationsplus.R; @@ -87,7 +102,6 @@ import de.thedevstack.conversationsplus.entities.TransferablePlaceholder; import de.thedevstack.conversationsplus.generator.IqGenerator; import de.thedevstack.conversationsplus.generator.MessageGenerator; import de.thedevstack.conversationsplus.generator.PresenceGenerator; -import de.thedevstack.conversationsplus.parser.IqParser; import de.thedevstack.conversationsplus.parser.MessageParser; import de.thedevstack.conversationsplus.parser.PresenceParser; import de.thedevstack.conversationsplus.persistance.DatabaseBackend; @@ -150,7 +164,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa this); private OnMessagePacketReceived mMessageParser = new MessageParser(this); private OnPresencePacketReceived mPresenceParser = new PresenceParser(this); - private IqParser mIqParser = new IqParser(this); + private OnIqPacketReceived mDefaultIqHandler = new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket packet) { @@ -695,11 +709,20 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa public XmppConnection createConnection(final Account account) { account.setResource(ConversationsPlusPreferences.resource().toLowerCase(Locale.getDefault())); + + // TODO Find better place for initialization + IqPacketReceiver iqPacketReceiver = IqPacketReceiver.getInstance(); + XepRegistry.add(new EntityTimeXep(ConversationsPlusPreferences.sendEntityTime())); + XepRegistry.add(new SoftwareVersionXep(ConversationsPlusPreferences.sendSoftwareVersion())); + XepRegistry.add(new ServiceDiscoveryXep()); + XepRegistry.add(new PingXep()); + XepRegistry.add(new OpenPgpXep()); + final XmppConnection connection = new XmppConnection(account, this); connection.setOnMessagePacketReceivedListener(this.mMessageParser); connection.setOnStatusChangedListener(this.statusListener); connection.setOnPresencePacketReceivedListener(this.mPresenceParser); - connection.setOnUnregisteredIqPacketReceivedListener(this.mIqParser); + connection.setOnUnregisteredIqPacketReceivedListener(iqPacketReceiver); connection.setOnJinglePacketReceivedListener(this.jingleListener); connection.setOnBindListener(this.mOnBindListener); connection.setOnMessageAcknowledgeListener(this.mOnMessageAcknowledgedListener); @@ -875,7 +898,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } } - private void sendUnsentMessages(final Conversation conversation) { + public void sendUnsentMessages(final Conversation conversation) { conversation.findWaitingMessages(new Conversation.OnMessageFound() { @Override @@ -898,7 +921,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa Logging.d(Config.LOGTAG, account.getJid().toBareJid() + ": fetching roster"); } iqPacket.query(Xmlns.ROSTER).setAttribute("ver", account.getRosterVersion()); - sendIqPacket(account, iqPacket, mIqParser); + XmppSendUtil.sendIqPacket(account, iqPacket, IqPacketReceiver.getInstance().getLegacyIqParser()); //TODO -> replace with RosterIqHandler } public void fetchBookmarks(final Account account) { @@ -1657,44 +1680,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa if (account.getStatus() == Account.State.ONLINE) { conversation.resetMucOptions(); conversation.setHasMessagesLeftOnServer(false); - fetchConferenceConfiguration(conversation, new OnConferenceConfigurationFetched() { - - private void join(Conversation conversation) { - Account account = conversation.getAccount(); - final MucOptions mucOptions = conversation.getMucOptions(); - final Jid joinJid = mucOptions.getSelf().getFullJid(); - Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": joining conversation " + joinJid.toString()); - long lastMessageTransmitted = conversation.getLastMessageTransmitted(); - PresencePacket packet = PresenceGenerator.generateMucJoin(account, joinJid, mucOptions, lastMessageTransmitted); - XmppSendUtil.sendPresencePacket(account, packet); - if (onConferenceJoined != null) { - onConferenceJoined.onConferenceJoined(conversation); - } - if (!joinJid.equals(conversation.getJid())) { - conversation.setContactJid(joinJid); - databaseBackend.updateConversation(conversation); - } - - if (mucOptions.mamSupport()) { - getMessageArchiveService().catchupMUC(conversation); - } - if (mucOptions.membersOnly() && mucOptions.nonanonymous()) { - fetchConferenceMembers(conversation); - } - sendUnsentMessages(conversation); - } - - @Override - public void onConferenceConfigurationFetched(Conversation conversation) { - join(conversation); - } - - @Override - public void onFetchFailed(final Conversation conversation, Element error) { - join(conversation); - fetchConferenceConfiguration(conversation); - } - }); + fetchConferenceConfiguration(conversation, new ConferenceConfigurationFetched(onConferenceJoined)); updateConversationUi(); } else { account.pendingConferenceJoins.add(conversation); @@ -1704,37 +1690,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } } - private void fetchConferenceMembers(final Conversation conversation) { - final Account account = conversation.getAccount(); - final String[] affiliations = {"member","admin","owner"}; - OnIqPacketReceived callback = new OnIqPacketReceived() { - - private int i = 0; - - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - Element query = packet.query("http://jabber.org/protocol/muc#admin"); - if (packet.getType() == IqPacket.TYPE.RESULT && query != null) { - for(Element child : query.getChildren()) { - if ("item".equals(child.getName())) { - conversation.getMucOptions().putMember(child.getAttributeAsJid("jid")); - } - } - } else { - Log.d(Config.LOGTAG,account.getJid().toBareJid()+": could not request affiliation "+affiliations[i]+" in "+conversation.getJid().toBareJid()); - } - ++i; - if (i >= affiliations.length) { - Log.d(Config.LOGTAG,account.getJid().toBareJid()+": retrieved members for "+conversation.getJid().toBareJid()+": "+conversation.getMucOptions().getMembers()); - } - } - }; - for(String affiliation : affiliations) { - sendIqPacket(account, mIqGenerator.queryAffiliation(conversation, affiliation), callback); - } - Log.d(Config.LOGTAG,account.getJid().toBareJid()+": fetching members for "+conversation.getName()); - } - public void providePasswordForMuc(Conversation conversation, String password) { if (conversation.getMode() == Conversation.MODE_MULTI) { conversation.getMucOptions().setPassword(password); @@ -1889,40 +1844,8 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } public void fetchConferenceConfiguration(final Conversation conversation, final OnConferenceConfigurationFetched callback) { - IqPacket request = new IqPacket(IqPacket.TYPE.GET); - request.setTo(conversation.getJid().toBareJid()); - request.query("http://jabber.org/protocol/disco#info"); - sendIqPacket(conversation.getAccount(), request, new OnIqPacketReceived() { - @Override - public void onIqPacketReceived(Account account, IqPacket packet) { - Element query = packet.findChild("query","http://jabber.org/protocol/disco#info"); - if (packet.getType() == IqPacket.TYPE.RESULT && query != null) { - ArrayList features = new ArrayList<>(); - for (Element child : query.getChildren()) { - if (child != null && child.getName().equals("feature")) { - String var = child.getAttribute("var"); - if (var != null) { - features.add(var); - } - } - } - Element form = query.findChild("x", "jabber:x:data"); - if (form != null) { - conversation.getMucOptions().updateFormData(Data.parse(form)); - } - conversation.getMucOptions().updateFeatures(features); - if (callback != null) { - callback.onConferenceConfigurationFetched(conversation); - } - Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": fetched muc configuration for " + conversation.getJid().toBareJid() + " - " + features.toString()); - updateConversationUi(); - } else if (packet.getType() == IqPacket.TYPE.ERROR) { - if (callback != null) { - callback.onFetchFailed(conversation, packet.getError()); - } - } - } - }); + IqPacket request = ServiceDiscoveryIqPacketGenerator.generateRequest(conversation.getJid().toBareJid()); + XmppSendUtil.sendIqPacket(conversation.getAccount(), request, new ConferenceServiceDiscoveryReceived(conversation, callback)); } public void pushConferenceConfiguration(final Conversation conversation, final Bundle options, final OnConferenceOptionsPushed callback) { @@ -2466,10 +2389,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa return this.mIqGenerator; } - public IqParser getIqParser() { - return this.mIqParser; - } - public JingleConnectionManager getJingleConnectionManager() { return this.mJingleConnectionManager; } @@ -2529,7 +2448,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa public void onIqPacketReceived(final Account account, final IqPacket packet) { if (packet.getType() == IqPacket.TYPE.RESULT) { account.getBlocklist().add(jid); - updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED); + UiUpdateHelper.updateBlocklistUi(OnUpdateBlocklist.Status.BLOCKED); } } }); @@ -2544,7 +2463,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa public void onIqPacketReceived(final Account account, final IqPacket packet) { if (packet.getType() == IqPacket.TYPE.RESULT) { account.getBlocklist().remove(jid); - updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED); + UiUpdateHelper.updateBlocklistUi(OnUpdateBlocklist.Status.UNBLOCKED); } } }); @@ -2587,15 +2506,13 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } else { if (!account.inProgressDiscoFetches.contains(key)) { account.inProgressDiscoFetches.add(key); - IqPacket request = new IqPacket(IqPacket.TYPE.GET); - request.setTo(jid); - request.query("http://jabber.org/protocol/disco#info"); + IqPacket request = ServiceDiscoveryIqPacketGenerator.generateRequest(jid); Log.d(Config.LOGTAG,account.getJid().toBareJid()+": making disco request for "+key.second+" to "+jid); sendIqPacket(account, request, new OnIqPacketReceived() { @Override public void onIqPacketReceived(Account account, IqPacket discoPacket) { - if (discoPacket.getType() == IqPacket.TYPE.RESULT) { - ServiceDiscoveryResult disco = new ServiceDiscoveryResult(discoPacket); + ServiceDiscoveryResult disco = ServiceDiscoveryIqPacketParser.parse(discoPacket); + if (null != disco) { if (presence.getVer().equals(disco.getVer())) { databaseBackend.insertDiscoveryResult(disco); injectServiceDiscorveryResult(account.getRoster(), presence.getHash(), presence.getVer(), disco); @@ -2657,15 +2574,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa void informUser(int r); } - public interface OnMoreMessagesLoaded { - void onMoreMessagesLoaded(int count, Conversation conversation); - - void informUser(int r); - - void setLoadingInProgress(); - boolean isLoadingInProgress(); - } public interface OnAccountPasswordChanged { void onPasswordChangeSucceeded(); @@ -2694,10 +2603,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } public interface OnCaptchaRequested { - void onCaptchaRequested(Account account, - String id, - Data data, - Bitmap captcha); + void onCaptchaRequested(Account account, String id, Data data, Bitmap captcha); } public interface OnRosterUpdate { @@ -2708,16 +2614,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa void onMucRosterUpdate(); } - public interface OnConferenceConfigurationFetched { - void onConferenceConfigurationFetched(Conversation conversation); - - void onFetchFailed(Conversation conversation, Element error); - } - - public interface OnConferenceJoined { - void onConferenceJoined(Conversation conversation); - } - public interface OnConferenceOptionsPushed { void onPushSucceeded(); -- cgit v1.2.3