Revision 54986b23 src/com/rackspacecloud/android/ListAccountsActivity.java

b/src/com/rackspacecloud/android/ListAccountsActivity.java
27 27
import android.app.ProgressDialog;
28 28
import android.content.Context;
29 29
import android.content.DialogInterface;
30
import android.content.DialogInterface.OnCancelListener;
30 31
import android.content.Intent;
31 32
import android.os.AsyncTask;
32 33
import android.os.Bundle;
......
53 54

  
54 55
	private final int PASSWORD_PROMPT = 123;
55 56
	private final String FILENAME = "accounts.data";
56
	
57

  
57 58
	private boolean authenticating;
58 59
	private ArrayList<Account> accounts;
59 60
	private Intent tabViewIntent;
60 61
	private ProgressDialog dialog;
61 62
	private Context context;
62
	
63
	private AndroidCloudApplication app;
64

  
63 65
	//need to store if the user has successfully logged in
64 66
	private boolean loggedIn;
65 67

  
66 68

  
67 69
	public void onCreate(Bundle savedInstanceState) {
68
        super.onCreate(savedInstanceState);
69
        onRestoreInstanceState(savedInstanceState);
70
        registerForContextMenu(getListView());
71
        context = getApplicationContext();
72
        tabViewIntent = new Intent(this, TabViewActivity.class);
73
        verifyPassword();
74
    }
70
		super.onCreate(savedInstanceState);
71
		onRestoreInstanceState(savedInstanceState);
72
		registerForContextMenu(getListView());
73
		context = getApplicationContext();
74
		tabViewIntent = new Intent(this, TabViewActivity.class);
75
		verifyPassword();
76
	}
75 77

  
76 78
	@Override
77 79
	protected void onSaveInstanceState(Bundle outState) {
78 80
		super.onSaveInstanceState(outState);
79 81
		outState.putBoolean("authenticating", authenticating);
80 82
		outState.putBoolean("loggedIn", loggedIn);
81
		
83
		outState.putSerializable("accounts", accounts);
84

  
82 85
		//need to set authenticating back to true because it is set to false
83 86
		//in hideDialog()
84 87
		if(authenticating){
......
87 90
		}
88 91
		writeAccounts();
89 92
	}
90
	
93

  
94
	@SuppressWarnings("unchecked")
91 95
	@Override
92 96
	protected void onRestoreInstanceState(Bundle state) {
97

  
98
		/*
99
		 * need reference to the app so you can access
100
		 * isLoggingIn
101
		 */
102
		app = (AndroidCloudApplication)this.getApplication();
103
		
93 104
		if (state != null && state.containsKey("loggedIn")){
94 105
			loggedIn = state.getBoolean("loggedIn");
95 106
		}
96 107
		else{
97 108
			loggedIn = false;
98 109
		}
110

  
99 111
		if (state != null && state.containsKey("authenticating") && state.getBoolean("authenticating")) {
100
    		showDialog();
101
    	} else {
102
    		hideDialog();
103
    	}
112
			showDialog();
113
		} else {
114
			hideDialog();
115
		}
104 116
		if (state != null && state.containsKey("accounts")) {
105
    		accounts = readAccounts();
106
    		if (accounts.size() == 0) {
107
    			displayNoAccountsCell();
108
    		} else {
109
    			getListView().setDividerHeight(1); // restore divider lines 
110
    			setListAdapter(new AccountAdapter());
111
    		}
112
    	} else {
113
            loadAccounts();        
114
    	} 	
115
    }
117
			//accounts = readAccounts();
118
			accounts = (ArrayList<Account>)state.getSerializable("accounts");
119
			if (accounts.size() == 0) {
120
				displayNoAccountsCell();
121
			} else {
122
				getListView().setDividerHeight(1); // restore divider lines 
123
				setListAdapter(new AccountAdapter());
124
			}
125
		} else {
126
			loadAccounts();        
127
		} 	
128
	}
116 129

  
117 130
	@Override
