Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (29.2 kB)

1 7dbfc514 koutsoub
package com.rackspace.cloud.android;
2 3d6041e8 Phillip Toohill
3 802fbd9e koutsoub
4 802fbd9e koutsoub
5 802fbd9e koutsoub
import java.io.File;
6 802fbd9e koutsoub
import java.net.URLEncoder;
7 3d6041e8 Phillip Toohill
import java.util.ArrayList;
8 d3ea294b Adam Menz
import java.util.Arrays;
9 802fbd9e koutsoub
import java.util.List;
10 3d6041e8 Phillip Toohill
11 3d6041e8 Phillip Toohill
import org.apache.http.HttpResponse;
12 3d6041e8 Phillip Toohill
13 3d6041e8 Phillip Toohill
import android.app.Activity;
14 3d6041e8 Phillip Toohill
import android.app.AlertDialog;
15 3d6041e8 Phillip Toohill
import android.app.Dialog;
16 802fbd9e koutsoub
import android.content.ActivityNotFoundException;
17 3d6041e8 Phillip Toohill
import android.content.DialogInterface;
18 3d6041e8 Phillip Toohill
import android.content.Intent;
19 802fbd9e koutsoub
import android.database.Cursor;
20 802fbd9e koutsoub
import android.net.Uri;
21 3d6041e8 Phillip Toohill
import android.os.AsyncTask;
22 3d6041e8 Phillip Toohill
import android.os.Bundle;
23 ef6fa21a koutsoub
import android.util.Log;
24 3d6041e8 Phillip Toohill
import android.view.LayoutInflater;
25 3d6041e8 Phillip Toohill
import android.view.Menu;
26 3d6041e8 Phillip Toohill
import android.view.MenuInflater;
27 3d6041e8 Phillip Toohill
import android.view.MenuItem;
28 3d6041e8 Phillip Toohill
import android.view.View;
29 3d6041e8 Phillip Toohill
import android.view.ViewGroup;
30 802fbd9e koutsoub
import android.webkit.MimeTypeMap;
31 3d6041e8 Phillip Toohill
import android.widget.ArrayAdapter;
32 d3ea294b Adam Menz
import android.widget.EditText;
33 f506786e Adam Menz
import android.widget.ImageView;
34 3d6041e8 Phillip Toohill
import android.widget.ListView;
35 3d6041e8 Phillip Toohill
import android.widget.TextView;
36 802fbd9e koutsoub
import android.widget.Toast;
37 3d6041e8 Phillip Toohill
38 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.Container;
39 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerManager;
40 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
41 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerObjects;
42 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.CloudServersException;
43 32731215 Adam Menz
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
44 3d6041e8 Phillip Toohill
45 240418be Phillip Toohill
/**
46 3d6041e8 Phillip Toohill
 * 
47 3d6041e8 Phillip Toohill
 * @author Phillip Toohill
48 240418be Phillip Toohill
 * 
49 3d6041e8 Phillip Toohill
 */
