Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (20.6 kB)

1 3d6041e8 Phillip Toohill
package com.rackspacecloud.android;
2 3d6041e8 Phillip Toohill
3 3d6041e8 Phillip Toohill
import java.io.IOException;
4 3d6041e8 Phillip Toohill
import java.io.StringReader;
5 3d6041e8 Phillip Toohill
import java.util.ArrayList;
6 d3ea294b Adam Menz
import java.util.Arrays;
7 3d6041e8 Phillip Toohill
8 3d6041e8 Phillip Toohill
import javax.xml.parsers.FactoryConfigurationError;
9 3d6041e8 Phillip Toohill
import javax.xml.parsers.ParserConfigurationException;
10 3d6041e8 Phillip Toohill
import javax.xml.parsers.SAXParser;
11 3d6041e8 Phillip Toohill
import javax.xml.parsers.SAXParserFactory;
12 3d6041e8 Phillip Toohill
13 3d6041e8 Phillip Toohill
import org.apache.http.HttpResponse;
14 3d6041e8 Phillip Toohill
import org.apache.http.client.ClientProtocolException;
15 3d6041e8 Phillip Toohill
import org.apache.http.impl.client.BasicResponseHandler;
16 3d6041e8 Phillip Toohill
import org.xml.sax.InputSource;
17 3d6041e8 Phillip Toohill
import org.xml.sax.SAXException;
18 3d6041e8 Phillip Toohill
import org.xml.sax.XMLReader;
19 3d6041e8 Phillip Toohill
20 3d6041e8 Phillip Toohill
import android.app.Activity;
21 3d6041e8 Phillip Toohill
import android.app.AlertDialog;
22 3d6041e8 Phillip Toohill
import android.app.Dialog;
23 3d6041e8 Phillip Toohill
import android.app.ListActivity;
24 d3ea294b Adam Menz
import android.app.ProgressDialog;
25 b347d5e3 Chmouel Boudjnah
import android.content.Context;
26 3d6041e8 Phillip Toohill
import android.content.DialogInterface;
27 3d6041e8 Phillip Toohill
import android.content.Intent;
28 3d6041e8 Phillip Toohill
import android.os.AsyncTask;
29 3d6041e8 Phillip Toohill
import android.os.Bundle;
30 3d6041e8 Phillip Toohill
import android.util.Log;
31 3d6041e8 Phillip Toohill
import android.view.LayoutInflater;
32 3d6041e8 Phillip Toohill
import android.view.Menu;
33 3d6041e8 Phillip Toohill
import android.view.MenuInflater;
34 3d6041e8 Phillip Toohill
import android.view.MenuItem;
35 3d6041e8 Phillip Toohill
import android.view.View;
36 3d6041e8 Phillip Toohill
import android.view.ViewGroup;
37 3d6041e8 Phillip Toohill
import android.widget.ArrayAdapter;
38 d3ea294b Adam Menz
import android.widget.EditText;
39 3d6041e8 Phillip Toohill
import android.widget.ListView;
40 3d6041e8 Phillip Toohill
import android.widget.TextView;
41 3d6041e8 Phillip Toohill
42 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.Container;
43 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerManager;
44 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
45 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerObjects;
46 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.CloudServersException;
47 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
48 d3ea294b Adam Menz
//import com.rackspacecloud.android.ViewServerActivity.RenameServerTask;
49 3d6041e8 Phillip Toohill
50 240418be Phillip Toohill
/**
51 3d6041e8 Phillip Toohill
 * 
52 3d6041e8 Phillip Toohill
 * @author Phillip Toohill
53 240418be Phillip Toohill
 * 
54 3d6041e8 Phillip Toohill
 */
