anotherim/src/eu/siacs/conversations/entities/Account.java

387 lines
9.8 KiB
Java
Raw Normal View History

2014-02-28 17:46:01 +00:00
package eu.siacs.conversations.entities;
2014-02-16 15:32:15 +00:00
import java.security.interfaces.DSAPublicKey;
2014-07-14 09:47:42 +00:00
import java.util.List;
2014-07-04 14:25:02 +00:00
import java.util.Locale;
2014-07-18 19:57:10 +00:00
import java.util.concurrent.CopyOnWriteArrayList;
2014-02-16 15:32:15 +00:00
import net.java.otr4j.crypto.OtrCryptoEngineImpl;
import net.java.otr4j.crypto.OtrCryptoException;
2014-02-13 22:40:08 +00:00
import org.json.JSONException;
import org.json.JSONObject;
import eu.siacs.conversations.R;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.crypto.OtrEngine;
import eu.siacs.conversations.persistance.FileBackend;
import eu.siacs.conversations.utils.UIHelper;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.xmpp.XmppConnection;
import android.content.ContentValues;
2014-02-13 22:40:08 +00:00
import android.content.Context;
2014-01-28 18:21:54 +00:00
import android.database.Cursor;
import android.graphics.Bitmap;
public class Account extends AbstractEntity {
2014-01-28 18:21:54 +00:00
public static final String TABLENAME = "accounts";
2014-01-28 18:21:54 +00:00
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";
2014-02-13 22:40:08 +00:00
public static final String KEYS = "keys";
2014-02-02 16:53:34 +00:00
public static final int OPTION_USETLS = 0;
2014-02-04 20:44:16 +00:00
public static final int OPTION_DISABLED = 1;
public static final int OPTION_REGISTER = 2;
2014-04-03 16:16:14 +00:00
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;
public static final int STATUS_ONLINE = 1;
public static final int STATUS_NO_INTERNET = 2;
public static final int STATUS_UNAUTHORIZED = 3;
public static final int STATUS_SERVER_NOT_FOUND = 5;
2014-03-06 14:11:56 +00:00
2014-03-13 16:29:22 +00:00
public static final int STATUS_REGISTRATION_FAILED = 7;
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;
2014-01-28 18:21:54 +00:00
protected String username;
protected String server;
protected String password;
protected int options = 0;
protected String rosterVersion;
protected String resource = "mobile";
protected int status = -1;
2014-02-13 22:40:08 +00:00
protected JSONObject keys = new JSONObject();
protected String avatar;
2014-01-28 18:21:54 +00:00
protected boolean online = false;
2014-02-13 22:40:08 +00:00
transient OtrEngine otrEngine = null;
transient XmppConnection xmppConnection = null;
transient protected Presences presences = new Presences();
2014-02-16 15:32:15 +00:00
private String otrFingerprint;
private Roster roster = null;
2014-07-14 09:47:42 +00:00
private List<Bookmark> bookmarks = new CopyOnWriteArrayList<Bookmark>();
2014-07-18 19:57:10 +00:00
public List<Conversation> pendingConferenceJoins = new CopyOnWriteArrayList<Conversation>();
public List<Conversation> pendingConferenceLeaves = new CopyOnWriteArrayList<Conversation>();
public Account() {
2014-01-28 18:21:54 +00:00
this.uuid = "0";
}
2014-01-28 18:21:54 +00:00
public Account(String username, String server, String password) {
this(java.util.UUID.randomUUID().toString(), username, server,
password, 0, null, "");
2014-01-28 18:21:54 +00:00
}
public Account(String uuid, String username, String server,
String password, int options, String rosterVersion, String keys) {
2014-01-28 18:21:54 +00:00
this.uuid = uuid;
this.username = username;
this.server = server;
this.password = password;
this.options = options;
this.rosterVersion = rosterVersion;
2014-02-13 22:40:08 +00:00
try {
this.keys = new JSONObject(keys);
} catch (JSONException e) {
2014-02-13 22:40:08 +00:00
}
2014-01-28 18:21:54 +00:00
}
2014-02-02 16:53:34 +00:00
public boolean isOptionSet(int option) {
return ((options & (1 << option)) != 0);
}
2014-02-04 20:44:16 +00:00
public void setOption(int option, boolean value) {
if (value) {
2014-02-21 18:14:03 +00:00
this.options |= 1 << option;
2014-02-04 20:44:16 +00:00
} else {
2014-02-21 18:14:03 +00:00
this.options &= ~(1 << option);
2014-02-04 20:44:16 +00:00
}
}
2014-01-28 18:21:54 +00:00
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getServer() {
return server;
}
2014-01-28 18:21:54 +00:00
public void setServer(String server) {
this.server = server;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void setStatus(int status) {
this.status = status;
}
public int getStatus() {
2014-02-04 20:44:16 +00:00
if (isOptionSet(OPTION_DISABLED)) {
return STATUS_DISABLED;
} else {
return this.status;
}
}
public boolean errorStatus() {
int s = getStatus();
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);
}
public void setResource(String resource) {
this.resource = resource;
2014-01-28 18:21:54 +00:00
}
public String getResource() {
return this.resource;
}
2014-01-28 18:21:54 +00:00
public String getJid() {
return username.toLowerCase(Locale.getDefault()) + "@"
+ server.toLowerCase(Locale.getDefault());
2014-01-28 18:21:54 +00:00
}
2014-02-13 22:40:08 +00:00
public JSONObject getKeys() {
return keys;
}
2014-03-07 13:24:33 +00:00
public String getSSLFingerprint() {
if (keys.has("ssl_cert")) {
try {
return keys.getString("ssl_cert");
} catch (JSONException e) {
return null;
}
} else {
return null;
}
}
2014-03-07 13:24:33 +00:00
public void setSSLCertFingerprint(String fingerprint) {
this.setKey("ssl_cert", fingerprint);
}
2014-02-27 23:22:56 +00:00
public boolean setKey(String keyName, String keyValue) {
try {
this.keys.put(keyName, keyValue);
return true;
} catch (JSONException e) {
return false;
}
2014-02-13 22:40:08 +00:00
}
2014-01-28 18:21:54 +00:00
@Override
public ContentValues getContentValues() {
2014-01-28 18:21:54 +00:00
ContentValues values = new ContentValues();
values.put(UUID, uuid);
2014-01-28 18:21:54 +00:00
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);
2014-01-28 18:21:54 +00:00
return values;
}
2014-01-28 18:21:54 +00:00
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)),
2014-02-13 22:40:08 +00:00
cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
cursor.getString(cursor.getColumnIndex(KEYS)));
}
2014-02-13 22:40:08 +00:00
public OtrEngine getOtrEngine(Context context) {
if (otrEngine == null) {
otrEngine = new OtrEngine(context, this);
2014-02-13 22:40:08 +00:00
}
return this.otrEngine;
}
public XmppConnection getXmppConnection() {
return this.xmppConnection;
}
public void setXmppConnection(XmppConnection connection) {
this.xmppConnection = connection;
}
public String getFullJid() {
return this.getJid() + "/" + this.resource;
2014-02-13 22:40:08 +00:00
}
2014-02-16 15:32:15 +00:00
public String getOtrFingerprint() {
if (this.otrFingerprint == null) {
try {
DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine
.getPublicKey();
2014-03-14 20:36:38 +00:00
if (pubkey == null) {
return null;
}
StringBuilder builder = new StringBuilder(
new OtrCryptoEngineImpl().getFingerprint(pubkey));
2014-02-16 15:32:15 +00:00
builder.insert(8, " ");
builder.insert(17, " ");
builder.insert(26, " ");
builder.insert(35, " ");
this.otrFingerprint = builder.toString();
} catch (OtrCryptoException e) {
2014-02-16 15:32:15 +00:00
}
}
return this.otrFingerprint;
}
public String getRosterVersion() {
if (this.rosterVersion == null) {
return "";
} else {
return this.rosterVersion;
}
}
public void setRosterVersion(String version) {
this.rosterVersion = version;
}
2014-03-14 20:36:38 +00:00
public String getOtrFingerprint(Context applicationContext) {
this.getOtrEngine(applicationContext);
return this.getOtrFingerprint();
}
public void updatePresence(String resource, int status) {
this.presences.updatePresence(resource, status);
}
public void removePresence(String resource) {
this.presences.removePresence(resource);
}
public void clearPresences() {
this.presences = new Presences();
}
public int countPresences() {
return this.presences.size();
}
2014-05-01 20:33:49 +00:00
public String getPgpSignature() {
if (keys.has("pgp_signature")) {
try {
return keys.getString("pgp_signature");
} catch (JSONException e) {
return null;
}
} else {
return null;
}
}
public Roster getRoster() {
if (this.roster == null) {
this.roster = new Roster(this);
}
return this.roster;
}
2014-07-14 09:47:42 +00:00
public void setBookmarks(List<Bookmark> bookmarks) {
this.bookmarks = bookmarks;
}
2014-07-14 09:47:42 +00:00
public List<Bookmark> getBookmarks() {
return this.bookmarks;
}
2014-07-15 12:32:19 +00:00
public boolean hasBookmarkFor(String conferenceJid) {
for (Bookmark bmark : this.bookmarks) {
2014-07-15 12:32:19 +00:00
if (bmark.getJid().equals(conferenceJid)) {
return true;
}
}
return false;
}
public Bitmap getImage(Context context, int size) {
if (this.avatar != null) {
2014-08-11 21:22:46 +00:00
Bitmap bm = FileBackend.getAvatar(this.avatar, size, context);
if (bm == null) {
return UIHelper.getContactPicture(getJid(), size, context,
false);
} else {
return bm;
}
} else {
return UIHelper.getContactPicture(getJid(), size, context, false);
}
}
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;
}
}
}