50 6b8dad86 Adam Menz
51 b2a2d2f1 Adam Menz
public class ContainerObjectsActivity extends CloudListActivity {
52 3d6041e8 Phillip Toohill
53 3d6041e8 Phillip Toohill
        private static final int deleteContainer = 0;
54 37a14877 Adam Menz
        private static final int deleteFolder = 1;
55 71e52c4a Adam Menz
56 3d6041e8 Phillip Toohill
        private ContainerObjects[] files;
57 3d6041e8 Phillip Toohill
        private static Container container;
58 3d6041e8 Phillip Toohill
        public String LOG = "viewFilesActivity";
59 8b432514 Phillip Toohill
        private String cdnEnabledIs;
60 6864568a Phillip Toohill
        public Object megaBytes;
61 6864568a Phillip Toohill
        public Object kiloBytes;
62 6864568a Phillip Toohill
        public int bConver = 1048576;
63 6864568a Phillip Toohill
        public int kbConver = 1024;
64 51938302 Adam Menz
        private boolean loadingFiles;
65 b2a2d2f1 Adam Menz
        private String currentPath;
66 a7d1fdc2 Adam Menz
        private AndroidCloudApplication app;
67 a7d1fdc2 Adam Menz
        private AddObjectListenerTask task;
68 a7d1fdc2 Adam Menz
        private DeleteObjectListenerTask deleteObjTask;
69 a7d1fdc2 Adam Menz
        private DeleteContainerListenerTask deleteContainerTask;
70 71e52c4a Adam Menz
71 3d6041e8 Phillip Toohill
        @Override
72 240418be Phillip Toohill
        public void onCreate(Bundle savedInstanceState) {
73 240418be Phillip Toohill
                super.onCreate(savedInstanceState);
74 5018a7f8 Adam Menz
                trackPageView(GoogleAnalytics.PAGE_FOLDER);
75 856ad13d koutsoub
                container = getContainer();
76 240418be Phillip Toohill
                if (container.isCdnEnabled() == true) {
77 240418be Phillip Toohill
                        cdnEnabledIs = "true";
78 240418be Phillip Toohill
                } else {
79 240418be Phillip Toohill
                        cdnEnabledIs = "false";
80 240418be Phillip Toohill
                }
81 240418be Phillip Toohill
                restoreState(savedInstanceState);
82 240418be Phillip Toohill
        }
83 802fbd9e koutsoub
84 802fbd9e koutsoub
        protected Container getContainer() {
85 856ad13d koutsoub
                return (Container) this.getIntent().getExtras().get("container");
86 856ad13d koutsoub
        }
87 802fbd9e koutsoub
88 3d6041e8 Phillip Toohill
        @Override
89 3d6041e8 Phillip Toohill
        protected void onSaveInstanceState(Bundle outState) {
90 3d6041e8 Phillip Toohill
                super.onSaveInstanceState(outState);
91 71e52c4a Adam Menz
92 802fbd9e koutsoub
                // files stores all the files in the container
93 3d6041e8 Phillip Toohill
                outState.putSerializable("container", files);
94 71e52c4a Adam Menz
95 802fbd9e koutsoub
                // current path represents where you have "navigated" to
96 c16dc9e7 Adam Menz
                outState.putString("path", currentPath);
97 71e52c4a Adam Menz
98 51938302 Adam Menz
                outState.putBoolean("loadingFiles", loadingFiles);
99 3d6041e8 Phillip Toohill
        }
100 a7d1fdc2 Adam Menz
101 b2a2d2f1 Adam Menz
        protected void restoreState(Bundle state) {
102 b2a2d2f1 Adam Menz
                super.restoreState(state);
103 71e52c4a Adam Menz
104 a7d1fdc2 Adam Menz
                /*
105 802fbd9e koutsoub
                 * need reference to the app so you can access curDirFiles as well as
106 802fbd9e koutsoub
                 * processing status
107 a7d1fdc2 Adam Menz
                 */
108 802fbd9e koutsoub
                app = (AndroidCloudApplication) this.getApplication();
109 a7d1fdc2 Adam Menz
110 802fbd9e koutsoub
                if (state != null) {
111 802fbd9e koutsoub
                        if (state.containsKey("path")) {
112 d3ea294b Adam Menz
                                currentPath = state.getString("path");
113 802fbd9e koutsoub
                        } else {
114 d3ea294b Adam Menz
                                currentPath = "";
115 d3ea294b Adam Menz
                        }
116 a7d1fdc2 Adam Menz
117 802fbd9e koutsoub
                        if (state.containsKey("loadingFiles")
118 802fbd9e koutsoub
                                        && state.getBoolean("loadingFiles")) {
119 51938302 Adam Menz
                                loadFiles();
120 802fbd9e koutsoub
                        } else if (state.containsKey("container")) {
121 71e52c4a Adam Menz
                                files = (ContainerObjects[]) state.getSerializable("container");
122 71e52c4a Adam Menz
                                if (app.getCurFiles() == null || app.getCurFiles().size() == 0) {
123 71e52c4a Adam Menz
                                        displayNoFilesCell();
124 71e52c4a Adam Menz
                                } else {
125 71e52c4a Adam Menz
                                        getListView().setDividerHeight(1); // restore divider lines
126 71e52c4a Adam Menz
                                        setListAdapter(new FileAdapter());
127 a7d1fdc2 Adam Menz
128 d3ea294b Adam Menz
                                }
129 71e52c4a Adam Menz
130 240418be Phillip Toohill
                        }
131 802fbd9e koutsoub
                } else {
132 d3ea294b Adam Menz
                        currentPath = "";
133 d3ea294b Adam Menz
                        loadFiles();
134 802fbd9e koutsoub
                }
135 71e52c4a Adam Menz
136 a7d1fdc2 Adam Menz
                /*
137 802fbd9e koutsoub
                 * if the app is process when we enter the activity we must listen for
138 802fbd9e koutsoub
                 * the new curDirFiles list
139 a7d1fdc2 Adam Menz
                 */
140 802fbd9e koutsoub
                if (app.isAddingObject()) {
141 a7d1fdc2 Adam Menz
                        task = new AddObjectListenerTask();
142 a7d1fdc2 Adam Menz
                        task.execute();
143 a7d1fdc2 Adam Menz
                }
144 71e52c4a Adam Menz
145 802fbd9e koutsoub
                if (app.isDeletingObject()) {
146 a7d1fdc2 Adam Menz
                        displayNoFilesCell();
147 a7d1fdc2 Adam Menz
                        deleteObjTask = new DeleteObjectListenerTask();
148 a7d1fdc2 Adam Menz
                        deleteObjTask.execute();
149 a7d1fdc2 Adam Menz
                }
150 71e52c4a Adam Menz
151 802fbd9e koutsoub
                if (app.isDeletingContainer()) {
152 877f6f58 Adam Menz
                        displayNoFilesCell();
153 877f6f58 Adam Menz
                        deleteContainerTask = new DeleteContainerListenerTask();
154 877f6f58 Adam Menz
                        deleteContainerTask.execute();
155 877f6f58 Adam Menz
                }
156 a7d1fdc2 Adam Menz
157 240418be Phillip Toohill
        }
158 a7d1fdc2 Adam Menz
159 a7d1fdc2 Adam Menz
        @Override
160 802fbd9e koutsoub
        protected void onStop() {
161 a7d1fdc2 Adam Menz
                super.onStop();
162 a7d1fdc2 Adam Menz
163 a7d1fdc2 Adam Menz
                /*
164 802fbd9e koutsoub
                 * Need to stop running listener task if we exit
165 a7d1fdc2 Adam Menz
                 */
166 802fbd9e koutsoub
                if (task != null) {
167 a7d1fdc2 Adam Menz
                        task.cancel(true);
168 a7d1fdc2 Adam Menz
                }
169 71e52c4a Adam Menz
170 802fbd9e koutsoub
                if (deleteObjTask != null) {
171 3e180b04 Adam Menz
                        deleteObjTask.cancel(true);
172 3e180b04 Adam Menz
                }
173 71e52c4a Adam Menz
174 802fbd9e koutsoub
                if (deleteContainerTask != null) {
175 3e180b04 Adam Menz
                        deleteContainerTask.cancel(true);
176 3e180b04 Adam Menz
                }
177 3e180b04 Adam Menz
178 240418be Phillip Toohill
        }
179 240418be Phillip Toohill
180 37a14877 Adam Menz
        /*
181 37a14877 Adam Menz
         * overriding back button press, because we are not actually changing
182 d3ea294b Adam Menz
         * activities when we navigate the file structure
183 37a14877 Adam Menz
         */
184 37a14877 Adam Menz
        public void onBackPressed() {
185 cabcecb2 koutsoub
                
186 802fbd9e koutsoub
                if (currentPath.equals("")) {
187 cabcecb2 koutsoub
                        if(container.getName().equals(Container.MYSHARED)){}
188 cabcecb2 koutsoub
                        else
189 cabcecb2 koutsoub
                                finish();
190 802fbd9e koutsoub
                } else {
191 37a14877 Adam Menz
                        goUpDirectory();
192 240418be Phillip Toohill
                }
193 240418be Phillip Toohill
        }
194 a7d1fdc2 Adam Menz
195 37a14877 Adam Menz
        /*
196 37a14877 Adam Menz
         * go to the current directory's parent and display that data
197 37a14877 Adam Menz
         */
198 802fbd9e koutsoub
        private void goUpDirectory() {
199 802fbd9e koutsoub
                currentPath = currentPath.substring(
200 802fbd9e koutsoub
                                0,
201 802fbd9e koutsoub
                                currentPath.substring(0, currentPath.length() - 2).lastIndexOf(
202 802fbd9e koutsoub
                                                "/") + 1);
203 a7d1fdc2 Adam Menz
                loadCurrentDirectoryFiles();
204 37a14877 Adam Menz
                displayCurrentFiles();
205 37a14877 Adam Menz
        }
206 240418be Phillip Toohill
207 cc71758a Adam Menz
        /*
208 51938302 Adam Menz
         * load all file that are in the container
209 51938302 Adam Menz
         */
210 240418be Phillip Toohill
        private void loadFiles() {
211 802fbd9e koutsoub
                // displayLoadingCell();
212 37a14877 Adam Menz
                new LoadFilesTask().execute();
213 37a14877 Adam Menz
        }
214 a7d1fdc2 Adam Menz
215 802fbd9e koutsoub
        /*
216 802fbd9e koutsoub
         * load only the files that should display for the current directory in the
217 802fbd9e koutsoub
         * curDirFiles[]
218 37a14877 Adam Menz
         */
219 802fbd9e koutsoub
        protected void loadCurrentDirectoryFiles() {
220 37a14877 Adam Menz
                ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>();
221 802fbd9e koutsoub
                Log.i(LOG, "loading files");
222 802fbd9e koutsoub
                if (files != null) {
223 802fbd9e koutsoub
                        for (int i = 0; i < files.length; i++) {
224 802fbd9e koutsoub
                                Log.i(LOG, "loading files" + i);
225 802fbd9e koutsoub
                                if (fileBelongsInDir(files[i])) {
226 37a14877 Adam Menz
                                        curFiles.add(files[i]);
227 37a14877 Adam Menz
                                }
228 37a14877 Adam Menz
                        }
229 a7d1fdc2 Adam Menz
                        app.setCurFiles(curFiles);
230 37a14877 Adam Menz
                }
231 37a14877 Adam Menz
        }
232 a7d1fdc2 Adam Menz
233 37a14877 Adam Menz
        /*
234 802fbd9e koutsoub
         * determines if a file should be displayed in current directory
235 37a14877 Adam Menz
         */
236 802fbd9e koutsoub
        protected Boolean fileBelongsInDir(ContainerObjects obj) {
237 37a14877 Adam Menz
                String objPath = obj.getCName();
238 802fbd9e koutsoub
239 802fbd9e koutsoub
                if (!objPath.startsWith(currentPath)) {
240 802fbd9e koutsoub
                        Log.i(LOG, "Path is:" + currentPath + " " + objPath + " " + false);
241 37a14877 Adam Menz
                        return false;
242 802fbd9e koutsoub
                } else {
243 37a14877 Adam Menz
                        objPath = objPath.substring(currentPath.length());
244 802fbd9e koutsoub
                        Log.i(LOG, "Path is:" + currentPath + " " + objPath + " "
245 802fbd9e koutsoub
                                        + !objPath.contains("/"));
246 37a14877 Adam Menz
                        return !objPath.contains("/");
247 37a14877 Adam Menz
                }
248 240418be Phillip Toohill
        }
249 240418be Phillip Toohill
250 37a14877 Adam Menz
        /*
251 802fbd9e koutsoub
         * loads all the files that are in the container into one array
252 37a14877 Adam Menz
         */
253 3d6041e8 Phillip Toohill
        private void setFileList(ArrayList<ContainerObjects> files) {
254 240418be Phillip Toohill
                if (files == null) {
255 240418be Phillip Toohill
                        files = new ArrayList<ContainerObjects>();
256 240418be Phillip Toohill
                }
257 240418be Phillip Toohill
                String[] fileNames = new String[files.size()];
258 240418be Phillip Toohill
                this.files = new ContainerObjects[files.size()];
259 240418be Phillip Toohill
260 240418be Phillip Toohill
                if (files != null) {
261 3d6041e8 Phillip Toohill
                        for (int i = 0; i < files.size(); i++) {
262 240418be Phillip Toohill
                                ContainerObjects file = files.get(i);
263 802fbd9e koutsoub
                                // Log.i(file.getCName(),file.getCName());
264 3d6041e8 Phillip Toohill
                                this.files[i] = file;
265 3d6041e8 Phillip Toohill
                                fileNames[i] = file.getName();
266 3d6041e8 Phillip Toohill
                        }
267 3d6041e8 Phillip Toohill
                }
268 37a14877 Adam Menz
        }
269 a7d1fdc2 Adam Menz
270 802fbd9e koutsoub
        protected void displayCurrentFiles() {
271 a7d1fdc2 Adam Menz
                if (app.getCurFiles().size() == 0) {
272 a7d1fdc2 Adam Menz
                        displayNoFilesCell();
273 3d6041e8 Phillip Toohill
                } else {
274 3ea9d68e Adam Menz
                        ArrayList<ContainerObjects> tempList = new ArrayList<ContainerObjects>();
275 802fbd9e koutsoub
                        for (int i = 0; i < app.getCurFiles().size(); i++) {
276 a7d1fdc2 Adam Menz
                                tempList.add(app.getCurFiles().get(i));
277 3ea9d68e Adam Menz
                        }
278 a7d1fdc2 Adam Menz
                        getListView().setDividerHeight(1); // restore divider lines
279 a7d1fdc2 Adam Menz
                        setListAdapter(new FileAdapter());
280 3d6041e8 Phillip Toohill
                }
281 240418be Phillip Toohill
        }
282 240418be Phillip Toohill
283 37a14877 Adam Menz
        /*
284 802fbd9e koutsoub
         * display a different empty page depending of if you are at top of
285 802fbd9e koutsoub
         * container or in a folder
286 37a14877 Adam Menz
         */
287 856ad13d koutsoub
        protected void displayNoFilesCell() {
288 240418be Phillip Toohill
                String a[] = new String[1];
289 802fbd9e koutsoub
                if (currentPath.equals("")) {
290 37a14877 Adam Menz
                        a[0] = "Empty Container";
291 802fbd9e koutsoub
                        setListAdapter(new ArrayAdapter<String>(this,
292 802fbd9e koutsoub
                                        R.layout.noobjectscell, R.id.no_files_label, a));
293 802fbd9e koutsoub
                } else {
294 37a14877 Adam Menz
                        a[0] = "No Files";
295 37a14877 Adam Menz
                        setListAdapter(new ArrayAdapter<String>(this, R.layout.nofilescell,
296 71e52c4a Adam Menz
                                        R.id.no_files_label, a));
297 37a14877 Adam Menz
                }
298 240418be Phillip Toohill
                getListView().setTextFilterEnabled(true);
299 240418be Phillip Toohill
                getListView().setDividerHeight(0); // hide the dividers so it won't look
300 71e52c4a Adam Menz
                // like a list row
301 240418be Phillip Toohill
                getListView().setItemsCanFocus(false);
302 240418be Phillip Toohill
        }
303 a7d1fdc2 Adam Menz
304 802fbd9e koutsoub
        /*
305 802fbd9e koutsoub
         * just get the last part of the filename so the entire file path does not
306 802fbd9e koutsoub
         * show
307 a7d1fdc2 Adam Menz
         */
308 802fbd9e koutsoub
        private String getShortName(String longName) {
309 c16dc9e7 Adam Menz
                String s = longName;
310 802fbd9e koutsoub
                if (!s.contains("/")) {
311 c16dc9e7 Adam Menz
                        return s;
312 802fbd9e koutsoub
                } else {
313 802fbd9e koutsoub
                        String tmp = s.substring(s.lastIndexOf('/') + 1);
314 802fbd9e koutsoub
                        if (tmp.equals("")) {
315 07c153b1 koutsoub
                                return s;
316 07c153b1 koutsoub
                        }
317 802fbd9e koutsoub
                        return tmp;
318 3d6041e8 Phillip Toohill
                }
319 240418be Phillip Toohill
        }
320 a7d1fdc2 Adam Menz
321 d3ea294b Adam Menz
        /*
322 802fbd9e koutsoub
         * removed a specified object from the array of all the files in the
323 802fbd9e koutsoub
         * container
324 d3ea294b Adam Menz
         */
325 802fbd9e koutsoub
        private void removeFromList(String path) {
326 802fbd9e koutsoub
                ArrayList<ContainerObjects> temp = new ArrayList<ContainerObjects>(
327 802fbd9e koutsoub
                                Arrays.asList(files));
328 802fbd9e koutsoub
                for (int i = 0; i < files.length; i++) {
329 802fbd9e koutsoub
                        if (files[i].getCName()
330 802fbd9e koutsoub
                                        .equals(path.substring(0, path.length() - 1))) {
331 d3ea294b Adam Menz
                                temp.remove(i);
332 d3ea294b Adam Menz
                        }
333 3d6041e8 Phillip Toohill
                }
334 d3ea294b Adam Menz
                files = new ContainerObjects[temp.size()];
335 802fbd9e koutsoub
                for (int i = 0; i < temp.size(); i++) {
336 d3ea294b Adam Menz
                        files[i] = temp.get(i);
337 d3ea294b Adam Menz
                }
338 d3ea294b Adam Menz
        }
339 240418be Phillip Toohill
340 d3ea294b Adam Menz
        protected void onListItemClick(ListView l, View v, int position, long id) {
341 a7d1fdc2 Adam Menz
                if (app.getCurFiles() != null && app.getCurFiles().size() > 0) {
342 d3ea294b Adam Menz
                        Intent viewIntent;
343 802fbd9e koutsoub
                        if (app.getCurFiles().get(position).getContentType()
344 802fbd9e koutsoub
                                        .startsWith("application/directory")) {
345 a7d1fdc2 Adam Menz
                                currentPath = app.getCurFiles().get(position).getCName() + "/";
346 a7d1fdc2 Adam Menz
                                loadCurrentDirectoryFiles();
347 d3ea294b Adam Menz
                                displayCurrentFiles();
348 d3ea294b Adam Menz
                        }
349 a7d1fdc2 Adam Menz
350 802fbd9e koutsoub
                        else {
351 d3ea294b Adam Menz
                                viewIntent = new Intent(this, ContainerObjectDetails.class);
352 802fbd9e koutsoub
                                viewIntent.putExtra("container", app.getCurFiles()
353 802fbd9e koutsoub
                                                .get(position));
354 d3ea294b Adam Menz
                                viewIntent.putExtra("cdnUrl", container.getCdnUrl());
355 d3ea294b Adam Menz
                                viewIntent.putExtra("containerNames", container.getName());
356 d3ea294b Adam Menz
                                viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
357 802fbd9e koutsoub
                                startActivityForResult(viewIntent, 55); // arbitrary number;
358 802fbd9e koutsoub
                                                                                                                // never
359 d3ea294b Adam Menz
                                // used again
360 6864568a Phillip Toohill
                        }
361 3d6041e8 Phillip Toohill
                }
362 240418be Phillip Toohill
        }
363 240418be Phillip Toohill
364 240418be Phillip Toohill
        @Override
365 240418be Phillip Toohill
        public boolean onCreateOptionsMenu(Menu menu) {
366 3d6041e8 Phillip Toohill
                super.onCreateOptionsMenu(menu);
367 3d6041e8 Phillip Toohill
                MenuInflater inflater = getMenuInflater();
368 3d6041e8 Phillip Toohill
                inflater.inflate(R.menu.view_container_object_list_menu, menu);
369 3d6041e8 Phillip Toohill
                return true;
370 240418be Phillip Toohill
        }
371 240418be Phillip Toohill
372 240418be Phillip Toohill
        @Override
373 37a14877 Adam Menz
        /*
374 802fbd9e koutsoub
         * option performed for delete depends on if you are at the top of a
375 802fbd9e koutsoub
         * container or in a folder
376 37a14877 Adam Menz
         */
377 240418be Phillip Toohill
        public boolean onOptionsItemSelected(MenuItem item) {
378 3d6041e8 Phillip Toohill
                switch (item.getItemId()) {
379 3d6041e8 Phillip Toohill
                case R.id.delete_container:
380 802fbd9e koutsoub
                        if (currentPath.equals("")) {
381 37a14877 Adam Menz
                                showDialog(deleteContainer);
382 802fbd9e koutsoub
                        } else {
383 37a14877 Adam Menz
                                showDialog(deleteFolder);
384 37a14877 Adam Menz
                        }
385 3d6041e8 Phillip Toohill
                        return true;
386 3d6041e8 Phillip Toohill
                case R.id.enable_cdn:
387 3d6041e8 Phillip Toohill
                        Intent viewIntent1 = new Intent(this, EnableCDNActivity.class);
388 240418be Phillip Toohill
                        viewIntent1.putExtra("Cname", container.getName());
389 240418be Phillip Toohill
                        startActivityForResult(viewIntent1, 56);
390 3d6041e8 Phillip Toohill
                        return true;
391 3d6041e8 Phillip Toohill
                case R.id.refresh:
392 3d6041e8 Phillip Toohill
                        loadFiles();
393 240418be Phillip Toohill
                        return true;
394 d3ea294b Adam Menz
                case R.id.add_folder:
395 d3ea294b Adam Menz
                        showDialog(R.id.add_folder);
396 44ac7bea Adam Menz
                        return true;
397 d3ea294b Adam Menz
                case R.id.add_file:
398 802fbd9e koutsoub
                        /*
399 802fbd9e koutsoub
                         * Intent viewIntent2 = new Intent(this, AddFileActivity.class);
400 802fbd9e koutsoub
                         * viewIntent2.putExtra("Cname", container.getName());
401 802fbd9e koutsoub
                         * viewIntent2.putExtra("curPath", currentPath);
402 802fbd9e koutsoub
                         * startActivityForResult(viewIntent2, 56);
403 802fbd9e koutsoub
                         */
404 802fbd9e koutsoub
405 802fbd9e koutsoub
                        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
406 802fbd9e koutsoub
                        intent.addCategory(Intent.CATEGORY_OPENABLE);
407 802fbd9e koutsoub
                        intent.setData(Uri.parse("file://"));
408 802fbd9e koutsoub
                        intent.setType("*/*");
409 802fbd9e koutsoub
                        try {
410 802fbd9e koutsoub
                                int i = 1;
411 802fbd9e koutsoub
                                startActivityForResult(intent, i);
412 802fbd9e koutsoub
413 802fbd9e koutsoub
                        } catch (ActivityNotFoundException e) {
414 802fbd9e koutsoub
                                Toast.makeText(this, "No File Manager", Toast.LENGTH_SHORT)
415 802fbd9e koutsoub
                                                .show();
416 802fbd9e koutsoub
                        }
417 802fbd9e koutsoub
418 802fbd9e koutsoub
                        return true;
419 3d6041e8 Phillip Toohill
                }
420 3d6041e8 Phillip Toohill
                return false;
421 240418be Phillip Toohill
        }
422 240418be Phillip Toohill
423 3d6041e8 Phillip Toohill
        @Override
424 240418be Phillip Toohill
        protected Dialog onCreateDialog(int id) {
425 240418be Phillip Toohill
                switch (id) {
426 240418be Phillip Toohill
                case deleteContainer:
427 802fbd9e koutsoub
                        if (app.getCurFiles().size() == 0) {
428 37a14877 Adam Menz
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
429 802fbd9e koutsoub
                                                .setIcon(R.drawable.alert_dialog_icon)
430 802fbd9e koutsoub
                                                .setTitle("Delete Container")
431 802fbd9e koutsoub
                                                .setMessage(
432 802fbd9e koutsoub
                                                                "Are you sure you want to delete this Container?")
433 802fbd9e koutsoub
                                                .setPositiveButton("Delete Container",
434 802fbd9e koutsoub
                                                                new DialogInterface.OnClickListener() {
435 802fbd9e koutsoub
                                                                        public void onClick(DialogInterface dialog,
436 802fbd9e koutsoub
                                                                                        int whichButton) {
437 802fbd9e koutsoub
                                                                                // User clicked OK so do some stuff
438 802fbd9e koutsoub
                                                                                trackEvent(
439 802fbd9e koutsoub
                                                                                                GoogleAnalytics.CATEGORY_CONTAINER,
440 802fbd9e koutsoub
                                                                                                GoogleAnalytics.EVENT_DELETE,
441 802fbd9e koutsoub
                                                                                                "", -1);
442 802fbd9e koutsoub
                                                                                new DeleteContainerTask()
443 802fbd9e koutsoub
                                                                                                .execute(currentPath);
444 802fbd9e koutsoub
                                                                        }
445 802fbd9e koutsoub
                                                                })
446 802fbd9e koutsoub
                                                .setNegativeButton("Cancel",
447 802fbd9e koutsoub
                                                                new DialogInterface.OnClickListener() {
448 802fbd9e koutsoub
                                                                        public void onClick(DialogInterface dialog,
449 802fbd9e koutsoub
                                                                                        int whichButton) {
450 802fbd9e koutsoub
                                                                                // User clicked Cancel so do some stuff
451 802fbd9e koutsoub
                                                                        }
452 802fbd9e koutsoub
                                                                }).create();
453 802fbd9e koutsoub
                        } else {
454 37a14877 Adam Menz
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
455 802fbd9e koutsoub
                                                .setIcon(R.drawable.alert_dialog_icon)
456 802fbd9e koutsoub
                                                .setTitle("Delete Container")
457 802fbd9e koutsoub
                                                .setMessage("Container must be empty to delete")
458 802fbd9e koutsoub
                                                .setNegativeButton("OK",
459 802fbd9e koutsoub
                                                                new DialogInterface.OnClickListener() {
460 802fbd9e koutsoub
                                                                        public void onClick(DialogInterface dialog,
461 802fbd9e koutsoub
                                                                                        int whichButton) {
462 802fbd9e koutsoub
                                                                                // User clicked Cancel so do some stuff
463 802fbd9e koutsoub
                                                                        }
464 802fbd9e koutsoub
                                                                }).create();
465 37a14877 Adam Menz
                        }
466 37a14877 Adam Menz
                case deleteFolder:
467 802fbd9e koutsoub
                        if (app.getCurFiles().size() == 0) {
468 37a14877 Adam Menz
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
469 802fbd9e koutsoub
                                                .setIcon(R.drawable.alert_dialog_icon)
470 802fbd9e koutsoub
                                                .setTitle("Delete Folder")
471 802fbd9e koutsoub
                                                .setMessage(
472 802fbd9e koutsoub
                                                                "Are you sure you want to delete this Folder?")
473 802fbd9e koutsoub
                                                .setPositiveButton("Delete Folder",
474 802fbd9e koutsoub
                                                                new DialogInterface.OnClickListener() {
475 802fbd9e koutsoub
                                                                        public void onClick(DialogInterface dialog,
476 802fbd9e koutsoub
                                                                                        int whichButton) {
477 802fbd9e koutsoub
                                                                                // User clicked OK so do some stuff
478 802fbd9e koutsoub
                                                                                new DeleteObjectTask().execute();
479 802fbd9e koutsoub
                                                                        }
480 802fbd9e koutsoub
                                                                })
481 802fbd9e koutsoub
                                                .setNegativeButton("Cancel",
482 802fbd9e koutsoub
                                                                new DialogInterface.OnClickListener() {
483 802fbd9e koutsoub
                                                                        public void onClick(DialogInterface dialog,
484 802fbd9e koutsoub
                                                                                        int whichButton) {
485 802fbd9e koutsoub
                                                                                // User clicked Cancel so do some stuff
486 802fbd9e koutsoub
                                                                        }
487 802fbd9e koutsoub
                                                                }).create();
488 802fbd9e koutsoub
                        } else {
489 37a14877 Adam Menz
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
490 802fbd9e koutsoub
                                                .setIcon(R.drawable.alert_dialog_icon)
491 802fbd9e koutsoub
                                                .setTitle("Delete Folder")
492 802fbd9e koutsoub
                                                .setMessage("Folder must be empty to delete")
493 802fbd9e koutsoub
                                                .setNegativeButton("OK",
494 802fbd9e koutsoub
                                                                new DialogInterface.OnClickListener() {
495 802fbd9e koutsoub
                                                                        public void onClick(DialogInterface dialog,
496 802fbd9e koutsoub
                                                                                        int whichButton) {
497 802fbd9e koutsoub
                                                                                // User clicked Cancel so do some stuff
498 802fbd9e koutsoub
                                                                        }
499 802fbd9e koutsoub
                                                                }).create();
500 37a14877 Adam Menz
                        }
501 d3ea294b Adam Menz
                case R.id.add_folder:
502 d3ea294b Adam Menz
                        final EditText input = new EditText(this);
503 a7d1fdc2 Adam Menz
                        return new AlertDialog.Builder(ContainerObjectsActivity.this)
504 802fbd9e koutsoub
                                        .setIcon(R.drawable.alert_dialog_icon)
505 802fbd9e koutsoub
                                        .setView(input)
506 802fbd9e koutsoub
                                        .setTitle("Add Folder")
507 802fbd9e koutsoub
                                        .setMessage("Enter new name for folder: ")
508 802fbd9e koutsoub
                                        .setPositiveButton("Add",
509 802fbd9e koutsoub
                                                        new DialogInterface.OnClickListener() {
510 802fbd9e koutsoub
                                                                public void onClick(DialogInterface dialog,
511 802fbd9e koutsoub
                                                                                int whichButton) {
512 802fbd9e koutsoub
                                                                        // User clicked OK so do some stuff
513 802fbd9e koutsoub
                                                                        String[] info = {
514 802fbd9e koutsoub
                                                                                        input.getText().toString(),
515 802fbd9e koutsoub
                                                                                        "application/directory" };
516 802fbd9e koutsoub
                                                                        new AddFolderTask().execute(info);
517 802fbd9e koutsoub
                                                                }
518 802fbd9e koutsoub
                                                        })
519 802fbd9e koutsoub
                                        .setNegativeButton("Cancel",
520 802fbd9e koutsoub
                                                        new DialogInterface.OnClickListener() {
521 802fbd9e koutsoub
                                                                public void onClick(DialogInterface dialog,
522 802fbd9e koutsoub
                                                                                int whichButton) {
523 802fbd9e koutsoub
                                                                        // User clicked Cancel so do some stuff
524 802fbd9e koutsoub
                                                                }
525 802fbd9e koutsoub
                                                        }).create();
526 240418be Phillip Toohill
                }
527 240418be Phillip Toohill
                return null;
528 3d6041e8 Phillip Toohill
        }
529 3d6041e8 Phillip Toohill
530 240418be Phillip Toohill
        @Override
531 240418be Phillip Toohill
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
532 240418be Phillip Toohill
                super.onActivityResult(requestCode, resultCode, data);
533 71e52c4a Adam Menz
534 e6c34a0f Adam Menz
                if (resultCode == RESULT_OK && requestCode == 56) {
535 240418be Phillip Toohill
                        // a sub-activity kicked back, so we want to refresh the server list
536 240418be Phillip Toohill
                        loadFiles();
537 240418be Phillip Toohill
                }
538 802fbd9e koutsoub
                
539 802fbd9e koutsoub
                if (resultCode == RESULT_OK && requestCode == 1) {
540 802fbd9e koutsoub
                        String filename = data.getDataString();
541 802fbd9e koutsoub
                        uploadFile(filename, data);
542 802fbd9e koutsoub
                        
543 802fbd9e koutsoub
                }
544 71e52c4a Adam Menz
545 3e180b04 Adam Menz
                // deleted file so need to update the list
546 3e180b04 Adam Menz
                if (requestCode == 55 && resultCode == 99) {
547 3e180b04 Adam Menz
                        loadFiles();
548 240418be Phillip Toohill
                }
549 a7d1fdc2 Adam Menz
550 32731215 Adam Menz
        }