55 3d6041e8 Phillip Toohill
public class ContainerObjectsActivity extends ListActivity {
56 3d6041e8 Phillip Toohill
57 3d6041e8 Phillip Toohill
        private static final int deleteContainer = 0;
58 37a14877 Adam Menz
        private static final int deleteFolder = 1;
59 3d6041e8 Phillip Toohill
        private ContainerObjects[] files;
60 3d6041e8 Phillip Toohill
        private static Container container;
61 3d6041e8 Phillip Toohill
        public String LOG = "viewFilesActivity";
62 8b432514 Phillip Toohill
        private String cdnEnabledIs;
63 6864568a Phillip Toohill
        public Object megaBytes;
64 6864568a Phillip Toohill
        public Object kiloBytes;
65 6864568a Phillip Toohill
        public int bConver = 1048576;
66 6864568a Phillip Toohill
        public int kbConver = 1024;
67 b347d5e3 Chmouel Boudjnah
        private Context context;
68 c16dc9e7 Adam Menz
        private String currentPath;
69 37a14877 Adam Menz
        private ContainerObjects[] curDirFiles;
70 d3ea294b Adam Menz
        ProgressDialog dialog;
71 240418be Phillip Toohill
72 3d6041e8 Phillip Toohill
        @Override
73 240418be Phillip Toohill
        public void onCreate(Bundle savedInstanceState) {
74 240418be Phillip Toohill
                super.onCreate(savedInstanceState);
75 240418be Phillip Toohill
                container = (Container) this.getIntent().getExtras().get("container");
76 4daf0073 Phillip Toohill
                Log.v(LOG, "CDNEnabled:" + container.isCdnEnabled());
77 b347d5e3 Chmouel Boudjnah
        context = getApplicationContext();
78 240418be Phillip Toohill
                if (container.isCdnEnabled() == true) {
79 240418be Phillip Toohill
                        cdnEnabledIs = "true";
80 240418be Phillip Toohill
                } else {
81 240418be Phillip Toohill
                        cdnEnabledIs = "false";
82 240418be Phillip Toohill
                }
83 240418be Phillip Toohill
                restoreState(savedInstanceState);
84 240418be Phillip Toohill
        }
85 c16dc9e7 Adam Menz
        
86 3d6041e8 Phillip Toohill
        @Override
87 3d6041e8 Phillip Toohill
        protected void onSaveInstanceState(Bundle outState) {
88 3d6041e8 Phillip Toohill
                super.onSaveInstanceState(outState);
89 3d6041e8 Phillip Toohill
                outState.putSerializable("container", files);
90 c16dc9e7 Adam Menz
                outState.putString("path", currentPath);
91 d3ea294b Adam Menz
                outState.putSerializable("curFiles", curDirFiles);
92 3d6041e8 Phillip Toohill
        }
93 3d6041e8 Phillip Toohill
94 240418be Phillip Toohill
        private void restoreState(Bundle state) {
95 d3ea294b Adam Menz
                if(state != null){
96 d3ea294b Adam Menz
                        if(state.containsKey("path")){
97 d3ea294b Adam Menz
                                currentPath = state.getString("path");
98 240418be Phillip Toohill
                        }
99 c16dc9e7 Adam Menz
                        else{
100 d3ea294b Adam Menz
                                currentPath = "";
101 d3ea294b Adam Menz
                        }
102 d3ea294b Adam Menz
                        if(state.containsKey("container") && state.containsKey("curFiles")){
103 d3ea294b Adam Menz
                                files = (ContainerObjects[]) state.getSerializable("container");
104 d3ea294b Adam Menz
                                curDirFiles = (ContainerObjects[]) state.getSerializable("curFiles");
105 d3ea294b Adam Menz
                                if(curDirFiles.length == 0){
106 d3ea294b Adam Menz
                                        displayNoServersCell();
107 d3ea294b Adam Menz
                                } else {
108 d3ea294b Adam Menz
                                        getListView().setDividerHeight(1); //restore divider lines
109 d3ea294b Adam Menz
                                        setListAdapter(new FileAdapter());
110 d3ea294b Adam Menz
                                }
111 c16dc9e7 Adam Menz
                        }
112 240418be Phillip Toohill
                }
113 d3ea294b Adam Menz
                else {
114 d3ea294b Adam Menz
                        currentPath = "";
115 d3ea294b Adam Menz
                        loadFiles();
116 d3ea294b Adam Menz
                }        
117 240418be Phillip Toohill
        }
118 d3ea294b Adam Menz
119 37a14877 Adam Menz
        /*
120 37a14877 Adam Menz
         * overriding back button press, because we are not actually changing
121 d3ea294b Adam Menz
         * activities when we navigate the file structure
122 37a14877 Adam Menz
         */
123 37a14877 Adam Menz
        public void onBackPressed() {
124 37a14877 Adam Menz
                if(currentPath.equals("")){
125 37a14877 Adam Menz
                        finish();
126 37a14877 Adam Menz
                }
127 37a14877 Adam Menz
                else{
128 37a14877 Adam Menz
                        goUpDirectory();
129 37a14877 Adam Menz
                }
130 37a14877 Adam Menz
        }
131 37a14877 Adam Menz
        
132 37a14877 Adam Menz
        /*
133 37a14877 Adam Menz
         * go to the current directory's parent and display that data
134 37a14877 Adam Menz
         */
135 37a14877 Adam Menz
        private void goUpDirectory(){
136 37a14877 Adam Menz
                currentPath = currentPath.substring(0, currentPath.substring(0, currentPath.length()-2).lastIndexOf("/")+1);
137 37a14877 Adam Menz
                loadCurrentDirectoryFiles();
138 37a14877 Adam Menz
                displayCurrentFiles();
139 37a14877 Adam Menz
        }
140 240418be Phillip Toohill
141 240418be Phillip Toohill
        private void loadFiles() {
142 d3ea294b Adam Menz
                //displayLoadingCell();
143 37a14877 Adam Menz
                new LoadFilesTask().execute();
144 37a14877 Adam Menz
        }
145 d3ea294b Adam Menz
146 37a14877 Adam Menz
        
147 37a14877 Adam Menz
        /* load only the files that should display for the 
148 37a14877 Adam Menz
         * current directory in the curDirFiles[]
149 37a14877 Adam Menz
         */
150 37a14877 Adam Menz
        private void loadCurrentDirectoryFiles(){
151 37a14877 Adam Menz
                ArrayList<ContainerObjects> curFiles = new ArrayList<ContainerObjects>();
152 37a14877 Adam Menz
153 37a14877 Adam Menz
                if(files != null){
154 37a14877 Adam Menz
                        for(int i = 0 ; i < files.length; i ++){
155 37a14877 Adam Menz
                                if(fileBelongsInDir(files[i])){
156 37a14877 Adam Menz
                                        curFiles.add(files[i]);
157 37a14877 Adam Menz
                                }
158 37a14877 Adam Menz
                        }
159 240418be Phillip Toohill
160 37a14877 Adam Menz
                        curDirFiles = new ContainerObjects[curFiles.size()];
161 37a14877 Adam Menz
                        for(int i = 0; i < curFiles.size(); i++){
162 37a14877 Adam Menz
                                curDirFiles[i] = curFiles.get(i);
163 37a14877 Adam Menz
                        }
164 37a14877 Adam Menz
                }
165 37a14877 Adam Menz
        }
166 37a14877 Adam Menz
        
167 37a14877 Adam Menz
        /*
168 37a14877 Adam Menz
         * determines if a file should be displayed in current 
169 37a14877 Adam Menz
         * directory
170 37a14877 Adam Menz
         */
171 37a14877 Adam Menz
        private Boolean fileBelongsInDir(ContainerObjects obj){
172 37a14877 Adam Menz
                String objPath = obj.getCName();
173 37a14877 Adam Menz
                if(!objPath.startsWith(currentPath)){
174 37a14877 Adam Menz
                        return false;
175 37a14877 Adam Menz
                }
176 37a14877 Adam Menz
                else{
177 37a14877 Adam Menz
                        objPath = objPath.substring(currentPath.length());
178 37a14877 Adam Menz
                        return !objPath.contains("/");
179 37a14877 Adam Menz
                }
180 240418be Phillip Toohill
        }
181 240418be Phillip Toohill
182 37a14877 Adam Menz
        
183 37a14877 Adam Menz
        /*
184 37a14877 Adam Menz
         * loads all the files that are in the container
185 37a14877 Adam Menz
         * into one array
186 37a14877 Adam Menz
         */
187 3d6041e8 Phillip Toohill
        private void setFileList(ArrayList<ContainerObjects> files) {
188 240418be Phillip Toohill
                if (files == null) {
189 240418be Phillip Toohill
                        files = new ArrayList<ContainerObjects>();
190 240418be Phillip Toohill
                }
191 240418be Phillip Toohill
                String[] fileNames = new String[files.size()];
192 240418be Phillip Toohill
                this.files = new ContainerObjects[files.size()];
193 240418be Phillip Toohill
194 240418be Phillip Toohill
                if (files != null) {
195 3d6041e8 Phillip Toohill
                        for (int i = 0; i < files.size(); i++) {
196 240418be Phillip Toohill
                                ContainerObjects file = files.get(i);
197 3d6041e8 Phillip Toohill
                                this.files[i] = file;
198 3d6041e8 Phillip Toohill
                                fileNames[i] = file.getName();
199 3d6041e8 Phillip Toohill
                        }
200 3d6041e8 Phillip Toohill
                }
201 240418be Phillip Toohill
202 37a14877 Adam Menz
                displayCurrentFiles();
203 37a14877 Adam Menz
        }
204 37a14877 Adam Menz
        
205 37a14877 Adam Menz
        private void displayCurrentFiles(){
206 37a14877 Adam Menz
                loadCurrentDirectoryFiles();
207 37a14877 Adam Menz
                if (curDirFiles.length == 0) {
208 3d6041e8 Phillip Toohill
                        displayNoServersCell();
209 3d6041e8 Phillip Toohill
                } else {
210 240418be Phillip Toohill
                        getListView().setDividerHeight(1); // restore divider lines
211 3d6041e8 Phillip Toohill
                        setListAdapter(new FileAdapter());
212 3d6041e8 Phillip Toohill
                }
213 240418be Phillip Toohill
        }
214 240418be Phillip Toohill
215 37a14877 Adam Menz
        /*
216 37a14877 Adam Menz
         * display a different empty page depending
217 37a14877 Adam Menz
         * of if you are at top of container or
218 37a14877 Adam Menz
         * in a folder
219 37a14877 Adam Menz
         */
220 240418be Phillip Toohill
        private void displayNoServersCell() {
221 240418be Phillip Toohill
                String a[] = new String[1];
222 37a14877 Adam Menz
                if(currentPath.equals("")){
223 37a14877 Adam Menz
                        a[0] = "Empty Container";
224 37a14877 Adam Menz
                        setListAdapter(new ArrayAdapter<String>(this, R.layout.noobjectscell,
225 240418be Phillip Toohill
                                R.id.no_files_label, a));
226 37a14877 Adam Menz
                }
227 37a14877 Adam Menz
                else{
228 37a14877 Adam Menz
                        a[0] = "No Files";
229 37a14877 Adam Menz
                        setListAdapter(new ArrayAdapter<String>(this, R.layout.nofilescell,
230 37a14877 Adam Menz
                                                R.id.no_files_label, a));
231 37a14877 Adam Menz
                }
232 240418be Phillip Toohill
                getListView().setTextFilterEnabled(true);
233 240418be Phillip Toohill
                getListView().setDividerHeight(0); // hide the dividers so it won't look
234 240418be Phillip Toohill
                                                                                        // like a list row
235 240418be Phillip Toohill
                getListView().setItemsCanFocus(false);
236 240418be Phillip Toohill
        }
237 240418be Phillip Toohill
238 240418be Phillip Toohill
        private void showAlert(String title, String message) {
239 240418be Phillip Toohill
                // Can't create handler inside thread that has not called
240 240418be Phillip Toohill
                // Looper.prepare()
241 240418be Phillip Toohill
                // Looper.prepare();
242 240418be Phillip Toohill
                try {
243 240418be Phillip Toohill
                        AlertDialog alert = new AlertDialog.Builder(this).create();
244 240418be Phillip Toohill
                        alert.setTitle(title);
245 240418be Phillip Toohill
                        alert.setMessage(message);
246 240418be Phillip Toohill
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
247 240418be Phillip Toohill
                                public void onClick(DialogInterface dialog, int which) {
248 240418be Phillip Toohill
                                        return;
249 240418be Phillip Toohill
                                }
250 240418be Phillip Toohill
                        });
251 240418be Phillip Toohill
                        alert.show();
252 240418be Phillip Toohill
                } catch (Exception e) {
253 240418be Phillip Toohill
                        e.printStackTrace();
254 240418be Phillip Toohill
                }
