Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / ContainerObjectsActivity.java @ 856ad13d

History | View | Annotate | Download (22.8 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 = getContainer();
67
                if (container.isCdnEnabled() == true) {
68
                        cdnEnabledIs = "true";
69
                } else {
70
                        cdnEnabledIs = "false";
71
                }
72
                restoreState(savedInstanceState);
73
        }
74
        
75
        protected Container getContainer(){
76
                return (Container) this.getIntent().getExtras().get("container");
77
        }
78
        
79
        @Override
80
        protected void onSaveInstanceState(Bundle outState) {
81
                super.onSaveInstanceState(outState);
82

    
83
                //files stores all the files in the container
84
                outState.putSerializable("container", files);
85

    
86
                //current path represents where you have "navigated" to
87
                outState.putString("path", currentPath);
88

    
89
                outState.putBoolean("loadingFiles", loadingFiles);
90
        }
91

    
92

    
93

    
94
        protected void restoreState(Bundle state) {
95
                super.restoreState(state);
96

    
97
                /*
98
                 * need reference to the app so you can access curDirFiles
99
                 * as well as processing status
100
                 */
101
                app = (AndroidCloudApplication)this.getApplication();
102

    
103
                if(state != null){
104
                        if(state.containsKey("path")){
105
                                currentPath = state.getString("path");
106
                        }
107
                        else{
108
                                currentPath = "";
109
                        }
110

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

    
122
                                }
123

    
124
                        }
125
                }
126
                else {
127
                        currentPath = "";
128
                        loadFiles();
129
                }        
130

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

    
140
                if(app.isDeletingObject()){
141
                        displayNoFilesCell();
142
                        deleteObjTask = new DeleteObjectListenerTask();
143
                        deleteObjTask.execute();
144
                }
145

    
146
                if(app.isDeletingContainer()){
147
                        displayNoFilesCell();
148
                        deleteContainerTask = new DeleteContainerListenerTask();
149
                        deleteContainerTask.execute();
150
                }
151

    
152

    
153
        }
154

    
155
        @Override
156
        protected void onStop(){
157
                super.onStop();
158

    
159
                /*
160
                 * Need to stop running listener task
161
                 * if we exit
162
                 */
163
                if(task != null){
164
                        task.cancel(true);
165
                }
166

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

    
171
                if(deleteContainerTask != null){
172
                        deleteContainerTask.cancel(true);
173
                }
174

    
175
        }
176

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

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

    
199
        /*
200
         * load all file that are in the container
201
         */
202
        private void loadFiles() {
203
                //displayLoadingCell();
204
                new LoadFilesTask().execute();
205
        }
206

    
207

    
208
        /* load only the files that should display for the 
209
         * current directory in the curDirFiles[]
210
         */
211
        protected void loadCurrentDirectoryFiles(){
212
                ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>();
213
                Log.i(LOG,"loading files");
214
                if(files != null){
215
                        for(int i = 0 ; i < files.length; i ++){
216
                                Log.i(LOG,"loading files"+i);
217
                                if(fileBelongsInDir(files[i])){
218
                                        curFiles.add(files[i]);
219
                                }
220
                        }
221
                        app.setCurFiles(curFiles);
222
                }
223
        }
224

    
225
        /*
226
         * determines if a file should be displayed in current 
227
         * directory
228
         */
229
        protected Boolean fileBelongsInDir(ContainerObjects obj){
230
                String objPath = obj.getCName();
231
                
232
                if(!objPath.startsWith(currentPath)){
233
                        Log.i(LOG,"Path is:"+currentPath+" "+objPath+" "+false);
234
                        return false;
235
                }
236
                else{
237
                        objPath = objPath.substring(currentPath.length());
238
                        Log.i(LOG,"Path is:"+currentPath+" "+objPath+" "+!objPath.contains("/"));
239
                        return !objPath.contains("/");
240
                }
241
        }
242

    
243

    
244
        /*
245
         * loads all the files that are in the container
246
         * into one array
247
         */
248
        private void setFileList(ArrayList<ContainerObjects> files) {
249
                if (files == null) {
250
                        files = new ArrayList<ContainerObjects>();
251
                }
252
                String[] fileNames = new String[files.size()];
253
                this.files = new ContainerObjects[files.size()];
254

    
255

    
256

    
257
                if (files != null) {
258
                        for (int i = 0; i < files.size(); i++) {
259
                                ContainerObjects file = files.get(i);
260
                                //Log.i(file.getCName(),file.getCName());
261
                                this.files[i] = file;
262
                                fileNames[i] = file.getName();
263
                        }
264
                }
265
        }
