Contact.java

  1package de.gultsch.chat.entities;
  2
  3import java.io.Serializable;
  4import java.util.Hashtable;
  5
  6import android.content.ContentValues;
  7import android.database.Cursor;
  8
  9public class Contact extends AbstractEntity implements Serializable {
 10	private static final long serialVersionUID = -4570817093119419962L;
 11
 12	public static final String TABLENAME = "contacts";
 13
 14	public static final String DISPLAYNAME = "name";
 15	public static final String JID = "jid";
 16	public static final String SUBSCRIPTION = "subscription";
 17	public static final String SYSTEMACCOUNT = "systemaccount";
 18	public static final String PHOTOURI = "photouri";
 19	public static final String OPENPGPKEY = "pgpkey";
 20	public static final String PRESENCES = "presences";
 21	public static final String ACCOUNT = "accountUuid";
 22
 23	protected String accountUuid;
 24	protected String displayName;
 25	protected String jid;
 26	protected String subscription;
 27	protected int systemAccount;
 28	protected String photoUri;
 29	protected String openPGPKey;
 30	protected Presences presences = new Presences();
 31
 32	protected Account account;
 33
 34	public Contact(Account account, String displayName, String jid,
 35			String photoUri) {
 36		if (account == null) {
 37			this.accountUuid = null;
 38		} else {
 39			this.accountUuid = account.getUuid();
 40		}
 41		this.displayName = displayName;
 42		this.jid = jid;
 43		this.photoUri = photoUri;
 44	}
 45
 46	public Contact(String uuid, String account, String displayName, String jid,
 47			String subscription, String photoUri, int systemAccount,
 48			String pgpKey,String presences) {
 49		this.uuid = uuid;
 50		this.accountUuid = account;
 51		this.displayName = displayName;
 52		this.jid = jid;
 53		this.subscription = subscription;
 54		this.photoUri = photoUri;
 55		this.systemAccount = systemAccount;
 56		this.openPGPKey = pgpKey;
 57		this.presences = Presences.fromJsonString(presences);
 58	}
 59
 60	public String getDisplayName() {
 61		return this.displayName;
 62	}
 63
 64	public String getProfilePhoto() {
 65		return this.photoUri;
 66	}
 67
 68	public String getJid() {
 69		return this.jid;
 70	}
 71
 72	public boolean match(String needle) {
 73		return (jid.toLowerCase().contains(needle.toLowerCase()) || (displayName
 74				.toLowerCase().contains(needle.toLowerCase())));
 75	}
 76
 77	@Override
 78	public ContentValues getContentValues() {
 79		ContentValues values = new ContentValues();
 80		values.put(UUID, uuid);
 81		values.put(ACCOUNT, accountUuid);
 82		values.put(DISPLAYNAME, displayName);
 83		values.put(JID, jid);
 84		values.put(SUBSCRIPTION, subscription);
 85		values.put(SYSTEMACCOUNT, systemAccount);
 86		values.put(PHOTOURI, photoUri);
 87		values.put(OPENPGPKEY, openPGPKey);
 88		values.put(PRESENCES, presences.toJsonString());
 89		return values;
 90	}
 91
 92	public static Contact fromCursor(Cursor cursor) {
 93		return new Contact(cursor.getString(cursor.getColumnIndex(UUID)),
 94				cursor.getString(cursor.getColumnIndex(ACCOUNT)),
 95				cursor.getString(cursor.getColumnIndex(DISPLAYNAME)),
 96				cursor.getString(cursor.getColumnIndex(JID)),
 97				cursor.getString(cursor.getColumnIndex(SUBSCRIPTION)),
 98				cursor.getString(cursor.getColumnIndex(PHOTOURI)),
 99				cursor.getInt(cursor.getColumnIndex(SYSTEMACCOUNT)),
100				cursor.getString(cursor.getColumnIndex(OPENPGPKEY)),
101				cursor.getString(cursor.getColumnIndex(PRESENCES)));
102	}
103
104	public void setSubscription(String subscription) {
105		this.subscription = subscription;
106	}
107	
108	public String getSubscription() {
109		return this.subscription;
110	}
111
112	public void setSystemAccount(int account) {
113		this.systemAccount = account;
114	}
115
116	public void setAccount(Account account) {
117		this.account = account;
118		this.accountUuid = account.getUuid();
119	}
120
121	public Account getAccount() {
122		return this.account;
123	}
124
125	public void setUuid(String uuid) {
126		this.uuid = uuid;
127	}
128
129	public boolean couldBeMuc() {
130		String[] split = this.getJid().split("@");
131		if (split.length != 2) {
132			return false;
133		} else {
134			String[] domainParts = split[1].split("\\.");
135			if (domainParts.length < 3) {
136				return false;
137			} else {
138				return (domainParts[0].equals("conf")
139						|| domainParts[0].equals("conference") || domainParts[0]
140							.equals("muc"));
141			}
142		}
143	}
144	
145	public Hashtable<String, Integer> getPresences() {
146		return this.presences.getPresences();
147	}
148	
149	public void updatePresence(String resource, int status) {
150		this.presences.updatePresence(resource, status);
151	}
152
153	public void removePresence(String resource) {
154		this.presences.removePresence(resource);
155	}
156	
157	public int getMostAvailableStatus() {
158		return this.presences.getMostAvailableStatus();
159	}
160}