Account.java

  1package de.gultsch.chat.entities;
  2
  3import android.content.ContentValues;
  4import android.database.Cursor;
  5import android.util.Log;
  6
  7public class Account  extends AbstractEntity{
  8
  9	private static final long serialVersionUID = 6174825093869578035L;
 10	
 11	public static final String TABLENAME = "accounts";
 12	
 13	public static final String USERNAME = "username";
 14	public static final String SERVER = "server";
 15	public static final String PASSWORD = "password";
 16	public static final String OPTIONS = "options";
 17	public static final String ROSTERVERSION = "rosterversion";
 18	
 19	public static final int OPTION_USETLS = 0;
 20	public static final int OPTION_DISABLED = 1;
 21	
 22	public static final int STATUS_DISABLED = -1;
 23	public static final int STATUS_OFFLINE = 0;
 24	public static final int STATUS_ONLINE = 1;
 25	public static final int STATUS_UNAUTHORIZED = 2;
 26	public static final int STATUS_NOINTERNET = 3;
 27	public static final int STATUS_TLS_ERROR = 4;
 28	public static final int STATUS_SERVER_NOT_FOUND = 5;
 29	
 30	protected String username;
 31	protected String server;
 32	protected String password;
 33	protected int options;
 34	protected String rosterVersion;
 35	protected String resource;
 36	protected int status = 0;
 37	
 38	protected boolean online = false;
 39	
 40	public Account() {
 41		this.uuid = "0";
 42	}
 43	
 44	public Account(String username, String server, String password) {
 45		this(java.util.UUID.randomUUID().toString(),username,server,password,0,null);
 46	}
 47	public Account(String uuid, String username, String server,String password, int options, String rosterVersion) {
 48		this.uuid = uuid;
 49		this.username = username;
 50		this.server = server;
 51		this.password = password;
 52		this.options = options;
 53		this.rosterVersion = rosterVersion;
 54	}
 55	
 56	public boolean isOptionSet(int option) {
 57		return ((options & (1 << option)) != 0);
 58	}
 59	
 60	public void setOption(int option, boolean value) {
 61		if (value) {
 62			this.options = (this.options | 1 << option);
 63		} else {
 64			this.options = (this.options ^ 1 << option);
 65		}
 66	}
 67	
 68	public String getUsername() {
 69		return username;
 70	}
 71
 72	public void setUsername(String username) {
 73		this.username = username;
 74	}
 75
 76	public String getServer() {
 77		return server;
 78	}
 79
 80	public void setServer(String server) {
 81		this.server = server;
 82	}
 83
 84	public String getPassword() {
 85		return password;
 86	}
 87
 88	public void setPassword(String password) {
 89		this.password = password;
 90	}
 91	
 92	public void setStatus(int status) {
 93		this.status = status;
 94	}
 95	
 96	public int getStatus() {
 97		if (isOptionSet(OPTION_DISABLED)) {
 98			return STATUS_DISABLED;
 99		} else {
100			return this.status;
101		}
102	}
103	
104	public void setResource(String resource) {
105		this.resource = resource;
106	}
107	
108	public String getJid() {
109		return username+"@"+server;
110	}
111
112	@Override
113	public ContentValues getContentValues() {
114		ContentValues values = new ContentValues();
115		values.put(UUID,uuid);
116		values.put(USERNAME, username);
117		values.put(SERVER, server);
118		values.put(PASSWORD, password);
119		values.put(OPTIONS,options);
120		return values;
121	}
122	
123	public static Account fromCursor(Cursor cursor) {
124		return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
125				cursor.getString(cursor.getColumnIndex(USERNAME)),
126				cursor.getString(cursor.getColumnIndex(SERVER)),
127				cursor.getString(cursor.getColumnIndex(PASSWORD)),
128				cursor.getInt(cursor.getColumnIndex(OPTIONS)),
129				cursor.getString(cursor.getColumnIndex(ROSTERVERSION))
130				);
131	}
132
133}