Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (25.3 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.ProgressDialog;
24
import android.content.Context;
25
import android.content.DialogInterface;
26
import android.content.Intent;
27
import android.os.AsyncTask;
28
import android.os.Bundle;
29
import android.util.Log;
30
import android.view.LayoutInflater;
31
import android.view.Menu;
32
import android.view.MenuInflater;
33
import android.view.MenuItem;
34
import android.view.View;
35
import android.view.ViewGroup;
36
import android.widget.ArrayAdapter;
37
import android.widget.EditText;
38
import android.widget.ListView;
39
import android.widget.TextView;
40

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

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

    
56
        private static final int deleteContainer = 0;
57
        private static final int deleteFolder = 1;
58
        
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
        @Override
78
        public void onCreate(Bundle savedInstanceState) {
79
                super.onCreate(savedInstanceState);
80
                trackPageView(PAGE_FOLDER);
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
                if(app.isDeletingContainer()){
178
                        displayNoFilesCell();
179
                        deleteContainerTask = new DeleteContainerListenerTask();
180
                        deleteContainerTask.execute();
181
                }
182

    
183

    
184
        }
185
        
186
        @Override
187
        protected void onStart(){
188
                super.onStart();
189
                if(displayDialog){
190
                        showDialog();
191
                }
192
        }
193

    
194
        
195
        @Override
196
        protected void onStop(){
197
                super.onStop();
198

    
199
                if(displayDialog){
200
                        hideDialog();
201
                        displayDialog = true;
202
                }
203

    
204
                /*
205
                 * Need to stop running listener task
206
                 * if we exit
207
                 */
208
                if(task != null){
209
                        task.cancel(true);
210
                }
211
                
212
                if(deleteObjTask != null){
213
                        deleteObjTask.cancel(true);
214
                }
215
                
216
                if(deleteContainerTask != null){
217
                        deleteContainerTask.cancel(true);
218
                }
219

    
220
        }
221

    
222
        /*
223
         * overriding back button press, because we are not actually changing
224
         * activities when we navigate the file structure
225
         */
226
        public void onBackPressed() {
227
                if(currentPath.equals("")){
228
                        finish();
229
                }
230
                else{
231
                        goUpDirectory();
232
                }
233
        }
234

    
235
        /*
236
         * go to the current directory's parent and display that data
237
         */
238
        private void goUpDirectory(){
239
                currentPath = currentPath.substring(0, currentPath.substring(0, currentPath.length()-2).lastIndexOf("/")+1);
240
                loadCurrentDirectoryFiles();
241
                displayCurrentFiles();
242
        }
243

    
244
        /*
245
         * load all file that are in the container
246
         */
247
        private void loadFiles() {
248
                //displayLoadingCell();
249
                new LoadFilesTask().execute();
250
        }
251

    
252

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

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

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

    
284

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

    
296

    
297

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

    
307
        private void displayCurrentFiles(){
308
                if (app.getCurFiles().size() == 0) {
309
                        displayNoFilesCell();
310
                } else {
311
                        ArrayList<ContainerObjects> tempList = new ArrayList<ContainerObjects>();
312
                        for(int i = 0; i < app.getCurFiles().size(); i++){
313
                                tempList.add(app.getCurFiles().get(i));
314
                        }
315
                        getListView().setDividerHeight(1); // restore divider lines
316
                        setListAdapter(new FileAdapter());
317
                }
318
        }
319

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

    
343

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

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

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

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

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

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

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

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

    
563
        @Override
564
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
565
                super.onActivityResult(requestCode, resultCode, data);
566
                        
567
                if (resultCode == RESULT_OK && requestCode == 56) {
568
                        // a sub-activity kicked back, so we want to refresh the server list
569
                        loadFiles();
570
                }
571
                
572
                // deleted file so need to update the list
573
                if (requestCode == 55 && resultCode == 99) {
574
                        loadFiles();
575
                }
576
                
577
        }
