Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / ContainerObjectsActivity.java @ cabcecb2

History | View | Annotate | Download (29.2 kB)

1
package com.rackspace.cloud.android;
2

    
3

    
4

    
5
import java.io.File;
6
import java.net.URLEncoder;
7
import java.util.ArrayList;
8
import java.util.Arrays;
9
import java.util.List;
10

    
11
import org.apache.http.HttpResponse;
12

    
13
import android.app.Activity;
14
import android.app.AlertDialog;
15
import android.app.Dialog;
16
import android.content.ActivityNotFoundException;
17
import android.content.DialogInterface;
18
import android.content.Intent;
19
import android.database.Cursor;
20
import android.net.Uri;
21
import android.os.AsyncTask;
22
import android.os.Bundle;
23
import android.util.Log;
24
import android.view.LayoutInflater;
25
import android.view.Menu;
26
import android.view.MenuInflater;
27
import android.view.MenuItem;
28
import android.view.View;
29
import android.view.ViewGroup;
30
import android.webkit.MimeTypeMap;
31
import android.widget.ArrayAdapter;
32
import android.widget.EditText;
33
import android.widget.ImageView;
34
import android.widget.ListView;
35
import android.widget.TextView;
36
import android.widget.Toast;
37

    
38
import com.rackspace.cloud.files.api.client.Container;
39
import com.rackspace.cloud.files.api.client.ContainerManager;
40
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
41
import com.rackspace.cloud.files.api.client.ContainerObjects;
42
import com.rackspace.cloud.servers.api.client.CloudServersException;
43
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
44

    
45
/**
46
 * 
47
 * @author Phillip Toohill
48
 * 
49
 */
