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 = 0;
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 Log.d("xmppService","enabling option "+this.options);
64 } else {
65 this.options = (this.options & 0 << option);
66 Log.d("xmppService","disabeling option "+this.options);
67 }
68 }
69
70 public String getUsername() {
71 return username;
72 }
73
74 public void setUsername(String username) {
75 this.username = username;
76 }
77
78 public String getServer() {
79 return server;
80 }
81
82 public void setServer(String server) {
83 this.server = server;
84 }
85
86 public String getPassword() {
87 return password;
88 }
89
90 public void setPassword(String password) {
91 this.password = password;
92 }
93
94 public void setStatus(int status) {
95 this.status = status;
96 }
97
98 public int getStatus() {
99 if (isOptionSet(OPTION_DISABLED)) {
100 return STATUS_DISABLED;
101 } else {
102 return this.status;
103 }
104 }
105
106 public void setResource(String resource) {
107 this.resource = resource;
108 }
109
110 public String getJid() {
111 return username+"@"+server;
112 }
113
114 @Override
115 public ContentValues getContentValues() {
116 ContentValues values = new ContentValues();
117 values.put(UUID,uuid);
118 values.put(USERNAME, username);
119 values.put(SERVER, server);
120 values.put(PASSWORD, password);
121 values.put(OPTIONS,options);
122 return values;
123 }
124
125 public static Account fromCursor(Cursor cursor) {
126 return new Account(cursor.getString(cursor.getColumnIndex(UUID)),
127 cursor.getString(cursor.getColumnIndex(USERNAME)),
128 cursor.getString(cursor.getColumnIndex(SERVER)),
129 cursor.getString(cursor.getColumnIndex(PASSWORD)),
130 cursor.getInt(cursor.getColumnIndex(OPTIONS)),
131 cursor.getString(cursor.getColumnIndex(ROSTERVERSION))
132 );
133 }
134
135}