Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (30.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.Collections;
10
import java.util.List;
11

    
12
import org.apache.http.HttpResponse;
13

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

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

    
47
/**
48
 * 
49
 * @author Phillip Toohill
50
 * 
51
 */
52

    
53
public class ContainerObjectsActivity extends CloudListActivity {
54

    
55
        private static final int deleteContainer = 0;
56
        private static final int deleteFolder = 1;
57

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

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

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

    
90
        @Override
91
        protected void onSaveInstanceState(Bundle outState) {
92
                super.onSaveInstanceState(outState);
93

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

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

    
100
                outState.putBoolean("loadingFiles", loadingFiles);
101
        }
102

    
103
        protected void restoreState(Bundle state) {
104
                super.restoreState(state);
105

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

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

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

    
130
                                }
131

    
132
                        }
133
                } else {
134
                        currentPath = "";
135
                        loadFiles();
136
                }
137

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

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

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

    
159
        }
160

    
161
        @Override
162
        protected void onStop() {
163
                super.onStop();
164

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

    
172
                if (deleteObjTask != null) {
173
                        deleteObjTask.cancel(true);
174
                }
175

    
176
                if (deleteContainerTask != null) {
177
                        deleteContainerTask.cancel(true);
178
                }
179

    
180
        }
181

    
182
        /*
183
         * overriding back button press, because we are not actually changing
184
         * activities when we navigate the file structure
185
         */
186
        public void onBackPressed() {
187
                
188
                if (currentPath.equals("")) {
189
                        if(container.getName().equals(Container.MYSHARED)){}
190
                        else
191
                                finish();
192
                } else {
193
                        goUpDirectory();
194
                }
195
        }
196
        List<ContainerObjects> previousFiles = new ArrayList<ContainerObjects>();
197
        /*
198
         * go to the current directory's parent and display that data
199
         */
200
        private void goUpDirectory() {
201
                currentPath = currentPath.substring(
202
                                0,
203
                                currentPath.substring(0, currentPath.length() - 2).lastIndexOf(
204
                                                "/") + 1);
205
                if(container.getName().equalsIgnoreCase(Container.MYSHARED)){
206
                        loadCurrentDirectoryFiles();
207
                        displayCurrentFiles();
208
                }
209
                else if(previousFiles!=null&&previousFiles.size()>0){
210
                        files = previousFiles.toArray(new ContainerObjects[]{});
211
                        previousFiles=null;
212
                        loadCurrentDirectoryFiles();
213
                        displayCurrentFiles();
214
                }
215
                else
216
                        loadFiles();
217
                //now executing new call
218
                
219
        }
220

    
221
        /*
222
         * load all file that are in the container
223
         */
224
        private void loadFiles() {
225
                // displayLoadingCell();
226
                new LoadFilesTask().execute(currentPath);
227
        }
228

    
229
        /*
230
         * load only the files that should display for the current directory in the
231
         * curDirFiles[]
232
         */
233
        protected void loadCurrentDirectoryFiles() {
234
                ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>();
235
                Log.i(LOG, "loading files");
236
                if (files != null) {
237
                        for (int i = 0; i < files.length; i++) {
238
                                Log.i(LOG, "loading files" + i);
239
                                if (fileBelongsInDir(files[i])) {
240
                                        curFiles.add(files[i]);
241
                                }
242
                        }
243
                        app.setCurFiles(curFiles);
244
                }
245
        }
246

    
247
        /*
248
         * determines if a file should be displayed in current directory
249
         */
250
        protected Boolean fileBelongsInDir(ContainerObjects obj) {
251
                String objPath = obj.getCName();
252

    
253
                if (!objPath.startsWith(currentPath)) {
254
                        Log.i(LOG, "Path is:" + currentPath + " " + objPath + " " + false);
255
                        return false;
256
                } else {
257
                        objPath = objPath.substring(currentPath.length());
258
                        Log.i(LOG, "Path is:" + currentPath + " " + objPath + " "
259
                                        + !objPath.contains("/"));
260
                        return !objPath.contains("/");
261
                }
262
        }
263

    
264
        /*
265
         * loads all the files that are in the container into one array
266
         */
