rework last activity logic to something that should work pretty well in Conversations only enviroments

This commit is contained in:
Daniel Gultsch 2017-03-20 17:58:58 +01:00
parent 644a3a729d
commit 76b9010c39
10 changed files with 33 additions and 16 deletions

View file

@ -528,8 +528,13 @@ public class Contact implements ListItem, Blockable {
return this.mActive; return this.mActive;
} }
public void setLastseen(long timestamp) { public boolean setLastseen(long timestamp) {
this.mLastseen = Math.max(timestamp, mLastseen); if (timestamp > this.mLastseen) {
this.mLastseen = timestamp;
return true;
} else {
return false;
}
} }
public long getLastseen() { public long getLastseen() {

View file

@ -3,15 +3,11 @@ package eu.siacs.conversations.entities;
import android.util.Pair; import android.util.Pair;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import eu.siacs.conversations.xml.Element;
public class Presences { public class Presences {
private final Hashtable<String, Presence> presences = new Hashtable<>(); private final Hashtable<String, Presence> presences = new Hashtable<>();

View file

@ -17,6 +17,7 @@ import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.axolotl.AxolotlService; import eu.siacs.conversations.crypto.axolotl.AxolotlService;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.utils.PhoneHelper; import eu.siacs.conversations.utils.PhoneHelper;
import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xmpp.jingle.stanzas.Content; import eu.siacs.conversations.xmpp.jingle.stanzas.Content;
public abstract class AbstractGenerator { public abstract class AbstractGenerator {
@ -118,6 +119,9 @@ public abstract class AbstractGenerator {
if (Config.supportOtr()) { if (Config.supportOtr()) {
features.addAll(Arrays.asList(OTR)); features.addAll(Arrays.asList(OTR));
} }
if (mXmppConnectionService.broadcastLastActivity()) {
features.add(Namespace.IDLE);
}
Collections.sort(features); Collections.sort(features);
return features; return features;
} }

View file

@ -19,6 +19,7 @@ import eu.siacs.conversations.generator.IqGenerator;
import eu.siacs.conversations.generator.PresenceGenerator; import eu.siacs.conversations.generator.PresenceGenerator;
import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xmpp.OnPresencePacketReceived; import eu.siacs.conversations.xmpp.OnPresencePacketReceived;
import eu.siacs.conversations.xmpp.jid.Jid; import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.pep.Avatar; import eu.siacs.conversations.xmpp.pep.Avatar;
@ -211,18 +212,19 @@ public class PresenceParser extends AbstractParser implements
mXmppConnectionService.fetchCaps(account, from, presence); 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) { if (idle != null) {
contact.flagInactive(); contact.flagInactive();
String since = idle.getAttribute("since"); final String since = idle.getAttribute("since");
try { try {
contact.setLastseen(AbstractParser.parseTimestamp(since)); contact.setLastseen(AbstractParser.parseTimestamp(since));
} catch (NullPointerException | ParseException e) { } catch (NullPointerException | ParseException e) {
contact.setLastseen(System.currentTimeMillis()); contact.setLastseen(System.currentTimeMillis());
} }
} else { } else {
contact.flagActive(); if (contact.setLastseen(AbstractParser.parseTimestamp(packet))) {
contact.setLastseen(AbstractParser.parseTimestamp(packet)); contact.flagActive();
}
} }
PgpEngine pgp = mXmppConnectionService.getPgpEngine(); PgpEngine pgp = mXmppConnectionService.getPgpEngine();
@ -235,6 +237,9 @@ public class PresenceParser extends AbstractParser implements
boolean online = sizeBefore < contact.getPresences().size(); boolean online = sizeBefore < contact.getPresences().size();
mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, online); mXmppConnectionService.onContactStatusChanged.onContactStatusChanged(contact, online);
} else if (type.equals("unavailable")) { } else if (type.equals("unavailable")) {
if (contact.setLastseen(AbstractParser.parseTimestamp(packet))) {
contact.flagInactive();
}
if (from.isBareJid()) { if (from.isBareJid()) {
contact.clearPresences(); contact.clearPresences();
} else { } else {

View file

@ -3261,7 +3261,7 @@ public class XmppConnectionService extends Service {
} }
public boolean broadcastLastActivity() { public boolean broadcastLastActivity() {
return getPreferences().getBoolean("last_activity", false); return getPreferences().getBoolean(SettingsActivity.BROADCAST_LAST_ACTIVITY, false);
} }
public int unreadCount() { public int unreadCount() {
@ -3527,7 +3527,7 @@ public class XmppConnectionService extends Service {
} }
if (mLastActivity > 0 && includeIdleTimestamp) { if (mLastActivity > 0 && includeIdleTimestamp) {
long since = Math.min(mLastActivity, System.currentTimeMillis()); //don't send future dates 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); sendPresencePacket(account, packet);
} }

View file

@ -48,6 +48,7 @@ import eu.siacs.conversations.services.XmppConnectionService.OnRosterUpdate;
import eu.siacs.conversations.utils.CryptoHelper; import eu.siacs.conversations.utils.CryptoHelper;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.utils.XmppUri; import eu.siacs.conversations.utils.XmppUri;
import eu.siacs.conversations.xml.Namespace;
import eu.siacs.conversations.xmpp.OnKeyStatusUpdated; import eu.siacs.conversations.xmpp.OnKeyStatusUpdated;
import eu.siacs.conversations.xmpp.OnUpdateBlocklist; import eu.siacs.conversations.xmpp.OnUpdateBlocklist;
import eu.siacs.conversations.xmpp.XmppConnection; import eu.siacs.conversations.xmpp.XmppConnection;
@ -410,7 +411,9 @@ public class ContactDetailsActivity extends OmemoActivity implements OnAccountUp
lastseen.setVisibility(View.VISIBLE); lastseen.setVisibility(View.VISIBLE);
lastseen.setText(R.string.contact_blocked); lastseen.setText(R.string.contact_blocked);
} else { } else {
if (showLastSeen && contact.getLastseen() > 0) { if (showLastSeen
&& contact.getLastseen() > 0
&& contact.getPresences().allOrNonSupport(Namespace.IDLE)) {
lastseen.setVisibility(View.VISIBLE); lastseen.setVisibility(View.VISIBLE);
lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen())); lastseen.setText(UIHelper.lastseen(getApplicationContext(), contact.isActive(), contact.getLastseen()));
} else { } else {

View file

@ -45,6 +45,7 @@ public class SettingsActivity extends XmppActivity implements
public static final String MANUALLY_CHANGE_PRESENCE = "manually_change_presence"; public static final String MANUALLY_CHANGE_PRESENCE = "manually_change_presence";
public static final String BLIND_TRUST_BEFORE_VERIFICATION = "btbv"; public static final String BLIND_TRUST_BEFORE_VERIFICATION = "btbv";
public static final String AUTOMATIC_MESSAGE_DELETION = "automatic_message_deletion"; 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; public static final int REQUEST_WRITE_LOGS = 0xbf8701;
private SettingsFragment mSettingsFragment; private SettingsFragment mSettingsFragment;
@ -334,7 +335,7 @@ public class SettingsActivity extends XmppActivity implements
"allow_message_correction", "allow_message_correction",
TREAT_VIBRATE_AS_SILENT, TREAT_VIBRATE_AS_SILENT,
MANUALLY_CHANGE_PRESENCE, MANUALLY_CHANGE_PRESENCE,
"last_activity"); BROADCAST_LAST_ACTIVITY);
if (name.equals("resource")) { if (name.equals("resource")) {
String resource = preferences.getString("resource", "mobile") String resource = preferences.getString("resource", "mobile")
.toLowerCase(Locale.US); .toLowerCase(Locale.US);

View file

@ -115,8 +115,9 @@ public class UIHelper {
public static String lastseen(Context context, boolean active, long time) { public static String lastseen(Context context, boolean active, long time) {
long difference = (System.currentTimeMillis() - time) / 1000; long difference = (System.currentTimeMillis() - time) / 1000;
active = active && difference <= 300; if (active) {
if (active || difference < 60) { return context.getString(R.string.online_right_now);
} else if (difference < 60) {
return context.getString(R.string.last_seen_now); return context.getString(R.string.last_seen_now);
} else if (difference < 60 * 2) { } else if (difference < 60 * 2) {
return context.getString(R.string.last_seen_min); return context.getString(R.string.last_seen_min);

View file

@ -9,4 +9,5 @@ public final class Namespace {
public static final String STANZA_IDS = "urn:xmpp:sid:0"; public static final String STANZA_IDS = "urn:xmpp:sid:0";
public static final String MAM = "urn:xmpp:mam:2"; public static final String MAM = "urn:xmpp:mam:2";
public static final String MAM_LEGACY = "urn:xmpp:mam:0"; public static final String MAM_LEGACY = "urn:xmpp:mam:0";
public static final String IDLE = "urn:xmpp:idle:1";
} }

View file

@ -743,4 +743,5 @@
<string name="received_message_from_stranger">Received message from stranger</string> <string name="received_message_from_stranger">Received message from stranger</string>
<string name="block_stranger">Block stranger</string> <string name="block_stranger">Block stranger</string>
<string name="block_entire_domain">Block entire domain</string> <string name="block_entire_domain">Block entire domain</string>
<string name="online_right_now">online right now</string>
</resources> </resources>