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