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 |= 1 << option;
 87		} else {
 88			this.options &= ~(1 << 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 boolean setKey(String keyName, String keyValue) {
141		try {
142			this.keys.put(keyName, keyValue);
143			return true;
144		} catch (JSONException e) {
145			return false;
146		}
147	}
148
149	@Override
150	public ContentValues getContentValues() {
151		ContentValues values = new ContentValues();
152		values.put(UUID,uuid);
153		values.put(USERNAME, username);
154		values.put(SERVER, server);
155		values.put(PASSWORD, password);
156		values.put(OPTIONS,options);
157		values.put(KEYS,this.keys.toString());
158		values.put(ROSTERVERSION,rosterVersion);
159		return values;
160	}
161	
162	public static Account fromCursor(Cursor cursor) {
163		return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
164				cursor.getString(cursor.getColumnIndex(USERNAME)),
165				cursor.getString(cursor.getColumnIndex(SERVER)),
166				cursor.getString(cursor.getColumnIndex(PASSWORD)),
167				cursor.getInt(cursor.getColumnIndex(OPTIONS)),
168				cursor.getString(cursor.getColumnIndex(ROSTERVERSION)),
169				cursor.getString(cursor.getColumnIndex(KEYS))
170				);
171	}
172
173	
174	public OtrEngine getOtrEngine(Context context) {
175		if (otrEngine==null) {
176			otrEngine = new OtrEngine(context,this);
177		}
178		return this.otrEngine;
179	}
180
181	public XmppConnection getXmppConnection() {
182		return this.xmppConnection;
183	}
184
185	public void setXmppConnection(XmppConnection connection) {
186		this.xmppConnection = connection;
187	}
188
189	public String getFullJid() {
190		return this.getJid()+"/"+this.resource;
191	}
192	
193	public String getOtrFingerprint() {
194		if (this.otrFingerprint == null) {
195			try {
196				DSAPublicKey pubkey = (DSAPublicKey) this.otrEngine.getPublicKey();
197				StringBuilder builder = new StringBuilder(new OtrCryptoEngineImpl().getFingerprint(pubkey));
198				builder.insert(8, " ");
199				builder.insert(17, " ");
200				builder.insert(26, " ");
201				builder.insert(35, " ");
202				this.otrFingerprint = builder.toString();
203			} catch (OtrCryptoException e) {
204				
205			}
206		}
207		return this.otrFingerprint;
208	}
209
210	public String getRosterVersion() {
211		if (this.rosterVersion==null) {
212			return "";
213		} else {
214			return this.rosterVersion;
215		}
216	}
217	
218	public void setRosterVersion(String version) {
219		this.rosterVersion = version;
220	}
221}