578

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

    
610
        private void startFileError(String message, HttpBundle bundle){
611
                Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
612
                viewIntent.putExtra("errorMessage", message);
613
                viewIntent.putExtra("response", bundle.getResponseText());
614
                viewIntent.putExtra("request", bundle.getCurlRequest());
615
                startActivity(viewIntent);
616
        }
617
        
618
        private void showDialog() {
619
                if(dialog == null || !dialog.isShowing()){
620
                        displayDialog = true;
621
                        dialog = ProgressDialog.show(ContainerObjectsActivity.this, "", "Loading...", true);
622
                }
623
    }
624
    
625
    private void hideDialog() {
626
            if(dialog != null){
627
                    dialog.dismiss();
628
            }
629
            displayDialog = false;
630
    }
631

    
632
        class FileAdapter extends ArrayAdapter<ContainerObjects> {
633
                FileAdapter() {
634
                        super(ContainerObjectsActivity.this,
635
                                        R.layout.listcontainerobjectcell, app.getCurFiles());        
636
                }
637

    
638
                public View getView(int position, View convertView, ViewGroup parent) {
639
                        ContainerObjects file = app.getCurFiles().get(position);
640
                        LayoutInflater inflater = getLayoutInflater();
641
                        View row = inflater.inflate(R.layout.listcontainerobjectcell,
642
                                        parent, false);
643

    
644
                        TextView label = (TextView) row.findViewById(R.id.label);
645
                        //label.setText(file.getCName());
646
                        label.setText(getShortName(file.getCName()));
647

    
648
                        if (file.getBytes() >= bConver) {
649
                                megaBytes = Math.abs(file.getBytes() / bConver + 0.2);
650
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
651
                                sublabel.setText(megaBytes + " MB");
652
                        } else if (file.getBytes() >= kbConver) {
653
                                kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2);
654
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
655
                                sublabel.setText(kiloBytes + " KB");
656
                        } else {
657
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
658
                                sublabel.setText(file.getBytes() + " B");
659
                        }
660

    
661
                        return (row);
662
                }
663
        }
664

    
665
        private class LoadFilesTask extends
666
        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
667

    
668
                private CloudServersException exception;
669
                protected void onPreExecute(){
670
                        showDialog();
671
                        loadingFiles = true;
672
                }
673

    
674
                @Override
675
                protected ArrayList<ContainerObjects> doInBackground(String... path) {
676
                        ArrayList<ContainerObjects> files = null;
677
                        try {
678
                                files = (new ContainerObjectManager(context)).createList(true,
679
                                                container.getName());
680
                        } catch (CloudServersException e) {
681
                                exception = e;
682
                                e.printStackTrace();
683
                        }
684
                        return files;
685
                }
686

    
687
                @Override
688
                protected void onPostExecute(ArrayList<ContainerObjects> result) {
689
                        hideDialog();
690
                        
691
                        if (exception != null) {
692
                                showAlert("Error", exception.getMessage());
693
                        }
694

    
695
                        setFileList(result);
696
                        loadCurrentDirectoryFiles();
697
                        displayCurrentFiles();
698
                        loadingFiles = false;
699
                }
700

    
701
        }
702

    
703
        private class AddFolderTask extends
