Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ContainerObjectsActivity.java @ 3e180b04

History | View | Annotate | Download (25.6 kB)

1
package com.rackspacecloud.android;
2

    
3
import java.io.IOException;
4
import java.io.StringReader;
5
import java.util.ArrayList;
6
import java.util.Arrays;
7

    
8
import javax.xml.parsers.FactoryConfigurationError;
9
import javax.xml.parsers.ParserConfigurationException;
10
import javax.xml.parsers.SAXParser;
11
import javax.xml.parsers.SAXParserFactory;
12

    
13
import org.apache.http.HttpResponse;
14
import org.apache.http.client.ClientProtocolException;
15
import org.apache.http.impl.client.BasicResponseHandler;
16
import org.xml.sax.InputSource;
17
import org.xml.sax.SAXException;
18
import org.xml.sax.XMLReader;
19

    
20
import android.app.Activity;
21
import android.app.AlertDialog;
22
import android.app.Dialog;
23
import android.app.ListActivity;
24
import android.app.ProgressDialog;
25
import android.content.Context;
26
import android.content.DialogInterface;
27
import android.content.Intent;
28
import android.os.AsyncTask;
29
import android.os.Bundle;
30
import android.util.Log;
31
import android.view.LayoutInflater;
32
import android.view.Menu;
33
import android.view.MenuInflater;
34
import android.view.MenuItem;
35
import android.view.View;
36
import android.view.ViewGroup;
37
import android.widget.ArrayAdapter;
38
import android.widget.EditText;
39
import android.widget.ListView;
40
import android.widget.TextView;
41

    
42
import com.rackspace.cloud.files.api.client.Container;
43
import com.rackspace.cloud.files.api.client.ContainerManager;
44
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
45
import com.rackspace.cloud.files.api.client.ContainerObjects;
46
import com.rackspace.cloud.servers.api.client.CloudServersException;
47
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
48
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
49

    
50
/**
51
 * 
52
 * @author Phillip Toohill
53
 * 
54
 */
