Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ContainerObjectsActivity.java @ 07c153b1

History | View | Annotate | Download (25.4 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
                        String tmp = s.substring(s.lastIndexOf('/')+1);
373
                        if(tmp.equals("")){
374
                                return s;
375
                        }
376
                        return tmp; 
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
        @Override
421
        public boolean onCreateOptionsMenu(Menu menu) {
422
                super.onCreateOptionsMenu(menu);
423
                MenuInflater inflater = getMenuInflater();
424
                inflater.inflate(R.menu.view_container_object_list_menu, menu);
425
                return true;
426
        }
427

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

    
464
        @Override
465
        protected Dialog onCreateDialog(int id) {
466
                switch (id) {
467
                case deleteContainer:
468
                        if(app.getCurFiles().size() == 0){
469
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
470
                                .setIcon(R.drawable.alert_dialog_icon)
471
                                .setTitle("Delete Container")
472
                                .setMessage(
473
                                "Are you sure you want to delete this Container?")
474
                                .setPositiveButton("Delete Container",
475
                                                new DialogInterface.OnClickListener() {
476
                                        public void onClick(DialogInterface dialog,
477
                                                        int whichButton) {
478
                                                // User clicked OK so do some stuff
479
                                                trackEvent(CATEGORY_CONTAINER, EVENT_DELETE, "", -1);
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
                        // a sub-activity kicked back, so we want to refresh the server list
572
                        loadFiles();
573
                }
574
                
575
                // deleted file so need to update the list
576
                if (requestCode == 55 && resultCode == 99) {
577
                        loadFiles();
578
                }
579
                
580
        }
581

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

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

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

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

    
647
                        TextView label = (TextView) row.findViewById(R.id.label);
648
                        //label.setText(file.getCName());
649
                        label.setText(getShortName(file.getCName()));
650

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

    
664
                        return (row);
665
                }
666
        }
667
                
668
        private class LoadFilesTask extends
669
        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
670

    
671
                private CloudServersException exception;
672
                protected void onPreExecute(){
673
                        showDialog();
674
                        loadingFiles = true; 
675
                }
676

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

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

    
697
                        setFileList(result);
698
                        loadCurrentDirectoryFiles();
699
                        displayCurrentFiles();
700
                        loadingFiles = false;
701
                }
702

    
703
        }
704

    
705
        private class AddFolderTask extends
706
        AsyncTask<String, Void, HttpBundle> {
707

    
708
                private CloudServersException exception;
709

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

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

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

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

    
813
        private class DeleteContainerTask extends
814
        AsyncTask<String, Void, HttpBundle> {
815

    
816
                private CloudServersException exception;
817

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

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

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

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

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

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

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

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

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

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

    
950
                        hideDialog();
951
                        setResult(RESULT_OK);
952
                        finish();
953
                }
954
        }
955
}