Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / ContainerObjectsActivity.java @ ef6fa21a

History | View | Annotate | Download (22.2 kB)

1
package com.rackspace.cloud.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.util.Log;
16
import android.view.LayoutInflater;
17
import android.view.Menu;
18
import android.view.MenuInflater;
19
import android.view.MenuItem;
20
import android.view.View;
21
import android.view.ViewGroup;
22
import android.widget.ArrayAdapter;
23
import android.widget.EditText;
24
import android.widget.ImageView;
25
import android.widget.ListView;
26
import android.widget.TextView;
27

    
28
import com.rackspace.cloud.android.R;
29
import com.rackspace.cloud.files.api.client.Container;
30
import com.rackspace.cloud.files.api.client.ContainerManager;
31
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
32
import com.rackspace.cloud.files.api.client.ContainerObjects;
33
import com.rackspace.cloud.servers.api.client.CloudServersException;
34
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
35

    
36
/**
37
 * 
38
 * @author Phillip Toohill
39
 * 
40
 */
41

    
42
public class ContainerObjectsActivity extends CloudListActivity {
43

    
44
        private static final int deleteContainer = 0;
45
        private static final int deleteFolder = 1;
46

    
47
        private ContainerObjects[] files;
48
        private static Container container;
49
        public String LOG = "viewFilesActivity";
50
        private String cdnEnabledIs;
51
        public Object megaBytes;
52
        public Object kiloBytes;
53
        public int bConver = 1048576;
54
        public int kbConver = 1024;
55
        private boolean loadingFiles;
56
        private String currentPath;
57
        private AndroidCloudApplication app;
58
        private AddObjectListenerTask task;
59
        private DeleteObjectListenerTask deleteObjTask;
60
        private DeleteContainerListenerTask deleteContainerTask;
61

    
62
        @Override
63
        public void onCreate(Bundle savedInstanceState) {
64
                super.onCreate(savedInstanceState);
65
                trackPageView(GoogleAnalytics.PAGE_FOLDER);
66
                container = (Container) this.getIntent().getExtras().get("container");
67
                if (container.isCdnEnabled() == true) {
68
                        cdnEnabledIs = "true";
69
                } else {
70
                        cdnEnabledIs = "false";
71
                }
72
                restoreState(savedInstanceState);
73
        }
74

    
75
        @Override
76
        protected void onSaveInstanceState(Bundle outState) {
77
                super.onSaveInstanceState(outState);
78

    
79
                //files stores all the files in the container
80
                outState.putSerializable("container", files);
81

    
82
                //current path represents where you have "navigated" to
83
                outState.putString("path", currentPath);
84

    
85
                outState.putBoolean("loadingFiles", loadingFiles);
86
        }
87

    
88

    
89

    
90
        protected void restoreState(Bundle state) {
91
                super.restoreState(state);
92

    
93
                /*
94
                 * need reference to the app so you can access curDirFiles
95
                 * as well as processing status
96
                 */
97
                app = (AndroidCloudApplication)this.getApplication();
98

    
99
                if(state != null){
100
                        if(state.containsKey("path")){
101
                                currentPath = state.getString("path");
102
                        }
103
                        else{
104
                                currentPath = "";
105
                        }
106

    
107
                        if(state.containsKey("loadingFiles") && state.getBoolean("loadingFiles")){
108
                                loadFiles();
109
                        }
110
                        else if(state.containsKey("container")){
111
                                files = (ContainerObjects[]) state.getSerializable("container");
112
                                if (app.getCurFiles() == null || app.getCurFiles().size() == 0) {
113
                                        displayNoFilesCell();
114
                                } else {
115
                                        getListView().setDividerHeight(1); // restore divider lines
116
                                        setListAdapter(new FileAdapter());
117

    
118
                                }
119

    
120
                        }
121
                }
122
                else {
123
                        currentPath = "";
124
                        loadFiles();
125
                }        
126

    
127
                /*
128
                 * if the app is process when we enter the activity
129
                 * we must listen for the new curDirFiles list
130
                 */
131
                if(app.isAddingObject()){
132
                        task = new AddObjectListenerTask();
133
                        task.execute();
134
                }
135

    
136
                if(app.isDeletingObject()){
137
                        displayNoFilesCell();
138
                        deleteObjTask = new DeleteObjectListenerTask();
139
                        deleteObjTask.execute();
140
                }
141

    
142
                if(app.isDeletingContainer()){
143
                        displayNoFilesCell();
144
                        deleteContainerTask = new DeleteContainerListenerTask();
145
                        deleteContainerTask.execute();
146
                }
147

    
148

    
149
        }
150

    
151
        @Override
152
        protected void onStop(){
153
                super.onStop();
154

    
155
                /*
156
                 * Need to stop running listener task
157
                 * if we exit
158
                 */
159
                if(task != null){
160
                        task.cancel(true);
161
                }
162

    
163
                if(deleteObjTask != null){
164
                        deleteObjTask.cancel(true);
165
                }
166

    
167
                if(deleteContainerTask != null){
168
                        deleteContainerTask.cancel(true);
169
                }
170

    
171
        }
172

    
173
        /*
174
         * overriding back button press, because we are not actually changing
175
         * activities when we navigate the file structure
176
         */
177
        public void onBackPressed() {
178
                if(currentPath.equals("")){
179
                        finish();
180
                }
181
                else{
182
                        goUpDirectory();
183
                }
184
        }
185

    
186
        /*
187
         * go to the current directory's parent and display that data
188
         */
189
        private void goUpDirectory(){
190
                currentPath = currentPath.substring(0, currentPath.substring(0, currentPath.length()-2).lastIndexOf("/")+1);
191
                loadCurrentDirectoryFiles();
192
                displayCurrentFiles();
193
        }
194

    
195
        /*
196
         * load all file that are in the container
197
         */
198
        private void loadFiles() {
199
                //displayLoadingCell();
200
                new LoadFilesTask().execute();
201
        }
202

    
203

    
204
        /* load only the files that should display for the 
205
         * current directory in the curDirFiles[]
206
         */
207
        private void loadCurrentDirectoryFiles(){
208
                ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>();
209

    
210
                if(files != null){
211
                        for(int i = 0 ; i < files.length; i ++){
212
                                if(fileBelongsInDir(files[i])){
213
                                        curFiles.add(files[i]);
214
                                }
215
                        }
216
                        app.setCurFiles(curFiles);
217
                }
218
        }
219

    
220
        /*
221
         * determines if a file should be displayed in current 
222
         * directory
223
         */
224
        private Boolean fileBelongsInDir(ContainerObjects obj){
225
                String objPath = obj.getCName();
226
                Log.i("INFO",currentPath);
227
                if(!objPath.startsWith(currentPath)){
228
                        return false;
229
                }
230
                else{
231
                        objPath = objPath.substring(currentPath.length());
232
                        return !objPath.contains("/");
233
                }
234
        }
235

    
236

    
237
        /*
238
         * loads all the files that are in the container
239
         * into one array
240
         */
241
        private void setFileList(ArrayList<ContainerObjects> files) {
242
                if (files == null) {
243
                        files = new ArrayList<ContainerObjects>();
244
                }
245
                String[] fileNames = new String[files.size()];
246
                this.files = new ContainerObjects[files.size()];
247

    
248

    
249

    
250
                if (files != null) {
251
                        for (int i = 0; i < files.size(); i++) {
252
                                ContainerObjects file = files.get(i);
253
                                Log.i(file.getCName(),file.getCName());
254
                                this.files[i] = file;
255
                                fileNames[i] = file.getName();
256
                        }
257
                }
258
        }
259

    
260
        private void displayCurrentFiles(){
261
                if (app.getCurFiles().size() == 0) {
262
                        displayNoFilesCell();
263
                } else {
264
                        ArrayList<ContainerObjects> tempList = new ArrayList<ContainerObjects>();
265
                        for(int i = 0; i < app.getCurFiles().size(); i++){
266
                                tempList.add(app.getCurFiles().get(i));
267
                        }
268
                        getListView().setDividerHeight(1); // restore divider lines
269
                        setListAdapter(new FileAdapter());
270
                }
271
        }
272

    
273
        /*
274
         * display a different empty page depending
275
         * of if you are at top of container or
276
         * in a folder
277
         */
278
        private void displayNoFilesCell() {
279
                String a[] = new String[1];
280
                if(currentPath.equals("")){
281
                        a[0] = "Empty Container";
282
                        setListAdapter(new ArrayAdapter<String>(this, R.layout.noobjectscell,
283
                                        R.id.no_files_label, a));
284
                }
285
                else{
286
                        a[0] = "No Files";
287
                        setListAdapter(new ArrayAdapter<String>(this, R.layout.nofilescell,
288
                                        R.id.no_files_label, a));
289
                }
290
                getListView().setTextFilterEnabled(true);
291
                getListView().setDividerHeight(0); // hide the dividers so it won't look
292
                // like a list row
293
                getListView().setItemsCanFocus(false);
294
        }
295

    
296
        /* just get the last part of the filename
297
         * so the entire file path does not show
298
         */
299
        private String getShortName(String longName){
300
                String s = longName;
301
                if(!s.contains("/")){
302
                        return s;
303
                }
304
                else {
305
                        String tmp = s.substring(s.lastIndexOf('/')+1);
306
                        if(tmp.equals("")){
307
                                return s;
308
                        }
309
                        return tmp; 
310
                }
311
        }
312

    
313
        /*
314
         * removed a specified object from the array of 
315
         * all the files in the container
316
         */
317
        private void removeFromList(String path){
318
                ArrayList<ContainerObjects> temp = new ArrayList<ContainerObjects>(Arrays.asList(files));
319
                for(int i = 0; i < files.length; i++){
320
                        if(files[i].getCName().equals(path.substring(0, path.length()-1))){
321
                                temp.remove(i);
322
                        }
323
                }
324
                files = new ContainerObjects[temp.size()];
325
                for(int i = 0; i < temp.size(); i++){
326
                        files[i] = temp.get(i);
327
                }
328
        }
329

    
330
        protected void onListItemClick(ListView l, View v, int position, long id) {
331
                if (app.getCurFiles() != null && app.getCurFiles().size() > 0) {
332
                        Intent viewIntent;
333
                        if(app.getCurFiles().get(position).getContentType().startsWith("application/directory")){                        
334
                                currentPath = app.getCurFiles().get(position).getCName() + "/";
335
                                loadCurrentDirectoryFiles();
336
                                displayCurrentFiles();
337
                        }
338

    
339
                        else{
340
                                viewIntent = new Intent(this, ContainerObjectDetails.class);
341
                                viewIntent.putExtra("container", app.getCurFiles().get(position));
342
                                viewIntent.putExtra("cdnUrl", container.getCdnUrl());
343
                                viewIntent.putExtra("containerNames", container.getName());
344
                                viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
345
                                startActivityForResult(viewIntent, 55); // arbitrary number; never
346
                                // used again
347
                        }
348
                }
349
        }
350

    
351
        /* 
352
         * Create the Menu options
353
        @Override
354
        public boolean onCreateOptionsMenu(Menu menu) {
355
                super.onCreateOptionsMenu(menu);
356
                MenuInflater inflater = getMenuInflater();
357
                inflater.inflate(R.menu.view_container_object_list_menu, menu);
358
                return true;
359
        }
360

361
        @Override
362
        /*
363
         * option performed for delete depends on if you
364
         * are at the top of a container or in a folder
365
         */
366
        public boolean onOptionsItemSelected(MenuItem item) {
367
                switch (item.getItemId()) {
368
                case R.id.delete_container:
369
                        if(currentPath.equals("")){
370
                                showDialog(deleteContainer);
371
                        }
372
                        else{
373
                                showDialog(deleteFolder);
374
                        }
375
                        return true;
376
                case R.id.enable_cdn:
377
                        Intent viewIntent1 = new Intent(this, EnableCDNActivity.class);
378
                        viewIntent1.putExtra("Cname", container.getName());
379
                        startActivityForResult(viewIntent1, 56);
380
                        return true;
381
                case R.id.refresh:
382
                        loadFiles();
383
                        return true;
384
                case R.id.add_folder:
385
                        showDialog(R.id.add_folder);
386
                        return true;
387
                case R.id.add_file:
388
                        Intent viewIntent2 = new Intent(this, AddFileActivity.class);
389
                        viewIntent2.putExtra("Cname", container.getName());
390
                        viewIntent2.putExtra("curPath", currentPath);
391
                        startActivityForResult(viewIntent2, 56);
392
                        return true;        
393
                }
394
                return false;
395
        }
396

    
397
        @Override
398
        protected Dialog onCreateDialog(int id) {
399
                switch (id) {
400
                case deleteContainer:
401
                        if(app.getCurFiles().size() == 0){
402
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
403
                                .setIcon(R.drawable.alert_dialog_icon)
404
                                .setTitle("Delete Container")
405
                                .setMessage(
406
                                "Are you sure you want to delete this Container?")
407
                                .setPositiveButton("Delete Container",
408
                                                new DialogInterface.OnClickListener() {
409
                                        public void onClick(DialogInterface dialog,
410
                                                        int whichButton) {
411
                                                // User clicked OK so do some stuff
412
                                                trackEvent(GoogleAnalytics.CATEGORY_CONTAINER, GoogleAnalytics.EVENT_DELETE, "", -1);
413
                                                new DeleteContainerTask()
414
                                                .execute(currentPath);
415
                                        }
416
                                })
417
                                .setNegativeButton("Cancel",
418
                                                new DialogInterface.OnClickListener() {
419
                                        public void onClick(DialogInterface dialog,
420
                                                        int whichButton) {
421
                                                // User clicked Cancel so do some stuff
422
                                        }
423
                                }).create();
424
                        }
425
                        else{
426
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
427
                                .setIcon(R.drawable.alert_dialog_icon)
428
                                .setTitle("Delete Container")
429
                                .setMessage("Container must be empty to delete")
430
                                .setNegativeButton("OK",
431
                                                new DialogInterface.OnClickListener() {
432
                                        public void onClick(DialogInterface dialog,
433
                                                        int whichButton) {
434
                                                // User clicked Cancel so do some stuff
435
                                        }
436
                                }).create();
437
                        }
438
                case deleteFolder:
439
                        if(app.getCurFiles().size() == 0){
440
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
441
                                .setIcon(R.drawable.alert_dialog_icon)
442
                                .setTitle("Delete Folder")
443
                                .setMessage(
444
                                "Are you sure you want to delete this Folder?")
445
                                .setPositiveButton("Delete Folder",
446
                                                new DialogInterface.OnClickListener() {
447
                                        public void onClick(DialogInterface dialog,
448
                                                        int whichButton) {
449
                                                // User clicked OK so do some stuff
450
                                                new DeleteObjectTask().execute();
451
                                        }
452
                                })
453
                                .setNegativeButton("Cancel",
454
                                                new DialogInterface.OnClickListener() {
455
                                        public void onClick(DialogInterface dialog,
456
                                                        int whichButton) {
457
                                                // User clicked Cancel so do some stuff
458
                                        }
459
                                }).create();
460
                        }
461
                        else{
462
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
463
                                .setIcon(R.drawable.alert_dialog_icon)
464
                                .setTitle("Delete Folder")
465
                                .setMessage(
466
                                "Folder must be empty to delete")
467
                                .setNegativeButton("OK",
468
                                                new DialogInterface.OnClickListener() {
469
                                        public void onClick(DialogInterface dialog,
470
                                                        int whichButton) {
471
                                                // User clicked Cancel so do some stuff
472
                                        }
473
                                }).create();
474
                        }
475
                case R.id.add_folder:
476
                        final EditText input = new EditText(this);
477
                        return new AlertDialog.Builder(ContainerObjectsActivity.this)
478
                        .setIcon(R.drawable.alert_dialog_icon)
479
                        .setView(input)
480
                        .setTitle("Add Folder")
481
                        .setMessage("Enter new name for folder: ")                         
482
                        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
483
                                public void onClick(DialogInterface dialog, int whichButton) {
484
                                        //User clicked OK so do some stuff
485
                                        String[] info = {input.getText().toString(), "application/directory"};
486
                                        new AddFolderTask().execute(info);
487
                                }
488
                        })
489
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
490
                                public void onClick(DialogInterface dialog, int whichButton) {
491
                                        // User clicked Cancel so do some stuff
492
                                }
493
                        })
