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;
}
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() {

View File

@ -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<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.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;
}

View File

@ -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 {

View File

@ -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);
}

View File

@ -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 {

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 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);

View File

@ -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);

View File

@ -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";
}

View File

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