2015-03-03 11:44:49 -08:00
|
|
|
package org.thoughtcrime.securesms.crypto;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.storage.TextSecureSessionStore;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.SignalProtocolAddress;
|
2017-06-06 18:03:09 -07:00
|
|
|
import org.whispersystems.libsignal.state.SessionRecord;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.state.SessionStore;
|
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
2015-03-03 11:44:49 -08:00
|
|
|
|
2017-06-06 18:03:09 -07:00
|
|
|
import java.util.List;
|
|
|
|
|
2015-03-03 11:44:49 -08:00
|
|
|
public class SessionUtil {
|
|
|
|
|
|
|
|
public static boolean hasSession(Context context, MasterSecret masterSecret, Recipient recipient) {
|
|
|
|
return hasSession(context, masterSecret, recipient.getNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean hasSession(Context context, MasterSecret masterSecret, @NonNull String number) {
|
2016-03-23 10:34:41 -07:00
|
|
|
SessionStore sessionStore = new TextSecureSessionStore(context, masterSecret);
|
|
|
|
SignalProtocolAddress axolotlAddress = new SignalProtocolAddress(number, SignalServiceAddress.DEFAULT_DEVICE_ID);
|
2015-03-03 11:44:49 -08:00
|
|
|
|
|
|
|
return sessionStore.containsSession(axolotlAddress);
|
|
|
|
}
|
2017-06-06 18:03:09 -07:00
|
|
|
|
|
|
|
public static void archiveSiblingSessions(Context context, SignalProtocolAddress address) {
|
|
|
|
SessionStore sessionStore = new TextSecureSessionStore(context);
|
|
|
|
List<Integer> devices = sessionStore.getSubDeviceSessions(address.getName());
|
|
|
|
devices.add(1);
|
|
|
|
|
|
|
|
for (int device : devices) {
|
|
|
|
if (device != address.getDeviceId()) {
|
|
|
|
SignalProtocolAddress sibling = new SignalProtocolAddress(address.getName(), device);
|
|
|
|
|
|
|
|
if (sessionStore.containsSession(sibling)) {
|
|
|
|
SessionRecord sessionRecord = sessionStore.loadSession(sibling);
|
|
|
|
sessionRecord.archiveCurrentState();
|
|
|
|
sessionStore.storeSession(sibling, sessionRecord);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-05 13:12:59 -07:00
|
|
|
}
|
2017-06-06 18:03:09 -07:00
|
|
|
|
2017-07-05 13:12:59 -07:00
|
|
|
public static void archiveAllSessions(Context context) {
|
|
|
|
new TextSecureSessionStore(context).archiveAllSessions();
|
2017-06-06 18:03:09 -07:00
|
|
|
}
|
2015-03-03 11:44:49 -08:00
|
|
|
}
|