From 332fe0fd194a80501cf935f22faed645f3032b57 Mon Sep 17 00:00:00 2001 From: iNPUTmice Date: Thu, 9 Apr 2015 12:46:54 +0200 Subject: [PATCH] don't resume old session when changing resource --- .../eu/siacs/conversations/entities/Account.java | 14 ++++++++++---- .../conversations/generator/PresenceGenerator.java | 7 +++++++ .../services/XmppConnectionService.java | 5 +++++ .../siacs/conversations/ui/SettingsActivity.java | 12 +++++++++--- .../siacs/conversations/xmpp/XmppConnection.java | 4 ++++ 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index 2bc2c9540..fe1030945 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -229,11 +229,17 @@ public class Account extends AbstractEntity { return jid.getResourcepart(); } - public void setResource(final String resource) { - try { - jid = Jid.fromParts(jid.getLocalpart(), jid.getDomainpart(), resource); - } catch (final InvalidJidException ignored) { + public boolean setResource(final String resource) { + final String oldResource = jid.getResourcepart(); + if (oldResource == null || !oldResource.equals(resource)) { + try { + jid = Jid.fromParts(jid.getLocalpart(), jid.getDomainpart(), resource); + return true; + } catch (final InvalidJidException ignored) { + return true; + } } + return false; } public Jid getJid() { diff --git a/src/main/java/eu/siacs/conversations/generator/PresenceGenerator.java b/src/main/java/eu/siacs/conversations/generator/PresenceGenerator.java index 1e8967244..526005f33 100644 --- a/src/main/java/eu/siacs/conversations/generator/PresenceGenerator.java +++ b/src/main/java/eu/siacs/conversations/generator/PresenceGenerator.java @@ -54,4 +54,11 @@ 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; + } } \ No newline at end of file diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 9badbbd80..669ad62cf 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -1701,6 +1701,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } } } + sendOfflinePresence(account); } account.getXmppConnection().disconnect(force); } @@ -2261,6 +2262,10 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa sendPresencePacket(account, mPresenceGenerator.sendPresence(account)); } + public void sendOfflinePresence(final Account account) { + sendPresencePacket(account, mPresenceGenerator.sendOfflinePresence(account)); + } + public MessageGenerator getMessageGenerator() { return this.mMessageGenerator; } diff --git a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java index 39e215f2b..e3cb2ee79 100644 --- a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.Locale; import eu.siacs.conversations.entities.Account; +import eu.siacs.conversations.xmpp.XmppConnection; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; @@ -63,9 +64,14 @@ public class SettingsActivity extends XmppActivity implements .toLowerCase(Locale.US); if (xmppConnectionServiceBound) { for (Account account : xmppConnectionService.getAccounts()) { - account.setResource(resource); - if (!account.isOptionSet(Account.OPTION_DISABLED)) { - xmppConnectionService.reconnectAccountInBackground(account); + if (account.setResource(resource)) { + if (!account.isOptionSet(Account.OPTION_DISABLED)) { + XmppConnection connection = account.getXmppConnection(); + if (connection != null) { + connection.resetStreamId(); + } + xmppConnectionService.reconnectAccountInBackground(account); + } } } } diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index 48dc2150d..25db8ecca 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -981,6 +981,10 @@ public class XmppConnection implements Runnable { } } + public void resetStreamId() { + this.streamId = null; + } + public List findDiscoItemsByFeature(final String feature) { final List items = new ArrayList<>(); for (final Entry> cursor : disco.entrySet()) {