Contact.java

  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.account = account;
 48		this.displayName = displayName;
 49		this.jid = jid;
 50		this.photoUri = photoUri;
 51		this.uuid = java.util.UUID.randomUUID().toString();
 52	}
 53
 54	public Contact(String uuid, String account, String displayName, String jid,
 55			String subscription, String photoUri, String systemAccount,
 56			String keys, String presences) {
 57		this.uuid = uuid;
 58		this.accountUuid = account;
 59		this.displayName = displayName;
 60		this.jid = jid;
 61		this.subscription = subscription;
 62		this.photoUri = photoUri;
 63		this.systemAccount = systemAccount;
 64		if (keys == null) {
 65			keys = "";
 66		}
 67		try {
 68			this.keys = new JSONObject(keys);
 69		} catch (JSONException e) {
 70			this.keys = new JSONObject();
 71		}
 72		this.presences = Presences.fromJsonString(presences);
 73	}
 74
 75	public String getDisplayName() {
 76		return this.displayName;
 77	}
 78
 79	public String getProfilePhoto() {
 80		return this.photoUri;
 81	}
 82
 83	public String getJid() {
 84		return this.jid;
 85	}
 86
 87	public boolean match(String needle) {
 88		return (jid.toLowerCase().contains(needle.toLowerCase()) || (displayName
 89				.toLowerCase().contains(needle.toLowerCase())));
 90	}
 91
 92	@Override
 93	public ContentValues getContentValues() {
 94		ContentValues values = new ContentValues();
 95		values.put(UUID, uuid);
 96		values.put(ACCOUNT, accountUuid);
 97		values.put(DISPLAYNAME, displayName);
 98		values.put(JID, jid);
 99		values.put(SUBSCRIPTION, subscription);
100		values.put(SYSTEMACCOUNT, systemAccount);
101		values.put(PHOTOURI, photoUri);
102		values.put(KEYS, keys.toString());
103		values.put(PRESENCES, presences.toJsonString());
104		return values;
105	}
106
107	public static Contact fromCursor(Cursor cursor) {
108		return new Contact(cursor.getString(cursor.getColumnIndex(UUID)),
109				cursor.getString(cursor.getColumnIndex(ACCOUNT)),
110				cursor.getString(cursor.getColumnIndex(DISPLAYNAME)),
111				cursor.getString(cursor.getColumnIndex(JID)),
112				cursor.getString(cursor.getColumnIndex(SUBSCRIPTION)),
113				cursor.getString(cursor.getColumnIndex(PHOTOURI)),
114				cursor.getString(cursor.getColumnIndex(SYSTEMACCOUNT)),
115				cursor.getString(cursor.getColumnIndex(KEYS)),
116				cursor.getString(cursor.getColumnIndex(PRESENCES)));
117	}
118
119	public void setSubscription(String subscription) {
120		this.subscription = subscription;
121	}
122
123	public String 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}