255 240418be Phillip Toohill
        }
256 240418be Phillip Toohill
257 c16dc9e7 Adam Menz
        /* just get the last part of the filename
258 c16dc9e7 Adam Menz
         * so the entire file path does not show
259 c16dc9e7 Adam Menz
        */
260 c16dc9e7 Adam Menz
        private String getShortName(String longName){
261 c16dc9e7 Adam Menz
                String s = longName;
262 c16dc9e7 Adam Menz
                if(!s.contains("/")){
263 c16dc9e7 Adam Menz
                        return s;
264 c16dc9e7 Adam Menz
                }
265 c16dc9e7 Adam Menz
                else {
266 d3ea294b Adam Menz
                        return s.substring(s.lastIndexOf('/')+1);
267 d3ea294b Adam Menz
                }
268 d3ea294b Adam Menz
        }
269 d3ea294b Adam Menz
        
270 d3ea294b Adam Menz
        /*
271 d3ea294b Adam Menz
         * removed a specified object from the array of 
272 d3ea294b Adam Menz
         * all the files in the container
273 d3ea294b Adam Menz
         */
274 d3ea294b Adam Menz
        private void removeFromList(String path){
275 d3ea294b Adam Menz
                ArrayList<ContainerObjects> temp = new ArrayList<ContainerObjects>(Arrays.asList(files));
276 d3ea294b Adam Menz
                for(int i = 0; i < files.length; i++){
277 d3ea294b Adam Menz
                        if(files[i].getCName().equals(path.substring(0, path.length()-1))){
278 d3ea294b Adam Menz
                                temp.remove(i);
279 d3ea294b Adam Menz
                        }
280 d3ea294b Adam Menz
                }
281 d3ea294b Adam Menz
                files = new ContainerObjects[temp.size()];
282 d3ea294b Adam Menz
                for(int i = 0; i < temp.size(); i++){
283 d3ea294b Adam Menz
                        files[i] = temp.get(i);
284 d3ea294b Adam Menz
                }
285 d3ea294b Adam Menz
        }
