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