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