494
                        .create();     
495
                }
496
                return null;
497
        }
498

    
499
        @Override
500
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
501
                super.onActivityResult(requestCode, resultCode, data);
502

    
503
                if (resultCode == RESULT_OK && requestCode == 56) {
504
                        // a sub-activity kicked back, so we want to refresh the server list
505
                        loadFiles();
506
                }
507

    
508
                // deleted file so need to update the list
509
                if (requestCode == 55 && resultCode == 99) {
510
                        loadFiles();
511
                }
512

    
513
        }
514

    
515
        class FileAdapter extends ArrayAdapter<ContainerObjects> {
516
                FileAdapter() {
517
                        super(ContainerObjectsActivity.this,
518
                                        R.layout.listcontainerobjectcell, app.getCurFiles());        
519
                }
520

    
521
                public View getView(int position, View convertView, ViewGroup parent) {
522
                        ContainerObjects file = app.getCurFiles().get(position);
523
                        LayoutInflater inflater = getLayoutInflater();
524
                        View row = inflater.inflate(R.layout.listcontainerobjectcell,
525
                                        parent, false);
526

    
527
                        TextView label = (TextView) row.findViewById(R.id.label);
528
                        label.setText(getShortName(file.getCName()));
529

    
530
                        ImageView objectImage = (ImageView) row.findViewById(R.id.file_type_image);
531
                        if(file.getContentType().startsWith("application/directory")){
532
                                objectImage.setImageResource(R.drawable.folder);
533
                        } else {
534
                                objectImage.setImageResource(R.drawable.file);
535
                        }
536
                        
537
                        if (file.getBytes() >= bConver) {
538
                                megaBytes = Math.abs(file.getBytes() / bConver + 0.2);
539
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
540
                                sublabel.setText(megaBytes + " MB");
541
                        } else if (file.getBytes() >= kbConver) {
542
                                kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2);
543
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
544
                                sublabel.setText(kiloBytes + " KB");
545
                        } else {
546
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
547
                                sublabel.setText(file.getBytes() + " B");
548
                        }
549

    
550
                        return (row);
551
                }