50

    
51
public class ContainerObjectsActivity extends CloudListActivity {
52

    
53
        private static final int deleteContainer = 0;
54
        private static final int deleteFolder = 1;
55

    
56
        private ContainerObjects[] files;
57
        private static Container container;
58
        public String LOG = "viewFilesActivity";
59
        private String cdnEnabledIs;
60
        public Object megaBytes;
61
        public Object kiloBytes;
62
        public int bConver = 1048576;
63
        public int kbConver = 1024;
64
        private boolean loadingFiles;
65
        private String currentPath;
66
        private AndroidCloudApplication app;
67
        private AddObjectListenerTask task;
68
        private DeleteObjectListenerTask deleteObjTask;
69
        private DeleteContainerListenerTask deleteContainerTask;
70

    
71
        @Override
72
        public void onCreate(Bundle savedInstanceState) {
73
                super.onCreate(savedInstanceState);
74
                trackPageView(GoogleAnalytics.PAGE_FOLDER);
75
                container = getContainer();
76
                if (container.isCdnEnabled() == true) {
77
                        cdnEnabledIs = "true";
78
                } else {
79
                        cdnEnabledIs = "false";
80
                }
81
                restoreState(savedInstanceState);
82
        }
83

    
84
        protected Container getContainer() {
85
                return (Container) this.getIntent().getExtras().get("container");
86
        }
87

    
88
        @Override
89
        protected void onSaveInstanceState(Bundle outState) {
90
                super.onSaveInstanceState(outState);
91

    
92
                // files stores all the files in the container
93
                outState.putSerializable("container", files);
94

    
95
                // current path represents where you have "navigated" to
96
                outState.putString("path", currentPath);
97

    
98
                outState.putBoolean("loadingFiles", loadingFiles);
99
        }
100

    
101
        protected void restoreState(Bundle state) {
102
                super.restoreState(state);
103

    
104
                /*
105
                 * need reference to the app so you can access curDirFiles as well as
106
                 * processing status
107
                 */
108
                app = (AndroidCloudApplication) this.getApplication();
109

    
110
                if (state != null) {
111
                        if (state.containsKey("path")) {
112
                                currentPath = state.getString("path");
113
                        } else {
114
                                currentPath = "";
115
                        }
116

    
117
                        if (state.containsKey("loadingFiles")
118
                                        && state.getBoolean("loadingFiles")) {
119
                                loadFiles();
120
                        } else if (state.containsKey("container")) {
121
                                files = (ContainerObjects[]) state.getSerializable("container");
122
                                if (app.getCurFiles() == null || app.getCurFiles().size() == 0) {
123
                                        displayNoFilesCell();
124
                                } else {
125
                                        getListView().setDividerHeight(1); // restore divider lines
126
                                        setListAdapter(new FileAdapter());
127

    
128
                                }
129

    
130
                        }
131
                } else {
132
                        currentPath = "";
133
                        loadFiles();
134
                }
135

    
136
                /*
137
                 * if the app is process when we enter the activity we must listen for
138
                 * the new curDirFiles list
139
                 */
140
                if (app.isAddingObject()) {
141
                        task = new AddObjectListenerTask();
142
                        task.execute();
143
                }
144

    
145
                if (app.isDeletingObject()) {
146
                        displayNoFilesCell();
147
                        deleteObjTask = new DeleteObjectListenerTask();
148
                        deleteObjTask.execute();
149
                }
150

    
151
                if (app.isDeletingContainer()) {
152
                        displayNoFilesCell();
153
                        deleteContainerTask = new DeleteContainerListenerTask();
154
                        deleteContainerTask.execute();
155
                }
156

    
157
        }
158

    
159
        @Override
160
        protected void onStop() {
161
                super.onStop();
162

    
163
                /*
164
                 * Need to stop running listener task if we exit
165
                 */
166
                if (task != null) {
167
                        task.cancel(true);
168
                }
169

    
170
                if (deleteObjTask != null) {
171
                        deleteObjTask.cancel(true);
172
                }
173

    
174
                if (deleteContainerTask != null) {
175
                        deleteContainerTask.cancel(true);
176
                }
177

    
178
        }
179

    
180
        /*
181
         * overriding back button press, because we are not actually changing
182
         * activities when we navigate the file structure
183
         */
184
        public void onBackPressed() {
185
                
186
                if (currentPath.equals("")) {
187
                        if(container.getName().equals(Container.MYSHARED)){}
188
                        else
189
                                finish();
190
                } else {
191
                        goUpDirectory();
192
                }
193
        }
194

    
195
        /*
196
         * go to the current directory's parent and display that data
197
         */
198
        private void goUpDirectory() {
199
                currentPath = currentPath.substring(
200
                                0,
201
                                currentPath.substring(0, currentPath.length() - 2).lastIndexOf(
202
                                                "/") + 1);
203
                loadCurrentDirectoryFiles();
204
                displayCurrentFiles();
205
        }
206

    
207
        /*
208
         * load all file that are in the container
209
         */
210
        private void loadFiles() {
211
                // displayLoadingCell();
212
                new LoadFilesTask().execute();
213
        }
214

    
215
        /*
216
         * load only the files that should display for the current directory in the
217
         * curDirFiles[]
218
         */
219
        protected void loadCurrentDirectoryFiles() {
220
                ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>();
221
                Log.i(LOG, "loading files");
222
                if (files != null) {
223
                        for (int i = 0; i < files.length; i++) {
224
                                Log.i(LOG, "loading files" + i);
225
                                if (fileBelongsInDir(files[i])) {
226
                                        curFiles.add(files[i]);
227
                                }
228
                        }
229
                        app.setCurFiles(curFiles);
230
                }
231
        }
232

    
233
        /*
234
         * determines if a file should be displayed in current directory
235
         */
236
        protected Boolean fileBelongsInDir(ContainerObjects obj) {
237
                String objPath = obj.getCName();
238

    
239
                if (!objPath.startsWith(currentPath)) {
240
                        Log.i(LOG, "Path is:" + currentPath + " " + objPath + " " + false);
241
                        return false;
242
                } else {
243
                        objPath = objPath.substring(currentPath.length());
244
                        Log.i(LOG, "Path is:" + currentPath + " " + objPath + " "
245
                                        + !objPath.contains("/"));
246
                        return !objPath.contains("/");
247
                }
248
        }
249

    
250
        /*
251
         * loads all the files that are in the container into one array
252
         */
253
        private void setFileList(ArrayList<ContainerObjects> files) {
254
                if (files == null) {
255
                        files = new ArrayList<ContainerObjects>();
256
                }
257
                String[] fileNames = new String[files.size()];
258
                this.files = new ContainerObjects[files.size()];
259

    
260
                if (files != null) {
261
                        for (int i = 0; i < files.size(); i++) {
262
                                ContainerObjects file = files.get(i);
263
                                // Log.i(file.getCName(),file.getCName());
264
                                this.files[i] = file;
265
                                fileNames[i] = file.getName();
266
                        }
267
                }
268
        }
269

    
270
        protected void displayCurrentFiles() {
271
                if (app.getCurFiles().size() == 0) {
272
                        displayNoFilesCell();
273
                } else {
274
                        ArrayList<ContainerObjects> tempList = new ArrayList<ContainerObjects>();
275
                        for (int i = 0; i < app.getCurFiles().size(); i++) {
276
                                tempList.add(app.getCurFiles().get(i));
277
                        }
278
                        getListView().setDividerHeight(1); // restore divider lines
279
                        setListAdapter(new FileAdapter());
280
                }
281
        }
282

    
283
        /*
284
         * display a different empty page depending of if you are at top of
285
         * container or in a folder
286
         */
287
        protected void displayNoFilesCell() {
288
                String a[] = new String[1];
289
                if (currentPath.equals("")) {
290
                        a[0] = "Empty Container";
291
                        setListAdapter(new ArrayAdapter<String>(this,
292
                                        R.layout.noobjectscell, R.id.no_files_label, a));
293
                } else {
294
                        a[0] = "No Files";
295
                        setListAdapter(new ArrayAdapter<String>(this, R.layout.nofilescell,
296
                                        R.id.no_files_label, a));
297
                }
298
                getListView().setTextFilterEnabled(true);
299
                getListView().setDividerHeight(0); // hide the dividers so it won't look
300
                // like a list row
301
                getListView().setItemsCanFocus(false);
302
        }
303

    
304
        /*
305
         * just get the last part of the filename so the entire file path does not
306
         * show
307
         */
308
        private String getShortName(String longName) {
309
                String s = longName;
310
                if (!s.contains("/")) {
311
                        return s;
312
                } else {
313
                        String tmp = s.substring(s.lastIndexOf('/') + 1);
314
                        if (tmp.equals("")) {
315
                                return s;
316
                        }
317
                        return tmp;
318
                }
319
        }
320

    
321
        /*
322
         * removed a specified object from the array of all the files in the
323
         * container
324
         */
325
        private void removeFromList(String path) {
326
                ArrayList<ContainerObjects> temp = new ArrayList<ContainerObjects>(
327
                                Arrays.asList(files));
328
                for (int i = 0; i < files.length; i++) {
329
                        if (files[i].getCName()
330
                                        .equals(path.substring(0, path.length() - 1))) {
331
                                temp.remove(i);
332
                        }
333
                }
334
                files = new ContainerObjects[temp.size()];
335
                for (int i = 0; i < temp.size(); i++) {
336
                        files[i] = temp.get(i);
337
                }
338
        }
339

    
340
        protected void onListItemClick(ListView l, View v, int position, long id) {
341
                if (app.getCurFiles() != null && app.getCurFiles().size() > 0) {
342
                        Intent viewIntent;
343
                        if (app.getCurFiles().get(position).getContentType()
344
                                        .startsWith("application/directory")) {
345
                                currentPath = app.getCurFiles().get(position).getCName() + "/";
346
                                loadCurrentDirectoryFiles();
347
                                displayCurrentFiles();
348
                        }
349

    
350
                        else {
351
                                viewIntent = new Intent(this, ContainerObjectDetails.class);
352
                                viewIntent.putExtra("container", app.getCurFiles()
353
                                                .get(position));
354
                                viewIntent.putExtra("cdnUrl", container.getCdnUrl());
355
                                viewIntent.putExtra("containerNames", container.getName());
356
                                viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
357
                                startActivityForResult(viewIntent, 55); // arbitrary number;
358
                                                                                                                // never
359
                                // used again
360
                        }
361
                }
362
        }
363

    
364
        @Override
365
        public boolean onCreateOptionsMenu(Menu menu) {
366
                super.onCreateOptionsMenu(menu);
367
                MenuInflater inflater = getMenuInflater();
368
                inflater.inflate(R.menu.view_container_object_list_menu, menu);
369
                return true;
370
        }
371

    
372
        @Override
373
        /*
374
         * option performed for delete depends on if you are at the top of a
375
         * container or in a folder
376
         */
377
        public boolean onOptionsItemSelected(MenuItem item) {
378
                switch (item.getItemId()) {
379
                case R.id.delete_container:
380
                        if (currentPath.equals("")) {
381
                                showDialog(deleteContainer);
382
                        } else {
383
                                showDialog(deleteFolder);
384
                        }
385
                        return true;
386
                case R.id.enable_cdn:
387
                        Intent viewIntent1 = new Intent(this, EnableCDNActivity.class);
388
                        viewIntent1.putExtra("Cname", container.getName());
389
                        startActivityForResult(viewIntent1, 56);
390
                        return true;
391
                case R.id.refresh:
392
                        loadFiles();
393
                        return true;
394
                case R.id.add_folder:
395
                        showDialog(R.id.add_folder);
396
                        return true;
397
                case R.id.add_file:
398
                        /*
399
                         * Intent viewIntent2 = new Intent(this, AddFileActivity.class);
400
                         * viewIntent2.putExtra("Cname", container.getName());
401
                         * viewIntent2.putExtra("curPath", currentPath);
402
                         * startActivityForResult(viewIntent2, 56);
403
                         */
404

    
405
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
406
                        intent.addCategory(Intent.CATEGORY_OPENABLE);
407
                        intent.setData(Uri.parse("file://"));
408
                        intent.setType("*/*");
409
                        try {
410
                                int i = 1;
411
                                startActivityForResult(intent, i);
412

    
413
                        } catch (ActivityNotFoundException e) {
414
                                Toast.makeText(this, "No File Manager", Toast.LENGTH_SHORT)
415
                                                .show();
416
                        }
417

    
418
                        return true;
419
                }
420
                return false;
421
        }
422

    
423
        @Override
424
        protected Dialog onCreateDialog(int id) {
425
                switch (id) {
426
                case deleteContainer:
427
                        if (app.getCurFiles().size() == 0) {
428
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
429
                                                .setIcon(R.drawable.alert_dialog_icon)
430
                                                .setTitle("Delete Container")
431
                                                .setMessage(
432
                                                                "Are you sure you want to delete this Container?")
433
                                                .setPositiveButton("Delete Container",
434
                                                                new DialogInterface.OnClickListener() {
435
                                                                        public void onClick(DialogInterface dialog,
436
                                                                                        int whichButton) {
437
                                                                                // User clicked OK so do some stuff
438
                                                                                trackEvent(
439
                                                                                                GoogleAnalytics.CATEGORY_CONTAINER,
440
                                                                                                GoogleAnalytics.EVENT_DELETE,
441
                                                                                                "", -1);
442
                                                                                new DeleteContainerTask()
443
                                                                                                .execute(currentPath);
444
                                                                        }
445
                                                                })
446
                                                .setNegativeButton("Cancel",
447
                                                                new DialogInterface.OnClickListener() {
448
                                                                        public void onClick(DialogInterface dialog,
449
                                                                                        int whichButton) {
450
                                                                                // User clicked Cancel so do some stuff
451
                                                                        }
452
                                                                }).create();
453
                        } else {
454
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
455
                                                .setIcon(R.drawable.alert_dialog_icon)
456
                                                .setTitle("Delete Container")
457
                                                .setMessage("Container must be empty to delete")
458
                                                .setNegativeButton("OK",
459
                                                                new DialogInterface.OnClickListener() {
460
                                                                        public void onClick(DialogInterface dialog,
461
                                                                                        int whichButton) {
462
                                                                                // User clicked Cancel so do some stuff
463
                                                                        }
464
                                                                }).create();
465
                        }
466
                case deleteFolder:
467
                        if (app.getCurFiles().size() == 0) {
468
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
469
                                                .setIcon(R.drawable.alert_dialog_icon)
470
                                                .setTitle("Delete Folder")
471
                                                .setMessage(
472
                                                                "Are you sure you want to delete this Folder?")
473
                                                .setPositiveButton("Delete Folder",
474
                                                                new DialogInterface.OnClickListener() {
475
                                                                        public void onClick(DialogInterface dialog,
476
                                                                                        int whichButton) {
477
                                                                                // User clicked OK so do some stuff
478
                                                                                new DeleteObjectTask().execute();
479
                                                                        }
480
                                                                })
481
                                                .setNegativeButton("Cancel",
482
                                                                new DialogInterface.OnClickListener() {
483
                                                                        public void onClick(DialogInterface dialog,
484
                                                                                        int whichButton) {
485
                                                                                // User clicked Cancel so do some stuff
486
                                                                        }
487
                                                                }).create();
488
                        } else {
489
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
490
                                                .setIcon(R.drawable.alert_dialog_icon)
491
                                                .setTitle("Delete Folder")
492
                                                .setMessage("Folder must be empty to delete")
493
                                                .setNegativeButton("OK",
494
                                                                new DialogInterface.OnClickListener() {
495
                                                                        public void onClick(DialogInterface dialog,
496
                                                                                        int whichButton) {
497
                                                                                // User clicked Cancel so do some stuff
498
                                                                        }
499
                                                                }).create();
500
                        }
501
                case R.id.add_folder:
502
                        final EditText input = new EditText(this);
503
                        return new AlertDialog.Builder(ContainerObjectsActivity.this)
504
                                        .setIcon(R.drawable.alert_dialog_icon)
505
                                        .setView(input)
506
                                        .setTitle("Add Folder")
507
                                        .setMessage("Enter new name for folder: ")
508
                                        .setPositiveButton("Add",
509
                                                        new DialogInterface.OnClickListener() {
510
                                                                public void onClick(DialogInterface dialog,
511
                                                                                int whichButton) {
512
                                                                        // User clicked OK so do some stuff
513
                                                                        String[] info = {
514
                                                                                        input.getText().toString(),
515
                                                                                        "application/directory" };
516
                                                                        new AddFolderTask().execute(info);
517
                                                                }
518
                                                        })
519
                                        .setNegativeButton("Cancel",
520
                                                        new DialogInterface.OnClickListener() {
521
                                                                public void onClick(DialogInterface dialog,
522
                                                                                int whichButton) {
523
                                                                        // User clicked Cancel so do some stuff
524
                                                                }
525
                                                        }).create();
526
                }
527
                return null;
528
        }
529

    
530
        @Override
531
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
532
                super.onActivityResult(requestCode, resultCode, data);
533

    
534
                if (resultCode == RESULT_OK && requestCode == 56) {
535
                        // a sub-activity kicked back, so we want to refresh the server list
536
                        loadFiles();
537
                }
538
                
539
                if (resultCode == RESULT_OK && requestCode == 1) {
540
                        String filename = data.getDataString();
541
                        uploadFile(filename, data);
542
                        
543
                }
544

    
545
                // deleted file so need to update the list
546
                if (requestCode == 55 && resultCode == 99) {
547
                        loadFiles();
548
                }
549

    
550
        }