704
        AsyncTask<String, Void, HttpBundle> {
705

    
706
                private CloudServersException exception;
707

    
708
                @Override
709
                protected void onPreExecute(){
710
                        showDialog();
711
                        app.setAddingObject(true);
712
                        task = new AddObjectListenerTask();
713
                        task.execute();
714
                }
715

    
716
                @Override
717
                protected HttpBundle doInBackground(String... data) {
718
                        HttpBundle bundle = null;
719
                        try {
720
                                
721
                                bundle = (new ContainerObjectManager(context)).addObject(container.getName(), currentPath, data[0], data[1]);
722
                        } catch (CloudServersException e) {
723
                                exception = e;
724
                        }
725
                        return bundle;
726
                }
727

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

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

    
811
        private class DeleteContainerTask extends
812
        AsyncTask<String, Void, HttpBundle> {
813

    
814
                private CloudServersException exception;
815

    
816
                @Override
817
                protected void onPreExecute(){
818
                        showDialog();
819
                        app.setDeletingContainer(true);
820
                        deleteContainerTask = new DeleteContainerListenerTask();
821
                        deleteContainerTask.execute();
822
                }
823
                
824
                @Override
825
                protected HttpBundle doInBackground(String... object) {
826
                        HttpBundle bundle = null;
827
                        try {
828
                                
829
                                bundle = (new ContainerManager(context)).delete(container.getName());
830
                        } catch (CloudServersException e) {
831
                                exception = e;
832
                        }
833
                        return bundle;
834
                }
835

    
836
                @Override
837
                protected void onPostExecute(HttpBundle bundle) {
838
                        hideDialog();
839
                        app.setDeletingContainer(false);
840
                        HttpResponse response = bundle.getResponse();
841
                        if (response != null) {
842
                                int statusCode = response.getStatusLine().getStatusCode();
843
                                if (statusCode == 409) {
844
                                        startFileError("Container must be empty in order to delete", bundle);
845
                                }
846
                                if (statusCode == 204) {
847
                                        setResult(Activity.RESULT_OK);
848

    
849
                                } else {
850
                                        CloudServersException cse = parseCloudServersException(response);
851
                                        if ("".equals(cse.getMessage())) {
852
                                                startFileError("There was a problem deleting your container.", bundle);
853
                                        } else {
854
                                                startFileError("There was a problem deleting your container: "
855
                                                                + cse.getMessage(), bundle);
856
                                        }
857
                                }
858
                        } else if (exception != null) {
859
                                startFileError("There was a problem deleting your server: "
860
                                                + exception.getMessage(), bundle);
861
                        }
862
                }
863
        }
864

    
865
        /*
866
         * listens for the application to change isProcessing
867
         * listens so activity knows when it should display
868
         * the new curDirFiles
869
         */
870
        private class AddObjectListenerTask extends
871
        AsyncTask<Void, Void, Void> {
872
                
873
                @Override
874
                protected Void doInBackground(Void... arg1) {
875

    
876
                        while(app.isAddingObject()){
877
                                // wait for process to finish
878
                                // or have it be canceled
879
                                if(task.isCancelled()){
880
                                        return null;
881
                                }
882
                        }
883
                        return null;
884
                }
885

    
886
                /*
887
                 * when no longer processing, time to load
888
                 * the new files
889
                 */
890
                @Override
891
                protected void onPostExecute(Void arg1) {
892
                        loadFiles();
893
                }
894
        }
895
        
896
        
897
        private class DeleteObjectListenerTask extends
898
        AsyncTask<Void, Void, Void> {
899
                
900
                @Override
901
                protected Void doInBackground(Void... arg1) {
902

    
903
                        while(app.isDeletingObject()){
904
                                // wait for process to finish
905
                                // or have it be canceled
906
                                if(deleteObjTask.isCancelled()){
907
                                        return null;
908
                                }
909
                        }
910
                        return null;
911
                }
912

    
913
                /*
914
                 * when no longer processing, time to load
915
                 * the new files
916
                 */
917
                @Override
918
                protected void onPostExecute(Void arg1) {
919
                        hideDialog();
920
                        removeFromList(currentPath);
921
                        goUpDirectory();
922
                }
923
        }
924
        
925
        private class DeleteContainerListenerTask extends
926
        AsyncTask<Void, Void, Void> {
927
                
928
                @Override
929
                protected Void doInBackground(Void... arg1) {
930

    
931
                        while(app.isDeletingContainer()){
932
                                // wait for process to finish
933
                                // or have it be canceled
934
                                if(deleteContainerTask.isCancelled()){
935
                                        return null;
936
                                }
937
                        }
938
                        return null;
939
                }
940

    
941
                /*
942
                 * when no longer processing, time to load
943
                 * the new files
944
                 */
945
                @Override
946
                protected void onPostExecute(Void arg1) {
947

    
948
                        hideDialog();
949
                        setResult(RESULT_OK);
950
                        finish();
951
                }
952
        }
953
}