118 131
	protected void onStart(){
......
121 134
			showDialog();
122 135
		}
123 136
	}
124
	
137

  
125 138
	@Override
126 139
	protected void onStop(){
127 140
		super.onStop();
......
130 143
			authenticating = true;
131 144
		}
132 145
	}
133
	
146

  
134 147

  
135 148
	/*
136 149
	 * if the application is password protected,
......
144 157
			createCustomDialog(PASSWORD_PROMPT);
145 158
		}
146 159
	}
147
	
160

  
148 161
	private boolean rightPassword(String password){
149 162
		PasswordManager pwManager = new PasswordManager(getSharedPreferences(
150 163
				Preferences.SHARED_PREFERENCES_NAME, MODE_PRIVATE));
151 164
		return pwManager.verifyEnteredPassword(password);
152 165
	}
153
	
154
	
166

  
167

  
155 168
	/*
156 169
	 * forces the user to enter a correct password
157 170
	 * before they gain access to application data
......
177 190
						loggedIn = true;
178 191
					}
179 192
				}
180
				
193

  
181 194
			});
182 195
			dialog.show();
183 196
		}
184 197
	}
185
	
198

  
186 199
	private void loadAccounts() {
187 200
		//check and see if there are any in memory
188 201
		if(accounts == null){
......
197 210
	}
198 211

  
199 212
	private void setAccountList() {
200
	
213

  
201 214
		if (accounts.size() == 0) {
202 215
			displayNoAccountsCell();
203 216
		} else {
......
249 262
			e.printStackTrace();
250 263
		}
251 264
		return null;
252
		
265

  
253 266
	}
254 267

  
255 268
	private void displayNoAccountsCell() {
256
    	String a[] = new String[1];
257
    	a[0] = "No Accounts";
258
        setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.noaccountscell, R.id.no_accounts_label, a));
259
        getListView().setTextFilterEnabled(true);
260
        getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
261
        getListView().setItemsCanFocus(false);
262
    }
263
	
269
		String a[] = new String[1];
270
		a[0] = "No Accounts";
271
		setListAdapter(new ArrayAdapter<String>(getApplicationContext(), R.layout.noaccountscell, R.id.no_accounts_label, a));
272
		getListView().setTextFilterEnabled(true);
273
		getListView().setDividerHeight(0); // hide the dividers so it won't look like a list row
274
		getListView().setItemsCanFocus(false);
275
	}
276

  
264 277
	protected void onListItemClick(ListView l, View v, int position, long id) {
265 278
		if (accounts != null && accounts.size() > 0) {
266 279
			//setActivityIndicatorsVisibility(View.VISIBLE, v);
267 280
			Account.setAccount(accounts.get(position));
268 281
			login();
269 282
		}		
270
    }
271
	
283
	}
284

  
272 285
	public void login() {
273
        //showActivityIndicators();
274
        //setLoginPreferences();
275
        new AuthenticateTask().execute((Void[]) null);
276
    }
277
	
286
		//showActivityIndicators();
287
		//setLoginPreferences();
288
		new AuthenticateTask().execute((Void[]) null);
289
	}
290

  
278 291
	//setup menu for when menu button is pressed
279 292
	public boolean onCreateOptionsMenu(Menu menu) {
280 293
		super.onCreateOptionsMenu(menu);
......
282 295
		inflater.inflate(R.menu.accounts_list_menu, menu);
283 296
		return true;
284 297
	} 
285
    
286
    @Override 
287
    //in options menu, when add account is selected go to add account activity
288
    public boolean onOptionsItemSelected(MenuItem item) {
289
    	switch (item.getItemId()) {
290
    	case R.id.add_account:
291
    		startActivityForResult(new Intent(this, AddAccountActivity.class), 78); // arbitrary number; never used again
292
    		return true;
293

  
294
    	case R.id.contact_rackspace:
295
    		startActivity(new Intent(this, ContactActivity.class));
296
    		return true;
297
    		
298
    	case R.id.add_password:
299
    		startActivity(new Intent(this, CreatePasswordActivity.class));
300
    		return true;
301
    	}	
302
    	return false;
303
    } 
304

  
305
    //the context menu for a long press on an account
298

  
299
	@Override 
300
	//in options menu, when add account is selected go to add account activity
301
	public boolean onOptionsItemSelected(MenuItem item) {
302
		switch (item.getItemId()) {
303
		case R.id.add_account:
304
			startActivityForResult(new Intent(this, AddAccountActivity.class), 78); // arbitrary number; never used again
305
			return true;
306

  
307
		case R.id.contact_rackspace:
308
			startActivity(new Intent(this, ContactActivity.class));
309
			return true;
310

  
311
		case R.id.add_password:
312
			startActivity(new Intent(this, CreatePasswordActivity.class));
313
			return true;
314
		}	
315
		return false;
316
	} 
317

  
318
	//the context menu for a long press on an account
306 319
	public void onCreateContextMenu(ContextMenu menu, View v,
307 320
			ContextMenuInfo menuInfo) {
308 321
		super.onCreateContextMenu(menu, v, menuInfo);
......
324 337
		AccountAdapter() {
325 338
			super(ListAccountsActivity.this, R.layout.listaccountcell, accounts);
326 339
		}
327
		
340

  
328 341
		public View getView(int position, View convertView, ViewGroup parent) {
329
			
342

  
330 343
			LayoutInflater inflater = getLayoutInflater();
331 344
			View row = inflater.inflate(R.layout.listaccountcell, parent, false);
332 345

  
333 346
			TextView label = (TextView) row.findViewById(R.id.label);
334 347
			label.setText(accounts.get(position).getUsername());
335
			
348

  
336 349
			TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
337 350
			sublabel.setText(getAccountServer(accounts.get(position)));
338
			
351

  
339 352
			ImageView icon = (ImageView) row.findViewById(R.id.account_type_icon);
340 353
			icon.setImageResource(setAccountIcon(accounts.get(position)));
341
			
354

  
342 355
			return row;
343 356
		}
344 357
	}
345
	
358

  
346 359
	public String getAccountServer(Account account){
347 360
		String authServer = account.getAuthServer();
348 361
		String result;
......
358 371
		}
359 372
		return result;
360 373
	}
361
	
374

  
362 375
	//display rackspace logo for cloud accounts and openstack logo for others
363 376
	private int setAccountIcon(Account account){
364 377
		if(account.getAuthServer().equals(Preferences.COUNTRY_UK_AUTH_SERVER) 
......
372 385

  
373 386
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
374 387
		super.onActivityResult(requestCode, resultCode, data);
375
		
388

  
376 389
		if(requestCode == 187){
377 390
			hideDialog(); 
378 391
		}
379
		
392

  
380 393
		if (resultCode == RESULT_OK && requestCode == 78) {	  
381 394
			Account acc = new Account();
382 395
			Bundle b = data.getBundleExtra("accountInfo");
......
388 401
			loadAccounts();
389 402
		}
390 403
	}	
391
/*
404
	/*
392 405
	private void setActivityIndicatorsVisibility(int visibility) {
393 406
		//FINISH THIS TO LET USER KNOW PROGRAM IS STILL WORKING
394
		
407

  
395 408
        //ProgressBar pb = new ProgressBar();
396 409
    	//TextView tv = (TextView) findViewById(R.id.login_authenticating_label);
397 410
        //pb.setVisibility(visibility);
398 411
        //tv.setVisibility(visibility);
399 412
    }
400
	
413

  
401 414
	private void setActivityIndicatorsVisibility(int visibility, View v) {
402 415
		//FINISH THIS TO LET USER KNOW PROGRAM IS STILL WORKING
403
		
416

  
404 417
        //ProgressBar pb = new ProgressBar();
405 418
    	//TextView tv = (TextView) findViewById(R.id.login_authenticating_label);
406 419
        //pb.setVisibility(visibility);
407 420
        //tv.setVisibility(visibility);
408 421
    }
409
*/
410
	
