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