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