Presence.java

  1package eu.siacs.conversations.entities;
  2
  3import androidx.annotation.NonNull;
  4
  5import java.util.Locale;
  6
  7import eu.siacs.conversations.xml.Element;
  8
  9public class Presence implements Comparable<Presence> {
 10
 11	public enum Status {
 12		CHAT, ONLINE, AWAY, XA, DND, OFFLINE;
 13
 14		public String toShowString() {
 15			switch(this) {
 16				case CHAT: return "chat";
 17				case AWAY: return "away";
 18				case XA:   return "xa";
 19				case DND:  return "dnd";
 20			}
 21			return null;
 22		}
 23
 24		public static Status fromShowString(String show) {
 25			if (show == null) {
 26				return ONLINE;
 27			} else {
 28				switch (show.toLowerCase(Locale.US)) {
 29					case "away":
 30						return AWAY;
 31					case "xa":
 32						return XA;
 33					case "dnd":
 34						return DND;
 35					case "chat":
 36						return CHAT;
 37					default:
 38						return ONLINE;
 39				}
 40			}
 41		}
 42	}
 43
 44	private final Status status;
 45	private ServiceDiscoveryResult disco;
 46	private final String ver;
 47	private final String hash;
 48	private final String node;
 49	private final String message;
 50
 51	public Presence(Status status, String ver, String hash, String node, String message) {
 52		this.status = status;
 53		this.ver = ver;
 54		this.hash = hash;
 55		this.node = node;
 56		this.message = message;
 57	}
 58
 59	public static Presence parse(String show, Element caps, String message) {
 60		final String hash = caps == null ? null : caps.getAttribute("hash");
 61		final String ver = caps == null ? null : caps.getAttribute("ver");
 62		final String node = caps == null ? null : caps.getAttribute("node");
 63		return new Presence(Status.fromShowString(show), ver, hash, node, message);
 64	}
 65
 66	public int compareTo(@NonNull Presence other) {
 67		return this.status.compareTo(other.status);
 68	}
 69
 70	public Status getStatus() {
 71		return this.status;
 72	}
 73
 74	public boolean hasCaps() {
 75		return ver != null && hash != null;
 76	}
 77
 78	public String getVer() {
 79		return this.ver;
 80	}
 81
 82	public String getNode() {
 83		return this.node;
 84	}
 85
 86	public String getHash() {
 87		return this.hash;
 88	}
 89
 90	public String getMessage() {
 91		return this.message;
 92	}
 93
 94	public void setServiceDiscoveryResult(ServiceDiscoveryResult disco) {
 95		this.disco = disco;
 96	}
 97
 98	public ServiceDiscoveryResult getServiceDiscoveryResult() {
 99		return disco;
100	}
101}