Account.java

  1package de.gultsch.chat.entities;
  2
  3import java.security.interfaces.DSAPublicKey;
  4
  5import net.java.otr4j.crypto.OtrCryptoEngineImpl;
  6import net.java.otr4j.crypto.OtrCryptoException;
  7
  8import org.json.JSONException;
  9import org.json.JSONObject;
 10
 11import de.gultsch.chat.crypto.OtrEngine;
 12import de.gultsch.chat.xmpp.XmppConnection;
 13import android.content.ContentValues;
 14import android.content.Context;
 15import android.database.Cursor;
 16import android.util.JsonReader;
 17import android.util.Log;
 18
 19public class Account  extends AbstractEntity{
 20
 21	private static final long serialVersionUID = 6174825093869578035L;
 22	
 23	public static final String TABLENAME = "accounts";
 24	
 25	public static final String USERNAME = "username";
 26	public static final String SERVER = "server";
 27	public static final String PASSWORD = "password";
 28	public static final String OPTIONS = "options";
 29	public static final String ROSTERVERSION = "rosterversion";
 30	public static final String KEYS = "keys";
 31	
 32	public static final int OPTION_USETLS = 0;
 33	public static final int OPTION_DISABLED = 1;
 34	
 35	public static final int STATUS_DISABLED = -1;
 36	public static final int STATUS_OFFLINE = 0;
 37	public static final int STATUS_ONLINE = 1;
 38	public static final int STATUS_UNAUTHORIZED = 2;
 39	public static final int STATUS_NOINTERNET = 3;
 40	public static final int STATUS_TLS_ERROR = 4;
 41	public static final int STATUS_SERVER_NOT_FOUND = 5;
 42	
 43	protected String username;
 44	protected String server;
 45	protected String password;
 46	protected int options = 0;
 47	protected String rosterVersion;
 48	protected String resource;
 49	protected int status = 0;
 50	protected JSONObject keys = new JSONObject();
 51	
 52	protected boolean online = false;
 53	
 54	transient OtrEngine otrEngine = null;
 55	transient XmppConnection xmppConnection = null;
 56
 57	private String otrFingerprint;
 58	
 59	public Account() {
 60		this.uuid = "0";
 61	}
 62	
 63	public Account(String username, String server, String password) {
 64		this(java.util.UUID.randomUUID().toString(),username,server,password,0,null,"");
 65	}
 66	public Account(String uuid, String username, String server,String password, int options, String rosterVersion, String keys) {
 67		this.uuid = uuid;
 68		this.username = username;
 69		this.server = server;
 70		this.password = password;
 71		this.options = options;
 72		this.rosterVersion = rosterVersion;
 73		try {
 74			this.keys = new JSONObject(keys);
 75		} catch (JSONException e) {
 76			
 77		}
 78	}
 79	
 80	public boolean isOptionSet(int option) {
 81		return ((options & (1 << option)) != 0);
 82	}
 83	
 84	public void setOption(int option, boolean value) {
 85		if (value) {
 86			this.options = (this.options | 1 << option);
 87		} else {
 88			this.options = (this.options & 0 << option);
 89		}
 90	}
 91	
 92	public String getUsername() {
 93		return username;
 94	}
 95
 96	public void setUsername(String username) {
 97		this.username = username;
 98	}
 99
100	public String getServer() {
101		return server;
102	}
103
104	public void setServer(String server) {
105		this.server = server;
106	}
107
108	public String getPassword() {
109		return password;
110	}
111
112	public void setPassword(String password) {
113		this.password = password;
114	}
115	
116	public void setStatus(int status) {
117		this.status = status;
118	}
119	
120	public int getStatus() {
121		if (isOptionSet(OPTION_DISABLED)) {
122			return STATUS_DISABLED;
123		} else {
124			return this.status;
125		}
126	}
127	
128	public void setResource(String resource) {
129		this.resource = resource;
130	}
131	
132	public String getJid() {
133		return username+"@"+server;
134	}
135	
136	public JSONObject getKeys() {
137		return keys;
138	}
139	
140	public void setKey(String keyName, String keyValue) throws JSONException {
141		this.keys.put(keyName, keyValue);
142	}
143
144	@Override
145	public ContentValues getContentValues() {
146		ContentValues values = new ContentValues();
147		values.put(UUID,uuid);
148		values.put(USERNAME, username);
149		values.put(SERVER, server);
150		values.put(PASSWORD, password);
151		values.put(OPTIONS,options);
152		values.put(KEYS,this.keys.toString());
153		values.put(ROSTERVERSION,rosterVersion);
154		return values;
155	}
156	
157	public static Account fromCursor(Cursor cursor) {
158		return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
159				cursor.getString(cursor.getColumnIndex(USERNAME)),
160				cursor.getString(cursor.getColumnIndex(SERVER)),
161				cursor.getString(cursor.getColumnIndex(PASSWORD)),
162				cursor.getInt(cursor.getColumnIndex(OPTIONS)),
163				cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
164				cursor.getString(cursor.getColumnIndex(KEYS))
165				);
166	}
167
168	
169	public OtrEngine getOtrEngine(Context context) {
170		if (otrEngine==null) {
171			otrEngine = new OtrEngine(context,this);
172		}
173		return this.otrEngine;
174	}
175
176	public XmppConnection getXmppConnection() {
177		return this.xmppConnection;
178	}
179
180	public void setXmppConnection(XmppConnection connection) {
181		this.xmppConnection = connection;
182	}
183
184	public String getFullJid() {
185		return this.getJid()+"/"+this.resource;
186	}
187	
188	public String getOtrFingerprint() {
189		if (this.otrFingerprint == null) {
190			try {
191				DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey();
192				StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(pubkey));
193				builder.insert(8, " ");
194				builder.insert(17, " ");
195				builder.insert(26, " ");
196				builder.insert(35, " ");
197				this.otrFingerprint = builder.toString();
198			} catch (OtrCryptoException e) {
199				
200			}
201		}
202		return this.otrFingerprint;
203	}
204
205	public String getRosterVersion() {
206		if (this.rosterVersion==null) {
207			return "";
208		} else {
209			return this.rosterVersion;
210		}
211	}
212	
213	public void setRosterVersion(String version) {
214		this.rosterVersion = version;
215	}
216}