422
	 */
423

  
411 424
	private void showDialog() {
425
		app.setIsLoggingIn(true);
412 426
		authenticating = true;
413 427
		if(dialog == null || !dialog.isShowing()){
414 428
			dialog = ProgressDialog.show(ListAccountsActivity.this, "", "Authenticating...", true);
429
			dialog.setCancelable(true);
430
			dialog.setOnCancelListener(new OnCancelListener() {
431

  
432
				@Override
433
				public void onCancel(DialogInterface dialog) {
434
					app.setIsLoggingIn(false);
435
					hideDialog();
436
				}
437
			});
415 438
		}
416
    }
417
    
418
    private void hideDialog() {
419
    	if(dialog != null){
420
    		dialog.dismiss();
421
    	}
422
    	authenticating = false;
423
    }
439
	}
440

  
441
	private void hideDialog() {
442
		if(dialog != null){
443
			dialog.dismiss();
444
		}
445
		authenticating = false;
446
	}
424 447

  
425 448
	private class AuthenticateTask extends AsyncTask<Void, Void, Boolean> {
426
    	
449

  
427 450
		@Override
428 451
		protected void onPreExecute(){
429 452
			Log.d("info", "AuthenticateTask Started");
430 453
			showDialog();
431 454
		}
432
		
455

  
433 456
		@Override
434 457
		protected Boolean doInBackground(Void... arg0) {
435
			long startTime = System.currentTimeMillis();
436
			boolean b =  new Boolean(Authentication.authenticate(context));
437
			long endTime = System.currentTimeMillis();
438
			Log.d("info", "it took " + (endTime - startTime) + " millis");
439
			return b;
440
			//return true;
441
		}
442
    	
458
			return new Boolean(Authentication.authenticate(context));
459
		}
