diff options
author | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-02-09 00:47:11 +0100 |
---|---|---|
committer | Daniel Gultsch <daniel.gultsch@rwth-aachen.de> | 2014-02-09 00:47:11 +0100 |
commit | c31101dd6458225a007466a3d58a2f4b591c684f (patch) | |
tree | 5606bbb1679301e6bbc51b92d5d6372d10789793 /src/de/gultsch/chat/entities/Presences.java | |
parent | 95068ee776d9ec8e6c390133ca2bed083a46546f (diff) |
presences are now somewhat stored and displayed to the user via the contact details action.
Diffstat (limited to 'src/de/gultsch/chat/entities/Presences.java')
-rw-r--r-- | src/de/gultsch/chat/entities/Presences.java | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/src/de/gultsch/chat/entities/Presences.java b/src/de/gultsch/chat/entities/Presences.java new file mode 100644 index 000000000..aabc84404 --- /dev/null +++ b/src/de/gultsch/chat/entities/Presences.java @@ -0,0 +1,76 @@ +package de.gultsch.chat.entities; + +import java.util.Hashtable; +import java.util.Iterator; +import java.util.Map.Entry; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +public class Presences { + + 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 Hashtable<String, Integer> presences = new Hashtable<String, Integer>(); + + public Hashtable<String, Integer> getPresences() { + return this.presences; + } + + public void updatePresence(String resource, int status) { + this.presences.put(resource, status); + } + + public void removePresence(String resource) { + this.presences.remove(resource); + } + + public int getMostAvailableStatus() { + int status = OFFLINE; + 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 String toJsonString() { + JSONArray json = new JSONArray(); + Iterator<Entry<String, Integer>> it = presences.entrySet().iterator(); + + while (it.hasNext()) { + Entry<String, Integer> entry = it.next(); + JSONObject jObj = new JSONObject(); + try { + jObj.put("resource", entry.getKey()); + jObj.put("status", entry.getValue()); + } catch (JSONException e) { + + } + json.put(jObj); + } + return json.toString(); + } + + public static Presences fromJsonString(String jsonString) { + Presences presences = new Presences(); + try { + JSONArray json = new JSONArray(jsonString); + for (int i = 0; i < json.length(); ++i) { + JSONObject jObj = json.getJSONObject(i); + presences.updatePresence(jObj.getString("resource"), + jObj.getInt("status")); + } + } catch (JSONException e1) { + + } + return presences; + } +} |