Initial commit
[mustard2:mustard2.git] / src / org / mustard2 / manager / AccountManager.java
1 package org.mustard2.manager;
2
3 import java.net.URL;
4
5 import org.mustard2.android.Account;
6 import org.mustard2.android.MustardApp;
7 import org.mustard2.db.AccountTable;
8 import org.mustard2.provider.Provider;
9 import org.mustard2.util.AndroidUtil;
10 import org.mustard2.util.CryptUtil;
11
12 import android.content.Context;
13 import android.database.Cursor;
14 import android.util.Log;
15
16 public class AccountManager {
17
18         private Context mContext;
19
20         private static final int K_DEFAULT=0;
21         private static final int K_ENABLED=1;
22         private static final int K_ALL=2;
23
24         public AccountManager (Context context) {
25                 mContext = context;
26         }
27
28         public Account getDefaultAccount() {
29                 Account[] accounts = getAccounts(K_DEFAULT,-1);
30                 return (accounts.length == 0) ? null : accounts[0];
31         }
32
33         public Account[] getEnabledAccounts() {
34                 Account[] accounts = getAccounts(K_ENABLED,-1);
35                 return (accounts.length == 0) ? null : accounts;
36         }
37
38         public Account[] getAccounts() {
39                 return getAccounts(K_ALL,-1);
40         }
41
42         public Account getAccount(long rowid) {
43                 Account[] accounts = getAccounts(K_ALL,rowid);
44                 return (accounts.length == 0) ? null : accounts[0];
45         }
46
47         public Account[] getAccounts(int accountType, long account_id) {
48                 Account[] accounts = null;
49
50                 DbManager mDbHelper = new DbManager(mContext);
51                 mDbHelper.open();
52                 Cursor accountCursor = null;
53                 try {
54                         switch (accountType) {
55                         case K_DEFAULT:
56                                 accountCursor = mDbHelper.fetchDefaultAccount();
57                                 break;
58
59                         case K_ENABLED:
60                                 accountCursor = mDbHelper.fetchAllMergedAccounts();
61                                 break;
62
63                         case K_ALL:
64                                 if(account_id>=0)
65                                         accountCursor = mDbHelper.fetchAccount(account_id);
66                                 else
67                                         accountCursor = mDbHelper.fetchAllAccounts();
68                                 break;
69                                 
70                         default:
71                                 Log.e(MustardApp.APPLICATION_NAME, "AccountManager.getAccounts UNKNOWN TYPE: " + accountType);
72                         }
73                 } catch (Exception e) {
74                         if (MustardApp.DEBUG) {
75                                 Log.e(MustardApp.APPLICATION_NAME,e.toString());
76                                 e.printStackTrace();
77                         }
78                         if (accountCursor!=null) try { accountCursor.close(); } catch (Exception ee) {}
79                         mDbHelper.close();
80                         return new Account[] {};
81                 }
82
83                 if (accountCursor == null) {
84                         Log.e(MustardApp.APPLICATION_NAME,"accountCursor is null!");
85                         mDbHelper.close();
86                         return new Account[] {};
87                 }
88
89                 int a = accountCursor.getCount();
90                 if (a == 0) {
91                         Log.v(MustardApp.APPLICATION_NAME,"There is no account");
92                         if (accountCursor!=null) try { accountCursor.close(); } catch (Exception e) {}
93                         mDbHelper.close();
94                         return new Account[] {};
95                 } else {
96                         Log.v(MustardApp.APPLICATION_NAME,"There are " + a + " account(s)");
97                 }
98                 accounts = new Account[a];
99                 try {
100                         int i=0;
101                         while(accountCursor.moveToNext()) {
102                                 int userIndex = accountCursor.getColumnIndexOrThrow(AccountTable.KEY_USER);
103                                 int accountIdIndex = accountCursor.getColumnIndexOrThrow(AccountTable.KEY_USER_ID);
104                                 int passwordIndex = accountCursor.getColumnIndexOrThrow(AccountTable.KEY_PASSWORD);
105                                 int instanceIndex = accountCursor.getColumnIndexOrThrow(AccountTable.KEY_INSTANCE);
106                                 int rowidIndex = accountCursor.getColumnIndexOrThrow(AccountTable.KEY_ROWID);
107                                 int tokenIndex = accountCursor.getColumnIndexOrThrow(AccountTable.KEY_TOKEN);
108                                 int tokenSecretIndex = accountCursor.getColumnIndexOrThrow(AccountTable.KEY_TOKEN_SECRET);
109                                 int isdefaultIndex = accountCursor.getColumnIndexOrThrow(AccountTable.KEY_DEFAULT);
110                                 int isToMergeIndex = accountCursor.getColumnIndexOrThrow(AccountTable.KEY_TO_MERGE);
111                                 int apiTypeIndex = accountCursor.getColumnIndexOrThrow(AccountTable.KEY_APITYPE);
112                                 int userImageIndex = accountCursor.getColumnIndexOrThrow(AccountTable.KEY_IMAGE);
113                                 String user=accountCursor.getString(userIndex);
114                                 String password=accountCursor.getString(passwordIndex);
115                                 if (password != null && !"".equals(password)) {
116                                         // Decrypt it
117                                         String seed = AndroidUtil.getIMSI(mContext);
118                                         try {
119                                                 password = CryptUtil.decrypt(seed, password);
120                                         } catch (Exception e) {
121                                                 Log.e(MustardApp.APPLICATION_NAME,"ERROR CRYPTING PASSWORD: " + e.getMessage());
122                                         }
123                                 }
124                                 String instance=accountCursor.getString(instanceIndex);
125                                 long usernameId=accountCursor.getLong(rowidIndex);
126                                 String accountId=accountCursor.getString(accountIdIndex);
127                                 String token=accountCursor.getString(tokenIndex);
128                                 String tokenSecret=accountCursor.getString(tokenSecretIndex);
129                                 boolean isdefault=accountCursor.getInt(isdefaultIndex) == 1 ? true : false;
130                                 boolean isToMerge=accountCursor.getInt(isToMergeIndex) == 1 ? true : false;
131                                 String apiType = accountCursor.getString(apiTypeIndex);
132                                 String userImage = accountCursor.getString(userImageIndex);
133                                 Provider provider = new Provider();
134                                 provider.url = new URL(instance);
135                                 provider.apiType = apiType;
136                                 Account account = new Account();
137                                 account.setProvider(provider);
138                                 account.setId(usernameId);
139                                 account.setAccountId(accountId);
140                                 account.setNickname(user);
141                                 account.setPassword(password);
142                                 account.setOauthKey(token);
143                                 account.setOauthSecret(tokenSecret);
144                                 //                              account.setInstance(instance);
145                                 //                              account.setApiType(apiType);
146                                 account.setDefault(isdefault);
147                                 account.setToMerge(isToMerge);
148                                 account.setAvatarUrl(userImage);
149
150                                 accounts[i]=account;
151                                 i++;
152                         }
153                 } catch (Exception e) {
154                         if (MustardApp.DEBUG) e.printStackTrace();
155                         return new Account[] {};
156                 } finally {
157                         if(accountCursor != null)
158                                 try {accountCursor.close(); } catch (Exception e){}
159                         mDbHelper.close();
160                 }
161
162                 return accounts;
163
164
165         }
166
167         public long createAccount(Account account) {
168                 long accountId = -1;
169                 DbManager mDbHelper = new DbManager(mContext);
170                 mDbHelper.open();
171                 try {
172                         mDbHelper.resetDefaultAccounts();
173                         String oauthKey = account.getOauthKey();
174                         if (oauthKey != null && !"".equals(oauthKey) ) {
175                                 accountId = mDbHelper.createAccount(
176                                                 account.getId(),
177                                                 account.getNickname(), 
178                                                 account.getOauthKey(), 
179                                                 account.getOauthSecret(), 
180                                                 account.getInstance(),
181                                                 account.getVersion(),
182                                                 account.getTextLimit(),
183                                                 account.getProvider().apiType,
184                                                 account.getAvatarUrl());
185                         } else {
186                                 accountId = mDbHelper.createAccount(
187                                                 account.getId(),
188                                                 account.getNickname(), 
189                                                 account.getPassword(), 
190                                                 account.getURL(),
191                                                 account.getVersion(),
192                                                 account.getTextLimit(),
193                                                 account.getProvider().apiType,
194                                                 account.getAvatarUrl() );
195                         }
196                 } catch (Exception e) {
197                         e.printStackTrace();
198                 } finally {
199                         mDbHelper.close();                      
200                 }
201
202                 return accountId;
203         }
204
205         public Account updateAccount(Account account) {
206                 DbManager mDbHelper = new DbManager(mContext);
207                 mDbHelper.open();
208                 String oauthKey = account.getOauthKey();
209                 if (oauthKey != null && !"".equals(oauthKey) ) {
210                         mDbHelper.updateAccount(account.getNickname(), account.getOauthKey(), account.getOauthSecret(), 
211                                         account.getInstance(), account.isDefault() ? 1 : 0 , 
212                                                         account.isToMerge() ? 1 : 0,
213                                                                         account.getVersion(),account.getProvider().apiType,
214                                                                         account.getAvatarUrl() );                       
215                 } else {
216                         mDbHelper.updateAccount(account.getNickname(), account.getPassword(), 
217                                         account.getInstance(), account.isDefault() ? 1 : 0 , 
218                                                         account.isToMerge() ? 1 : 0,
219                                                                         account.getVersion(),account.getProvider().apiType,
220                                                                         account.getAvatarUrl() );
221                 }
222                 mDbHelper.close();
223                 return account;
224         }
225
226         public synchronized void setDefaultAccount(Account account) {
227                 DbManager mDbHelper = new DbManager(mContext);
228                 mDbHelper.open();
229                 mDbHelper.setDefaultAccount(account.getId());
230                 mDbHelper.close();
231         }
232
233         public synchronized void setToMerge(Account account, boolean toMerge) {
234                 DbManager mDbHelper = new DbManager(mContext);
235                 mDbHelper.open();
236                 mDbHelper.setToMergeAccount(account.getId(),toMerge);
237                 mDbHelper.close();
238         }
239
240         public synchronized void resetDefaultAccount(Account account) {
241                 DbManager mDbHelper = new DbManager(mContext);
242                 mDbHelper.open();
243                 mDbHelper.resetDefaultAccount(account.getId());
244                 mDbHelper.close();
245         }
246
247         public void deleteAccount(Account account) {
248                 DbManager mDbHelper = new DbManager(mContext);
249                 mDbHelper.open();
250                 mDbHelper.deleteAccount(account.getId());
251                 mDbHelper.resetDefaultAccount();
252                 mDbHelper.close();
253         }
254
255 }