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