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