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