root / src / com / rackspacecloud / android / AddFileActivity.java @ 038ac9a4
History | View | Annotate | Download (6.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.files.api.client.ContainerObjectManager; |
19 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
20 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
21 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
22 |
|
23 |
import android.app.Activity; |
24 |
import android.app.AlertDialog; |
25 |
import android.app.ProgressDialog; |
26 |
import android.content.Context; |
27 |
import android.content.DialogInterface; |
28 |
import android.content.Intent; |
29 |
import android.os.AsyncTask; |
30 |
import android.os.Bundle; |
31 |
import android.view.View; |
32 |
import android.view.View.OnClickListener; |
33 |
import android.widget.Button; |
34 |
import android.widget.EditText; |
35 |
|
36 |
public class AddFileActivity extends GaActivity implements OnClickListener { |
37 |
|
38 |
private Context context; |
39 |
private EditText fileName;
|
40 |
private EditText contents;
|
41 |
private String containerName; |
42 |
private String path; |
43 |
private boolean isAdding; |
44 |
private ProgressDialog dialog;
|
45 |
|
46 |
/** Called when the activity is first created. */
|
47 |
@Override
|
48 |
public void onCreate(Bundle savedInstanceState) { |
49 |
super.onCreate(savedInstanceState);
|
50 |
trackPageView(PAGE_ADD_OBJECT); |
51 |
setContentView(R.layout.addtextfile); |
52 |
context = getApplicationContext(); |
53 |
containerName = (String) this.getIntent().getExtras().get("Cname"); |
54 |
path = (String) this.getIntent().getExtras().get("curPath"); |
55 |
setUpDialog(savedInstanceState); |
56 |
setUpInputs(); |
57 |
} |
58 |
|
59 |
private void setUpInputs() { |
60 |
((Button) findViewById(R.id.new_file_button)).setOnClickListener(this); |
61 |
fileName = ((EditText) findViewById(R.id.file_name_text)); |
62 |
fileName.append(".txt");
|
63 |
contents = ((EditText) findViewById(R.id.new_file_text)); |
64 |
} |
65 |
|
66 |
private void setUpDialog(Bundle savedInstanceState) { |
67 |
isAdding = savedInstanceState != null
|
68 |
&& savedInstanceState.containsKey("isAdding")
|
69 |
&& savedInstanceState.getBoolean("isAdding");
|
70 |
if (isAdding) {
|
71 |
showDialog(); |
72 |
} |
73 |
|
74 |
} |
75 |
|
76 |
@Override
|
77 |
protected void onSaveInstanceState(Bundle outState) { |
78 |
super.onSaveInstanceState(outState);
|
79 |
outState.putBoolean("isAdding", isAdding);
|
80 |
if (isAdding) {
|
81 |
hideDialog(); |
82 |
} |
83 |
} |
84 |
|
85 |
public void onClick(View arg0) { |
86 |
if ("".equals(fileName.getText().toString())) { |
87 |
showAlert("Required Fields Missing", " File name is required."); |
88 |
} else {
|
89 |
// showActivityIndicators();
|
90 |
trackEvent(CATEGORY_FILE, EVENT_CREATE, "", -1); |
91 |
new SaveFileTask().execute((Void[]) null); |
92 |
} |
93 |
} |
94 |
|
95 |
// using cloudServersException, it works for us too
|
96 |
private CloudServersException parseCloudServersException(
|
97 |
HttpResponse response) { |
98 |
CloudServersException cse = new CloudServersException();
|
99 |
try {
|
100 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
101 |
String body = responseHandler.handleResponse(response);
|
102 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
103 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
104 |
XMLReader xmlReader = saxParser.getXMLReader(); |
105 |
xmlReader.setContentHandler(parser); |
106 |
xmlReader.parse(new InputSource(new StringReader(body))); |
107 |
cse = parser.getException(); |
108 |
} catch (ClientProtocolException e) {
|
109 |
cse = new CloudServersException();
|
110 |
cse.setMessage(e.getLocalizedMessage()); |
111 |
} catch (IOException e) { |
112 |
cse = new CloudServersException();
|
113 |
cse.setMessage(e.getLocalizedMessage()); |
114 |
} catch (ParserConfigurationException e) { |
115 |
cse = new CloudServersException();
|
116 |
cse.setMessage(e.getLocalizedMessage()); |
117 |
} catch (SAXException e) {
|
118 |
cse = new CloudServersException();
|
119 |
cse.setMessage(e.getLocalizedMessage()); |
120 |
} catch (FactoryConfigurationError e) { |
121 |
cse = new CloudServersException();
|
122 |
cse.setMessage(e.getLocalizedMessage()); |
123 |
} |
124 |
return cse;
|
125 |
} |
126 |
|
127 |
private void showAlert(String title, String message) { |
128 |
try {
|
129 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
130 |
alert.setTitle(title); |
131 |
alert.setMessage(message); |
132 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
133 |
public void onClick(DialogInterface dialog, int which) { |
134 |
return;
|
135 |
} |
136 |
}); |
137 |
alert.show(); |
138 |
} catch (Exception e) { |
139 |
e.printStackTrace(); |
140 |
} |
141 |
} |
142 |
|
143 |
private void startFileError(String message, HttpBundle bundle) { |
144 |
Intent viewIntent = new Intent(getApplicationContext(),
|
145 |
ServerErrorActivity.class); |
146 |
viewIntent.putExtra("errorMessage", message);
|
147 |
viewIntent.putExtra("response", bundle.getResponseText());
|
148 |
viewIntent.putExtra("request", bundle.getCurlRequest());
|
149 |
startActivity(viewIntent); |
150 |
} |
151 |
|
152 |
private class SaveFileTask extends AsyncTask<Void, Void, HttpBundle> { |
153 |
private CloudServersException exception;
|
154 |
|
155 |
@Override
|
156 |
protected void onPreExecute() { |
157 |
isAdding = true;
|
158 |
showDialog(); |
159 |
} |
160 |
|
161 |
@Override
|
162 |
protected HttpBundle doInBackground(Void... arg0) { |
163 |
HttpBundle bundle = null;
|
164 |
try {
|
165 |
bundle = (new ContainerObjectManager(context)).addObject(
|
166 |
containerName, path, fileName.getText().toString(), |
167 |
"text/plain", contents.getText().toString());
|
168 |
} catch (CloudServersException e) {
|
169 |
exception = e; |
170 |
} |
171 |
return bundle;
|
172 |
} |
173 |
|
174 |
@Override
|
175 |
protected void onPostExecute(HttpBundle bundle) { |
176 |
isAdding = false;
|
177 |
hideDialog(); |
178 |
HttpResponse response = bundle.getResponse(); |
179 |
if (response != null) { |
180 |
int statusCode = response.getStatusLine().getStatusCode();
|
181 |
if (statusCode == 201) { |
182 |
setResult(Activity.RESULT_OK); |
183 |
finish(); |
184 |
} else {
|
185 |
CloudServersException cse = parseCloudServersException(response); |
186 |
if ("".equals(cse.getMessage())) { |
187 |
startFileError( |
188 |
"There was a problem creating your file.",
|
189 |
bundle); |
190 |
} else {
|
191 |
startFileError( |
192 |
"There was a problem creating your file: "
|
193 |
+ cse.getMessage() |
194 |
+ " Check file name and try again",
|
195 |
bundle); |
196 |
} |
197 |
} |
198 |
} else if (exception != null) { |
199 |
startFileError("There was a problem creating your file: "
|
200 |
+ exception.getMessage() |
201 |
+ " Check file name and try again", bundle);
|
202 |
} |
203 |
} |
204 |
} |
205 |
|
206 |
private void showDialog() { |
207 |
if (dialog == null || !dialog.isShowing()) { |
208 |
dialog = ProgressDialog.show(AddFileActivity.this, "",
|
209 |
"Adding File...", true); |
210 |
} |
211 |
} |
212 |
|
213 |
private void hideDialog() { |
214 |
if (dialog != null) { |
215 |
dialog.dismiss(); |
216 |
} |
217 |
} |
218 |
} |