55
public class ContainerObjectsActivity extends ListActivity {
56

    
57
        private static final int deleteContainer = 0;
58
        private static final int deleteFolder = 1;
59
        private ContainerObjects[] files;
60
        private static Container container;
61
        public String LOG = "viewFilesActivity";
62
        private String cdnEnabledIs;
63
        public Object megaBytes;
64
        public Object kiloBytes;
65
        public int bConver = 1048576;
66
        public int kbConver = 1024;
67
        private Context context;
68
        private String currentPath;
69
        private boolean loadingFiles;
70
        private boolean displayDialog;
71
        private ProgressDialog dialog;
72
        private AndroidCloudApplication app;
73
        private AddObjectListenerTask task;
74
        private DeleteObjectListenerTask deleteObjTask;
75
        private DeleteContainerListenerTask deleteContainerTask;
76

    
77

    
78
        @Override
79
        public void onCreate(Bundle savedInstanceState) {
80
                super.onCreate(savedInstanceState);
81
                container = (Container) this.getIntent().getExtras().get("container");
82
                Log.v(LOG, "CDNEnabled:" + container.isCdnEnabled());
83
                context = getApplicationContext();
84
                if (container.isCdnEnabled() == true) {
85
                        cdnEnabledIs = "true";
86
                } else {
87
                        cdnEnabledIs = "false";
88
                }
89
                restoreState(savedInstanceState);
90
        }
91

    
92
        @Override
93
        protected void onSaveInstanceState(Bundle outState) {
94
                super.onSaveInstanceState(outState);
95
                
96
                //files stores all the files in the container
97
                outState.putSerializable("container", files);
98
                
99
                //current path represents where you have "navigated" to
100
                outState.putString("path", currentPath);
101
                
102
                //stores state if you are loading or not 
103
                outState.putBoolean("loadingFiles", loadingFiles);
104
                
105
                //stores whether dialog is showing or not
106
                outState.putBoolean("displayDialog", displayDialog);
107
                
108
                //need to set authenticating back to true because it is set to false
109
                //in hideDialog()
110
                if(displayDialog){
111
                        hideDialog();
112
                        displayDialog = true;
113
                }
114
        }
115

    
116

    
117

    
118
        private void restoreState(Bundle state) {
119
                
120
                
121
                /*
122
                 * need reference to the app so you can access curDirFiles
123
                 * as well as processing status
124
                 */
125
                app = (AndroidCloudApplication)this.getApplication();
126

    
127
                if (state != null && state.containsKey("displayDialog") && state.getBoolean("displayDialog")) {
128
                    showDialog();
129
            } else {
130
                    hideDialog();
131
            }
132
                
133
                if(state != null){
134
                        if(state.containsKey("path")){
135
                                currentPath = state.getString("path");
136
                        }
137
                        else{
138
                                currentPath = "";
139
                        }
140

    
141
                        if(state.containsKey("loadingFiles") && state.getBoolean("loadingFiles")){
142
                                loadFiles();
143
                        }
144
                        else{
145
                                if(state.containsKey("container")){
146
                                        files = (ContainerObjects[]) state.getSerializable("container");
147
                                        if (app.getCurFiles() == null) {
148
                                                displayNoFilesCell();
149
                                        } else {
150
                                                getListView().setDividerHeight(1); // restore divider lines
151
                                                setListAdapter(new FileAdapter());
152

    
153
                                        }
154
                                }
155
                        }
156
                }
157
                else {
158
                        currentPath = "";
159
                        loadFiles();
160
                }        
161
                
162
                /*
163
                 * if the app is process when we enter the activity
164
                 * we must listen for the new curDirFiles list
165
                 */
166
                if(app.isAddingObject()){
167
                        task = new AddObjectListenerTask();
168
                        task.execute();
169
                }
170
                
171
                if(app.isDeletingObject()){
172
                        displayNoFilesCell();
173
                        deleteObjTask = new DeleteObjectListenerTask();
174
                        deleteObjTask.execute();
175
                }
176

    
177

    
178
        }
179
        
180
        @Override
181
        protected void onStart(){
182
                super.onStart();
183
                if(displayDialog){
184
                        showDialog();
185
                }
186
        }
187

    
188
        
189
        @Override
190
        protected void onStop(){
191
                super.onStop();
192

    
193
                if(displayDialog){
194
                        hideDialog();
195
                        displayDialog = true;
196
                }
197

    
198
                /*
199
                 * Need to stop running listener task
200
                 * if we exit
201
                 */
202
                if(task != null){
203
                        task.cancel(true);
204
                }
205
                
206
                if(deleteObjTask != null){
207
                        deleteObjTask.cancel(true);
208
                }
209
                
210
                if(deleteContainerTask != null){
211
                        deleteContainerTask.cancel(true);
212
                }
213

    
214
        }
215

    
216
        /*
217
         * overriding back button press, because we are not actually changing
218
         * activities when we navigate the file structure
219
         */
220
        public void onBackPressed() {
221
                if(currentPath.equals("")){
222
                        finish();
223
                }
224
                else{
225
                        goUpDirectory();
226
                }
227
        }
228

    
229
        /*
230
         * go to the current directory's parent and display that data
231
         */
232
        private void goUpDirectory(){
233
                currentPath = currentPath.substring(0, currentPath.substring(0, currentPath.length()-2).lastIndexOf("/")+1);
234
                loadCurrentDirectoryFiles();
235
                displayCurrentFiles();
236
        }
237

    
238
        /*
239
         * load all file that are in the container
240
         */
241
        private void loadFiles() {
242
                //displayLoadingCell();
243
                new LoadFilesTask().execute();
244
        }
245

    
246
        /*
247
        private void displayLoadingCell() {
248
                String a[] = new String[1];
249
                a[0] = "Loading...";
250
                setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell,
251
                                R.id.loading_label, a));
252
                getListView().setTextFilterEnabled(true);
253
                getListView().setDividerHeight(0); // hide the dividers so it won't look
254
                                                                                        // like a list row
255
                getListView().setItemsCanFocus(false);
256
        }
257
         */
258

    
259
        /* load only the files that should display for the 
260
         * current directory in the curDirFiles[]
261
         */
262
        private void loadCurrentDirectoryFiles(){
263
                ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>();
264

    
265
                if(files != null){
266
                        for(int i = 0 ; i < files.length; i ++){
267
                                if(fileBelongsInDir(files[i])){
268
                                        curFiles.add(files[i]);
269
                                }
270
                        }
271
                        app.setCurFiles(curFiles);
272
                }
273
        }
274

    
275
        /*
276
         * determines if a file should be displayed in current 
277
         * directory
278
         */
279
        private Boolean fileBelongsInDir(ContainerObjects obj){
280
                String objPath = obj.getCName();
281
                if(!objPath.startsWith(currentPath)){
282
                        return false;
283
                }
284
                else{
285
                        objPath = objPath.substring(currentPath.length());
286
                        return !objPath.contains("/");
287
                }
288
        }
289

    
290

    
291
        /*
292
         * loads all the files that are in the container
293
         * into one array
294
         */
295
        private void setFileList(ArrayList<ContainerObjects> files) {
296
                if (files == null) {
297
                        files = new ArrayList<ContainerObjects>();
298
                }
299
                String[] fileNames = new String[files.size()];
300
                this.files = new ContainerObjects[files.size()];
301

    
302

    
303

    
304
                if (files != null) {
305
                        for (int i = 0; i < files.size(); i++) {
306
                                ContainerObjects file = files.get(i);
307
                                this.files[i] = file;
308
                                fileNames[i] = file.getName();
309
                        }
310
                }
311
        }
312

    
313
        private void displayCurrentFiles(){
314
                if (app.getCurFiles().size() == 0) {
315
                        displayNoFilesCell();
316
                } else {
317
                        ArrayList<ContainerObjects> tempList = new ArrayList<ContainerObjects>();
318
                        for(int i = 0; i < app.getCurFiles().size(); i++){
319
                                tempList.add(app.getCurFiles().get(i));
320
                        }
321
                        /*
322
                        adapter.clear();
323
                        for(int i = 0; i < tempList.size(); i++){
324
                                adapter.add(tempList.get(i));
325
                                Log.d("info", "the count is: " + adapter.getCount());
326
                        }
327
                        */
328
                        getListView().setDividerHeight(1); // restore divider lines
329
                        setListAdapter(new FileAdapter());
330
                }
331
        }
332

    
333
        /*
334
         * display a different empty page depending
335
         * of if you are at top of container or
336
         * in a folder
337
         */
338
        private void displayNoFilesCell() {
339
                String a[] = new String[1];
340
                if(currentPath.equals("")){
341
                        a[0] = "Empty Container";
342
                        setListAdapter(new ArrayAdapter<String>(this, R.layout.noobjectscell,
343
                                R.id.no_files_label, a));
344
                }
345
                else{
346
                        a[0] = "No Files";
347
                        setListAdapter(new ArrayAdapter<String>(this, R.layout.nofilescell,
348
                                                R.id.no_files_label, a));
349
                }
350
                getListView().setTextFilterEnabled(true);
351
                getListView().setDividerHeight(0); // hide the dividers so it won't look
352
                                                                                        // like a list row
353
                getListView().setItemsCanFocus(false);
354
        }
355

    
356

    
357
        private void showAlert(String title, String message) {
358
                // Can't create handler inside thread that has not called
359
                // Looper.prepare()
360
                // Looper.prepare();
361
                try {
362
                        AlertDialog alert = new AlertDialog.Builder(this).create();
363
                        alert.setTitle(title);
364
                        alert.setMessage(message);
365
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
366
                                public void onClick(DialogInterface dialog, int which) {
367
                                        return;
368
                                }
369
                        });
370
                        alert.show();
371
                } catch (Exception e) {
372
                        e.printStackTrace();
373
                }