286 d3ea294b Adam Menz
287 d3ea294b Adam Menz
        protected void onListItemClick(ListView l, View v, int position, long id) {
288 d3ea294b Adam Menz
                if (curDirFiles != null && curDirFiles.length > 0) {
289 d3ea294b Adam Menz
                        Intent viewIntent;
290 d3ea294b Adam Menz
                        if(curDirFiles[position].getContentType().equals("application/directory")){                        
291 d3ea294b Adam Menz
                                currentPath = curDirFiles[position].getCName() + "/";
292 d3ea294b Adam Menz
                                loadCurrentDirectoryFiles();
293 d3ea294b Adam Menz
                                displayCurrentFiles();
294 d3ea294b Adam Menz
                        }
295 d3ea294b Adam Menz
        
296 d3ea294b Adam Menz
                        else{
297 d3ea294b Adam Menz
                                viewIntent = new Intent(this, ContainerObjectDetails.class);
298 d3ea294b Adam Menz
                                viewIntent.putExtra("container", curDirFiles[position]);
299 d3ea294b Adam Menz
                                viewIntent.putExtra("cdnUrl", container.getCdnUrl());
300 d3ea294b Adam Menz
                                viewIntent.putExtra("containerNames", container.getName());
301 d3ea294b Adam Menz
                                viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
302 d3ea294b Adam Menz
                                startActivityForResult(viewIntent, 55); // arbitrary number; never
303 d3ea294b Adam Menz
                                // used again
304 d3ea294b Adam Menz
                        }
305 c16dc9e7 Adam Menz
                }
306 c16dc9e7 Adam Menz
        }
307 240418be Phillip Toohill
308 37a14877 Adam Menz
        /* 
309 37a14877 Adam Menz
         * Create the Menu options
310 37a14877 Adam Menz
         */
311 240418be Phillip Toohill
        @Override
312 240418be Phillip Toohill
        public boolean onCreateOptionsMenu(Menu menu) {
313 3d6041e8 Phillip Toohill
                super.onCreateOptionsMenu(menu);
314 3d6041e8 Phillip Toohill
                MenuInflater inflater = getMenuInflater();
315 3d6041e8 Phillip Toohill
                inflater.inflate(R.menu.view_container_object_list_menu, menu);
316 3d6041e8 Phillip Toohill
                return true;
317 240418be Phillip Toohill
        }
318 240418be Phillip Toohill
319 240418be Phillip Toohill
        @Override
320 37a14877 Adam Menz
        /*
321 37a14877 Adam Menz
         * option performed for delete depends on if you
322 37a14877 Adam Menz
         * are at the top of a container or in a folder
323 37a14877 Adam Menz
         */
324 240418be Phillip Toohill
        public boolean onOptionsItemSelected(MenuItem item) {
325 3d6041e8 Phillip Toohill
                switch (item.getItemId()) {
326 3d6041e8 Phillip Toohill
                case R.id.delete_container:
327 37a14877 Adam Menz
                        if(currentPath.equals("")){
328 37a14877 Adam Menz
                                showDialog(deleteContainer);
329 37a14877 Adam Menz
                        }
330 37a14877 Adam Menz
                        else{
331 37a14877 Adam Menz
                                showDialog(deleteFolder);
332 37a14877 Adam Menz
                        }
333 3d6041e8 Phillip Toohill
                        return true;
334 3d6041e8 Phillip Toohill
                case R.id.enable_cdn:
335 3d6041e8 Phillip Toohill
                        Intent viewIntent1 = new Intent(this, EnableCDNActivity.class);
336 240418be Phillip Toohill
                        viewIntent1.putExtra("Cname", container.getName());
337 240418be Phillip Toohill
                        startActivityForResult(viewIntent1, 56);
338 3d6041e8 Phillip Toohill
                        return true;
339 3d6041e8 Phillip Toohill
                case R.id.refresh:
340 3d6041e8 Phillip Toohill
                        loadFiles();
341 240418be Phillip Toohill
                        return true;
342 d3ea294b Adam Menz
                case R.id.add_folder:
343 d3ea294b Adam Menz
                        showDialog(R.id.add_folder);
344 d3ea294b Adam Menz
                case R.id.add_file:
345 d3ea294b Adam Menz
                        Intent viewIntent2 = new Intent(this, AddFileActivity.class);
346 d3ea294b Adam Menz
                        viewIntent2.putExtra("Cname", container.getName());
347 d3ea294b Adam Menz
                        viewIntent2.putExtra("curPath", currentPath);
348 d3ea294b Adam Menz
                        startActivityForResult(viewIntent2, 56);
349 d3ea294b Adam Menz
                        return true;        
350 3d6041e8 Phillip Toohill
                }
351 3d6041e8 Phillip Toohill
                return false;
352 240418be Phillip Toohill
        }
353 240418be Phillip Toohill
354 3d6041e8 Phillip Toohill
        @Override