552
        }
553

    
554
        private class LoadFilesTask extends
555
        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
556

    
557
                private CloudServersException exception;
558

    
559
                protected void onPreExecute(){
560
                        showDialog();
561
                        loadingFiles = true;
562
                }
563

    
564
                @Override
565
                protected ArrayList<ContainerObjects> doInBackground(String... path) {
566
                        ArrayList<ContainerObjects> files = null;
567
                        try {
568
                                if(container.getName().equals(Container.MYSHARED)){
569
                                        files = (new ContainerObjectManager(getContext())).createListMyShared(true,
570
                                                        container.getName(),new ContainerManager(getContext()).createList(true));
571
                                }
572
                                else if(container.getName().equals(Container.OTHERS)){
573
                                        
574
                                }
575
                                else
576
                                        files = (new ContainerObjectManager(getContext())).createList(true,
577
                                                container.getName());
578
                        } catch (CloudServersException e) {
579
                                exception = e;
580
                                e.printStackTrace();
581
                        }
582
                        return files;
583
                }
584

    
585
                @Override
586
                protected void onPostExecute(ArrayList<ContainerObjects> result) {
587
                        loadingFiles = false;
588
                        hideDialog();
589
                        if (exception != null) {
590
                                showAlert("Error", exception.getMessage());
591
                        }
592
                        setFileList(result);
593
                        loadCurrentDirectoryFiles();
594
                        displayCurrentFiles();
595
                }
