Revision 6b8dad86 src/com/rackspacecloud/android/EnableCDNActivity.java
b/src/com/rackspacecloud/android/EnableCDNActivity.java | ||
---|---|---|
1 | 1 |
package com.rackspacecloud.android; |
2 | 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 | 3 |
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 | 4 |
|
18 | 5 |
import android.app.Activity; |
19 | 6 |
import android.app.AlertDialog; |
20 | 7 |
import android.app.Dialog; |
21 |
import android.content.Context; |
|
22 | 8 |
import android.content.DialogInterface; |
23 |
import android.content.Intent; |
|
24 | 9 |
import android.os.AsyncTask; |
25 | 10 |
import android.os.Bundle; |
26 | 11 |
import android.view.View; |
... | ... | |
29 | 14 |
import android.widget.ArrayAdapter; |
30 | 15 |
import android.widget.Button; |
31 | 16 |
import android.widget.Spinner; |
32 |
import android.widget.Toast; |
|
33 | 17 |
import android.widget.AdapterView.OnItemSelectedListener; |
34 | 18 |
|
35 | 19 |
import com.rackspace.cloud.files.api.client.ContainerManager; |
36 | 20 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
37 | 21 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
38 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
|
39 | 22 |
|
40 |
public class EnableCDNActivity extends GaActivity implements OnClickListener,
|
|
23 |
public class EnableCDNActivity extends CloudActivity implements OnClickListener,
|
|
41 | 24 |
OnItemSelectedListener { |
42 | 25 |
|
43 | 26 |
public static String containerName = null; |
... | ... | |
47 | 30 |
private Spinner ttlSpinner; |
48 | 31 |
private Spinner logRetSpinner; |
49 | 32 |
private Spinner cdnSpinner; |
50 |
private Context context; |
|
51 | 33 |
|
52 | 34 |
/** Called when the activity is first created. */ |
53 | 35 |
@Override |
... | ... | |
55 | 37 |
super.onCreate(savedInstanceState); |
56 | 38 |
trackPageView(PAGE_CONTAINER_DETAILS); |
57 | 39 |
setContentView(R.layout.enable_cdn_container); |
58 |
context = getApplicationContext(); |
|
59 | 40 |
containerName = (String) this.getIntent().getExtras().get("Cname"); |
41 |
restoreState(savedInstanceState); |
|
42 |
} |
|
43 |
|
|
44 |
protected void restoreState(Bundle state){ |
|
45 |
super.restoreState(state); |
|
60 | 46 |
setupButtons(); |
61 | 47 |
loadTtlSpinner(); |
62 | 48 |
loadLogRetSpinner(); |
63 | 49 |
loadCdnSpinner(); |
64 |
|
|
65 | 50 |
} |
66 | 51 |
|
67 | 52 |
private void setupButton(int resourceId, OnClickListener onClickListener) { |
... | ... | |
129 | 114 |
|
130 | 115 |
} |
131 | 116 |
|
132 |
private void showToast(String message) { |
|
133 |
Context context = getApplicationContext(); |
|
134 |
int duration = Toast.LENGTH_SHORT; |
|
135 |
Toast toast = Toast.makeText(context, message, duration); |
|
136 |
toast.show(); |
|
137 |
} |
|
138 |
|
|
139 | 117 |
@Override |
140 | 118 |
protected Dialog onCreateDialog(int id) { |
141 | 119 |
switch (id) { |
... | ... | |
186 | 164 |
} |
187 | 165 |
return null; |
188 | 166 |
} |
189 |
/* |
|
190 |
private void setActivityIndicatorsVisibility(int visibility) { |
|
191 |
ProgressBar pb = (ProgressBar) findViewById(R.id.save_container_progress_bar); |
|
192 |
TextView tv = (TextView) findViewById(R.id.saving_container_label); |
|
193 |
pb.setVisibility(visibility); |
|
194 |
tv.setVisibility(visibility); |
|
195 |
} |
|
196 |
|
|
197 |
private void showActivityIndicators() { |
|
198 |
setActivityIndicatorsVisibility(View.VISIBLE); |
|
199 |
} |
|
200 |
|
|
201 |
private void hideActivityIndicators() { |
|
202 |
setActivityIndicatorsVisibility(View.INVISIBLE); |
|
203 |
} |
|
204 |
*/ |
|
205 |
|
|
206 |
// using CloudServersException, it works for us too |
|
207 |
private CloudServersException parseCloudServersException( |
|
208 |
HttpResponse response) { |
|
209 |
CloudServersException cse = new CloudServersException(); |
|
210 |
try { |
|
211 |
BasicResponseHandler responseHandler = new BasicResponseHandler(); |
|
212 |
String body = responseHandler.handleResponse(response); |
|
213 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser(); |
|
214 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
|
215 |
XMLReader xmlReader = saxParser.getXMLReader(); |
|
216 |
xmlReader.setContentHandler(parser); |
|
217 |
xmlReader.parse(new InputSource(new StringReader(body))); |
|
218 |
cse = parser.getException(); |
|
219 |
} catch (ClientProtocolException e) { |
|
220 |
cse = new CloudServersException(); |
|
221 |
cse.setMessage(e.getLocalizedMessage()); |
|
222 |
} catch (IOException e) { |
|
223 |
cse = new CloudServersException(); |
|
224 |
cse.setMessage(e.getLocalizedMessage()); |
|
225 |
} catch (ParserConfigurationException e) { |
|
226 |
cse = new CloudServersException(); |
|
227 |
cse.setMessage(e.getLocalizedMessage()); |
|
228 |
} catch (SAXException e) { |
|
229 |
cse = new CloudServersException(); |
|
230 |
cse.setMessage(e.getLocalizedMessage()); |
|
231 |
} catch (FactoryConfigurationError e) { |
|
232 |
cse = new CloudServersException(); |
|
233 |
cse.setMessage(e.getLocalizedMessage()); |
|
234 |
} |
|
235 |
return cse; |
|
236 |
} |
|
237 | 167 |
|
238 |
private void startFileError(String message, HttpBundle bundle){ |
|
239 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class); |
|
240 |
viewIntent.putExtra("errorMessage", message); |
|
241 |
viewIntent.putExtra("response", bundle.getResponseText()); |
|
242 |
viewIntent.putExtra("request", bundle.getCurlRequest()); |
|
243 |
startActivity(viewIntent); |
|
244 |
} |
|
245 |
|
|
246 | 168 |
public class EnableCDNTask extends AsyncTask<Void, Void, HttpBundle> { |
247 | 169 |
private CloudServersException exception; |
248 | 170 |
|
... | ... | |
250 | 172 |
protected HttpBundle doInBackground(Void... arg0) { |
251 | 173 |
HttpBundle bundle = null; |
252 | 174 |
try { |
253 |
bundle = (new ContainerManager(context)).enable(containerName,
|
|
175 |
bundle = (new ContainerManager(getContext())).enable(containerName,
|
|
254 | 176 |
selectedTtlId, selectedLogRetId); |
255 | 177 |
} catch (CloudServersException e) { |
256 | 178 |
exception = e; |
... | ... | |
272 | 194 |
} else { |
273 | 195 |
CloudServersException cse = parseCloudServersException(response); |
274 | 196 |
if ("".equals(cse.getMessage())) { |
275 |
startFileError("There was a problem creating your container.", bundle);
|
|
197 |
showError("There was a problem creating your container.", bundle);
|
|
276 | 198 |
} else { |
277 |
startFileError("There was a problem creating your container: "
|
|
199 |
showError("There was a problem creating your container: "
|
|
278 | 200 |
+ cse.getMessage() |
279 | 201 |
+ " Check container name and try again", bundle); |
280 | 202 |
} |
281 | 203 |
} |
282 | 204 |
} else if (exception != null) { |
283 |
startFileError("There was a problem creating your container: "
|
|
205 |
showError("There was a problem creating your container: "
|
|
284 | 206 |
+ exception.getMessage() |
285 | 207 |
+ " Check container name and try again", bundle); |
286 | 208 |
} |
... | ... | |
294 | 216 |
protected HttpBundle doInBackground(Void... arg0) { |
295 | 217 |
HttpBundle bundle = null; |
296 | 218 |
try { |
297 |
bundle = (new ContainerManager(context)).disable(containerName,
|
|
219 |
bundle = (new ContainerManager(getContext())).disable(containerName,
|
|
298 | 220 |
selectedCdnId, selectedTtlId, selectedLogRetId); |
299 | 221 |
} catch (CloudServersException e) { |
300 | 222 |
exception = e; |
... | ... | |
313 | 235 |
} else { |
314 | 236 |
CloudServersException cse = parseCloudServersException(response); |
315 | 237 |
if ("".equals(cse.getMessage())) { |
316 |
startFileError("There was a problem creating your container.", bundle);
|
|
238 |
showError("There was a problem creating your container.", bundle);
|
|
317 | 239 |
} else { |
318 |
startFileError("There was a problem creating your container: "
|
|
240 |
showError("There was a problem creating your container: "
|
|
319 | 241 |
+ cse.getMessage() |
320 | 242 |
+ " Check container name and try again", bundle); |
321 | 243 |
} |
322 | 244 |
} |
323 | 245 |
} else if (exception != null) { |
324 |
startFileError("There was a problem creating your container: "
|
|
246 |
showError("There was a problem creating your container: "
|
|
325 | 247 |
+ exception.getMessage() |
326 | 248 |
+ " Check container name and try again", bundle); |
327 | 249 |
} |
Also available in: Unified diff