2017-06-02 12:26:37 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright 2017 Paul Schaub
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smackx.omemo.element;
|
|
|
|
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashSet;
|
2019-12-13 18:28:09 +01:00
|
|
|
import java.util.Iterator;
|
2017-06-02 12:26:37 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
|
|
|
import org.jivesoftware.smack.util.Objects;
|
|
|
|
import org.jivesoftware.smack.util.XmlStringBuilder;
|
2019-05-06 22:06:13 +02:00
|
|
|
|
2018-06-13 12:29:16 +02:00
|
|
|
import org.jivesoftware.smackx.omemo.internal.OmemoCachedDeviceList;
|
2017-06-02 12:26:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A OMEMO device list update containing the IDs of all active devices of a contact.
|
|
|
|
*
|
|
|
|
* @author Paul Schaub
|
|
|
|
*/
|
|
|
|
public abstract class OmemoDeviceListElement implements ExtensionElement {
|
|
|
|
|
|
|
|
public static final String DEVICE = "device";
|
|
|
|
public static final String ID = "id";
|
|
|
|
public static final String LIST = "list";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unmodifiable set of device IDs.
|
|
|
|
*/
|
|
|
|
private final Set<Integer> deviceIds;
|
|
|
|
|
|
|
|
public OmemoDeviceListElement(Set<Integer> deviceIds) {
|
|
|
|
deviceIds = Objects.requireNonNull(deviceIds);
|
|
|
|
this.deviceIds = Collections.unmodifiableSet(deviceIds);
|
|
|
|
}
|
|
|
|
|
2018-06-13 12:29:16 +02:00
|
|
|
public OmemoDeviceListElement(OmemoCachedDeviceList cachedList) {
|
|
|
|
this.deviceIds = Collections.unmodifiableSet(cachedList.getActiveDevices());
|
|
|
|
}
|
|
|
|
|
2017-06-02 12:26:37 +02:00
|
|
|
public Set<Integer> getDeviceIds() {
|
|
|
|
return deviceIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Set<Integer> copyDeviceIds() {
|
|
|
|
return new HashSet<>(deviceIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getElementName() {
|
|
|
|
return LIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-02-04 13:27:41 +01:00
|
|
|
public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
|
2017-06-02 12:26:37 +02:00
|
|
|
XmlStringBuilder sb = new XmlStringBuilder(this).rightAngleBracket();
|
|
|
|
|
|
|
|
for (Integer id : deviceIds) {
|
|
|
|
sb.halfOpenElement(DEVICE).attribute(ID, id).closeEmptyElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
sb.closeElement(this);
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final String toString() {
|
2019-12-13 18:28:09 +01:00
|
|
|
StringBuilder sb = new StringBuilder("OmemoDeviceListElement[");
|
|
|
|
Iterator<Integer> iterator = deviceIds.iterator();
|
2017-06-02 12:26:37 +02:00
|
|
|
for (int i : deviceIds) {
|
2019-12-13 18:28:09 +01:00
|
|
|
sb.append(i);
|
|
|
|
if (iterator.hasNext()) {
|
|
|
|
sb.append(',');
|
|
|
|
}
|
2017-06-02 12:26:37 +02:00
|
|
|
}
|
2019-12-13 18:28:09 +01:00
|
|
|
return sb.append(']').toString();
|
2017-06-02 12:26:37 +02:00
|
|
|
}
|
|
|
|
}
|