374
        }
375

    
376
        /* just get the last part of the filename
377
         * so the entire file path does not show
378
         */
379
        private String getShortName(String longName){
380
                String s = longName;
381
                if(!s.contains("/")){
382
                        return s;
383
                }
384
                else {
385
                        return s.substring(s.lastIndexOf('/')+1);
386
                }
387
        }
388

    
389
        /*
390
         * removed a specified object from the array of 
391
         * all the files in the container
392
         */
393
        private void removeFromList(String path){
394
                ArrayList<ContainerObjects> temp = new ArrayList<ContainerObjects>(Arrays.asList(files));
395
                for(int i = 0; i < files.length; i++){
396
                        if(files[i].getCName().equals(path.substring(0, path.length()-1))){
397
                                temp.remove(i);
398
                        }
399
                }
400
                files = new ContainerObjects[temp.size()];
401
                for(int i = 0; i < temp.size(); i++){
402
                        files[i] = temp.get(i);
403
                }
404
        }
405

    
406
        protected void onListItemClick(ListView l, View v, int position, long id) {
407
                if (app.getCurFiles() != null && app.getCurFiles().size() > 0) {
408
                        Intent viewIntent;
409
                        if(app.getCurFiles().get(position).getContentType().equals("application/directory")){                        
410
                                currentPath = app.getCurFiles().get(position).getCName() + "/";
411
                                loadCurrentDirectoryFiles();
412
                                displayCurrentFiles();
413
                        }
414

    
415
                        else{
416
                                viewIntent = new Intent(this, ContainerObjectDetails.class);
417
                                viewIntent.putExtra("container", app.getCurFiles().get(position));
418
                                viewIntent.putExtra("cdnUrl", container.getCdnUrl());
419
                                viewIntent.putExtra("containerNames", container.getName());
420
                                viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
421
                                startActivityForResult(viewIntent, 55); // arbitrary number; never
422
                                // used again
423
                        }
424
                }
425
        }
