Contact.java

  1package eu.siacs.conversations.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 eu.siacs.conversations.xml.Element;
 13
 14import android.content.ContentValues;
 15import android.database.Cursor;
 16import android.util.Log;
 17
 18public class Contact extends AbstractEntity implements Serializable {
 19	private static final long serialVersionUID = -4570817093119419962L;
 20
 21	public static final String TABLENAME = "contacts";
 22
 23	public static final String DISPLAYNAME = "name";
 24	public static final String JID = "jid";
 25	public static final String SUBSCRIPTION = "subscription";
 26	public static final String SYSTEMACCOUNT = "systemaccount";
 27	public static final String PHOTOURI = "photouri";
 28	public static final String KEYS = "pgpkey";
 29	public static final String PRESENCES = "presences";
 30	public static final String ACCOUNT = "accountUuid";
 31
 32	protected String accountUuid;
 33	protected String displayName;
 34	protected String jid;
 35	protected int subscription = 0;
 36	protected String systemAccount;
 37	protected String photoUri;
 38	protected JSONObject keys = new JSONObject();
 39	protected Presences presences = new Presences();
 40
 41	protected Account account;
 42
 43	protected boolean inRoster = true;
 44
 45	public Contact(Account account, String displayName, String jid,
 46			String photoUri) {
 47		if (account == null) {
 48			this.accountUuid = null;
 49		} else {
 50			this.accountUuid = account.getUuid();
 51		}
 52		this.account = account;
 53		this.displayName = displayName;
 54		this.jid = jid;
 55		this.photoUri = photoUri;
 56		this.uuid = java.util.UUID.randomUUID().toString();
 57	}
 58
 59	public Contact(String uuid, String account, String displayName, String jid,
 60			int subscription, String photoUri, String systemAccount,
 61			String keys, String presences) {
 62		this.uuid = uuid;
 63		this.accountUuid = account;
 64		this.displayName = displayName;
 65		this.jid = jid;
 66		this.subscription = subscription;
 67		this.photoUri = photoUri;
 68		this.systemAccount = systemAccount;
 69		if (keys == null) {
 70			keys = "";
 71		}
 72		try {
 73			this.keys = new JSONObject(keys);
 74		} catch (JSONException e) {
 75			this.keys = new JSONObject();
 76		}
 77		this.presences = Presences.fromJsonString(presences);
 78	}
 79
 80	public String getDisplayName() {
 81		return this.displayName;
 82	}
 83
 84	public String getProfilePhoto() {
 85		return this.photoUri;
 86	}
 87
 88	public String getJid() {
 89		return this.jid;
 90	}
 91
 92	public boolean match(String needle) {
 93		return (jid.toLowerCase().contains(needle.toLowerCase()) || (displayName
 94				.toLowerCase().contains(needle.toLowerCase())));
 95	}
 96
 97	@Override
 98	public ContentValues getContentValues() {
 99		ContentValues values = new ContentValues();
100		values.put(UUID, uuid);
101		values.put(ACCOUNT, accountUuid);
102		values.put(DISPLAYNAME, displayName);
103		values.put(JID, jid);
104		values.put(SUBSCRIPTION, subscription);
105		values.put(SYSTEMACCOUNT, systemAccount);
106		values.put(PHOTOURI, photoUri);
107		values.put(KEYS, keys.toString());
108		values.put(PRESENCES, presences.toJsonString());
109		return values;
110	}
111
112	public static Contact fromCursor(Cursor cursor) {
113		return new Contact(cursor.getString(cursor.getColumnIndex(UUID)),
114				cursor.getString(cursor.getColumnIndex(ACCOUNT)),
115				cursor.getString(cursor.getColumnIndex(DISPLAYNAME)),
116				cursor.getString(cursor.getColumnIndex(JID)),
117				cursor.getInt(cursor.getColumnIndex(SUBSCRIPTION)),
118				cursor.getString(cursor.getColumnIndex(PHOTOURI)),
119				cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)),
120				cursor.getString(cursor.getColumnIndex(KEYS)),
121				cursor.getString(cursor.getColumnIndex(PRESENCES)));
122	}
123
124	public int getSubscription() {
125		return this.subscription;
126	}
127
128	public void setSystemAccount(String account) {
129		this.systemAccount = account;
130	}
131
132	public void setAccount(Account account) {
133		this.account = account;
134		this.accountUuid = account.getUuid();
135	}
136
137	public Account getAccount() {
138		return this.account;
139	}
140
141	public void setUuid(String uuid) {
142		this.uuid = uuid;
143	}
144
145	public boolean couldBeMuc() {
146		String[] split = this.getJid().split("@");
147		if (split.length != 2) {
148			return false;
149		} else {
150			String[] domainParts = split[1].split("\\.");
151			if (domainParts.length < 3) {
152				return false;
153			} else {
154				return (domainParts[0].equals("conf")
155						|| domainParts[0].equals("conference")
156						|| domainParts[0].equals("muc")
157						|| domainParts[0].equals("sala")
158						|| domainParts[0].equals("salas"));
159			}
160		}
161	}
162
163	public Hashtable<String, Integer> getPresences() {
164		return this.presences.getPresences();
165	}
166
167	public void updatePresence(String resource, int status) {
168		this.presences.updatePresence(resource, status);
169	}
170
171	public void removePresence(String resource) {
172		this.presences.removePresence(resource);
173	}
174
175	public void clearPresences() {
176		this.presences.clearPresences();
177	}
178
179	public int getMostAvailableStatus() {
180		return this.presences.getMostAvailableStatus();
181	}
182
183	public void setPresences(Presences pres) {
184		this.presences = pres;
185	}
186
187	public void setPhotoUri(String uri) {
188		this.photoUri = uri;
189	}
190
191	public void setDisplayName(String name) {
192		this.displayName = name;
193	}
194
195	public String getSystemAccount() {
196		return systemAccount;
197	}
198
199	public Set<String> getOtrFingerprints() {
200		Set<String> set = new HashSet<String>();
201		try {
202			if (this.keys.has("otr_fingerprints")) {
203				JSONArray fingerprints = this.keys
204						.getJSONArray("otr_fingerprints");
205				for (int i = 0; i < fingerprints.length(); ++i) {
206					set.add(fingerprints.getString(i));
207				}
208			}
209		} catch (JSONException e) {
210			// TODO Auto-generated catch block
211			e.printStackTrace();
212		}
213		return set;
214	}
215
216	public void addOtrFingerprint(String print) {
217		try {
218			JSONArray fingerprints;
219			if (!this.keys.has("otr_fingerprints")) {
220				fingerprints = new JSONArray();
221
222			} else {
223				fingerprints = this.keys.getJSONArray("otr_fingerprints");
224			}
225			fingerprints.put(print);
226			this.keys.put("otr_fingerprints", fingerprints);
227		} catch (JSONException e) {
228
229		}
230	}
231
232	public void setPgpKeyId(long keyId) {
233		try {
234			this.keys.put("pgp_keyid", keyId);
235		} catch (JSONException e) {
236
237		}
238	}
239
240	public long getPgpKeyId() {
241		if (this.keys.has("pgp_keyid")) {
242			try {
243				return this.keys.getLong("pgp_keyid");
244			} catch (JSONException e) {
245				return 0;
246			}
247		} else {
248			return 0;
249		}
250	}
251
252	public void setSubscriptionOption(int option) {
253		this.subscription |= 1 << option;
254	}
255
256	public void resetSubscriptionOption(int option) {
257		this.subscription &= ~(1 << option);
258	}
259
260	public boolean getSubscriptionOption(int option) {
261		return ((this.subscription & (1 << option)) != 0);
262	}
263
264	public void parseSubscriptionFromElement(Element item) {
265		String ask = item.getAttribute("ask");
266		String subscription = item.getAttribute("subscription");
267
268		if (subscription != null) {
269			if (subscription.equals("to")) {
270				this.resetSubscriptionOption(Contact.Subscription.FROM);
271				this.setSubscriptionOption(Contact.Subscription.TO);
272			} else if (subscription.equals("from")) {
273				this.resetSubscriptionOption(Contact.Subscription.TO);
274				this.setSubscriptionOption(Contact.Subscription.FROM);
275			} else if (subscription.equals("both")) {
276				this.setSubscriptionOption(Contact.Subscription.TO);
277				this.setSubscriptionOption(Contact.Subscription.FROM);
278			}
279		}
280
281		if ((ask != null) && (ask.equals("subscribe"))) {
282			this.setSubscriptionOption(Contact.Subscription.ASKING);
283		} else {
284			this.resetSubscriptionOption(Contact.Subscription.ASKING);
285		}
286	}
287
288	public class Subscription {
289		public static final int TO = 0;
290		public static final int FROM = 1;
291		public static final int ASKING = 2;
292		public static final int PREEMPTIVE_GRANT = 4;
293	}
294
295	public void flagAsNotInRoster() {
296		this.inRoster = false;
297	}
298
299	public boolean isInRoster() {
300		return this.inRoster;
301	}
302
303	public String getAccountUuid() {
304		return this.accountUuid;
305	}
306}