551 a7d1fdc2 Adam Menz
552 d3ea294b Adam Menz
        class FileAdapter extends ArrayAdapter<ContainerObjects> {
553 d3ea294b Adam Menz
                FileAdapter() {
554 d3ea294b Adam Menz
                        super(ContainerObjectsActivity.this,
555 802fbd9e koutsoub
                                        R.layout.listcontainerobjectcell, app.getCurFiles());
556 d3ea294b Adam Menz
                }
557 a7d1fdc2 Adam Menz
558 d3ea294b Adam Menz
                public View getView(int position, View convertView, ViewGroup parent) {
559 a7d1fdc2 Adam Menz
                        ContainerObjects file = app.getCurFiles().get(position);
560 d3ea294b Adam Menz
                        LayoutInflater inflater = getLayoutInflater();
561 d3ea294b Adam Menz
                        View row = inflater.inflate(R.layout.listcontainerobjectcell,
562 d3ea294b Adam Menz
                                        parent, false);
563 a7d1fdc2 Adam Menz
564 d3ea294b Adam Menz
                        TextView label = (TextView) row.findViewById(R.id.label);
565 d3ea294b Adam Menz
                        label.setText(getShortName(file.getCName()));
566 a7d1fdc2 Adam Menz
567 802fbd9e koutsoub
                        ImageView objectImage = (ImageView) row
568 802fbd9e koutsoub
                                        .findViewById(R.id.file_type_image);
569 802fbd9e koutsoub
                        if (file.getContentType().startsWith("application/directory")) {
570 f506786e Adam Menz
                                objectImage.setImageResource(R.drawable.folder);
571 f506786e Adam Menz
                        } else {
572 f506786e Adam Menz
                                objectImage.setImageResource(R.drawable.file);
573 f506786e Adam Menz
                        }
574 802fbd9e koutsoub
575 d3ea294b Adam Menz
                        if (file.getBytes() >= bConver) {
576 d3ea294b Adam Menz
                                megaBytes = Math.abs(file.getBytes() / bConver + 0.2);
577 d3ea294b Adam Menz
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
578 d3ea294b Adam Menz
                                sublabel.setText(megaBytes + " MB");
579 d3ea294b Adam Menz
                        } else if (file.getBytes() >= kbConver) {
580 d3ea294b Adam Menz
                                kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2);
581 d3ea294b Adam Menz
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
582 d3ea294b Adam Menz
                                sublabel.setText(kiloBytes + " KB");
583 d3ea294b Adam Menz
                        } else {
584 d3ea294b Adam Menz
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
585 d3ea294b Adam Menz
                                sublabel.setText(file.getBytes() + " B");
586 d3ea294b Adam Menz
                        }
587 a7d1fdc2 Adam Menz
588 d3ea294b Adam Menz
                        return (row);
589 d3ea294b Adam Menz
                }