355 240418be Phillip Toohill
        protected Dialog onCreateDialog(int id) {
356 240418be Phillip Toohill
                switch (id) {
357 240418be Phillip Toohill
                case deleteContainer:
358 37a14877 Adam Menz
                        if(curDirFiles.length == 0){
359 37a14877 Adam Menz
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
360 37a14877 Adam Menz
                                .setIcon(R.drawable.alert_dialog_icon)
361 37a14877 Adam Menz
                                .setTitle("Delete Container")
362 37a14877 Adam Menz
                                .setMessage(
363 37a14877 Adam Menz
                                "Are you sure you want to delete this Container?")
364 37a14877 Adam Menz
                                .setPositiveButton("Delete Container",
365 37a14877 Adam Menz
                                                new DialogInterface.OnClickListener() {
366 37a14877 Adam Menz
                                        public void onClick(DialogInterface dialog,
367 37a14877 Adam Menz
                                                        int whichButton) {
368 37a14877 Adam Menz
                                                // User clicked OK so do some stuff
369 37a14877 Adam Menz
                                                new DeleteContainerTask()
370 37a14877 Adam Menz
                                                .execute(currentPath);
371 37a14877 Adam Menz
                                        }
372 37a14877 Adam Menz
                                })
373 37a14877 Adam Menz
                                .setNegativeButton("Cancel",
374 37a14877 Adam Menz
                                                new DialogInterface.OnClickListener() {
375 37a14877 Adam Menz
                                        public void onClick(DialogInterface dialog,
376 37a14877 Adam Menz
                                                        int whichButton) {
377 37a14877 Adam Menz
                                                // User clicked Cancel so do some stuff
378 37a14877 Adam Menz
                                        }
379 37a14877 Adam Menz
                                }).create();
380 37a14877 Adam Menz
                        }
381 37a14877 Adam Menz
                        else{
382 37a14877 Adam Menz
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
383 37a14877 Adam Menz
                                .setIcon(R.drawable.alert_dialog_icon)
384 37a14877 Adam Menz
                                .setTitle("Delete Container")
385 37a14877 Adam Menz
                                .setMessage("Container must be empty to delete")
386 37a14877 Adam Menz
                                .setNegativeButton("OK",
387 37a14877 Adam Menz
                                                new DialogInterface.OnClickListener() {
388 37a14877 Adam Menz
                                        public void onClick(DialogInterface dialog,
389 37a14877 Adam Menz
                                                        int whichButton) {
390 37a14877 Adam Menz
                                                // User clicked Cancel so do some stuff
391 37a14877 Adam Menz
                                        }
392 37a14877 Adam Menz
                                }).create();
393 37a14877 Adam Menz
                        }
394 37a14877 Adam Menz
                case deleteFolder:
395 37a14877 Adam Menz
                        if(curDirFiles.length == 0){
396 37a14877 Adam Menz
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
397 37a14877 Adam Menz
                                .setIcon(R.drawable.alert_dialog_icon)
398 37a14877 Adam Menz
                                .setTitle("Delete Folder")
399 37a14877 Adam Menz
                                .setMessage(
400 37a14877 Adam Menz
                                "Are you sure you want to delete this Folder?")
401 37a14877 Adam Menz
                                .setPositiveButton("Delete Folder",
402 37a14877 Adam Menz
                                                new DialogInterface.OnClickListener() {
403 37a14877 Adam Menz
                                        public void onClick(DialogInterface dialog,
404 37a14877 Adam Menz
                                                        int whichButton) {
405 37a14877 Adam Menz
                                                // User clicked OK so do some stuff
406 37a14877 Adam Menz
                                                new DeleteObjectTask()
407 37a14877 Adam Menz
                                                .execute();
408 37a14877 Adam Menz
                                        }
409 37a14877 Adam Menz
                                })
410 37a14877 Adam Menz
                                .setNegativeButton("Cancel",
411 37a14877 Adam Menz
                                                new DialogInterface.OnClickListener() {
412 37a14877 Adam Menz
                                        public void onClick(DialogInterface dialog,
413 37a14877 Adam Menz
                                                        int whichButton) {
414 37a14877 Adam Menz
                                                // User clicked Cancel so do some stuff
415 37a14877 Adam Menz
                                        }
416 37a14877 Adam Menz
                                }).create();
417 37a14877 Adam Menz
                        }
418 37a14877 Adam Menz
                        else{
419 37a14877 Adam Menz
                                return new AlertDialog.Builder(ContainerObjectsActivity.this)
420 37a14877 Adam Menz
                                .setIcon(R.drawable.alert_dialog_icon)
421 37a14877 Adam Menz
                                .setTitle("Delete Folder")
422 37a14877 Adam Menz
                                .setMessage(
423 37a14877 Adam Menz
                                "Folder must be empty to delete")
424 37a14877 Adam Menz
                                .setNegativeButton("OK",
425 37a14877 Adam Menz
                                                new DialogInterface.OnClickListener() {
426 37a14877 Adam Menz
                                        public void onClick(DialogInterface dialog,
427 37a14877 Adam Menz
                                                        int whichButton) {
428 37a14877 Adam Menz
                                                // User clicked Cancel so do some stuff
429 37a14877 Adam Menz
                                        }
430 37a14877 Adam Menz
                                }).create();
431 37a14877 Adam Menz
                        }
432 d3ea294b Adam Menz
                case R.id.add_folder:
433 d3ea294b Adam Menz
                        final EditText input = new EditText(this);
434 d3ea294b Adam Menz
            return new AlertDialog.Builder(ContainerObjectsActivity.this)
435 d3ea294b Adam Menz
                .setIcon(R.drawable.alert_dialog_icon)
436 d3ea294b Adam Menz
            .setView(input)
437 d3ea294b Adam Menz
                .setTitle("Add Folder")
438 d3ea294b Adam Menz
                .setMessage("Enter new name for folder: ")                         
439 d3ea294b Adam Menz
                .setPositiveButton("Add", new DialogInterface.OnClickListener() {
440 d3ea294b Adam Menz
                        public void onClick(DialogInterface dialog, int whichButton) {
441 d3ea294b Adam Menz
                                // User clicked OK so do some stuff
442 d3ea294b Adam Menz
                                String[] info = {input.getText().toString(), "application/directory"};
443 d3ea294b Adam Menz
                                new AddFolderTask().execute(info);
444 d3ea294b Adam Menz
                        }
445 d3ea294b Adam Menz
                })
446 d3ea294b Adam Menz
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
447 d3ea294b Adam Menz
                        public void onClick(DialogInterface dialog, int whichButton) {
448 d3ea294b Adam Menz
                                // User clicked Cancel so do some stuff
449 d3ea294b Adam Menz
                        }
450 d3ea294b Adam Menz
                })