267
        private void setFileList(ArrayList<ContainerObjects> files) {
268
                if (files == null) {
269
                        files = new ArrayList<ContainerObjects>();
270
                }
271
                String[] fileNames = new String[files.size()];
272
                this.files = new ContainerObjects[files.size()];
273

    
274
                if (files != null) {
275
                        for (int i = 0; i < files.size(); i++) {
276
                                ContainerObjects file = files.get(i);
277
                                // Log.i(file.getCName(),file.getCName());
278
                                this.files[i] = file;
279
                                fileNames[i] = file.getName();
280
                        }
281
                }
282
        }
283

    
284
        protected void displayCurrentFiles() {
285
                if (app.getCurFiles().size() == 0) {
286
                        displayNoFilesCell();
287
                } else {
288
                        ArrayList<ContainerObjects> tempList = new ArrayList<ContainerObjects>();
289
                        for (int i = 0; i < app.getCurFiles().size(); i++) {
290
                                tempList.add(app.getCurFiles().get(i));
291
                        }
292
                        getListView().setDividerHeight(1); // restore divider lines
293
                        setListAdapter(new FileAdapter());
294
                }
295
        }
296

    
297
        /*
298
         * display a different empty page depending of if you are at top of
299
         * container or in a folder
300
         */
301
        protected void displayNoFilesCell() {
302
                String a[] = new String[1];
303
                if (currentPath.equals("")) {
304
                        a[0] = "Empty Container";
305
                        setListAdapter(new ArrayAdapter<String>(this,
306
                                        R.layout.noobjectscell, R.id.no_files_label, a));
307
                } else {
308
                        a[0] = "No Files";
309
                        setListAdapter(new ArrayAdapter<String>(this, R.layout.nofilescell,
310
                                        R.id.no_files_label, a));
311
                }
312
                getListView().setTextFilterEnabled(true);
313
                getListView().setDividerHeight(0); // hide the dividers so it won't look
314
                // like a list row
315
                getListView().setItemsCanFocus(false);
316
        }
317

    
318
        /*
319
         * just get the last part of the filename so the entire file path does not
320
         * show
321
         */
322
        private String getShortName(String longName) {
323
                String s = longName;
324
                if (!s.contains("/")) {
325
                        return s;
326
                } else {
327
                        String tmp = s.substring(s.lastIndexOf('/') + 1);
328
                        if (tmp.equals("")) {
329
                                return s;
330
                        }
331
                        return tmp;
332
                }
333
        }
334

    
335
        /*
336
         * removed a specified object from the array of all the files in the
337
         * container
338
         */
339
        private void removeFromList(String path) {
340
                ArrayList<ContainerObjects> temp = new ArrayList<ContainerObjects>(
341
                                Arrays.asList(files));
342
                for (int i = 0; i < files.length; i++) {
343
                        if (files[i].getCName()
344
                                        .equals(path.substring(0, path.length() - 1))) {
345
                                temp.remove(i);
346
                        }
347
                }
348
                files = new ContainerObjects[temp.size()];
349
                for (int i = 0; i < temp.size(); i++) {
350
                        files[i] = temp.get(i);
351
                }
352
        }
353

    
354
        protected void onListItemClick(ListView l, View v, int position, long id) {
355
                if (app.getCurFiles() != null && app.getCurFiles().size() > 0) {
356
                        Intent viewIntent;
357
                        if (app.getCurFiles().get(position).getContentType()
358
                                        .startsWith("application/directory")) {
359
                                currentPath = app.getCurFiles().get(position).getCName() + "/";
360
                                if(container.getName().equalsIgnoreCase(Container.MYSHARED)){
361
                                        loadCurrentDirectoryFiles();
362
                                        displayCurrentFiles();
363
                                }
364
                                else{
365
                                        previousFiles = Arrays.asList(files);
366
                                        loadFiles();
367
                                }
368
                                //loadCurrentDirectoryFiles();
369
                                //displayCurrentFiles();
370
                        }
371

    
372
                        else {
373
                                viewIntent = new Intent(this, ContainerObjectDetails.class);
374
                                viewIntent.putExtra("container", app.getCurFiles()
375
                                                .get(position));
376
                                viewIntent.putExtra("cdnUrl", container.getCdnUrl());
377
                                viewIntent.putExtra("containerNames", container.getName());
378
                                viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
379
                                startActivityForResult(viewIntent, 55); // arbitrary number;
380
                                                                                                                // never
381
                                // used again
382
                        }
383
                }
384
        }
