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