266

    
267
        protected void displayCurrentFiles(){
268
                if (app.getCurFiles().size() == 0) {
269
                        displayNoFilesCell();
270
                } else {
271
                        ArrayList<ContainerObjects> tempList = new ArrayList<ContainerObjects>();
272
                        for(int i = 0; i < app.getCurFiles().size(); i++){
273
                                tempList.add(app.getCurFiles().get(i));
274
                        }
275
                        getListView().setDividerHeight(1); // restore divider lines
276
                        setListAdapter(new FileAdapter());
277
                }
278
        }
279

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

    
303
        /* just get the last part of the filename
304
         * so the entire file path does not show
305
         */
306
        private String getShortName(String longName){
307
                String s = longName;
308
                if(!s.contains("/")){
309
                        return s;
310
                }
311
                else {
312
                        String tmp = s.substring(s.lastIndexOf('/')+1);
313
                        if(tmp.equals("")){
314
                                return s;
315
                        }
316
                        return tmp; 
317
                }
318
        }
319

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

    
337
        protected void onListItemClick(ListView l, View v, int position, long id) {
338
                if (app.getCurFiles() != null && app.getCurFiles().size() > 0) {
339
                        Intent viewIntent;
340
                        if(app.getCurFiles().get(position).getContentType().startsWith("application/directory")){                        
341
                                currentPath = app.getCurFiles().get(position).getCName() + "/";
342
                                loadCurrentDirectoryFiles();
343
                                displayCurrentFiles();
344
                        }
345

    
346
                        else{
347
                                viewIntent = new Intent(this, ContainerObjectDetails.class);
348
                                viewIntent.putExtra("container", app.getCurFiles().get(position));
349
                                viewIntent.putExtra("cdnUrl", container.getCdnUrl());
350
                                viewIntent.putExtra("containerNames", container.getName());
351
                                viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
352
                                startActivityForResult(viewIntent, 55); // arbitrary number; never
353
                                // used again
354
                        }
355
                }
356
        }
357

    
358
         
359
        
360
        @Override
361
        public boolean onCreateOptionsMenu(Menu menu) {
362
                super.onCreateOptionsMenu(menu);
363
                MenuInflater inflater = getMenuInflater();
364
                inflater.inflate(R.menu.view_container_object_list_menu, menu);
365
                return true;
366
        }
367

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

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

    
506
        @Override
507
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
508
                super.onActivityResult(requestCode, resultCode, data);
509

    
510
                if (resultCode == RESULT_OK && requestCode == 56) {
511
                        // a sub-activity kicked back, so we want to refresh the server list
512
                        loadFiles();
513
                }
514

    
515
                // deleted file so need to update the list
516
                if (requestCode == 55 && resultCode == 99) {
517
                        loadFiles();
518
                }
519

    
520
        }
521

    
522
        class FileAdapter extends ArrayAdapter<ContainerObjects> {
523
                FileAdapter() {
524
                        super(ContainerObjectsActivity.this,
525
                                        R.layout.listcontainerobjectcell, app.getCurFiles());        
526
                }
527

    
528
                public View getView(int position, View convertView, ViewGroup parent) {
529
                        ContainerObjects file = app.getCurFiles().get(position);
530
                        LayoutInflater inflater = getLayoutInflater();
531
                        View row = inflater.inflate(R.layout.listcontainerobjectcell,
532
                                        parent, false);
533

    
534
                        TextView label = (TextView) row.findViewById(R.id.label);
535
                        label.setText(getShortName(file.getCName()));
536

    
537
                        ImageView objectImage = (ImageView) row.findViewById(R.id.file_type_image);
538
                        if(file.getContentType().startsWith("application/directory")){
539
                                objectImage.setImageResource(R.drawable.folder);
540
                        } else {
541
                                objectImage.setImageResource(R.drawable.file);
542
                        }
543
                        
544
                        if (file.getBytes() >= bConver) {
545
                                megaBytes = Math.abs(file.getBytes() / bConver + 0.2);
546
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
547
                                sublabel.setText(megaBytes + " MB");
548
                        } else if (file.getBytes() >= kbConver) {
549
                                kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2);
550
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
551
                                sublabel.setText(kiloBytes + " KB");
552
                        } else {
553
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
554
                                sublabel.setText(file.getBytes() + " B");
555
                        }
556

    
557
                        return (row);
558
                }
559
        }
560

    
561
        private class LoadFilesTask extends
562
        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
563

    
564
                private CloudServersException exception;
565

    
566
                protected void onPreExecute(){
567
                        showDialog();
568
                        loadingFiles = true;
569
                }
570

    
571
                @Override
