aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/entities/Presence.java
blob: 14836c1453fa253a40774341ca1633298122e0cb (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
package de.pixart.messenger.entities;

import android.support.annotation.NonNull;

import java.util.Locale;

import de.pixart.messenger.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;
        }

        public static Status fromShowString(String show) {
            if (show == null) {
                return ONLINE;
            } else {
                switch (show.toLowerCase(Locale.US)) {
                    case "away":
                        return AWAY;
                    case "xa":
                        return XA;
                    case "dnd":
                        return DND;
                    case "chat":
                        return CHAT;
                    default:
                        return ONLINE;
                }
            }
        }
    }

    private final Status status;
    private ServiceDiscoveryResult disco;
    private final String ver;
    private final String hash;
    private final String node;
    private final String message;

    private Presence(Status status, String ver, String hash, String node, String message) {
        this.status = status;
        this.ver = ver;
        this.hash = hash;
        this.node = node;
        this.message = message;
    }

    public static Presence parse(String show, Element caps, String message) {
        final String hash = caps == null ? null : caps.getAttribute("hash");
        final String ver = caps == null ? null : caps.getAttribute("ver");
        final String node = caps == null ? null : caps.getAttribute("node");
        return new Presence(Status.fromShowString(show), ver, hash, node, message);
    }

    public int compareTo(@NonNull Object other) {
        return this.status.compareTo(((Presence) other).status);
    }

    public Status getStatus() {
        return this.status;
    }

    public boolean hasCaps() {
        return ver != null && hash != null;
    }

    public String getVer() {
        return this.ver;
    }

    public String getNode() {
        return this.node;
    }

    public String getHash() {
        return this.hash;
    }

    public String getMessage() {
        return this.message;
    }

    public void setServiceDiscoveryResult(ServiceDiscoveryResult disco) {
        this.disco = disco;
    }

    public ServiceDiscoveryResult getServiceDiscoveryResult() {
        return disco;
    }
}