460

  
443 461
		@Override
444 462
		protected void onPostExecute(Boolean result) {
445 463
			if (result.booleanValue()) {
446 464
				//startActivity(tabViewIntent);
447
	        	new LoadImagesTask().execute((Void[]) null);
465
				if(app.isLogginIn()){
466
					new LoadImagesTask().execute((Void[]) null);
467
				} else {
468
					hideDialog();
469
				}
448 470
			} else {
449 471
				hideDialog();
450 472
				showAlert("Login Failure", "Authentication failed.  Please check your User Name and API Key.");
451 473
			}
452 474
		}
453
    }
475
	}
476

  
477
	private class LoadImagesTask extends AsyncTask<Void, Void, ArrayList<Image>> {
454 478

  
455
    private class LoadImagesTask extends AsyncTask<Void, Void, ArrayList<Image>> {
456
    	
457 479
		@Override
458 480
		protected ArrayList<Image> doInBackground(Void... arg0) {
459 481
			Log.d("info", "LoadImagesTask Started");
460 482
			return (new ImageManager()).createList(true, context);
461 483
		}
462
    	
484

  
463 485
		@Override
464 486
		protected void onPostExecute(ArrayList<Image> result) {
465 487
			if (result != null && result.size() > 0) {
......
469 491
					imageMap.put(image.getId(), image);
470 492
				}
471 493
				Image.setImages(imageMap);
472
				new LoadProtocolsTask().execute((Void[]) null); 
494
				if(app.isLogginIn()){
495
					new LoadProtocolsTask().execute((Void[]) null); 
496
				} else {
497
					hideDialog();
498
				}
473 499
			} else {
474 500
				hideDialog();
475 501
				showAlert("Login Failure", "There was a problem loading server images.  Please try again.");
476 502
			}
477 503
		}
478
    }
479
    
480
    private class LoadProtocolsTask extends AsyncTask<Void, Void, ArrayList<Protocol>> {
504
	}
