1package de.gultsch.chat.entities;
2
3import java.io.Serializable;
4import java.util.HashSet;
5import java.util.Hashtable;
6import java.util.Set;
7
8import org.json.JSONArray;
9import org.json.JSONException;
10import org.json.JSONObject;
11
12import android.content.ContentValues;
13import android.database.Cursor;
14
15public class Contact extends AbstractEntity implements Serializable {
16 private static final long serialVersionUID = -4570817093119419962L;
17
18 public static final String TABLENAME = "contacts";
19
20 public static final String DISPLAYNAME = "name";
21 public static final String JID = "jid";
22 public static final String SUBSCRIPTION = "subscription";
23 public static final String SYSTEMACCOUNT = "systemaccount";
24 public static final String PHOTOURI = "photouri";
25 public static final String KEYS = "pgpkey";
26 public static final String PRESENCES = "presences";
27 public static final String ACCOUNT = "accountUuid";
28
29 protected String accountUuid;
30 protected String displayName;
31 protected String jid;
32 protected String subscription;
33 protected String systemAccount;
34 protected String photoUri;
35 protected JSONObject keys = new JSONObject();
36 protected Presences presences = new Presences();
37
38 protected Account account;
39
40 public Contact(Account account, String displayName, String jid,
41 String photoUri) {
42 if (account == null) {
43 this.accountUuid = null;
44 } else {
45 this.accountUuid = account.getUuid();
46 }
47 this.displayName = displayName;
48 this.jid = jid;
49 this.photoUri = photoUri;
50 this.uuid = java.util.UUID.randomUUID().toString();
51 }
52
53 public Contact(String uuid, String account, String displayName, String jid,
54 String subscription, String photoUri, String systemAccount,
55 String keys, String presences) {
56 this.uuid = uuid;
57 this.accountUuid = account;
58 this.displayName = displayName;
59 this.jid = jid;
60 this.subscription = subscription;
61 this.photoUri = photoUri;
62 this.systemAccount = systemAccount;
63 if (keys == null) {
64 keys = "";
65 }
66 try {
67 this.keys = new JSONObject(keys);
68 } catch (JSONException e) {
69 this.keys = new JSONObject();
70 }
71 this.presences = Presences.fromJsonString(presences);
72 }
73
74 public String getDisplayName() {
75 return this.displayName;
76 }
77
78 public String getProfilePhoto() {
79 return this.photoUri;
80 }
81
82 public String getJid() {
83 return this.jid;
84 }
85
86 public boolean match(String needle) {
87 return (jid.toLowerCase().contains(needle.toLowerCase()) || (displayName
88 .toLowerCase().contains(needle.toLowerCase())));
89 }
90
91 @Override
92 public ContentValues getContentValues() {
93 ContentValues values = new ContentValues();
94 values.put(UUID, uuid);
95 values.put(ACCOUNT, accountUuid);
96 values.put(DISPLAYNAME, displayName);
97 values.put(JID, jid);
98 values.put(SUBSCRIPTION, subscription);
99 values.put(SYSTEMACCOUNT, systemAccount);
100 values.put(PHOTOURI, photoUri);
101 values.put(KEYS, keys.toString());
102 values.put(PRESENCES, presences.toJsonString());
103 return values;
104 }
105
106 public static Contact fromCursor(Cursor cursor) {
107 return new Contact(cursor.getString(cursor.getColumnIndex(UUID)),
108 cursor.getString(cursor.getColumnIndex(ACCOUNT)),
109 cursor.getString(cursor.getColumnIndex(DISPLAYNAME)),
110 cursor.getString(cursor.getColumnIndex(JID)),
111 cursor.getString(cursor.getColumnIndex(SUBSCRIPTION)),
112 cursor.getString(cursor.getColumnIndex(PHOTOURI)),
113 cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)),
114 cursor.getString(cursor.getColumnIndex(KEYS)),
115 cursor.getString(cursor.getColumnIndex(PRESENCES)));
116 }
117
118 public void setSubscription(String subscription) {
119 this.subscription = subscription;
120 }
121
122 public String getSubscription() {
123 return this.subscription;
124 }
125
126 public void setSystemAccount(String account) {
127 this.systemAccount = account;
128 }
129
130 public void setAccount(Account account) {
131 this.account = account;
132 this.accountUuid = account.getUuid();
133 }
134
135 public Account getAccount() {
136 return this.account;
137 }
138
139 public void setUuid(String uuid) {
140 this.uuid = uuid;
141 }
142
143 public boolean couldBeMuc() {
144 String[] split = this.getJid().split("@");
145 if (split.length != 2) {
146 return false;
147 } else {
148 String[] domainParts = split[1].split("\\.");
149 if (domainParts.length < 3) {
150 return false;
151 } else {
152 return (domainParts[0].equals("conf")
153 || domainParts[0].equals("conference") || domainParts[0]
154 .equals("muc"));
155 }
156 }
157 }
158
159 public Hashtable<String, Integer> getPresences() {
160 return this.presences.getPresences();
161 }
162
163 public void updatePresence(String resource, int status) {
164 this.presences.updatePresence(resource, status);
165 }
166
167 public void removePresence(String resource) {
168 this.presences.removePresence(resource);
169 }
170
171 public int getMostAvailableStatus() {
172 return this.presences.getMostAvailableStatus();
173 }
174
175 public void setPresences(Presences pres) {
176 this.presences = pres;
177 }
178
179 public void setPhotoUri(String uri) {
180 this.photoUri = uri;
181 }
182
183 public void setDisplayName(String name) {
184 this.displayName = name;
185 }
186
187 public String getSystemAccount() {
188 return systemAccount;
189 }
190
191 public Set<String> getOtrFingerprints() {
192 Set<String> set = new HashSet<String>();
193 try {
194 if (this.keys.has("otr_fingerprints")) {
195 JSONArray fingerprints = this.keys.getJSONArray("otr_fingerprints");
196 for (int i = 0; i < fingerprints.length(); ++i) {
197 set.add(fingerprints.getString(i));
198 }
199 }
200 } catch (JSONException e) {
201 // TODO Auto-generated catch block
202 e.printStackTrace();
203 }
204 return set;
205 }
206
207 public void addOtrFingerprint(String print) {
208 try {
209 JSONArray fingerprints;
210 if (!this.keys.has("otr_fingerprints")) {
211 fingerprints = new JSONArray();
212
213 } else {
214 fingerprints = this.keys.getJSONArray("otr_fingerprints");
215 }
216 fingerprints.put(print);
217 this.keys.put("otr_fingerprints", fingerprints);
218 } catch (JSONException e) {
219
220 }
221 }
222}