Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (25.5 kB)

1
package com.rackspacecloud.android;
2

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

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

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

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

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

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

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

    
77

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

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

    
116

    
117

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

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

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

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

    
177

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

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

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

    
198
                /*
199
                 * Need to stop running listener task
200
                 * if we exit
201
                 */
202
                if(task != null){
203
                        task.cancel(true);
204
                }
205
        }
206

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

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

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

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

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

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

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

    
281

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

    
293

    
294

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

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

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

    
347

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

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

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

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

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

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

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

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

    
566
        @Override
567
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
568
                super.onActivityResult(requestCode, resultCode, data);
569

    
570
                if (resultCode == RESULT_OK) {
571
                        // a sub-activity kicked back, so we want to refresh the server list
572
                        loadFiles();
573
                }
574
                /*
575
                if (requestCode == 56) {
576
                        if (resultCode == RESULT_OK) {
577
                                Intent viewIntent1 = new Intent(this,
578
                                                ListContainerActivity.class);
579
                                startActivityForResult(viewIntent1, 56);
580
                        }
581
                }
582
                 */
583
        }
584

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

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

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

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

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

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

    
667
                        return (row);
668
                }
669
        }
670

    
671
        private class LoadFilesTask extends
672
        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
673

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

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

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

    
701
                        setFileList(result);
702
                        loadCurrentDirectoryFiles();
703
                        displayCurrentFiles();
704
                        loadingFiles = false;
705
                }
706

    
707
        }
708

    
709
        private class AddFolderTask extends
710
        AsyncTask<String, Void, HttpBundle> {
711

    
712
                private CloudServersException exception;
713

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

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

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

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

    
816
        private class DeleteContainerTask extends
817
        AsyncTask<String, Void, HttpBundle> {
818

    
819
                private CloudServersException exception;
820

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

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

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

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

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

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

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

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

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

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