505

  
506
	private class LoadProtocolsTask extends AsyncTask<Void, Void, ArrayList<Protocol>> {
481 507

  
482 508
		@Override
483 509
		protected ArrayList<Protocol> doInBackground(Void... arg0) {
......
489 515
		protected void onPostExecute(ArrayList<Protocol> result) {
490 516
			if (result != null && result.size() > 0) {
491 517
				Protocol.setProtocols(result);
492
				new LoadAlgorithmsTask().execute((Void[]) null);
518
				if(app.isLogginIn()){
519
					new LoadAlgorithmsTask().execute((Void[]) null);
520
				} else {
521
					hideDialog();
522
				}
493 523
			} else {
494 524
				hideDialog();
495 525
				showAlert("Login Failure", "There was a problem loading load balancer protocols.  Please try again.");
496 526
			}
497 527
		}
498 528
	}
499
    
500
    private class LoadAlgorithmsTask extends AsyncTask<Void, Void, ArrayList<Algorithm>> {
529

  
530
	private class LoadAlgorithmsTask extends AsyncTask<Void, Void, ArrayList<Algorithm>> {
501 531

  
502 532
		@Override
503 533
		protected ArrayList<Algorithm> doInBackground(Void... arg0) {
......
509 539
		protected void onPostExecute(ArrayList<Algorithm> result) {
510 540
			if (result != null && result.size() > 0) {
511 541
				Algorithm.setAlgorithms(result);
512
				new LoadFlavorsTask().execute((Void[]) null);
542
				if(app.isLogginIn()){
543
					new LoadFlavorsTask().execute((Void[]) null);
544
				} else {
545
					hideDialog();
546
				}
513 547
			} else {
514 548
				hideDialog();
515 549
				showAlert("Login Failure", "There was a problem loading load balancer algorithms.  Please try again.");
516 550
			}
517 551
		}
518 552
	}
519
    
520
    private class LoadFlavorsTask extends AsyncTask<Void, Void, ArrayList<Flavor>> {
521
    	
553

  
554
	private class LoadFlavorsTask extends AsyncTask<Void, Void, ArrayList<Flavor>> {
555

  
522 556
		@Override
523 557
		protected ArrayList<Flavor> doInBackground(Void... arg0) {
524 558
			Log.d("info", "LoadFlavorsTask Started");
525 559
			return (new FlavorManager()).createList(true, context);
526 560
		}
527
    	
561

  
528 562
		@Override
529 563
		protected void onPostExecute(ArrayList<Flavor> result) {
530 564
			if (result != null && result.size() > 0) {
......
536 570
				Flavor.setFlavors(flavorMap);
537 571
				hideDialog();
538 572
				Log.d("info", "Starting TabViewIntent");
539
				startActivityForResult(tabViewIntent, 187);
573
				if(app.isLogginIn()){
574
					startActivityForResult(tabViewIntent, 187);
575
				} else {
576
					hideDialog();
577
				}
540 578
			} else {
541 579
				hideDialog();
542 580
				showAlert("Login Failure", "There was a problem loading server flavors.  Please try again.");
543 581
			}
544 582
		}
545
    }
546
    
547
    private void showAlert(String title, String message) {
583
	}
584

  
585
	private void showAlert(String title, String message) {
548 586
		AlertDialog alert = new AlertDialog.Builder(this).create();
549 587
		alert.setTitle(title);
550 588
		alert.setMessage(message);
551 589
		alert.setButton("OK", new DialogInterface.OnClickListener() {
552
	      public void onClick(DialogInterface dialog, int which) {
553
	        return;
554
	    } }); 
590
			public void onClick(DialogInterface dialog, int which) {
591
				return;
592
			} }); 
555 593
		alert.show();
556
    }
557
    
558
    private void showToast(String message) {
594
	}
595

  
596
	private void showToast(String message) {
559 597
		Context context = getApplicationContext();
560 598
		int duration = Toast.LENGTH_SHORT;
561 599
		Toast toast = Toast.makeText(context, message, duration);
562 600
		toast.show();
563
    }
564
    
565
    
566
	
567
		
601
	}
602

  
603

  
604

  
605

  
568 606
}

Also available in: Unified diff