385

    
386
        @Override
387
        public boolean onCreateOptionsMenu(Menu menu) {
388
                super.onCreateOptionsMenu(menu);
389
                MenuInflater inflater = getMenuInflater();
390
                inflater.inflate(R.menu.view_container_object_list_menu, menu);
391
                menu.findItem(R.id.enable_cdn).setVisible(false);
392
                if(Container.TRASH.equalsIgnoreCase(container.getName())){
393
                        menu.findItem(R.id.delete_container).setVisible(false);
394
                        menu.findItem(R.id.add_folder).setVisible(false);
395
                        menu.findItem(R.id.add_file).setVisible(false);
396
                }
397
                return true;
398
        }
399

    
400
        @Override
401
        /*
402
         * option performed for delete depends on if you are at the top of a
403
         * container or in a folder
404
         */
405
        public boolean onOptionsItemSelected(MenuItem item) {
406
                switch (item.getItemId()) {
407
                case R.id.delete_container:
408
                        if (currentPath.equals("")) {
409
                                showDialog(deleteContainer);
410
                        } else {
411
                                showDialog(deleteFolder);
412
                        }
413
                        return true;
414
                case R.id.enable_cdn:
415
                        Intent viewIntent1 = new Intent(this, EnableCDNActivity.class);
416
                        viewIntent1.putExtra("Cname", container.getName());
417
                        startActivityForResult(viewIntent1, 56);
418
                        return true;
419
                case R.id.refresh:
420
                        loadFiles();
421
                        return true;
422
                case R.id.add_folder:
423
                        showDialog(R.id.add_folder);
424
                        return true;
425
                case R.id.add_file:
426
                        /*
427
                         * Intent viewIntent2 = new Intent(this, AddFileActivity.class);
428
                         * viewIntent2.putExtra("Cname", container.getName());
429
                         * viewIntent2.putExtra("curPath", currentPath);
430
                         * startActivityForResult(viewIntent2, 56);
431
                         */
432

    
433
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
434
                        intent.addCategory(Intent.CATEGORY_OPENABLE);
435
                        intent.setData(Uri.parse("file://"));
436
                        intent.setType("*/*");
437
                        try {
438
                                int i = 1;
439
                                startActivityForResult(intent, i);
440

    
441
                        } catch (ActivityNotFoundException e) {
442
                                Toast.makeText(this, "No File Manager", Toast.LENGTH_SHORT)
443
                                                .show();
444
                        }
445

    
446
                        return true;
447
                }
448
                return false;
449
        }
450

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

    
558
        @Override
559
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
560
                super.onActivityResult(requestCode, resultCode, data);
561

    
562
                if (resultCode == RESULT_OK && requestCode == 56) {
563
                        // a sub-activity kicked back, so we want to refresh the server list
564
                        loadFiles();
565
                }
566
                
567
                if (resultCode == RESULT_OK && requestCode == 1) {
568
                        String filename = data.getDataString();
569
                        uploadFile(filename, data);
570
                        
571
                }
572

    
573
                // deleted file so need to update the list
574
                if (requestCode == 55 && resultCode == 99) {
575
                        loadFiles();
576
                }
577

    
578
        }
579

    
580
        class FileAdapter extends ArrayAdapter<ContainerObjects> {
581
                FileAdapter() {
582
                        super(ContainerObjectsActivity.this,
583
                                        R.layout.listcontainerobjectcell, app.getCurFiles());
584
                }
585

    
586
                public View getView(int position, View convertView, ViewGroup parent) {
587
                        ContainerObjects file = app.getCurFiles().get(position);
588
                        LayoutInflater inflater = getLayoutInflater();
589
                        View row = inflater.inflate(R.layout.listcontainerobjectcell,
590
                                        parent, false);
591

    
592
                        TextView label = (TextView) row.findViewById(R.id.label);
593
                        label.setText(getShortName(file.getCName()));
594

    
595
                        ImageView objectImage = (ImageView) row
596
                                        .findViewById(R.id.file_type_image);
597
                        if (file.getContentType().startsWith("application/directory")||file.getContentType().startsWith("application/folder")) {
598
                                objectImage.setImageResource(R.drawable.folder);
599
                        } else {
600
                                objectImage.setImageResource(R.drawable.file);
601
                        }
602

    
603
                        if (file.getBytes() >= bConver) {
604
                                megaBytes = Math.abs(file.getBytes() / bConver + 0.2);
605
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
606
                                sublabel.setText(megaBytes + " MB");
607
                        } else if (file.getBytes() >= kbConver) {
608
                                kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2);
609
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
610
                                sublabel.setText(kiloBytes + " KB");
611
                        } else {
612
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
613
                                sublabel.setText(file.getBytes() + " B");
614
                        }
615

    
616
                        return (row);
617
                }
