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;
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 }
51
52 public Contact(String uuid, String account, String displayName, String jid,
53 String subscription, String photoUri, String systemAccount,
54 String keys, String presences) {
55 this.uuid = uuid;
56 this.accountUuid = account;
57 this.displayName = displayName;
58 this.jid = jid;
59 this.subscription = subscription;
60 this.photoUri = photoUri;
61 this.systemAccount = systemAccount;
62 if (keys == null) {
63 keys = "";
64 }
65 try {
66 this.keys = new JSONObject(keys);
67 } catch (JSONException e) {
68 this.keys = new JSONObject();
69 }
70 this.presences = Presences.fromJsonString(presences);
71 }
72
73 public String getDisplayName() {
74 return this.displayName;
75 }
76
77 public String getProfilePhoto() {
78 return this.photoUri;
79 }
80
81 public String getJid() {
82 return this.jid;
83 }
84
85 public boolean match(String needle) {
86 return (jid.toLowerCase().contains(needle.toLowerCase()) || (displayName
87 .toLowerCase().contains(needle.toLowerCase())));
88 }
89
90 @Override
91 public ContentValues getContentValues() {
92 ContentValues values = new ContentValues();
93 values.put(UUID, uuid);
94 values.put(ACCOUNT, accountUuid);
95 values.put(DISPLAYNAME, displayName);
96 values.put(JID, jid);
97 values.put(SUBSCRIPTION, subscription);
98 values.put(SYSTEMACCOUNT, systemAccount);
99 values.put(PHOTOURI, photoUri);
100 values.put(KEYS, keys.toString());
101 values.put(PRESENCES, presences.toJsonString());
102 return values;
103 }
104
105 public static Contact fromCursor(Cursor cursor) {
106 return new Contact(cursor.getString(cursor.getColumnIndex(UUID)),
107 cursor.getString(cursor.getColumnIndex(ACCOUNT)),
108 cursor.getString(cursor.getColumnIndex(DISPLAYNAME)),
109 cursor.getString(cursor.getColumnIndex(JID)),
110 cursor.getString(cursor.getColumnIndex(SUBSCRIPTION)),
111 cursor.getString(cursor.getColumnIndex(PHOTOURI)),
112 cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)),
113 cursor.getString(cursor.getColumnIndex(KEYS)),
114 cursor.getString(cursor.getColumnIndex(PRESENCES)));
115 }
116
117 public void setSubscription(String subscription) {
118 this.subscription = subscription;
119 }
120
121 public String getSubscription() {
122 return this.subscription;
123 }
124
125 public void setSystemAccount(String account) {
126 this.systemAccount = account;
127 }
128
129 public void setAccount(Account account) {
130 this.account = account;
131 this.accountUuid = account.getUuid();
132 }
133
134 public Account getAccount() {
135 return this.account;
136 }
137
138 public void setUuid(String uuid) {
139 this.uuid = uuid;
140 }
141
142 public boolean couldBeMuc() {
143 String[] split = this.getJid().split("@");
144 if (split.length != 2) {
145 return false;
146 } else {
147 String[] domainParts = split[1].split("\\.");
148 if (domainParts.length < 3) {
149 return false;
150 } else {
151 return (domainParts[0].equals("conf")
152 || domainParts[0].equals("conference") || domainParts[0]
153 .equals("muc"));
154 }
155 }
156 }
157
158 public Hashtable<String, Integer> getPresences() {
159 return this.presences.getPresences();
160 }
161
162 public void updatePresence(String resource, int status) {
163 this.presences.updatePresence(resource, status);
164 }
165
166 public void removePresence(String resource) {
167 this.presences.removePresence(resource);
168 }
169
170 public int getMostAvailableStatus() {
171 return this.presences.getMostAvailableStatus();
172 }
173
174 public void setPresences(Presences pres) {
175 this.presences = pres;
176 }
177
178 public void setPhotoUri(String uri) {
179 this.photoUri = uri;
180 }
181
182 public void setDisplayName(String name) {
183 this.displayName = name;
184 }
185
186 public String getSystemAccount() {
187 return systemAccount;
188 }
189
190 public Set<String> getOtrFingerprints() {
191 Set<String> set = new HashSet<String>();
192 try {
193 if (this.keys.has("otr_fingerprints")) {
194 JSONArray fingerprints = this.keys.getJSONArray("otr_fingerprints");
195 for (int i = 0; i < fingerprints.length(); ++i) {
196 set.add(fingerprints.getString(i));
197 }
198 }
199 } catch (JSONException e) {
200 // TODO Auto-generated catch block
201 e.printStackTrace();
202 }
203 return set;
204 }
205
206 public void addOtrFingerprint(String print) {
207 try {
208 JSONArray fingerprints;
209 if (!this.keys.has("otr_fingerprints")) {
210 fingerprints = new JSONArray();
211
212 } else {
213 fingerprints = this.keys.getJSONArray("otr_fingerprints");
214 }
215 fingerprints.put(print);
216 this.keys.put("otr_fingerprints", fingerprints);
217 } catch (JSONException e) {
218
219 }
220 }
221}