root / src / com / rackspacecloud / android / AddContainerActivity.java @ b347d5e3
History | View | Annotate | Download (5.3 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 android.app.Activity; |
19 |
import android.app.AlertDialog; |
20 |
import android.content.Context; |
21 |
import android.content.DialogInterface; |
22 |
import android.os.AsyncTask; |
23 |
import android.os.Bundle; |
24 |
import android.view.View; |
25 |
import android.view.View.OnClickListener; |
26 |
import android.widget.Button; |
27 |
import android.widget.EditText; |
28 |
import android.widget.ProgressBar; |
29 |
import android.widget.TextView; |
30 |
|
31 |
import com.rackspace.cloud.files.api.client.ContainerManager; |
32 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
33 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
34 |
/**
|
35 |
*
|
36 |
* @author Phillip Toohill
|
37 |
*
|
38 |
*/
|
39 |
public class AddContainerActivity extends Activity implements OnClickListener { |
40 |
|
41 |
|
42 |
private EditText fileName;
|
43 |
private Context context; |
44 |
|
45 |
/** Called when the activity is first created. */
|
46 |
@Override
|
47 |
public void onCreate(Bundle savedInstanceState) { |
48 |
super.onCreate(savedInstanceState);
|
49 |
context = getApplicationContext(); |
50 |
setContentView(R.layout.createcontainer); |
51 |
fileName = (EditText) findViewById(R.id.container_name); |
52 |
((Button) findViewById(R.id.save_button)).setOnClickListener(this); |
53 |
} |
54 |
|
55 |
|
56 |
public void onClick(View arg0) { |
57 |
if ("".equals(fileName.getText().toString())) { |
58 |
showAlert("Required Fields Missing", " Container name is required."); |
59 |
} else {
|
60 |
showActivityIndicators(); |
61 |
new SaveFileTask().execute((Void[]) null); |
62 |
} |
63 |
} |
64 |
|
65 |
|
66 |
private void showAlert(String title, String message) { |
67 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
68 |
alert.setTitle(title); |
69 |
alert.setMessage(message); |
70 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
71 |
public void onClick(DialogInterface dialog, int which) { |
72 |
return;
|
73 |
} }); |
74 |
alert.show(); |
75 |
hideActivityIndicators(); |
76 |
} |
77 |
|
78 |
private void setActivityIndicatorsVisibility(int visibility) { |
79 |
ProgressBar pb = (ProgressBar) findViewById(R.id.save_container_progress_bar); |
80 |
TextView tv = (TextView) findViewById(R.id.saving_container_label); |
81 |
pb.setVisibility(visibility); |
82 |
tv.setVisibility(visibility); |
83 |
} |
84 |
|
85 |
private void showActivityIndicators() { |
86 |
setActivityIndicatorsVisibility(View.VISIBLE);
|
87 |
} |
88 |
|
89 |
private void hideActivityIndicators() { |
90 |
setActivityIndicatorsVisibility(View.INVISIBLE);
|
91 |
} |
92 |
//using cloudServersException, it works for us too
|
93 |
private CloudServersException parseCloudServersException(HttpResponse response) {
|
94 |
CloudServersException cse = new CloudServersException();
|
95 |
try {
|
96 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
97 |
String body = responseHandler.handleResponse(response);
|
98 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
99 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
100 |
XMLReader xmlReader = saxParser.getXMLReader(); |
101 |
xmlReader.setContentHandler(parser); |
102 |
xmlReader.parse(new InputSource(new StringReader(body))); |
103 |
cse = parser.getException(); |
104 |
} catch (ClientProtocolException e) {
|
105 |
cse = new CloudServersException();
|
106 |
cse.setMessage(e.getLocalizedMessage()); |
107 |
} catch (IOException e) { |
108 |
cse = new CloudServersException();
|
109 |
cse.setMessage(e.getLocalizedMessage()); |
110 |
} catch (ParserConfigurationException e) { |
111 |
cse = new CloudServersException();
|
112 |
cse.setMessage(e.getLocalizedMessage()); |
113 |
} catch (SAXException e) {
|
114 |
cse = new CloudServersException();
|
115 |
cse.setMessage(e.getLocalizedMessage()); |
116 |
} catch (FactoryConfigurationError e) { |
117 |
cse = new CloudServersException();
|
118 |
cse.setMessage(e.getLocalizedMessage()); |
119 |
} |
120 |
return cse;
|
121 |
} |
122 |
|
123 |
private class SaveFileTask extends AsyncTask<Void, Void, HttpResponse> { |
124 |
private CloudServersException exception;
|
125 |
|
126 |
@Override
|
127 |
protected HttpResponse doInBackground(Void... arg0) { |
128 |
HttpResponse resp = null;
|
129 |
try {
|
130 |
resp = (new ContainerManager(context)).create(fileName.getText());
|
131 |
} catch (CloudServersException e) {
|
132 |
exception = e; |
133 |
} |
134 |
return resp;
|
135 |
} |
136 |
|
137 |
@Override
|
138 |
protected void onPostExecute(HttpResponse response) { |
139 |
if (response != null) { |
140 |
int statusCode = response.getStatusLine().getStatusCode();
|
141 |
if (statusCode == 201) { |
142 |
setResult(Activity.RESULT_OK); |
143 |
finish(); |
144 |
} else {
|
145 |
CloudServersException cse = parseCloudServersException(response); |
146 |
if ("".equals(cse.getMessage())) { |
147 |
showAlert("Error", "There was a problem creating your container."); |
148 |
} else {
|
149 |
showAlert("Error", "There was a problem creating your container: " + cse.getMessage() + " Check container name and try again"); |
150 |
} |
151 |
} |
152 |
} else if (exception != null) { |
153 |
showAlert("Error", "There was a problem creating your container: " + exception.getMessage()+" Check container name and try again"); |
154 |
} |
155 |
} |
156 |
} |
157 |
|
158 |
|
159 |
|
160 |
} |