diff options
Diffstat (limited to 'src/eu/siacs/conversations/entities')
-rw-r--r-- | src/eu/siacs/conversations/entities/Account.java | 156 |
1 files changed, 96 insertions, 60 deletions
diff --git a/src/eu/siacs/conversations/entities/Account.java b/src/eu/siacs/conversations/entities/Account.java index 5de2c532..19da1faf 100644 --- a/src/eu/siacs/conversations/entities/Account.java +++ b/src/eu/siacs/conversations/entities/Account.java @@ -11,6 +11,7 @@ import net.java.otr4j.crypto.OtrCryptoException; import org.json.JSONException; import org.json.JSONObject; +import eu.siacs.conversations.R; import eu.siacs.conversations.crypto.OtrEngine; import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.utils.UIHelper; @@ -19,24 +20,23 @@ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -public class Account extends AbstractEntity{ - +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 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; - + public static final int STATUS_CONNECTING = 0; public static final int STATUS_DISABLED = -2; public static final int STATUS_OFFLINE = -1; @@ -49,7 +49,7 @@ 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; protected String password; @@ -59,30 +59,33 @@ public class Account extends AbstractEntity{ protected int status = -1; protected JSONObject keys = new JSONObject(); protected String avatar; - + protected boolean online = false; - + transient OtrEngine otrEngine = null; transient XmppConnection xmppConnection = null; transient protected Presences presences = new Presences(); 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>(); - + public Account() { this.uuid = "0"; } - + public Account(String username, String server, String password) { - this(java.util.UUID.randomUUID().toString(),username,server,password,0,null,""); + this(java.util.UUID.randomUUID().toString(), username, server, + password, 0, null, ""); } - public Account(String uuid, String username, String server,String password, int options, String rosterVersion, String keys) { + + public Account(String uuid, String username, String server, + String password, int options, String rosterVersion, String keys) { this.uuid = uuid; this.username = username; this.server = server; @@ -92,14 +95,14 @@ public class Account extends AbstractEntity{ try { this.keys = new JSONObject(keys); } catch (JSONException e) { - + } } - + public boolean isOptionSet(int option) { return ((options & (1 << option)) != 0); } - + public void setOption(int option, boolean value) { if (value) { this.options |= 1 << option; @@ -107,7 +110,7 @@ public class Account extends AbstractEntity{ this.options &= ~(1 << option); } } - + public String getUsername() { return username; } @@ -131,11 +134,11 @@ public class Account extends AbstractEntity{ public void setPassword(String password) { this.password = password; } - + public void setStatus(int status) { this.status = status; } - + public int getStatus() { if (isOptionSet(OPTION_DISABLED)) { return STATUS_DISABLED; @@ -143,32 +146,34 @@ public class Account extends AbstractEntity{ return this.status; } } - + public boolean errorStatus() { int s = getStatus(); - return (s == STATUS_OFFLINE || s == STATUS_SERVER_NOT_FOUND || s == STATUS_UNAUTHORIZED); + return (s == STATUS_REGISTRATION_FAILED || s == STATUS_REGISTRATION_CONFLICT || s == STATUS_REGISTRATION_NOT_SUPPORTED || s == STATUS_SERVER_NOT_FOUND || s == STATUS_UNAUTHORIZED); } - + public boolean hasErrorStatus() { - return getStatus() > STATUS_NO_INTERNET && (getXmppConnection().getAttempt() >= 2); + return getStatus() > STATUS_NO_INTERNET + && (getXmppConnection().getAttempt() >= 2); } - + public void setResource(String resource) { this.resource = resource; } - + public String getResource() { return this.resource; } - + public String getJid() { - return username.toLowerCase(Locale.getDefault())+"@"+server.toLowerCase(Locale.getDefault()); + return username.toLowerCase(Locale.getDefault()) + "@" + + server.toLowerCase(Locale.getDefault()); } - + public JSONObject getKeys() { return keys; } - + public String getSSLFingerprint() { if (keys.has("ssl_cert")) { try { @@ -180,11 +185,11 @@ public class Account extends AbstractEntity{ return null; } } - + public void setSSLCertFingerprint(String fingerprint) { this.setKey("ssl_cert", fingerprint); } - + public boolean setKey(String keyName, String keyValue) { try { this.keys.put(keyName, keyValue); @@ -197,16 +202,16 @@ public class Account extends AbstractEntity{ @Override public ContentValues getContentValues() { ContentValues values = new ContentValues(); - values.put(UUID,uuid); + values.put(UUID, uuid); values.put(USERNAME, username); values.put(SERVER, server); values.put(PASSWORD, password); - values.put(OPTIONS,options); - values.put(KEYS,this.keys.toString()); - values.put(ROSTERVERSION,rosterVersion); + values.put(OPTIONS, options); + values.put(KEYS, this.keys.toString()); + values.put(ROSTERVERSION, rosterVersion); return values; } - + public static Account fromCursor(Cursor cursor) { return new Account(cursor.getString(cursor.getColumnIndex(UUID)), cursor.getString(cursor.getColumnIndex(USERNAME)), @@ -214,14 +219,12 @@ public class Account extends AbstractEntity{ cursor.getString(cursor.getColumnIndex(PASSWORD)), cursor.getInt(cursor.getColumnIndex(OPTIONS)), cursor.getString(cursor.getColumnIndex(ROSTERVERSION)), - cursor.getString(cursor.getColumnIndex(KEYS)) - ); + cursor.getString(cursor.getColumnIndex(KEYS))); } - public OtrEngine getOtrEngine(Context context) { - if (otrEngine==null) { - otrEngine = new OtrEngine(context,this); + if (otrEngine == null) { + otrEngine = new OtrEngine(context, this); } return this.otrEngine; } @@ -235,37 +238,39 @@ public class Account extends AbstractEntity{ } public String getFullJid() { - return this.getJid()+"/"+this.resource; + return this.getJid() + "/" + this.resource; } - + public String getOtrFingerprint() { if (this.otrFingerprint == null) { try { - DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey(); + DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine + .getPublicKey(); if (pubkey == null) { return null; } - StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(pubkey)); + StringBuilder builder = new StringBuilder( + new OtrCryptoEngineImpl().getFingerprint(pubkey)); builder.insert(8, " "); builder.insert(17, " "); builder.insert(26, " "); builder.insert(35, " "); this.otrFingerprint = builder.toString(); } catch (OtrCryptoException e) { - + } } return this.otrFingerprint; } public String getRosterVersion() { - if (this.rosterVersion==null) { + if (this.rosterVersion == null) { return ""; } else { return this.rosterVersion; } } - + public void setRosterVersion(String version) { this.rosterVersion = version; } @@ -274,7 +279,7 @@ public class Account extends AbstractEntity{ this.getOtrEngine(applicationContext); return this.getOtrFingerprint(); } - + public void updatePresence(String resource, int status) { this.presences.updatePresence(resource, status); } @@ -282,7 +287,7 @@ public class Account extends AbstractEntity{ public void removePresence(String resource) { this.presences.removePresence(resource); } - + public void clearPresences() { this.presences = new Presences(); } @@ -302,9 +307,9 @@ public class Account extends AbstractEntity{ return null; } } - + public Roster getRoster() { - if (this.roster==null) { + if (this.roster == null) { this.roster = new Roster(this); } return this.roster; @@ -313,13 +318,13 @@ public class Account extends AbstractEntity{ public void setBookmarks(List<Bookmark> bookmarks) { this.bookmarks = bookmarks; } - + public List<Bookmark> getBookmarks() { return this.bookmarks; } public boolean hasBookmarkFor(String conferenceJid) { - for(Bookmark bmark : this.bookmarks) { + for (Bookmark bmark : this.bookmarks) { if (bmark.getJid().equals(conferenceJid)) { return true; } @@ -328,10 +333,11 @@ public class Account extends AbstractEntity{ } public Bitmap getImage(Context context, int size) { - if (this.avatar!=null) { + if (this.avatar != null) { Bitmap bm = FileBackend.getAvatar(this.avatar, size, context); - if (bm==null) { - return UIHelper.getContactPicture(getJid(), size, context, false); + if (bm == null) { + return UIHelper.getContactPicture(getJid(), size, context, + false); } else { return bm; } @@ -343,8 +349,38 @@ public class Account extends AbstractEntity{ public void setAvatar(String filename) { this.avatar = filename; } - + public String getAvatar() { return this.avatar; } + + 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; + } + } } |