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