426

    
427
        /* 
428
         * Create the Menu options
429
         */
430
        @Override
431
        public boolean onCreateOptionsMenu(Menu menu) {
432
                super.onCreateOptionsMenu(menu);
433
                MenuInflater inflater = getMenuInflater();
434
                inflater.inflate(R.menu.view_container_object_list_menu, menu);
435
                return true;
436
        }
437

    
438
        @Override
439
        /*
440
         * option performed for delete depends on if you
441
         * are at the top of a container or in a folder
442
         */
443
        public boolean onOptionsItemSelected(MenuItem item) {
444
                switch (item.getItemId()) {
445
                case R.id.delete_container:
446
                        if(currentPath.equals("")){
447
                                showDialog(deleteContainer);
448
                        }
449
                        else{
450
                                showDialog(deleteFolder);
451
                        }
452
                        return true;
453
                case R.id.enable_cdn:
454
                        Intent viewIntent1 = new Intent(this, EnableCDNActivity.class);
455
                        viewIntent1.putExtra("Cname", container.getName());
456
                        startActivityForResult(viewIntent1, 56);
457
                        return true;
458
                case R.id.refresh:
459
                        loadFiles();
460
                        return true;
461
                case R.id.add_folder:
462
                        showDialog(R.id.add_folder);
463
                        return true;
464
                case R.id.add_file:
465
                        Intent viewIntent2 = new Intent(this, AddFileActivity.class);
466
                        viewIntent2.putExtra("Cname", container.getName());
467
                        viewIntent2.putExtra("curPath", currentPath);
468
                        startActivityForResult(viewIntent2, 56);
469
                        return true;        
470
                }
471
                return false;
472
        }
473

    
474
        @Override