551

    
552
        class FileAdapter extends ArrayAdapter<ContainerObjects> {
553
                FileAdapter() {
554
                        super(ContainerObjectsActivity.this,
555
                                        R.layout.listcontainerobjectcell, app.getCurFiles());
556
                }
557

    
558
                public View getView(int position, View convertView, ViewGroup parent) {
559
                        ContainerObjects file = app.getCurFiles().get(position);
560
                        LayoutInflater inflater = getLayoutInflater();
561
                        View row = inflater.inflate(R.layout.listcontainerobjectcell,
562
                                        parent, false);
563

    
564
                        TextView label = (TextView) row.findViewById(R.id.label);
565
                        label.setText(getShortName(file.getCName()));
566

    
567
                        ImageView objectImage = (ImageView) row
568
                                        .findViewById(R.id.file_type_image);
569
                        if (file.getContentType().startsWith("application/directory")) {
570
                                objectImage.setImageResource(R.drawable.folder);
571
                        } else {
572
                                objectImage.setImageResource(R.drawable.file);
573
                        }
574

    
575
                        if (file.getBytes() >= bConver) {
576
                                megaBytes = Math.abs(file.getBytes() / bConver + 0.2);
577
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
578
                                sublabel.setText(megaBytes + " MB");
579
                        } else if (file.getBytes() >= kbConver) {
580
                                kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2);
581
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
582
                                sublabel.setText(kiloBytes + " KB");
583
                        } else {
584
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
585
                                sublabel.setText(file.getBytes() + " B");
586
                        }
587

    
588
                        return (row);
589
                }