572
                protected ArrayList<ContainerObjects> doInBackground(String... path) {
573
                        ArrayList<ContainerObjects> files = null;
574
                        try {
575
                                if(container.getName().equals(Container.MYSHARED)){
576
                                        files = (new ContainerObjectManager(getContext())).createListMyShared(true,
577
                                                        container.getName(),new ContainerManager(getContext()).createList(true));
578
                                }
579
                                else if(container.getName().equals(Container.OTHERS)){
580
                                        
581
                                }
582
                                else{
583
                                        if(container.getOtherUser()==null)
584
                                                files = (new ContainerObjectManager(getContext())).createList(true,
585
                                                container.getName());
586
                                        else
587
                                                files = (new ContainerObjectManager(getContext())).createOtherList(true,
588
                                                                container.getName(), container.getOtherUser());
589
                                }
590
                        } catch (CloudServersException e) {
591
                                exception = e;
592
                                e.printStackTrace();
593
                        }
594
                        return files;
595
                }
596

    
597
                @Override
598
                protected void onPostExecute(ArrayList<ContainerObjects> result) {
599
                        loadingFiles = false;
600
                        hideDialog();
601
                        if (exception != null) {
602
                                showAlert("Error", exception.getMessage());
603
                        }
604
                        setFileList(result);
605
                        loadCurrentDirectoryFiles();
606
                        displayCurrentFiles();
607
                }
608

    
609
        }
610

    
611
        private class AddFolderTask extends
612
        AsyncTask<String, Void, HttpBundle> {
613

    
614
                private CloudServersException exception;
615

    
616
                @Override
617
                protected void onPreExecute(){
618
                        showDialog();
619
                        app.setAddingObject(true);
620
                        task = new AddObjectListenerTask();
621
                        task.execute();
622
                }
623

    
624
                @Override
625
                protected HttpBundle doInBackground(String... data) {
626
                        HttpBundle bundle = null;
627
                        try {
628

    
629
                                bundle = (new ContainerObjectManager(getContext())).addObject(container.getName(), currentPath, data[0], data[1]);
630
                        } catch (CloudServersException e) {
631
                                exception = e;
632
                        }
633
                        return bundle;
634
                }
635

    
636
                @Override
637
                protected void onPostExecute(HttpBundle bundle) {
638
                        app.setAddingObject(false);
639
                        hideDialog();
640
                        HttpResponse response = bundle.getResponse();
641
                        if (response != null) {
642
                                int statusCode = response.getStatusLine().getStatusCode();
643
                                if (statusCode == 201) {
644
                                        setResult(Activity.RESULT_OK);
645
                                        //loading the new files is done by ListenerTask
646
                                } else {
647
                                        CloudServersException cse = parseCloudServersException(response);
648
                                        if ("".equals(cse.getMessage())) {
649
                                                showError("There was a problem deleting your folder.", bundle);
650
                                        } else {
651
                                                showError("There was a problem deleting your folder: "
652
                                                                + cse.getMessage(), bundle);
653
                                        }
654
                                }
655
                        } else if (exception != null) {
656
                                showError("There was a problem deleting your folder: "
657
                                                + exception.getMessage(), bundle);
658
                        }
659
                }
660
        }
661

    
662
        private class DeleteObjectTask extends
663
        AsyncTask<Void, Void, HttpBundle> {
664

    
665
                private CloudServersException exception;
666

    
667
                @Override
668
                protected void onPreExecute(){
669
                        showDialog();
670
                        app.setDeleteingObject(true);
671
                        deleteObjTask = new DeleteObjectListenerTask();
672
                        deleteObjTask.execute();
673
                }
674

    
675
                @Override
676
                protected HttpBundle doInBackground(Void... arg0) {
677
                        HttpBundle bundle = null;
678
                        try {
679
                                //subtring because the current directory contains a "/" at the end of the string
680
                                bundle = (new ContainerObjectManager(getContext())).deleteObject(container.getName(), currentPath.substring(0, currentPath.length()-1));
681
                        } catch (CloudServersException e) {
682
                                exception = e;
683
                        }
684
                        return bundle;
685
                }
686

    
687
                @Override
688
                protected void onPostExecute(HttpBundle bundle) {
689
                        app.setDeleteingObject(false);
690
                        hideDialog();
691
                        HttpResponse response = bundle.getResponse();
692
                        if (response != null) {
693
                                int statusCode = response.getStatusLine().getStatusCode();
694
                                if (statusCode == 409) {
695
                                        showAlert("Error",
696
                                        "Folder must be empty in order to delete");
697
                                }
698
                                if (statusCode == 204) {
699
                                        setResult(Activity.RESULT_OK);
700
                                } else {
701
                                        CloudServersException cse = parseCloudServersException(response);
702
                                        if ("".equals(cse.getMessage())) {
703
                                                showError("There was a problem deleting your folder.", bundle);
704
                                        } else {
705
                                                showError("There was a problem deleting your folder: "
706
                                                                + cse.getMessage(), bundle);
707
                                        }
708
                                }
709
                        } else if (exception != null) {
710
                                showError("There was a problem deleting your folder: "
711
                                                + exception.getMessage(), bundle);
712
                        }
713
                }
714
        }
