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