root / src / com / rackspace / cloud / android / CloudListActivity.java @ 378fe36a
History | View | Annotate | Download (8 kB)
1 |
package com.rackspace.cloud.android; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
5 |
import java.util.Calendar; |
6 |
|
7 |
import javax.xml.parsers.FactoryConfigurationError; |
8 |
import javax.xml.parsers.ParserConfigurationException; |
9 |
import javax.xml.parsers.SAXParser; |
10 |
import javax.xml.parsers.SAXParserFactory; |
11 |
|
12 |
import org.apache.http.HttpResponse; |
13 |
import org.apache.http.client.ClientProtocolException; |
14 |
import org.apache.http.impl.client.BasicResponseHandler; |
15 |
import org.xml.sax.InputSource; |
16 |
import org.xml.sax.SAXException; |
17 |
import org.xml.sax.XMLReader; |
18 |
|
19 |
import android.app.AlertDialog; |
20 |
import android.app.Dialog; |
21 |
import android.content.Context; |
22 |
import android.content.DialogInterface; |
23 |
import android.content.DialogInterface.OnCancelListener; |
24 |
import android.content.Intent; |
25 |
import android.os.AsyncTask; |
26 |
import android.os.Bundle; |
27 |
import android.view.View; |
28 |
import android.view.View.OnClickListener; |
29 |
import android.view.WindowManager; |
30 |
import android.widget.Button; |
31 |
import android.widget.EditText; |
32 |
import android.widget.Toast; |
33 |
|
34 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
35 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
36 |
import com.rackspace.cloud.servers.api.client.http.HttpBundleNoNetwork; |
37 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
38 |
|
39 |
/*
|
40 |
* CloudActivity manages the display and hiding of
|
41 |
* pDialog.
|
42 |
*
|
43 |
* Also provides many accessory methods that are common
|
44 |
* to Activities
|
45 |
*/
|
46 |
public class CloudListActivity extends GaListActivity { |
47 |
|
48 |
private final int PASSWORD_PROMPT = 123; |
49 |
private Context context; |
50 |
private boolean isLoading; |
51 |
private Dialog pDialog; |
52 |
protected AndroidCloudApplication app;
|
53 |
// need to store if the user has successfully logged in
|
54 |
private boolean loggedIn; |
55 |
|
56 |
@Override
|
57 |
protected void onCreate(Bundle savedInstanceState) { |
58 |
super.onCreate(savedInstanceState);
|
59 |
app = (AndroidCloudApplication) this.getApplication();
|
60 |
// So keyboard doesn't open till user clicks
|
61 |
this.getWindow().setSoftInputMode(
|
62 |
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); |
63 |
registerForContextMenu(getListView()); |
64 |
} |
65 |
|
66 |
@Override
|
67 |
protected void onSaveInstanceState(Bundle outState) { |
68 |
super.onSaveInstanceState(outState);
|
69 |
|
70 |
outState.putBoolean("isLoading", isLoading);
|
71 |
outState.putBoolean("loggedIn", loggedIn);
|
72 |
|
73 |
if (pDialog != null && pDialog.isShowing()) { |
74 |
hideDialog(); |
75 |
} |
76 |
|
77 |
} |
78 |
|
79 |
protected void restoreState(Bundle state) { |
80 |
context = getApplicationContext(); |
81 |
if (state != null && state.containsKey("loggedIn")) { |
82 |
loggedIn = state.getBoolean("loggedIn");
|
83 |
} else {
|
84 |
loggedIn = false;
|
85 |
} |
86 |
|
87 |
/*
|
88 |
* need to restore the pDialog is was shown before a config change
|
89 |
*/
|
90 |
if (state != null && state.containsKey("isLoading")) { |
91 |
isLoading = state.getBoolean("isLoading");
|
92 |
if (isLoading) {
|
93 |
showDialog(); |
94 |
} |
95 |
} |
96 |
|
97 |
} |
98 |
|
99 |
@Override
|
100 |
protected void onStart() { |
101 |
super.onStart();
|
102 |
if (isLoading) {
|
103 |
showDialog(); |
104 |
} |
105 |
} |
106 |
|
107 |
@Override
|
108 |
protected void onStop() { |
109 |
super.onStop();
|
110 |
if (isLoading) {
|
111 |
hideDialog(); |
112 |
isLoading = true;
|
113 |
} |
114 |
} |
115 |
|
116 |
@Override
|
117 |
protected void onPause() { |
118 |
super.onPause();
|
119 |
Calendar cal = Calendar.getInstance(); |
120 |
AndroidCloudApplication.lastPause = cal.getTimeInMillis(); |
121 |
} |
122 |
|
123 |
@Override
|
124 |
protected void onResume() { |
125 |
super.onResume();
|
126 |
Calendar cal = Calendar.getInstance(); |
127 |
long curTime = cal.getTimeInMillis();
|
128 |
if (curTime - AndroidCloudApplication.lastPause > 5000) { |
129 |
verifyPassword(); |
130 |
} |
131 |
} |
132 |
|
133 |
/*
|
134 |
* if the application is password protected, the user must provide the
|
135 |
* password before gaining access
|
136 |
*/
|
137 |
private void verifyPassword() { |
138 |
PasswordManager pwManager = new PasswordManager(getSharedPreferences(
|
139 |
Preferences.SHARED_PREFERENCES_NAME, MODE_PRIVATE));
|
140 |
if (pwManager.hasPassword()) {
|
141 |
createCustomDialog(PASSWORD_PROMPT); |
142 |
} |
143 |
} |
144 |
|
145 |
private boolean rightPassword(String password) { |
146 |
PasswordManager pwManager = new PasswordManager(getSharedPreferences(
|
147 |
Preferences.SHARED_PREFERENCES_NAME, MODE_PRIVATE));
|
148 |
return pwManager.verifyEnteredPassword(password);
|
149 |
} |
150 |
|
151 |
/*
|
152 |
* forces the user to enter a correct password before they gain access to
|
153 |
* application data
|
154 |
*/
|
155 |
private void createCustomDialog(int id) { |
156 |
final Dialog dialog = new Dialog(CloudListActivity.this); |
157 |
switch (id) {
|
158 |
case PASSWORD_PROMPT:
|
159 |
dialog.setContentView(R.layout.passworddialog); |
160 |
dialog.setTitle("Enter your password:");
|
161 |
dialog.setCancelable(false);
|
162 |
Button button = (Button) dialog.findViewById(R.id.submit_password); |
163 |
button.setOnClickListener(new OnClickListener() {
|
164 |
public void onClick(View v) { |
165 |
EditText passwordText = ((EditText) dialog |
166 |
.findViewById(R.id.submit_password_text)); |
167 |
if (!rightPassword(passwordText.getText().toString())) {
|
168 |
passwordText.setText("");
|
169 |
showToast("Password was incorrect.");
|
170 |
loggedIn = false;
|
171 |
} else {
|
172 |
dialog.dismiss(); |
173 |
loggedIn = true;
|
174 |
} |
175 |
} |
176 |
|
177 |
}); |
178 |
dialog.show(); |
179 |
} |
180 |
} |
181 |
|
182 |
protected final void showAlert(String title, String message) { |
183 |
try {
|
184 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
185 |
alert.setTitle(title); |
186 |
alert.setMessage(message); |
187 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
188 |
public void onClick(DialogInterface dialog, int which) { |
189 |
return;
|
190 |
} |
191 |
}); |
192 |
alert.show(); |
193 |
} catch (Exception e) { |
194 |
e.printStackTrace(); |
195 |
} |
196 |
} |
197 |
|
198 |
protected final void showError(final String message, HttpBundle bundle) { |
199 |
new AsyncTask<HttpBundle, Void, HttpBundle>() { |
200 |
|
201 |
@Override
|
202 |
protected HttpBundle doInBackground(HttpBundle... params) {
|
203 |
if (params != null && params.length > 0) { |
204 |
HttpBundle result = params[0];
|
205 |
result.parseResponse(); |
206 |
return result;
|
207 |
} |
208 |
return null; |
209 |
} |
210 |
|
211 |
@Override
|
212 |
protected void onPostExecute(HttpBundle result) { |
213 |
// TODO Auto-generated method stub
|
214 |
super.onPostExecute(result);
|
215 |
if (result != null) { |
216 |
Intent viewIntent = new Intent(getApplicationContext(),
|
217 |
ServerErrorActivity.class); |
218 |
viewIntent.putExtra("errorMessage", message);
|
219 |
viewIntent.putExtra("response",
|
220 |
result.getResponseTextStatic()); |
221 |
viewIntent.putExtra("request", result.getCurlRequest());
|
222 |
startActivity(viewIntent); |
223 |
} |
224 |
} |
225 |
|
226 |
}.execute(bundle); |
227 |
|
228 |
} |
229 |
|
230 |
|
231 |
protected void showToast(String message) { |
232 |
Context context = getApplicationContext();
|
233 |
int duration = Toast.LENGTH_SHORT;
|
234 |
Toast toast = Toast.makeText(context, message, duration); |
235 |
toast.show(); |
236 |
} |
237 |
|
238 |
protected final CloudServersException parseCloudServersException( |
239 |
HttpResponse response) { |
240 |
CloudServersException cse = new CloudServersException();
|
241 |
try {
|
242 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
243 |
String body = responseHandler.handleResponse(response);
|
244 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
245 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
246 |
XMLReader xmlReader = saxParser.getXMLReader(); |
247 |
xmlReader.setContentHandler(parser); |
248 |
xmlReader.parse(new InputSource(new StringReader(body))); |
249 |
cse = parser.getException(); |
250 |
} catch (ClientProtocolException e) {
|
251 |
cse = new CloudServersException();
|
252 |
cse.setMessage(e.getLocalizedMessage()); |
253 |
} catch (IOException e) { |
254 |
cse = new CloudServersException();
|
255 |
cse.setMessage(e.getLocalizedMessage()); |
256 |
} catch (ParserConfigurationException e) { |
257 |
cse = new CloudServersException();
|
258 |
cse.setMessage(e.getLocalizedMessage()); |
259 |
} catch (SAXException e) {
|
260 |
cse = new CloudServersException();
|
261 |
cse.setMessage(e.getLocalizedMessage()); |
262 |
} catch (FactoryConfigurationError e) { |
263 |
cse = new CloudServersException();
|
264 |
cse.setMessage(e.getLocalizedMessage()); |
265 |
} |
266 |
return cse;
|
267 |
} |
268 |
|
269 |
protected final void hideDialog() { |
270 |
if (pDialog != null) { |
271 |
isLoading = false;
|
272 |
pDialog.dismiss(); |
273 |
} |
274 |
} |
275 |
|
276 |
protected final void showDialog() { |
277 |
if (pDialog == null || !pDialog.isShowing()) { |
278 |
isLoading = true;
|
279 |
pDialog = CenteredProgressDialog.show(this, "", "", false, true, |
280 |
new OnCancelListener() {
|
281 |
@Override
|
282 |
public void onCancel(DialogInterface dialog) { |
283 |
finish(); |
284 |
} |
285 |
}); |
286 |
pDialog.show(); |
287 |
} |
288 |
} |
289 |
|
290 |
protected Context getContext() { |
291 |
return context;
|
292 |
} |
293 |
} |