596

    
597
        }
598

    
599
        private class AddFolderTask extends
600
        AsyncTask<String, Void, HttpBundle> {
601

    
602
                private CloudServersException exception;
603

    
604
                @Override
605
                protected void onPreExecute(){
606
                        showDialog();
607
                        app.setAddingObject(true);
608
                        task = new AddObjectListenerTask();
609
                        task.execute();
610
                }
611

    
612
                @Override
613
                protected HttpBundle doInBackground(String... data) {
614
                        HttpBundle bundle = null;
615
                        try {
616

    
617
                                bundle = (new ContainerObjectManager(getContext())).addObject(container.getName(), currentPath, data[0], data[1]);
618
                        } catch (CloudServersException e) {
619
                                exception = e;
620
                        }
621
                        return bundle;
622
                }
623

    
624
                @Override
625
                protected void onPostExecute(HttpBundle bundle) {
626
                        app.setAddingObject(false);
627
                        hideDialog();
628
                        HttpResponse response = bundle.getResponse();
629
                        if (response != null) {
630
                                int statusCode = response.getStatusLine().getStatusCode();
631
                                if (statusCode == 201) {
632
                                        setResult(Activity.RESULT_OK);
633
                                        //loading the new files is done by ListenerTask
634
                                } else {
635
                                        CloudServersException cse = parseCloudServersException(response);
636
                                        if ("".equals(cse.getMessage())) {
637
                                                showError("There was a problem deleting your folder.", bundle);
638
                                        } else {
639
                                                showError("There was a problem deleting your folder: "
640
                                                                + cse.getMessage(), bundle);
641
                                        }
642
                                }
643
                        } else if (exception != null) {
644
                                showError("There was a problem deleting your folder: "
645
                                                + exception.getMessage(), bundle);
646
                        }
647
                }
648
        }
