aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/entities/Presences.java
blob: 6afd973fca2e97ef11f90eec6ee82b0cddeaf2d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package de.pixart.messenger.entities;

import android.util.Pair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

public class Presences {
    private final Hashtable<String, Presence> presences = new Hashtable<>();

    public Hashtable<String, Presence> getPresences() {
        return this.presences;
    }

    public void updatePresence(String resource, Presence presence) {
        synchronized (this.presences) {
            this.presences.put(resource, presence);
        }
    }

    public void removePresence(String resource) {
        synchronized (this.presences) {
            this.presences.remove(resource);
        }
    }

    public void clearPresences() {
        synchronized (this.presences) {
            this.presences.clear();
        }
    }

    public Presence.Status getShownStatus() {
        Presence.Status status = Presence.Status.OFFLINE;
        synchronized (this.presences) {
            for (Presence p : presences.values()) {
                if (p.getStatus() == Presence.Status.DND) {
                    return p.getStatus();
                } else if (p.getStatus().compareTo(status) < 0) {
                    status = p.getStatus();
                }
            }
        }
        return status;
    }

    public int size() {
        synchronized (this.presences) {
            return presences.size();
        }
    }

    public String[] toResourceArray() {
        synchronized (this.presences) {
            final String[] presencesArray = new String[presences.size()];
            presences.keySet().toArray(presencesArray);
            return presencesArray;
        }
    }

    public List<PresenceTemplate> asTemplates() {
        synchronized (this.presences) {
            ArrayList<PresenceTemplate> templates = new ArrayList<>(presences.size());
            for (Presence p : presences.values()) {
                if (p.getMessage() != null && !p.getMessage().trim().isEmpty()) {
                    templates.add(new PresenceTemplate(p.getStatus(), p.getMessage()));
                }
            }
            return templates;
        }
    }

    public boolean has(String presence) {
        synchronized (this.presences) {
            return presences.containsKey(presence);
        }
    }

    public List<String> getStatusMessages() {
        ArrayList<String> messages = new ArrayList<>();
        synchronized (this.presences) {
            for (Presence presence : this.presences.values()) {
                String message = presence.getMessage() == null ? null : presence.getMessage().trim();
                if (message != null && !message.isEmpty() && !messages.contains(message)) {
                    messages.add(message);
                }
            }
        }
        return messages;
    }

    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;
    }

    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);
    }
}