2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.entities;
|
|
|
|
|
2014-11-09 15:20:36 +00:00
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.os.SystemClock;
|
2016-02-03 09:40:02 +00:00
|
|
|
import android.util.Pair;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2015-10-15 22:21:47 +00:00
|
|
|
import eu.siacs.conversations.crypto.PgpDecryptionService;
|
2016-06-13 11:32:14 +00:00
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2016-11-17 21:28:45 +00:00
|
|
|
import java.util.ArrayList;
|
2014-12-21 20:43:58 +00:00
|
|
|
import java.util.Collection;
|
2016-02-03 09:40:02 +00:00
|
|
|
import java.util.HashSet;
|
2014-11-09 15:20:36 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
2014-12-21 20:43:58 +00:00
|
|
|
import java.util.concurrent.CopyOnWriteArraySet;
|
2014-11-09 15:20:36 +00:00
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.R;
|
2015-06-25 14:56:34 +00:00
|
|
|
import eu.siacs.conversations.crypto.axolotl.AxolotlService;
|
2016-11-17 21:28:45 +00:00
|
|
|
import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
2016-11-17 21:28:45 +00:00
|
|
|
import eu.siacs.conversations.utils.XmppUri;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.xmpp.XmppConnection;
|
2014-11-05 20:55:47 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
|
|
|
|
import eu.siacs.conversations.xmpp.jid.Jid;
|
|
|
|
|
2014-10-22 16:38:44 +00: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";
|
2015-10-29 12:41:08 +00:00
|
|
|
public static final String DISPLAY_NAME = "display_name";
|
2015-11-28 19:11:38 +00:00
|
|
|
public static final String HOSTNAME = "hostname";
|
|
|
|
public static final String PORT = "port";
|
2016-04-22 19:25:06 +00:00
|
|
|
public static final String STATUS = "status";
|
|
|
|
public static final String STATUS_MESSAGE = "status_message";
|
2018-02-27 19:33:21 +00:00
|
|
|
public static final String RESOURCE = "resource";
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2014-11-15 14:42:40 +00:00
|
|
|
public static final String PINNED_MECHANISM_KEY = "pinned_mechanism";
|
|
|
|
|
2014-10-22 16:38:44 +00: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;
|
2016-04-26 21:23:48 +00:00
|
|
|
public static final int OPTION_MAGIC_CREATE = 4;
|
2017-08-10 12:13:07 +00:00
|
|
|
public static final int OPTION_REQUIRES_ACCESS_MODE_CHANGE = 5;
|
|
|
|
public static final int OPTION_LOGGED_IN_SUCCESSFULLY = 6;
|
2016-02-03 09:40:02 +00:00
|
|
|
public final HashSet<Pair<String, String>> inProgressDiscoFetches = new HashSet<>();
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2016-03-31 19:56:59 +00:00
|
|
|
public boolean httpUploadAvailable(long filesize) {
|
|
|
|
return xmppConnection != null && xmppConnection.getFeatures().httpUpload(filesize);
|
|
|
|
}
|
|
|
|
|
2015-06-28 11:14:21 +00:00
|
|
|
public boolean httpUploadAvailable() {
|
2016-03-31 19:56:59 +00:00
|
|
|
return httpUploadAvailable(0);
|
2015-06-28 11:14:21 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 12:41:08 +00:00
|
|
|
public void setDisplayName(String displayName) {
|
|
|
|
this.displayName = displayName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getDisplayName() {
|
|
|
|
return displayName;
|
|
|
|
}
|
|
|
|
|
2015-11-01 13:50:06 +00:00
|
|
|
public XmppConnection.Identity getServerIdentity() {
|
|
|
|
if (xmppConnection == null) {
|
|
|
|
return XmppConnection.Identity.UNKNOWN;
|
|
|
|
} else {
|
|
|
|
return xmppConnection.getServerIdentity();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-13 08:45:30 +00:00
|
|
|
public Contact getSelfContact() {
|
|
|
|
return getRoster().getContact(jid);
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:32:14 +00:00
|
|
|
public boolean hasPendingPgpIntent(Conversation conversation) {
|
|
|
|
return pgpDecryptionService != null && pgpDecryptionService.hasPendingIntent(conversation);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isPgpDecryptionServiceConnected() {
|
|
|
|
return pgpDecryptionService != null && pgpDecryptionService.isConnected();
|
|
|
|
}
|
|
|
|
|
2016-10-04 09:16:59 +00:00
|
|
|
public boolean setShowErrorNotification(boolean newValue) {
|
|
|
|
boolean oldValue = showErrorNotification();
|
|
|
|
setKey("show_error",Boolean.toString(newValue));
|
|
|
|
return newValue != oldValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean showErrorNotification() {
|
|
|
|
String key = getKey("show_error");
|
|
|
|
return key == null || Boolean.parseBoolean(key);
|
|
|
|
}
|
|
|
|
|
2017-11-06 12:57:25 +00:00
|
|
|
public boolean isEnabled() {
|
|
|
|
return !isOptionSet(Account.OPTION_DISABLED);
|
|
|
|
}
|
|
|
|
|
2015-11-28 19:11:38 +00:00
|
|
|
public enum State {
|
2017-08-12 20:17:04 +00:00
|
|
|
DISABLED(false,false),
|
|
|
|
OFFLINE(false),
|
|
|
|
CONNECTING(false),
|
|
|
|
ONLINE(false),
|
|
|
|
NO_INTERNET(false),
|
|
|
|
UNAUTHORIZED,
|
|
|
|
SERVER_NOT_FOUND,
|
|
|
|
REGISTRATION_SUCCESSFUL(false),
|
|
|
|
REGISTRATION_FAILED(true,false),
|
|
|
|
REGISTRATION_WEB(true,false),
|
|
|
|
REGISTRATION_CONFLICT(true,false),
|
|
|
|
REGISTRATION_NOT_SUPPORTED(true,false),
|
|
|
|
REGISTRATION_PLEASE_WAIT(true,false),
|
|
|
|
REGISTRATION_PASSWORD_TOO_WEAK(true,false),
|
|
|
|
TLS_ERROR,
|
|
|
|
INCOMPATIBLE_SERVER,
|
|
|
|
TOR_NOT_AVAILABLE,
|
|
|
|
DOWNGRADE_ATTACK,
|
|
|
|
SESSION_FAILURE,
|
|
|
|
BIND_FAILURE,
|
|
|
|
HOST_UNKNOWN,
|
|
|
|
STREAM_ERROR,
|
|
|
|
POLICY_VIOLATION,
|
|
|
|
PAYMENT_REQUIRED,
|
|
|
|
MISSING_INTERNET_PERMISSION(false),
|
2017-08-02 16:58:51 +00:00
|
|
|
NETWORK_IS_UNREACHABLE(false);
|
2014-11-15 16:09:02 +00:00
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
private final boolean isError;
|
2017-08-12 20:17:04 +00:00
|
|
|
private final boolean attemptReconnect;
|
2014-11-15 16:09:02 +00:00
|
|
|
|
|
|
|
public boolean isError() {
|
|
|
|
return this.isError;
|
|
|
|
}
|
|
|
|
|
2017-08-12 20:17:04 +00:00
|
|
|
public boolean isAttemptReconnect() {
|
|
|
|
return this.attemptReconnect;
|
|
|
|
}
|
|
|
|
|
2016-07-25 13:57:47 +00:00
|
|
|
State(final boolean isError) {
|
2017-08-12 20:17:04 +00:00
|
|
|
this(isError,true);
|
|
|
|
}
|
|
|
|
|
|
|
|
State(final boolean isError, final boolean reconnect) {
|
2014-11-15 16:09:02 +00:00
|
|
|
this.isError = isError;
|
2017-08-12 20:17:04 +00:00
|
|
|
this.attemptReconnect = reconnect;
|
2014-11-15 16:09:02 +00:00
|
|
|
}
|
|
|
|
|
2016-07-25 13:57:47 +00:00
|
|
|
State() {
|
2017-08-12 20:17:04 +00:00
|
|
|
this(true,true);
|
2014-11-15 16:09:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2017-06-19 18:02:41 +00:00
|
|
|
case REGISTRATION_WEB:
|
|
|
|
return R.string.account_status_regis_web;
|
2014-11-15 16:09:02 +00:00
|
|
|
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;
|
2017-05-05 07:33:05 +00:00
|
|
|
case TLS_ERROR:
|
|
|
|
return R.string.account_status_tls_error;
|
2014-11-15 16:29:58 +00:00
|
|
|
case INCOMPATIBLE_SERVER:
|
|
|
|
return R.string.account_status_incompatible_server;
|
2015-11-30 15:01:48 +00:00
|
|
|
case TOR_NOT_AVAILABLE:
|
|
|
|
return R.string.account_status_tor_unavailable;
|
2016-05-05 18:22:47 +00:00
|
|
|
case BIND_FAILURE:
|
|
|
|
return R.string.account_status_bind_failure;
|
2017-05-05 07:33:05 +00:00
|
|
|
case SESSION_FAILURE:
|
|
|
|
return R.string.session_failure;
|
|
|
|
case DOWNGRADE_ATTACK:
|
2017-05-24 17:12:37 +00:00
|
|
|
return R.string.sasl_downgrade;
|
2016-05-10 08:29:02 +00:00
|
|
|
case HOST_UNKNOWN:
|
|
|
|
return R.string.account_status_host_unknown;
|
2016-07-14 15:05:43 +00:00
|
|
|
case POLICY_VIOLATION:
|
|
|
|
return R.string.account_status_policy_violation;
|
2016-05-24 11:26:30 +00:00
|
|
|
case REGISTRATION_PLEASE_WAIT:
|
|
|
|
return R.string.registration_please_wait;
|
2016-07-25 13:57:47 +00:00
|
|
|
case REGISTRATION_PASSWORD_TOO_WEAK:
|
|
|
|
return R.string.registration_password_too_weak;
|
2016-07-14 15:05:43 +00:00
|
|
|
case STREAM_ERROR:
|
|
|
|
return R.string.account_status_stream_error;
|
2016-08-16 08:39:59 +00:00
|
|
|
case PAYMENT_REQUIRED:
|
|
|
|
return R.string.payment_required;
|
2016-08-19 19:47:08 +00:00
|
|
|
case MISSING_INTERNET_PERMISSION:
|
|
|
|
return R.string.missing_internet_permission;
|
2017-08-02 16:58:51 +00:00
|
|
|
case NETWORK_IS_UNREACHABLE:
|
|
|
|
return R.string.network_is_unreachable;
|
2014-11-15 16:09:02 +00:00
|
|
|
default:
|
|
|
|
return R.string.account_status_unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-09 15:20:36 +00:00
|
|
|
public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<>();
|
|
|
|
public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<>();
|
2015-10-29 13:03:41 +00:00
|
|
|
|
|
|
|
private static final String KEY_PGP_SIGNATURE = "pgp_signature";
|
|
|
|
private static final String KEY_PGP_ID = "pgp_id";
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
protected Jid jid;
|
2014-10-22 16:38:44 +00:00
|
|
|
protected String password;
|
|
|
|
protected int options = 0;
|
|
|
|
protected String rosterVersion;
|
2014-11-15 16:09:02 +00:00
|
|
|
protected State status = State.OFFLINE;
|
2016-10-13 09:27:26 +00:00
|
|
|
protected final JSONObject keys;
|
2018-02-27 19:33:21 +00:00
|
|
|
protected String resource;
|
2014-10-22 16:38:44 +00:00
|
|
|
protected String avatar;
|
2015-10-29 12:41:08 +00:00
|
|
|
protected String displayName = null;
|
2015-11-28 19:11:38 +00:00
|
|
|
protected String hostname = null;
|
|
|
|
protected int port = 5222;
|
2014-10-22 16:38:44 +00:00
|
|
|
protected boolean online = false;
|
2015-06-25 14:56:34 +00:00
|
|
|
private AxolotlService axolotlService = null;
|
2015-10-15 22:21:47 +00:00
|
|
|
private PgpDecryptionService pgpDecryptionService = null;
|
2014-10-22 16:38:44 +00:00
|
|
|
private XmppConnection xmppConnection = null;
|
|
|
|
private long mEndGracePeriod = 0L;
|
2014-12-20 16:23:03 +00:00
|
|
|
private final Roster roster = new Roster(this);
|
2014-11-05 20:55:47 +00:00
|
|
|
private List<Bookmark> bookmarks = new CopyOnWriteArrayList<>();
|
2014-12-21 20:43:58 +00:00
|
|
|
private final Collection<Jid> blocklist = new CopyOnWriteArraySet<>();
|
2016-04-22 19:25:06 +00:00
|
|
|
private Presence.Status presenceStatus = Presence.Status.ONLINE;
|
|
|
|
private String presenceStatusMessage = null;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
public Account(final Jid jid, final String password) {
|
|
|
|
this(java.util.UUID.randomUUID().toString(), jid,
|
2016-04-22 19:25:06 +00:00
|
|
|
password, 0, null, "", null, null, null, 5222, Presence.Status.ONLINE, null);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-16 17:57:47 +00:00
|
|
|
private Account(final String uuid, final Jid jid,
|
2016-04-22 19:25:06 +00:00
|
|
|
final String password, final int options, final String rosterVersion, final String keys,
|
|
|
|
final String avatar, String displayName, String hostname, int port,
|
|
|
|
final Presence.Status status, String statusMessage) {
|
2014-10-22 16:38:44 +00:00
|
|
|
this.uuid = uuid;
|
2014-11-09 15:20:36 +00:00
|
|
|
this.jid = jid;
|
2014-10-22 16:38:44 +00:00
|
|
|
this.password = password;
|
|
|
|
this.options = options;
|
|
|
|
this.rosterVersion = rosterVersion;
|
2016-10-13 09:27:26 +00:00
|
|
|
JSONObject tmp;
|
2014-10-22 16:38:44 +00:00
|
|
|
try {
|
2016-10-13 09:27:26 +00:00
|
|
|
tmp = new JSONObject(keys);
|
|
|
|
} catch(JSONException e) {
|
|
|
|
tmp = new JSONObject();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2016-10-13 09:27:26 +00:00
|
|
|
this.keys = tmp;
|
2014-10-22 16:38:44 +00:00
|
|
|
this.avatar = avatar;
|
2015-10-29 12:41:08 +00:00
|
|
|
this.displayName = displayName;
|
2015-11-28 19:11:38 +00:00
|
|
|
this.hostname = hostname;
|
|
|
|
this.port = port;
|
2016-04-22 19:25:06 +00:00
|
|
|
this.presenceStatus = status;
|
|
|
|
this.presenceStatusMessage = statusMessage;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 22:19:00 +00:00
|
|
|
public static Account fromCursor(final Cursor cursor) {
|
2014-11-09 15:20:36 +00:00
|
|
|
Jid jid = null;
|
|
|
|
try {
|
2018-02-27 19:33:21 +00:00
|
|
|
jid = Jid.fromParts(
|
|
|
|
cursor.getString(cursor.getColumnIndex(USERNAME)),
|
|
|
|
cursor.getString(cursor.getColumnIndex(SERVER)),
|
|
|
|
cursor.getString(cursor.getColumnIndex(RESOURCE)));
|
2014-11-09 15:20:36 +00:00
|
|
|
} 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)),
|
2015-10-29 12:41:08 +00:00
|
|
|
cursor.getString(cursor.getColumnIndex(AVATAR)),
|
2015-11-28 19:11:38 +00:00
|
|
|
cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)),
|
|
|
|
cursor.getString(cursor.getColumnIndex(HOSTNAME)),
|
2016-04-22 19:25:06 +00:00
|
|
|
cursor.getInt(cursor.getColumnIndex(PORT)),
|
|
|
|
Presence.Status.fromShowString(cursor.getString(cursor.getColumnIndex(STATUS))),
|
|
|
|
cursor.getString(cursor.getColumnIndex(STATUS_MESSAGE)));
|
2014-11-09 15:20:36 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 22:19:00 +00:00
|
|
|
public boolean isOptionSet(final int option) {
|
2014-10-22 16:38:44 +00:00
|
|
|
return ((options & (1 << option)) != 0);
|
|
|
|
}
|
|
|
|
|
2017-08-10 12:13:07 +00:00
|
|
|
public boolean setOption(final int option, final boolean value) {
|
|
|
|
final int before = this.options;
|
2014-10-22 16:38:44 +00:00
|
|
|
if (value) {
|
|
|
|
this.options |= 1 << option;
|
|
|
|
} else {
|
|
|
|
this.options &= ~(1 << option);
|
|
|
|
}
|
2017-08-10 12:13:07 +00:00
|
|
|
return before != this.options;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getUsername() {
|
2014-11-05 20:55:47 +00:00
|
|
|
return jid.getLocalpart();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 10:26:38 +00:00
|
|
|
public boolean setJid(final Jid next) {
|
2018-02-27 19:33:21 +00:00
|
|
|
final Jid previousFull = this.jid;
|
2016-09-14 10:26:38 +00:00
|
|
|
final Jid prev = this.jid != null ? this.jid.toBareJid() : null;
|
2017-08-10 12:17:47 +00:00
|
|
|
final boolean changed = prev == null || (next != null && !prev.equals(next.toBareJid()));
|
|
|
|
if (changed) {
|
|
|
|
final AxolotlService oldAxolotlService = this.axolotlService;
|
|
|
|
if (oldAxolotlService != null) {
|
|
|
|
oldAxolotlService.destroy();
|
|
|
|
this.jid = next;
|
|
|
|
this.axolotlService = oldAxolotlService.makeNew();
|
|
|
|
}
|
|
|
|
}
|
2016-09-14 10:26:38 +00:00
|
|
|
this.jid = next;
|
2018-02-27 19:33:21 +00:00
|
|
|
return next != null && next.equals(previousFull);
|
2014-11-09 15:20:36 +00:00
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
public Jid getServer() {
|
|
|
|
return jid.toDomainJid();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getPassword() {
|
|
|
|
return password;
|
|
|
|
}
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
public void setPassword(final String password) {
|
2014-10-22 16:38:44 +00:00
|
|
|
this.password = password;
|
|
|
|
}
|
|
|
|
|
2015-11-28 19:11:38 +00:00
|
|
|
public void setHostname(String hostname) {
|
|
|
|
this.hostname = hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getHostname() {
|
|
|
|
return this.hostname == null ? "" : this.hostname;
|
|
|
|
}
|
|
|
|
|
2015-11-29 14:44:45 +00:00
|
|
|
public boolean isOnion() {
|
2016-08-20 08:44:50 +00:00
|
|
|
final Jid server = getServer();
|
|
|
|
return server != null && server.toString().toLowerCase().endsWith(".onion");
|
2015-11-29 14:44:45 +00:00
|
|
|
}
|
|
|
|
|
2015-11-28 19:11:38 +00:00
|
|
|
public void setPort(int port) {
|
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPort() {
|
|
|
|
return this.port;
|
|
|
|
}
|
|
|
|
|
2014-11-15 16:09:02 +00:00
|
|
|
public State getStatus() {
|
2014-10-22 16:38:44 +00:00
|
|
|
if (isOptionSet(OPTION_DISABLED)) {
|
2014-11-15 16:09:02 +00:00
|
|
|
return State.DISABLED;
|
2014-10-22 16:38:44 +00:00
|
|
|
} else {
|
|
|
|
return this.status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 11:44:24 +00:00
|
|
|
public State getTrueStatus() {
|
|
|
|
return this.status;
|
|
|
|
}
|
|
|
|
|
2014-11-15 16:09:02 +00:00
|
|
|
public void setStatus(final State status) {
|
2014-11-09 15:20:36 +00:00
|
|
|
this.status = status;
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public boolean errorStatus() {
|
2014-11-15 16:09:02 +00:00
|
|
|
return getStatus().isError();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasErrorStatus() {
|
2016-07-14 21:23:13 +00:00
|
|
|
return getXmppConnection() != null
|
|
|
|
&& (getStatus().isError() || getStatus() == State.CONNECTING)
|
|
|
|
&& getXmppConnection().getAttempt() >= 3;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 19:25:06 +00:00
|
|
|
public void setPresenceStatus(Presence.Status status) {
|
|
|
|
this.presenceStatus = status;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Presence.Status getPresenceStatus() {
|
|
|
|
return this.presenceStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setPresenceStatusMessage(String message) {
|
|
|
|
this.presenceStatusMessage = message;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPresenceStatusMessage() {
|
|
|
|
return this.presenceStatusMessage;
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public String getResource() {
|
2014-11-05 20:55:47 +00:00
|
|
|
return jid.getResourcepart();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 10:46:54 +00: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 15:20:36 +00:00
|
|
|
}
|
2015-04-09 10:46:54 +00:00
|
|
|
return false;
|
2014-11-09 15:20:36 +00:00
|
|
|
}
|
|
|
|
|
2014-11-05 20:55:47 +00:00
|
|
|
public Jid getJid() {
|
2014-11-09 15:57:22 +00:00
|
|
|
return jid;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public JSONObject getKeys() {
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2015-05-29 09:17:26 +00:00
|
|
|
public String getKey(final String name) {
|
2016-10-13 09:27:26 +00:00
|
|
|
synchronized (this.keys) {
|
|
|
|
return this.keys.optString(name, null);
|
|
|
|
}
|
2015-05-29 09:17:26 +00:00
|
|
|
}
|
|
|
|
|
2016-10-13 09:27:26 +00:00
|
|
|
public int getKeyAsInt(final String name, int defaultValue) {
|
|
|
|
String key = getKey(name);
|
2014-10-22 16:38:44 +00:00
|
|
|
try {
|
2016-10-13 09:27:26 +00:00
|
|
|
return key == null ? defaultValue : Integer.parseInt(key);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setKey(final String keyName, final String keyValue) {
|
|
|
|
synchronized (this.keys) {
|
|
|
|
try {
|
|
|
|
this.keys.put(keyName, keyValue);
|
|
|
|
return true;
|
|
|
|
} catch (final JSONException e) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-09 11:37:08 +00:00
|
|
|
public boolean setPrivateKeyAlias(String alias) {
|
|
|
|
return setKey("private_key_alias", alias);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPrivateKeyAlias() {
|
|
|
|
return getKey("private_key_alias");
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
@Override
|
|
|
|
public ContentValues getContentValues() {
|
2014-12-23 22:19:00 +00:00
|
|
|
final ContentValues values = new ContentValues();
|
2014-10-22 16:38:44 +00:00
|
|
|
values.put(UUID, uuid);
|
2014-11-05 20:55:47 +00:00
|
|
|
values.put(USERNAME, jid.getLocalpart());
|
|
|
|
values.put(SERVER, jid.getDomainpart());
|
2014-10-22 16:38:44 +00:00
|
|
|
values.put(PASSWORD, password);
|
|
|
|
values.put(OPTIONS, options);
|
2016-10-13 09:27:26 +00:00
|
|
|
synchronized (this.keys) {
|
|
|
|
values.put(KEYS, this.keys.toString());
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
values.put(ROSTERVERSION, rosterVersion);
|
|
|
|
values.put(AVATAR, avatar);
|
2015-10-29 12:41:08 +00:00
|
|
|
values.put(DISPLAY_NAME, displayName);
|
2015-11-28 19:11:38 +00:00
|
|
|
values.put(HOSTNAME, hostname);
|
|
|
|
values.put(PORT, port);
|
2016-04-22 19:25:06 +00:00
|
|
|
values.put(STATUS, presenceStatus.toShowString());
|
|
|
|
values.put(STATUS_MESSAGE, presenceStatusMessage);
|
2018-02-27 19:33:21 +00:00
|
|
|
values.put(RESOURCE,jid.getResourcepart());
|
2014-10-22 16:38:44 +00:00
|
|
|
return values;
|
|
|
|
}
|
|
|
|
|
2015-06-25 14:56:34 +00:00
|
|
|
public AxolotlService getAxolotlService() {
|
|
|
|
return axolotlService;
|
|
|
|
}
|
|
|
|
|
2015-05-26 02:36:32 +00:00
|
|
|
public void initAccountServices(final XmppConnectionService context) {
|
2015-06-25 14:56:34 +00:00
|
|
|
this.axolotlService = new AxolotlService(this, context);
|
2016-06-13 11:32:14 +00:00
|
|
|
this.pgpDecryptionService = new PgpDecryptionService(context);
|
2015-10-16 21:48:42 +00:00
|
|
|
if (xmppConnection != null) {
|
|
|
|
xmppConnection.addOnAdvancedStreamFeaturesAvailableListener(axolotlService);
|
|
|
|
}
|
2014-11-15 23:20:20 +00:00
|
|
|
}
|
|
|
|
|
2015-10-15 22:21:47 +00:00
|
|
|
public PgpDecryptionService getPgpDecryptionService() {
|
2016-06-13 11:32:14 +00:00
|
|
|
return this.pgpDecryptionService;
|
2015-10-15 22:21:47 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public XmppConnection getXmppConnection() {
|
|
|
|
return this.xmppConnection;
|
|
|
|
}
|
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
public void setXmppConnection(final XmppConnection connection) {
|
2014-10-22 16:38:44 +00:00
|
|
|
this.xmppConnection = connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getRosterVersion() {
|
|
|
|
if (this.rosterVersion == null) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return this.rosterVersion;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-21 20:43:58 +00:00
|
|
|
public void setRosterVersion(final String version) {
|
2014-10-22 16:38:44 +00:00
|
|
|
this.rosterVersion = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int countPresences() {
|
2016-05-15 10:35:31 +00:00
|
|
|
return this.getSelfContact().getPresences().size();
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getPgpSignature() {
|
2016-10-13 09:27:26 +00:00
|
|
|
return getKey(KEY_PGP_SIGNATURE);
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 13:03:41 +00:00
|
|
|
public boolean setPgpSignature(String signature) {
|
2016-10-13 09:27:26 +00:00
|
|
|
return setKey(KEY_PGP_SIGNATURE, signature);
|
2015-10-29 13:03:41 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 15:43:55 +00:00
|
|
|
public boolean unsetPgpSignature() {
|
2016-10-13 09:27:26 +00:00
|
|
|
synchronized (this.keys) {
|
|
|
|
return keys.remove(KEY_PGP_SIGNATURE) != null;
|
2015-12-02 15:43:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 13:03:41 +00:00
|
|
|
public long getPgpId() {
|
2016-10-13 09:27:26 +00:00
|
|
|
synchronized (this.keys) {
|
|
|
|
if (keys.has(KEY_PGP_ID)) {
|
|
|
|
try {
|
|
|
|
return keys.getLong(KEY_PGP_ID);
|
|
|
|
} catch (JSONException e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-05 11:17:04 +00:00
|
|
|
return 0;
|
2015-10-29 13:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean setPgpSignId(long pgpID) {
|
2016-10-13 09:27:26 +00:00
|
|
|
synchronized (this.keys) {
|
|
|
|
try {
|
2017-05-04 11:02:46 +00:00
|
|
|
if (pgpID == 0) {
|
|
|
|
keys.remove(KEY_PGP_ID);
|
|
|
|
} else {
|
|
|
|
keys.put(KEY_PGP_ID, pgpID);
|
|
|
|
}
|
2016-10-13 09:27:26 +00:00
|
|
|
} catch (JSONException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2015-10-29 13:03:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
public Roster getRoster() {
|
|
|
|
return this.roster;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<Bookmark> getBookmarks() {
|
|
|
|
return this.bookmarks;
|
|
|
|
}
|
|
|
|
|
2018-02-10 18:06:31 +00:00
|
|
|
public void setBookmarks(final CopyOnWriteArrayList<Bookmark> bookmarks) {
|
2014-11-09 15:20:36 +00:00
|
|
|
this.bookmarks = bookmarks;
|
|
|
|
}
|
|
|
|
|
2014-11-06 19:45:38 +00:00
|
|
|
public boolean hasBookmarkFor(final Jid conferenceJid) {
|
2018-02-10 18:06:31 +00:00
|
|
|
return getBookmark(conferenceJid) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Bookmark getBookmark(final Jid jid) {
|
|
|
|
for(final Bookmark bookmark : this.bookmarks) {
|
|
|
|
if (bookmark.getJid() != null && jid.toBareJid().equals(bookmark.getJid().toBareJid())) {
|
|
|
|
return bookmark;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-10 18:06:31 +00:00
|
|
|
return null;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 22:19:00 +00:00
|
|
|
public boolean setAvatar(final String filename) {
|
2014-10-22 16:38:44 +00:00
|
|
|
if (this.avatar != null && this.avatar.equals(filename)) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
this.avatar = filename;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getAvatar() {
|
|
|
|
return this.avatar;
|
|
|
|
}
|
|
|
|
|
2016-06-01 22:24:37 +00:00
|
|
|
public void activateGracePeriod(long duration) {
|
|
|
|
this.mEndGracePeriod = SystemClock.elapsedRealtime() + duration;
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void deactivateGracePeriod() {
|
|
|
|
this.mEndGracePeriod = 0L;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean inGracePeriod() {
|
|
|
|
return SystemClock.elapsedRealtime() < this.mEndGracePeriod;
|
|
|
|
}
|
2014-11-15 23:20:20 +00:00
|
|
|
|
|
|
|
public String getShareableUri() {
|
2016-11-17 21:28:45 +00:00
|
|
|
List<XmppUri.Fingerprint> fingerprints = this.getFingerprints();
|
|
|
|
String uri = "xmpp:"+this.getJid().toBareJid().toString();
|
|
|
|
if (fingerprints.size() > 0) {
|
2016-12-03 12:37:26 +00:00
|
|
|
return XmppUri.getFingerprintUri(uri,fingerprints,';');
|
|
|
|
} else {
|
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getShareableLink() {
|
|
|
|
List<XmppUri.Fingerprint> fingerprints = this.getFingerprints();
|
|
|
|
String uri = "https://conversations.im/i/"+this.getJid().toBareJid().toString();
|
|
|
|
if (fingerprints.size() > 0) {
|
|
|
|
return XmppUri.getFingerprintUri(uri,fingerprints,'&');
|
2014-11-15 23:20:20 +00:00
|
|
|
} else {
|
2016-11-17 21:28:45 +00:00
|
|
|
return uri;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<XmppUri.Fingerprint> getFingerprints() {
|
|
|
|
ArrayList<XmppUri.Fingerprint> fingerprints = new ArrayList<>();
|
2016-12-23 20:19:38 +00:00
|
|
|
if (axolotlService == null) {
|
|
|
|
return fingerprints;
|
|
|
|
}
|
2016-11-17 21:28:45 +00:00
|
|
|
fingerprints.add(new XmppUri.Fingerprint(XmppUri.FingerprintType.OMEMO,axolotlService.getOwnFingerprint().substring(2),axolotlService.getOwnDeviceId()));
|
|
|
|
for(XmppAxolotlSession session : axolotlService.findOwnSessions()) {
|
|
|
|
if (session.getTrust().isVerified() && session.getTrust().isActive()) {
|
|
|
|
fingerprints.add(new XmppUri.Fingerprint(XmppUri.FingerprintType.OMEMO,session.getFingerprint().substring(2).replaceAll("\\s",""),session.getRemoteAddress().getDeviceId()));
|
|
|
|
}
|
2014-11-15 23:20:20 +00:00
|
|
|
}
|
2016-11-17 21:28:45 +00:00
|
|
|
return fingerprints;
|
2014-11-15 23:20:20 +00:00
|
|
|
}
|
2014-12-21 20:43:58 +00: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 22:19:00 +00:00
|
|
|
|
|
|
|
public boolean isOnlineAndConnected() {
|
|
|
|
return this.getStatus() == State.ONLINE && this.getXmppConnection() != null;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|