conversations-classic/src/eu/siacs/conversations/entities/Account.java

223 lines
5.4 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;
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;
2014-02-28 17:46:01 +00:00
import eu.siacs.conversations.crypto.OtrEngine;
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;
2014-02-13 22:40:08 +00:00
import android.util.JsonReader;
2014-01-28 18:21:54 +00:00
import android.util.Log;
public class Account extends AbstractEntity{
private static final long serialVersionUID = 6174825093869578035L;
2014-01-28 18:21:54 +00:00
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";
2014-02-13 22:40:08 +00:00
public static final String KEYS = "keys";
2014-01-28 18:21:54 +00:00
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;
2014-02-02 16:53:34 +00:00
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_UNAUTHORIZED = 2;
public static final int STATUS_NO_INTERNET = 3;
public static final int STATUS_TLS_ERROR = 4;
public static final int STATUS_SERVER_NOT_FOUND = 5;
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;
protected int status = -1;
2014-02-13 22:40:08 +00:00
protected JSONObject keys = new JSONObject();
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;
2014-02-16 15:32:15 +00:00
private String otrFingerprint;
2014-02-13 22:40:08 +00:00
public Account() {
2014-01-28 18:21:54 +00:00
this.uuid = "0";
}
public Account(String username, String server, String password) {
2014-02-13 22:40:08 +00:00
this(java.util.UUID.randomUUID().toString(),username,server,password,0,null,"");
2014-01-28 18:21:54 +00:00
}
2014-02-13 22:40:08 +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-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 void setResource(String resource) {
this.resource = resource;
2014-01-28 18:21:54 +00:00
}
public String getJid() {
return username+"@"+server;
}
2014-02-13 22:40:08 +00:00
public JSONObject getKeys() {
return keys;
}
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);
values.put(USERNAME, username);
values.put(SERVER, server);
values.put(PASSWORD, password);
2014-02-04 20:44:16 +00:00
values.put(OPTIONS,options);
2014-02-13 22:40:08 +00:00
values.put(KEYS,this.keys.toString());
values.put(ROSTERVERSION,rosterVersion);
2014-01-28 18:21:54 +00:00
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)),
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);
}
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-16 15:32:15 +00:00
public String getOtrFingerprint() {
if (this.otrFingerprint == null) {
try {
DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey();
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) {
return "";
} else {
return this.rosterVersion;
}
}
public void setRosterVersion(String version) {
this.rosterVersion = version;
}
}