715

    
716
        private class DeleteContainerTask extends
717
        AsyncTask<String, Void, HttpBundle> {
718

    
719
                private CloudServersException exception;
720

    
721
                @Override
722
                protected void onPreExecute(){ 
723
                        showDialog();
724
                        app.setDeletingContainer(true);
725
                        deleteContainerTask = new DeleteContainerListenerTask();
726
                        deleteContainerTask.execute();
727
                }
728

    
729
                @Override
730
                protected HttpBundle doInBackground(String... object) {
731
                        HttpBundle bundle = null;
732
                        try {
733
                                bundle = (new ContainerManager(getContext())).delete(container.getName());
734
                        } catch (CloudServersException e) {
735
                                exception = e;
736
                        }
737
                        return bundle;
738
                }
739

    
740
                @Override
741
                protected void onPostExecute(HttpBundle bundle) {
742
                        hideDialog();
743
                        app.setDeletingContainer(false);
744
                        HttpResponse response = bundle.getResponse();
745
                        if (response != null) {
746
                                int statusCode = response.getStatusLine().getStatusCode();
747
                                if (statusCode == 409) {
748
                                        showError("Container must be empty in order to delete", bundle);
749
                                }
750
                                if (statusCode == 204) {
751
                                        setResult(Activity.RESULT_OK);
752

    
753
                                } else {
754
                                        CloudServersException cse = parseCloudServersException(response);
755
                                        if ("".equals(cse.getMessage())) {
756
                                                showError("There was a problem deleting your container.", bundle);
757
                                        } else {
758
                                                showError("There was a problem deleting your container: "
759
                                                                + cse.getMessage(), bundle);
760
                                        }
761
                                }
762
                        } else if (exception != null) {
763
                                showError("There was a problem deleting your server: "
764
                                                + exception.getMessage(), bundle);
765
                        }
766
                }
767
        }
768

    
769
        /*
770
         * listens for the application to change isProcessing
771
         * listens so activity knows when it should display
772
         * the new curDirFiles
773
         */
774
        private class AddObjectListenerTask extends
775
        AsyncTask<Void, Void, Void> {
776

    
777
                @Override
778
                protected Void doInBackground(Void... arg1) {
779

    
780
                        while(app.isAddingObject()){
781
                                // wait for process to finish
782
                                // or have it be canceled
783
                                if(task.isCancelled()){
784
                                        return null;
785
                                }
786
                        }
787
                        return null;
788
                }
789

    
790
                /*
791
                 * when no longer processing, time to load
792
                 * the new files
793
                 */
794
                @Override
795
                protected void onPostExecute(Void arg1) {
796
                        hideDialog();
797
                        loadFiles();
798
                }
799
        }
800

    
801

    
802
        private class DeleteObjectListenerTask extends
803
        AsyncTask<Void, Void, Void> {
804

    
805
                @Override
806
                protected Void doInBackground(Void... arg1) {
807

    
808
                        while(app.isDeletingObject()){
809
                                // wait for process to finish
810
                                // or have it be canceled
811
                                if(deleteObjTask.isCancelled()){
812
                                        return null;
813
                                }
814
                        }
815
                        return null;
816
                }
817

    
818
                /*
819
                 * when no longer processing, time to load
820
                 * the new files
821
                 */
822
                @Override
823
                protected void onPostExecute(Void arg1) {
824
                        hideDialog();
825
                        removeFromList(currentPath);
826
                        goUpDirectory();
827
                }
828
        }
829

    
830
        private class DeleteContainerListenerTask extends
831
        AsyncTask<Void, Void, Void> {
832

    
833
                @Override
834
                protected Void doInBackground(Void... arg1) {
835

    
836
                        while(app.isDeletingContainer()){
837
                                // wait for process to finish
838
                                // or have it be canceled
839
                                if(deleteContainerTask.isCancelled()){
840
                                        return null;
841
                                }
842
                        }
843
                        return null;
844
                }
845

    
846
                /*
847
                 * when no longer processing, time to load
848
                 * the new files
849
                 */
850
                @Override
851
                protected void onPostExecute(Void arg1) {
852

    
853
                        hideDialog();
854
                        setResult(RESULT_OK);
855
                        finish();
856
                }
857
        }
858
        
859
        
860
        public String getCurrentPath() {
861
                return currentPath;
862
        }
863
        
864
        public ContainerObjects[] getFiles() {
865
                return files;
866
        }
867
}