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