475
        protected Dialog onCreateDialog(int id) {
476
                switch (id) {
477
                case deleteContainer:
478
                        if(app.getCurFiles().size() == 0){
479
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
480
                                .setIcon(R.drawable.alert_dialog_icon)
481
                                .setTitle("Delete Container")
482
                                .setMessage(
483
                                "Are you sure you want to delete this Container?")
484
                                .setPositiveButton("Delete Container",
485
                                                new DialogInterface.OnClickListener() {
486
                                        public void onClick(DialogInterface dialog,
487
                                                        int whichButton) {
488
                                                // User clicked OK so do some stuff
489
                                                new DeleteContainerTask()
490
                                                .execute(currentPath);
491
                                        }
492
                                })
493
                                .setNegativeButton("Cancel",
494
                                                new DialogInterface.OnClickListener() {
495
                                        public void onClick(DialogInterface dialog,
496
                                                        int whichButton) {
497
                                                // User clicked Cancel so do some stuff
498
                                        }
499
                                }).create();
500
                        }
501
                        else{
502
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
503
                                .setIcon(R.drawable.alert_dialog_icon)
504
                                .setTitle("Delete Container")
505
                                .setMessage("Container must be empty to delete")
506
                                .setNegativeButton("OK",
507
                                                new DialogInterface.OnClickListener() {
508
                                        public void onClick(DialogInterface dialog,
509
                                                        int whichButton) {
510
                                                // User clicked Cancel so do some stuff
511
                                        }
512
                                }).create();
513
                        }
514
                case deleteFolder:
515
                        if(app.getCurFiles().size() == 0){
516
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
517
                                .setIcon(R.drawable.alert_dialog_icon)
518
                                .setTitle("Delete Folder")
519
                                .setMessage(
520
                                "Are you sure you want to delete this Folder?")
521
                                .setPositiveButton("Delete Folder",
522
                                                new DialogInterface.OnClickListener() {
523
                                        public void onClick(DialogInterface dialog,
524
                                                        int whichButton) {
525
                                                // User clicked OK so do some stuff
526
                                                new DeleteObjectTask().execute();
527
                                        }
528
                                })
529
                                .setNegativeButton("Cancel",
530
                                                new DialogInterface.OnClickListener() {
531
                                        public void onClick(DialogInterface dialog,
532
                                                        int whichButton) {
533
                                                // User clicked Cancel so do some stuff
534
                                        }
535
                                }).create();
536
                        }
537
                        else{
538
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
539
                                .setIcon(R.drawable.alert_dialog_icon)
540
                                .setTitle("Delete Folder")
541
                                .setMessage(
542
                                "Folder must be empty to delete")
543
                                .setNegativeButton("OK",
544
                                                new DialogInterface.OnClickListener() {
545
                                        public void onClick(DialogInterface dialog,
546
                                                        int whichButton) {
547
                                                // User clicked Cancel so do some stuff
548
                                        }
549
                                }).create();
550
                        }
551
                case R.id.add_folder:
552
                        final EditText input = new EditText(this);
553
                        return new AlertDialog.Builder(ContainerObjectsActivity.this)
554
                        .setIcon(R.drawable.alert_dialog_icon)
555
                        .setView(input)
556
                        .setTitle("Add Folder")
557
                        .setMessage("Enter new name for folder: ")                         
558
                        .setPositiveButton("Add", new DialogInterface.OnClickListener() {
559
                                public void onClick(DialogInterface dialog, int whichButton) {
560
                                        //User clicked OK so do some stuff
561
                                        String[] info = {input.getText().toString(), "application/directory"};
562
                                        new AddFolderTask().execute(info);
563
                                }
564
                        })
565
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
566
                                public void onClick(DialogInterface dialog, int whichButton) {
567
                                        // User clicked Cancel so do some stuff
568
                                }
569
                        })
570
                        .create();     
571
                }
572
                return null;
573
        }
574

    
575
        @Override
576
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
577
                super.onActivityResult(requestCode, resultCode, data);
578
                        
579
                if (resultCode == RESULT_OK && requestCode == 56) {
580
                        // a sub-activity kicked back, so we want to refresh the server list
581
                        loadFiles();
582
                }
583
                
584
                // deleted file so need to update the list
585
                if (requestCode == 55 && resultCode == 99) {
586
                        loadFiles();
587
                }
588
                
589
        }