590
        }
591

    
592
        private class LoadFilesTask extends
593
                        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
594

    
595
                private CloudServersException exception;
596

    
597
                protected void onPreExecute() {
598
                        showDialog();
599
                        loadingFiles = true;
600
                }
601

    
602
                @Override
603
                protected ArrayList<ContainerObjects> doInBackground(String... path) {
604
                        ArrayList<ContainerObjects> files = null;
605
                        try {
606
                                if (container.getName().equals(Container.MYSHARED)) {
607
                                        files = (new ContainerObjectManager(getContext()))
608
                                                        .createListMyShared(true, container.getName(),
609
                                                                        new ContainerManager(getContext())
610
                                                                                        .createList(true));
611
                                } else if (container.getName().equals(Container.OTHERS)) {
612

    
613
                                } else {
614
                                        if (container.getOtherUser() == null)
615
                                                files = (new ContainerObjectManager(getContext()))
616
                                                                .createList(true, container.getName());
617
                                        else
618
                                                files = (new ContainerObjectManager(getContext()))
619
                                                                .createOtherList(true, container.getName(),
620
                                                                                container.getOtherUser());
621
                                }
622
                        } catch (CloudServersException e) {
623
                                exception = e;
624
                                e.printStackTrace();
625
                        }
626
                        return files;
627
                }
