aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/entities/Account.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/eu/siacs/conversations/entities/Account.java')
-rw-r--r--src/main/java/eu/siacs/conversations/entities/Account.java192
1 files changed, 94 insertions, 98 deletions
diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java
index 80a9d62f9..8b6a7bd72 100644
--- a/src/main/java/eu/siacs/conversations/entities/Account.java
+++ b/src/main/java/eu/siacs/conversations/entities/Account.java
@@ -1,9 +1,8 @@
package eu.siacs.conversations.entities;
-import java.security.interfaces.DSAPublicKey;
-import java.util.List;
-import java.util.Locale;
-import java.util.concurrent.CopyOnWriteArrayList;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.os.SystemClock;
import net.java.otr4j.crypto.OtrCryptoEngineImpl;
import net.java.otr4j.crypto.OtrCryptoException;
@@ -11,14 +10,17 @@ import net.java.otr4j.crypto.OtrCryptoException;
import org.json.JSONException;
import org.json.JSONObject;
+import java.security.interfaces.DSAPublicKey;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.crypto.OtrEngine;
import eu.siacs.conversations.services.XmppConnectionService;
import eu.siacs.conversations.xmpp.XmppConnection;
-import android.content.ContentValues;
-import android.database.Cursor;
-import android.os.SystemClock;
+import eu.siacs.conversations.xmpp.jid.InvalidJidException;
+import eu.siacs.conversations.xmpp.jid.Jid;
public class Account extends AbstractEntity {
@@ -49,56 +51,68 @@ public class Account extends AbstractEntity {
public static final int STATUS_REGISTRATION_CONFLICT = 8;
public static final int STATUS_REGISTRATION_SUCCESSFULL = 9;
public static final int STATUS_REGISTRATION_NOT_SUPPORTED = 10;
-
- protected String username;
- protected String server;
+ public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<>();
+ public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<>();
+ protected Jid jid;
protected String password;
protected int options = 0;
protected String rosterVersion;
- protected String resource = "mobile";
protected int status = -1;
protected JSONObject keys = new JSONObject();
protected String avatar;
-
protected boolean online = false;
-
private OtrEngine otrEngine = null;
private XmppConnection xmppConnection = null;
private Presences presences = new Presences();
private long mEndGracePeriod = 0L;
private String otrFingerprint;
private Roster roster = null;
-
- private List<Bookmark> bookmarks = new CopyOnWriteArrayList<Bookmark>();
- public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<Conversation>();
- public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<Conversation>();
+ private List<Bookmark> bookmarks = new CopyOnWriteArrayList<>();
public Account() {
this.uuid = "0";
}
- public Account(String username, String server, String password) {
- this(java.util.UUID.randomUUID().toString(), username, server,
+ public Account(final Jid jid, final String password) {
+ this(java.util.UUID.randomUUID().toString(), jid,
password, 0, null, "", null);
}
- public Account(String uuid, String username, String server,
- String password, int options, String rosterVersion, String keys,
- String avatar) {
+ public Account(final String uuid, final Jid jid,
+ final String password, final int options, final String rosterVersion, final String keys,
+ final String avatar) {
this.uuid = uuid;
- this.username = username;
- this.server = server;
+ this.jid = jid;
+ if (jid.isBareJid()) {
+ this.setResource("mobile");
+ }
this.password = password;
this.options = options;
this.rosterVersion = rosterVersion;
try {
this.keys = new JSONObject(keys);
- } catch (JSONException e) {
+ } catch (final JSONException ignored) {
}
this.avatar = avatar;
}
+ public static Account fromCursor(Cursor cursor) {
+ 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)));
+ }
+
public boolean isOptionSet(int option) {
return ((options & (1 << option)) != 0);
}
@@ -112,33 +126,29 @@ public class Account extends AbstractEntity {
}
public String getUsername() {
- return username;
+ return jid.getLocalpart();
}
- public void setUsername(String username) {
- this.username = username;
+ public void setUsername(final String username) throws InvalidJidException {
+ jid = Jid.fromParts(username, jid.getDomainpart(), jid.getResourcepart());
}
- public String getServer() {
- return server;
+ public Jid getServer() {
+ return jid.toDomainJid();
}
- public void setServer(String server) {
- this.server = server;
+ public void setServer(final String server) throws InvalidJidException {
+ jid = Jid.fromParts(jid.getLocalpart(), server, jid.getResourcepart());
}
public String getPassword() {
return password;
}
- public void setPassword(String password) {
+ public void setPassword(final String password) {
this.password = password;
}
- public void setStatus(int status) {
- this.status = status;
- }
-
public int getStatus() {
if (isOptionSet(OPTION_DISABLED)) {
return STATUS_DISABLED;
@@ -147,6 +157,10 @@ public class Account extends AbstractEntity {
}
}
+ public void setStatus(final int status) {
+ this.status = status;
+ }
+
public boolean errorStatus() {
int s = getStatus();
return (s == STATUS_REGISTRATION_FAILED
@@ -156,25 +170,22 @@ public class Account extends AbstractEntity {
}
public boolean hasErrorStatus() {
- if (getXmppConnection() == null) {
- return false;
- } else {
- return getStatus() > STATUS_NO_INTERNET
- && (getXmppConnection().getAttempt() >= 2);
- }
+ return getXmppConnection() != null && getStatus() > STATUS_NO_INTERNET && (getXmppConnection().getAttempt() >= 2);
}
- public void setResource(String resource) {
- this.resource = resource;
+ public String getResource() {
+ return jid.getResourcepart();
}
- public String getResource() {
- return this.resource;
+ public void setResource(final String resource) {
+ try {
+ jid = Jid.fromParts(jid.getLocalpart(), jid.getDomainpart(), resource);
+ } catch (final InvalidJidException ignored) {
+ }
}
- public String getJid() {
- return username.toLowerCase(Locale.getDefault()) + "@"
- + server.toLowerCase(Locale.getDefault());
+ public Jid getJid() {
+ return jid;
}
public JSONObject getKeys() {
@@ -210,8 +221,8 @@ public class Account extends AbstractEntity {
public ContentValues getContentValues() {
ContentValues values = new ContentValues();
values.put(UUID, uuid);
- values.put(USERNAME, username);
- values.put(SERVER, server);
+ values.put(USERNAME, jid.getLocalpart());
+ values.put(SERVER, jid.getDomainpart());
values.put(PASSWORD, password);
values.put(OPTIONS, options);
values.put(KEYS, this.keys.toString());
@@ -220,17 +231,6 @@ public class Account extends AbstractEntity {
return values;
}
- public static Account fromCursor(Cursor cursor) {
- return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
- cursor.getString(cursor.getColumnIndex(USERNAME)),
- cursor.getString(cursor.getColumnIndex(SERVER)),
- 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)));
- }
-
public OtrEngine getOtrEngine(XmppConnectionService context) {
if (otrEngine == null) {
otrEngine = new OtrEngine(context, this);
@@ -246,10 +246,6 @@ public class Account extends AbstractEntity {
this.xmppConnection = connection;
}
- public String getFullJid() {
- return this.getJid() + "/" + this.resource;
- }
-
public String getOtrFingerprint() {
if (this.otrFingerprint == null) {
try {
@@ -265,7 +261,7 @@ public class Account extends AbstractEntity {
builder.insert(26, " ");
builder.insert(35, " ");
this.otrFingerprint = builder.toString();
- } catch (OtrCryptoException e) {
+ } catch (final OtrCryptoException ignored) {
}
}
@@ -324,17 +320,17 @@ public class Account extends AbstractEntity {
return this.roster;
}
- public void setBookmarks(List<Bookmark> bookmarks) {
- this.bookmarks = bookmarks;
- }
-
public List<Bookmark> getBookmarks() {
return this.bookmarks;
}
- public boolean hasBookmarkFor(String conferenceJid) {
+ public void setBookmarks(List<Bookmark> bookmarks) {
+ this.bookmarks = bookmarks;
+ }
+
+ public boolean hasBookmarkFor(final Jid conferenceJid) {
for (Bookmark bmark : this.bookmarks) {
- if (bmark.getJid().equals(conferenceJid)) {
+ if (bmark.getJid().equals(conferenceJid.toBareJid())) {
return true;
}
}
@@ -357,30 +353,30 @@ public class Account extends AbstractEntity {
public int getReadableStatusId() {
switch (getStatus()) {
- case Account.STATUS_DISABLED:
- return R.string.account_status_disabled;
- case Account.STATUS_ONLINE:
- return R.string.account_status_online;
- case Account.STATUS_CONNECTING:
- return R.string.account_status_connecting;
- case Account.STATUS_OFFLINE:
- return R.string.account_status_offline;
- case Account.STATUS_UNAUTHORIZED:
- return R.string.account_status_unauthorized;
- case Account.STATUS_SERVER_NOT_FOUND:
- return R.string.account_status_not_found;
- case Account.STATUS_NO_INTERNET:
- return R.string.account_status_no_internet;
- case Account.STATUS_REGISTRATION_FAILED:
- return R.string.account_status_regis_fail;
- case Account.STATUS_REGISTRATION_CONFLICT:
- return R.string.account_status_regis_conflict;
- case Account.STATUS_REGISTRATION_SUCCESSFULL:
- return R.string.account_status_regis_success;
- case Account.STATUS_REGISTRATION_NOT_SUPPORTED:
- return R.string.account_status_regis_not_sup;
- default:
- return R.string.account_status_unknown;
+ case Account.STATUS_DISABLED:
+ return R.string.account_status_disabled;
+ case Account.STATUS_ONLINE:
+ return R.string.account_status_online;
+ case Account.STATUS_CONNECTING:
+ return R.string.account_status_connecting;
+ case Account.STATUS_OFFLINE:
+ return R.string.account_status_offline;
+ case Account.STATUS_UNAUTHORIZED:
+ return R.string.account_status_unauthorized;
+ case Account.STATUS_SERVER_NOT_FOUND:
+ return R.string.account_status_not_found;
+ case Account.STATUS_NO_INTERNET:
+ return R.string.account_status_no_internet;
+ case Account.STATUS_REGISTRATION_FAILED:
+ return R.string.account_status_regis_fail;
+ case Account.STATUS_REGISTRATION_CONFLICT:
+ return R.string.account_status_regis_conflict;
+ case Account.STATUS_REGISTRATION_SUCCESSFULL:
+ return R.string.account_status_regis_success;
+ case Account.STATUS_REGISTRATION_NOT_SUPPORTED:
+ return R.string.account_status_regis_not_sup;
+ default:
+ return R.string.account_status_unknown;
}
}