590

    
591
        private CloudServersException parseCloudServersException(
592
                        HttpResponse response) {
593
                CloudServersException cse = new CloudServersException();
594
                try {
595
                        BasicResponseHandler responseHandler = new BasicResponseHandler();
596
                        String body = responseHandler.handleResponse(response);
597
                        CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
598
                        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
599
                        XMLReader xmlReader = saxParser.getXMLReader();
600
                        xmlReader.setContentHandler(parser);
601
                        xmlReader.parse(new InputSource(new StringReader(body)));
602
                        cse = parser.getException();
603
                } catch (ClientProtocolException e) {
604
                        cse = new CloudServersException();
605
                        cse.setMessage(e.getLocalizedMessage());
606
                } catch (IOException e) {
607
                        cse = new CloudServersException();
608
                        cse.setMessage(e.getLocalizedMessage());
609
                } catch (ParserConfigurationException e) {
610
                        cse = new CloudServersException();
611
                        cse.setMessage(e.getLocalizedMessage());
612
                } catch (SAXException e) {
613
                        cse = new CloudServersException();
614
                        cse.setMessage(e.getLocalizedMessage());
615
                } catch (FactoryConfigurationError e) {
616
                        cse = new CloudServersException();
617
                        cse.setMessage(e.getLocalizedMessage());
618
                }
619
                return cse;
620
        }
621

    
622
        private void startFileError(String message, HttpBundle bundle){
623
                Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
624
                viewIntent.putExtra("errorMessage", message);
625
                viewIntent.putExtra("response", bundle.getResponseText());
626
                viewIntent.putExtra("request", bundle.getCurlRequest());
627
                startActivity(viewIntent);
628
        }
629
        
630
        private void showDialog() {
631
                if(dialog == null || !dialog.isShowing()){
632
                        displayDialog = true;
633
                        dialog = ProgressDialog.show(ContainerObjectsActivity.this, "", "Loading...", true);
634
                }
635
    }
636
    
637
    private void hideDialog() {
638
            if(dialog != null){
639
                    dialog.dismiss();
640
            }
641
            displayDialog = false;
642
    }
643

    
644
        class FileAdapter extends ArrayAdapter<ContainerObjects> {
645
                FileAdapter() {
646
                        super(ContainerObjectsActivity.this,
647
                                        R.layout.listcontainerobjectcell, app.getCurFiles());        
648
                }
649

    
650
                public View getView(int position, View convertView, ViewGroup parent) {
651
                        ContainerObjects file = app.getCurFiles().get(position);
652
                        LayoutInflater inflater = getLayoutInflater();
653
                        View row = inflater.inflate(R.layout.listcontainerobjectcell,
654
                                        parent, false);
655

    
656
                        TextView label = (TextView) row.findViewById(R.id.label);
657
                        //label.setText(file.getCName());
658
                        label.setText(getShortName(file.getCName()));
659

    
660
                        if (file.getBytes() >= bConver) {
661
                                megaBytes = Math.abs(file.getBytes() / bConver + 0.2);
662
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
663
                                sublabel.setText(megaBytes + " MB");
664
                        } else if (file.getBytes() >= kbConver) {
665
                                kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2);
666
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
667
                                sublabel.setText(kiloBytes + " KB");
668
                        } else {
669
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
670
                                sublabel.setText(file.getBytes() + " B");
671
                        }
672

    
673
                        return (row);
674
                }
675
        }
676

    
677
        private class LoadFilesTask extends
678
        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
679

    
680
                private CloudServersException exception;
681
                protected void onPreExecute(){
682
                        showDialog();
683
                        loadingFiles = true;
684
                }
685

    
686
                @Override
687
                protected ArrayList<ContainerObjects> doInBackground(String... path) {
688
                        ArrayList<ContainerObjects> files = null;
689
                        try {
690
                                files = (new ContainerObjectManager(context)).createList(true,
691
                                                container.getName());
692
                        } catch (CloudServersException e) {
693
                                exception = e;
694
                                e.printStackTrace();
695
                        }
696
                        return files;
697
                }
698

    
699
                @Override
700
                protected void onPostExecute(ArrayList<ContainerObjects> result) {
701
                        hideDialog();
702
                        
703
                        if (exception != null) {
704
                                showAlert("Error", exception.getMessage());
705
                        }
706

    
707
                        setFileList(result);
708
                        loadCurrentDirectoryFiles();
709
                        displayCurrentFiles();
710
                        loadingFiles = false;
711
                }
