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