root / src / com / rackspacecloud / android / CloudListActivity.java @ 21c57799
History | View | Annotate | Download (5.5 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.ProgressDialog; |
24 |
import android.content.Context; |
25 |
import android.content.DialogInterface; |
26 |
import android.content.Intent; |
27 |
import android.content.DialogInterface.OnCancelListener; |
28 |
import android.os.Bundle; |
29 |
import android.util.Log; |
30 |
import android.view.WindowManager; |
31 |
import android.view.ViewGroup.LayoutParams; |
32 |
import android.widget.ProgressBar; |
33 |
import android.widget.Toast; |
34 |
|
35 |
/*
|
36 |
* CloudActivity manages the display and hiding of
|
37 |
* pDialog.
|
38 |
*
|
39 |
* Also provides many accessory methods that are common
|
40 |
* to Activities
|
41 |
*/
|
42 |
public class CloudListActivity extends GaListActivity{ |
43 |
|
44 |
private Context context; |
45 |
private boolean isLoading; |
46 |
private ProgressDialog pDialog;
|
47 |
|
48 |
@Override
|
49 |
protected void onCreate(Bundle savedInstanceState){ |
50 |
super.onCreate(savedInstanceState);
|
51 |
|
52 |
//So keyboard doesn't open till user clicks
|
53 |
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
|
54 |
} |
55 |
|
56 |
@Override
|
57 |
protected void onSaveInstanceState(Bundle outState) { |
58 |
super.onSaveInstanceState(outState);
|
59 |
|
60 |
outState.putBoolean("isLoading", isLoading);
|
61 |
|
62 |
if(pDialog != null && pDialog.isShowing()){ |
63 |
hideDialog(); |
64 |
} |
65 |
|
66 |
} |
67 |
|
68 |
protected void restoreState(Bundle state) { |
69 |
context = getApplicationContext(); |
70 |
|
71 |
/*
|
72 |
* need to restore the pDialog is was shown before
|
73 |
* a config change
|
74 |
*/
|
75 |
if (state != null && state.containsKey("isLoading")){ |
76 |
isLoading = state.getBoolean("isLoading");
|
77 |
if(isLoading){
|
78 |
showDialog(); |
79 |
} |
80 |
} |
81 |
|
82 |
} |
83 |
|
84 |
@Override
|
85 |
protected void onStart(){ |
86 |
super.onStart();
|
87 |
if(isLoading){
|
88 |
showDialog(); |
89 |
} |
90 |
} |
91 |
|
92 |
@Override
|
93 |
protected void onStop(){ |
94 |
super.onStop();
|
95 |
if(isLoading){
|
96 |
hideDialog(); |
97 |
isLoading = true;
|
98 |
} |
99 |
} |
100 |
|
101 |
protected final void showAlert(String title, String message) { |
102 |
try {
|
103 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
104 |
alert.setTitle(title); |
105 |
alert.setMessage(message); |
106 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
107 |
public void onClick(DialogInterface dialog, int which) { |
108 |
return;
|
109 |
} |
110 |
}); |
111 |
alert.show(); |
112 |
} catch (Exception e) { |
113 |
e.printStackTrace(); |
114 |
} |
115 |
} |
116 |
|
117 |
protected final void showError(String message, HttpBundle bundle){ |
118 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
|
119 |
viewIntent.putExtra("errorMessage", message);
|
120 |
viewIntent.putExtra("response", bundle.getResponseText());
|
121 |
viewIntent.putExtra("request", bundle.getCurlRequest());
|
122 |
startActivity(viewIntent); |
123 |
} |
124 |
|
125 |
protected void showToast(String message) { |
126 |
Context context = getApplicationContext();
|
127 |
int duration = Toast.LENGTH_SHORT;
|
128 |
Toast toast = Toast.makeText(context, message, duration); |
129 |
toast.show(); |
130 |
} |
131 |
|
132 |
protected final CloudServersException parseCloudServersException(HttpResponse response) { |
133 |
CloudServersException cse = new CloudServersException();
|
134 |
try {
|
135 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
136 |
String body = responseHandler.handleResponse(response);
|
137 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
138 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
139 |
XMLReader xmlReader = saxParser.getXMLReader(); |
140 |
xmlReader.setContentHandler(parser); |
141 |
xmlReader.parse(new InputSource(new StringReader(body))); |
142 |
cse = parser.getException(); |
143 |
} catch (ClientProtocolException e) {
|
144 |
cse = new CloudServersException();
|
145 |
cse.setMessage(e.getLocalizedMessage()); |
146 |
} catch (IOException e) { |
147 |
cse = new CloudServersException();
|
148 |
cse.setMessage(e.getLocalizedMessage()); |
149 |
} catch (ParserConfigurationException e) { |
150 |
cse = new CloudServersException();
|
151 |
cse.setMessage(e.getLocalizedMessage()); |
152 |
} catch (SAXException e) {
|
153 |
cse = new CloudServersException();
|
154 |
cse.setMessage(e.getLocalizedMessage()); |
155 |
} catch (FactoryConfigurationError e) { |
156 |
cse = new CloudServersException();
|
157 |
cse.setMessage(e.getLocalizedMessage()); |
158 |
} |
159 |
return cse;
|
160 |
} |
161 |
|
162 |
protected final void hideDialog() { |
163 |
if(pDialog != null){ |
164 |
Log.d("info", "dialog hide"); |
165 |
isLoading = false;
|
166 |
pDialog.dismiss(); |
167 |
} |
168 |
} |
169 |
|
170 |
protected final void showDialog() { |
171 |
if(pDialog == null || !pDialog.isShowing()){ |
172 |
Log.d("info", "dialog created"); |
173 |
isLoading = true;
|
174 |
pDialog = new ProgressDialog(this); |
175 |
pDialog.setProgressStyle(R.style.NewDialog); |
176 |
|
177 |
/*
|
178 |
* if back is pressed while dialog is showing it will
|
179 |
* still finish the activity
|
180 |
*/
|
181 |
pDialog.setOnCancelListener(new OnCancelListener() {
|
182 |
@Override
|
183 |
public void onCancel(DialogInterface dialog) { |
184 |
finish(); |
185 |
} |
186 |
}); |
187 |
pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
188 |
pDialog.show(); |
189 |
pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); |
190 |
} |
191 |
} |
192 |
|
193 |
protected Context getContext(){ |
194 |
return context;
|
195 |
} |
196 |
} |