618
        }
619

    
620
        private class LoadFilesTask extends
621
                        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
622

    
623
                private CloudServersException exception;
624

    
625
                protected void onPreExecute() {
626
                        showDialog();
627
                        loadingFiles = true;
628
                }
629

    
630
                @Override
631
                protected ArrayList<ContainerObjects> doInBackground(String... path) {
632
                        ArrayList<ContainerObjects> files = null;
633
                        try {
634
                                if (container.getName().equals(Container.MYSHARED)) {
635
                                        files = (new ContainerObjectManager(getContext()))
636
                                                        .createListMyShared(true, container.getName(),
637
                                                                        new ContainerManager(getContext())
638
                                                                                        .createList(true));
639
                                } else if (container.getName().equals(Container.OTHERS)) {
640

    
641
                                } else {
642
                                        if (container.getOtherUser() == null)
643
                                                files = (new ContainerObjectManager(getContext()))
644
                                                                .createList(true, container.getName(),path[0]);
645
                                        else
646
                                                files = (new ContainerObjectManager(getContext()))
647
                                                                .createOtherList(true, container.getName(),
648
                                                                                container.getOtherUser());
649
                                }
650
                        } catch (CloudServersException e) {
651
                                exception = e;
652
                                e.printStackTrace();
653
                        }
654
                        return files;
655
                }
656

    
657
                @Override
658
                protected void onPostExecute(ArrayList<ContainerObjects> result) {
659
                        loadingFiles = false;
660
                        hideDialog();
661
                        if (exception != null) {
662
                                showAlert("Error", exception.getMessage());
663
                        }
664
                        setFileList(result);
665
                        loadCurrentDirectoryFiles();
666
                        displayCurrentFiles();
667
                }
668

    
669
        }
670

    
671
        private class AddFolderTask extends AsyncTask<String, Void, HttpBundle> {
672

    
673
                private CloudServersException exception;
674

    
675
                @Override
676
                protected void onPreExecute() {
677
                        showDialog();
678
                        app.setAddingObject(true);
679
                        task = new AddObjectListenerTask();
680
                        task.execute();
681
                }
682

    
683
                @Override
684
                protected HttpBundle doInBackground(String... data) {
685
                        HttpBundle bundle = null;
686
                        try {
687

    
688
                                bundle = (new ContainerObjectManager(getContext())).addObject(
689
                                                container.getName(), currentPath, data[0], data[1]);
690
                        } catch (CloudServersException e) {
691
                                exception = e;
692
                        }
693
                        return bundle;
694
                }
695

    
696
                @Override
697
                protected void onPostExecute(HttpBundle bundle) {
698
                        app.setAddingObject(false);
699
                        hideDialog();
700
                        HttpResponse response = bundle.getResponse();
701
                        if (response != null) {
702
                                int statusCode = response.getStatusLine().getStatusCode();
703
                                if (statusCode == 201) {
704
                                        setResult(Activity.RESULT_OK);
705
                                        // loading the new files is done by ListenerTask
706
                                } else {
707
                                        CloudServersException cse = parseCloudServersException(response);
708
                                        if ("".equals(cse.getMessage())) {
709
                                                showError("There was a problem deleting your folder.",
710
                                                                bundle);
711
                                        } else {
712
                                                showError("There was a problem deleting your folder: "
713
                                                                + cse.getMessage(), bundle);
714
                                        }
715
                                }
716
                        } else if (exception != null) {
717
                                showError("There was a problem deleting your folder: "
718
                                                + exception.getMessage(), bundle);
719
                        }
720
                }
721
        }
722
        
