| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | package eu.siacs.conversations.entities;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | import android.content.ContentValues;
 | 
					
						
							|  |  |  | import android.database.Cursor;
 | 
					
						
							|  |  |  | import android.os.SystemClock;
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import net.java.otr4j.crypto.OtrCryptoEngineImpl;
 | 
					
						
							|  |  |  | import net.java.otr4j.crypto.OtrCryptoException;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import org.json.JSONException;
 | 
					
						
							|  |  |  | import org.json.JSONObject;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | import java.security.PublicKey;
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | import java.security.interfaces.DSAPublicKey;
 | 
					
						
							| 
									
										
										
										
											2014-12-21 21:43:58 +01:00
										 |  |  | import java.util.Collection;
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | import java.util.List;
 | 
					
						
							|  |  |  | import java.util.concurrent.CopyOnWriteArrayList;
 | 
					
						
							| 
									
										
										
										
											2014-12-21 21:43:58 +01:00
										 |  |  | import java.util.concurrent.CopyOnWriteArraySet;
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | import eu.siacs.conversations.Config;
 | 
					
						
							|  |  |  | import eu.siacs.conversations.R;
 | 
					
						
							| 
									
										
										
										
											2015-05-26 04:36:32 +02:00
										 |  |  | import eu.siacs.conversations.crypto.OtrService;
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | import eu.siacs.conversations.services.XmppConnectionService;
 | 
					
						
							|  |  |  | import eu.siacs.conversations.xmpp.XmppConnection;
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 | 
					
						
							|  |  |  | import eu.siacs.conversations.xmpp.jid.Jid;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | public class Account extends AbstractEntity {
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public static final String TABLENAME = "accounts";
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public static final String USERNAME = "username";
 | 
					
						
							|  |  |  | 	public static final String SERVER = "server";
 | 
					
						
							|  |  |  | 	public static final String PASSWORD = "password";
 | 
					
						
							|  |  |  | 	public static final String OPTIONS = "options";
 | 
					
						
							|  |  |  | 	public static final String ROSTERVERSION = "rosterversion";
 | 
					
						
							|  |  |  | 	public static final String KEYS = "keys";
 | 
					
						
							|  |  |  | 	public static final String AVATAR = "avatar";
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-15 15:42:40 +01:00
										 |  |  | 	public static final String PINNED_MECHANISM_KEY = "pinned_mechanism";
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	public static final int OPTION_USETLS = 0;
 | 
					
						
							|  |  |  | 	public static final int OPTION_DISABLED = 1;
 | 
					
						
							|  |  |  | 	public static final int OPTION_REGISTER = 2;
 | 
					
						
							|  |  |  | 	public static final int OPTION_USECOMPRESSION = 3;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-28 13:14:21 +02:00
										 |  |  | 	public boolean httpUploadAvailable() {
 | 
					
						
							|  |  |  | 		return xmppConnection != null && xmppConnection.getFeatures().httpUpload();
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:09:02 +01:00
										 |  |  | 	public static enum State {
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:19:42 +01:00
										 |  |  | 		DISABLED,
 | 
					
						
							|  |  |  | 		OFFLINE,
 | 
					
						
							|  |  |  | 		CONNECTING,
 | 
					
						
							|  |  |  | 		ONLINE,
 | 
					
						
							|  |  |  | 		NO_INTERNET,
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:09:02 +01:00
										 |  |  | 		UNAUTHORIZED(true),
 | 
					
						
							|  |  |  | 		SERVER_NOT_FOUND(true),
 | 
					
						
							|  |  |  | 		REGISTRATION_FAILED(true),
 | 
					
						
							|  |  |  | 		REGISTRATION_CONFLICT(true),
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:19:42 +01:00
										 |  |  | 		REGISTRATION_SUCCESSFUL,
 | 
					
						
							|  |  |  | 		REGISTRATION_NOT_SUPPORTED(true),
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:29:58 +01:00
										 |  |  | 		SECURITY_ERROR(true),
 | 
					
						
							|  |  |  | 		INCOMPATIBLE_SERVER(true);
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:09:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-21 21:43:58 +01:00
										 |  |  | 		private final boolean isError;
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:09:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		public boolean isError() {
 | 
					
						
							|  |  |  | 			return this.isError;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		private State(final boolean isError) {
 | 
					
						
							|  |  |  | 			this.isError = isError;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		private State() {
 | 
					
						
							|  |  |  | 			this(false);
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		public int getReadableId() {
 | 
					
						
							|  |  |  | 			switch (this) {
 | 
					
						
							|  |  |  | 				case DISABLED:
 | 
					
						
							|  |  |  | 					return R.string.account_status_disabled;
 | 
					
						
							|  |  |  | 				case ONLINE:
 | 
					
						
							|  |  |  | 					return R.string.account_status_online;
 | 
					
						
							|  |  |  | 				case CONNECTING:
 | 
					
						
							|  |  |  | 					return R.string.account_status_connecting;
 | 
					
						
							|  |  |  | 				case OFFLINE:
 | 
					
						
							|  |  |  | 					return R.string.account_status_offline;
 | 
					
						
							|  |  |  | 				case UNAUTHORIZED:
 | 
					
						
							|  |  |  | 					return R.string.account_status_unauthorized;
 | 
					
						
							|  |  |  | 				case SERVER_NOT_FOUND:
 | 
					
						
							|  |  |  | 					return R.string.account_status_not_found;
 | 
					
						
							|  |  |  | 				case NO_INTERNET:
 | 
					
						
							|  |  |  | 					return R.string.account_status_no_internet;
 | 
					
						
							|  |  |  | 				case REGISTRATION_FAILED:
 | 
					
						
							|  |  |  | 					return R.string.account_status_regis_fail;
 | 
					
						
							|  |  |  | 				case REGISTRATION_CONFLICT:
 | 
					
						
							|  |  |  | 					return R.string.account_status_regis_conflict;
 | 
					
						
							|  |  |  | 				case REGISTRATION_SUCCESSFUL:
 | 
					
						
							|  |  |  | 					return R.string.account_status_regis_success;
 | 
					
						
							|  |  |  | 				case REGISTRATION_NOT_SUPPORTED:
 | 
					
						
							|  |  |  | 					return R.string.account_status_regis_not_sup;
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:19:42 +01:00
										 |  |  | 				case SECURITY_ERROR:
 | 
					
						
							|  |  |  | 					return R.string.account_status_security_error;
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:29:58 +01:00
										 |  |  | 				case INCOMPATIBLE_SERVER:
 | 
					
						
							|  |  |  | 					return R.string.account_status_incompatible_server;
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:09:02 +01:00
										 |  |  | 				default:
 | 
					
						
							|  |  |  | 					return R.string.account_status_unknown;
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | 	public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<>();
 | 
					
						
							|  |  |  | 	public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<>();
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 	protected Jid jid;
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	protected String password;
 | 
					
						
							|  |  |  | 	protected int options = 0;
 | 
					
						
							|  |  |  | 	protected String rosterVersion;
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:09:02 +01:00
										 |  |  | 	protected State status = State.OFFLINE;
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	protected JSONObject keys = new JSONObject();
 | 
					
						
							|  |  |  | 	protected String avatar;
 | 
					
						
							|  |  |  | 	protected boolean online = false;
 | 
					
						
							| 
									
										
										
										
											2015-05-26 04:36:32 +02:00
										 |  |  | 	private OtrService mOtrService = null;
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	private XmppConnection xmppConnection = null;
 | 
					
						
							|  |  |  | 	private long mEndGracePeriod = 0L;
 | 
					
						
							|  |  |  | 	private String otrFingerprint;
 | 
					
						
							| 
									
										
										
										
											2014-12-20 17:23:03 +01:00
										 |  |  | 	private final Roster roster = new Roster(this);
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 	private List<Bookmark> bookmarks = new CopyOnWriteArrayList<>();
 | 
					
						
							| 
									
										
										
										
											2014-12-21 21:43:58 +01:00
										 |  |  | 	private final Collection<Jid> blocklist = new CopyOnWriteArraySet<>();
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	public Account() {
 | 
					
						
							|  |  |  | 		this.uuid = "0";
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 	public Account(final Jid jid, final String password) {
 | 
					
						
							|  |  |  | 		this(java.util.UUID.randomUUID().toString(), jid,
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 				password, 0, null, "", null);
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 	public Account(final String uuid, final Jid jid,
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:19:42 +01:00
										 |  |  | 			final String password, final int options, final String rosterVersion, final String keys,
 | 
					
						
							|  |  |  | 			final String avatar) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		this.uuid = uuid;
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | 		this.jid = jid;
 | 
					
						
							| 
									
										
										
										
											2014-11-09 19:13:19 +01:00
										 |  |  | 		if (jid.isBareJid()) {
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | 			this.setResource("mobile");
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		this.password = password;
 | 
					
						
							|  |  |  | 		this.options = options;
 | 
					
						
							|  |  |  | 		this.rosterVersion = rosterVersion;
 | 
					
						
							|  |  |  | 		try {
 | 
					
						
							|  |  |  | 			this.keys = new JSONObject(keys);
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 		} catch (final JSONException ignored) {
 | 
					
						
							| 
									
										
										
										
											2015-02-26 16:55:49 +01:00
										 |  |  | 			this.keys = new JSONObject();
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 		this.avatar = avatar;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 	public static Account fromCursor(final Cursor cursor) {
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | 		Jid jid = null;
 | 
					
						
							|  |  |  | 		try {
 | 
					
						
							|  |  |  | 			jid = Jid.fromParts(cursor.getString(cursor.getColumnIndex(USERNAME)),
 | 
					
						
							|  |  |  | 					cursor.getString(cursor.getColumnIndex(SERVER)), "mobile");
 | 
					
						
							|  |  |  | 		} catch (final InvalidJidException ignored) {
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
 | 
					
						
							|  |  |  | 				jid,
 | 
					
						
							|  |  |  | 				cursor.getString(cursor.getColumnIndex(PASSWORD)),
 | 
					
						
							|  |  |  | 				cursor.getInt(cursor.getColumnIndex(OPTIONS)),
 | 
					
						
							|  |  |  | 				cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
 | 
					
						
							|  |  |  | 				cursor.getString(cursor.getColumnIndex(KEYS)),
 | 
					
						
							|  |  |  | 				cursor.getString(cursor.getColumnIndex(AVATAR)));
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 	public boolean isOptionSet(final int option) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		return ((options & (1 << option)) != 0);
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 	public void setOption(final int option, final boolean value) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		if (value) {
 | 
					
						
							|  |  |  | 			this.options |= 1 << option;
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			this.options &= ~(1 << option);
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public String getUsername() {
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 		return jid.getLocalpart();
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 	public void setUsername(final String username) throws InvalidJidException {
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | 		jid = Jid.fromParts(username, jid.getDomainpart(), jid.getResourcepart());
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 	public Jid getServer() {
 | 
					
						
							|  |  |  | 		return jid.toDomainJid();
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 	public void setServer(final String server) throws InvalidJidException {
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | 		jid = Jid.fromParts(jid.getLocalpart(), server, jid.getResourcepart());
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public String getPassword() {
 | 
					
						
							|  |  |  | 		return password;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 	public void setPassword(final String password) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		this.password = password;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:09:02 +01:00
										 |  |  | 	public State getStatus() {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		if (isOptionSet(OPTION_DISABLED)) {
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:09:02 +01:00
										 |  |  | 			return State.DISABLED;
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		} else {
 | 
					
						
							|  |  |  | 			return this.status;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:09:02 +01:00
										 |  |  | 	public void setStatus(final State status) {
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | 		this.status = status;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	public boolean errorStatus() {
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:09:02 +01:00
										 |  |  | 		return getStatus().isError();
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public boolean hasErrorStatus() {
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:09:02 +01:00
										 |  |  | 		return getXmppConnection() != null && getStatus().isError() && getXmppConnection().getAttempt() >= 2;
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public String getResource() {
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 		return jid.getResourcepart();
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-09 12:46:54 +02:00
										 |  |  | 	public boolean setResource(final String resource) {
 | 
					
						
							|  |  |  | 		final String oldResource = jid.getResourcepart();
 | 
					
						
							|  |  |  | 		if (oldResource == null || !oldResource.equals(resource)) {
 | 
					
						
							|  |  |  | 			try {
 | 
					
						
							|  |  |  | 				jid = Jid.fromParts(jid.getLocalpart(), jid.getDomainpart(), resource);
 | 
					
						
							|  |  |  | 				return true;
 | 
					
						
							|  |  |  | 			} catch (final InvalidJidException ignored) {
 | 
					
						
							|  |  |  | 				return true;
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2015-04-09 12:46:54 +02:00
										 |  |  | 		return false;
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 	public Jid getJid() {
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:57:22 +01:00
										 |  |  | 		return jid;
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public JSONObject getKeys() {
 | 
					
						
							|  |  |  | 		return keys;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-29 11:17:26 +02:00
										 |  |  | 	public String getKey(final String name) {
 | 
					
						
							|  |  |  | 		return this.keys.optString(name, null);
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 	public boolean setKey(final String keyName, final String keyValue) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		try {
 | 
					
						
							|  |  |  | 			this.keys.put(keyName, keyValue);
 | 
					
						
							|  |  |  | 			return true;
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 		} catch (final JSONException e) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 			return false;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	@Override
 | 
					
						
							|  |  |  | 	public ContentValues getContentValues() {
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 		final ContentValues values = new ContentValues();
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		values.put(UUID, uuid);
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 		values.put(USERNAME, jid.getLocalpart());
 | 
					
						
							|  |  |  | 		values.put(SERVER, jid.getDomainpart());
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		values.put(PASSWORD, password);
 | 
					
						
							|  |  |  | 		values.put(OPTIONS, options);
 | 
					
						
							|  |  |  | 		values.put(KEYS, this.keys.toString());
 | 
					
						
							|  |  |  | 		values.put(ROSTERVERSION, rosterVersion);
 | 
					
						
							|  |  |  | 		values.put(AVATAR, avatar);
 | 
					
						
							|  |  |  | 		return values;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 04:36:32 +02:00
										 |  |  | 	public void initAccountServices(final XmppConnectionService context) {
 | 
					
						
							|  |  |  | 		this.mOtrService = new OtrService(context, this);
 | 
					
						
							| 
									
										
										
										
											2014-11-16 00:20:20 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 04:36:32 +02:00
										 |  |  | 	public OtrService getOtrService() {
 | 
					
						
							|  |  |  | 		return this.mOtrService;
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public XmppConnection getXmppConnection() {
 | 
					
						
							|  |  |  | 		return this.xmppConnection;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-21 21:43:58 +01:00
										 |  |  | 	public void setXmppConnection(final XmppConnection connection) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		this.xmppConnection = connection;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public String getOtrFingerprint() {
 | 
					
						
							|  |  |  | 		if (this.otrFingerprint == null) {
 | 
					
						
							|  |  |  | 			try {
 | 
					
						
							| 
									
										
										
										
											2015-05-26 04:36:32 +02:00
										 |  |  | 				if (this.mOtrService == null) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 					return null;
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2015-05-26 04:36:32 +02:00
										 |  |  | 				final PublicKey publicKey = this.mOtrService.getPublicKey();
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 				if (publicKey == null || !(publicKey instanceof DSAPublicKey)) {
 | 
					
						
							| 
									
										
										
										
											2014-11-16 00:20:20 +01:00
										 |  |  | 					return null;
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							|  |  |  | 				this.otrFingerprint = new OtrCryptoEngineImpl().getFingerprint(publicKey);
 | 
					
						
							|  |  |  | 				return this.otrFingerprint;
 | 
					
						
							| 
									
										
										
										
											2014-11-05 21:55:47 +01:00
										 |  |  | 			} catch (final OtrCryptoException ignored) {
 | 
					
						
							| 
									
										
										
										
											2014-11-16 00:20:20 +01:00
										 |  |  | 				return null;
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2014-11-16 00:20:20 +01:00
										 |  |  | 		} else {
 | 
					
						
							|  |  |  | 			return this.otrFingerprint;
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public String getRosterVersion() {
 | 
					
						
							|  |  |  | 		if (this.rosterVersion == null) {
 | 
					
						
							|  |  |  | 			return "";
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			return this.rosterVersion;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-21 21:43:58 +01:00
										 |  |  | 	public void setRosterVersion(final String version) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		this.rosterVersion = version;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public int countPresences() {
 | 
					
						
							| 
									
										
										
										
											2014-12-20 17:23:03 +01:00
										 |  |  | 		return this.getRoster().getContact(this.getJid().toBareJid()).getPresences().size();
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public String getPgpSignature() {
 | 
					
						
							|  |  |  | 		if (keys.has("pgp_signature")) {
 | 
					
						
							|  |  |  | 			try {
 | 
					
						
							|  |  |  | 				return keys.getString("pgp_signature");
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 			} catch (final JSONException e) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 				return null;
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			return null;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public Roster getRoster() {
 | 
					
						
							|  |  |  | 		return this.roster;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public List<Bookmark> getBookmarks() {
 | 
					
						
							|  |  |  | 		return this.bookmarks;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-21 21:43:58 +01:00
										 |  |  | 	public void setBookmarks(final List<Bookmark> bookmarks) {
 | 
					
						
							| 
									
										
										
										
											2014-11-09 16:20:36 +01:00
										 |  |  | 		this.bookmarks = bookmarks;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-06 20:45:38 +01:00
										 |  |  | 	public boolean hasBookmarkFor(final Jid conferenceJid) {
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 		for (final Bookmark bookmark : this.bookmarks) {
 | 
					
						
							| 
									
										
										
										
											2014-12-03 00:18:07 +01:00
										 |  |  | 			final Jid jid = bookmark.getJid();
 | 
					
						
							|  |  |  | 			if (jid != null && jid.equals(conferenceJid.toBareJid())) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 				return true;
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return false;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 	public boolean setAvatar(final String filename) {
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 		if (this.avatar != null && this.avatar.equals(filename)) {
 | 
					
						
							|  |  |  | 			return false;
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			this.avatar = filename;
 | 
					
						
							|  |  |  | 			return true;
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public String getAvatar() {
 | 
					
						
							|  |  |  | 		return this.avatar;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public void activateGracePeriod() {
 | 
					
						
							|  |  |  | 		this.mEndGracePeriod = SystemClock.elapsedRealtime()
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:19:42 +01:00
										 |  |  | 			+ (Config.CARBON_GRACE_PERIOD * 1000);
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public void deactivateGracePeriod() {
 | 
					
						
							|  |  |  | 		this.mEndGracePeriod = 0L;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public boolean inGracePeriod() {
 | 
					
						
							|  |  |  | 		return SystemClock.elapsedRealtime() < this.mEndGracePeriod;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2014-11-16 00:20:20 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	public String getShareableUri() {
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 		final String fingerprint = this.getOtrFingerprint();
 | 
					
						
							| 
									
										
										
										
											2014-11-16 00:20:20 +01:00
										 |  |  | 		if (fingerprint != null) {
 | 
					
						
							|  |  |  | 			return "xmpp:" + this.getJid().toBareJid().toString() + "?otr-fingerprint="+fingerprint;
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			return "xmpp:" + this.getJid().toBareJid().toString();
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2014-12-21 21:43:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	public boolean isBlocked(final ListItem contact) {
 | 
					
						
							|  |  |  | 		final Jid jid = contact.getJid();
 | 
					
						
							|  |  |  | 		return jid != null && (blocklist.contains(jid.toBareJid()) || blocklist.contains(jid.toDomainJid()));
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public boolean isBlocked(final Jid jid) {
 | 
					
						
							|  |  |  | 		return jid != null && blocklist.contains(jid.toBareJid());
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public Collection<Jid> getBlocklist() {
 | 
					
						
							|  |  |  | 		return this.blocklist;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public void clearBlocklist() {
 | 
					
						
							|  |  |  | 		getBlocklist().clear();
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2014-12-23 23:19:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	public boolean isOnlineAndConnected() {
 | 
					
						
							|  |  |  | 		return this.getStatus() == State.ONLINE && this.getXmppConnection() != null;
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2014-10-22 18:38:44 +02:00
										 |  |  | }
 |