From 76b9010c396155d956098ad48b86ec8ab0dd360e Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Mon, 20 Mar 2017 17:58:58 +0100 Subject: [PATCH] rework last activity logic to something that should work pretty well in Conversations only enviroments --- .../eu/siacs/conversations/entities/Contact.java | 9 +++++++-- .../eu/siacs/conversations/entities/Presences.java | 4 ---- .../conversations/generator/AbstractGenerator.java | 4 ++++ .../siacs/conversations/parser/PresenceParser.java | 13 +++++++++---- .../services/XmppConnectionService.java | 4 ++-- .../conversations/ui/ContactDetailsActivity.java | 5 ++++- .../eu/siacs/conversations/ui/SettingsActivity.java | 3 ++- .../java/eu/siacs/conversations/utils/UIHelper.java | 5 +++-- .../java/eu/siacs/conversations/xml/Namespace.java | 1 + src/main/res/values/strings.xml | 1 + 10 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Contact.java b/src/main/java/eu/siacs/conversations/entities/Contact.java index 28c052b70..76670faff 100644 --- a/src/main/java/eu/siacs/conversations/entities/Contact.java +++ b/src/main/java/eu/siacs/conversations/entities/Contact.java @@ -528,8 +528,13 @@ public class Contact implements ListItem, Blockable { return this.mActive; } - public void setLastseen(long timestamp) { - this.mLastseen = Math.max(timestamp, mLastseen); + public boolean setLastseen(long timestamp) { + if (timestamp > this.mLastseen) { + this.mLastseen = timestamp; + return true; + } else { + return false; + } } public long getLastseen() { diff --git a/src/main/java/eu/siacs/conversations/entities/Presences.java b/src/main/java/eu/siacs/conversations/entities/Presences.java index f9dfee36b..bf1774bf5 100644 --- a/src/main/java/eu/siacs/conversations/entities/Presences.java +++ b/src/main/java/eu/siacs/conversations/entities/Presences.java @@ -3,15 +3,11 @@ package eu.siacs.conversations.entities; import android.util.Pair; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.Hashtable; -import java.util.Iterator; import java.util.List; import java.util.Map; -import eu.siacs.conversations.xml.Element; - public class Presences { private final Hashtable presences = new Hashtable<>(); diff --git a/src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java b/src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java index 8edc24c3f..2866ccf52 100644 --- a/src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java +++ b/src/main/java/eu/siacs/conversations/generator/AbstractGenerator.java @@ -17,6 +17,7 @@ import eu.siacs.conversations.R; import eu.siacs.conversations.crypto.axolotl.AxolotlService; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.utils.PhoneHelper; +import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xmpp.jingle.stanzas.Content; public abstract class AbstractGenerator { @@ -118,6 +119,9 @@ public abstract class AbstractGenerator { if (Config.supportOtr()) { features.addAll(Arrays.asList(OTR)); } + if (mXmppConnectionService.broadcastLastActivity()) { + features.add(Namespace.IDLE); + } Collections.sort(features); return features; } diff --git a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java index de02cd34c..4ff11401e 100644 --- a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java +++ b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java @@ -19,6 +19,7 @@ import eu.siacs.conversations.generator.IqGenerator; import eu.siacs.conversations.generator.PresenceGenerator; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.xml.Element; +import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xmpp.OnPresencePacketReceived; import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.pep.Avatar; @@ -211,18 +212,19 @@ public class PresenceParser extends AbstractParser implements mXmppConnectionService.fetchCaps(account, from, presence); } - final Element idle = packet.findChild("idle","urn:xmpp:idle:1"); + final Element idle = packet.findChild("idle", Namespace.IDLE); if (idle != null) { contact.flagInactive(); - String since = idle.getAttribute("since"); + final String since = idle.getAttribute("since"); try { contact.setLastseen(AbstractParser.parseTimestamp(since)); } catch (NullPointerException | ParseException e) { contact.setLastseen(System.currentTimeMillis()); } } else { - contact.flagActive(); - contact.setLastseen(AbstractParser.parseTimestamp(packet)); + if (contact.setLastseen(AbstractParser.parseTimestamp(packet))) { + contact.flagActive(); + } } PgpEngine pgp = mXmppConnectionService.getPgpEngine(); @@ -235,6 +237,9 @@ public class PresenceParser extends AbstractParser implements boolean online = sizeBefore < contact.getPresences().size(); mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, online); } else if (type.equals("unavailable")) { + if (contact.setLastseen(AbstractParser.parseTimestamp(packet))) { + contact.flagInactive(); + } if (from.isBareJid()) { contact.clearPresences(); } else { diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index e19d84903..56717488f 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -3261,7 +3261,7 @@ public class XmppConnectionService extends Service { } public boolean broadcastLastActivity() { - return getPreferences().getBoolean("last_activity", false); + return getPreferences().getBoolean(SettingsActivity.BROADCAST_LAST_ACTIVITY, false); } public int unreadCount() { @@ -3527,7 +3527,7 @@ public class XmppConnectionService extends Service { } if (mLastActivity > 0 && includeIdleTimestamp) { long since = Math.min(mLastActivity, System.currentTimeMillis()); //don't send future dates - packet.addChild("idle","urn:xmpp:idle:1").setAttribute("since", AbstractGenerator.getTimestamp(since)); + packet.addChild("idle",Namespace.IDLE).setAttribute("since", AbstractGenerator.getTimestamp(since)); } sendPresencePacket(account, packet); } diff --git a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java index fca344d77..6afc58d73 100644 --- a/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ContactDetailsActivity.java @@ -48,6 +48,7 @@ import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate; import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.XmppUri; +import eu.siacs.conversations.xml.Namespace; import eu.siacs.conversations.xmpp.OnKeyStatusUpdated; import eu.siacs.conversations.xmpp.OnUpdateBlocklist; import eu.siacs.conversations.xmpp.XmppConnection; @@ -410,7 +411,9 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp lastseen.setVisibility(View.VISIBLE); lastseen.setText(R.string.contact_blocked); } else { - if (showLastSeen && contact.getLastseen() > 0) { + if (showLastSeen + && contact.getLastseen() > 0 + && contact.getPresences().allOrNonSupport(Namespace.IDLE)) { lastseen.setVisibility(View.VISIBLE); lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen())); } else { diff --git a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java index da12266ea..d48839cdd 100644 --- a/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/SettingsActivity.java @@ -45,6 +45,7 @@ public class SettingsActivity extends XmppActivity implements public static final String MANUALLY_CHANGE_PRESENCE = "manually_change_presence"; public static final String BLIND_TRUST_BEFORE_VERIFICATION = "btbv"; public static final String AUTOMATIC_MESSAGE_DELETION = "automatic_message_deletion"; + public static final String BROADCAST_LAST_ACTIVITY = "last_activity"; public static final int REQUEST_WRITE_LOGS = 0xbf8701; private SettingsFragment mSettingsFragment; @@ -334,7 +335,7 @@ public class SettingsActivity extends XmppActivity implements "allow_message_correction", TREAT_VIBRATE_AS_SILENT, MANUALLY_CHANGE_PRESENCE, - "last_activity"); + BROADCAST_LAST_ACTIVITY); if (name.equals("resource")) { String resource = preferences.getString("resource", "mobile") .toLowerCase(Locale.US); diff --git a/src/main/java/eu/siacs/conversations/utils/UIHelper.java b/src/main/java/eu/siacs/conversations/utils/UIHelper.java index eb398641f..b16b97d49 100644 --- a/src/main/java/eu/siacs/conversations/utils/UIHelper.java +++ b/src/main/java/eu/siacs/conversations/utils/UIHelper.java @@ -115,8 +115,9 @@ public class UIHelper { public static String lastseen(Context context, boolean active, long time) { long difference = (System.currentTimeMillis() - time) / 1000; - active = active && difference <= 300; - if (active || difference < 60) { + if (active) { + return context.getString(R.string.online_right_now); + } else if (difference < 60) { return context.getString(R.string.last_seen_now); } else if (difference < 60 * 2) { return context.getString(R.string.last_seen_min); diff --git a/src/main/java/eu/siacs/conversations/xml/Namespace.java b/src/main/java/eu/siacs/conversations/xml/Namespace.java index bf99e9ca8..913ed0357 100644 --- a/src/main/java/eu/siacs/conversations/xml/Namespace.java +++ b/src/main/java/eu/siacs/conversations/xml/Namespace.java @@ -9,4 +9,5 @@ public final class Namespace { public static final String STANZA_IDS = "urn:xmpp:sid:0"; public static final String MAM = "urn:xmpp:mam:2"; public static final String MAM_LEGACY = "urn:xmpp:mam:0"; + public static final String IDLE = "urn:xmpp:idle:1"; } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index ac9f7f716..1e000706a 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -743,4 +743,5 @@ Received message from stranger Block stranger Block entire domain + online right now