712

    
713
        }
714

    
715
        private class AddFolderTask extends
716
        AsyncTask<String, Void, HttpBundle> {
717

    
718
                private CloudServersException exception;
719

    
720
                @Override
721
                protected void onPreExecute(){
722
                        showDialog();
723
                        app.setAddingObject(true);
724
                        task = new AddObjectListenerTask();
725
                        task.execute();
726
                }
727

    
728
                @Override
729
                protected HttpBundle doInBackground(String... data) {
730
                        HttpBundle bundle = null;
731
                        try {
732
                                
733
                                bundle = (new ContainerObjectManager(context)).addObject(container.getName(), currentPath, data[0], data[1]);
734
                        } catch (CloudServersException e) {
735
                                exception = e;
736
                        }
737
                        return bundle;
738
                }
739

    
740
                @Override
741
                protected void onPostExecute(HttpBundle bundle) {
742
                        app.setAddingObject(false);
743
                        HttpResponse response = bundle.getResponse();
744
                        if (response != null) {
745
                                int statusCode = response.getStatusLine().getStatusCode();
746
                                if (statusCode == 201) {
747
                                        setResult(Activity.RESULT_OK);
748
                                        //loading the new files is done by ListenerTask
749
                                } else {
750
                                        hideDialog();
751
                                        CloudServersException cse = parseCloudServersException(response);
752
                                        if ("".equals(cse.getMessage())) {
753
                                                startFileError("There was a problem deleting your folder.", bundle);
754
                                        } else {
755
                                                startFileError("There was a problem deleting your folder: "
756
                                                                + cse.getMessage(), bundle);
757
                                        }
758
                                }
759
                        } else if (exception != null) {
760
                                hideDialog();
761
                                startFileError("There was a problem deleting your folder: "
762
                                                + exception.getMessage(), bundle);
763
                        }
764
                }
765
        }
766

    
767
        private class DeleteObjectTask extends
768
        AsyncTask<Void, Void, HttpBundle> {
769
        
770
                private CloudServersException exception;
771
        
772
                @Override
773
                protected void onPreExecute(){
774
                        showDialog();
775
                        app.setDeleteingObject(true);
776
                        deleteObjTask = new DeleteObjectListenerTask();
777
                        deleteObjTask.execute();
778
                }
779
        
780
                @Override
781
                protected HttpBundle doInBackground(Void... arg0) {
782
                        HttpBundle bundle = null;
783
                        try {
784
                                //subtring because the current directory contains a "/" at the end of the string
785
                                bundle = (new ContainerObjectManager(context)).deleteObject(container.getName(), currentPath.substring(0, currentPath.length()-1));
786
                        } catch (CloudServersException e) {
787
                                exception = e;
788
                        }
789
                        return bundle;
790
                }
791
        
792
                @Override
793
                protected void onPostExecute(HttpBundle bundle) {
794
                        app.setDeleteingObject(false);
795
                        HttpResponse response = bundle.getResponse();
796
                        if (response != null) {
797
                                int statusCode = response.getStatusLine().getStatusCode();
798
                                if (statusCode == 409) {
799
                                        hideDialog();
800
                                        showAlert("Error",
801
                                        "Folder must be empty in order to delete");
802
                                }
803
                                if (statusCode == 204) {
804
                                        setResult(Activity.RESULT_OK);
805
                                } else {
806
                                        hideDialog();
807
                                        CloudServersException cse = parseCloudServersException(response);
808
                                        if ("".equals(cse.getMessage())) {
809
                                                startFileError("There was a problem deleting your folder.", bundle);
810
                                        } else {
811
                                                startFileError("There was a problem deleting your folder: "
812
                                                                + cse.getMessage(), bundle);
813
                                        }
814
                                }
815
                        } else if (exception != null) {
816
                                hideDialog();
817
                                startFileError("There was a problem deleting your folder: "
818
                                                + exception.getMessage(), bundle);
819
                        }
820
                }
821
        }
822

    
823
        private class DeleteContainerTask extends