723
        private class AddFileTask extends AsyncTask<String, Void, HttpBundle> {
724

    
725
                private CloudServersException exception;
726

    
727
                @Override
728
                protected void onPreExecute() {
729
                        showDialog();
730
                        app.setAddingObject(true);
731
                        task = new AddObjectListenerTask();
732
                        task.execute();
733
                }
734

    
735
                @Override
736
                protected HttpBundle doInBackground(String... data) {
737
                        HttpBundle bundle = null;
738
                        try {
739

    
740
                                bundle = (new ContainerObjectManager(getContext())).addFileObject(
741
                                                container.getName(), currentPath, data[0], data[1], data[2]);
742
                        } catch (CloudServersException e) {
743
                                exception = e;
744
                        }
745
                        return bundle;
746
                }
747

    
748
                @Override
749
                protected void onPostExecute(HttpBundle bundle) {
750
                        app.setAddingObject(false);
751
                        hideDialog();
752
                        HttpResponse response = bundle.getResponse();
753
                        if (response != null) {
754
                                int statusCode = response.getStatusLine().getStatusCode();
755
                                if (statusCode == 201) {
756
                                        setResult(Activity.RESULT_OK);
757
                                        // loading the new files is done by ListenerTask
758
                                } else {
759
                                        CloudServersException cse = parseCloudServersException(response);
760
                                        if ("".equals(cse.getMessage())) {
761
                                                showError("There was a problem deleting your folder.",
762
                                                                bundle);
763
                                        } else {
764
                                                showError("There was a problem deleting your folder: "
765
                                                                + cse.getMessage(), bundle);
766
                                        }
767
                                }
768
                        } else if (exception != null) {
769
                                showError("There was a problem deleting your folder: "
770
                                                + exception.getMessage(), bundle);
771
                        }
772
                }
773
        }
774

    
775
        private class DeleteObjectTask extends AsyncTask<Void, Void, HttpBundle> {
776

    
777
                private CloudServersException exception;
778

    
779
                @Override
780
                protected void onPreExecute() {
781
                        showDialog();
782
                        app.setDeleteingObject(true);
783
                        deleteObjTask = new DeleteObjectListenerTask();
784
                        deleteObjTask.execute();
785
                }
786

    
787
                @Override
788
                protected HttpBundle doInBackground(Void... arg0) {
789
                        HttpBundle bundle = null;
790
                        try {
791
                                // subtring because the current directory contains a "/" at the
792
                                // end of the string
793
                                bundle = (new ContainerObjectManager(getContext()))
794
                                                .deleteObject(container.getName(), currentPath
795
                                                                .substring(0, currentPath.length() - 1));
796
                        } catch (CloudServersException e) {
797
                                exception = e;
798
                        }
799
                        return bundle;
800
                }
801

    
802
                @Override
803
                protected void onPostExecute(HttpBundle bundle) {
804
                        app.setDeleteingObject(false);
805
                        hideDialog();
806
                        HttpResponse response = bundle.getResponse();
807
                        if (response != null) {
808
                                int statusCode = response.getStatusLine().getStatusCode();
809
                                if (statusCode == 409) {
810
                                        showAlert("Error",
811
                                                        "Folder must be empty in order to delete");
812
                                }
813
                                if (statusCode == 204) {
814
                                        setResult(Activity.RESULT_OK);
815
                                } else {
816
                                        CloudServersException cse = parseCloudServersException(response);
817
                                        if ("".equals(cse.getMessage())) {
818
                                                showError("There was a problem deleting your folder.",
819
                                                                bundle);
820
                                        } else {
821
                                                showError("There was a problem deleting your folder: "
822
                                                                + cse.getMessage(), bundle);
823
                                        }
824
                                }
825
                        } else if (exception != null) {
826
                                showError("There was a problem deleting your folder: "
827
                                                + exception.getMessage(), bundle);
828
                        }
829
                }
830
        }
831

    
832
        private class DeleteContainerTask extends
