aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/entities/Account.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/entities/Account.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/entities/Account.java171
1 files changed, 147 insertions, 24 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/entities/Account.java b/src/main/java/de/thedevstack/conversationsplus/entities/Account.java
index 8d879fa0..f7dee013 100644
--- a/src/main/java/de/thedevstack/conversationsplus/entities/Account.java
+++ b/src/main/java/de/thedevstack/conversationsplus/entities/Account.java
@@ -3,6 +3,7 @@ package de.thedevstack.conversationsplus.entities;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.SystemClock;
+import android.util.Pair;
import net.java.otr4j.crypto.OtrCryptoEngineImpl;
import net.java.otr4j.crypto.OtrCryptoException;
@@ -13,13 +14,19 @@ import org.json.JSONObject;
import java.security.PublicKey;
import java.security.interfaces.DSAPublicKey;
import java.util.Collection;
+import java.util.HashSet;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
+import de.thedevstack.conversationsplus.ConversationsPlusPreferences;
import de.thedevstack.conversationsplus.Config;
import de.thedevstack.conversationsplus.R;
import de.thedevstack.conversationsplus.crypto.OtrService;
+import de.thedevstack.conversationsplus.crypto.PgpDecryptionService;
+import de.thedevstack.conversationsplus.crypto.axolotl.AxolotlService;
+import de.thedevstack.conversationsplus.crypto.axolotl.AxolotlServiceImpl;
+import de.thedevstack.conversationsplus.crypto.axolotl.AxolotlServiceStub;
import de.thedevstack.conversationsplus.services.XmppConnectionService;
import de.thedevstack.conversationsplus.xmpp.XmppConnection;
import de.thedevstack.conversationsplus.xmpp.jid.InvalidJidException;
@@ -36,6 +43,9 @@ public class Account extends AbstractEntity {
public static final String ROSTERVERSION = "rosterversion";
public static final String KEYS = "keys";
public static final String AVATAR = "avatar";
+ public static final String DISPLAY_NAME = "display_name";
+ public static final String HOSTNAME = "hostname";
+ public static final String PORT = "port";
public static final String PINNED_MECHANISM_KEY = "pinned_mechanism";
@@ -43,12 +53,29 @@ public class Account extends AbstractEntity {
public static final int OPTION_DISABLED = 1;
public static final int OPTION_REGISTER = 2;
public static final int OPTION_USECOMPRESSION = 3;
+ public final HashSet<Pair<String, String>> inProgressDiscoFetches = new HashSet<>();
public boolean httpUploadAvailable() {
return xmppConnection != null && xmppConnection.getFeatures().httpUpload();
}
- public static enum State {
+ public void setDisplayName(String displayName) {
+ this.displayName = displayName;
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
+
+ public XmppConnection.Identity getServerIdentity() {
+ if (xmppConnection == null) {
+ return XmppConnection.Identity.UNKNOWN;
+ } else {
+ return xmppConnection.getServerIdentity();
+ }
+ }
+
+ public enum State {
DISABLED,
OFFLINE,
CONNECTING,
@@ -61,7 +88,8 @@ public class Account extends AbstractEntity {
REGISTRATION_SUCCESSFUL,
REGISTRATION_NOT_SUPPORTED(true),
SECURITY_ERROR(true),
- INCOMPATIBLE_SERVER(true);
+ INCOMPATIBLE_SERVER(true),
+ TOR_NOT_AVAILABLE(true);
private final boolean isError;
@@ -105,6 +133,8 @@ public class Account extends AbstractEntity {
return R.string.account_status_security_error;
case INCOMPATIBLE_SERVER:
return R.string.account_status_incompatible_server;
+ case TOR_NOT_AVAILABLE:
+ return R.string.account_status_tor_unavailable;
default:
return R.string.account_status_unknown;
}
@@ -113,6 +143,10 @@ public class Account extends AbstractEntity {
public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<>();
public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<>();
+
+ private static final String KEY_PGP_SIGNATURE = "pgp_signature";
+ private static final String KEY_PGP_ID = "pgp_id";
+
protected Jid jid;
protected String password;
protected int options = 0;
@@ -120,15 +154,19 @@ public class Account extends AbstractEntity {
protected State status = State.OFFLINE;
protected JSONObject keys = new JSONObject();
protected String avatar;
+ protected String displayName = null;
+ protected String hostname = null;
+ protected int port = 5222;
protected boolean online = false;
private OtrService mOtrService = null;
+ private AxolotlService axolotlService = null;
+ private PgpDecryptionService pgpDecryptionService = null;
private XmppConnection xmppConnection = null;
private long mEndGracePeriod = 0L;
private String otrFingerprint;
private final Roster roster = new Roster(this);
private List<Bookmark> bookmarks = new CopyOnWriteArrayList<>();
private final Collection<Jid> blocklist = new CopyOnWriteArraySet<>();
- private XmppConnectionService mXmppConnectionService;
public Account() {
this.uuid = "0";
@@ -136,12 +174,12 @@ public class Account extends AbstractEntity {
public Account(final Jid jid, final String password) {
this(java.util.UUID.randomUUID().toString(), jid,
- password, 0, null, "", null);
+ password, 0, null, "", null, null, null, 5222);
}
- public Account(final String uuid, final Jid jid,
+ private Account(final String uuid, final Jid jid,
final String password, final int options, final String rosterVersion, final String keys,
- final String avatar) {
+ final String avatar, String displayName, String hostname, int port) {
this.uuid = uuid;
this.jid = jid;
if (jid.isBareJid()) {
@@ -156,6 +194,9 @@ public class Account extends AbstractEntity {
this.keys = new JSONObject();
}
this.avatar = avatar;
+ this.displayName = displayName;
+ this.hostname = hostname;
+ this.port = port;
}
public static Account fromCursor(final Cursor cursor) {
@@ -171,7 +212,10 @@ public class Account extends AbstractEntity {
cursor.getInt(cursor.getColumnIndex(OPTIONS)),
cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
cursor.getString(cursor.getColumnIndex(KEYS)),
- cursor.getString(cursor.getColumnIndex(AVATAR)));
+ cursor.getString(cursor.getColumnIndex(AVATAR)),
+ cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)),
+ cursor.getString(cursor.getColumnIndex(HOSTNAME)),
+ cursor.getInt(cursor.getColumnIndex(PORT)));
}
public boolean isOptionSet(final int option) {
@@ -190,18 +234,14 @@ public class Account extends AbstractEntity {
return jid.getLocalpart();
}
- public void setUsername(final String username) throws InvalidJidException {
- jid = Jid.fromParts(username, jid.getDomainpart(), jid.getResourcepart());
+ public void setJid(final Jid jid) {
+ this.jid = jid;
}
public Jid getServer() {
return jid.toDomainJid();
}
- public void setServer(final String server) throws InvalidJidException {
- jid = Jid.fromParts(jid.getLocalpart(), server, jid.getResourcepart());
- }
-
public String getPassword() {
return password;
}
@@ -210,6 +250,22 @@ public class Account extends AbstractEntity {
this.password = password;
}
+ public void setHostname(String hostname) {
+ this.hostname = hostname;
+ }
+
+ public String getHostname() {
+ return this.hostname == null ? "" : this.hostname;
+ }
+
+ public void setPort(int port) {
+ this.port = port;
+ }
+
+ public int getPort() {
+ return this.port;
+ }
+
public State getStatus() {
if (isOptionSet(OPTION_DISABLED)) {
return State.DISABLED;
@@ -227,7 +283,7 @@ public class Account extends AbstractEntity {
}
public boolean hasErrorStatus() {
- return getXmppConnection() != null && getStatus().isError() && getXmppConnection().getAttempt() >= 2;
+ return getXmppConnection() != null && getStatus().isError() && getXmppConnection().getAttempt() >= 3;
}
public String getResource() {
@@ -255,6 +311,10 @@ public class Account extends AbstractEntity {
return keys;
}
+ public String getKey(final String name) {
+ return this.keys.optString(name, null);
+ }
+
public boolean setKey(final String keyName, final String keyValue) {
try {
this.keys.put(keyName, keyValue);
@@ -264,6 +324,14 @@ public class Account extends AbstractEntity {
}
}
+ public boolean setPrivateKeyAlias(String alias) {
+ return setKey("private_key_alias", alias);
+ }
+
+ public String getPrivateKeyAlias() {
+ return getKey("private_key_alias");
+ }
+
@Override
public ContentValues getContentValues() {
final ContentValues values = new ContentValues();
@@ -275,18 +343,38 @@ public class Account extends AbstractEntity {
values.put(KEYS, this.keys.toString());
values.put(ROSTERVERSION, rosterVersion);
values.put(AVATAR, avatar);
+ values.put(DISPLAY_NAME, displayName);
+ values.put(HOSTNAME, hostname);
+ values.put(PORT, port);
return values;
}
+ public AxolotlService getAxolotlService() {
+ return axolotlService;
+ }
+
public void initAccountServices(final XmppConnectionService context) {
- this.mXmppConnectionService = context;
this.mOtrService = new OtrService(context, this);
+ if (ConversationsPlusPreferences.omemoEnabled()) {
+ this.axolotlService = new AxolotlServiceImpl(this, context);
+ if (xmppConnection != null) {
+ xmppConnection.addOnAdvancedStreamFeaturesAvailableListener(axolotlService);
+ }
+ } else {
+ this.axolotlService = new AxolotlServiceStub();
+ }
+
+ this.pgpDecryptionService = new PgpDecryptionService(context);
}
public OtrService getOtrService() {
return this.mOtrService;
}
+ public PgpDecryptionService getPgpDecryptionService() {
+ return pgpDecryptionService;
+ }
+
public XmppConnection getXmppConnection() {
return this.xmppConnection;
}
@@ -332,17 +420,56 @@ public class Account extends AbstractEntity {
}
public String getPgpSignature() {
- if (keys.has("pgp_signature")) {
- try {
- return keys.getString("pgp_signature");
- } catch (final JSONException e) {
+ try {
+ if (keys.has(KEY_PGP_SIGNATURE) && !"null".equals(keys.getString(KEY_PGP_SIGNATURE))) {
+ return keys.getString(KEY_PGP_SIGNATURE);
+ } else {
return null;
}
- } else {
+ } catch (final JSONException e) {
return null;
}
}
+ public boolean setPgpSignature(String signature) {
+ try {
+ keys.put(KEY_PGP_SIGNATURE, signature);
+ } catch (JSONException e) {
+ return false;
+ }
+ return true;
+ }
+
+ public boolean unsetPgpSignature() {
+ try {
+ keys.put(KEY_PGP_SIGNATURE, JSONObject.NULL);
+ } catch (JSONException e) {
+ return false;
+ }
+ return true;
+ }
+
+ public long getPgpId() {
+ if (keys.has(KEY_PGP_ID)) {
+ try {
+ return keys.getLong(KEY_PGP_ID);
+ } catch (JSONException e) {
+ return -1;
+ }
+ } else {
+ return -1;
+ }
+ }
+
+ public boolean setPgpSignId(long pgpID) {
+ try {
+ keys.put(KEY_PGP_ID, pgpID);
+ } catch (JSONException e) {
+ return false;
+ }
+ return true;
+ }
+
public Roster getRoster() {
return this.roster;
}
@@ -420,8 +547,4 @@ public class Account extends AbstractEntity {
public boolean isOnlineAndConnected() {
return this.getStatus() == State.ONLINE && this.getXmppConnection() != null;
}
-
- public XmppConnectionService getXmppConnectionService() {
- return mXmppConnectionService;
- }
}