summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAlex Clayton <alex.clayton@isode.com>2016-03-11 13:18:15 (GMT)
committerAlex Clayton <alex.clayton@isode.com>2016-03-14 15:17:54 (GMT)
commita0a0b9fd74f6d99da1c756b36aad6cc0122f011b (patch)
tree0c79a9093b73db6eaf459ef4fc311006822a1201 /src
parentc77facbc3036590cfa347f38859f9f536f082d6b (diff)
downloadstroke-a0a0b9fd74f6d99da1c756b36aad6cc0122f011b.zip
stroke-a0a0b9fd74f6d99da1c756b36aad6cc0122f011b.tar.bz2
Add methods to PresenceOracle.
As per swiften patch 'Change bare JID presence lookup code to ignore priorities' (0f5ef716a50c8d9761cafda12aacf818cdfd6353) add a couple of methods and a test for PresenceOracle. Test-information: Unit tests still pass. Change-Id: If3961f29be821a065ffa854faeab7f20da666d25
Diffstat (limited to 'src')
-rw-r--r--src/com/isode/stroke/presence/PresenceOracle.java117
1 files changed, 117 insertions, 0 deletions
diff --git a/src/com/isode/stroke/presence/PresenceOracle.java b/src/com/isode/stroke/presence/PresenceOracle.java
index 7ac5b9d..e983d51 100644
--- a/src/com/isode/stroke/presence/PresenceOracle.java
+++ b/src/com/isode/stroke/presence/PresenceOracle.java
@@ -6,8 +6,11 @@ package com.isode.stroke.presence;
6 6
7import java.util.ArrayList; 7import java.util.ArrayList;
8import java.util.Collection; 8import java.util.Collection;
9import java.util.Comparator;
9import java.util.HashMap; 10import java.util.HashMap;
11import java.util.List;
10import java.util.Map; 12import java.util.Map;
13import java.util.PriorityQueue;
11 14
12import com.isode.stroke.client.StanzaChannel; 15import com.isode.stroke.client.StanzaChannel;
13import com.isode.stroke.elements.Presence; 16import com.isode.stroke.elements.Presence;
@@ -135,6 +138,120 @@ public class PresenceOracle {
135 results.addAll(presenceMap.values()); 138 results.addAll(presenceMap.values());
136 return results; 139 return results;
137 } 140 }
141
142 private static class PresenceAccountCmp implements Comparator<Presence> {
143
144 private static int preferenceFromStatusShow(StatusShow.Type showType) {
145 switch (showType) {
146 case FFC:
147 return 5;
148 case Online:
149 return 4;
150 case DND:
151 return 3;
152 case Away:
153 return 2;
154 case XA:
155 return 1;
156 case None:
157 return 0;
158 }
159 assert(false);
160 return -1;
161 }
162
163 @Override
164 public int compare(Presence a, Presence b) {
165 int aPreference = preferenceFromStatusShow(a.getShow());
166 int bPreference = preferenceFromStatusShow(b.getShow());
167
168 if (aPreference != bPreference) {
169 return (aPreference > bPreference) ? -1 : 1;
170 }
171 if (a.getPriority() != b.getPriority()) {
172 return (a.getPriority() > b.getPriority()) ? -1 : 1;
173 }
174 return -a.getFrom().getResource().compareTo(b.getFrom().getResource());
175 }
176
177 }
178
179 /**
180 * Returns the relevant presence for a list of resource presences.
181 *
182 * It only takes the presence show type into account. Priorities are
183 * ignored as various clients set them to arbitrary values unrelated
184 * to actual end point availability.
185 *
186 * The presences of the resources are group by availablilty and sorted
187 * by show type in the following order:
188 *
189 * -# Online
190 * -# Free for Chat
191 * -# Available
192 * -# Away
193 * -# DND
194 * -# Extended Away
195 * -# Away
196 * -# Offline
197 * -# Unavailable
198 * @param presences List of resource presences. Should not be null.
199 * @return The relevant presence.
200 */
201 public static Presence getActivePresence(Collection<? extends Presence> presences) {
202
203 PriorityQueue<Presence> online = new PriorityQueue<Presence>(presences.size(),new PresenceAccountCmp());
204 PriorityQueue<Presence> away = new PriorityQueue<Presence>(presences.size(),new PresenceAccountCmp());
205 PriorityQueue<Presence> offline = new PriorityQueue<Presence>(presences.size(),new PresenceAccountCmp());
206
207 for (Presence presence : presences) {
208 switch (presence.getShow()) {
209 case Online:
210 online.add(presence);
211 break;
212 case Away:
213 away.add(presence);
214 break;
215 case FFC:
216 online.add(presence);
217 break;
218 case XA:
219 away.add(presence);
220 break;
221 case DND:
222 away.add(presence);
223 break;
224 case None:
225 offline.add(presence);
226 break;
227 }
228 }
229
230 Presence accountPresence = null;
231 if (!online.isEmpty()) {
232 accountPresence = online.peek();
233 }
234 else if (!away.isEmpty()) {
235 accountPresence = away.peek();
236 }
237 else if (!offline.isEmpty()) {
238 accountPresence = offline.peek();
239 }
240 return accountPresence;
241 }
242
243 /**
244 * This considers all online resources of a bare JID and returns
245 * the value returned by {@link #getActivePresence(List)}
246 * when passing this list.
247 * @param jid A bare JID
248 * @return The value returned by {@link #getActivePresence(List)}
249 */
250 public Presence getAccountPresence(JID jid) {
251 Collection<Presence> allPresences = getAllPresence(jid.toBare());
252 Presence accountPresence = getActivePresence(allPresences);
253 return accountPresence;
254 }
138 255
139 public Presence getHighestPriorityPresence(final JID bareJID) { 256 public Presence getHighestPriorityPresence(final JID bareJID) {
140 Map<JID,Presence> presenceMap = entries_.get(bareJID); 257 Map<JID,Presence> presenceMap = entries_.get(bareJID);