OmemoCachedDeviceList.java

  1. /**
  2.  *
  3.  * Copyright 2017 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.omemo.internal;

  18. import java.io.Serializable;
  19. import java.util.HashSet;
  20. import java.util.Set;

  21. /**
  22.  * This class is used to represent device lists of contacts.
  23.  * There are active devices (a set of device ids, which was published with the last device list update)
  24.  * and inactive devices (set of devices that once were active, but are not included in recent list updates).
  25.  * Both kinds are cached by the client. When a device that was active in the last update is not included in
  26.  * a new update, it becomes an inactive device. Vice versa, inactive devices can also become active again, by
  27.  * being included in the latest device list update.
  28.  * <p>
  29.  * The client ensures, that his own device id is on the list of active devices, as soon as he gets online.
  30.  *
  31.  * @author Paul Schaub
  32.  */
  33. public class OmemoCachedDeviceList implements Serializable {
  34.     private static final long serialVersionUID = 3153579238321261203L;

  35.     private final Set<Integer> activeDevices;
  36.     private final Set<Integer> inactiveDevices;

  37.     public OmemoCachedDeviceList() {
  38.         this.activeDevices = new HashSet<>();
  39.         this.inactiveDevices = new HashSet<>();
  40.     }

  41.     public OmemoCachedDeviceList(Set<Integer> activeDevices, Set<Integer> inactiveDevices) {
  42.         this();
  43.         this.activeDevices.addAll(activeDevices);
  44.         this.inactiveDevices.addAll(inactiveDevices);
  45.     }

  46.     public OmemoCachedDeviceList(OmemoCachedDeviceList original) {
  47.         this(original.getActiveDevices(), original.getInactiveDevices());
  48.     }

  49.     /**
  50.      * Returns all active devices.
  51.      * Active devices are all devices that were in the latest DeviceList update.
  52.      *
  53.      * @return active devices
  54.      */
  55.     public Set<Integer> getActiveDevices() {
  56.         return activeDevices;
  57.     }

  58.     /**
  59.      * Return all inactive devices.
  60.      * Inactive devices are devices which were in a past DeviceList update once, but were not included in
  61.      * the latest update.
  62.      *
  63.      * @return inactive devices
  64.      */
  65.     public Set<Integer> getInactiveDevices() {
  66.         return inactiveDevices;
  67.     }

  68.     /**
  69.      * Returns an OmemoDeviceListElement containing all devices (active and inactive).
  70.      *
  71.      * @return all devices
  72.      */
  73.     public Set<Integer> getAllDevices() {
  74.         Set<Integer> all = new HashSet<>();
  75.         all.addAll(activeDevices);
  76.         all.addAll(inactiveDevices);
  77.         return all;
  78.     }

  79.     /**
  80.      * Merge a device list update into the CachedDeviceList.
  81.      * The source code should be self explanatory.
  82.      *
  83.      * @param deviceListUpdate received device list update
  84.      */
  85.     public void merge(Set<Integer> deviceListUpdate) {
  86.         inactiveDevices.addAll(activeDevices);
  87.         activeDevices.clear();
  88.         activeDevices.addAll(deviceListUpdate);
  89.         inactiveDevices.removeAll(activeDevices);
  90.     }

  91.     /**
  92.      * Add a device to the list of active devices and remove it from inactive.
  93.      *
  94.      * @param deviceId deviceId that will be added
  95.      */
  96.     public void addDevice(int deviceId) {
  97.         activeDevices.add(deviceId);
  98.         inactiveDevices.remove(deviceId);
  99.     }

  100.     public void addInactiveDevice(int deviceId) {
  101.         activeDevices.remove(deviceId);
  102.         inactiveDevices.add(deviceId);
  103.     }

  104.     /**
  105.      * Returns true if deviceId is either in the list of active or inactive devices.
  106.      *
  107.      * @param deviceId id
  108.      * @return true or false
  109.      */
  110.     public boolean contains(int deviceId) {
  111.         return activeDevices.contains(deviceId) || inactiveDevices.contains(deviceId);
  112.     }

  113.     public boolean isActive(int deviceId) {
  114.         return getActiveDevices().contains(deviceId);
  115.     }

  116.     @Override
  117.     public String toString() {
  118.         String out = "active: [";
  119.         for (int id : activeDevices) {
  120.             out += id + " ";
  121.         }
  122.         out += "] inacitve: [";
  123.         for (int id : inactiveDevices) {
  124.             out += id + " ";
  125.         }
  126.         out += "]";
  127.         return out;
  128.     }
  129. }