root / src / com / rackspacecloud / android / RackspaceCloudActivity.java @ 3d6041e8
History | View | Annotate | Download (7 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.view.KeyEvent; |
16 |
import android.view.View; |
17 |
import android.widget.Button; |
18 |
import android.widget.EditText; |
19 |
import android.widget.ProgressBar; |
20 |
import android.widget.TextView; |
21 |
import android.widget.TextView.OnEditorActionListener; |
22 |
|
23 |
import com.rackspace.cloud.servers.api.client.Account; |
24 |
import com.rackspace.cloud.servers.api.client.Flavor; |
25 |
import com.rackspace.cloud.servers.api.client.FlavorManager; |
26 |
import com.rackspace.cloud.servers.api.client.Image; |
27 |
import com.rackspace.cloud.servers.api.client.ImageManager; |
28 |
import com.rackspace.cloud.servers.api.client.http.Authentication; |
29 |
|
30 |
public class RackspaceCloudActivity extends Activity implements View.OnClickListener, OnEditorActionListener { |
31 |
|
32 |
private static final String OPT_USERNAME = "username"; |
33 |
private static final String OPT_USERNAME_DEF = ""; |
34 |
private static final String OPT_API_KEY = "apiKey"; |
35 |
private static final String OPT_API_KEY_DEF = ""; |
36 |
|
37 |
private Intent tabViewIntent;
|
38 |
private boolean authenticating; |
39 |
|
40 |
/** Called when the activity is first created. */
|
41 |
@Override
|
42 |
public void onCreate(Bundle savedInstanceState) { |
43 |
super.onCreate(savedInstanceState);
|
44 |
setContentView(R.layout.main); |
45 |
((Button) findViewById(R.id.button)).setOnClickListener(this); |
46 |
((EditText) findViewById(R.id.login_apikey)).setOnEditorActionListener(this);
|
47 |
loadLoginPreferences(); |
48 |
restoreState(savedInstanceState); |
49 |
|
50 |
// use the TabViewActivity when Cloud Files is added
|
51 |
// tabViewIntent = new Intent(this, TabViewActivity.class);
|
52 |
|
53 |
tabViewIntent = new Intent(this, TabViewActivity.class); |
54 |
} |
55 |
|
56 |
@Override
|
57 |
protected void onSaveInstanceState(Bundle outState) { |
58 |
super.onSaveInstanceState(outState);
|
59 |
outState.putBoolean("authenticating", authenticating);
|
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 |
} |
69 |
|
70 |
public void login() { |
71 |
if (hasValidInput()) {
|
72 |
showActivityIndicators(); |
73 |
setLoginPreferences(); |
74 |
new AuthenticateTask().execute((Void[]) null); |
75 |
} else {
|
76 |
showAlert("Fields Missing", "User Name and API Key are required."); |
77 |
} |
78 |
} |
79 |
|
80 |
public void onClick(View view) { |
81 |
login(); |
82 |
} |
83 |
|
84 |
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) { |
85 |
login(); |
86 |
return false; |
87 |
} |
88 |
|
89 |
private void loadLoginPreferences() { |
90 |
SharedPreferences sp = this.getPreferences(Context.MODE_PRIVATE); |
91 |
String username = sp.getString(OPT_USERNAME, OPT_USERNAME_DEF);
|
92 |
String apiKey = sp.getString(OPT_API_KEY, OPT_API_KEY_DEF);
|
93 |
EditText usernameText = (EditText) findViewById(R.id.login_username); |
94 |
usernameText.setText(username); |
95 |
EditText apiKeyText = (EditText) findViewById(R.id.login_apikey); |
96 |
apiKeyText.setText(apiKey); |
97 |
} |
98 |
|
99 |
private void setLoginPreferences() { |
100 |
String username = ((EditText) findViewById(R.id.login_username)).getText().toString();
|
101 |
String apiKey = ((EditText) findViewById(R.id.login_apikey)).getText().toString();
|
102 |
Account.setUsername(username); |
103 |
Account.setApiKey(apiKey); |
104 |
|
105 |
Editor e = this.getPreferences(Context.MODE_PRIVATE).edit(); |
106 |
e.putString(OPT_USERNAME, username); |
107 |
e.putString(OPT_API_KEY, apiKey); |
108 |
e.commit(); |
109 |
} |
110 |
|
111 |
private void showAlert(String title, String message) { |
112 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
113 |
alert.setTitle(title); |
114 |
alert.setMessage(message); |
115 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
116 |
public void onClick(DialogInterface dialog, int which) { |
117 |
return;
|
118 |
} }); |
119 |
alert.show(); |
120 |
hideActivityIndicators(); |
121 |
} |
122 |
|
123 |
private boolean hasValidInput() { |
124 |
String username = ((EditText) findViewById(R.id.login_username)).getText().toString();
|
125 |
String apiKey = ((EditText) findViewById(R.id.login_apikey)).getText().toString();
|
126 |
return !"".equals(username) && !"".equals(apiKey); |
127 |
} |
128 |
|
129 |
private void setActivityIndicatorsVisibility(int visibility) { |
130 |
ProgressBar pb = (ProgressBar) findViewById(R.id.login_progress_bar); |
131 |
TextView tv = (TextView) findViewById(R.id.login_authenticating_label); |
132 |
pb.setVisibility(visibility); |
133 |
tv.setVisibility(visibility); |
134 |
} |
135 |
|
136 |
private void showActivityIndicators() { |
137 |
setActivityIndicatorsVisibility(View.VISIBLE);
|
138 |
} |
139 |
|
140 |
private void hideActivityIndicators() { |
141 |
setActivityIndicatorsVisibility(View.INVISIBLE);
|
142 |
} |
143 |
|
144 |
private class AuthenticateTask extends AsyncTask<Void, Void, Boolean> { |
145 |
|
146 |
@Override
|
147 |
protected Boolean doInBackground(Void... arg0) { |
148 |
authenticating = true;
|
149 |
return new Boolean(Authentication.authenticate()); |
150 |
} |
151 |
|
152 |
@Override
|
153 |
protected void onPostExecute(Boolean result) { |
154 |
authenticating = false;
|
155 |
if (result.booleanValue()) {
|
156 |
//startActivity(tabViewIntent);
|
157 |
new LoadImagesTask().execute((Void[]) null); |
158 |
} else {
|
159 |
showAlert("Login Failure", "Authentication failed. Please check your User Name and API Key."); |
160 |
} |
161 |
} |
162 |
} |
163 |
|
164 |
private class LoadFlavorsTask extends AsyncTask<Void, Void, ArrayList<Flavor>> { |
165 |
|
166 |
@Override
|
167 |
protected ArrayList<Flavor> doInBackground(Void... arg0) { |
168 |
return (new FlavorManager()).createList(true); |
169 |
} |
170 |
|
171 |
@Override
|
172 |
protected void onPostExecute(ArrayList<Flavor> result) { |
173 |
if (result != null && result.size() > 0) { |
174 |
TreeMap<String, Flavor> flavorMap = new TreeMap<String, Flavor>(); |
175 |
for (int i = 0; i < result.size(); i++) { |
176 |
Flavor flavor = result.get(i); |
177 |
flavorMap.put(flavor.getId(), flavor); |
178 |
} |
179 |
Flavor.setFlavors(flavorMap); |
180 |
startActivity(tabViewIntent); |
181 |
} else {
|
182 |
showAlert("Login Failure", "There was a problem loading server flavors. Please try again."); |
183 |
} |
184 |
hideActivityIndicators(); |
185 |
} |
186 |
} |
187 |
|
188 |
private class LoadImagesTask extends AsyncTask<Void, Void, ArrayList<Image>> { |
189 |
|
190 |
@Override
|
191 |
protected ArrayList<Image> doInBackground(Void... arg0) { |
192 |
return (new ImageManager()).createList(true); |
193 |
} |
194 |
|
195 |
@Override
|
196 |
protected void onPostExecute(ArrayList<Image> result) { |
197 |
if (result != null && result.size() > 0) { |
198 |
TreeMap<String, Image> imageMap = new TreeMap<String, Image>(); |
199 |
for (int i = 0; i < result.size(); i++) { |
200 |
Image image = result.get(i);
|
201 |
imageMap.put(image.getId(), image); |
202 |
} |
203 |
Image.setImages(imageMap);
|
204 |
new LoadFlavorsTask().execute((Void[]) null); |
205 |
//startActivity(tabViewIntent);
|
206 |
} else {
|
207 |
showAlert("Login Failure", "There was a problem loading server images. Please try again."); |
208 |
} |
209 |
//hideActivityIndicators();
|
210 |
} |
211 |
} |
212 |
|
213 |
} |