451 d3ea294b Adam Menz
                .create();     
452 240418be Phillip Toohill
                }
453 240418be Phillip Toohill
                return null;
454 3d6041e8 Phillip Toohill
        }
455 3d6041e8 Phillip Toohill
456 240418be Phillip Toohill
        @Override
457 240418be Phillip Toohill
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
458 240418be Phillip Toohill
                super.onActivityResult(requestCode, resultCode, data);
459 240418be Phillip Toohill
460 240418be Phillip Toohill
                if (resultCode == RESULT_OK) {
461 240418be Phillip Toohill
                        // a sub-activity kicked back, so we want to refresh the server list
462 240418be Phillip Toohill
                        loadFiles();
463 240418be Phillip Toohill
                }
464 240418be Phillip Toohill
                if (requestCode == 56) {
465 240418be Phillip Toohill
                        if (resultCode == RESULT_OK) {
466 4daf0073 Phillip Toohill
                                Intent viewIntent1 = new Intent(this,
467 4daf0073 Phillip Toohill
                                                ListContainerActivity.class);
468 4daf0073 Phillip Toohill
                                startActivityForResult(viewIntent1, 56);
469 3d6041e8 Phillip Toohill
                        }
470 240418be Phillip Toohill
                }
471 240418be Phillip Toohill
        }
472 240418be Phillip Toohill
473 240418be Phillip Toohill
        private CloudServersException parseCloudServersException(
474 240418be Phillip Toohill
                        HttpResponse response) {
475 240418be Phillip Toohill
                CloudServersException cse = new CloudServersException();
476 240418be Phillip Toohill
                try {
477 240418be Phillip Toohill
                        BasicResponseHandler responseHandler = new BasicResponseHandler();
478 240418be Phillip Toohill
                        String body = responseHandler.handleResponse(response);
479 240418be Phillip Toohill
                        CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
480 240418be Phillip Toohill
                        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
481 240418be Phillip Toohill
                        XMLReader xmlReader = saxParser.getXMLReader();
482 240418be Phillip Toohill
                        xmlReader.setContentHandler(parser);
483 240418be Phillip Toohill
                        xmlReader.parse(new InputSource(new StringReader(body)));
484 240418be Phillip Toohill
                        cse = parser.getException();
485 240418be Phillip Toohill
                } catch (ClientProtocolException e) {
486 240418be Phillip Toohill
                        cse = new CloudServersException();
487 240418be Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
488 240418be Phillip Toohill
                } catch (IOException e) {
489 240418be Phillip Toohill
                        cse = new CloudServersException();
490 240418be Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
491 240418be Phillip Toohill
                } catch (ParserConfigurationException e) {
492 240418be Phillip Toohill
                        cse = new CloudServersException();
493 240418be Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
494 240418be Phillip Toohill
                } catch (SAXException e) {
495 240418be Phillip Toohill
                        cse = new CloudServersException();
496 240418be Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
497 240418be Phillip Toohill
                } catch (FactoryConfigurationError e) {
498 240418be Phillip Toohill
                        cse = new CloudServersException();
499 240418be Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
500 240418be Phillip Toohill
                }
501 240418be Phillip Toohill
                return cse;
502 240418be Phillip Toohill
        }
503 d3ea294b Adam Menz
        
504 d3ea294b Adam Menz
        class FileAdapter extends ArrayAdapter<ContainerObjects> {
505 d3ea294b Adam Menz
                FileAdapter() {
506 d3ea294b Adam Menz
                        super(ContainerObjectsActivity.this,
507 d3ea294b Adam Menz
                                        R.layout.listcontainerobjectcell, curDirFiles);
508 d3ea294b Adam Menz
                }
509 d3ea294b Adam Menz
        
510 d3ea294b Adam Menz
                public View getView(int position, View convertView, ViewGroup parent) {
511 d3ea294b Adam Menz
        
512 d3ea294b Adam Menz
                        ContainerObjects file = curDirFiles[position];
513 d3ea294b Adam Menz
                        LayoutInflater inflater = getLayoutInflater();
514 d3ea294b Adam Menz
                        View row = inflater.inflate(R.layout.listcontainerobjectcell,
515 d3ea294b Adam Menz
                                        parent, false);
516 d3ea294b Adam Menz
        
517 d3ea294b Adam Menz
                        TextView label = (TextView) row.findViewById(R.id.label);
518 d3ea294b Adam Menz
                        //label.setText(file.getCName());
519 d3ea294b Adam Menz
                        label.setText(getShortName(file.getCName()));
520 d3ea294b Adam Menz
        
521 d3ea294b Adam Menz
                        if (file.getBytes() >= bConver) {
522 d3ea294b Adam Menz
                                megaBytes = Math.abs(file.getBytes() / bConver + 0.2);
523 d3ea294b Adam Menz
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
524 d3ea294b Adam Menz
                                sublabel.setText(megaBytes + " MB");
525 d3ea294b Adam Menz
                        } else if (file.getBytes() >= kbConver) {
526 d3ea294b Adam Menz
                                kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2);
527 d3ea294b Adam Menz
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
528 d3ea294b Adam Menz
                                sublabel.setText(kiloBytes + " KB");
529 d3ea294b Adam Menz
                        } else {
530 d3ea294b Adam Menz
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
531 d3ea294b Adam Menz
                                sublabel.setText(file.getBytes() + " B");
532 d3ea294b Adam Menz
                        }
533 d3ea294b Adam Menz
        
534 d3ea294b Adam Menz
                        return (row);
535 d3ea294b Adam Menz
                }
536 d3ea294b Adam Menz
        }
537 240418be Phillip Toohill
538 d3ea294b Adam Menz
        private class LoadFilesTask extends
