Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ContainerObjectsActivity.java @ e6c34a0f

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

    
207
        /*
208
         * overriding back button press, because we are not actually changing
209
         * activities when we navigate the file structure
210
         */
211
        public void onBackPressed() {
212
                if(currentPath.equals("")){
213
                        finish();
214
                }
215
                else{
216
                        goUpDirectory();
217
                }
218
        }
219

    
220
        /*
221
         * go to the current directory's parent and display that data
222
         */
223
        private void goUpDirectory(){
224
                currentPath = currentPath.substring(0, currentPath.substring(0, currentPath.length()-2).lastIndexOf("/")+1);
225
                loadCurrentDirectoryFiles();
226
                displayCurrentFiles();
227
        }
228

    
229
        /*
230
         * load all file that are in the container
231
         */
232
        private void loadFiles() {
233
                //displayLoadingCell();
234
                new LoadFilesTask().execute();
235
        }
236

    
237
        /*
238
        private void displayLoadingCell() {
239
                String a[] = new String[1];
240
                a[0] = "Loading...";
241
                setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell,
242
                                R.id.loading_label, a));
243
                getListView().setTextFilterEnabled(true);
244
                getListView().setDividerHeight(0); // hide the dividers so it won't look
245
                                                                                        // like a list row
246
                getListView().setItemsCanFocus(false);
247
        }
248
         */
249

    
250
        /* load only the files that should display for the 
251
         * current directory in the curDirFiles[]
252
         */
253
        private void loadCurrentDirectoryFiles(){
254
                ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>();
255

    
256
                if(files != null){
257
                        for(int i = 0 ; i < files.length; i ++){
258
                                if(fileBelongsInDir(files[i])){
259
                                        curFiles.add(files[i]);
260
                                }
261
                        }
262
                        app.setCurFiles(curFiles);
263
                }
264
        }
265

    
266
        /*
267
         * determines if a file should be displayed in current 
268
         * directory
269
         */
270
        private Boolean fileBelongsInDir(ContainerObjects obj){
271
                String objPath = obj.getCName();
272
                if(!objPath.startsWith(currentPath)){
273
                        return false;
274
                }
275
                else{
276
                        objPath = objPath.substring(currentPath.length());
277
                        return !objPath.contains("/");
278
                }
279
        }
280

    
281

    
282
        /*
283
         * loads all the files that are in the container
284
         * into one array
285
         */
286
        private void setFileList(ArrayList<ContainerObjects> files) {
287
                if (files == null) {
288
                        files = new ArrayList<ContainerObjects>();
289
                }
290
                String[] fileNames = new String[files.size()];
291
                this.files = new ContainerObjects[files.size()];
292

    
293

    
294

    
295
                if (files != null) {
296
                        for (int i = 0; i < files.size(); i++) {
297
                                ContainerObjects file = files.get(i);
298
                                this.files[i] = file;
299
                                fileNames[i] = file.getName();
300
                        }
301
                }
302
        }
303

    
304
        private void displayCurrentFiles(){
305
                if (app.getCurFiles().size() == 0) {
306
                        displayNoFilesCell();
307
                } else {
308
                        ArrayList<ContainerObjects> tempList = new ArrayList<ContainerObjects>();
309
                        for(int i = 0; i < app.getCurFiles().size(); i++){
310
                                tempList.add(app.getCurFiles().get(i));
311
                        }
312
                        /*
313
                        adapter.clear();
314
                        for(int i = 0; i < tempList.size(); i++){
315
                                adapter.add(tempList.get(i));
316
                                Log.d("info", "the count is: " + adapter.getCount());
317
                        }
318
                        */
319
                        getListView().setDividerHeight(1); // restore divider lines
320
                        setListAdapter(new FileAdapter());
321
                }
322
        }
323

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

    
347

    
348
        private void showAlert(String title, String message) {
349
                // Can't create handler inside thread that has not called
350
                // Looper.prepare()
351
                // Looper.prepare();
352
                try {
353
                        AlertDialog alert = new AlertDialog.Builder(this).create();
354
                        alert.setTitle(title);
355
                        alert.setMessage(message);
356
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
357
                                public void onClick(DialogInterface dialog, int which) {
358
                                        return;
359
                                }
360
                        });
361
                        alert.show();
362
                } catch (Exception e) {
363
                        e.printStackTrace();
364
                }
365
        }
366

    
367
        /* just get the last part of the filename
368
         * so the entire file path does not show
369
         */
370
        private String getShortName(String longName){
371
                String s = longName;
372
                if(!s.contains("/")){
373
                        return s;
374
                }
375
                else {
376
                        return s.substring(s.lastIndexOf('/')+1);
377
                }
378
        }
379

    
380
        /*
381
         * removed a specified object from the array of 
382
         * all the files in the container
383
         */
384
        private void removeFromList(String path){
385
                ArrayList<ContainerObjects> temp = new ArrayList<ContainerObjects>(Arrays.asList(files));
386
                for(int i = 0; i < files.length; i++){
387
                        if(files[i].getCName().equals(path.substring(0, path.length()-1))){
388
                                temp.remove(i);
389
                        }
390
                }
391
                files = new ContainerObjects[temp.size()];
392
                for(int i = 0; i < temp.size(); i++){
393
                        files[i] = temp.get(i);
394
                }
395
        }
