root / src / com / rackspacecloud / android / CloudListActivity.java @ b722cab3
History | View | Annotate | Download (4.6 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
5 |
|
6 |
import javax.xml.parsers.FactoryConfigurationError; |
7 |
import javax.xml.parsers.ParserConfigurationException; |
8 |
import javax.xml.parsers.SAXParser; |
9 |
import javax.xml.parsers.SAXParserFactory; |
10 |
|
11 |
import org.apache.http.HttpResponse; |
12 |
import org.apache.http.client.ClientProtocolException; |
13 |
import org.apache.http.impl.client.BasicResponseHandler; |
14 |
import org.xml.sax.InputSource; |
15 |
import org.xml.sax.SAXException; |
16 |
import org.xml.sax.XMLReader; |
17 |
|
18 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
19 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
20 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
21 |
|
22 |
import android.app.AlertDialog; |
23 |
import android.app.ListActivity; |
24 |
import android.app.ProgressDialog; |
25 |
import android.content.Context; |
26 |
import android.content.DialogInterface; |
27 |
import android.content.Intent; |
28 |
import android.content.DialogInterface.OnCancelListener; |
29 |
import android.os.Bundle; |
30 |
import android.view.WindowManager; |
31 |
import android.view.ViewGroup.LayoutParams; |
32 |
import android.widget.ProgressBar; |
33 |
|
34 |
public class CloudListActivity extends ListActivity{ |
35 |
|
36 |
private Context context; |
37 |
private boolean isLoading; |
38 |
private ProgressDialog pDialog;
|
39 |
|
40 |
@Override
|
41 |
protected void onSaveInstanceState(Bundle outState) { |
42 |
super.onSaveInstanceState(outState);
|
43 |
|
44 |
outState.putBoolean("isLoading", isLoading);
|
45 |
|
46 |
if(pDialog != null && pDialog.isShowing()){ |
47 |
hideDialog(); |
48 |
} |
49 |
|
50 |
} |
51 |
|
52 |
protected void restoreState(Bundle state) { |
53 |
context = getApplicationContext(); |
54 |
|
55 |
/*
|
56 |
* need to restore the pDialog is was shown before
|
57 |
* a config change
|
58 |
*/
|
59 |
if (state != null && state.containsKey("isLoading")){ |
60 |
isLoading = state.getBoolean("isLoading");
|
61 |
if(isLoading){
|
62 |
showDialog(); |
63 |
} |
64 |
} |
65 |
|
66 |
} |
67 |
|
68 |
protected final void showAlert(String title, String message) { |
69 |
try {
|
70 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
71 |
alert.setTitle(title); |
72 |
alert.setMessage(message); |
73 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
74 |
public void onClick(DialogInterface dialog, int which) { |
75 |
return;
|
76 |
} |
77 |
}); |
78 |
alert.show(); |
79 |
} catch (Exception e) { |
80 |
e.printStackTrace(); |
81 |
} |
82 |
} |
83 |
|
84 |
protected final void showError(String message, HttpBundle bundle){ |
85 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
|
86 |
viewIntent.putExtra("errorMessage", message);
|
87 |
viewIntent.putExtra("response", bundle.getResponseText());
|
88 |
viewIntent.putExtra("request", bundle.getCurlRequest());
|
89 |
startActivity(viewIntent); |
90 |
} |
91 |
|
92 |
protected final CloudServersException parseCloudServersException(HttpResponse response) { |
93 |
CloudServersException cse = new CloudServersException();
|
94 |
try {
|
95 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
96 |
String body = responseHandler.handleResponse(response);
|
97 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
98 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
99 |
XMLReader xmlReader = saxParser.getXMLReader(); |
100 |
xmlReader.setContentHandler(parser); |
101 |
xmlReader.parse(new InputSource(new StringReader(body))); |
102 |
cse = parser.getException(); |
103 |
} catch (ClientProtocolException e) {
|
104 |
cse = new CloudServersException();
|
105 |
cse.setMessage(e.getLocalizedMessage()); |
106 |
} catch (IOException e) { |
107 |
cse = new CloudServersException();
|
108 |
cse.setMessage(e.getLocalizedMessage()); |
109 |
} catch (ParserConfigurationException e) { |
110 |
cse = new CloudServersException();
|
111 |
cse.setMessage(e.getLocalizedMessage()); |
112 |
} catch (SAXException e) {
|
113 |
cse = new CloudServersException();
|
114 |
cse.setMessage(e.getLocalizedMessage()); |
115 |
} catch (FactoryConfigurationError e) { |
116 |
cse = new CloudServersException();
|
117 |
cse.setMessage(e.getLocalizedMessage()); |
118 |
} |
119 |
return cse;
|
120 |
} |
121 |
|
122 |
protected final void hideDialog() { |
123 |
if(pDialog != null){ |
124 |
isLoading = false;
|
125 |
pDialog.dismiss(); |
126 |
} |
127 |
} |
128 |
|
129 |
protected final void showDialog() { |
130 |
if(pDialog == null || !pDialog.isShowing()){ |
131 |
isLoading = true;
|
132 |
pDialog = new ProgressDialog(this, R.style.NewDialog); |
133 |
|
134 |
/*
|
135 |
* if back is pressed while dialog is showing it will
|
136 |
* still finish the activity
|
137 |
*/
|
138 |
pDialog.setOnCancelListener(new OnCancelListener() {
|
139 |
@Override
|
140 |
public void onCancel(DialogInterface dialog) { |
141 |
finish(); |
142 |
} |
143 |
}); |
144 |
|
145 |
pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
146 |
pDialog.show(); |
147 |
pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); |
148 |
} |
149 |
} |
150 |
|
151 |
protected Context getContext(){ |
152 |
return context;
|
153 |
} |
154 |
|
155 |
} |