From bc50239c2af2b458c07ab85b755dc2ed3f5d2ac7 Mon Sep 17 00:00:00 2001 From: ChaosKid42 Date: Sat, 26 May 2018 15:57:33 +0200 Subject: [PATCH] improve active_since implementation (XEP-0319: Last User Interaction in Presence) (#3045) * set contact to inactive if it becomes offline * send idle also in case app started in background --- .../siacs/conversations/parser/PresenceParser.java | 3 +++ .../services/XmppConnectionService.java | 12 +++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java index 610d40e9e..5bd714937 100644 --- a/src/main/java/eu/siacs/conversations/parser/PresenceParser.java +++ b/src/main/java/eu/siacs/conversations/parser/PresenceParser.java @@ -287,6 +287,9 @@ public class PresenceParser extends AbstractParser implements } else { contact.removePresence(from.getResource()); } + if (contact.getShownStatus() == Presence.Status.OFFLINE) { + contact.flagInactive(); + } mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, false); } else if (type.equals("subscribe")) { if (contact.getOption(Contact.Options.PREEMPTIVE_GRANT)) { diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 43c97d154..84c705f87 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -151,6 +151,8 @@ public class XmppConnectionService extends Service { public static final String ACTION_FCM_MESSAGE_RECEIVED = "fcm_message_received"; private static final String ACTION_MERGE_PHONE_CONTACTS = "merge_phone_contacts"; + private static final String SETTING_LAST_ACTIVITY_TS = "last_activity_timestamp"; + static { URL.setURLStreamHandlerFactory(new CustomURLStreamHandlerFactory()); } @@ -943,6 +945,9 @@ public class XmppConnectionService extends Service { return bitmap.getByteCount() / 1024; } }; + if (mLastActivity == 0) { + mLastActivity = getPreferences().getLong(SETTING_LAST_ACTIVITY_TS, System.currentTimeMillis()); + } Log.d(Config.LOGTAG, "initializing database..."); this.databaseBackend = DatabaseBackend.getInstance(getApplicationContext()); @@ -1871,7 +1876,6 @@ public class XmppConnectionService extends Service { public void setOnConversationListChangedListener(OnConversationUpdate listener) { synchronized (this) { - this.mLastActivity = System.currentTimeMillis(); if (checkListeners()) { switchToForeground(); } @@ -2054,6 +2058,12 @@ public class XmppConnectionService extends Service { private void switchToBackground() { final boolean broadcastLastActivity = broadcastLastActivity(); + if (broadcastLastActivity) { + mLastActivity = System.currentTimeMillis(); + final SharedPreferences.Editor editor = getPreferences().edit(); + editor.putLong(SETTING_LAST_ACTIVITY_TS, mLastActivity); + editor.apply(); + } for (Account account : getAccounts()) { if (account.getStatus() == Account.State.ONLINE) { XmppConnection connection = account.getXmppConnection();