649

    
650
        private class DeleteObjectTask extends
651
        AsyncTask<Void, Void, HttpBundle> {
652

    
653
                private CloudServersException exception;
654

    
655
                @Override
656
                protected void onPreExecute(){
657
                        showDialog();
658
                        app.setDeleteingObject(true);
659
                        deleteObjTask = new DeleteObjectListenerTask();
660
                        deleteObjTask.execute();
661
                }
662

    
663
                @Override
664
                protected HttpBundle doInBackground(Void... arg0) {
665
                        HttpBundle bundle = null;
666
                        try {
667
                                //subtring because the current directory contains a "/" at the end of the string
668
                                bundle = (new ContainerObjectManager(getContext())).deleteObject(container.getName(), currentPath.substring(0, currentPath.length()-1));
669
                        } catch (CloudServersException e) {
670
                                exception = e;
671
                        }
672
                        return bundle;
673
                }
674

    
675
                @Override
676
                protected void onPostExecute(HttpBundle bundle) {
677
                        app.setDeleteingObject(false);
678
                        hideDialog();
679
                        HttpResponse response = bundle.getResponse();
680
                        if (response != null) {
681
                                int statusCode = response.getStatusLine().getStatusCode();
682
                                if (statusCode == 409) {
683
                                        showAlert("Error",
684
                                        "Folder must be empty in order to delete");
685
                                }
686
                                if (statusCode == 204) {
687
                                        setResult(Activity.RESULT_OK);
688
                                } else {
689
                                        CloudServersException cse = parseCloudServersException(response);
690
                                        if ("".equals(cse.getMessage())) {
691
                                                showError("There was a problem deleting your folder.", bundle);
692
                                        } else {
693
                                                showError("There was a problem deleting your folder: "
694
                                                                + cse.getMessage(), bundle);
695
                                        }
696
                                }
697
                        } else if (exception != null) {
698
                                showError("There was a problem deleting your folder: "
699
                                                + exception.getMessage(), bundle);
700
                        }
701
                }
702
        }
