Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / android / ContainerObjectsActivity.java @ 802fbd9e

History | View | Annotate | Download (29.1 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
                if (currentPath.equals("")) {
186
                        finish();
187
                } else {
188
                        goUpDirectory();
189
                }
190
        }
191

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
410
                        } catch (ActivityNotFoundException e) {
411
                                Toast.makeText(this, "No File Manager", Toast.LENGTH_SHORT)
412
                                                .show();
413
                        }
414

    
415
                        return true;
416
                }
417
                return false;
418
        }
419

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

    
527
        @Override
528
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
529
                super.onActivityResult(requestCode, resultCode, data);
530

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

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

    
547
        }
548

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

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

    
561
                        TextView label = (TextView) row.findViewById(R.id.label);
562
                        label.setText(getShortName(file.getCName()));
563

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

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

    
585
                        return (row);
586
                }
587
        }
588

    
589
        private class LoadFilesTask extends
590
                        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
591

    
592
                private CloudServersException exception;
593

    
594
                protected void onPreExecute() {
595
                        showDialog();
596
                        loadingFiles = true;
597
                }
598

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

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

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

    
638
        }
639

    
640
        private class AddFolderTask extends AsyncTask<String, Void, HttpBundle> {
641

    
642
                private CloudServersException exception;
643

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

    
652
                @Override
653
                protected HttpBundle doInBackground(String... data) {
654
                        HttpBundle bundle = null;
655
                        try {
656

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

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

    
694
                private CloudServersException exception;
695

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

    
704
                @Override
705
                protected HttpBundle doInBackground(String... data) {
706
                        HttpBundle bundle = null;
707
                        try {
708

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

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

    
744
        private class DeleteObjectTask extends AsyncTask<Void, Void, HttpBundle> {
745

    
746
                private CloudServersException exception;
747

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

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

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

    
801
        private class DeleteContainerTask extends
802
                        AsyncTask<String, Void, HttpBundle> {
803

    
804
                private CloudServersException exception;
805

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

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

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

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

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

    
865
                @Override
866
                protected Void doInBackground(Void... arg1) {
867

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

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

    
888
        private class DeleteObjectListenerTask extends AsyncTask<Void, Void, Void> {
889

    
890
                @Override
891
                protected Void doInBackground(Void... arg1) {
892

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

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

    
914
        private class DeleteContainerListenerTask extends
915
                        AsyncTask<Void, Void, Void> {
916

    
917
                @Override
918
                protected Void doInBackground(Void... arg1) {
919

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

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

    
936
                        hideDialog();
937
                        setResult(RESULT_OK);
938
                        finish();
939
                }
940
        }
941

    
942
        public String getCurrentPath() {
943
                return currentPath;
944
        }
945

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

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

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

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