diff options
author | Christian Schneppe <christian@pix-art.de> | 2016-11-20 17:32:24 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2016-11-20 17:32:24 +0100 |
commit | 7d72a6bcf10f3c52af1f9a453225e04d7e209350 (patch) | |
tree | 98ed2e5406b18e16675118cfbbc66a6b6fe1a381 /src/main/java/de/pixart/messenger/crypto/axolotl | |
parent | 80b71f7f462faef91fca628aabe0f6d5c20e0b90 (diff) |
save last activation time in fingerprint status
Diffstat (limited to 'src/main/java/de/pixart/messenger/crypto/axolotl')
4 files changed, 48 insertions, 7 deletions
diff --git a/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java b/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java index 13c80ef96..8c9b5a7bb 100644 --- a/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java +++ b/src/main/java/de/pixart/messenger/crypto/axolotl/AxolotlService.java @@ -25,7 +25,10 @@ import java.security.PrivateKey; import java.security.Security; import java.security.Signature; import java.security.cert.X509Certificate; +import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -296,14 +299,18 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { return new AxolotlAddress(jid.toPreppedString(), 0); } - public Set<XmppAxolotlSession> findOwnSessions() { + public Collection<XmppAxolotlSession> findOwnSessions() { AxolotlAddress ownAddress = getAddressForJid(account.getJid().toBareJid()); - return new HashSet<>(this.sessions.getAll(ownAddress).values()); + ArrayList<XmppAxolotlSession> s = new ArrayList<>(this.sessions.getAll(ownAddress).values()); + Collections.sort(s); + return s; } - public Set<XmppAxolotlSession> findSessionsForContact(Contact contact) { + public Collection<XmppAxolotlSession> findSessionsForContact(Contact contact) { AxolotlAddress contactAddress = getAddressForJid(contact.getJid()); - return new HashSet<>(this.sessions.getAll(contactAddress).values()); + ArrayList<XmppAxolotlSession> s = new ArrayList<>(this.sessions.getAll(contactAddress).values()); + Collections.sort(s); + return s; } private Set<XmppAxolotlSession> findSessionsForConversation(Conversation conversation) { @@ -929,7 +936,7 @@ public class AxolotlService implements OnAdvancedStreamFeaturesLoaded { account.getJid().toBareJid(), getOwnDeviceId()); Set<XmppAxolotlSession> remoteSessions = findSessionsForConversation(conversation); - Set<XmppAxolotlSession> ownSessions = findOwnSessions(); + Collection<XmppAxolotlSession> ownSessions = findOwnSessions(); if (remoteSessions.isEmpty()) { return null; } diff --git a/src/main/java/de/pixart/messenger/crypto/axolotl/FingerprintStatus.java b/src/main/java/de/pixart/messenger/crypto/axolotl/FingerprintStatus.java index 3be469b5a..bffb94e1f 100644 --- a/src/main/java/de/pixart/messenger/crypto/axolotl/FingerprintStatus.java +++ b/src/main/java/de/pixart/messenger/crypto/axolotl/FingerprintStatus.java @@ -3,10 +3,13 @@ package de.pixart.messenger.crypto.axolotl; import android.content.ContentValues; import android.database.Cursor; -public class FingerprintStatus { +public class FingerprintStatus implements Comparable<FingerprintStatus> { + + private static final long DO_NOT_OVERWRITE = -1; private Trust trust = Trust.UNTRUSTED; private boolean active = false; + private long lastActivation = DO_NOT_OVERWRITE; @Override public boolean equals(Object o) { @@ -34,6 +37,9 @@ public class FingerprintStatus { final ContentValues contentValues = new ContentValues(); contentValues.put(SQLiteAxolotlStore.TRUST, trust.toString()); contentValues.put(SQLiteAxolotlStore.ACTIVE, active ? 1 : 0); + if (lastActivation != DO_NOT_OVERWRITE) { + contentValues.put(SQLiteAxolotlStore.LAST_ACTIVATION,lastActivation); + } return contentValues; } @@ -45,6 +51,7 @@ public class FingerprintStatus { status.trust = Trust.UNTRUSTED; } status.active = cursor.getInt(cursor.getColumnIndex(SQLiteAxolotlStore.ACTIVE)) > 0; + status.lastActivation = cursor.getLong(cursor.getColumnIndex(SQLiteAxolotlStore.LAST_ACTIVATION)); return status; } @@ -52,6 +59,7 @@ public class FingerprintStatus { final FingerprintStatus status = new FingerprintStatus(); status.trust = Trust.UNDECIDED; status.active = true; + status.lastActivation = System.currentTimeMillis(); return status; } @@ -92,6 +100,9 @@ public class FingerprintStatus { public FingerprintStatus toActive() { FingerprintStatus status = new FingerprintStatus(); status.trust = trust; + if (!status.active) { + status.lastActivation = System.currentTimeMillis(); + } status.active = true; return status; } @@ -128,6 +139,23 @@ public class FingerprintStatus { return status; } + @Override + public int compareTo(FingerprintStatus o) { + if (active == o.active) { + if (lastActivation > o.lastActivation) { + return -1; + } else if (lastActivation < o.lastActivation) { + return 1; + } else { + return 0; + } + } else if (active){ + return -1; + } else { + return 1; + } + } + public enum Trust { COMPROMISED, UNDECIDED, diff --git a/src/main/java/de/pixart/messenger/crypto/axolotl/SQLiteAxolotlStore.java b/src/main/java/de/pixart/messenger/crypto/axolotl/SQLiteAxolotlStore.java index c45e5045e..0d246795d 100644 --- a/src/main/java/de/pixart/messenger/crypto/axolotl/SQLiteAxolotlStore.java +++ b/src/main/java/de/pixart/messenger/crypto/axolotl/SQLiteAxolotlStore.java @@ -38,6 +38,7 @@ public class SQLiteAxolotlStore implements AxolotlStore { public static final String TRUSTED = "trusted"; //no longer used public static final String TRUST = "trust"; public static final String ACTIVE = "active"; + public static final String LAST_ACTIVATION = "last_activation"; public static final String OWN = "ownkey"; public static final String CERTIFICATE = "certificate"; diff --git a/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java b/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java index f1e5589bf..d850e18e7 100644 --- a/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java +++ b/src/main/java/de/pixart/messenger/crypto/axolotl/XmppAxolotlSession.java @@ -22,7 +22,7 @@ import org.whispersystems.libaxolotl.protocol.WhisperMessage; import de.pixart.messenger.Config; import de.pixart.messenger.entities.Account; -public class XmppAxolotlSession { +public class XmppAxolotlSession implements Comparable<XmppAxolotlSession> { private final SessionCipher cipher; private final SQLiteAxolotlStore sqLiteAxolotlStore; private final AxolotlAddress remoteAddress; @@ -136,4 +136,9 @@ public class XmppAxolotlSession { public Account getAccount() { return account; } + + @Override + public int compareTo(XmppAxolotlSession o) { + return getTrust().compareTo(o.getTrust()); + } } |