539 d3ea294b Adam Menz
                        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
540 d3ea294b Adam Menz
        
541 d3ea294b Adam Menz
                private CloudServersException exception;
542 d3ea294b Adam Menz
                
543 d3ea294b Adam Menz
                protected void onPreExecute(){
544 d3ea294b Adam Menz
                        dialog = ProgressDialog.show(ContainerObjectsActivity.this, "", "Loading Files...", true);
545 d3ea294b Adam Menz
                }
546 37a14877 Adam Menz
        
547 d3ea294b Adam Menz
                @Override
548 d3ea294b Adam Menz
                protected ArrayList<ContainerObjects> doInBackground(String... path) {
549 d3ea294b Adam Menz
                        ArrayList<ContainerObjects> files = null;
550 d3ea294b Adam Menz
                        try {
551 d3ea294b Adam Menz
                                files = (new ContainerObjectManager(context)).createList(true,
552 d3ea294b Adam Menz
                                                container.getName());
553 d3ea294b Adam Menz
                        } catch (CloudServersException e) {
554 d3ea294b Adam Menz
                                exception = e;
555 d3ea294b Adam Menz
                                e.printStackTrace();
556 d3ea294b Adam Menz
                        }
557 d3ea294b Adam Menz
                        return files;
558 d3ea294b Adam Menz
                }
559 d3ea294b Adam Menz
        
560 d3ea294b Adam Menz
                @Override
561 d3ea294b Adam Menz
                protected void onPostExecute(ArrayList<ContainerObjects> result) {
562 d3ea294b Adam Menz
                        dialog.dismiss();
563 d3ea294b Adam Menz
                        if (exception != null) {
564 d3ea294b Adam Menz
                                showAlert("Error", exception.getMessage());
565 d3ea294b Adam Menz
                        }
566 d3ea294b Adam Menz
                        setFileList(result);
567 d3ea294b Adam Menz
                }
568 d3ea294b Adam Menz
        }
569 d3ea294b Adam Menz
570 37a14877 Adam Menz
        private class DeleteObjectTask extends
571 37a14877 Adam Menz
        AsyncTask<Void, Void, HttpResponse> {
572 240418be Phillip Toohill
573 3d6041e8 Phillip Toohill
                private CloudServersException exception;
574 d3ea294b Adam Menz
                
575 d3ea294b Adam Menz
                protected void onPreExecute(){
576 d3ea294b Adam Menz
                        dialog = ProgressDialog.show(ContainerObjectsActivity.this, "", "Deleting...", true);
577 d3ea294b Adam Menz
                }
578 d3ea294b Adam Menz
                
579 3d6041e8 Phillip Toohill
                @Override
580 3d6041e8 Phillip Toohill
                protected HttpResponse doInBackground(Void... arg0) {
581 3d6041e8 Phillip Toohill
                        HttpResponse resp = null;
582 3d6041e8 Phillip Toohill
                        try {
583 37a14877 Adam Menz
                                //subtring because the current directory contains a "/" at the end of the string
584 37a14877 Adam Menz
                                resp = (new ContainerObjectManager(context)).deleteObject(container.getName(), currentPath.substring(0, currentPath.length()-1));
585 37a14877 Adam Menz
                        } catch (CloudServersException e) {
586 37a14877 Adam Menz
                                exception = e;
587 37a14877 Adam Menz
                        }
588 37a14877 Adam Menz
                        return resp;
589 37a14877 Adam Menz
                }
590 37a14877 Adam Menz
591 37a14877 Adam Menz
                @Override
592 37a14877 Adam Menz
                protected void onPostExecute(HttpResponse response) {
593 37a14877 Adam Menz
                        if (response != null) {
594 37a14877 Adam Menz
                                int statusCode = response.getStatusLine().getStatusCode();
595 37a14877 Adam Menz
                                if (statusCode == 409) {
596 37a14877 Adam Menz
                                        showAlert("Error",
597 37a14877 Adam Menz
                                        "Folder must be empty in order to delete");
598 37a14877 Adam Menz
                                }
599 37a14877 Adam Menz
                                if (statusCode == 204) {
600 37a14877 Adam Menz
                                        setResult(Activity.RESULT_OK);
601 d3ea294b Adam Menz
                                        removeFromList(currentPath);
602 37a14877 Adam Menz
                                        goUpDirectory();
603 d3ea294b Adam Menz
                                        dialog.dismiss();
604 37a14877 Adam Menz
                                } else {
605 37a14877 Adam Menz
                                        CloudServersException cse = parseCloudServersException(response);
606 37a14877 Adam Menz
                                        if ("".equals(cse.getMessage())) {
607 37a14877 Adam Menz
                                                showAlert("Error",
608 37a14877 Adam Menz
                                                        "There was a problem deleting your folder.");
609 37a14877 Adam Menz
                                        } else {
610 37a14877 Adam Menz
                                                showAlert("Error",
611 37a14877 Adam Menz
                                                                "There was a problem deleting your folder: "
612 37a14877 Adam Menz
                                                                        + cse.getMessage());
613 37a14877 Adam Menz
                                        }
614 37a14877 Adam Menz
                                }
615 37a14877 Adam Menz
                        } else if (exception != null) {
616 37a14877 Adam Menz
                                showAlert("Error", "There was a problem deleting your folder: "
617 37a14877 Adam Menz
                                                + exception.getMessage());
618 37a14877 Adam Menz
                        }
619 37a14877 Adam Menz
                }
620 37a14877 Adam Menz
        }
621 37a14877 Adam Menz
        
622 d3ea294b Adam Menz
        private class AddFolderTask extends
