diff options
Diffstat (limited to '')
-rw-r--r-- | src/main/java/eu/siacs/conversations/services/XmppConnectionService.java | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 84eeeeef2..c2f1810a5 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -79,6 +79,7 @@ import eu.siacs.conversations.entities.Roster; import eu.siacs.conversations.entities.ServiceDiscoveryResult; import eu.siacs.conversations.entities.Transferable; import eu.siacs.conversations.entities.TransferablePlaceholder; +import eu.siacs.conversations.generator.AbstractGenerator; import eu.siacs.conversations.generator.IqGenerator; import eu.siacs.conversations.generator.MessageGenerator; import eu.siacs.conversations.generator.PresenceGenerator; @@ -140,6 +141,9 @@ public class XmppConnectionService extends Service { private final List<Conversation> conversations = new CopyOnWriteArrayList<>(); private final IqGenerator mIqGenerator = new IqGenerator(this); private final List<String> mInProgressAvatarFetches = new ArrayList<>(); + + private long mLastActivity = 0; + public DatabaseBackend databaseBackend; private ContentObserver contactObserver = new ContentObserver(null) { @Override @@ -1578,6 +1582,7 @@ public class XmppConnectionService extends Service { public void setOnConversationListChangedListener(OnConversationUpdate listener) { synchronized (this) { + this.mLastActivity = System.currentTimeMillis(); if (checkListeners()) { switchToForeground(); } @@ -1790,15 +1795,21 @@ public class XmppConnectionService extends Service { } private void switchToForeground() { + final boolean broadcastLastActivity = broadcastLastActivity(); for (Conversation conversation : getConversations()) { conversation.setIncomingChatState(ChatState.ACTIVE); } for (Account account : getAccounts()) { if (account.getStatus() == Account.State.ONLINE) { account.deactivateGracePeriod(); - XmppConnection connection = account.getXmppConnection(); - if (connection != null && connection.getFeatures().csi()) { - connection.sendActive(); + final XmppConnection connection = account.getXmppConnection(); + if (connection != null ) { + if (connection.getFeatures().csi()) { + connection.sendActive(); + } + if (broadcastLastActivity) { + sendPresence(account, false); //send new presence but don't include idle because we are not + } } } } @@ -1806,10 +1817,14 @@ public class XmppConnectionService extends Service { } private void switchToBackground() { + final boolean broadcastLastActivity = broadcastLastActivity(); for (Account account : getAccounts()) { if (account.getStatus() == Account.State.ONLINE) { XmppConnection connection = account.getXmppConnection(); if (connection != null) { + if (broadcastLastActivity) { + sendPresence(account, broadcastLastActivity); + } if (connection.getFeatures().csi()) { connection.sendInactive(); } @@ -2247,6 +2262,7 @@ public class XmppConnectionService extends Service { private void disconnect(Account account, boolean force) { if ((account.getStatus() == Account.State.ONLINE) || (account.getStatus() == Account.State.DISABLED)) { + final XmppConnection connection = account.getXmppConnection(); if (!force) { List<Conversation> conversations = getConversations(); for (Conversation conversation : conversations) { @@ -2264,7 +2280,7 @@ public class XmppConnectionService extends Service { } sendOfflinePresence(account); } - account.getXmppConnection().disconnect(force); + connection.disconnect(force); } } @@ -2808,6 +2824,10 @@ public class XmppConnectionService extends Service { return getPreferences().getBoolean("show_connection_options", false); } + public boolean broadcastLastActivity() { + return getPreferences().getBoolean("last_activity", false); + } + public int unreadCount() { int count = 0; for (Conversation conversation : getConversations()) { @@ -3046,6 +3066,10 @@ public class XmppConnectionService extends Service { } public void sendPresence(final Account account) { + sendPresence(account, checkListeners() && broadcastLastActivity()); + } + + private void sendPresence(final Account account, final boolean includeIdleTimestamp) { PresencePacket packet; if (manuallyChangePresence()) { packet = mPresenceGenerator.selfPresence(account, account.getPresenceStatus()); @@ -3056,6 +3080,10 @@ public class XmppConnectionService extends Service { } else { packet = mPresenceGenerator.selfPresence(account, getTargetPresence()); } + 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)); + } sendPresencePacket(account, packet); } @@ -3066,9 +3094,10 @@ public class XmppConnectionService extends Service { } public void refreshAllPresences() { + boolean includeIdleTimestamp = checkListeners() && broadcastLastActivity(); for (Account account : getAccounts()) { if (!account.isOptionSet(Account.OPTION_DISABLED)) { - sendPresence(account); + sendPresence(account, includeIdleTimestamp); } } } |