590 d3ea294b Adam Menz
        }
591 71e52c4a Adam Menz
592 d3ea294b Adam Menz
        private class LoadFilesTask extends
593 802fbd9e koutsoub
                        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
594 240418be Phillip Toohill
595 3d6041e8 Phillip Toohill
                private CloudServersException exception;
596 71e52c4a Adam Menz
597 802fbd9e koutsoub
                protected void onPreExecute() {
598 a7d1fdc2 Adam Menz
                        showDialog();
599 b2a2d2f1 Adam Menz
                        loadingFiles = true;
600 d3ea294b Adam Menz
                }
601 a7d1fdc2 Adam Menz
602 d3ea294b Adam Menz
                @Override
603 d3ea294b Adam Menz
                protected ArrayList<ContainerObjects> doInBackground(String... path) {
604 d3ea294b Adam Menz
                        ArrayList<ContainerObjects> files = null;
605 d3ea294b Adam Menz
                        try {
606 802fbd9e koutsoub
                                if (container.getName().equals(Container.MYSHARED)) {
607 802fbd9e koutsoub
                                        files = (new ContainerObjectManager(getContext()))
608 802fbd9e koutsoub
                                                        .createListMyShared(true, container.getName(),
609 802fbd9e koutsoub
                                                                        new ContainerManager(getContext())
610 802fbd9e koutsoub
                                                                                        .createList(true));
611 802fbd9e koutsoub
                                } else if (container.getName().equals(Container.OTHERS)) {
612 802fbd9e koutsoub
613 802fbd9e koutsoub
                                } else {
614 802fbd9e koutsoub
                                        if (container.getOtherUser() == null)
615 802fbd9e koutsoub
                                                files = (new ContainerObjectManager(getContext()))
616 802fbd9e koutsoub
                                                                .createList(true, container.getName());
617 856ad13d koutsoub
                                        else
618 802fbd9e koutsoub
                                                files = (new ContainerObjectManager(getContext()))
619 802fbd9e koutsoub
                                                                .createOtherList(true, container.getName(),
620 802fbd9e koutsoub
                                                                                container.getOtherUser());
621 856ad13d koutsoub
                                }
622 d3ea294b Adam Menz
                        } catch (CloudServersException e) {
623 d3ea294b Adam Menz
                                exception = e;
624 d3ea294b Adam Menz
                                e.printStackTrace();
625 d3ea294b Adam Menz
                        }
626 d3ea294b Adam Menz
                        return files;
627 d3ea294b Adam Menz
                }
