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