833
                        AsyncTask<String, Void, HttpBundle> {
834

    
835
                private CloudServersException exception;
836

    
837
                @Override
838
                protected void onPreExecute() {
839
                        showDialog();
840
                        app.setDeletingContainer(true);
841
                        deleteContainerTask = new DeleteContainerListenerTask();
842
                        deleteContainerTask.execute();
843
                }
844

    
845
                @Override
846
                protected HttpBundle doInBackground(String... object) {
847
                        HttpBundle bundle = null;
848
                        try {
849
                                bundle = (new ContainerManager(getContext())).delete(container
850
                                                .getName());
851
                        } catch (CloudServersException e) {
852
                                exception = e;
853
                        }
854
                        return bundle;
855
                }
856

    
857
                @Override
858
                protected void onPostExecute(HttpBundle bundle) {
859
                        hideDialog();
860
                        app.setDeletingContainer(false);
861
                        HttpResponse response = bundle.getResponse();
862
                        if (response != null) {
863
                                int statusCode = response.getStatusLine().getStatusCode();
864
                                if (statusCode == 409) {
865
                                        showError("Container must be empty in order to delete",
866
                                                        bundle);
867
                                }
868
                                if (statusCode == 204) {
869
                                        setResult(Activity.RESULT_OK);
870

    
871
                                } else {
872
                                        CloudServersException cse = parseCloudServersException(response);
873
                                        if ("".equals(cse.getMessage())) {
874
                                                showError(
875
                                                                "There was a problem deleting your container.",
876
                                                                bundle);
877
                                        } else {
878
                                                showError(
879
                                                                "There was a problem deleting your container: "
880
                                                                                + cse.getMessage(), bundle);
881
                                        }
882
                                }
883
                        } else if (exception != null) {
884
                                showError("There was a problem deleting your server: "
885
                                                + exception.getMessage(), bundle);
886
                        }
887
                }
888
        }
889

    
890
        /*
891
         * listens for the application to change isProcessing listens so activity
892
         * knows when it should display the new curDirFiles
893
         */
894
        private class AddObjectListenerTask extends AsyncTask<Void, Void, Void> {
895

    
896
                @Override
897
                protected Void doInBackground(Void... arg1) {
898

    
899
                        while (app.isAddingObject()) {
900
                                // wait for process to finish
901
                                // or have it be canceled
902
                                if (task.isCancelled()) {
903
                                        return null;
904
                                }
905
                        }
906
                        return null;
907
                }
908

    
909
                /*
910
                 * when no longer processing, time to load the new files
911
                 */
912
                @Override
913
                protected void onPostExecute(Void arg1) {
914
                        hideDialog();
915
                        loadFiles();
916
                }
917
        }
918

    
919
        private class DeleteObjectListenerTask extends AsyncTask<Void, Void, Void> {
920

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

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

    
934
                /*
935
                 * when no longer processing, time to load the new files
936
                 */
937
                @Override
938
                protected void onPostExecute(Void arg1) {
939
                        hideDialog();
940
                        removeFromList(currentPath);
941
                        goUpDirectory();
942
                }
943
        }
944

    
945
        private class DeleteContainerListenerTask extends
946
                        AsyncTask<Void, Void, Void> {
947

    
948
                @Override
949
                protected Void doInBackground(Void... arg1) {
950

    
951
                        while (app.isDeletingContainer()) {
952
                                // wait for process to finish
953
                                // or have it be canceled
954
                                if (deleteContainerTask.isCancelled()) {
955
                                        return null;
956
                                }
957
                        }
958
                        return null;
959
                }
960

    
961
                /*
962
                 * when no longer processing, time to load the new files
963
                 */
964
                @Override
965
                protected void onPostExecute(Void arg1) {
966

    
967
                        hideDialog();
968
                        setResult(RESULT_OK);
969
                        finish();
970
                }
971
        }
972

    
973
        public String getCurrentPath() {
974
                return currentPath;
975
        }
976

    
977
        public ContainerObjects[] getFiles() {
978
                return files;
979
        }
980
        
981
        /***** UPLOAD FILE ****/