628 a7d1fdc2 Adam Menz
629 d3ea294b Adam Menz
                @Override
630 d3ea294b Adam Menz
                protected void onPostExecute(ArrayList<ContainerObjects> result) {
631 b2a2d2f1 Adam Menz
                        loadingFiles = false;
632 a7d1fdc2 Adam Menz
                        hideDialog();
633 d3ea294b Adam Menz
                        if (exception != null) {
634 d3ea294b Adam Menz
                                showAlert("Error", exception.getMessage());
635 d3ea294b Adam Menz
                        }
636 d3ea294b Adam Menz
                        setFileList(result);
637 3ea9d68e Adam Menz
                        loadCurrentDirectoryFiles();
638 3ea9d68e Adam Menz
                        displayCurrentFiles();
639 d3ea294b Adam Menz
                }
640 51938302 Adam Menz
641 d3ea294b Adam Menz
        }
642 d3ea294b Adam Menz
643 802fbd9e koutsoub
        private class AddFolderTask extends AsyncTask<String, Void, HttpBundle> {
644 802fbd9e koutsoub
645 802fbd9e koutsoub
                private CloudServersException exception;
646 802fbd9e koutsoub
647 802fbd9e koutsoub
                @Override
648 802fbd9e koutsoub
                protected void onPreExecute() {
649 802fbd9e koutsoub
                        showDialog();
650 802fbd9e koutsoub
                        app.setAddingObject(true);
651 802fbd9e koutsoub
                        task = new AddObjectListenerTask();
652 802fbd9e koutsoub
                        task.execute();
653 802fbd9e koutsoub
                }
654 802fbd9e koutsoub
655 802fbd9e koutsoub
                @Override
656 802fbd9e koutsoub
                protected HttpBundle doInBackground(String... data) {
657 802fbd9e koutsoub
                        HttpBundle bundle = null;
658 802fbd9e koutsoub
                        try {
659 802fbd9e koutsoub
660 802fbd9e koutsoub
                                bundle = (new ContainerObjectManager(getContext())).addObject(
661 802fbd9e koutsoub
                                                container.getName(), currentPath, data[0], data[1]);
662 802fbd9e koutsoub
                        } catch (CloudServersException e) {
663 802fbd9e koutsoub
                                exception = e;
664 802fbd9e koutsoub
                        }
665 802fbd9e koutsoub
                        return bundle;
666 802fbd9e koutsoub
                }
667 802fbd9e koutsoub
668 802fbd9e koutsoub
                @Override
669 802fbd9e koutsoub
                protected void onPostExecute(HttpBundle bundle) {
670 802fbd9e koutsoub
                        app.setAddingObject(false);
671 802fbd9e koutsoub
                        hideDialog();
672 802fbd9e koutsoub
                        HttpResponse response = bundle.getResponse();
673 802fbd9e koutsoub
                        if (response != null) {
674 802fbd9e koutsoub
                                int statusCode = response.getStatusLine().getStatusCode();
675 802fbd9e koutsoub
                                if (statusCode == 201) {
676 802fbd9e koutsoub
                                        setResult(Activity.RESULT_OK);
677 802fbd9e koutsoub
                                        // loading the new files is done by ListenerTask
678 802fbd9e koutsoub
                                } else {
679 802fbd9e koutsoub
                                        CloudServersException cse = parseCloudServersException(response);
680 802fbd9e koutsoub
                                        if ("".equals(cse.getMessage())) {
681 802fbd9e koutsoub
                                                showError("There was a problem deleting your folder.",
682 802fbd9e koutsoub
                                                                bundle);
683 802fbd9e koutsoub
                                        } else {
684 802fbd9e koutsoub
                                                showError("There was a problem deleting your folder: "
685 802fbd9e koutsoub
                                                                + cse.getMessage(), bundle);
686 802fbd9e koutsoub
                                        }
687 802fbd9e koutsoub
                                }
688 802fbd9e koutsoub
                        } else if (exception != null) {
689 802fbd9e koutsoub
                                showError("There was a problem deleting your folder: "
690 802fbd9e koutsoub
                                                + exception.getMessage(), bundle);
691 802fbd9e koutsoub
                        }
692 802fbd9e koutsoub
                }
693 802fbd9e koutsoub
        }
