root / src / com / rackspacecloud / android / ContainerObjectsActivity.java @ b2a2d2f1
History | View | Annotate | Download (21.5 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
import java.util.Arrays; |
5 |
|
6 |
import org.apache.http.HttpResponse; |
7 |
|
8 |
import android.app.Activity; |
9 |
import android.app.AlertDialog; |
10 |
import android.app.Dialog; |
11 |
import android.content.DialogInterface; |
12 |
import android.content.Intent; |
13 |
import android.os.AsyncTask; |
14 |
import android.os.Bundle; |
15 |
import android.view.LayoutInflater; |
16 |
import android.view.Menu; |
17 |
import android.view.MenuInflater; |
18 |
import android.view.MenuItem; |
19 |
import android.view.View; |
20 |
import android.view.ViewGroup; |
21 |
import android.widget.ArrayAdapter; |
22 |
import android.widget.EditText; |
23 |
import android.widget.ListView; |
24 |
import android.widget.TextView; |
25 |
|
26 |
import com.rackspace.cloud.files.api.client.Container; |
27 |
import com.rackspace.cloud.files.api.client.ContainerManager; |
28 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
29 |
import com.rackspace.cloud.files.api.client.ContainerObjects; |
30 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
31 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
32 |
|
33 |
/**
|
34 |
*
|
35 |
* @author Phillip Toohill
|
36 |
*
|
37 |
*/
|
38 |
|
39 |
public class ContainerObjectsActivity extends CloudListActivity { |
40 |
|
41 |
private static final int deleteContainer = 0; |
42 |
private static final int deleteFolder = 1; |
43 |
|
44 |
private ContainerObjects[] files; |
45 |
private static Container container; |
46 |
public String LOG = "viewFilesActivity"; |
47 |
private String cdnEnabledIs; |
48 |
public Object megaBytes; |
49 |
public Object kiloBytes; |
50 |
public int bConver = 1048576; |
51 |
public int kbConver = 1024; |
52 |
private boolean loadingFiles; |
53 |
private String currentPath; |
54 |
private AndroidCloudApplication app;
|
55 |
private AddObjectListenerTask task;
|
56 |
private DeleteObjectListenerTask deleteObjTask;
|
57 |
private DeleteContainerListenerTask deleteContainerTask;
|
58 |
|
59 |
@Override
|
60 |
public void onCreate(Bundle savedInstanceState) { |
61 |
super.onCreate(savedInstanceState);
|
62 |
trackPageView(PAGE_FOLDER); |
63 |
container = (Container) this.getIntent().getExtras().get("container"); |
64 |
if (container.isCdnEnabled() == true) { |
65 |
cdnEnabledIs = "true";
|
66 |
} else {
|
67 |
cdnEnabledIs = "false";
|
68 |
} |
69 |
restoreState(savedInstanceState); |
70 |
} |
71 |
|
72 |
@Override
|
73 |
protected void onSaveInstanceState(Bundle outState) { |
74 |
super.onSaveInstanceState(outState);
|
75 |
|
76 |
//files stores all the files in the container
|
77 |
outState.putSerializable("container", files);
|
78 |
|
79 |
//current path represents where you have "navigated" to
|
80 |
outState.putString("path", currentPath);
|
81 |
|
82 |
outState.putBoolean("loadingFiles", loadingFiles);
|
83 |
} |
84 |
|
85 |
|
86 |
|
87 |
protected void restoreState(Bundle state) { |
88 |
super.restoreState(state);
|
89 |
|
90 |
/*
|
91 |
* need reference to the app so you can access curDirFiles
|
92 |
* as well as processing status
|
93 |
*/
|
94 |
app = (AndroidCloudApplication)this.getApplication();
|
95 |
|
96 |
if(state != null){ |
97 |
if(state.containsKey("path")){ |
98 |
currentPath = state.getString("path");
|
99 |
} |
100 |
else{
|
101 |
currentPath = "";
|
102 |
} |
103 |
|
104 |
if(state.containsKey("loadingFiles") && state.getBoolean("loadingFiles")){ |
105 |
loadFiles(); |
106 |
} |
107 |
else{
|
108 |
if(state.containsKey("container")){ |
109 |
files = (ContainerObjects[]) state.getSerializable("container"); |
110 |
if (app.getCurFiles() == null) { |
111 |
displayNoFilesCell(); |
112 |
} else {
|
113 |
getListView().setDividerHeight(1); // restore divider lines |
114 |
setListAdapter(new FileAdapter());
|
115 |
|
116 |
} |
117 |
} |
118 |
} |
119 |
} |
120 |
else {
|
121 |
currentPath = "";
|
122 |
loadFiles(); |
123 |
} |
124 |
|
125 |
/*
|
126 |
* if the app is process when we enter the activity
|
127 |
* we must listen for the new curDirFiles list
|
128 |
*/
|
129 |
if(app.isAddingObject()){
|
130 |
task = new AddObjectListenerTask();
|
131 |
task.execute(); |
132 |
} |
133 |
|
134 |
if(app.isDeletingObject()){
|
135 |
displayNoFilesCell(); |
136 |
deleteObjTask = new DeleteObjectListenerTask();
|
137 |
deleteObjTask.execute(); |
138 |
} |
139 |
|
140 |
if(app.isDeletingContainer()){
|
141 |
displayNoFilesCell(); |
142 |
deleteContainerTask = new DeleteContainerListenerTask();
|
143 |
deleteContainerTask.execute(); |
144 |
} |
145 |
|
146 |
|
147 |
} |
148 |
|
149 |
@Override
|
150 |
protected void onStop(){ |
151 |
super.onStop();
|
152 |
|
153 |
/*
|
154 |
* Need to stop running listener task
|
155 |
* if we exit
|
156 |
*/
|
157 |
if(task != null){ |
158 |
task.cancel(true);
|
159 |
} |
160 |
|
161 |
if(deleteObjTask != null){ |
162 |
deleteObjTask.cancel(true);
|
163 |
} |
164 |
|
165 |
if(deleteContainerTask != null){ |
166 |
deleteContainerTask.cancel(true);
|
167 |
} |
168 |
|
169 |
} |
170 |
|
171 |
/*
|
172 |
* overriding back button press, because we are not actually changing
|
173 |
* activities when we navigate the file structure
|
174 |
*/
|
175 |
public void onBackPressed() { |
176 |
if(currentPath.equals("")){ |
177 |
finish(); |
178 |
} |
179 |
else{
|
180 |
goUpDirectory(); |
181 |
} |
182 |
} |
183 |
|
184 |
/*
|
185 |
* go to the current directory's parent and display that data
|
186 |
*/
|
187 |
private void goUpDirectory(){ |
188 |
currentPath = currentPath.substring(0, currentPath.substring(0, currentPath.length()-2).lastIndexOf("/")+1); |
189 |
loadCurrentDirectoryFiles(); |
190 |
displayCurrentFiles(); |
191 |
} |
192 |
|
193 |
/*
|
194 |
* load all file that are in the container
|
195 |
*/
|
196 |
private void loadFiles() { |
197 |
//displayLoadingCell();
|
198 |
new LoadFilesTask().execute();
|
199 |
} |
200 |
|
201 |
|
202 |
/* load only the files that should display for the
|
203 |
* current directory in the curDirFiles[]
|
204 |
*/
|
205 |
private void loadCurrentDirectoryFiles(){ |
206 |
ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>(); |
207 |
|
208 |
if(files != null){ |
209 |
for(int i = 0 ; i < files.length; i ++){ |
210 |
if(fileBelongsInDir(files[i])){
|
211 |
curFiles.add(files[i]); |
212 |
} |
213 |
} |
214 |
app.setCurFiles(curFiles); |
215 |
} |
216 |
} |
217 |
|
218 |
/*
|
219 |
* determines if a file should be displayed in current
|
220 |
* directory
|
221 |
*/
|
222 |
private Boolean fileBelongsInDir(ContainerObjects obj){ |
223 |
String objPath = obj.getCName();
|
224 |
if(!objPath.startsWith(currentPath)){
|
225 |
return false; |
226 |
} |
227 |
else{
|
228 |
objPath = objPath.substring(currentPath.length()); |
229 |
return !objPath.contains("/"); |
230 |
} |
231 |
} |
232 |
|
233 |
|
234 |
/*
|
235 |
* loads all the files that are in the container
|
236 |
* into one array
|
237 |
*/
|
238 |
private void setFileList(ArrayList<ContainerObjects> files) { |
239 |
if (files == null) { |
240 |
files = new ArrayList<ContainerObjects>(); |
241 |
} |
242 |
String[] fileNames = new String[files.size()]; |
243 |
this.files = new ContainerObjects[files.size()]; |
244 |
|
245 |
|
246 |
|
247 |
if (files != null) { |
248 |
for (int i = 0; i < files.size(); i++) { |
249 |
ContainerObjects file = files.get(i); |
250 |
this.files[i] = file;
|
251 |
fileNames[i] = file.getName(); |
252 |
} |
253 |
} |
254 |
} |
255 |
|
256 |
private void displayCurrentFiles(){ |
257 |
if (app.getCurFiles().size() == 0) { |
258 |
displayNoFilesCell(); |
259 |
} else {
|
260 |
ArrayList<ContainerObjects> tempList = new ArrayList<ContainerObjects>(); |
261 |
for(int i = 0; i < app.getCurFiles().size(); i++){ |
262 |
tempList.add(app.getCurFiles().get(i)); |
263 |
} |
264 |
getListView().setDividerHeight(1); // restore divider lines |
265 |
setListAdapter(new FileAdapter());
|
266 |
} |
267 |
} |
268 |
|
269 |
/*
|
270 |
* display a different empty page depending
|
271 |
* of if you are at top of container or
|
272 |
* in a folder
|
273 |
*/
|
274 |
private void displayNoFilesCell() { |
275 |
String a[] = new String[1]; |
276 |
if(currentPath.equals("")){ |
277 |
a[0] = "Empty Container"; |
278 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.noobjectscell, |
279 |
R.id.no_files_label, a)); |
280 |
} |
281 |
else{
|
282 |
a[0] = "No Files"; |
283 |
setListAdapter(new ArrayAdapter<String>(this, R.layout.nofilescell, |
284 |
R.id.no_files_label, a)); |
285 |
} |
286 |
getListView().setTextFilterEnabled(true);
|
287 |
getListView().setDividerHeight(0); // hide the dividers so it won't look |
288 |
// like a list row
|
289 |
getListView().setItemsCanFocus(false);
|
290 |
} |
291 |
|
292 |
/* just get the last part of the filename
|
293 |
* so the entire file path does not show
|
294 |
*/
|
295 |
private String getShortName(String longName){ |
296 |
String s = longName;
|
297 |
if(!s.contains("/")){ |
298 |
return s;
|
299 |
} |
300 |
else {
|
301 |
return s.substring(s.lastIndexOf('/')+1); |
302 |
} |
303 |
} |
304 |
|
305 |
/*
|
306 |
* removed a specified object from the array of
|
307 |
* all the files in the container
|
308 |
*/
|
309 |
private void removeFromList(String path){ |
310 |
ArrayList<ContainerObjects> temp = new ArrayList<ContainerObjects>(Arrays.asList(files)); |
311 |
for(int i = 0; i < files.length; i++){ |
312 |
if(files[i].getCName().equals(path.substring(0, path.length()-1))){ |
313 |
temp.remove(i); |
314 |
} |
315 |
} |
316 |
files = new ContainerObjects[temp.size()];
|
317 |
for(int i = 0; i < temp.size(); i++){ |
318 |
files[i] = temp.get(i); |
319 |
} |
320 |
} |
321 |
|
322 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
323 |
if (app.getCurFiles() != null && app.getCurFiles().size() > 0) { |
324 |
Intent viewIntent; |
325 |
if(app.getCurFiles().get(position).getContentType().equals("application/directory")){ |
326 |
currentPath = app.getCurFiles().get(position).getCName() + "/";
|
327 |
loadCurrentDirectoryFiles(); |
328 |
displayCurrentFiles(); |
329 |
} |
330 |
|
331 |
else{
|
332 |
viewIntent = new Intent(this, ContainerObjectDetails.class); |
333 |
viewIntent.putExtra("container", app.getCurFiles().get(position));
|
334 |
viewIntent.putExtra("cdnUrl", container.getCdnUrl());
|
335 |
viewIntent.putExtra("containerNames", container.getName());
|
336 |
viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
|
337 |
startActivityForResult(viewIntent, 55); // arbitrary number; never |
338 |
// used again
|
339 |
} |
340 |
} |
341 |
} |
342 |
|
343 |
/*
|
344 |
* Create the Menu options
|
345 |
*/
|
346 |
@Override
|
347 |
public boolean onCreateOptionsMenu(Menu menu) { |
348 |
super.onCreateOptionsMenu(menu);
|
349 |
MenuInflater inflater = getMenuInflater(); |
350 |
inflater.inflate(R.menu.view_container_object_list_menu, menu); |
351 |
return true; |
352 |
} |
353 |
|
354 |
@Override
|
355 |
/*
|
356 |
* option performed for delete depends on if you
|
357 |
* are at the top of a container or in a folder
|
358 |
*/
|
359 |
public boolean onOptionsItemSelected(MenuItem item) { |
360 |
switch (item.getItemId()) {
|
361 |
case R.id.delete_container:
|
362 |
if(currentPath.equals("")){ |
363 |
showDialog(deleteContainer); |
364 |
} |
365 |
else{
|
366 |
showDialog(deleteFolder); |
367 |
} |
368 |
return true; |
369 |
case R.id.enable_cdn:
|
370 |
Intent viewIntent1 = new Intent(this, EnableCDNActivity.class); |
371 |
viewIntent1.putExtra("Cname", container.getName());
|
372 |
startActivityForResult(viewIntent1, 56);
|
373 |
return true; |
374 |
case R.id.refresh:
|
375 |
loadFiles(); |
376 |
return true; |
377 |
case R.id.add_folder:
|
378 |
showDialog(R.id.add_folder); |
379 |
return true; |
380 |
case R.id.add_file:
|
381 |
Intent viewIntent2 = new Intent(this, AddFileActivity.class); |
382 |
viewIntent2.putExtra("Cname", container.getName());
|
383 |
viewIntent2.putExtra("curPath", currentPath);
|
384 |
startActivityForResult(viewIntent2, 56);
|
385 |
return true; |
386 |
} |
387 |
return false; |
388 |
} |
389 |
|
390 |
@Override
|
391 |
protected Dialog onCreateDialog(int id) { |
392 |
switch (id) {
|
393 |
case deleteContainer:
|
394 |
if(app.getCurFiles().size() == 0){ |
395 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
396 |
.setIcon(R.drawable.alert_dialog_icon) |
397 |
.setTitle("Delete Container")
|
398 |
.setMessage( |
399 |
"Are you sure you want to delete this Container?")
|
400 |
.setPositiveButton("Delete Container",
|
401 |
new DialogInterface.OnClickListener() {
|
402 |
public void onClick(DialogInterface dialog, |
403 |
int whichButton) {
|
404 |
// User clicked OK so do some stuff
|
405 |
trackEvent(CATEGORY_CONTAINER, EVENT_DELETE, "", -1); |
406 |
new DeleteContainerTask()
|
407 |
.execute(currentPath); |
408 |
} |
409 |
}) |
410 |
.setNegativeButton("Cancel",
|
411 |
new DialogInterface.OnClickListener() {
|
412 |
public void onClick(DialogInterface dialog, |
413 |
int whichButton) {
|
414 |
// User clicked Cancel so do some stuff
|
415 |
} |
416 |
}).create(); |
417 |
} |
418 |
else{
|
419 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
420 |
.setIcon(R.drawable.alert_dialog_icon) |
421 |
.setTitle("Delete Container")
|
422 |
.setMessage("Container must be empty to delete")
|
423 |
.setNegativeButton("OK",
|
424 |
new DialogInterface.OnClickListener() {
|
425 |
public void onClick(DialogInterface dialog, |
426 |
int whichButton) {
|
427 |
// User clicked Cancel so do some stuff
|
428 |
} |
429 |
}).create(); |
430 |
} |
431 |
case deleteFolder:
|
432 |
if(app.getCurFiles().size() == 0){ |
433 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
434 |
.setIcon(R.drawable.alert_dialog_icon) |
435 |
.setTitle("Delete Folder")
|
436 |
.setMessage( |
437 |
"Are you sure you want to delete this Folder?")
|
438 |
.setPositiveButton("Delete Folder",
|
439 |
new DialogInterface.OnClickListener() {
|
440 |
public void onClick(DialogInterface dialog, |
441 |
int whichButton) {
|
442 |
// User clicked OK so do some stuff
|
443 |
new DeleteObjectTask().execute();
|
444 |
} |
445 |
}) |
446 |
.setNegativeButton("Cancel",
|
447 |
new DialogInterface.OnClickListener() {
|
448 |
public void onClick(DialogInterface dialog, |
449 |
int whichButton) {
|
450 |
// User clicked Cancel so do some stuff
|
451 |
} |
452 |
}).create(); |
453 |
} |
454 |
else{
|
455 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
456 |
.setIcon(R.drawable.alert_dialog_icon) |
457 |
.setTitle("Delete Folder")
|
458 |
.setMessage( |
459 |
"Folder must be empty to delete")
|
460 |
.setNegativeButton("OK",
|
461 |
new DialogInterface.OnClickListener() {
|
462 |
public void onClick(DialogInterface dialog, |
463 |
int whichButton) {
|
464 |
// User clicked Cancel so do some stuff
|
465 |
} |
466 |
}).create(); |
467 |
} |
468 |
case R.id.add_folder:
|
469 |
final EditText input = new EditText(this); |
470 |
return new AlertDialog.Builder(ContainerObjectsActivity.this) |
471 |
.setIcon(R.drawable.alert_dialog_icon) |
472 |
.setView(input) |
473 |
.setTitle("Add Folder")
|
474 |
.setMessage("Enter new name for folder: ")
|
475 |
.setPositiveButton("Add", new DialogInterface.OnClickListener() { |
476 |
public void onClick(DialogInterface dialog, int whichButton) { |
477 |
//User clicked OK so do some stuff
|
478 |
String[] info = {input.getText().toString(), "application/directory"}; |
479 |
new AddFolderTask().execute(info);
|
480 |
} |
481 |
}) |
482 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
483 |
public void onClick(DialogInterface dialog, int whichButton) { |
484 |
// User clicked Cancel so do some stuff
|
485 |
} |
486 |
}) |
487 |
.create(); |
488 |
} |
489 |
return null; |
490 |
} |
491 |
|
492 |
@Override
|
493 |
public void onActivityResult(int requestCode, int resultCode, Intent data) { |
494 |
super.onActivityResult(requestCode, resultCode, data);
|
495 |
|
496 |
if (resultCode == RESULT_OK && requestCode == 56) { |
497 |
// a sub-activity kicked back, so we want to refresh the server list
|
498 |
loadFiles(); |
499 |
} |
500 |
|
501 |
// deleted file so need to update the list
|
502 |
if (requestCode == 55 && resultCode == 99) { |
503 |
loadFiles(); |
504 |
} |
505 |
|
506 |
} |
507 |
|
508 |
class FileAdapter extends ArrayAdapter<ContainerObjects> { |
509 |
FileAdapter() { |
510 |
super(ContainerObjectsActivity.this,
|
511 |
R.layout.listcontainerobjectcell, app.getCurFiles()); |
512 |
} |
513 |
|
514 |
public View getView(int position, View convertView, ViewGroup parent) { |
515 |
ContainerObjects file = app.getCurFiles().get(position); |
516 |
LayoutInflater inflater = getLayoutInflater(); |
517 |
View row = inflater.inflate(R.layout.listcontainerobjectcell,
|
518 |
parent, false);
|
519 |
|
520 |
TextView label = (TextView) row.findViewById(R.id.label); |
521 |
//label.setText(file.getCName());
|
522 |
label.setText(getShortName(file.getCName())); |
523 |
|
524 |
if (file.getBytes() >= bConver) {
|
525 |
megaBytes = Math.abs(file.getBytes() / bConver + 0.2); |
526 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
527 |
sublabel.setText(megaBytes + " MB");
|
528 |
} else if (file.getBytes() >= kbConver) { |
529 |
kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2); |
530 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
531 |
sublabel.setText(kiloBytes + " KB");
|
532 |
} else {
|
533 |
TextView sublabel = (TextView) row.findViewById(R.id.sublabel); |
534 |
sublabel.setText(file.getBytes() + " B");
|
535 |
} |
536 |
|
537 |
return (row);
|
538 |
} |
539 |
} |
540 |
|
541 |
private class LoadFilesTask extends |
542 |
AsyncTask<String, Void, ArrayList<ContainerObjects>> { |
543 |
|
544 |
private CloudServersException exception;
|
545 |
|
546 |
protected void onPreExecute(){ |
547 |
showDialog(); |
548 |
loadingFiles = true;
|
549 |
} |
550 |
|
551 |
@Override
|
552 |
protected ArrayList<ContainerObjects> doInBackground(String... path) { |
553 |
ArrayList<ContainerObjects> files = null; |
554 |
try {
|
555 |
files = (new ContainerObjectManager(getContext())).createList(true, |
556 |
container.getName()); |
557 |
} catch (CloudServersException e) {
|
558 |
exception = e; |
559 |
e.printStackTrace(); |
560 |
} |
561 |
return files;
|
562 |
} |
563 |
|
564 |
@Override
|
565 |
protected void onPostExecute(ArrayList<ContainerObjects> result) { |
566 |
loadingFiles = false;
|
567 |
hideDialog(); |
568 |
if (exception != null) { |
569 |
showAlert("Error", exception.getMessage());
|
570 |
} |
571 |
setFileList(result); |
572 |
loadCurrentDirectoryFiles(); |
573 |
displayCurrentFiles(); |
574 |
} |
575 |
|
576 |
} |
577 |
|
578 |
private class AddFolderTask extends |
579 |
AsyncTask<String, Void, HttpBundle> { |
580 |
|
581 |
private CloudServersException exception;
|
582 |
|
583 |
@Override
|
584 |
protected void onPreExecute(){ |
585 |
showDialog(); |
586 |
app.setAddingObject(true);
|
587 |
task = new AddObjectListenerTask();
|
588 |
task.execute(); |
589 |
} |
590 |
|
591 |
@Override
|
592 |
protected HttpBundle doInBackground(String... data) { |
593 |
HttpBundle bundle = null;
|
594 |
try {
|
595 |
|
596 |
bundle = (new ContainerObjectManager(getContext())).addObject(container.getName(), currentPath, data[0], data[1]); |
597 |
} catch (CloudServersException e) {
|
598 |
exception = e; |
599 |
} |
600 |
return bundle;
|
601 |
} |
602 |
|
603 |
@Override
|
604 |
protected void onPostExecute(HttpBundle bundle) { |
605 |
app.setAddingObject(false);
|
606 |
hideDialog(); |
607 |
HttpResponse response = bundle.getResponse(); |
608 |
if (response != null) { |
609 |
int statusCode = response.getStatusLine().getStatusCode();
|
610 |
if (statusCode == 201) { |
611 |
setResult(Activity.RESULT_OK); |
612 |
//loading the new files is done by ListenerTask
|
613 |
} else {
|
614 |
CloudServersException cse = parseCloudServersException(response); |
615 |
if ("".equals(cse.getMessage())) { |
616 |
showError("There was a problem deleting your folder.", bundle);
|
617 |
} else {
|
618 |
showError("There was a problem deleting your folder: "
|
619 |
+ cse.getMessage(), bundle); |
620 |
} |
621 |
} |
622 |
} else if (exception != null) { |
623 |
showError("There was a problem deleting your folder: "
|
624 |
+ exception.getMessage(), bundle); |
625 |
} |
626 |
} |
627 |
} |
628 |
|
629 |
private class DeleteObjectTask extends |
630 |
AsyncTask<Void, Void, HttpBundle> { |
631 |
|
632 |
private CloudServersException exception;
|
633 |
|
634 |
@Override
|
635 |
protected void onPreExecute(){ |
636 |
showDialog(); |
637 |
app.setDeleteingObject(true);
|
638 |
deleteObjTask = new DeleteObjectListenerTask();
|
639 |
deleteObjTask.execute(); |
640 |
} |
641 |
|
642 |
@Override
|
643 |
protected HttpBundle doInBackground(Void... arg0) { |
644 |
HttpBundle bundle = null;
|
645 |
try {
|
646 |
//subtring because the current directory contains a "/" at the end of the string
|
647 |
bundle = (new ContainerObjectManager(getContext())).deleteObject(container.getName(), currentPath.substring(0, currentPath.length()-1)); |
648 |
} catch (CloudServersException e) {
|
649 |
exception = e; |
650 |
} |
651 |
return bundle;
|
652 |
} |
653 |
|
654 |
@Override
|
655 |
protected void onPostExecute(HttpBundle bundle) { |
656 |
app.setDeleteingObject(false);
|
657 |
hideDialog(); |
658 |
HttpResponse response = bundle.getResponse(); |
659 |
if (response != null) { |
660 |
int statusCode = response.getStatusLine().getStatusCode();
|
661 |
if (statusCode == 409) { |
662 |
showAlert("Error",
|
663 |
"Folder must be empty in order to delete");
|
664 |
} |
665 |
if (statusCode == 204) { |
666 |
setResult(Activity.RESULT_OK); |
667 |
} else {
|
668 |
CloudServersException cse = parseCloudServersException(response); |
669 |
if ("".equals(cse.getMessage())) { |
670 |
showError("There was a problem deleting your folder.", bundle);
|
671 |
} else {
|
672 |
showError("There was a problem deleting your folder: "
|
673 |
+ cse.getMessage(), bundle); |
674 |
} |
675 |
} |
676 |
} else if (exception != null) { |
677 |
showError("There was a problem deleting your folder: "
|
678 |
+ exception.getMessage(), bundle); |
679 |
} |
680 |
} |
681 |
} |
682 |
|
683 |
private class DeleteContainerTask extends |
684 |
AsyncTask<String, Void, HttpBundle> { |
685 |
|
686 |
private CloudServersException exception;
|
687 |
|
688 |
@Override
|
689 |
protected void onPreExecute(){ |
690 |
showDialog(); |
691 |
app.setDeletingContainer(true);
|
692 |
deleteContainerTask = new DeleteContainerListenerTask();
|
693 |
deleteContainerTask.execute(); |
694 |
} |
695 |
|
696 |
@Override
|
697 |
protected HttpBundle doInBackground(String... object) { |
698 |
HttpBundle bundle = null;
|
699 |
try {
|
700 |
bundle = (new ContainerManager(getContext())).delete(container.getName());
|
701 |
} catch (CloudServersException e) {
|
702 |
exception = e; |
703 |
} |
704 |
return bundle;
|
705 |
} |
706 |
|
707 |
@Override
|
708 |
protected void onPostExecute(HttpBundle bundle) { |
709 |
hideDialog(); |
710 |
app.setDeletingContainer(false);
|
711 |
HttpResponse response = bundle.getResponse(); |
712 |
if (response != null) { |
713 |
int statusCode = response.getStatusLine().getStatusCode();
|
714 |
if (statusCode == 409) { |
715 |
showError("Container must be empty in order to delete", bundle);
|
716 |
} |
717 |
if (statusCode == 204) { |
718 |
setResult(Activity.RESULT_OK); |
719 |
|
720 |
} else {
|
721 |
CloudServersException cse = parseCloudServersException(response); |
722 |
if ("".equals(cse.getMessage())) { |
723 |
showError("There was a problem deleting your container.", bundle);
|
724 |
} else {
|
725 |
showError("There was a problem deleting your container: "
|
726 |
+ cse.getMessage(), bundle); |
727 |
} |
728 |
} |
729 |
} else if (exception != null) { |
730 |
showError("There was a problem deleting your server: "
|
731 |
+ exception.getMessage(), bundle); |
732 |
} |
733 |
} |
734 |
} |
735 |
|
736 |
/*
|
737 |
* listens for the application to change isProcessing
|
738 |
* listens so activity knows when it should display
|
739 |
* the new curDirFiles
|
740 |
*/
|
741 |
private class AddObjectListenerTask extends |
742 |
AsyncTask<Void, Void, Void> { |
743 |
|
744 |
@Override
|
745 |
protected Void doInBackground(Void... arg1) { |
746 |
|
747 |
while(app.isAddingObject()){
|
748 |
// wait for process to finish
|
749 |
// or have it be canceled
|
750 |
if(task.isCancelled()){
|
751 |
return null; |
752 |
} |
753 |
} |
754 |
return null; |
755 |
} |
756 |
|
757 |
/*
|
758 |
* when no longer processing, time to load
|
759 |
* the new files
|
760 |
*/
|
761 |
@Override
|
762 |
protected void onPostExecute(Void arg1) { |
763 |
hideDialog(); |
764 |
loadFiles(); |
765 |
} |
766 |
} |
767 |
|
768 |
|
769 |
private class DeleteObjectListenerTask extends |
770 |
AsyncTask<Void, Void, Void> { |
771 |
|
772 |
@Override
|
773 |
protected Void doInBackground(Void... arg1) { |
774 |
|
775 |
while(app.isDeletingObject()){
|
776 |
// wait for process to finish
|
777 |
// or have it be canceled
|
778 |
if(deleteObjTask.isCancelled()){
|
779 |
return null; |
780 |
} |
781 |
} |
782 |
return null; |
783 |
} |
784 |
|
785 |
/*
|
786 |
* when no longer processing, time to load
|
787 |
* the new files
|
788 |
*/
|
789 |
@Override
|
790 |
protected void onPostExecute(Void arg1) { |
791 |
hideDialog(); |
792 |
removeFromList(currentPath); |
793 |
goUpDirectory(); |
794 |
} |
795 |
} |
796 |
|
797 |
private class DeleteContainerListenerTask extends |
798 |
AsyncTask<Void, Void, Void> { |
799 |
|
800 |
@Override
|
801 |
protected Void doInBackground(Void... arg1) { |
802 |
|
803 |
while(app.isDeletingContainer()){
|
804 |
// wait for process to finish
|
805 |
// or have it be canceled
|
806 |
if(deleteContainerTask.isCancelled()){
|
807 |
return null; |
808 |
} |
809 |
} |
810 |
return null; |
811 |
} |
812 |
|
813 |
/*
|
814 |
* when no longer processing, time to load
|
815 |
* the new files
|
816 |
*/
|
817 |
@Override
|
818 |
protected void onPostExecute(Void arg1) { |
819 |
|
820 |
hideDialog(); |
821 |
setResult(RESULT_OK); |
822 |
finish(); |
823 |
} |
824 |
} |
825 |
} |