982
        protected void uploadFile(String filename, Intent data){
983
                if (filename.startsWith("file://")) {
984
                        filename = filename.substring(7);
985
                }
986
                // replace %20 and so on
987
                filename = Uri.decode(filename);
988

    
989
                int indx = filename.lastIndexOf("//");
990
                File file = new File(data.getData().getPath());
991
                String filenameNew = null;
992
                Long fileSize = null;
993
                if (!file.exists()) {
994
                        Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
995
                        if (cursor != null) {
996
                                while (cursor.moveToNext()) {
997
                                        for (int i = 0; i < cursor.getColumnCount(); i++) {
998
                                                if (cursor.getColumnName(i).equals("_data"))
999
                                                        filenameNew = cursor.getString(i);
1000
                                                if (cursor.getColumnName(i).equals("_size"))
1001
                                                        fileSize = cursor.getLong(i);
1002
                                        }
1003
                                }
1004
                        }
1005
                } else {
1006
                        filenameNew = data.getData().getPath();
1007
                }
1008
                if (filenameNew != null) {
1009
                        file = new File(filenameNew);
1010
                        String ext = MimeTypeMap.getSingleton().getFileExtensionFromUrl(file.getPath());
1011
                        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
1012
                        String[] info = {
1013
                                        file.getName(),
1014
                                        mime, filenameNew};
1015
                        new AddFileTask().execute(info);
1016
                        //getTasks().getUploadTask(GssMyFolders.this).execute(getCurrentResource().getUri() + URLEncoder.encode(file.getName()), filenameNew);
1017
                }
1018
        }
1019
        
1020
        protected void uploadFile(List<Uri> uris){
1021
                List<String> strings = new ArrayList<String>();
1022
                String path = "";//getCurrentResource().getUri();
1023
                if(!path.endsWith("/"))
1024
                        path = path+"/";
1025
                
1026
                for(Uri data : uris){
1027
                        String filename = data.getPath();
1028
                        if (filename.startsWith("file://")) {
1029
                                filename = filename.substring(7);
1030
                        }
1031
                        // replace %20 and so on
1032
                        filename = Uri.decode(filename);
1033

    
1034
                        int indx = filename.lastIndexOf("//");
1035
                        File file = new File(filename);
1036
                        String filenameNew = null;
1037
                        Long fileSize = null;
1038
                        if (!file.exists()) {
1039
                                Cursor cursor = getContentResolver().query(data, null, null, null, null);
1040
                                if (cursor != null) {
1041
                                        while (cursor.moveToNext()) {
1042
                                                for (int i = 0; i < cursor.getColumnCount(); i++) {
1043
                                                        if (cursor.getColumnName(i).equals("_data"))
1044
                                                                filenameNew = cursor.getString(i);
1045
                                                        if (cursor.getColumnName(i).equals("_size"))
1046
                                                                fileSize = cursor.getLong(i);
1047
                                                }
1048
                                        }
1049
                                }
1050
                        } else {
1051
                                filenameNew = data.getPath();
1052
                        }
1053
                        if (filenameNew != null) {
1054
                                file = new File(filenameNew);
1055
                                strings.add(path+URLEncoder.encode(file.getName()));
1056
                                strings.add(filenameNew);
1057
                        }
1058
                }
1059
                Log.i("*******",""+strings.size());
1060
                //getTasks().getMultiUploadTask(this).execute(strings.toArray(new String[strings.size()]));
1061
                        
1062
                
1063
        }
1064
        protected void uploadFile(Uri data){
1065
                String filename = data.getPath();
1066
                if (filename.startsWith("file://")) {
1067
                        filename = filename.substring(7);
1068
                }
1069
                // replace %20 and so on
1070
                filename = Uri.decode(filename);
1071

    
1072
                int indx = filename.lastIndexOf("//");
1073
                File file = new File(filename);
1074
                String filenameNew = null;
1075
                Long fileSize = null;
1076
                if (!file.exists()) {
1077
                        Cursor cursor = getContentResolver().query(data, null, null, null, null);
1078
                        if (cursor != null) {
1079
                                while (cursor.moveToNext()) {
1080
                                        for (int i = 0; i < cursor.getColumnCount(); i++) {
1081
                                                if (cursor.getColumnName(i).equals("_data"))
1082
                                                        filenameNew = cursor.getString(i);
1083
                                                if (cursor.getColumnName(i).equals("_size"))
1084
                                                        fileSize = cursor.getLong(i);
1085
                                        }
1086
                                }
1087
                        }
1088
                } else {
1089
                        filenameNew = data.getPath();
1090
                }
1091
                if (filenameNew != null) {
1092
                        file = new File(filenameNew);
1093
                        //String path = getCurrentResource().getUri();
1094
                        //if(!path.endsWith("/"))
1095
                                //path = path+"/";
1096
                        //getTasks().getUploadTask(GssMyFolders.this).execute(path + URLEncoder.encode(file.getName()), filenameNew);
1097
                }
1098
                
1099
        }
1100
}