623 d3ea294b Adam Menz
        AsyncTask<String, Void, HttpResponse> {
624 d3ea294b Adam Menz
        
625 d3ea294b Adam Menz
                private CloudServersException exception;
626 d3ea294b Adam Menz
                
627 d3ea294b Adam Menz
                protected void onPreExecute(){
628 d3ea294b Adam Menz
                        dialog = ProgressDialog.show(ContainerObjectsActivity.this, "", "Adding Folder...", true);
629 d3ea294b Adam Menz
                }
630 d3ea294b Adam Menz
        
631 d3ea294b Adam Menz
                @Override
632 d3ea294b Adam Menz
                protected HttpResponse doInBackground(String... data) {
633 d3ea294b Adam Menz
                        HttpResponse resp = null;
634 d3ea294b Adam Menz
                        try {
635 d3ea294b Adam Menz
                                resp = (new ContainerObjectManager(context)).addObject(container.getName(), currentPath, data[0], data[1]);
636 d3ea294b Adam Menz
                        } catch (CloudServersException e) {
637 d3ea294b Adam Menz
                                exception = e;
638 d3ea294b Adam Menz
                        }
639 d3ea294b Adam Menz
                        return resp;
640 d3ea294b Adam Menz
                }
641 d3ea294b Adam Menz
        
642 d3ea294b Adam Menz
                @Override
643 d3ea294b Adam Menz
                protected void onPostExecute(HttpResponse response) {
644 d3ea294b Adam Menz
                        dialog.dismiss();
645 d3ea294b Adam Menz
                        if (response != null) {
646 d3ea294b Adam Menz
                                int statusCode = response.getStatusLine().getStatusCode();
647 d3ea294b Adam Menz
                                if (statusCode == 201) {
648 d3ea294b Adam Menz
                                        setResult(Activity.RESULT_OK);
649 d3ea294b Adam Menz
                                        loadFiles();
650 d3ea294b Adam Menz
                                } else {
651 d3ea294b Adam Menz
                                        CloudServersException cse = parseCloudServersException(response);
652 d3ea294b Adam Menz
                                        if ("".equals(cse.getMessage())) {
653 d3ea294b Adam Menz
                                                showAlert("Error",
654 d3ea294b Adam Menz
                                                        "There was a problem deleting your folder.");
655 d3ea294b Adam Menz
                                        } else {
656 d3ea294b Adam Menz
                                                showAlert("Error",
657 d3ea294b Adam Menz
                                                                "There was a problem deleting your folder: "
658 d3ea294b Adam Menz
                                                                        + cse.getMessage());
659 d3ea294b Adam Menz
                                        }
660 d3ea294b Adam Menz
                                }
661 d3ea294b Adam Menz
                        } else if (exception != null) {
662 d3ea294b Adam Menz
                                showAlert("Error", "There was a problem deleting your folder: "
663 d3ea294b Adam Menz
                                                + exception.getMessage());
664 d3ea294b Adam Menz
                        }
665 d3ea294b Adam Menz
                }
666 d3ea294b Adam Menz
        }
667 d3ea294b Adam Menz
668 37a14877 Adam Menz
        private class DeleteContainerTask extends
669 37a14877 Adam Menz
        AsyncTask<String, Void, HttpResponse> {
670 37a14877 Adam Menz
671 37a14877 Adam Menz
                private CloudServersException exception;
672 37a14877 Adam Menz
673 37a14877 Adam Menz
                @Override
674 37a14877 Adam Menz
                protected HttpResponse doInBackground(String... object) {
675 37a14877 Adam Menz
                        HttpResponse resp = null;
676 37a14877 Adam Menz
                        try {
677 b347d5e3 Chmouel Boudjnah
                                resp = (new ContainerManager(context)).delete(container.getName());
678 3d6041e8 Phillip Toohill
                                Log.v(LOG, "container's name " + container.getName());
679 3d6041e8 Phillip Toohill
                        } catch (CloudServersException e) {
680 3d6041e8 Phillip Toohill
                                exception = e;
681 3d6041e8 Phillip Toohill
                        }
682 3d6041e8 Phillip Toohill
                        return resp;
683 3d6041e8 Phillip Toohill
                }
684 240418be Phillip Toohill
685 3d6041e8 Phillip Toohill
                @Override
686 240418be Phillip Toohill
                protected void onPostExecute(HttpResponse response) {
687 3d6041e8 Phillip Toohill
                        if (response != null) {
688 3d6041e8 Phillip Toohill
                                int statusCode = response.getStatusLine().getStatusCode();
689 3d6041e8 Phillip Toohill
                                if (statusCode == 409) {
690 240418be Phillip Toohill
                                        showAlert("Error",
691 37a14877 Adam Menz
                                        "Container must be empty in order to delete");
692 3d6041e8 Phillip Toohill
                                }
693 3d6041e8 Phillip Toohill
                                if (statusCode == 204) {
694 3d6041e8 Phillip Toohill
                                        setResult(Activity.RESULT_OK);
695 37a14877 Adam Menz
                                        loadFiles();
696 3d6041e8 Phillip Toohill
                                        finish();
697 240418be Phillip Toohill
698 3d6041e8 Phillip Toohill
                                } else {
699 3d6041e8 Phillip Toohill
                                        CloudServersException cse = parseCloudServersException(response);
700 3d6041e8 Phillip Toohill
                                        if ("".equals(cse.getMessage())) {
701 240418be Phillip Toohill
                                                showAlert("Error",
702 37a14877 Adam Menz
                                                        "There was a problem deleting your container.");
703 3d6041e8 Phillip Toohill
                                        } else {
704 240418be Phillip Toohill
                                                showAlert("Error",
705 240418be Phillip Toohill
                                                                "There was a problem deleting your container: "
706 37a14877 Adam Menz
                                                                        + cse.getMessage());
707 3d6041e8 Phillip Toohill
                                        }
708 3d6041e8 Phillip Toohill
                                }
709 3d6041e8 Phillip Toohill
                        } else if (exception != null) {
710 240418be Phillip Toohill
                                showAlert("Error", "There was a problem deleting your server: "
711 240418be Phillip Toohill
                                                + exception.getMessage());
712 240418be Phillip Toohill
                        }
713 3d6041e8 Phillip Toohill
                }
714 240418be Phillip Toohill
        }
715 3d6041e8 Phillip Toohill
716 3d6041e8 Phillip Toohill
}