root / src / com / rackspacecloud / android / RackspaceCloudActivity.java @ 2aaecc36
History | View | Annotate | Download (8.3 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.TreeMap; |
5 |
|
6 |
import android.app.Activity; |
7 |
import android.app.AlertDialog; |
8 |
import android.app.Dialog; |
9 |
import android.content.Context; |
10 |
import android.content.DialogInterface; |
11 |
import android.content.Intent; |
12 |
import android.content.SharedPreferences; |
13 |
import android.content.SharedPreferences.Editor; |
14 |
import android.os.AsyncTask; |
15 |
import android.os.Bundle; |
16 |
import android.util.Log; |
17 |
import android.view.KeyEvent; |
18 |
import android.view.Menu; |
19 |
import android.view.MenuItem; |
20 |
import android.view.View; |
21 |
import android.widget.Button; |
22 |
import android.widget.EditText; |
23 |
import android.widget.ProgressBar; |
24 |
import android.widget.TextView; |
25 |
import android.widget.TextView.OnEditorActionListener; |
26 |
|
27 |
import com.rackspace.cloud.servers.api.client.Account; |
28 |
import com.rackspace.cloud.servers.api.client.Flavor; |
29 |
import com.rackspace.cloud.servers.api.client.FlavorManager; |
30 |
import com.rackspace.cloud.servers.api.client.Image; |
31 |
import com.rackspace.cloud.servers.api.client.ImageManager; |
32 |
import com.rackspace.cloud.servers.api.client.http.Authentication; |
33 |
|
34 |
public class RackspaceCloudActivity extends Activity implements View.OnClickListener, OnEditorActionListener { |
35 |
|
36 |
private static final String OPT_USERNAME = "username"; |
37 |
private static final String OPT_USERNAME_DEF = ""; |
38 |
private static final String OPT_API_KEY = "apiKey"; |
39 |
private static final String OPT_API_KEY_DEF = ""; |
40 |
|
41 |
private static final int SHOW_PREFERENCES = 1; |
42 |
|
43 |
private Intent tabViewIntent;
|
44 |
private boolean authenticating; |
45 |
|
46 |
/** Called when the activity is first created. */
|
47 |
@Override
|
48 |
public void onCreate(Bundle savedInstanceState) { |
49 |
super.onCreate(savedInstanceState);
|
50 |
setContentView(R.layout.main); |
51 |
((Button) findViewById(R.id.button)).setOnClickListener(this); |
52 |
((EditText) findViewById(R.id.login_apikey)).setOnEditorActionListener(this);
|
53 |
loadLoginPreferences(); |
54 |
restoreState(savedInstanceState); |
55 |
|
56 |
// use the TabViewActivity when Cloud Files is added
|
57 |
// tabViewIntent = new Intent(this, TabViewActivity.class);
|
58 |
|
59 |
tabViewIntent = new Intent(this, TabViewActivity.class); |
60 |
} |
61 |
|
62 |
@Override
|
63 |
protected void onSaveInstanceState(Bundle outState) { |
64 |
super.onSaveInstanceState(outState);
|
65 |
outState.putBoolean("authenticating", authenticating);
|
66 |
} |
67 |
|
68 |
public boolean onCreateOptionsMenu(Menu menu) { |
69 |
menu.add(0, SHOW_PREFERENCES, 0, "Preferences"); |
70 |
return true; |
71 |
} |
72 |
|
73 |
public boolean onOptionsItemSelected(MenuItem item) { |
74 |
|
75 |
switch (item.getItemId()) {
|
76 |
case SHOW_PREFERENCES:
|
77 |
showPreferences(); |
78 |
break;
|
79 |
} |
80 |
return true; |
81 |
} |
82 |
|
83 |
public void showPreferences() { |
84 |
Intent settingsActivity = new Intent(getBaseContext(),
|
85 |
Preferences.class);
|
86 |
startActivity(settingsActivity); |
87 |
} |
88 |
|
89 |
private void restoreState(Bundle state) { |
90 |
if (state != null && state.containsKey("authenticating") && state.getBoolean("authenticating")) { |
91 |
showActivityIndicators(); |
92 |
} else {
|
93 |
hideActivityIndicators(); |
94 |
} |
95 |
} |
96 |
|
97 |
public void login() { |
98 |
if (hasValidInput()) {
|
99 |
showActivityIndicators(); |
100 |
setLoginPreferences(); |
101 |
new AuthenticateTask().execute((Void[]) null); |
102 |
} else {
|
103 |
showAlert("Fields Missing", "User Name and API Key are required."); |
104 |
} |
105 |
} |
106 |
|
107 |
public void onClick(View view) { |
108 |
login(); |
109 |
} |
110 |
|
111 |
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) { |
112 |
login(); |
113 |
return false; |
114 |
} |
115 |
|
116 |
private void loadLoginPreferences() { |
117 |
SharedPreferences sp = this.getPreferences(Context.MODE_PRIVATE); |
118 |
String username = sp.getString(OPT_USERNAME, OPT_USERNAME_DEF);
|
119 |
String apiKey = sp.getString(OPT_API_KEY, OPT_API_KEY_DEF);
|
120 |
EditText usernameText = (EditText) findViewById(R.id.login_username); |
121 |
usernameText.setText(username); |
122 |
EditText apiKeyText = (EditText) findViewById(R.id.login_apikey); |
123 |
apiKeyText.setText(apiKey); |
124 |
} |
125 |
|
126 |
private void setLoginPreferences() { |
127 |
SharedPreferences prefs = getSharedPreferences( |
128 |
Preferences.SHARED_PREFERENCES_NAME,
|
129 |
Context.MODE_PRIVATE);
|
130 |
String resultType = prefs.getString(
|
131 |
Preferences.PREF_KEY_RESULTS_TYPE,
|
132 |
String.valueOf(Preferences.COUNTRY_US)); |
133 |
int resultTypeInt = Integer.parseInt(resultType); |
134 |
|
135 |
//Default Auth Server
|
136 |
String authServer = Preferences.COUNTRY_US_AUTH_SERVER; |
137 |
if (resultTypeInt == Preferences.COUNTRY_UK) |
138 |
authServer = Preferences.COUNTRY_UK_AUTH_SERVER;
|
139 |
|
140 |
String username = ((EditText) findViewById(R.id.login_username)).getText().toString();
|
141 |
String apiKey = ((EditText) findViewById(R.id.login_apikey)).getText().toString();
|
142 |
Account.setUsername(username); |
143 |
Account.setApiKey(apiKey); |
144 |
Account.setAuthServer(authServer); |
145 |
|
146 |
Editor e = this.getPreferences(Context.MODE_PRIVATE).edit(); |
147 |
e.putString(OPT_USERNAME, username); |
148 |
e.putString(OPT_API_KEY, apiKey); |
149 |
e.commit(); |
150 |
} |
151 |
|
152 |
private void showAlert(String title, String message) { |
153 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
154 |
alert.setTitle(title); |
155 |
alert.setMessage(message); |
156 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
157 |
public void onClick(DialogInterface dialog, int which) { |
158 |
return;
|
159 |
} }); |
160 |
alert.show(); |
161 |
hideActivityIndicators(); |
162 |
} |
163 |
|
164 |
private boolean hasValidInput() { |
165 |
String username = ((EditText) findViewById(R.id.login_username)).getText().toString();
|
166 |
String apiKey = ((EditText) findViewById(R.id.login_apikey)).getText().toString();
|
167 |
return !"".equals(username) && !"".equals(apiKey); |
168 |
} |
169 |
|
170 |
private void setActivityIndicatorsVisibility(int visibility) { |
171 |
ProgressBar pb = (ProgressBar) findViewById(R.id.login_progress_bar); |
172 |
TextView tv = (TextView) findViewById(R.id.login_authenticating_label); |
173 |
pb.setVisibility(visibility); |
174 |
tv.setVisibility(visibility); |
175 |
} |
176 |
|
177 |
private void showActivityIndicators() { |
178 |
setActivityIndicatorsVisibility(View.VISIBLE);
|
179 |
} |
180 |
|
181 |
private void hideActivityIndicators() { |
182 |
setActivityIndicatorsVisibility(View.INVISIBLE);
|
183 |
} |
184 |
|
185 |
private class AuthenticateTask extends AsyncTask<Void, Void, Boolean> { |
186 |
|
187 |
@Override
|
188 |
protected Boolean doInBackground(Void... arg0) { |
189 |
authenticating = true;
|
190 |
return new Boolean(Authentication.authenticate()); |
191 |
} |
192 |
|
193 |
@Override
|
194 |
protected void onPostExecute(Boolean result) { |
195 |
authenticating = false;
|
196 |
if (result.booleanValue()) {
|
197 |
//startActivity(tabViewIntent);
|
198 |
new LoadImagesTask().execute((Void[]) null); |
199 |
} else {
|
200 |
showAlert("Login Failure", "Authentication failed. Please check your User Name and API Key."); |
201 |
} |
202 |
} |
203 |
} |
204 |
|
205 |
private class LoadFlavorsTask extends AsyncTask<Void, Void, ArrayList<Flavor>> { |
206 |
|
207 |
@Override
|
208 |
protected ArrayList<Flavor> doInBackground(Void... arg0) { |
209 |
return (new FlavorManager()).createList(true); |
210 |
} |
211 |
|
212 |
@Override
|
213 |
protected void onPostExecute(ArrayList<Flavor> result) { |
214 |
if (result != null && result.size() > 0) { |
215 |
TreeMap<String, Flavor> flavorMap = new TreeMap<String, Flavor>(); |
216 |
for (int i = 0; i < result.size(); i++) { |
217 |
Flavor flavor = result.get(i); |
218 |
flavorMap.put(flavor.getId(), flavor); |
219 |
} |
220 |
Flavor.setFlavors(flavorMap); |
221 |
startActivity(tabViewIntent); |
222 |
} else {
|
223 |
showAlert("Login Failure", "There was a problem loading server flavors. Please try again."); |
224 |
} |
225 |
hideActivityIndicators(); |
226 |
} |
227 |
} |
228 |
|
229 |
private class LoadImagesTask extends AsyncTask<Void, Void, ArrayList<Image>> { |
230 |
|
231 |
@Override
|
232 |
protected ArrayList<Image> doInBackground(Void... arg0) { |
233 |
return (new ImageManager()).createList(true); |
234 |
} |
235 |
|
236 |
@Override
|
237 |
protected void onPostExecute(ArrayList<Image> result) { |
238 |
if (result != null && result.size() > 0) { |
239 |
TreeMap<String, Image> imageMap = new TreeMap<String, Image>(); |
240 |
for (int i = 0; i < result.size(); i++) { |
241 |
Image image = result.get(i);
|
242 |
imageMap.put(image.getId(), image); |
243 |
} |
244 |
Image.setImages(imageMap);
|
245 |
new LoadFlavorsTask().execute((Void[]) null); |
246 |
//startActivity(tabViewIntent);
|
247 |
} else {
|
248 |
showAlert("Login Failure", "There was a problem loading server images. Please try again."); |
249 |
} |
250 |
//hideActivityIndicators();
|
251 |
} |
252 |
} |
253 |
|
254 |
} |