628

    
629
                @Override
630
                protected void onPostExecute(ArrayList<ContainerObjects> result) {
631
                        loadingFiles = false;
632
                        hideDialog();
633
                        if (exception != null) {
634
                                showAlert("Error", exception.getMessage());
635
                        }
636
                        setFileList(result);
637
                        loadCurrentDirectoryFiles();
638
                        displayCurrentFiles();
639
                }
640

    
641
        }
642

    
643
        private class AddFolderTask extends AsyncTask<String, Void, HttpBundle> {
644

    
645
                private CloudServersException exception;
646

    
647
                @Override
648
                protected void onPreExecute() {
649
                        showDialog();
650
                        app.setAddingObject(true);
651
                        task = new AddObjectListenerTask();
652
                        task.execute();
653
                }
654

    
655
                @Override
656
                protected HttpBundle doInBackground(String... data) {
657
                        HttpBundle bundle = null;
658
                        try {
659

    
660
                                bundle = (new ContainerObjectManager(getContext())).addObject(
661
                                                container.getName(), currentPath, data[0], data[1]);
662
                        } catch (CloudServersException e) {
663
                                exception = e;
664
                        }
665
                        return bundle;
666
                }
667

    
668
                @Override
669
                protected void onPostExecute(HttpBundle bundle) {
670
                        app.setAddingObject(false);
671
                        hideDialog();
672
                        HttpResponse response = bundle.getResponse();
673
                        if (response != null) {
674
                                int statusCode = response.getStatusLine().getStatusCode();
675
                                if (statusCode == 201) {
676
                                        setResult(Activity.RESULT_OK);
677
                                        // loading the new files is done by ListenerTask
678
                                } else {
679
                                        CloudServersException cse = parseCloudServersException(response);
680
                                        if ("".equals(cse.getMessage())) {
681
                                                showError("There was a problem deleting your folder.",
682
                                                                bundle);
683
                                        } else {
684
                                                showError("There was a problem deleting your folder: "
685
                                                                + cse.getMessage(), bundle);
686
                                        }
687
                                }
688
                        } else if (exception != null) {
689
                                showError("There was a problem deleting your folder: "
690
                                                + exception.getMessage(), bundle);
691
                        }
692
                }
693
        }
