Account.java

  1package eu.siacs.conversations.entities;
  2
  3import java.security.interfaces.DSAPublicKey;
  4import java.util.List;
  5import java.util.Locale;
  6import java.util.concurrent.CopyOnWriteArrayList;
  7
  8import net.java.otr4j.crypto.OtrCryptoEngineImpl;
  9import net.java.otr4j.crypto.OtrCryptoException;
 10
 11import org.json.JSONException;
 12import org.json.JSONObject;
 13
 14import eu.siacs.conversations.Config;
 15import eu.siacs.conversations.R;
 16import eu.siacs.conversations.crypto.OtrEngine;
 17import eu.siacs.conversations.persistance.FileBackend;
 18import eu.siacs.conversations.services.XmppConnectionService;
 19import eu.siacs.conversations.utils.UIHelper;
 20import eu.siacs.conversations.xmpp.XmppConnection;
 21import android.content.ContentValues;
 22import android.content.Context;
 23import android.database.Cursor;
 24import android.graphics.Bitmap;
 25import android.os.SystemClock;
 26
 27public class Account extends AbstractEntity {
 28
 29	public static final String TABLENAME = "accounts";
 30
 31	public static final String USERNAME = "username";
 32	public static final String SERVER = "server";
 33	public static final String PASSWORD = "password";
 34	public static final String OPTIONS = "options";
 35	public static final String ROSTERVERSION = "rosterversion";
 36	public static final String KEYS = "keys";
 37	public static final String AVATAR = "avatar";
 38
 39	public static final int OPTION_USETLS = 0;
 40	public static final int OPTION_DISABLED = 1;
 41	public static final int OPTION_REGISTER = 2;
 42	public static final int OPTION_USECOMPRESSION = 3;
 43
 44	public static final int STATUS_CONNECTING = 0;
 45	public static final int STATUS_DISABLED = -2;
 46	public static final int STATUS_OFFLINE = -1;
 47	public static final int STATUS_ONLINE = 1;
 48	public static final int STATUS_NO_INTERNET = 2;
 49	public static final int STATUS_UNAUTHORIZED = 3;
 50	public static final int STATUS_SERVER_NOT_FOUND = 5;
 51
 52	public static final int STATUS_REGISTRATION_FAILED = 7;
 53	public static final int STATUS_REGISTRATION_CONFLICT = 8;
 54	public static final int STATUS_REGISTRATION_SUCCESSFULL = 9;
 55	public static final int STATUS_REGISTRATION_NOT_SUPPORTED = 10;
 56
 57	protected String username;
 58	protected String server;
 59	protected String password;
 60	protected int options = 0;
 61	protected String rosterVersion;
 62	protected String resource = "mobile";
 63	protected int status = -1;
 64	protected JSONObject keys = new JSONObject();
 65	protected String avatar;
 66
 67	protected boolean online = false;
 68
 69	private OtrEngine otrEngine = null;
 70	private XmppConnection xmppConnection = null;
 71	private Presences presences = new Presences();
 72	private long mEndGracePeriod = 0L;
 73	private String otrFingerprint;
 74	private Roster roster = null;
 75	
 76	private List<Bookmark> bookmarks = new CopyOnWriteArrayList<Bookmark>();
 77	public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<Conversation>();
 78	public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<Conversation>();
 79
 80	public Account() {
 81		this.uuid = "0";
 82	}
 83
 84	public Account(String username, String server, String password) {
 85		this(java.util.UUID.randomUUID().toString(), username, server,
 86				password, 0, null, "", null);
 87	}
 88
 89	public Account(String uuid, String username, String server,
 90			String password, int options, String rosterVersion, String keys,
 91			String avatar) {
 92		this.uuid = uuid;
 93		this.username = username;
 94		this.server = server;
 95		this.password = password;
 96		this.options = options;
 97		this.rosterVersion = rosterVersion;
 98		try {
 99			this.keys = new JSONObject(keys);
100		} catch (JSONException e) {
101
102		}
103		this.avatar = avatar;
104	}
105
106	public boolean isOptionSet(int option) {
107		return ((options & (1 << option)) != 0);
108	}
109
110	public void setOption(int option, boolean value) {
111		if (value) {
112			this.options |= 1 << option;
113		} else {
114			this.options &= ~(1 << option);
115		}
116	}
117
118	public String getUsername() {
119		return username;
120	}
121
122	public void setUsername(String username) {
123		this.username = username;
124	}
125
126	public String getServer() {
127		return server;
128	}
129
130	public void setServer(String server) {
131		this.server = server;
132	}
133
134	public String getPassword() {
135		return password;
136	}
137
138	public void setPassword(String password) {
139		this.password = password;
140	}
141
142	public void setStatus(int status) {
143		this.status = status;
144	}
145
146	public int getStatus() {
147		if (isOptionSet(OPTION_DISABLED)) {
148			return STATUS_DISABLED;
149		} else {
150			return this.status;
151		}
152	}
153
154	public boolean errorStatus() {
155		int s = getStatus();
156		return (s == STATUS_REGISTRATION_FAILED
157				|| s == STATUS_REGISTRATION_CONFLICT
158				|| s == STATUS_REGISTRATION_NOT_SUPPORTED
159				|| s == STATUS_SERVER_NOT_FOUND || s == STATUS_UNAUTHORIZED);
160	}
161
162	public boolean hasErrorStatus() {
163		if (getXmppConnection() == null) {
164			return false;
165		} else {
166			return getStatus() > STATUS_NO_INTERNET
167					&& (getXmppConnection().getAttempt() >= 2);
168		}
169	}
170
171	public void setResource(String resource) {
172		this.resource = resource;
173	}
174
175	public String getResource() {
176		return this.resource;
177	}
178
179	public String getJid() {
180		return username.toLowerCase(Locale.getDefault()) + "@"
181				+ server.toLowerCase(Locale.getDefault());
182	}
183
184	public JSONObject getKeys() {
185		return keys;
186	}
187
188	public String getSSLFingerprint() {
189		if (keys.has("ssl_cert")) {
190			try {
191				return keys.getString("ssl_cert");
192			} catch (JSONException e) {
193				return null;
194			}
195		} else {
196			return null;
197		}
198	}
199
200	public void setSSLCertFingerprint(String fingerprint) {
201		this.setKey("ssl_cert", fingerprint);
202	}
203
204	public boolean setKey(String keyName, String keyValue) {
205		try {
206			this.keys.put(keyName, keyValue);
207			return true;
208		} catch (JSONException e) {
209			return false;
210		}
211	}
212
213	@Override
214	public ContentValues getContentValues() {
215		ContentValues values = new ContentValues();
216		values.put(UUID, uuid);
217		values.put(USERNAME, username);
218		values.put(SERVER, server);
219		values.put(PASSWORD, password);
220		values.put(OPTIONS, options);
221		values.put(KEYS, this.keys.toString());
222		values.put(ROSTERVERSION, rosterVersion);
223		values.put(AVATAR, avatar);
224		return values;
225	}
226
227	public static Account fromCursor(Cursor cursor) {
228		return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
229				cursor.getString(cursor.getColumnIndex(USERNAME)),
230				cursor.getString(cursor.getColumnIndex(SERVER)),
231				cursor.getString(cursor.getColumnIndex(PASSWORD)),
232				cursor.getInt(cursor.getColumnIndex(OPTIONS)),
233				cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
234				cursor.getString(cursor.getColumnIndex(KEYS)),
235				cursor.getString(cursor.getColumnIndex(AVATAR)));
236	}
237
238	public OtrEngine getOtrEngine(XmppConnectionService context) {
239		if (otrEngine == null) {
240			otrEngine = new OtrEngine(context, this);
241		}
242		return this.otrEngine;
243	}
244
245	public XmppConnection getXmppConnection() {
246		return this.xmppConnection;
247	}
248
249	public void setXmppConnection(XmppConnection connection) {
250		this.xmppConnection = connection;
251	}
252
253	public String getFullJid() {
254		return this.getJid() + "/" + this.resource;
255	}
256
257	public String getOtrFingerprint() {
258		if (this.otrFingerprint == null) {
259			try {
260				DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine
261						.getPublicKey();
262				if (pubkey == null) {
263					return null;
264				}
265				StringBuilder builder = new StringBuilder(
266						new OtrCryptoEngineImpl().getFingerprint(pubkey));
267				builder.insert(8, " ");
268				builder.insert(17, " ");
269				builder.insert(26, " ");
270				builder.insert(35, " ");
271				this.otrFingerprint = builder.toString();
272			} catch (OtrCryptoException e) {
273
274			}
275		}
276		return this.otrFingerprint;
277	}
278
279	public String getRosterVersion() {
280		if (this.rosterVersion == null) {
281			return "";
282		} else {
283			return this.rosterVersion;
284		}
285	}
286
287	public void setRosterVersion(String version) {
288		this.rosterVersion = version;
289	}
290
291	public String getOtrFingerprint(XmppConnectionService service) {
292		this.getOtrEngine(service);
293		return this.getOtrFingerprint();
294	}
295
296	public void updatePresence(String resource, int status) {
297		this.presences.updatePresence(resource, status);
298	}
299
300	public void removePresence(String resource) {
301		this.presences.removePresence(resource);
302	}
303
304	public void clearPresences() {
305		this.presences = new Presences();
306	}
307
308	public int countPresences() {
309		return this.presences.size();
310	}
311
312	public String getPgpSignature() {
313		if (keys.has("pgp_signature")) {
314			try {
315				return keys.getString("pgp_signature");
316			} catch (JSONException e) {
317				return null;
318			}
319		} else {
320			return null;
321		}
322	}
323
324	public Roster getRoster() {
325		if (this.roster == null) {
326			this.roster = new Roster(this);
327		}
328		return this.roster;
329	}
330
331	public void setBookmarks(List<Bookmark> bookmarks) {
332		this.bookmarks = bookmarks;
333	}
334
335	public List<Bookmark> getBookmarks() {
336		return this.bookmarks;
337	}
338
339	public boolean hasBookmarkFor(String conferenceJid) {
340		for (Bookmark bmark : this.bookmarks) {
341			if (bmark.getJid().equals(conferenceJid)) {
342				return true;
343			}
344		}
345		return false;
346	}
347
348	public Bitmap getImage(Context context, int size) {
349		if (this.avatar != null) {
350			Bitmap bm = FileBackend.getAvatar(this.avatar, size, context);
351			if (bm == null) {
352				return UIHelper.getContactPicture(getJid(), size, context,
353						false);
354			} else {
355				return bm;
356			}
357		} else {
358			return UIHelper.getContactPicture(getJid(), size, context, false);
359		}
360	}
361
362	public boolean setAvatar(String filename) {
363		if (this.avatar != null && this.avatar.equals(filename)) {
364			return false;
365		} else {
366			this.avatar = filename;
367			return true;
368		}
369	}
370
371	public String getAvatar() {
372		return this.avatar;
373	}
374
375	public int getReadableStatusId() {
376		switch (getStatus()) {
377
378		case Account.STATUS_DISABLED:
379			return R.string.account_status_disabled;
380		case Account.STATUS_ONLINE:
381			return R.string.account_status_online;
382		case Account.STATUS_CONNECTING:
383			return R.string.account_status_connecting;
384		case Account.STATUS_OFFLINE:
385			return R.string.account_status_offline;
386		case Account.STATUS_UNAUTHORIZED:
387			return R.string.account_status_unauthorized;
388		case Account.STATUS_SERVER_NOT_FOUND:
389			return R.string.account_status_not_found;
390		case Account.STATUS_NO_INTERNET:
391			return R.string.account_status_no_internet;
392		case Account.STATUS_REGISTRATION_FAILED:
393			return R.string.account_status_regis_fail;
394		case Account.STATUS_REGISTRATION_CONFLICT:
395			return R.string.account_status_regis_conflict;
396		case Account.STATUS_REGISTRATION_SUCCESSFULL:
397			return R.string.account_status_regis_success;
398		case Account.STATUS_REGISTRATION_NOT_SUPPORTED:
399			return R.string.account_status_regis_not_sup;
400		default:
401			return R.string.account_status_unknown;
402		}
403	}
404	
405	public void activateGracePeriod() {
406		this.mEndGracePeriod = SystemClock.elapsedRealtime()
407				+ (Config.CARBON_GRACE_PERIOD * 1000);
408	}
409
410	public void deactivateGracePeriod() {
411		this.mEndGracePeriod = 0L;
412	}
413
414	public boolean inGracePeriod() {
415		return SystemClock.elapsedRealtime() < this.mEndGracePeriod;
416	}
417}