root / src / com / rackspacecloud / android / AddFileActivity.java @ 4f9d1a69
History | View | Annotate | Download (6.6 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 Activity 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 |
setContentView(R.layout.addtextfile); |
51 |
context = getApplicationContext(); |
52 |
containerName = (String) this.getIntent().getExtras().get("Cname"); |
53 |
path = (String) this.getIntent().getExtras().get("curPath"); |
54 |
setUpDialog(savedInstanceState); |
55 |
setUpInputs(); |
56 |
} |
57 |
|
58 |
private void setUpInputs(){ |
59 |
((Button) findViewById(R.id.new_file_button)).setOnClickListener(this); |
60 |
fileName = ((EditText)findViewById(R.id.file_name_text)); |
61 |
fileName.append(".txt");
|
62 |
contents = ((EditText)findViewById(R.id.new_file_text)); |
63 |
} |
64 |
|
65 |
private void setUpDialog(Bundle savedInstanceState){ |
66 |
isAdding = savedInstanceState != null && savedInstanceState.containsKey("isAdding") |
67 |
&& savedInstanceState.getBoolean("isAdding");
|
68 |
if(isAdding){
|
69 |
showDialog(); |
70 |
} |
71 |
|
72 |
} |
73 |
|
74 |
@Override
|
75 |
protected void onSaveInstanceState(Bundle outState) { |
76 |
super.onSaveInstanceState(outState);
|
77 |
outState.putBoolean("isAdding", isAdding);
|
78 |
if(isAdding){
|
79 |
hideDialog(); |
80 |
} |
81 |
} |
82 |
|
83 |
public void onClick(View arg0) { |
84 |
if ("".equals(fileName.getText().toString())) { |
85 |
showAlert("Required Fields Missing", " File name is required."); |
86 |
} else {
|
87 |
//showActivityIndicators();
|
88 |
new SaveFileTask().execute((Void[]) null); |
89 |
} |
90 |
} |
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 void showAlert(String title, String message) { |
124 |
try {
|
125 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
126 |
alert.setTitle(title); |
127 |
alert.setMessage(message); |
128 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
129 |
public void onClick(DialogInterface dialog, int which) { |
130 |
return;
|
131 |
} }); |
132 |
alert.show(); |
133 |
} catch (Exception e) { |
134 |
e.printStackTrace(); |
135 |
} |
136 |
} |
137 |
|
138 |
private void startFileError(String message, HttpBundle bundle){ |
139 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
|
140 |
viewIntent.putExtra("errorMessage", message);
|
141 |
viewIntent.putExtra("response", bundle.getResponseText());
|
142 |
viewIntent.putExtra("request", bundle.getCurlRequest());
|
143 |
startActivity(viewIntent); |
144 |
} |
145 |
|
146 |
private class SaveFileTask extends AsyncTask<Void, Void, HttpBundle> { |
147 |
private CloudServersException exception;
|
148 |
|
149 |
@Override
|
150 |
protected void onPreExecute(){ |
151 |
isAdding = true;
|
152 |
showDialog(); |
153 |
} |
154 |
|
155 |
@Override
|
156 |
protected HttpBundle doInBackground(Void... arg0) { |
157 |
HttpBundle bundle = null;
|
158 |
try {
|
159 |
bundle = (new ContainerObjectManager(context)).addObject(containerName, path, fileName.getText().toString(), "text/plain", contents.getText().toString()); |
160 |
} catch (CloudServersException e) {
|
161 |
exception = e; |
162 |
} |
163 |
return bundle;
|
164 |
} |
165 |
|
166 |
@Override
|
167 |
protected void onPostExecute(HttpBundle bundle) { |
168 |
isAdding = false;
|
169 |
hideDialog(); |
170 |
HttpResponse response = bundle.getResponse(); |
171 |
if (response != null) { |
172 |
int statusCode = response.getStatusLine().getStatusCode();
|
173 |
if (statusCode == 201) { |
174 |
setResult(Activity.RESULT_OK); |
175 |
finish(); |
176 |
} else {
|
177 |
CloudServersException cse = parseCloudServersException(response); |
178 |
if ("".equals(cse.getMessage())) { |
179 |
startFileError("There was a problem creating your file.", bundle);
|
180 |
} else {
|
181 |
startFileError("There was a problem creating your file: " + cse.getMessage() + " See details for more information", bundle); |
182 |
} |
183 |
} |
184 |
} else if (exception != null) { |
185 |
startFileError("There was a problem creating your file: " + exception.getMessage()+ " See details for more information", bundle); |
186 |
} |
187 |
} |
188 |
} |
189 |
|
190 |
private void showDialog() { |
191 |
if(dialog == null || !dialog.isShowing()){ |
192 |
dialog = ProgressDialog.show(AddFileActivity.this, "", "Adding File...", true); |
193 |
} |
194 |
} |
195 |
|
196 |
private void hideDialog() { |
197 |
if(dialog != null){ |
198 |
dialog.dismiss(); |
199 |
} |
200 |
} |
201 |
} |