824
        AsyncTask<String, Void, HttpBundle> {
825

    
826
                private CloudServersException exception;
827

    
828
                @Override
829
                protected void onPreExecute(){
830
                        showDialog();
831
                        app.setDeletingContainer(true);
832
                        deleteContainerTask = new DeleteContainerListenerTask();
833
                        deleteContainerTask.execute();
834
                }
835
                
836
                @Override
837
                protected HttpBundle doInBackground(String... object) {
838
                        HttpBundle bundle = null;
839
                        try {
840
                                
841
                                bundle = (new ContainerManager(context)).delete(container.getName());
842
                        } catch (CloudServersException e) {
843
                                exception = e;
844
                        }
845
                        return bundle;
846
                }
847

    
848
                @Override
849
                protected void onPostExecute(HttpBundle bundle) {
850
                        hideDialog();
851
                        app.setDeletingContainer(false);
852
                        HttpResponse response = bundle.getResponse();
853
                        if (response != null) {
854
                                int statusCode = response.getStatusLine().getStatusCode();
855
                                if (statusCode == 409) {
856
                                        startFileError("Container must be empty in order to delete", bundle);
857
                                }
858
                                if (statusCode == 204) {
859
                                        setResult(Activity.RESULT_OK);
860

    
861
                                } else {
862
                                        CloudServersException cse = parseCloudServersException(response);
863
                                        if ("".equals(cse.getMessage())) {
864
                                                startFileError("There was a problem deleting your container.", bundle);
865
                                        } else {
866
                                                startFileError("There was a problem deleting your container: "
867
                                                                + cse.getMessage(), bundle);
868
                                        }
869
                                }
870
                        } else if (exception != null) {
871
                                startFileError("There was a problem deleting your server: "
872
                                                + exception.getMessage(), bundle);
873
                        }
874
                }
875
        }
876

    
877
        /*
878
         * listens for the application to change isProcessing
879
         * listens so activity knows when it should display
880
         * the new curDirFiles
881
         */
882
        private class AddObjectListenerTask extends
883
        AsyncTask<Void, Void, Void> {
884
                
885
                @Override
886
                protected Void doInBackground(Void... arg1) {
887

    
888
                        while(app.isAddingObject()){
889
                                // wait for process to finish
890
                                // or have it be canceled
891
                                if(task.isCancelled()){
892
                                        return null;
893
                                }
894
                        }
895
                        return null;
896
                }
897

    
898
                /*
899
                 * when no longer processing, time to load
900
                 * the new files
901
                 */
902
                @Override
903
                protected void onPostExecute(Void arg1) {
904
                        loadFiles();
905
                }
906
        }
907
        
908
        
909
        private class DeleteObjectListenerTask extends
910
        AsyncTask<Void, Void, Void> {
911
                
912
                @Override
913
                protected Void doInBackground(Void... arg1) {
914

    
915
                        while(app.isDeletingObject()){
916
                                // wait for process to finish
917
                                // or have it be canceled
918
                                if(deleteObjTask.isCancelled()){
919
                                        return null;
920
                                }
921
                        }
922
                        return null;
923
                }
924

    
925
                /*
926
                 * when no longer processing, time to load
927
                 * the new files
928
                 */
929
                @Override
930
                protected void onPostExecute(Void arg1) {
931
                        hideDialog();
932
                        removeFromList(currentPath);
933
                        goUpDirectory();
934
                }
935
        }
936
        
937
        private class DeleteContainerListenerTask extends
938
        AsyncTask<Void, Void, Void> {
939
                
940
                @Override
941
                protected Void doInBackground(Void... arg1) {
942

    
943
                        while(app.isDeletingContainer()){
944
                                // wait for process to finish
945
                                // or have it be canceled
946
                                if(deleteContainerTask.isCancelled()){
947
                                        return null;
948
                                }
949
                        }
950
                        return null;
951
                }
952

    
953
                /*
954
                 * when no longer processing, time to load
955
                 * the new files
956
                 */
957
                @Override
958
                protected void onPostExecute(Void arg1) {
959
                        setResult(RESULT_OK);
960
                        finish();
961
                }
962
        }
963
}