root / src / com / rackspace / cloud / android / ListAccountsActivity.java @ fbe4e332
History | View | Annotate | Download (12.7 kB)
1 |
package com.rackspace.cloud.android; |
---|---|
2 |
|
3 |
import java.io.FileInputStream; |
4 |
import java.io.FileNotFoundException; |
5 |
import java.io.FileOutputStream; |
6 |
import java.io.IOException; |
7 |
import java.io.ObjectInputStream; |
8 |
import java.io.ObjectOutputStream; |
9 |
import java.io.StreamCorruptedException; |
10 |
import java.util.ArrayList; |
11 |
import java.util.TreeMap; |
12 |
|
13 |
import android.app.ProgressDialog; |
14 |
import android.content.Context; |
15 |
import android.content.DialogInterface; |
16 |
import android.content.DialogInterface.OnCancelListener; |
17 |
import android.content.Intent; |
18 |
import android.os.AsyncTask; |
19 |
import android.os.Bundle; |
20 |
import android.util.Log; |
21 |
import android.view.ContextMenu; |
22 |
import android.view.ContextMenu.ContextMenuInfo; |
23 |
import android.view.LayoutInflater; |
24 |
import android.view.Menu; |
25 |
import android.view.MenuInflater; |
26 |
import android.view.MenuItem; |
27 |
import android.view.View; |
28 |
import android.view.ViewGroup; |
29 |
import android.view.ViewGroup.LayoutParams; |
30 |
import android.view.WindowManager; |
31 |
import android.widget.AdapterView.AdapterContextMenuInfo; |
32 |
import android.widget.ArrayAdapter; |
33 |
import android.widget.ImageView; |
34 |
import android.widget.ListView; |
35 |
import android.widget.ProgressBar; |
36 |
import android.widget.TextView; |
37 |
|
38 |
import com.rackspace.cloud.android.R; |
39 |
import com.rackspace.cloud.servers.api.client.Account; |
40 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
41 |
import com.rackspace.cloud.servers.api.client.http.Authentication; |
42 |
|
43 |
//
|
44 |
public class ListAccountsActivity extends CloudListActivity{ |
45 |
|
46 |
private final String FILENAME = "accounts.data"; |
47 |
private static final String PAGE_ROOT = "/Root"; |
48 |
|
49 |
private boolean authenticating; |
50 |
private ArrayList<Account> accounts; |
51 |
private Intent tabViewIntent;
|
52 |
private ProgressDialog dialog;
|
53 |
private Context context; |
54 |
//used to track the current asynctask
|
55 |
@SuppressWarnings("rawtypes") |
56 |
private AsyncTask task;
|
57 |
|
58 |
public void onCreate(Bundle savedInstanceState) { |
59 |
super.onCreate(savedInstanceState);
|
60 |
trackPageView(PAGE_ROOT); |
61 |
onRestoreInstanceState(savedInstanceState); |
62 |
registerForContextMenu(getListView()); |
63 |
context = getApplicationContext(); |
64 |
tabViewIntent = new Intent(this, TabViewActivity.class); |
65 |
} |
66 |
|
67 |
@Override
|
68 |
protected void onSaveInstanceState(Bundle outState) { |
69 |
super.onSaveInstanceState(outState);
|
70 |
outState.putBoolean("authenticating", authenticating);
|
71 |
outState.putSerializable("accounts", accounts);
|
72 |
|
73 |
//need to set authenticating back to true because it is set to false
|
74 |
//in hideAccountDialog()
|
75 |
if(authenticating){
|
76 |
hideAccountDialog(); |
77 |
authenticating = true;
|
78 |
} |
79 |
writeAccounts(); |
80 |
} |
81 |
|
82 |
@SuppressWarnings("unchecked") |
83 |
@Override
|
84 |
protected void onRestoreInstanceState(Bundle state) { |
85 |
|
86 |
/*
|
87 |
* need reference to the app so you can access
|
88 |
* isLoggingIn
|
89 |
*/
|
90 |
|
91 |
|
92 |
if (state != null && state.containsKey("authenticating") && state.getBoolean("authenticating")) { |
93 |
showAccountDialog(); |
94 |
} else {
|
95 |
hideAccountDialog(); |
96 |
} |
97 |
if (state != null && state.containsKey("accounts")) { |
98 |
accounts = (ArrayList<Account>)state.getSerializable("accounts"); |
99 |
if (accounts.size() == 0) { |
100 |
displayNoAccountsCell(); |
101 |
} else {
|
102 |
getListView().setDividerHeight(1); // restore divider lines |
103 |
setListAdapter(new AccountAdapter());
|
104 |
} |
105 |
} else {
|
106 |
loadAccounts(); |
107 |
} |
108 |
} |
109 |
|
110 |
@Override
|
111 |
protected void onStart(){ |
112 |
super.onStart();
|
113 |
if(authenticating){
|
114 |
showAccountDialog(); |
115 |
} |
116 |
} |
117 |
|
118 |
@Override
|
119 |
protected void onStop(){ |
120 |
super.onStop();
|
121 |
if(authenticating){
|
122 |
hideAccountDialog(); |
123 |
authenticating = true;
|
124 |
} |
125 |
} |
126 |
|
127 |
private void loadAccounts() { |
128 |
//check and see if there are any in memory
|
129 |
if(accounts == null){ |
130 |
accounts = readAccounts(); |
131 |
} |
132 |
//if nothing was written before accounts will still be null
|
133 |
if(accounts == null){ |
134 |
accounts = new ArrayList<Account>(); |
135 |
} |
136 |
|
137 |
setAccountList(); |
138 |
} |
139 |
|
140 |
private void setAccountList() { |
141 |
if (accounts.size() == 0) { |
142 |
displayNoAccountsCell(); |
143 |
} else {
|
144 |
getListView().setDividerHeight(1); // restore divider lines |
145 |
this.setListAdapter(new AccountAdapter()); |
146 |
} |
147 |
} |
148 |
|
149 |
private void writeAccounts(){ |
150 |
FileOutputStream fos;
|
151 |
ObjectOutputStream out = null; |
152 |
try{
|
153 |
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
|
154 |
out = new ObjectOutputStream(fos); |
155 |
out.writeObject(accounts); |
156 |
out.flush(); |
157 |
out.close(); |
158 |
} catch (FileNotFoundException e) { |
159 |
showAlert("Error", "Could not save accounts."); |
160 |
e.printStackTrace(); |
161 |
} catch (IOException e) { |
162 |
showAlert("Error", "Could not save accounts."); |
163 |
e.printStackTrace(); |
164 |
} |
165 |
} |
166 |
|
167 |
private ArrayList<Account> readAccounts(){ |
168 |
FileInputStream fis;
|
169 |
ObjectInputStream in;
|
170 |
try {
|
171 |
fis = openFileInput(FILENAME); |
172 |
in = new ObjectInputStream(fis); |
173 |
@SuppressWarnings("unchecked") |
174 |
ArrayList<Account> file = (ArrayList<Account>)in.readObject(); |
175 |
in.close(); |
176 |
return file;
|
177 |
} catch (FileNotFoundException e) { |
178 |
//showAlert("Error", "Could not load accounts.");
|
179 |
e.printStackTrace(); |
180 |
return null; |
181 |
} catch (StreamCorruptedException e) { |
182 |
showAlert("Error", "Could not load accounts."); |
183 |
e.printStackTrace(); |
184 |
} catch (IOException e) { |
185 |
showAlert("Error", "Could not load accounts."); |
186 |
e.printStackTrace(); |
187 |
} catch (ClassNotFoundException e) { |
188 |
showAlert("Error", "Could not load accounts."); |
189 |
e.printStackTrace(); |
190 |
} |
191 |
return null; |
192 |
|
193 |
} |
194 |
|
195 |
private void displayNoAccountsCell() { |
196 |
String a[] = new String[1]; |
197 |
a[0] = "No Accounts"; |
198 |
setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.noaccountscell, R.id.no_accounts_label, a)); |
199 |
getListView().setTextFilterEnabled(true);
|
200 |
getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row |
201 |
getListView().setItemsCanFocus(false);
|
202 |
} |
203 |
|
204 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
205 |
if (accounts != null && accounts.size() > 0) { |
206 |
//setActivityIndicatorsVisibility(View.VISIBLE, v);
|
207 |
Account.setAccount(accounts.get(position)); |
208 |
Log.d("info", "the server is " + Account.getAccount().getAuthServerV2()); |
209 |
login(); |
210 |
} |
211 |
} |
212 |
|
213 |
public void login() { |
214 |
//showActivityIndicators();
|
215 |
//setLoginPreferences();
|
216 |
new AuthenticateTask().execute((Void[]) null); |
217 |
} |
218 |
|
219 |
//setup menu for when menu button is pressed
|
220 |
public boolean onCreateOptionsMenu(Menu menu) { |
221 |
super.onCreateOptionsMenu(menu);
|
222 |
MenuInflater inflater = getMenuInflater(); |
223 |
inflater.inflate(R.menu.accounts_list_menu, menu); |
224 |
return true; |
225 |
} |
226 |
|
227 |
@Override
|
228 |
//in options menu, when add account is selected go to add account activity
|
229 |
public boolean onOptionsItemSelected(MenuItem item) { |
230 |
switch (item.getItemId()) {
|
231 |
case R.id.add_account:
|
232 |
Intent intent = new Intent(this, AddAccountActivity.class); |
233 |
startActivityForResult(intent, 78); // arbitrary number; never used again |
234 |
return true; |
235 |
case R.id.login_pithos:
|
236 |
Intent intent2 = new Intent(this, PithosLoginActivity.class); |
237 |
intent2.putExtra("login", "https://pithos.dev.grnet.gr/im/login"); |
238 |
intent2.putExtra("auth", Preferences.PITHOS_DEV_SERVER); |
239 |
startActivityForResult(intent2, 78); // arbitrary number; never used again |
240 |
return true; |
241 |
|
242 |
case R.id.contact_rackspace:
|
243 |
startActivity(new Intent(this, ContactActivity.class)); |
244 |
return true; |
245 |
|
246 |
case R.id.add_password:
|
247 |
startActivity(new Intent(this, CreatePasswordActivity.class)); |
248 |
return true; |
249 |
} |
250 |
return false; |
251 |
} |
252 |
|
253 |
//the context menu for a long press on an account
|
254 |
public void onCreateContextMenu(ContextMenu menu, View v, |
255 |
ContextMenuInfo menuInfo) { |
256 |
super.onCreateContextMenu(menu, v, menuInfo);
|
257 |
MenuInflater inflater = getMenuInflater(); |
258 |
inflater.inflate(R.menu.account_context_menu, menu); |
259 |
} |
260 |
|
261 |
//removes the selected account from account list if remove is clicked
|
262 |
public boolean onContextItemSelected(MenuItem item) { |
263 |
if (accounts.size() == 0) { |
264 |
displayNoAccountsCell(); |
265 |
return true; |
266 |
} else {
|
267 |
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); |
268 |
accounts.remove(info.position); |
269 |
writeAccounts(); |
270 |
loadAccounts(); |
271 |
return true; |
272 |
} |
273 |
} |
274 |
|
275 |
class AccountAdapter extends ArrayAdapter<Account> { |
276 |
|
277 |
AccountAdapter() { |
278 |
super(ListAccountsActivity.this, R.layout.listaccountcell, accounts);
|
279 |
} |
280 |
|
281 |
public View getView(int position, View convertView, ViewGroup parent) { |
282 |
|
283 |
LayoutInflater inflater = getLayoutInflater(); |
284 |
View row = inflater.inflate(R.layout.listaccountcell, parent, false); |
285 |
|
286 |
TextView label = (TextView) row.findViewById(R.id.label); |
287 |
label.setText(accounts.get(position).getUsername()); |
288 |
|
289 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
290 |
sublabel.setText(getAccountServer(accounts.get(position))); |
291 |
|
292 |
ImageView icon = (ImageView) row.findViewById(R.id.account_type_icon); |
293 |
icon.setImageResource(setAccountIcon(accounts.get(position))); |
294 |
|
295 |
return row;
|
296 |
} |
297 |
} |
298 |
|
299 |
public String getAccountServer(Account account){ |
300 |
String authServer = account.getAuthServer();
|
301 |
if(authServer == null){ |
302 |
authServer = account.getAuthServerV2(); |
303 |
} |
304 |
String result;
|
305 |
|
306 |
if(authServer.equals(Preferences.COUNTRY_UK_AUTH_SERVER) || authServer.equals(Preferences.COUNTRY_UK_AUTH_SERVER_V2)){ |
307 |
result = "Rackspace Cloud (UK)";
|
308 |
} |
309 |
else if(authServer.equals(Preferences.COUNTRY_US_AUTH_SERVER) || authServer.equals(Preferences.COUNTRY_US_AUTH_SERVER_V2)){ |
310 |
result = "Rackspace Cloud (US)";
|
311 |
} |
312 |
else if(authServer.equals(Preferences.PITHOS_SERVER)){ |
313 |
result = "Pithos+";
|
314 |
} |
315 |
else if(authServer.equals(Preferences.PITHOS_DEV_SERVER)){ |
316 |
result = "Pithos+ Dev";
|
317 |
} |
318 |
else{
|
319 |
result = "Custom:" +authServer;
|
320 |
//setCustomIcon();
|
321 |
} |
322 |
return result;
|
323 |
} |
324 |
|
325 |
//display rackspace logo for cloud accounts and openstack logo for others
|
326 |
private int setAccountIcon(Account account){ |
327 |
String authServer = account.getAuthServer();
|
328 |
if(authServer == null){ |
329 |
authServer = account.getAuthServerV2(); |
330 |
} |
331 |
if(authServer.equals(Preferences.COUNTRY_UK_AUTH_SERVER) |
332 |
|| authServer.equals(Preferences.COUNTRY_US_AUTH_SERVER)
|
333 |
|| authServer.equals(Preferences.COUNTRY_US_AUTH_SERVER_V2)
|
334 |
|| authServer.equals(Preferences.COUNTRY_UK_AUTH_SERVER_V2)){
|
335 |
return R.drawable.rackspacecloud_icon;
|
336 |
} |
337 |
if(authServer.equals(Preferences.PITHOS_DEV_SERVER) |
338 |
|| authServer.equals(Preferences.PITHOS_SERVER)){
|
339 |
return R.drawable.pithos_icon;
|
340 |
} |
341 |
else{
|
342 |
return R.drawable.openstack_icon;
|
343 |
} |
344 |
} |
345 |
|
346 |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
347 |
super.onActivityResult(requestCode, resultCode, data);
|
348 |
|
349 |
if(requestCode == 187){ |
350 |
hideAccountDialog(); |
351 |
} |
352 |
|
353 |
if (resultCode == RESULT_OK && requestCode == 78) { |
354 |
Account acc = new Account();
|
355 |
Bundle b = data.getBundleExtra("accountInfo");
|
356 |
acc.setPassword(b.getString("apiKey"));
|
357 |
acc.setUsername(b.getString("username"));
|
358 |
acc.setAuthServer(b.getString("server"));
|
359 |
Log.d("info", "the set server was " + b.getString("server")); |
360 |
Log.d("info", "the server is " + acc.getAuthServer()); |
361 |
boolean found = false; |
362 |
for(Account a : accounts){
|
363 |
if(a.getUsername().equals(acc.getUsername())&&a.getAuthServer().equals(acc.getAuthServer())){
|
364 |
a.setPassword(acc.getPassword()); |
365 |
found=true;
|
366 |
} |
367 |
} |
368 |
if(!found)
|
369 |
accounts.add(acc); |
370 |
writeAccounts(); |
371 |
loadAccounts(); |
372 |
} |
373 |
} |
374 |
|
375 |
private void showAccountDialog() { |
376 |
app.setIsLoggingIn(true);
|
377 |
authenticating = true;
|
378 |
if(dialog == null || !dialog.isShowing()){ |
379 |
dialog = new ProgressDialog(this); |
380 |
dialog.setProgressStyle(R.style.NewDialog); |
381 |
dialog.setOnCancelListener(new OnCancelListener() {
|
382 |
|
383 |
@Override
|
384 |
public void onCancel(DialogInterface dialog) { |
385 |
app.setIsLoggingIn(false);
|
386 |
//need to cancel the old task or we may get a double login
|
387 |
task.cancel(true);
|
388 |
hideAccountDialog(); |
389 |
} |
390 |
}); |
391 |
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
392 |
dialog.show(); |
393 |
dialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); |
394 |
} |
395 |
|
396 |
|
397 |
} |
398 |
|
399 |
private void hideAccountDialog() { |
400 |
if(dialog != null){ |
401 |
dialog.dismiss(); |
402 |
} |
403 |
authenticating = false;
|
404 |
} |
405 |
|
406 |
private class AuthenticateTask extends AsyncTask<Void, Void, Boolean> { |
407 |
|
408 |
@Override
|
409 |
protected void onPreExecute(){ |
410 |
Log.d("info", "Starting authenticate"); |
411 |
task = this;
|
412 |
showAccountDialog(); |
413 |
} |
414 |
|
415 |
@Override
|
416 |
protected Boolean doInBackground(Void... arg0) { |
417 |
try {
|
418 |
return new Boolean(Authentication.authenticate(context)); |
419 |
} catch (CloudServersException e) {
|
420 |
e.printStackTrace(); |
421 |
return false; |
422 |
} |
423 |
} |
424 |
|
425 |
@Override
|
426 |
protected void onPostExecute(Boolean result) { |
427 |
if (result.booleanValue()) {
|
428 |
//startActivity(tabViewIntent);
|
429 |
/*if(app.isLoggingIn()){
|
430 |
new LoadImagesTask().execute((Void[]) null);
|
431 |
} else {
|
432 |
hideAccountDialog();
|
433 |
}*/
|
434 |
hideAccountDialog(); |
435 |
if(app.isLoggingIn()){
|
436 |
startActivityForResult(tabViewIntent, 187);
|
437 |
} |
438 |
} else {
|
439 |
hideAccountDialog(); |
440 |
if(app.isLoggingIn()){
|
441 |
showAlert("Login Failure", "Authentication failed. Please check your User Name and API Key."); |
442 |
} |
443 |
} |
444 |
} |
445 |
} |
446 |
|
447 |
|
448 |
|
449 |
} |