aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/entities
diff options
context:
space:
mode:
authorStephen Paul Weber <singpolyma@singpolyma.net>2016-01-17 16:28:38 -0500
committerStephen Paul Weber <singpolyma@singpolyma.net>2016-01-24 17:46:08 -0500
commitbf5b2f73f5a61f0a0179c9d4431579f87eecb001 (patch)
tree5bd8b9aaa8b4326dfecb699fd1cdcf97a02b8097 /src/main/java/eu/siacs/conversations/entities
parentad36a4ba89f880a5522ffd5179dcaa98985e4164 (diff)
Use a Presence class for presence information
Only has status for now, but doing it so I can add disco to it
Diffstat (limited to 'src/main/java/eu/siacs/conversations/entities')
-rw-r--r--src/main/java/eu/siacs/conversations/entities/Contact.java23
-rw-r--r--src/main/java/eu/siacs/conversations/entities/Presence.java49
-rw-r--r--src/main/java/eu/siacs/conversations/entities/Presences.java46
3 files changed, 71 insertions, 47 deletions
diff --git a/src/main/java/eu/siacs/conversations/entities/Contact.java b/src/main/java/eu/siacs/conversations/entities/Contact.java
index c2d8b278..ed773b6e 100644
--- a/src/main/java/eu/siacs/conversations/entities/Contact.java
+++ b/src/main/java/eu/siacs/conversations/entities/Contact.java
@@ -135,17 +135,17 @@ public class Contact implements ListItem, Blockable {
tags.add(new Tag(group, UIHelper.getColorForName(group)));
}
switch (getMostAvailableStatus()) {
- case Presences.CHAT:
- case Presences.ONLINE:
+ case CHAT:
+ case ONLINE:
tags.add(new Tag("online", 0xff259b24));
break;
- case Presences.AWAY:
+ case AWAY:
tags.add(new Tag("away", 0xffff9800));
break;
- case Presences.XA:
+ case XA:
tags.add(new Tag("not available", 0xfff44336));
break;
- case Presences.DND:
+ case DND:
tags.add(new Tag("dnd", 0xfff44336));
break;
}
@@ -225,8 +225,8 @@ public class Contact implements ListItem, Blockable {
this.presences = pres;
}
- public void updatePresence(final String resource, final int status) {
- this.presences.updatePresence(resource, status);
+ public void updatePresence(final String resource, final Presence presence) {
+ this.presences.updatePresence(resource, presence);
}
public void removePresence(final String resource) {
@@ -238,8 +238,13 @@ public class Contact implements ListItem, Blockable {
this.resetOption(Options.PENDING_SUBSCRIPTION_REQUEST);
}
- public int getMostAvailableStatus() {
- return this.presences.getMostAvailableStatus();
+ public Presence.Status getMostAvailableStatus() {
+ Presence p = this.presences.getMostAvailablePresence();
+ if (p == null) {
+ return Presence.Status.OFFLINE;
+ }
+
+ return p.getStatus();
}
public boolean setPhotoUri(String uri) {
diff --git a/src/main/java/eu/siacs/conversations/entities/Presence.java b/src/main/java/eu/siacs/conversations/entities/Presence.java
new file mode 100644
index 00000000..adba74d1
--- /dev/null
+++ b/src/main/java/eu/siacs/conversations/entities/Presence.java
@@ -0,0 +1,49 @@
+package eu.siacs.conversations.entities;
+
+import java.lang.Comparable;
+
+import eu.siacs.conversations.xml.Element;
+
+public class Presence implements Comparable {
+
+ public enum Status {
+ CHAT, ONLINE, AWAY, XA, DND, OFFLINE;
+
+ public String toShowString() {
+ switch(this) {
+ case CHAT: return "chat";
+ case AWAY: return "away";
+ case XA: return "xa";
+ case DND: return "dnd";
+ }
+
+ return null;
+ }
+ }
+
+ protected final Status status;
+
+ public Presence(Element show) {
+ if ((show == null) || (show.getContent() == null)) {
+ this.status = Status.ONLINE;
+ } else if (show.getContent().equals("away")) {
+ this.status = Status.AWAY;
+ } else if (show.getContent().equals("xa")) {
+ this.status = Status.XA;
+ } else if (show.getContent().equals("chat")) {
+ this.status = Status.CHAT;
+ } else if (show.getContent().equals("dnd")) {
+ this.status = Status.DND;
+ } else {
+ this.status = Status.OFFLINE;
+ }
+ }
+
+ public int compareTo(Object other) {
+ return this.status.compareTo(((Presence)other).status);
+ }
+
+ public Status getStatus() {
+ return this.status;
+ }
+}
diff --git a/src/main/java/eu/siacs/conversations/entities/Presences.java b/src/main/java/eu/siacs/conversations/entities/Presences.java
index 4729a11b..813eda7a 100644
--- a/src/main/java/eu/siacs/conversations/entities/Presences.java
+++ b/src/main/java/eu/siacs/conversations/entities/Presences.java
@@ -1,29 +1,21 @@
package eu.siacs.conversations.entities;
+import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
-import java.util.Map.Entry;
import eu.siacs.conversations.xml.Element;
public class Presences {
+ private final Hashtable<String, Presence> presences = new Hashtable<>();
- public static final int CHAT = -1;
- public static final int ONLINE = 0;
- public static final int AWAY = 1;
- public static final int XA = 2;
- public static final int DND = 3;
- public static final int OFFLINE = 4;
-
- private final Hashtable<String, Integer> presences = new Hashtable<>();
-
- public Hashtable<String, Integer> getPresences() {
+ public Hashtable<String, Presence> getPresences() {
return this.presences;
}
- public void updatePresence(String resource, int status) {
+ public void updatePresence(String resource, Presence presence) {
synchronized (this.presences) {
- this.presences.put(resource, status);
+ this.presences.put(resource, presence);
}
}
@@ -39,32 +31,10 @@ public class Presences {
}
}
- public int getMostAvailableStatus() {
- int status = OFFLINE;
+ public Presence getMostAvailablePresence() {
synchronized (this.presences) {
- Iterator<Entry<String, Integer>> it = presences.entrySet().iterator();
- while (it.hasNext()) {
- Entry<String, Integer> entry = it.next();
- if (entry.getValue() < status)
- status = entry.getValue();
- }
- }
- return status;
- }
-
- public static int parseShow(Element show) {
- if ((show == null) || (show.getContent() == null)) {
- return Presences.ONLINE;
- } else if (show.getContent().equals("away")) {
- return Presences.AWAY;
- } else if (show.getContent().equals("xa")) {
- return Presences.XA;
- } else if (show.getContent().equals("chat")) {
- return Presences.CHAT;
- } else if (show.getContent().equals("dnd")) {
- return Presences.DND;
- } else {
- return Presences.OFFLINE;
+ if (presences.size() < 1) { return null; }
+ return Collections.min(presences.values());
}
}