694
        
695
        private class AddFileTask extends AsyncTask<String, Void, HttpBundle> {
696

    
697
                private CloudServersException exception;
698

    
699
                @Override
700
                protected void onPreExecute() {
701
                        showDialog();
702
                        app.setAddingObject(true);
703
                        task = new AddObjectListenerTask();
704
                        task.execute();
705
                }
706

    
707
                @Override
708
                protected HttpBundle doInBackground(String... data) {
709
                        HttpBundle bundle = null;
710
                        try {
711

    
712
                                bundle = (new ContainerObjectManager(getContext())).addFileObject(
713
                                                container.getName(), currentPath, data[0], data[1], data[2]);
714
                        } catch (CloudServersException e) {
715
                                exception = e;
716
                        }
717
                        return bundle;
718
                }
719

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

    
747
        private class DeleteObjectTask extends AsyncTask<Void, Void, HttpBundle> {
748

    
749
                private CloudServersException exception;
750

    
751
                @Override
752
                protected void onPreExecute() {
753
                        showDialog();
754
                        app.setDeleteingObject(true);
755
                        deleteObjTask = new DeleteObjectListenerTask();
756
                        deleteObjTask.execute();
757
                }
758

    
759
                @Override
760
                protected HttpBundle doInBackground(Void... arg0) {
761
                        HttpBundle bundle = null;
762
                        try {
763
                                // subtring because the current directory contains a "/" at the
764
                                // end of the string
765
                                bundle = (new ContainerObjectManager(getContext()))
766
                                                .deleteObject(container.getName(), currentPath
767
                                                                .substring(0, currentPath.length() - 1));
768
                        } catch (CloudServersException e) {
769
                                exception = e;
770
                        }
771
                        return bundle;
772
                }
773

    
774
                @Override
775
                protected void onPostExecute(HttpBundle bundle) {
776
                        app.setDeleteingObject(false);
777
                        hideDialog();
778
                        HttpResponse response = bundle.getResponse();
779
                        if (response != null) {
780
                                int statusCode = response.getStatusLine().getStatusCode();
781
                                if (statusCode == 409) {
782
                                        showAlert("Error",
783
                                                        "Folder must be empty in order to delete");
784
                                }
785
                                if (statusCode == 204) {
786
                                        setResult(Activity.RESULT_OK);
787
                                } else {
788
                                        CloudServersException cse = parseCloudServersException(response);
789
                                        if ("".equals(cse.getMessage())) {
790
                                                showError("There was a problem deleting your folder.",
791
                                                                bundle);
792
                                        } else {
793
                                                showError("There was a problem deleting your folder: "
794
                                                                + cse.getMessage(), bundle);
795
                                        }
796
                                }
797
                        } else if (exception != null) {
798
                                showError("There was a problem deleting your folder: "
799
                                                + exception.getMessage(), bundle);
800
                        }
801
                }
802
        }
803

    
804
        private class DeleteContainerTask extends
805
                        AsyncTask<String, Void, HttpBundle> {
806

    
807
                private CloudServersException exception;
808

    
809
                @Override
810
                protected void onPreExecute() {
811
                        showDialog();
812
                        app.setDeletingContainer(true);
813
                        deleteContainerTask = new DeleteContainerListenerTask();
814
                        deleteContainerTask.execute();
815
                }
816

    
817
                @Override
818
                protected HttpBundle doInBackground(String... object) {
819
                        HttpBundle bundle = null;
820
                        try {
821
                                bundle = (new ContainerManager(getContext())).delete(container
822
                                                .getName());
823
                        } catch (CloudServersException e) {
824
                                exception = e;
825
                        }
826
                        return bundle;
827
                }
828

    
829
                @Override
830
                protected void onPostExecute(HttpBundle bundle) {
831
                        hideDialog();
832
                        app.setDeletingContainer(false);
833
                        HttpResponse response = bundle.getResponse();
834
                        if (response != null) {
835
                                int statusCode = response.getStatusLine().getStatusCode();
836
                                if (statusCode == 409) {
837
                                        showError("Container must be empty in order to delete",
838
                                                        bundle);
839
                                }
840
                                if (statusCode == 204) {
841
                                        setResult(Activity.RESULT_OK);
842

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

    
862
        /*
863
         * listens for the application to change isProcessing listens so activity
864
         * knows when it should display the new curDirFiles
865
         */
866
        private class AddObjectListenerTask extends AsyncTask<Void, Void, Void> {
867

    
868
                @Override
869
                protected Void doInBackground(Void... arg1) {
870

    
871
                        while (app.isAddingObject()) {
872
                                // wait for process to finish
873
                                // or have it be canceled
874
                                if (task.isCancelled()) {
875
                                        return null;
876
                                }
877
                        }
878
                        return null;
879
                }
880

    
881
                /*
882
                 * when no longer processing, time to load the new files
883
                 */
884
                @Override
885
                protected void onPostExecute(Void arg1) {
886
                        hideDialog();
887
                        loadFiles();
888
                }
889
        }
890

    
891
        private class DeleteObjectListenerTask extends AsyncTask<Void, Void, Void> {
892

    
893
                @Override
894
                protected Void doInBackground(Void... arg1) {
895

    
896
                        while (app.isDeletingObject()) {
897
                                // wait for process to finish
898
                                // or have it be canceled
899
                                if (deleteObjTask.isCancelled()) {
900
                                        return null;
901
                                }
902
                        }
903
                        return null;
904
                }
905

    
906
                /*
907
                 * when no longer processing, time to load the new files
908
                 */
909
                @Override
910
                protected void onPostExecute(Void arg1) {
911
                        hideDialog();
912
                        removeFromList(currentPath);
913
                        goUpDirectory();
914
                }
915
        }
916

    
917
        private class DeleteContainerListenerTask extends
918
                        AsyncTask<Void, Void, Void> {
919

    
920
                @Override
921
                protected Void doInBackground(Void... arg1) {
922

    
923
                        while (app.isDeletingContainer()) {
924
                                // wait for process to finish
925
                                // or have it be canceled
926
                                if (deleteContainerTask.isCancelled()) {
927
                                        return null;
928
                                }
929
                        }
930
                        return null;
931
                }
932

    
933
                /*
934
                 * when no longer processing, time to load the new files
935
                 */
936
                @Override
937
                protected void onPostExecute(Void arg1) {
938

    
939
                        hideDialog();
940
                        setResult(RESULT_OK);
941
                        finish();
942
                }
943
        }
944

    
945
        public String getCurrentPath() {
946
                return currentPath;
947
        }
948

    
949
        public ContainerObjects[] getFiles() {
950
                return files;
951
        }
952
        
953
        /***** UPLOAD FILE ****/
954
        protected void uploadFile(String filename, Intent data){
955
                if (filename.startsWith("file://")) {
956
                        filename = filename.substring(7);
957
                }
958
                // replace %20 and so on
959
                filename = Uri.decode(filename);
960

    
961
                int indx = filename.lastIndexOf("//");
962
                File file = new File(data.getData().getPath());
963
                String filenameNew = null;
964
                Long fileSize = null;
965
                if (!file.exists()) {
966
                        Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
967
                        if (cursor != null) {
968
                                while (cursor.moveToNext()) {
969
                                        for (int i = 0; i < cursor.getColumnCount(); i++) {
970
                                                if (cursor.getColumnName(i).equals("_data"))
971
                                                        filenameNew = cursor.getString(i);
972
                                                if (cursor.getColumnName(i).equals("_size"))
973
                                                        fileSize = cursor.getLong(i);
974
                                        }
975
                                }
976
                        }
977
                } else {
978
                        filenameNew = data.getData().getPath();
979
                }
980
                if (filenameNew != null) {
981
                        file = new File(filenameNew);
982
                        String ext = MimeTypeMap.getSingleton().getFileExtensionFromUrl(file.getPath());
983
                        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
984
                        String[] info = {
985
                                        file.getName(),
986
                                        mime, filenameNew};
987
                        new AddFileTask().execute(info);
988
                        //getTasks().getUploadTask(GssMyFolders.this).execute(getCurrentResource().getUri() + URLEncoder.encode(file.getName()), filenameNew);
989
                }
990
        }
991
        
992
        protected void uploadFile(List<Uri> uris){
993
                List<String> strings = new ArrayList<String>();
994
                String path = "";//getCurrentResource().getUri();
995
                if(!path.endsWith("/"))
996
                        path = path+"/";
997
                
998
                for(Uri data : uris){
999
                        String filename = data.getPath();
1000
                        if (filename.startsWith("file://")) {
1001
                                filename = filename.substring(7);
1002
                        }
1003
                        // replace %20 and so on
1004
                        filename = Uri.decode(filename);
1005

    
1006
                        int indx = filename.lastIndexOf("//");
1007
                        File file = new File(filename);
1008
                        String filenameNew = null;
1009
                        Long fileSize = null;
1010
                        if (!file.exists()) {
1011
                                Cursor cursor = getContentResolver().query(data, null, null, null, null);
1012
                                if (cursor != null) {
1013
                                        while (cursor.moveToNext()) {
1014
                                                for (int i = 0; i < cursor.getColumnCount(); i++) {
1015
                                                        if (cursor.getColumnName(i).equals("_data"))
1016
                                                                filenameNew = cursor.getString(i);
1017
                                                        if (cursor.getColumnName(i).equals("_size"))
1018
                                                                fileSize = cursor.getLong(i);
1019
                                                }
1020
                                        }
1021
                                }
1022
                        } else {
1023
                                filenameNew = data.getPath();
1024
                        }
1025
                        if (filenameNew != null) {
1026
                                file = new File(filenameNew);
1027
                                strings.add(path+URLEncoder.encode(file.getName()));
1028
                                strings.add(filenameNew);
1029
                        }
1030
                }
1031
                Log.i("*******",""+strings.size());
1032
                //getTasks().getMultiUploadTask(this).execute(strings.toArray(new String[strings.size()]));
1033
                        
1034
                
1035
        }
1036
        protected void uploadFile(Uri data){
1037
                String filename = data.getPath();
1038
                if (filename.startsWith("file://")) {
1039
                        filename = filename.substring(7);
1040
                }
1041
                // replace %20 and so on
1042
                filename = Uri.decode(filename);
1043

    
1044
                int indx = filename.lastIndexOf("//");
1045
                File file = new File(filename);
1046
                String filenameNew = null;
1047
                Long fileSize = null;
1048
                if (!file.exists()) {
1049
                        Cursor cursor = getContentResolver().query(data, null, null, null, null);
1050
                        if (cursor != null) {
1051
                                while (cursor.moveToNext()) {
1052
                                        for (int i = 0; i < cursor.getColumnCount(); i++) {
1053
                                                if (cursor.getColumnName(i).equals("_data"))
1054
                                                        filenameNew = cursor.getString(i);
1055
                                                if (cursor.getColumnName(i).equals("_size"))
1056
                                                        fileSize = cursor.getLong(i);
1057
                                        }
1058
                                }
1059
                        }
1060
                } else {
1061
                        filenameNew = data.getPath();
1062
                }
1063
                if (filenameNew != null) {
1064
                        file = new File(filenameNew);
1065
                        //String path = getCurrentResource().getUri();
1066
                        //if(!path.endsWith("/"))
1067
                                //path = path+"/";
1068
                        //getTasks().getUploadTask(GssMyFolders.this).execute(path + URLEncoder.encode(file.getName()), filenameNew);
1069
                }
1070
                
1071
        }
1072
}