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