694 802fbd9e koutsoub
        
695 802fbd9e koutsoub
        private class AddFileTask extends AsyncTask<String, Void, HttpBundle> {
696 3d6041e8 Phillip Toohill
697 3d6041e8 Phillip Toohill
                private CloudServersException exception;
698 a7d1fdc2 Adam Menz
699 a7d1fdc2 Adam Menz
                @Override
700 802fbd9e koutsoub
                protected void onPreExecute() {
701 a7d1fdc2 Adam Menz
                        showDialog();
702 a7d1fdc2 Adam Menz
                        app.setAddingObject(true);
703 a7d1fdc2 Adam Menz
                        task = new AddObjectListenerTask();
704 a7d1fdc2 Adam Menz
                        task.execute();
705 d3ea294b Adam Menz
                }
706 a7d1fdc2 Adam Menz
707 3d6041e8 Phillip Toohill
                @Override
708 a7d1fdc2 Adam Menz
                protected HttpBundle doInBackground(String... data) {
709 32731215 Adam Menz
                        HttpBundle bundle = null;
710 3d6041e8 Phillip Toohill
                        try {
711 71e52c4a Adam Menz
712 802fbd9e koutsoub
                                bundle = (new ContainerObjectManager(getContext())).addFileObject(
713 802fbd9e koutsoub
                                                container.getName(), currentPath, data[0], data[1], data[2]);
714 3d6041e8 Phillip Toohill
                        } catch (CloudServersException e) {
715 3d6041e8 Phillip Toohill
                                exception = e;
716 3d6041e8 Phillip Toohill
                        }
717 32731215 Adam Menz
                        return bundle;
718 3d6041e8 Phillip Toohill
                }
719 240418be Phillip Toohill
720 3d6041e8 Phillip Toohill
                @Override
721 32731215 Adam Menz
                protected void onPostExecute(HttpBundle bundle) {
722 a7d1fdc2 Adam Menz
                        app.setAddingObject(false);
723 b2a2d2f1 Adam Menz
                        hideDialog();
724 32731215 Adam Menz
                        HttpResponse response = bundle.getResponse();
725 3d6041e8 Phillip Toohill
                        if (response != null) {
726 3d6041e8 Phillip Toohill
                                int statusCode = response.getStatusLine().getStatusCode();
727 a7d1fdc2 Adam Menz
                                if (statusCode == 201) {
728 37a14877 Adam Menz
                                        setResult(Activity.RESULT_OK);
729 802fbd9e koutsoub
                                        // loading the new files is done by ListenerTask
730 37a14877 Adam Menz
                                } else {
731 37a14877 Adam Menz
                                        CloudServersException cse = parseCloudServersException(response);
732 37a14877 Adam Menz
                                        if ("".equals(cse.getMessage())) {
733 802fbd9e koutsoub
                                                showError("There was a problem deleting your folder.",
734 802fbd9e koutsoub
                                                                bundle);
735 37a14877 Adam Menz
                                        } else {
736 b2a2d2f1 Adam Menz
                                                showError("There was a problem deleting your folder: "
737 a7d1fdc2 Adam Menz
                                                                + cse.getMessage(), bundle);
738 37a14877 Adam Menz
                                        }
739 37a14877 Adam Menz
                                }
740 37a14877 Adam Menz
                        } else if (exception != null) {
741 b2a2d2f1 Adam Menz
                                showError("There was a problem deleting your folder: "
742 32731215 Adam Menz
                                                + exception.getMessage(), bundle);
743 37a14877 Adam Menz
                        }
744 37a14877 Adam Menz
                }
745 37a14877 Adam Menz
        }
746 a7d1fdc2 Adam Menz
747 802fbd9e koutsoub
        private class DeleteObjectTask extends AsyncTask<Void, Void, HttpBundle> {
748 71e52c4a Adam Menz
749 d3ea294b Adam Menz
                private CloudServersException exception;
750 71e52c4a Adam Menz
751 d3ea294b Adam Menz
                @Override
752 802fbd9e koutsoub
                protected void onPreExecute() {
753 a7d1fdc2 Adam Menz
                        showDialog();
754 a7d1fdc2 Adam Menz
                        app.setDeleteingObject(true);
755 a7d1fdc2 Adam Menz
                        deleteObjTask = new DeleteObjectListenerTask();
756 a7d1fdc2 Adam Menz
                        deleteObjTask.execute();
757 a7d1fdc2 Adam Menz
                }
758 71e52c4a Adam Menz
759 a7d1fdc2 Adam Menz
                @Override
760 a7d1fdc2 Adam Menz
                protected HttpBundle doInBackground(Void... arg0) {
761 32731215 Adam Menz
                        HttpBundle bundle = null;
762 d3ea294b Adam Menz
                        try {
763 802fbd9e koutsoub
                                // subtring because the current directory contains a "/" at the
764 802fbd9e koutsoub
                                // end of the string
765 802fbd9e koutsoub
                                bundle = (new ContainerObjectManager(getContext()))
766 802fbd9e koutsoub
                                                .deleteObject(container.getName(), currentPath
767 802fbd9e koutsoub
                                                                .substring(0, currentPath.length() - 1));
768 d3ea294b Adam Menz
                        } catch (CloudServersException e) {
769 d3ea294b Adam Menz
                                exception = e;
770 d3ea294b Adam Menz
                        }
771 32731215 Adam Menz
                        return bundle;
772 d3ea294b Adam Menz
                }
773 71e52c4a Adam Menz
774 d3ea294b Adam Menz
                @Override
775 32731215 Adam Menz
                protected void onPostExecute(HttpBundle bundle) {
776 a7d1fdc2 Adam Menz
                        app.setDeleteingObject(false);
777 b2a2d2f1 Adam Menz
                        hideDialog();
778 32731215 Adam Menz
                        HttpResponse response = bundle.getResponse();
779 d3ea294b Adam Menz
                        if (response != null) {
780 d3ea294b Adam Menz
                                int statusCode = response.getStatusLine().getStatusCode();
781 a7d1fdc2 Adam Menz
                                if (statusCode == 409) {
782 a7d1fdc2 Adam Menz
                                        showAlert("Error",
783 802fbd9e koutsoub
                                                        "Folder must be empty in order to delete");
784 a7d1fdc2 Adam Menz
                                }
785 a7d1fdc2 Adam Menz
                                if (statusCode == 204) {
786 d3ea294b Adam Menz
                                        setResult(Activity.RESULT_OK);
787 3e180b04 Adam Menz
                                } else {
788 d3ea294b Adam Menz
                                        CloudServersException cse = parseCloudServersException(response);
789 d3ea294b Adam Menz
                                        if ("".equals(cse.getMessage())) {
790 802fbd9e koutsoub
                                                showError("There was a problem deleting your folder.",
791 802fbd9e koutsoub
                                                                bundle);
792 d3ea294b Adam Menz
                                        } else {
793 b2a2d2f1 Adam Menz
                                                showError("There was a problem deleting your folder: "
794 a7d1fdc2 Adam Menz
                                                                + cse.getMessage(), bundle);
795 d3ea294b Adam Menz
                                        }
796 d3ea294b Adam Menz
                                }
797 d3ea294b Adam Menz
                        } else if (exception != null) {
798 b2a2d2f1 Adam Menz
                                showError("There was a problem deleting your folder: "
799 32731215 Adam Menz
                                                + exception.getMessage(), bundle);
800 d3ea294b Adam Menz
                        }
801 d3ea294b Adam Menz
                }
802 d3ea294b Adam Menz
        }
803 d3ea294b Adam Menz
804 37a14877 Adam Menz
        private class DeleteContainerTask extends
