From 38d9d96d1f36b5124bf211ce8cbce5446bf4c542 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Wed, 24 Jan 2018 14:17:22 +0100 Subject: [PATCH] Use set instead of list for handling OmemoDevice --- .../smackx/omemo/OmemoManager.java | 19 +++--- .../smackx/omemo/OmemoMessage.java | 10 +-- .../smackx/omemo/OmemoService.java | 67 ++++++++++--------- 3 files changed, 50 insertions(+), 46 deletions(-) diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoManager.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoManager.java index 9011fd0e9..7af975096 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoManager.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoManager.java @@ -26,6 +26,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Random; +import java.util.Set; import java.util.SortedSet; import java.util.TreeMap; import java.util.WeakHashMap; @@ -270,15 +271,15 @@ public final class OmemoManager extends Manager { } /** - * Return a list of all OMEMO capable devices of a contact. + * Return a set of all OMEMO capable devices of a contact. * Note, that this method does not explicitly refresh the device list of the contact, so it might be outdated. * @see #requestDeviceListUpdateFor(BareJid) - * @param contact contact we want to get a list of device of. - * @return list of known devices of that contact. + * @param contact contact we want to get a set of device of. + * @return set of known devices of that contact. */ - public List getDevicesOf(BareJid contact) { + public Set getDevicesOf(BareJid contact) { OmemoCachedDeviceList list = getOmemoService().getOmemoStoreBackend().loadCachedDeviceList(getOwnDevice(), contact); - ArrayList devices = new ArrayList<>(); + HashSet devices = new HashSet<>(); for (int deviceId : list.getActiveDevices()) { devices.add(new OmemoDevice(contact, deviceId)); @@ -307,7 +308,7 @@ public final class OmemoManager extends Manager { SmackException.NoResponseException, SmackException.NotLoggedInException { synchronized (LOCK) { - ArrayList recipients = new ArrayList<>(); + Set recipients = new HashSet<>(); recipients.add(recipient); return encrypt(recipients, message); } @@ -326,14 +327,14 @@ public final class OmemoManager extends Manager { * @throws SmackException.NoResponseException * @throws SmackException.NotLoggedInException */ - public OmemoMessage.Sent encrypt(ArrayList recipients, String message) + public OmemoMessage.Sent encrypt(Set recipients, String message) throws CryptoFailedException, UndecidedOmemoIdentityException, InterruptedException, SmackException.NotConnectedException, SmackException.NoResponseException, SmackException.NotLoggedInException { synchronized (LOCK) { LoggedInOmemoManager guard = new LoggedInOmemoManager(this); - List devices = getDevicesOf(getOwnJid()); + Set devices = getDevicesOf(getOwnJid()); for (BareJid recipient : recipients) { devices.addAll(getDevicesOf(recipient)); } @@ -367,7 +368,7 @@ public final class OmemoManager extends Manager { throw new NoOmemoSupportException(); } - ArrayList recipients = new ArrayList<>(); + Set recipients = new HashSet<>(); for (EntityFullJid e : muc.getOccupants()) { recipients.add(muc.getOccupant(e).getJid().asBareJid()); diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoMessage.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoMessage.java index af046e93c..823222d7c 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoMessage.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoMessage.java @@ -20,9 +20,9 @@ import static org.jivesoftware.smackx.omemo.util.OmemoConstants.BODY_OMEMO_HINT; import static org.jivesoftware.smackx.omemo.util.OmemoConstants.OMEMO; import static org.jivesoftware.smackx.omemo.util.OmemoConstants.OMEMO_NAMESPACE_V_AXOLOTL; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; +import java.util.HashSet; +import java.util.Set; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smackx.eme.element.ExplicitMessageEncryptionElement; @@ -74,7 +74,7 @@ public class OmemoMessage { * Outgoing OMEMO message. */ public static class Sent extends OmemoMessage { - private final ArrayList intendedDevices = new ArrayList<>(); + private final Set intendedDevices = new HashSet<>(); private final HashMap skippedDevices = new HashMap<>(); /** @@ -86,7 +86,7 @@ public class OmemoMessage { * @param skippedDevices devices which were skipped during encryption process because encryption * failed for some reason */ - Sent(OmemoElement element, byte[] key, byte[] iv, List intendedDevices, HashMap skippedDevices) { + Sent(OmemoElement element, byte[] key, byte[] iv, Set intendedDevices, HashMap skippedDevices) { super(element, key, iv); this.intendedDevices.addAll(intendedDevices); this.skippedDevices.putAll(skippedDevices); @@ -96,7 +96,7 @@ public class OmemoMessage { * Return a list of all devices the sender originally intended to encrypt the message for. * @return list of intended recipients. */ - public List getIntendedDevices() { + public Set getIntendedDevices() { return intendedDevices; } diff --git a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java index 9e9f3c8a0..82fc14400 100644 --- a/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java +++ b/smack-omemo/src/main/java/org/jivesoftware/smackx/omemo/OmemoService.java @@ -30,6 +30,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Random; import java.util.Set; @@ -326,7 +327,7 @@ public abstract class OmemoService contactsDevices, + Set contactsDevices, byte[] messageKey, byte[] iv, String message) @@ -353,7 +354,7 @@ public abstract class OmemoService undecidedDevices = getUndecidedDevices(userDevice, manager.getTrustCallback(), contactsDevices); + Set undecidedDevices = getUndecidedDevices(userDevice, manager.getTrustCallback(), contactsDevices); if (!undecidedDevices.isEmpty()) { throw new UndecidedOmemoIdentityException(undecidedDevices); } @@ -472,7 +473,7 @@ public abstract class OmemoServiceXEP-0384: Sending a key. * * @param managerGuard Initialized OmemoManager. - * @param contactsDevices recipient devices. + * @param contactsDevices set of recipient devices. * @param key AES-Key to be transported. * @param iv initialization vector to be used with the key. * @return KeyTransportElement @@ -484,7 +485,7 @@ public abstract class OmemoService contactsDevices, + Set contactsDevices, byte[] key, byte[] iv) throws InterruptedException, UndecidedOmemoIdentityException, CryptoFailedException, @@ -494,11 +495,13 @@ public abstract class OmemoService contactsDevices, + Set contactsDevices, String message) throws InterruptedException, UndecidedOmemoIdentityException, CryptoFailedException, SmackException.NotConnectedException, SmackException.NoResponseException @@ -764,24 +767,24 @@ public abstract class OmemoService buildMissingSessionsWithContact(XMPPConnection connection, + private Set buildMissingSessionsWithContact(XMPPConnection connection, OmemoDevice userDevice, BareJid contact) throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { OmemoCachedDeviceList contactsDeviceIds = getOmemoStoreBackend().loadCachedDeviceList(userDevice, contact); - ArrayList contactsDevices = new ArrayList<>(); + Set contactsDevices = new HashSet<>(); for (int deviceId : contactsDeviceIds.getActiveDevices()) { contactsDevices.add(new OmemoDevice(contact, deviceId)); } @@ -790,22 +793,22 @@ public abstract class OmemoService buildMissingSessionsWithDevices(XMPPConnection connection, + private Set buildMissingSessionsWithDevices(XMPPConnection connection, OmemoDevice userDevice, - List devices) + Set devices) throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { - ArrayList devicesWithSession = new ArrayList<>(); + Set devicesWithSession = new HashSet<>(); for (OmemoDevice device : devices) { if (hasSession(userDevice, device)) { @@ -831,34 +834,34 @@ public abstract class OmemoService buildMissingSessionsWithContacts(XMPPConnection connection, + private Set buildMissingSessionsWithContacts(XMPPConnection connection, OmemoDevice userDevice, - List contacts) + Set contacts) throws SmackException.NotConnectedException, InterruptedException, SmackException.NoResponseException { - ArrayList devicesWithSessions = new ArrayList<>(); + Set devicesWithSessions = new HashSet<>(); for (BareJid contact : contacts) { - ArrayList devices = buildMissingSessionsWithContact(connection, userDevice, contact); + Set devices = buildMissingSessionsWithContact(connection, userDevice, contact); devicesWithSessions.addAll(devices); } return devicesWithSessions; } - private ArrayList getUndecidedDevices(OmemoDevice userDevice, OmemoTrustCallback callback, List devices) { - ArrayList undecidedDevices = new ArrayList<>(); + private Set getUndecidedDevices(OmemoDevice userDevice, OmemoTrustCallback callback, Set devices) { + Set undecidedDevices = new HashSet<>(); for (OmemoDevice device : devices) { @@ -880,8 +883,8 @@ public abstract class OmemoService getUntrustedDeviced(OmemoDevice userDevice, OmemoTrustCallback trustCallback, List devices) { - ArrayList untrustedDevices = new ArrayList<>(); + private Set getUntrustedDeviced(OmemoDevice userDevice, OmemoTrustCallback trustCallback, Set devices) { + Set untrustedDevices = new HashSet<>(); for (OmemoDevice device : devices) {