396

    
397
        protected void onListItemClick(ListView l, View v, int position, long id) {
398
                if (app.getCurFiles() != null && app.getCurFiles().size() > 0) {
399
                        Intent viewIntent;
400
                        if(app.getCurFiles().get(position).getContentType().equals("application/directory")){                        
401
                                currentPath = app.getCurFiles().get(position).getCName() + "/";
402
                                loadCurrentDirectoryFiles();
403
                                displayCurrentFiles();
404
                        }
405

    
406
                        else{
407
                                viewIntent = new Intent(this, ContainerObjectDetails.class);
408
                                viewIntent.putExtra("container", app.getCurFiles().get(position));
409
                                viewIntent.putExtra("cdnUrl", container.getCdnUrl());
410
                                viewIntent.putExtra("containerNames", container.getName());
411
                                viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
412
                                startActivityForResult(viewIntent, 55); // arbitrary number; never
413
                                // used again
414
                        }
415
                }
416
        }
417

    
418
        /* 
419
         * Create the Menu options
420
         */
421
        @Override
422
        public boolean onCreateOptionsMenu(Menu menu) {
423
                super.onCreateOptionsMenu(menu);
424
                MenuInflater inflater = getMenuInflater();
425
                inflater.inflate(R.menu.view_container_object_list_menu, menu);
426
                return true;
427
        }
428

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

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

    
566
        @Override
567
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
568
                super.onActivityResult(requestCode, resultCode, data);
569
                        
570
                if (resultCode == RESULT_OK && requestCode == 56) {
571
                        Log.d("info", "top called");
572
                        // a sub-activity kicked back, so we want to refresh the server list
573
                        loadFiles();
574
                }
575
                /*
576
                if (requestCode == 55) {
577
                        Log.d("info", "bottom called");
578
                        if (resultCode == RESULT_OK) {
579
                                Intent viewIntent1 = new Intent(this,
580
                                                ListContainerActivity.class);
581
                                startActivityForResult(viewIntent1, 55);
582
                        }
583
                }
584
                */
585
        }
586

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

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

    
640
        class FileAdapter extends ArrayAdapter<ContainerObjects> {
641
                FileAdapter() {
642
                        super(ContainerObjectsActivity.this,
643
                                        R.layout.listcontainerobjectcell, app.getCurFiles());        
644
                }
645

    
646
                public View getView(int position, View convertView, ViewGroup parent) {
647
                        ContainerObjects file = app.getCurFiles().get(position);
648
                        LayoutInflater inflater = getLayoutInflater();
649
                        View row = inflater.inflate(R.layout.listcontainerobjectcell,
650
                                        parent, false);
651

    
652
                        TextView label = (TextView) row.findViewById(R.id.label);
653
                        //label.setText(file.getCName());
654
                        label.setText(getShortName(file.getCName()));
655

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

    
669
                        return (row);
670
                }
671
        }
672

    
673
        private class LoadFilesTask extends
674
        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
675

    
676
                private CloudServersException exception;
677
                protected void onPreExecute(){
678
                        showDialog();
679
                        loadingFiles = true;
680
                }
681

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

    
695
                @Override
696
                protected void onPostExecute(ArrayList<ContainerObjects> result) {
697
                        hideDialog();
698
                        
699
                        if (exception != null) {
700
                                showAlert("Error", exception.getMessage());
701
                        }
702

    
703
                        setFileList(result);
704
                        loadCurrentDirectoryFiles();
705
                        displayCurrentFiles();
706
                        loadingFiles = false;
707
                }
708

    
709
        }
710

    
711
        private class AddFolderTask extends
712
        AsyncTask<String, Void, HttpBundle> {
713

    
714
                private CloudServersException exception;
715

    
716
                @Override
717
                protected void onPreExecute(){
718
                        showDialog();
719
                        app.setAddingObject(true);
720
                        task = new AddObjectListenerTask();
721
                        task.execute();
722
                }
723

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

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

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

    
818
        private class DeleteContainerTask extends
819
        AsyncTask<String, Void, HttpBundle> {
820

    
821
                private CloudServersException exception;
822

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

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

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

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

    
883
                        while(app.isAddingObject()){
884
                                // wait for process to finish
885
                                // or have it be canceled
886
                                if(task.isCancelled()){
887
                                        return null;
888
                                }
889
                        }
890
                        return null;
891
                }
892

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

    
910
                        while(app.isDeletingObject()){
911
                                // wait for process to finish
912
                                // or have it be canceled
913
                                if(deleteObjTask.isCancelled()){
914
                                        return null;
915
                                }
916
                        }
917
                        return null;
918
                }
919

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

    
938
                        while(app.isDeletingContainer()){
939
                                // wait for process to finish
940
                                // or have it be canceled
941
                                if(deleteContainerTask.isCancelled()){
942
                                        return null;
943
                                }
944
                        }
945
                        return null;
946
                }
947

    
948
                /*
949
                 * when no longer processing, time to load
950
                 * the new files
951
                 */
952
                @Override
953
                protected void onPostExecute(Void arg1) {
954
                        setResult(RESULT_OK);
955
                        finish();
956
                }
957
        }
958
}