805 802fbd9e koutsoub
                        AsyncTask<String, Void, HttpBundle> {
806 37a14877 Adam Menz
807 37a14877 Adam Menz
                private CloudServersException exception;
808 37a14877 Adam Menz
809 37a14877 Adam Menz
                @Override
810 802fbd9e koutsoub
                protected void onPreExecute() {
811 a7d1fdc2 Adam Menz
                        showDialog();
812 a7d1fdc2 Adam Menz
                        app.setDeletingContainer(true);
813 a7d1fdc2 Adam Menz
                        deleteContainerTask = new DeleteContainerListenerTask();
814 a7d1fdc2 Adam Menz
                        deleteContainerTask.execute();
815 a7d1fdc2 Adam Menz
                }
816 71e52c4a Adam Menz
817 a7d1fdc2 Adam Menz
                @Override
818 32731215 Adam Menz
                protected HttpBundle doInBackground(String... object) {
819 32731215 Adam Menz
                        HttpBundle bundle = null;
820 37a14877 Adam Menz
                        try {
821 802fbd9e koutsoub
                                bundle = (new ContainerManager(getContext())).delete(container
822 802fbd9e koutsoub
                                                .getName());
823 3d6041e8 Phillip Toohill
                        } catch (CloudServersException e) {
824 3d6041e8 Phillip Toohill
                                exception = e;
825 3d6041e8 Phillip Toohill
                        }
826 32731215 Adam Menz
                        return bundle;
827 3d6041e8 Phillip Toohill
                }
828 240418be Phillip Toohill
829 3d6041e8 Phillip Toohill
                @Override
830 32731215 Adam Menz
                protected void onPostExecute(HttpBundle bundle) {
831 a7d1fdc2 Adam Menz
                        hideDialog();
832 a7d1fdc2 Adam Menz
                        app.setDeletingContainer(false);
833 32731215 Adam Menz
                        HttpResponse response = bundle.getResponse();
834 3d6041e8 Phillip Toohill
                        if (response != null) {
835 3d6041e8 Phillip Toohill
                                int statusCode = response.getStatusLine().getStatusCode();
836 3d6041e8 Phillip Toohill
                                if (statusCode == 409) {
837 802fbd9e koutsoub
                                        showError("Container must be empty in order to delete",
838 802fbd9e koutsoub
                                                        bundle);
839 3d6041e8 Phillip Toohill
                                }
840 3d6041e8 Phillip Toohill
                                if (statusCode == 204) {
841 3d6041e8 Phillip Toohill
                                        setResult(Activity.RESULT_OK);
842 240418be Phillip Toohill
843 3d6041e8 Phillip Toohill
                                } else {
844 3d6041e8 Phillip Toohill
                                        CloudServersException cse = parseCloudServersException(response);
845 3d6041e8 Phillip Toohill
                                        if ("".equals(cse.getMessage())) {
846 802fbd9e koutsoub
                                                showError(
847 802fbd9e koutsoub
                                                                "There was a problem deleting your container.",
848 802fbd9e koutsoub
                                                                bundle);
849 3d6041e8 Phillip Toohill
                                        } else {
850 802fbd9e koutsoub
                                                showError(
851 802fbd9e koutsoub
                                                                "There was a problem deleting your container: "
852 802fbd9e koutsoub
                                                                                + cse.getMessage(), bundle);
853 3d6041e8 Phillip Toohill
                                        }
854 3d6041e8 Phillip Toohill
                                }
855 3d6041e8 Phillip Toohill
                        } else if (exception != null) {
856 b2a2d2f1 Adam Menz
                                showError("There was a problem deleting your server: "
857 32731215 Adam Menz
                                                + exception.getMessage(), bundle);
858 240418be Phillip Toohill
                        }
859 3d6041e8 Phillip Toohill
                }
860 240418be Phillip Toohill
        }
861 3d6041e8 Phillip Toohill
862 a7d1fdc2 Adam Menz
        /*
863 802fbd9e koutsoub
         * listens for the application to change isProcessing listens so activity
864 802fbd9e koutsoub
         * knows when it should display the new curDirFiles
865 a7d1fdc2 Adam Menz
         */
866 802fbd9e koutsoub
        private class AddObjectListenerTask extends AsyncTask<Void, Void, Void> {
867 71e52c4a Adam Menz
868 a7d1fdc2 Adam Menz
                @Override
869 a7d1fdc2 Adam Menz
                protected Void doInBackground(Void... arg1) {
870 a7d1fdc2 Adam Menz
871 802fbd9e koutsoub
                        while (app.isAddingObject()) {
872 a7d1fdc2 Adam Menz
                                // wait for process to finish
873 a7d1fdc2 Adam Menz
                                // or have it be canceled
874 802fbd9e koutsoub
                                if (task.isCancelled()) {
875 a7d1fdc2 Adam Menz
                                        return null;
876 a7d1fdc2 Adam Menz
                                }
877 a7d1fdc2 Adam Menz
                        }
878 a7d1fdc2 Adam Menz
                        return null;
879 a7d1fdc2 Adam Menz
                }
880 a7d1fdc2 Adam Menz
881 a7d1fdc2 Adam Menz
                /*
882 802fbd9e koutsoub
                 * when no longer processing, time to load the new files
883 a7d1fdc2 Adam Menz
                 */
884 a7d1fdc2 Adam Menz
                @Override
885 a7d1fdc2 Adam Menz
                protected void onPostExecute(Void arg1) {
886 b2a2d2f1 Adam Menz
                        hideDialog();
887 a7d1fdc2 Adam Menz
                        loadFiles();
888 a7d1fdc2 Adam Menz
                }
889 a7d1fdc2 Adam Menz
        }
890 71e52c4a Adam Menz
891 802fbd9e koutsoub
        private class DeleteObjectListenerTask extends AsyncTask<Void, Void, Void> {
892 71e52c4a Adam Menz
893 a7d1fdc2 Adam Menz
                @Override
894 a7d1fdc2 Adam Menz
                protected Void doInBackground(Void... arg1) {
895 a7d1fdc2 Adam Menz
896 802fbd9e koutsoub
                        while (app.isDeletingObject()) {
897 a7d1fdc2 Adam Menz
                                // wait for process to finish
898 a7d1fdc2 Adam Menz
                                // or have it be canceled
899 802fbd9e koutsoub
                                if (deleteObjTask.isCancelled()) {
900 a7d1fdc2 Adam Menz
                                        return null;
901 a7d1fdc2 Adam Menz
                                }
902 a7d1fdc2 Adam Menz
                        }
903 a7d1fdc2 Adam Menz
                        return null;
904 a7d1fdc2 Adam Menz
                }
905 a7d1fdc2 Adam Menz
906 a7d1fdc2 Adam Menz
                /*
907 802fbd9e koutsoub
                 * when no longer processing, time to load the new files
908 a7d1fdc2 Adam Menz
                 */
909 a7d1fdc2 Adam Menz
                @Override
910 a7d1fdc2 Adam Menz
                protected void onPostExecute(Void arg1) {
911 a7d1fdc2 Adam Menz
                        hideDialog();
912 a7d1fdc2 Adam Menz
                        removeFromList(currentPath);
913 a7d1fdc2 Adam Menz
                        goUpDirectory();
914 a7d1fdc2 Adam Menz
                }
915 a7d1fdc2 Adam Menz
        }
916 71e52c4a Adam Menz
917 a7d1fdc2 Adam Menz
        private class DeleteContainerListenerTask extends
918 802fbd9e koutsoub
                        AsyncTask<Void, Void, Void> {
919 71e52c4a Adam Menz
920 a7d1fdc2 Adam Menz
                @Override
921 a7d1fdc2 Adam Menz
                protected Void doInBackground(Void... arg1) {
922 a7d1fdc2 Adam Menz
923 802fbd9e koutsoub
                        while (app.isDeletingContainer()) {
924 a7d1fdc2 Adam Menz
                                // wait for process to finish
925 a7d1fdc2 Adam Menz
                                // or have it be canceled
926 802fbd9e koutsoub
                                if (deleteContainerTask.isCancelled()) {
927 a7d1fdc2 Adam Menz
                                        return null;
928 a7d1fdc2 Adam Menz
                                }
929 a7d1fdc2 Adam Menz
                        }
930 a7d1fdc2 Adam Menz
                        return null;
931 a7d1fdc2 Adam Menz
                }
932 a7d1fdc2 Adam Menz
933 a7d1fdc2 Adam Menz
                /*
934 802fbd9e koutsoub
                 * when no longer processing, time to load the new files
935 a7d1fdc2 Adam Menz
                 */
936 a7d1fdc2 Adam Menz
                @Override
937 a7d1fdc2 Adam Menz
                protected void onPostExecute(Void arg1) {
938 877f6f58 Adam Menz
939 877f6f58 Adam Menz
                        hideDialog();
940 a06c1add Adam Menz
                        setResult(RESULT_OK);
941 a7d1fdc2 Adam Menz
                        finish();
942 a7d1fdc2 Adam Menz
                }
943 a7d1fdc2 Adam Menz
        }
