From fb5bd2488b7ca50b09bd8f561bcd14ad65a67893 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 13 May 2016 10:45:30 +0200 Subject: show presence of other resources as template --- .../eu/siacs/conversations/entities/Account.java | 4 ++++ .../conversations/entities/PresenceTemplate.java | 20 ++++++++++++++++++++ .../eu/siacs/conversations/entities/Presences.java | 11 +++++++++++ .../services/XmppConnectionService.java | 10 ++++++++++ .../siacs/conversations/ui/SetPresenceActivity.java | 4 ++-- .../eu/siacs/conversations/xmpp/XmppConnection.java | 6 ++---- 6 files changed, 49 insertions(+), 6 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index c6b655d0a..1cdf0d673 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -79,6 +79,10 @@ public class Account extends AbstractEntity { } } + public Contact getSelfContact() { + return getRoster().getContact(jid); + } + public enum State { DISABLED, OFFLINE, diff --git a/src/main/java/eu/siacs/conversations/entities/PresenceTemplate.java b/src/main/java/eu/siacs/conversations/entities/PresenceTemplate.java index 3b3ce08db..c268b24c2 100644 --- a/src/main/java/eu/siacs/conversations/entities/PresenceTemplate.java +++ b/src/main/java/eu/siacs/conversations/entities/PresenceTemplate.java @@ -53,4 +53,24 @@ public class PresenceTemplate extends AbstractEntity { public String getStatusMessage() { return statusMessage; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + PresenceTemplate template = (PresenceTemplate) o; + + if (statusMessage != null ? !statusMessage.equals(template.statusMessage) : template.statusMessage != null) + return false; + return status == template.status; + + } + + @Override + public int hashCode() { + int result = statusMessage != null ? statusMessage.hashCode() : 0; + result = 31 * result + status.hashCode(); + return result; + } } diff --git a/src/main/java/eu/siacs/conversations/entities/Presences.java b/src/main/java/eu/siacs/conversations/entities/Presences.java index 754175ab2..7630fd936 100644 --- a/src/main/java/eu/siacs/conversations/entities/Presences.java +++ b/src/main/java/eu/siacs/conversations/entities/Presences.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.Hashtable; import java.util.Iterator; import java.util.List; +import java.util.Map; import eu.siacs.conversations.xml.Element; @@ -54,6 +55,16 @@ public class Presences { } } + public List asTemplates() { + synchronized (this.presences) { + ArrayList templates = new ArrayList<>(presences.size()); + for(Presence p : presences.values()) { + templates.add(new PresenceTemplate(p.getStatus(),p.getMessage())); + } + return templates; + } + } + public boolean has(String presence) { synchronized (this.presences) { return presences.containsKey(presence); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index ff84e6567..1da05ce5e 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -3285,6 +3285,16 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa } } + public List getPresenceTemplates(Account account) { + List templates = databaseBackend.getPresenceTemplates(); + for(PresenceTemplate template : account.getSelfContact().getPresences().asTemplates()) { + if (!templates.contains(template)) { + templates.add(0, template); + } + } + return templates; + } + public interface OnMamPreferencesFetched { void onPreferencesFetched(Element prefs); void onPreferencesFetchFailed(); diff --git a/src/main/java/eu/siacs/conversations/ui/SetPresenceActivity.java b/src/main/java/eu/siacs/conversations/ui/SetPresenceActivity.java index 6d13e7eac..0962d209b 100644 --- a/src/main/java/eu/siacs/conversations/ui/SetPresenceActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/SetPresenceActivity.java @@ -118,7 +118,7 @@ public class SetPresenceActivity extends XmppActivity implements View.OnClickLis xmppConnectionService.changeStatus(status, statusMessage); finish(); } else if (mAccount != null) { - if (mAccount.getPgpId() == 0 && hasPgp()) { + if (mAccount.getPgpId() == 0 || !hasPgp()) { xmppConnectionService.changeStatus(mAccount, status, statusMessage, true); finish(); } else { @@ -177,7 +177,7 @@ public class SetPresenceActivity extends XmppActivity implements View.OnClickLis if (mStatusMessage.getText().length() == 0 && message != null) { mStatusMessage.append(message); } - mTemplates = xmppConnectionService.databaseBackend.getPresenceTemplates(); + mTemplates = xmppConnectionService.getPresenceTemplates(mAccount); if (this.mPostponedActivityResult != null) { this.onActivityResult(mPostponedActivityResult.first, RESULT_OK, mPostponedActivityResult.second); } diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index f9f8fb3bf..c4d5bfdf1 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -1328,10 +1328,8 @@ public class XmppConnection implements Runnable { } socket.close(); Log.d(Config.LOGTAG,account.getJid().toBareJid()+": closed tcp without closing stream"); - } catch (IOException e) { - e.printStackTrace(); - } catch (InterruptedException e) { - e.printStackTrace(); + } catch (IOException | InterruptedException e) { + return; } } }).start(); -- cgit v1.2.3