root / src / com / rackspacecloud / android / ContainerObjectsActivity.java @ 240418be
History | View | Annotate | Download (11.4 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
5 |
import java.util.ArrayList; |
6 |
|
7 |
import javax.xml.parsers.FactoryConfigurationError; |
8 |
import javax.xml.parsers.ParserConfigurationException; |
9 |
import javax.xml.parsers.SAXParser; |
10 |
import javax.xml.parsers.SAXParserFactory; |
11 |
|
12 |
import org.apache.http.HttpResponse; |
13 |
import org.apache.http.client.ClientProtocolException; |
14 |
import org.apache.http.impl.client.BasicResponseHandler; |
15 |
import org.xml.sax.InputSource; |
16 |
import org.xml.sax.SAXException; |
17 |
import org.xml.sax.XMLReader; |
18 |
|
19 |
import android.app.Activity; |
20 |
import android.app.AlertDialog; |
21 |
import android.app.Dialog; |
22 |
import android.app.ListActivity; |
23 |
import android.content.DialogInterface; |
24 |
import android.content.Intent; |
25 |
import android.os.AsyncTask; |
26 |
import android.os.Bundle; |
27 |
import android.util.Log; |
28 |
import android.view.LayoutInflater; |
29 |
import android.view.Menu; |
30 |
import android.view.MenuInflater; |
31 |
import android.view.MenuItem; |
32 |
import android.view.View; |
33 |
import android.view.ViewGroup; |
34 |
import android.widget.ArrayAdapter; |
35 |
import android.widget.ListView; |
36 |
import android.widget.TextView; |
37 |
|
38 |
import com.rackspace.cloud.files.api.client.Container; |
39 |
import com.rackspace.cloud.files.api.client.ContainerManager; |
40 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
41 |
import com.rackspace.cloud.files.api.client.ContainerObjects; |
42 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
43 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
44 |
|
45 |
/**
|
46 |
*
|
47 |
* @author Phillip Toohill
|
48 |
*
|
49 |
*/
|
50 |
public class ContainerObjectsActivity extends ListActivity { |
51 |
|
52 |
private static final int deleteContainer = 0; |
53 |
private ContainerObjects[] files; |
54 |
private static Container container; |
55 |
public String LOG = "viewFilesActivity"; |
56 |
private String cdnEnabledIs; |
57 |
public Object megaBytes; |
58 |
public Object kiloBytes; |
59 |
public int bConver = 1048576; |
60 |
public int kbConver = 1024; |
61 |
|
62 |
@Override
|
63 |
public void onCreate(Bundle savedInstanceState) { |
64 |
super.onCreate(savedInstanceState);
|
65 |
container = (Container) this.getIntent().getExtras().get("container"); |
66 |
if (container.isCdnEnabled() == true) { |
67 |
cdnEnabledIs = "true";
|
68 |
} else {
|
69 |
cdnEnabledIs = "false";
|
70 |
} |
71 |
restoreState(savedInstanceState); |
72 |
} |
73 |
|
74 |
@Override
|
75 |
protected void onSaveInstanceState(Bundle outState) { |
76 |
super.onSaveInstanceState(outState);
|
77 |
outState.putSerializable("container", files);
|
78 |
} |
79 |
|
80 |
private void restoreState(Bundle state) { |
81 |
if (state != null && state.containsKey("container")) { |
82 |
files = (ContainerObjects[]) state.getSerializable("container"); |
83 |
if (files.length == 0) { |
84 |
displayNoServersCell(); |
85 |
} else {
|
86 |
getListView().setDividerHeight(1); // restore divider lines |
87 |
setListAdapter(new FileAdapter());
|
88 |
|
89 |
} |
90 |
} else {
|
91 |
loadFiles(); |
92 |
|
93 |
} |
94 |
} |
95 |
|
96 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
97 |
if (files != null && files.length > 0) { |
98 |
Intent viewIntent = new Intent(this, ContainerObjectDetails.class); |
99 |
viewIntent.putExtra("container", files[position]);
|
100 |
viewIntent.putExtra("cdnUrl", container.getCdnUrl());
|
101 |
viewIntent.putExtra("containerNames", container.getName());
|
102 |
viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
|
103 |
startActivityForResult(viewIntent, 55); // arbitrary number; never |
104 |
// used again
|
105 |
} |
106 |
} |
107 |
|
108 |
private void loadFiles() { |
109 |
displayLoadingCell(); |
110 |
new LoadFilesTask().execute((Void[]) null); |
111 |
|
112 |
} |
113 |
|
114 |
private void setFileList(ArrayList<ContainerObjects> files) { |
115 |
if (files == null) { |
116 |
files = new ArrayList<ContainerObjects>(); |
117 |
} |
118 |
String[] fileNames = new String[files.size()]; |
119 |
this.files = new ContainerObjects[files.size()]; |
120 |
|
121 |
if (files != null) { |
122 |
for (int i = 0; i < files.size(); i++) { |
123 |
ContainerObjects file = files.get(i); |
124 |
this.files[i] = file;
|
125 |
fileNames[i] = file.getName(); |
126 |
} |
127 |
} |
128 |
|
129 |
if (fileNames.length == 0) { |
130 |
displayNoServersCell(); |
131 |
} else {
|
132 |
getListView().setDividerHeight(1); // restore divider lines |
133 |
setListAdapter(new FileAdapter());
|
134 |
} |
135 |
} |
136 |
|
137 |
private void displayLoadingCell() { |
138 |
String a[] = new String[1]; |
139 |
a[0] = "Loading..."; |
140 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell, |
141 |
R.id.loading_label, a)); |
142 |
getListView().setTextFilterEnabled(true);
|
143 |
getListView().setDividerHeight(0); // hide the dividers so it won't look |
144 |
// like a list row
|
145 |
getListView().setItemsCanFocus(false);
|
146 |
} |
147 |
|
148 |
private void displayNoServersCell() { |
149 |
String a[] = new String[1]; |
150 |
a[0] = "No Files"; |
151 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.noobjectscell, |
152 |
R.id.no_files_label, a)); |
153 |
getListView().setTextFilterEnabled(true);
|
154 |
getListView().setDividerHeight(0); // hide the dividers so it won't look |
155 |
// like a list row
|
156 |
getListView().setItemsCanFocus(false);
|
157 |
} |
158 |
|
159 |
private void showAlert(String title, String message) { |
160 |
// Can't create handler inside thread that has not called
|
161 |
// Looper.prepare()
|
162 |
// Looper.prepare();
|
163 |
try {
|
164 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
165 |
alert.setTitle(title); |
166 |
alert.setMessage(message); |
167 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
168 |
public void onClick(DialogInterface dialog, int which) { |
169 |
return;
|
170 |
} |
171 |
}); |
172 |
alert.show(); |
173 |
} catch (Exception e) { |
174 |
e.printStackTrace(); |
175 |
} |
176 |
} |
177 |
|
178 |
private class LoadFilesTask extends |
179 |
AsyncTask<Void, Void, ArrayList<ContainerObjects>> { |
180 |
|
181 |
private CloudServersException exception;
|
182 |
|
183 |
@Override
|
184 |
protected ArrayList<ContainerObjects> doInBackground(Void... arg0) { |
185 |
ArrayList<ContainerObjects> files = null; |
186 |
try {
|
187 |
files = (new ContainerObjectManager()).createList(true, |
188 |
container.getName()); |
189 |
} catch (CloudServersException e) {
|
190 |
exception = e; |
191 |
e.printStackTrace(); |
192 |
} |
193 |
return files;
|
194 |
} |
195 |
|
196 |
@Override
|
197 |
protected void onPostExecute(ArrayList<ContainerObjects> result) { |
198 |
if (exception != null) { |
199 |
showAlert("Error", exception.getMessage());
|
200 |
} |
201 |
setFileList(result); |
202 |
} |
203 |
} |
204 |
|
205 |
class FileAdapter extends ArrayAdapter<ContainerObjects> { |
206 |
FileAdapter() { |
207 |
super(ContainerObjectsActivity.this,
|
208 |
R.layout.listcontainerobjectcell, files); |
209 |
} |
210 |
|
211 |
public View getView(int position, View convertView, ViewGroup parent) { |
212 |
|
213 |
ContainerObjects file = files[position]; |
214 |
LayoutInflater inflater = getLayoutInflater(); |
215 |
View row = inflater.inflate(R.layout.listcontainerobjectcell,
|
216 |
parent, false);
|
217 |
|
218 |
TextView label = (TextView) row.findViewById(R.id.label); |
219 |
label.setText(file.getCName()); |
220 |
|
221 |
if (file.getBytes() >= bConver) {
|
222 |
megaBytes = Math.abs(file.getBytes() / bConver + 0.2); |
223 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
224 |
sublabel.setText(megaBytes + " MB");
|
225 |
} else if (file.getBytes() >= kbConver) { |
226 |
kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2); |
227 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
228 |
sublabel.setText(kiloBytes + " KB");
|
229 |
} else {
|
230 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
231 |
sublabel.setText(file.getBytes() + " B");
|
232 |
} |
233 |
|
234 |
return (row);
|
235 |
} |
236 |
} |
237 |
|
238 |
// Create the Menu options
|
239 |
@Override
|
240 |
public boolean onCreateOptionsMenu(Menu menu) { |
241 |
super.onCreateOptionsMenu(menu);
|
242 |
MenuInflater inflater = getMenuInflater(); |
243 |
inflater.inflate(R.menu.view_container_object_list_menu, menu); |
244 |
return true; |
245 |
} |
246 |
|
247 |
@Override
|
248 |
public boolean onOptionsItemSelected(MenuItem item) { |
249 |
switch (item.getItemId()) {
|
250 |
case R.id.delete_container:
|
251 |
showDialog(deleteContainer); |
252 |
return true; |
253 |
case R.id.enable_cdn:
|
254 |
Intent viewIntent1 = new Intent(this, EnableCDNActivity.class); |
255 |
viewIntent1.putExtra("Cname", container.getName());
|
256 |
startActivityForResult(viewIntent1, 56);
|
257 |
return true; |
258 |
case R.id.refresh:
|
259 |
loadFiles(); |
260 |
return true; |
261 |
} |
262 |
return false; |
263 |
} |
264 |
|
265 |
@Override
|
266 |
protected Dialog onCreateDialog(int id) { |
267 |
switch (id) {
|
268 |
case deleteContainer:
|
269 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
270 |
.setIcon(R.drawable.alert_dialog_icon) |
271 |
.setTitle("Delete Container")
|
272 |
.setMessage( |
273 |
"Are you sure you want to delete this Container?")
|
274 |
.setPositiveButton("Delete Container",
|
275 |
new DialogInterface.OnClickListener() {
|
276 |
public void onClick(DialogInterface dialog, |
277 |
int whichButton) {
|
278 |
// User clicked OK so do some stuff
|
279 |
new DeleteContainerTask()
|
280 |
.execute((Void[]) null); |
281 |
} |
282 |
}) |
283 |
.setNegativeButton("Cancel",
|
284 |
new DialogInterface.OnClickListener() {
|
285 |
public void onClick(DialogInterface dialog, |
286 |
int whichButton) {
|
287 |
// User clicked Cancel so do some stuff
|
288 |
} |
289 |
}).create(); |
290 |
|
291 |
} |
292 |
return null; |
293 |
} |
294 |
|
295 |
@Override
|
296 |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
297 |
super.onActivityResult(requestCode, resultCode, data);
|
298 |
|
299 |
if (resultCode == RESULT_OK) {
|
300 |
// a sub-activity kicked back, so we want to refresh the server list
|
301 |
loadFiles(); |
302 |
} |
303 |
if (requestCode == 56) { |
304 |
if (resultCode == RESULT_OK) {
|
305 |
Intent viewIntent1 = new Intent(this, ListContainerActivity.class); |
306 |
startActivityForResult(viewIntent1, 56);
|
307 |
} |
308 |
} |
309 |
} |
310 |
|
311 |
private CloudServersException parseCloudServersException(
|
312 |
HttpResponse response) { |
313 |
CloudServersException cse = new CloudServersException();
|
314 |
try {
|
315 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
316 |
String body = responseHandler.handleResponse(response);
|
317 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
318 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
319 |
XMLReader xmlReader = saxParser.getXMLReader(); |
320 |
xmlReader.setContentHandler(parser); |
321 |
xmlReader.parse(new InputSource(new StringReader(body))); |
322 |
cse = parser.getException(); |
323 |
} catch (ClientProtocolException e) {
|
324 |
cse = new CloudServersException();
|
325 |
cse.setMessage(e.getLocalizedMessage()); |
326 |
} catch (IOException e) { |
327 |
cse = new CloudServersException();
|
328 |
cse.setMessage(e.getLocalizedMessage()); |
329 |
} catch (ParserConfigurationException e) { |
330 |
cse = new CloudServersException();
|
331 |
cse.setMessage(e.getLocalizedMessage()); |
332 |
} catch (SAXException e) {
|
333 |
cse = new CloudServersException();
|
334 |
cse.setMessage(e.getLocalizedMessage()); |
335 |
} catch (FactoryConfigurationError e) { |
336 |
cse = new CloudServersException();
|
337 |
cse.setMessage(e.getLocalizedMessage()); |
338 |
} |
339 |
return cse;
|
340 |
} |
341 |
|
342 |
private class DeleteContainerTask extends |
343 |
AsyncTask<Void, Void, HttpResponse> { |
344 |
|
345 |
private CloudServersException exception;
|
346 |
|
347 |
@Override
|
348 |
protected HttpResponse doInBackground(Void... arg0) { |
349 |
HttpResponse resp = null;
|
350 |
try {
|
351 |
resp = (new ContainerManager()).delete(container.getName());
|
352 |
Log.v(LOG, "container's name " + container.getName());
|
353 |
} catch (CloudServersException e) {
|
354 |
exception = e; |
355 |
} |
356 |
return resp;
|
357 |
} |
358 |
|
359 |
@Override
|
360 |
protected void onPostExecute(HttpResponse response) { |
361 |
if (response != null) { |
362 |
int statusCode = response.getStatusLine().getStatusCode();
|
363 |
if (statusCode == 409) { |
364 |
showAlert("Error",
|
365 |
"Container must be empty in order to delete");
|
366 |
} |
367 |
if (statusCode == 204) { |
368 |
setResult(Activity.RESULT_OK); |
369 |
finish(); |
370 |
|
371 |
} else {
|
372 |
CloudServersException cse = parseCloudServersException(response); |
373 |
if ("".equals(cse.getMessage())) { |
374 |
showAlert("Error",
|
375 |
"There was a problem deleting your container.");
|
376 |
} else {
|
377 |
showAlert("Error",
|
378 |
"There was a problem deleting your container: "
|
379 |
+ cse.getMessage()); |
380 |
} |
381 |
} |
382 |
} else if (exception != null) { |
383 |
showAlert("Error", "There was a problem deleting your server: " |
384 |
+ exception.getMessage()); |
385 |
} |
386 |
} |
387 |
} |
388 |
|
389 |
} |