703

    
704
        private class DeleteContainerTask extends
705
        AsyncTask<String, Void, HttpBundle> {
706

    
707
                private CloudServersException exception;
708

    
709
                @Override
710
                protected void onPreExecute(){ 
711
                        showDialog();
712
                        app.setDeletingContainer(true);
713
                        deleteContainerTask = new DeleteContainerListenerTask();
714
                        deleteContainerTask.execute();
715
                }
716

    
717
                @Override
718
                protected HttpBundle doInBackground(String... object) {
719
                        HttpBundle bundle = null;
720
                        try {
721
                                bundle = (new ContainerManager(getContext())).delete(container.getName());
722
                        } catch (CloudServersException e) {
723
                                exception = e;
724
                        }
725
                        return bundle;
726
                }
727

    
728
                @Override
729
                protected void onPostExecute(HttpBundle bundle) {
730
                        hideDialog();
731
                        app.setDeletingContainer(false);
732
                        HttpResponse response = bundle.getResponse();
733
                        if (response != null) {
734
                                int statusCode = response.getStatusLine().getStatusCode();
735
                                if (statusCode == 409) {
736
                                        showError("Container must be empty in order to delete", bundle);
737
                                }
738
                                if (statusCode == 204) {
739
                                        setResult(Activity.RESULT_OK);
740

    
741
                                } else {
742
                                        CloudServersException cse = parseCloudServersException(response);
743
                                        if ("".equals(cse.getMessage())) {
744
                                                showError("There was a problem deleting your container.", bundle);
745
                                        } else {
746
                                                showError("There was a problem deleting your container: "
747
                                                                + cse.getMessage(), bundle);
748
                                        }
749
                                }
750
                        } else if (exception != null) {
751
                                showError("There was a problem deleting your server: "
752
                                                + exception.getMessage(), bundle);
753
                        }
754
                }
755
        }
