2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
2016-07-28 20:58:37 +00:00
|
|
|
import android.util.Pair;
|
|
|
|
|
2016-04-23 10:19:00 +00:00
|
|
|
import java.util.ArrayList;
|
2016-01-17 21:28:38 +00:00
|
|
|
import java.util.Collections;
|
2016-07-28 20:58:37 +00:00
|
|
|
import java.util.HashMap;
|
2014-10-22 16:38:44 +00:00
|
|
|
import java.util.Hashtable;
|
|
|
|
import java.util.Iterator;
|
2016-04-23 10:19:00 +00:00
|
|
|
import java.util.List;
|
2016-05-13 08:45:30 +00:00
|
|
|
import java.util.Map;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
import eu.siacs.conversations.xml.Element;
|
|
|
|
|
|
|
|
public class Presences {
|
2016-01-17 21:28:38 +00:00
|
|
|
private final Hashtable<String, Presence> presences = new Hashtable<>();
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2016-01-17 21:28:38 +00:00
|
|
|
public Hashtable<String, Presence> getPresences() {
|
2014-10-22 16:38:44 +00:00
|
|
|
return this.presences;
|
|
|
|
}
|
|
|
|
|
2016-01-17 21:28:38 +00:00
|
|
|
public void updatePresence(String resource, Presence presence) {
|
2014-11-13 13:44:19 +00:00
|
|
|
synchronized (this.presences) {
|
2016-01-17 21:28:38 +00:00
|
|
|
this.presences.put(resource, presence);
|
2014-11-13 13:44:19 +00:00
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void removePresence(String resource) {
|
2014-11-13 13:44:19 +00:00
|
|
|
synchronized (this.presences) {
|
|
|
|
this.presences.remove(resource);
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clearPresences() {
|
2014-11-13 13:44:19 +00:00
|
|
|
synchronized (this.presences) {
|
|
|
|
this.presences.clear();
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-30 21:08:55 +00:00
|
|
|
public Presence.Status getShownStatus() {
|
|
|
|
Presence.Status status = Presence.Status.OFFLINE;
|
2014-11-13 13:44:19 +00:00
|
|
|
synchronized (this.presences) {
|
2016-06-30 21:08:55 +00:00
|
|
|
for(Presence p : presences.values()) {
|
|
|
|
if (p.getStatus() == Presence.Status.DND) {
|
|
|
|
return p.getStatus();
|
|
|
|
} else if (p.getStatus().compareTo(status) < 0){
|
|
|
|
status = p.getStatus();
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2016-06-30 21:08:55 +00:00
|
|
|
return status;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public int size() {
|
2014-11-13 13:44:19 +00:00
|
|
|
synchronized (this.presences) {
|
|
|
|
return presences.size();
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-07-28 20:58:37 +00:00
|
|
|
public String[] toResourceArray() {
|
2014-11-13 13:44:19 +00:00
|
|
|
synchronized (this.presences) {
|
|
|
|
final String[] presencesArray = new String[presences.size()];
|
|
|
|
presences.keySet().toArray(presencesArray);
|
|
|
|
return presencesArray;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 08:45:30 +00:00
|
|
|
public List<PresenceTemplate> asTemplates() {
|
|
|
|
synchronized (this.presences) {
|
|
|
|
ArrayList<PresenceTemplate> templates = new ArrayList<>(presences.size());
|
|
|
|
for(Presence p : presences.values()) {
|
2016-05-31 22:25:14 +00:00
|
|
|
if (p.getMessage() != null && !p.getMessage().trim().isEmpty()) {
|
|
|
|
templates.add(new PresenceTemplate(p.getStatus(), p.getMessage()));
|
|
|
|
}
|
2016-05-13 08:45:30 +00:00
|
|
|
}
|
|
|
|
return templates;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public boolean has(String presence) {
|
2014-11-13 13:44:19 +00:00
|
|
|
synchronized (this.presences) {
|
|
|
|
return presences.containsKey(presence);
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2016-04-23 10:19:00 +00:00
|
|
|
|
|
|
|
public List<String> getStatusMessages() {
|
|
|
|
ArrayList<String> messages = new ArrayList<>();
|
|
|
|
synchronized (this.presences) {
|
|
|
|
for(Presence presence : this.presences.values()) {
|
2016-05-13 09:57:02 +00:00
|
|
|
String message = presence.getMessage() == null ? null : presence.getMessage().trim();
|
|
|
|
if (message != null && !message.isEmpty() && !messages.contains(message)) {
|
|
|
|
messages.add(message);
|
2016-04-23 10:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return messages;
|
|
|
|
}
|
2016-05-12 12:20:11 +00:00
|
|
|
|
|
|
|
public boolean allOrNonSupport(String namespace) {
|
|
|
|
synchronized (this.presences) {
|
|
|
|
for(Presence presence : this.presences.values()) {
|
|
|
|
ServiceDiscoveryResult disco = presence.getServiceDiscoveryResult();
|
|
|
|
if (disco == null || !disco.getFeatures().contains(namespace)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-07-28 20:58:37 +00:00
|
|
|
|
|
|
|
public Pair<Map<String, String>,Map<String,String>> toTypeAndNameMap() {
|
|
|
|
Map<String,String> typeMap = new HashMap<>();
|
|
|
|
Map<String,String> nameMap = new HashMap<>();
|
|
|
|
synchronized (this.presences) {
|
|
|
|
for(Map.Entry<String,Presence> presenceEntry : this.presences.entrySet()) {
|
|
|
|
String resource = presenceEntry.getKey();
|
|
|
|
Presence presence = presenceEntry.getValue();
|
|
|
|
ServiceDiscoveryResult serviceDiscoveryResult = presence == null ? null : presence.getServiceDiscoveryResult();
|
|
|
|
if (serviceDiscoveryResult != null && serviceDiscoveryResult.getIdentities().size() > 0) {
|
|
|
|
ServiceDiscoveryResult.Identity identity = serviceDiscoveryResult.getIdentities().get(0);
|
|
|
|
String type = identity.getType();
|
|
|
|
String name = identity.getName();
|
|
|
|
if (type != null) {
|
|
|
|
typeMap.put(resource,type);
|
|
|
|
}
|
|
|
|
if (name != null) {
|
|
|
|
nameMap.put(resource, name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Pair(typeMap,nameMap);
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|