Revision 0edf6b39 src/com/rackspacecloud/android/ListAccountsActivity.java
b/src/com/rackspacecloud/android/ListAccountsActivity.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
import android.app.AlertDialog; |
21 | 21 |
import android.app.Dialog; |
22 |
import android.app.ListActivity; |
|
23 | 22 |
import android.app.ProgressDialog; |
24 | 23 |
import android.content.Context; |
25 | 24 |
import android.content.DialogInterface; |
... | ... | |
45 | 44 |
import android.widget.TextView; |
46 | 45 |
import android.widget.Toast; |
47 | 46 |
|
48 |
public class ListAccountsActivity extends ListActivity{ |
|
47 |
public class ListAccountsActivity extends GaListActivity{
|
|
49 | 48 |
|
50 | 49 |
private final int PASSWORD_PROMPT = 123; |
51 | 50 |
private final String FILENAME = "accounts.data"; |
52 |
|
|
51 |
private static final String PAGE_ROOT = "/Root"; |
|
52 |
|
|
53 | 53 |
private boolean authenticating; |
54 | 54 |
private ArrayList<Account> accounts; |
55 | 55 |
private Intent tabViewIntent; |
56 | 56 |
private ProgressDialog dialog; |
57 | 57 |
private Context context; |
58 |
|
|
59 | 58 |
//need to store if the user has successfully logged in |
60 | 59 |
private boolean loggedIn; |
61 | 60 |
|
62 |
|
|
63 | 61 |
public void onCreate(Bundle savedInstanceState) { |
64 |
super.onCreate(savedInstanceState); |
|
65 |
onRestoreInstanceState(savedInstanceState); |
|
66 |
registerForContextMenu(getListView()); |
|
67 |
context = getApplicationContext(); |
|
68 |
tabViewIntent = new Intent(this, TabViewActivity.class); |
|
69 |
verifyPassword(); |
|
70 |
} |
|
62 |
super.onCreate(savedInstanceState); |
|
63 |
trackPageView(PAGE_ROOT); |
|
64 |
onRestoreInstanceState(savedInstanceState); |
|
65 |
registerForContextMenu(getListView()); |
|
66 |
context = getApplicationContext(); |
|
67 |
tabViewIntent = new Intent(this, TabViewActivity.class); |
|
68 |
verifyPassword(); |
|
69 |
} |
|
71 | 70 |
|
72 | 71 |
@Override |
73 | 72 |
protected void onSaveInstanceState(Bundle outState) { |
74 | 73 |
super.onSaveInstanceState(outState); |
75 | 74 |
outState.putBoolean("authenticating", authenticating); |
76 | 75 |
outState.putBoolean("loggedIn", loggedIn); |
77 |
|
|
76 |
|
|
78 | 77 |
//need to set authenticating back to true because it is set to false |
79 | 78 |
//in hideDialog() |
80 | 79 |
if(authenticating){ |
... | ... | |
83 | 82 |
} |
84 | 83 |
writeAccounts(); |
85 | 84 |
} |
86 |
|
|
85 |
|
|
87 | 86 |
@Override |
88 |
protected void onRestoreInstanceState(Bundle state) { |
|
87 |
protected void onRestoreInstanceState(Bundle state) {
|
|
89 | 88 |
if (state != null && state.containsKey("loggedIn")){ |
90 | 89 |
loggedIn = state.getBoolean("loggedIn"); |
91 | 90 |
} |
... | ... | |
94 | 93 |
} |
95 | 94 |
if (state != null && state.containsKey("authenticating") && state.getBoolean("authenticating")) { |
96 | 95 |
Log.d("info", "captin on restore show"); |
97 |
showDialog();
|
|
98 |
} else {
|
|
99 |
hideDialog();
|
|
100 |
}
|
|
96 |
showDialog();
|
|
97 |
} else {
|
|
98 |
hideDialog();
|
|
99 |
}
|
|
101 | 100 |
if (state != null && state.containsKey("accounts")) { |
102 |
accounts = readAccounts();
|
|
103 |
if (accounts.size() == 0) {
|
|
104 |
displayNoAccountsCell();
|
|
105 |
} else {
|
|
106 |
getListView().setDividerHeight(1); // restore divider lines
|
|
107 |
setListAdapter(new AccountAdapter());
|
|
108 |
}
|
|
109 |
} else {
|
|
110 |
loadAccounts();
|
|
111 |
}
|
|
112 |
}
|
|
101 |
accounts = readAccounts();
|
|
102 |
if (accounts.size() == 0) {
|
|
103 |
displayNoAccountsCell();
|
|
104 |
} else {
|
|
105 |
getListView().setDividerHeight(1); // restore divider lines
|
|
106 |
setListAdapter(new AccountAdapter());
|
|
107 |
}
|
|
108 |
} else {
|
|
109 |
loadAccounts();
|
|
110 |
}
|
|
111 |
}
|
|
113 | 112 |
|
114 | 113 |
@Override |
115 | 114 |
protected void onStart(){ |
... | ... | |
118 | 117 |
showDialog(); |
119 | 118 |
} |
120 | 119 |
} |
121 |
|
|
120 |
|
|
122 | 121 |
@Override |
123 | 122 |
protected void onStop(){ |
124 | 123 |
super.onStop(); |
... | ... | |
128 | 127 |
authenticating = true; |
129 | 128 |
} |
130 | 129 |
} |
131 |
|
|
130 |
|
|
132 | 131 |
|
133 | 132 |
/* |
134 | 133 |
* if the application is password protected, |
... | ... | |
142 | 141 |
createCustomDialog(PASSWORD_PROMPT); |
143 | 142 |
} |
144 | 143 |
} |
145 |
|
|
144 |
|
|
146 | 145 |
private boolean rightPassword(String password){ |
147 | 146 |
PasswordManager pwManager = new PasswordManager(getSharedPreferences( |
148 | 147 |
Preferences.SHARED_PREFERENCES_NAME, MODE_PRIVATE)); |
149 | 148 |
return pwManager.verifyEnteredPassword(password); |
150 | 149 |
} |
151 |
|
|
152 |
|
|
150 |
|
|
151 |
|
|
153 | 152 |
/* |
154 | 153 |
* forces the user to enter a correct password |
155 | 154 |
* before they gain access to application data |
... | ... | |
175 | 174 |
loggedIn = true; |
176 | 175 |
} |
177 | 176 |
} |
178 |
|
|
177 |
|
|
179 | 178 |
}); |
180 | 179 |
dialog.show(); |
181 | 180 |
} |
182 | 181 |
} |
183 |
|
|
182 |
|
|
184 | 183 |
private void loadAccounts() { |
185 | 184 |
//check and see if there are any in memory |
186 | 185 |
if(accounts == null){ |
... | ... | |
195 | 194 |
} |
196 | 195 |
|
197 | 196 |
private void setAccountList() { |
198 |
|
|
197 |
|
|
199 | 198 |
if (accounts.size() == 0) { |
200 | 199 |
displayNoAccountsCell(); |
201 | 200 |
} else { |
... | ... | |
247 | 246 |
e.printStackTrace(); |
248 | 247 |
} |
249 | 248 |
return null; |
250 |
|
|
249 |
|
|
251 | 250 |
} |
252 | 251 |
|
253 | 252 |
private void displayNoAccountsCell() { |
254 |
String a[] = new String[1];
|
|
255 |
a[0] = "No Accounts";
|
|
256 |
setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.noaccountscell, R.id.no_accounts_label, a));
|
|
257 |
getListView().setTextFilterEnabled(true);
|
|
258 |
getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
|
|
259 |
getListView().setItemsCanFocus(false);
|
|
260 |
}
|
|
261 |
|
|
253 |
String a[] = new String[1];
|
|
254 |
a[0] = "No Accounts";
|
|
255 |
setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.noaccountscell, R.id.no_accounts_label, a));
|
|
256 |
getListView().setTextFilterEnabled(true);
|
|
257 |
getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
|
|
258 |
getListView().setItemsCanFocus(false);
|
|
259 |
}
|
|
260 |
|
|
262 | 261 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
263 | 262 |
if (accounts != null && accounts.size() > 0) { |
264 | 263 |
//setActivityIndicatorsVisibility(View.VISIBLE, v); |
265 | 264 |
Account.setAccount(accounts.get(position)); |
266 | 265 |
login(); |
267 | 266 |
} |
268 |
}
|
|
269 |
|
|
267 |
}
|
|
268 |
|
|
270 | 269 |
public void login() { |
271 |
//showActivityIndicators();
|
|
272 |
//setLoginPreferences();
|
|
273 |
new AuthenticateTask().execute((Void[]) null);
|
|
274 |
}
|
|
275 |
|
|
270 |
//showActivityIndicators();
|
|
271 |
//setLoginPreferences();
|
|
272 |
new AuthenticateTask().execute((Void[]) null);
|
|
273 |
}
|
|
274 |
|
|
276 | 275 |
//setup menu for when menu button is pressed |
277 | 276 |
public boolean onCreateOptionsMenu(Menu menu) { |
278 | 277 |
super.onCreateOptionsMenu(menu); |
... | ... | |
280 | 279 |
inflater.inflate(R.menu.accounts_list_menu, menu); |
281 | 280 |
return true; |
282 | 281 |
} |
283 |
|
|
284 |
@Override
|
|
285 |
//in options menu, when add account is selected go to add account activity
|
|
286 |
public boolean onOptionsItemSelected(MenuItem item) {
|
|
287 |
switch (item.getItemId()) {
|
|
288 |
case R.id.add_account:
|
|
289 |
startActivityForResult(new Intent(this, AddAccountActivity.class), 78); // arbitrary number; never used again
|
|
290 |
return true;
|
|
291 |
|
|
292 |
case R.id.contact_rackspace:
|
|
293 |
startActivity(new Intent(this, ContactActivity.class));
|
|
294 |
return true;
|
|
295 |
|
|
296 |
case R.id.add_password:
|
|
297 |
startActivity(new Intent(this, CreatePasswordActivity.class));
|
|
298 |
return true;
|
|
299 |
}
|
|
300 |
return false;
|
|
301 |
}
|
|
302 |
|
|
303 |
//the context menu for a long press on an account
|
|
282 |
|
|
283 |
@Override
|
|
284 |
//in options menu, when add account is selected go to add account activity
|
|
285 |
public boolean onOptionsItemSelected(MenuItem item) {
|
|
286 |
switch (item.getItemId()) {
|
|
287 |
case R.id.add_account:
|
|
288 |
startActivityForResult(new Intent(this, AddAccountActivity.class), 78); // arbitrary number; never used again
|
|
289 |
return true;
|
|
290 |
|
|
291 |
case R.id.contact_rackspace:
|
|
292 |
startActivity(new Intent(this, ContactActivity.class));
|
|
293 |
return true;
|
|
294 |
|
|
295 |
case R.id.add_password:
|
|
296 |
startActivity(new Intent(this, CreatePasswordActivity.class));
|
|
297 |
return true;
|
|
298 |
}
|
|
299 |
return false;
|
|
300 |
}
|
|
301 |
|
|
302 |
//the context menu for a long press on an account
|
|
304 | 303 |
public void onCreateContextMenu(ContextMenu menu, View v, |
305 | 304 |
ContextMenuInfo menuInfo) { |
306 | 305 |
super.onCreateContextMenu(menu, v, menuInfo); |
... | ... | |
322 | 321 |
AccountAdapter() { |
323 | 322 |
super(ListAccountsActivity.this, R.layout.listaccountcell, accounts); |
324 | 323 |
} |
325 |
|
|
324 |
|
|
326 | 325 |
public View getView(int position, View convertView, ViewGroup parent) { |
327 |
|
|
326 |
|
|
328 | 327 |
LayoutInflater inflater = getLayoutInflater(); |
329 | 328 |
View row = inflater.inflate(R.layout.listaccountcell, parent, false); |
330 | 329 |
|
331 | 330 |
TextView label = (TextView) row.findViewById(R.id.label); |
332 | 331 |
label.setText(accounts.get(position).getUsername()); |
333 |
|
|
332 |
|
|
334 | 333 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
335 | 334 |
sublabel.setText(getAccountServer(accounts.get(position))); |
336 |
|
|
335 |
|
|
337 | 336 |
ImageView icon = (ImageView) row.findViewById(R.id.account_type_icon); |
338 | 337 |
icon.setImageResource(setAccountIcon(accounts.get(position))); |
339 |
|
|
338 |
|
|
340 | 339 |
return row; |
341 | 340 |
} |
342 | 341 |
} |
343 |
|
|
342 |
|
|
344 | 343 |
public String getAccountServer(Account account){ |
345 | 344 |
String authServer = account.getAuthServer(); |
346 | 345 |
String result; |
... | ... | |
356 | 355 |
} |
357 | 356 |
return result; |
358 | 357 |
} |
359 |
|
|
358 |
|
|
360 | 359 |
//display rackspace logo for cloud accounts and openstack logo for others |
361 | 360 |
private int setAccountIcon(Account account){ |
362 | 361 |
if(account.getAuthServer().equals(Preferences.COUNTRY_UK_AUTH_SERVER) |
... | ... | |
370 | 369 |
|
371 | 370 |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
372 | 371 |
super.onActivityResult(requestCode, resultCode, data); |
373 |
|
|
372 |
|
|
374 | 373 |
if(requestCode == 187){ |
375 | 374 |
hideDialog(); |
376 | 375 |
} |
377 |
|
|
376 |
|
|
378 | 377 |
if (resultCode == RESULT_OK && requestCode == 78) { |
379 | 378 |
Account acc = new Account(); |
380 | 379 |
Bundle b = data.getBundleExtra("accountInfo"); |
... | ... | |
386 | 385 |
loadAccounts(); |
387 | 386 |
} |
388 | 387 |
} |
389 |
/* |
|
388 |
/*
|
|
390 | 389 |
private void setActivityIndicatorsVisibility(int visibility) { |
391 | 390 |
//FINISH THIS TO LET USER KNOW PROGRAM IS STILL WORKING |
392 |
|
|
391 |
|
|
393 | 392 |
//ProgressBar pb = new ProgressBar(); |
394 | 393 |
//TextView tv = (TextView) findViewById(R.id.login_authenticating_label); |
395 | 394 |
//pb.setVisibility(visibility); |
396 | 395 |
//tv.setVisibility(visibility); |
397 | 396 |
} |
398 |
|
|
397 |
|
|
399 | 398 |
private void setActivityIndicatorsVisibility(int visibility, View v) { |
400 | 399 |
//FINISH THIS TO LET USER KNOW PROGRAM IS STILL WORKING |
401 |
|
|
400 |
|
|
402 | 401 |
//ProgressBar pb = new ProgressBar(); |
403 | 402 |
//TextView tv = (TextView) findViewById(R.id.login_authenticating_label); |
404 | 403 |
//pb.setVisibility(visibility); |
405 | 404 |
//tv.setVisibility(visibility); |
406 | 405 |
} |
407 |
*/ |
|
408 |
|
|
406 |
*/
|
|
407 |
|
|
409 | 408 |
private void showDialog() { |
410 | 409 |
authenticating = true; |
411 | 410 |
if(dialog == null || !dialog.isShowing()){ |
412 | 411 |
dialog = ProgressDialog.show(ListAccountsActivity.this, "", "Authenticating...", true); |
413 | 412 |
} |
414 |
}
|
|
415 |
|
|
416 |
private void hideDialog() {
|
|
417 |
if(dialog != null){
|
|
418 |
dialog.dismiss();
|
|
419 |
}
|
|
420 |
authenticating = false;
|
|
421 |
}
|
|
413 |
}
|
|
414 |
|
|
415 |
private void hideDialog() {
|
|
416 |
if(dialog != null){
|
|
417 |
dialog.dismiss();
|
|
418 |
}
|
|
419 |
authenticating = false;
|
|
420 |
}
|
|
422 | 421 |
|
423 | 422 |
private class AuthenticateTask extends AsyncTask<Void, Void, Boolean> { |
424 |
|
|
423 |
|
|
425 | 424 |
@Override |
426 | 425 |
protected void onPreExecute(){ |
427 | 426 |
Log.d("info", "auth show called"); |
428 | 427 |
showDialog(); |
429 | 428 |
} |
430 |
|
|
429 |
|
|
431 | 430 |
@Override |
432 | 431 |
protected Boolean doInBackground(Void... arg0) { |
433 | 432 |
return new Boolean(Authentication.authenticate(context)); |
434 | 433 |
//return true; |
435 | 434 |
} |
436 |
|
|
435 |
|
|
437 | 436 |
@Override |
438 | 437 |
protected void onPostExecute(Boolean result) { |
439 | 438 |
if (result.booleanValue()) { |
440 | 439 |
//startActivity(tabViewIntent); |
441 |
new LoadImagesTask().execute((Void[]) null);
|
|
440 |
new LoadImagesTask().execute((Void[]) null);
|
|
442 | 441 |
} else { |
443 | 442 |
hideDialog(); |
444 | 443 |
showAlert("Login Failure", "Authentication failed. Please check your User Name and API Key."); |
445 | 444 |
} |
446 | 445 |
} |
447 |
} |
|
446 |
} |
|
447 |
|
|
448 |
private class LoadFlavorsTask extends AsyncTask<Void, Void, ArrayList<Flavor>> { |
|
448 | 449 |
|
449 |
private class LoadFlavorsTask extends AsyncTask<Void, Void, ArrayList<Flavor>> { |
|
450 |
|
|
451 | 450 |
@Override |
452 | 451 |
protected ArrayList<Flavor> doInBackground(Void... arg0) { |
453 | 452 |
return (new FlavorManager()).createList(true, context); |
454 | 453 |
} |
455 |
|
|
454 |
|
|
456 | 455 |
@Override |
457 | 456 |
protected void onPostExecute(ArrayList<Flavor> result) { |
458 | 457 |
if (result != null && result.size() > 0) { |
... | ... | |
469 | 468 |
showAlert("Login Failure", "There was a problem loading server flavors. Please try again."); |
470 | 469 |
} |
471 | 470 |
} |
472 |
} |
|
471 |
} |
|
472 |
|
|
473 |
private class LoadImagesTask extends AsyncTask<Void, Void, ArrayList<Image>> { |
|
473 | 474 |
|
474 |
private class LoadImagesTask extends AsyncTask<Void, Void, ArrayList<Image>> { |
|
475 |
|
|
476 | 475 |
@Override |
477 | 476 |
protected ArrayList<Image> doInBackground(Void... arg0) { |
478 | 477 |
return (new ImageManager()).createList(true, context); |
479 | 478 |
} |
480 |
|
|
479 |
|
|
481 | 480 |
@Override |
482 | 481 |
protected void onPostExecute(ArrayList<Image> result) { |
483 | 482 |
if (result != null && result.size() > 0) { |
... | ... | |
494 | 493 |
showAlert("Login Failure", "There was a problem loading server images. Please try again."); |
495 | 494 |
} |
496 | 495 |
} |
497 |
}
|
|
498 |
|
|
499 |
private void showAlert(String title, String message) {
|
|
496 |
}
|
|
497 |
|
|
498 |
private void showAlert(String title, String message) {
|
|
500 | 499 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
501 | 500 |
alert.setTitle(title); |
502 | 501 |
alert.setMessage(message); |
503 | 502 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
504 |
public void onClick(DialogInterface dialog, int which) {
|
|
505 |
return;
|
|
506 |
} });
|
|
503 |
public void onClick(DialogInterface dialog, int which) {
|
|
504 |
return;
|
|
505 |
} });
|
|
507 | 506 |
alert.show(); |
508 |
}
|
|
509 |
|
|
510 |
private void showToast(String message) {
|
|
507 |
}
|
|
508 |
|
|
509 |
private void showToast(String message) {
|
|
511 | 510 |
Context context = getApplicationContext(); |
512 | 511 |
int duration = Toast.LENGTH_SHORT; |
513 | 512 |
Toast toast = Toast.makeText(context, message, duration); |
514 | 513 |
toast.show(); |
515 |
}
|
|
516 |
|
|
517 |
|
|
518 |
|
|
519 |
|
|
514 |
}
|
|
515 |
|
|
516 |
|
|
517 |
|
|
518 |
|
|
520 | 519 |
} |
Also available in: Unified diff