756

    
757
        /*
758
         * listens for the application to change isProcessing
759
         * listens so activity knows when it should display
760
         * the new curDirFiles
761
         */
762
        private class AddObjectListenerTask extends
763
        AsyncTask<Void, Void, Void> {
764

    
765
                @Override
766
                protected Void doInBackground(Void... arg1) {
767

    
768
                        while(app.isAddingObject()){
769
                                // wait for process to finish
770
                                // or have it be canceled
771
                                if(task.isCancelled()){
772
                                        return null;
773
                                }
774
                        }
775
                        return null;
776
                }
777

    
778
                /*
779
                 * when no longer processing, time to load
780
                 * the new files
781
                 */
782
                @Override
783
                protected void onPostExecute(Void arg1) {
784
                        hideDialog();
785
                        loadFiles();
786
                }
787
        }
788

    
789

    
790
        private class DeleteObjectListenerTask extends
791
        AsyncTask<Void, Void, Void> {
792

    
793
                @Override
794
                protected Void doInBackground(Void... arg1) {
795

    
796
                        while(app.isDeletingObject()){
797
                                // wait for process to finish
798
                                // or have it be canceled
799
                                if(deleteObjTask.isCancelled()){
800
                                        return null;
801
                                }
802
                        }
803
                        return null;
804
                }
805

    
806
                /*
807
                 * when no longer processing, time to load
808
                 * the new files
809
                 */
810
                @Override
811
                protected void onPostExecute(Void arg1) {
812
                        hideDialog();
813
                        removeFromList(currentPath);
814
                        goUpDirectory();
815
                }
816
        }
817

    
818
        private class DeleteContainerListenerTask extends
819
        AsyncTask<Void, Void, Void> {
820

    
821
                @Override
822
                protected Void doInBackground(Void... arg1) {
823

    
824
                        while(app.isDeletingContainer()){
825
                                // wait for process to finish
826
                                // or have it be canceled
827
                                if(deleteContainerTask.isCancelled()){
828
                                        return null;
829
                                }
830
                        }
831
                        return null;
832
                }
833

    
834
                /*
835
                 * when no longer processing, time to load
836
                 * the new files
837
                 */
838
                @Override
839
                protected void onPostExecute(Void arg1) {
840

    
841
                        hideDialog();
842
                        setResult(RESULT_OK);
843
                        finish();
844
                }
845
        }
846
}