944 802fbd9e koutsoub
945 856ad13d koutsoub
        public String getCurrentPath() {
946 856ad13d koutsoub
                return currentPath;
947 856ad13d koutsoub
        }
948 802fbd9e koutsoub
949 856ad13d koutsoub
        public ContainerObjects[] getFiles() {
950 856ad13d koutsoub
                return files;
951 856ad13d koutsoub
        }
952 802fbd9e koutsoub
        
953 802fbd9e koutsoub
        /***** UPLOAD FILE ****/
954 802fbd9e koutsoub
        protected void uploadFile(String filename, Intent data){
955 802fbd9e koutsoub
                if (filename.startsWith("file://")) {
956 802fbd9e koutsoub
                        filename = filename.substring(7);
957 802fbd9e koutsoub
                }
958 802fbd9e koutsoub
                // replace %20 and so on
959 802fbd9e koutsoub
                filename = Uri.decode(filename);
960 802fbd9e koutsoub
961 802fbd9e koutsoub
                int indx = filename.lastIndexOf("//");
962 802fbd9e koutsoub
                File file = new File(data.getData().getPath());
963 802fbd9e koutsoub
                String filenameNew = null;
964 802fbd9e koutsoub
                Long fileSize = null;
965 802fbd9e koutsoub
                if (!file.exists()) {
966 802fbd9e koutsoub
                        Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
967 802fbd9e koutsoub
                        if (cursor != null) {
968 802fbd9e koutsoub
                                while (cursor.moveToNext()) {
969 802fbd9e koutsoub
                                        for (int i = 0; i < cursor.getColumnCount(); i++) {
970 802fbd9e koutsoub
                                                if (cursor.getColumnName(i).equals("_data"))
971 802fbd9e koutsoub
                                                        filenameNew = cursor.getString(i);
972 802fbd9e koutsoub
                                                if (cursor.getColumnName(i).equals("_size"))
973 802fbd9e koutsoub
                                                        fileSize = cursor.getLong(i);
974 802fbd9e koutsoub
                                        }
975 802fbd9e koutsoub
                                }
976 802fbd9e koutsoub
                        }
977 802fbd9e koutsoub
                } else {
978 802fbd9e koutsoub
                        filenameNew = data.getData().getPath();
979 802fbd9e koutsoub
                }
980 802fbd9e koutsoub
                if (filenameNew != null) {
981 802fbd9e koutsoub
                        file = new File(filenameNew);
982 802fbd9e koutsoub
                        String ext = MimeTypeMap.getSingleton().getFileExtensionFromUrl(file.getPath());
983 802fbd9e koutsoub
                        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
984 802fbd9e koutsoub
                        String[] info = {
985 802fbd9e koutsoub
                                        file.getName(),
986 802fbd9e koutsoub
                                        mime, filenameNew};
987 802fbd9e koutsoub
                        new AddFileTask().execute(info);
988 802fbd9e koutsoub
                        //getTasks().getUploadTask(GssMyFolders.this).execute(getCurrentResource().getUri() + URLEncoder.encode(file.getName()), filenameNew);
989 802fbd9e koutsoub
                }
990 802fbd9e koutsoub
        }
991 802fbd9e koutsoub
        
992 802fbd9e koutsoub
        protected void uploadFile(List<Uri> uris){
993 802fbd9e koutsoub
                List<String> strings = new ArrayList<String>();
994 802fbd9e koutsoub
                String path = "";//getCurrentResource().getUri();
995 802fbd9e koutsoub
                if(!path.endsWith("/"))
996 802fbd9e koutsoub
                        path = path+"/";
997 802fbd9e koutsoub
                
998 802fbd9e koutsoub
                for(Uri data : uris){
999 802fbd9e koutsoub
                        String filename = data.getPath();
1000 802fbd9e koutsoub
                        if (filename.startsWith("file://")) {
1001 802fbd9e koutsoub
                                filename = filename.substring(7);
1002 802fbd9e koutsoub
                        }
1003 802fbd9e koutsoub
                        // replace %20 and so on
1004 802fbd9e koutsoub
                        filename = Uri.decode(filename);
1005 802fbd9e koutsoub
1006 802fbd9e koutsoub
                        int indx = filename.lastIndexOf("//");
1007 802fbd9e koutsoub
                        File file = new File(filename);
1008 802fbd9e koutsoub
                        String filenameNew = null;
1009 802fbd9e koutsoub
                        Long fileSize = null;
1010 802fbd9e koutsoub
                        if (!file.exists()) {
1011 802fbd9e koutsoub
                                Cursor cursor = getContentResolver().query(data, null, null, null, null);
1012 802fbd9e koutsoub
                                if (cursor != null) {
1013 802fbd9e koutsoub
                                        while (cursor.moveToNext()) {
1014 802fbd9e koutsoub
                                                for (int i = 0; i < cursor.getColumnCount(); i++) {
1015 802fbd9e koutsoub
                                                        if (cursor.getColumnName(i).equals("_data"))
1016 802fbd9e koutsoub
                                                                filenameNew = cursor.getString(i);
1017 802fbd9e koutsoub
                                                        if (cursor.getColumnName(i).equals("_size"))
1018 802fbd9e koutsoub
                                                                fileSize = cursor.getLong(i);
1019 802fbd9e koutsoub
                                                }
1020 802fbd9e koutsoub
                                        }
1021 802fbd9e koutsoub
                                }
1022 802fbd9e koutsoub
                        } else {
1023 802fbd9e koutsoub
                                filenameNew = data.getPath();
1024 802fbd9e koutsoub
                        }
1025 802fbd9e koutsoub
                        if (filenameNew != null) {
1026 802fbd9e koutsoub
                                file = new File(filenameNew);
1027 802fbd9e koutsoub
                                strings.add(path+URLEncoder.encode(file.getName()));
1028 802fbd9e koutsoub
                                strings.add(filenameNew);
1029 802fbd9e koutsoub
                        }
1030 802fbd9e koutsoub
                }
1031 802fbd9e koutsoub
                Log.i("*******",""+strings.size());
1032 802fbd9e koutsoub
                //getTasks().getMultiUploadTask(this).execute(strings.toArray(new String[strings.size()]));
1033 802fbd9e koutsoub
                        
1034 802fbd9e koutsoub
                
1035 802fbd9e koutsoub
        }
1036 802fbd9e koutsoub
        protected void uploadFile(Uri data){
1037 802fbd9e koutsoub
                String filename = data.getPath();
1038 802fbd9e koutsoub
                if (filename.startsWith("file://")) {
1039 802fbd9e koutsoub
                        filename = filename.substring(7);
1040 802fbd9e koutsoub
                }
1041 802fbd9e koutsoub
                // replace %20 and so on
1042 802fbd9e koutsoub
                filename = Uri.decode(filename);
1043 802fbd9e koutsoub
1044 802fbd9e koutsoub
                int indx = filename.lastIndexOf("//");
1045 802fbd9e koutsoub
                File file = new File(filename);
1046 802fbd9e koutsoub
                String filenameNew = null;
1047 802fbd9e koutsoub
                Long fileSize = null;
1048 802fbd9e koutsoub
                if (!file.exists()) {
1049 802fbd9e koutsoub
                        Cursor cursor = getContentResolver().query(data, null, null, null, null);
1050 802fbd9e koutsoub
                        if (cursor != null) {
1051 802fbd9e koutsoub
                                while (cursor.moveToNext()) {
1052 802fbd9e koutsoub
                                        for (int i = 0; i < cursor.getColumnCount(); i++) {
1053 802fbd9e koutsoub
                                                if (cursor.getColumnName(i).equals("_data"))
1054 802fbd9e koutsoub
                                                        filenameNew = cursor.getString(i);
1055 802fbd9e koutsoub
                                                if (cursor.getColumnName(i).equals("_size"))
1056 802fbd9e koutsoub
                                                        fileSize = cursor.getLong(i);
1057 802fbd9e koutsoub
                                        }
1058 802fbd9e koutsoub
                                }
1059 802fbd9e koutsoub
                        }
1060 802fbd9e koutsoub
                } else {
1061 802fbd9e koutsoub
                        filenameNew = data.getPath();
1062 802fbd9e koutsoub
                }
1063 802fbd9e koutsoub
                if (filenameNew != null) {
1064 802fbd9e koutsoub
                        file = new File(filenameNew);
1065 802fbd9e koutsoub
                        //String path = getCurrentResource().getUri();
1066 802fbd9e koutsoub
                        //if(!path.endsWith("/"))
1067 802fbd9e koutsoub
                                //path = path+"/";
1068 802fbd9e koutsoub
                        //getTasks().getUploadTask(GssMyFolders.this).execute(path + URLEncoder.encode(file.getName()), filenameNew);
1069 802fbd9e koutsoub
                }
1070 802fbd9e koutsoub
                
1071 802fbd9e koutsoub
        }
1072 3d6041e8 Phillip Toohill
}