Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (12.8 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 3d6041e8 Phillip Toohill
7 3d6041e8 Phillip Toohill
import javax.xml.parsers.FactoryConfigurationError;
8 3d6041e8 Phillip Toohill
import javax.xml.parsers.ParserConfigurationException;
9 3d6041e8 Phillip Toohill
import javax.xml.parsers.SAXParser;
10 3d6041e8 Phillip Toohill
import javax.xml.parsers.SAXParserFactory;
11 3d6041e8 Phillip Toohill
12 3d6041e8 Phillip Toohill
import org.apache.http.HttpResponse;
13 3d6041e8 Phillip Toohill
import org.apache.http.client.ClientProtocolException;
14 3d6041e8 Phillip Toohill
import org.apache.http.impl.client.BasicResponseHandler;
15 3d6041e8 Phillip Toohill
import org.xml.sax.InputSource;
16 3d6041e8 Phillip Toohill
import org.xml.sax.SAXException;
17 3d6041e8 Phillip Toohill
import org.xml.sax.XMLReader;
18 3d6041e8 Phillip Toohill
19 3d6041e8 Phillip Toohill
import android.app.Activity;
20 3d6041e8 Phillip Toohill
import android.app.AlertDialog;
21 3d6041e8 Phillip Toohill
import android.app.Dialog;
22 3d6041e8 Phillip Toohill
import android.app.ListActivity;
23 b347d5e3 Chmouel Boudjnah
import android.content.Context;
24 3d6041e8 Phillip Toohill
import android.content.DialogInterface;
25 3d6041e8 Phillip Toohill
import android.content.Intent;
26 3d6041e8 Phillip Toohill
import android.os.AsyncTask;
27 3d6041e8 Phillip Toohill
import android.os.Bundle;
28 3d6041e8 Phillip Toohill
import android.util.Log;
29 3d6041e8 Phillip Toohill
import android.view.LayoutInflater;
30 3d6041e8 Phillip Toohill
import android.view.Menu;
31 3d6041e8 Phillip Toohill
import android.view.MenuInflater;
32 3d6041e8 Phillip Toohill
import android.view.MenuItem;
33 3d6041e8 Phillip Toohill
import android.view.View;
34 3d6041e8 Phillip Toohill
import android.view.ViewGroup;
35 3d6041e8 Phillip Toohill
import android.widget.ArrayAdapter;
36 3d6041e8 Phillip Toohill
import android.widget.ListView;
37 3d6041e8 Phillip Toohill
import android.widget.TextView;
38 3d6041e8 Phillip Toohill
39 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.Container;
40 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerManager;
41 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerObjectManager;
42 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.ContainerObjects;
43 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.CloudServersException;
44 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
45 3d6041e8 Phillip Toohill
46 240418be Phillip Toohill
/**
47 3d6041e8 Phillip Toohill
 * 
48 3d6041e8 Phillip Toohill
 * @author Phillip Toohill
49 240418be Phillip Toohill
 * 
50 3d6041e8 Phillip Toohill
 */
51 3d6041e8 Phillip Toohill
public class ContainerObjectsActivity extends ListActivity {
52 3d6041e8 Phillip Toohill
53 3d6041e8 Phillip Toohill
        private static final int deleteContainer = 0;
54 3d6041e8 Phillip Toohill
        private ContainerObjects[] files;
55 3d6041e8 Phillip Toohill
        private static Container container;
56 3d6041e8 Phillip Toohill
        public String LOG = "viewFilesActivity";
57 8b432514 Phillip Toohill
        private String cdnEnabledIs;
58 6864568a Phillip Toohill
        public Object megaBytes;
59 6864568a Phillip Toohill
        public Object kiloBytes;
60 6864568a Phillip Toohill
        public int bConver = 1048576;
61 6864568a Phillip Toohill
        public int kbConver = 1024;
62 b347d5e3 Chmouel Boudjnah
        private Context context;
63 c16dc9e7 Adam Menz
        private String currentPath;
64 240418be Phillip Toohill
65 3d6041e8 Phillip Toohill
        @Override
66 240418be Phillip Toohill
        public void onCreate(Bundle savedInstanceState) {
67 240418be Phillip Toohill
                super.onCreate(savedInstanceState);
68 240418be Phillip Toohill
                container = (Container) this.getIntent().getExtras().get("container");
69 4daf0073 Phillip Toohill
                Log.v(LOG, "CDNEnabled:" + container.isCdnEnabled());
70 b347d5e3 Chmouel Boudjnah
        context = getApplicationContext();
71 240418be Phillip Toohill
                if (container.isCdnEnabled() == true) {
72 240418be Phillip Toohill
                        cdnEnabledIs = "true";
73 240418be Phillip Toohill
                } else {
74 240418be Phillip Toohill
                        cdnEnabledIs = "false";
75 240418be Phillip Toohill
                }
76 c16dc9e7 Adam Menz
                getPath();
77 240418be Phillip Toohill
                restoreState(savedInstanceState);
78 240418be Phillip Toohill
        }
79 c16dc9e7 Adam Menz
        
80 3d6041e8 Phillip Toohill
        @Override
81 3d6041e8 Phillip Toohill
        protected void onSaveInstanceState(Bundle outState) {
82 3d6041e8 Phillip Toohill
                super.onSaveInstanceState(outState);
83 3d6041e8 Phillip Toohill
                outState.putSerializable("container", files);
84 c16dc9e7 Adam Menz
                outState.putString("path", currentPath);
85 3d6041e8 Phillip Toohill
        }
86 3d6041e8 Phillip Toohill
87 240418be Phillip Toohill
        private void restoreState(Bundle state) {
88 240418be Phillip Toohill
                if (state != null && state.containsKey("container")) {
89 240418be Phillip Toohill
                        files = (ContainerObjects[]) state.getSerializable("container");
90 240418be Phillip Toohill
                        if (files.length == 0) {
91 240418be Phillip Toohill
                                displayNoServersCell();
92 240418be Phillip Toohill
                        } else {
93 240418be Phillip Toohill
                                getListView().setDividerHeight(1); // restore divider lines
94 240418be Phillip Toohill
                                setListAdapter(new FileAdapter());
95 240418be Phillip Toohill
                        }
96 c16dc9e7 Adam Menz
                } 
97 c16dc9e7 Adam Menz
                else {
98 240418be Phillip Toohill
                        loadFiles();
99 240418be Phillip Toohill
                }
100 240418be Phillip Toohill
        }
101 240418be Phillip Toohill
102 3d6041e8 Phillip Toohill
        protected void onListItemClick(ListView l, View v, int position, long id) {
103 240418be Phillip Toohill
                if (files != null && files.length > 0) {
104 c16dc9e7 Adam Menz
105 c16dc9e7 Adam Menz
                        Intent viewIntent;
106 c16dc9e7 Adam Menz
107 c16dc9e7 Adam Menz
                        if(files[position].getContentType().equals("application/directory")){
108 c16dc9e7 Adam Menz
                                viewIntent = new Intent(this, ContainerObjectsActivity.class);
109 c16dc9e7 Adam Menz
                                viewIntent.putExtra("container", container);
110 c16dc9e7 Adam Menz
                                viewIntent.putExtra("cdnUrl", container.getCdnUrl());
111 c16dc9e7 Adam Menz
                                viewIntent.putExtra("containerNames", container.getName());
112 c16dc9e7 Adam Menz
                                viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
113 c16dc9e7 Adam Menz
                                viewIntent.putExtra("path", files[position].getCName());
114 c16dc9e7 Adam Menz
                                Log.d("the path name is: ","captin " + files[position].getCName());
115 c16dc9e7 Adam Menz
                                startActivity(viewIntent);
116 c16dc9e7 Adam Menz
                        }
117 c16dc9e7 Adam Menz
118 c16dc9e7 Adam Menz
119 c16dc9e7 Adam Menz
                        else{
120 c16dc9e7 Adam Menz
                                viewIntent = new Intent(this, ContainerObjectDetails.class);
121 c16dc9e7 Adam Menz
                                viewIntent.putExtra("container", files[position]);
122 c16dc9e7 Adam Menz
                                viewIntent.putExtra("cdnUrl", container.getCdnUrl());
123 c16dc9e7 Adam Menz
                                viewIntent.putExtra("containerNames", container.getName());
124 c16dc9e7 Adam Menz
                                viewIntent.putExtra("isCdnEnabled", cdnEnabledIs);
125 c16dc9e7 Adam Menz
                                startActivityForResult(viewIntent, 55); // arbitrary number; never
126 c16dc9e7 Adam Menz
                                // used again
127 c16dc9e7 Adam Menz
                        }
128 240418be Phillip Toohill
                }
129 240418be Phillip Toohill
        }
130 240418be Phillip Toohill
131 c16dc9e7 Adam Menz
132 c16dc9e7 Adam Menz
        private void getPath(){
133 c16dc9e7 Adam Menz
                if(this.getIntent().getExtras().containsKey("path")){
134 c16dc9e7 Adam Menz
                        Log.d("contained path", "captin: " +  (String)this.getIntent().getExtras().get("path"));
135 c16dc9e7 Adam Menz
                        currentPath = (String)this.getIntent().getExtras().get("path");
136 c16dc9e7 Adam Menz
                }
137 c16dc9e7 Adam Menz
                else{
138 c16dc9e7 Adam Menz
                        Log.d("contained path", "captin no path");
139 c16dc9e7 Adam Menz
                        currentPath = "";
140 c16dc9e7 Adam Menz
                }
141 c16dc9e7 Adam Menz
                
142 c16dc9e7 Adam Menz
        }
143 c16dc9e7 Adam Menz
144 240418be Phillip Toohill
        private void loadFiles() {
145 240418be Phillip Toohill
                displayLoadingCell();
146 c16dc9e7 Adam Menz
                new LoadFilesTask().execute(currentPath);
147 240418be Phillip Toohill
148 240418be Phillip Toohill
        }
149 240418be Phillip Toohill
150 3d6041e8 Phillip Toohill
        private void setFileList(ArrayList<ContainerObjects> files) {
151 240418be Phillip Toohill
                if (files == null) {
152 240418be Phillip Toohill
                        files = new ArrayList<ContainerObjects>();
153 240418be Phillip Toohill
                }
154 240418be Phillip Toohill
                String[] fileNames = new String[files.size()];
155 240418be Phillip Toohill
                this.files = new ContainerObjects[files.size()];
156 240418be Phillip Toohill
157 240418be Phillip Toohill
                if (files != null) {
158 3d6041e8 Phillip Toohill
                        for (int i = 0; i < files.size(); i++) {
159 240418be Phillip Toohill
                                ContainerObjects file = files.get(i);
160 3d6041e8 Phillip Toohill
                                this.files[i] = file;
161 3d6041e8 Phillip Toohill
                                fileNames[i] = file.getName();
162 3d6041e8 Phillip Toohill
                        }
163 3d6041e8 Phillip Toohill
                }
164 240418be Phillip Toohill
165 3d6041e8 Phillip Toohill
                if (fileNames.length == 0) {
166 3d6041e8 Phillip Toohill
                        displayNoServersCell();
167 3d6041e8 Phillip Toohill
                } else {
168 240418be Phillip Toohill
                        getListView().setDividerHeight(1); // restore divider lines
169 3d6041e8 Phillip Toohill
                        setListAdapter(new FileAdapter());
170 3d6041e8 Phillip Toohill
                }
171 240418be Phillip Toohill
        }
172 240418be Phillip Toohill
173 240418be Phillip Toohill
        private void displayLoadingCell() {
174 240418be Phillip Toohill
                String a[] = new String[1];
175 240418be Phillip Toohill
                a[0] = "Loading...";
176 240418be Phillip Toohill
                setListAdapter(new ArrayAdapter<String>(this, R.layout.loadingcell,
177 240418be Phillip Toohill
                                R.id.loading_label, a));
178 240418be Phillip Toohill
                getListView().setTextFilterEnabled(true);
179 240418be Phillip Toohill
                getListView().setDividerHeight(0); // hide the dividers so it won't look
180 240418be Phillip Toohill
                                                                                        // like a list row
181 240418be Phillip Toohill
                getListView().setItemsCanFocus(false);
182 240418be Phillip Toohill
        }
183 240418be Phillip Toohill
184 240418be Phillip Toohill
        private void displayNoServersCell() {
185 240418be Phillip Toohill
                String a[] = new String[1];
186 240418be Phillip Toohill
                a[0] = "No Files";
187 240418be Phillip Toohill
                setListAdapter(new ArrayAdapter<String>(this, R.layout.noobjectscell,
188 240418be Phillip Toohill
                                R.id.no_files_label, a));
189 240418be Phillip Toohill
                getListView().setTextFilterEnabled(true);
190 240418be Phillip Toohill
                getListView().setDividerHeight(0); // hide the dividers so it won't look
191 240418be Phillip Toohill
                                                                                        // like a list row
192 240418be Phillip Toohill
                getListView().setItemsCanFocus(false);
193 240418be Phillip Toohill
        }
194 240418be Phillip Toohill
195 240418be Phillip Toohill
        private void showAlert(String title, String message) {
196 240418be Phillip Toohill
                // Can't create handler inside thread that has not called
197 240418be Phillip Toohill
                // Looper.prepare()
198 240418be Phillip Toohill
                // Looper.prepare();
199 240418be Phillip Toohill
                try {
200 240418be Phillip Toohill
                        AlertDialog alert = new AlertDialog.Builder(this).create();
201 240418be Phillip Toohill
                        alert.setTitle(title);
202 240418be Phillip Toohill
                        alert.setMessage(message);
203 240418be Phillip Toohill
                        alert.setButton("OK", new DialogInterface.OnClickListener() {
204 240418be Phillip Toohill
                                public void onClick(DialogInterface dialog, int which) {
205 240418be Phillip Toohill
                                        return;
206 240418be Phillip Toohill
                                }
207 240418be Phillip Toohill
                        });
208 240418be Phillip Toohill
                        alert.show();
209 240418be Phillip Toohill
                } catch (Exception e) {
210 240418be Phillip Toohill
                        e.printStackTrace();
211 240418be Phillip Toohill
                }
212 240418be Phillip Toohill
        }
213 240418be Phillip Toohill
214 240418be Phillip Toohill
        private class LoadFilesTask extends
215 c16dc9e7 Adam Menz
                        AsyncTask<String, Void, ArrayList<ContainerObjects>> {
216 240418be Phillip Toohill
217 240418be Phillip Toohill
                private CloudServersException exception;
218 240418be Phillip Toohill
219 3d6041e8 Phillip Toohill
                @Override
220 c16dc9e7 Adam Menz
                protected ArrayList<ContainerObjects> doInBackground(String... path) {
221 3d6041e8 Phillip Toohill
                        ArrayList<ContainerObjects> files = null;
222 3d6041e8 Phillip Toohill
                        try {
223 b347d5e3 Chmouel Boudjnah
                                files = (new ContainerObjectManager(context)).createList(true,
224 c16dc9e7 Adam Menz
                                                container.getName(), path[0]);
225 3d6041e8 Phillip Toohill
                        } catch (CloudServersException e) {
226 3d6041e8 Phillip Toohill
                                exception = e;
227 3d6041e8 Phillip Toohill
                                e.printStackTrace();
228 3d6041e8 Phillip Toohill
                        }
229 3d6041e8 Phillip Toohill
                        return files;
230 3d6041e8 Phillip Toohill
                }
231 240418be Phillip Toohill
232 3d6041e8 Phillip Toohill
                @Override
233 3d6041e8 Phillip Toohill
                protected void onPostExecute(ArrayList<ContainerObjects> result) {
234 3d6041e8 Phillip Toohill
                        if (exception != null) {
235 3d6041e8 Phillip Toohill
                                showAlert("Error", exception.getMessage());
236 240418be Phillip Toohill
                        }
237 3d6041e8 Phillip Toohill
                        setFileList(result);
238 3d6041e8 Phillip Toohill
                }
239 240418be Phillip Toohill
        }
240 240418be Phillip Toohill
241 240418be Phillip Toohill
        class FileAdapter extends ArrayAdapter<ContainerObjects> {
242 3d6041e8 Phillip Toohill
                FileAdapter() {
243 240418be Phillip Toohill
                        super(ContainerObjectsActivity.this,
244 240418be Phillip Toohill
                                        R.layout.listcontainerobjectcell, files);
245 3d6041e8 Phillip Toohill
                }
246 240418be Phillip Toohill
247 3d6041e8 Phillip Toohill
                public View getView(int position, View convertView, ViewGroup parent) {
248 240418be Phillip Toohill
249 3d6041e8 Phillip Toohill
                        ContainerObjects file = files[position];
250 3d6041e8 Phillip Toohill
                        LayoutInflater inflater = getLayoutInflater();
251 240418be Phillip Toohill
                        View row = inflater.inflate(R.layout.listcontainerobjectcell,
252 240418be Phillip Toohill
                                        parent, false);
253 3d6041e8 Phillip Toohill
254 3d6041e8 Phillip Toohill
                        TextView label = (TextView) row.findViewById(R.id.label);
255 c16dc9e7 Adam Menz
                        //label.setText(file.getCName());
256 c16dc9e7 Adam Menz
                        label.setText(getShortName(file.getCName()));
257 6864568a Phillip Toohill
258 6864568a Phillip Toohill
                        if (file.getBytes() >= bConver) {
259 240418be Phillip Toohill
                                megaBytes = Math.abs(file.getBytes() / bConver + 0.2);
260 240418be Phillip Toohill
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
261 240418be Phillip Toohill
                                sublabel.setText(megaBytes + " MB");
262 240418be Phillip Toohill
                        } else if (file.getBytes() >= kbConver) {
263 240418be Phillip Toohill
                                kiloBytes = Math.abs(file.getBytes() / kbConver + 0.2);
264 240418be Phillip Toohill
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
265 240418be Phillip Toohill
                                sublabel.setText(kiloBytes + " KB");
266 6864568a Phillip Toohill
                        } else {
267 240418be Phillip Toohill
                                TextView sublabel = (TextView) row.findViewById(R.id.sublabel);
268 240418be Phillip Toohill
                                sublabel.setText(file.getBytes() + " B");
269 6864568a Phillip Toohill
                        }
270 240418be Phillip Toohill
271 240418be Phillip Toohill
                        return (row);
272 3d6041e8 Phillip Toohill
                }
273 240418be Phillip Toohill
        }
274 c16dc9e7 Adam Menz
        
275 c16dc9e7 Adam Menz
        /* just get the last part of the filename
276 c16dc9e7 Adam Menz
         * so the entire file path does not show
277 c16dc9e7 Adam Menz
        */
278 c16dc9e7 Adam Menz
        private String getShortName(String longName){
279 c16dc9e7 Adam Menz
                String s = longName;
280 c16dc9e7 Adam Menz
                if(!s.contains("/")){
281 c16dc9e7 Adam Menz
                        return s;
282 c16dc9e7 Adam Menz
                }
283 c16dc9e7 Adam Menz
                else {
284 c16dc9e7 Adam Menz
                        return s.substring(s.lastIndexOf('/')+1);
285 c16dc9e7 Adam Menz
                }
286 c16dc9e7 Adam Menz
        }
287 240418be Phillip Toohill
288 240418be Phillip Toohill
        // Create the Menu options
289 240418be Phillip Toohill
        @Override
290 240418be Phillip Toohill
        public boolean onCreateOptionsMenu(Menu menu) {
291 3d6041e8 Phillip Toohill
                super.onCreateOptionsMenu(menu);
292 3d6041e8 Phillip Toohill
                MenuInflater inflater = getMenuInflater();
293 3d6041e8 Phillip Toohill
                inflater.inflate(R.menu.view_container_object_list_menu, menu);
294 3d6041e8 Phillip Toohill
                return true;
295 240418be Phillip Toohill
        }
296 240418be Phillip Toohill
297 240418be Phillip Toohill
        @Override
298 240418be Phillip Toohill
        public boolean onOptionsItemSelected(MenuItem item) {
299 3d6041e8 Phillip Toohill
                switch (item.getItemId()) {
300 3d6041e8 Phillip Toohill
                case R.id.delete_container:
301 3d6041e8 Phillip Toohill
                        showDialog(deleteContainer);
302 3d6041e8 Phillip Toohill
                        return true;
303 3d6041e8 Phillip Toohill
                case R.id.enable_cdn:
304 3d6041e8 Phillip Toohill
                        Intent viewIntent1 = new Intent(this, EnableCDNActivity.class);
305 240418be Phillip Toohill
                        viewIntent1.putExtra("Cname", container.getName());
306 240418be Phillip Toohill
                        startActivityForResult(viewIntent1, 56);
307 3d6041e8 Phillip Toohill
                        return true;
308 3d6041e8 Phillip Toohill
                case R.id.refresh:
309 3d6041e8 Phillip Toohill
                        loadFiles();
310 240418be Phillip Toohill
                        return true;
311 3d6041e8 Phillip Toohill
                }
312 3d6041e8 Phillip Toohill
                return false;
313 240418be Phillip Toohill
        }
314 240418be Phillip Toohill
315 3d6041e8 Phillip Toohill
        @Override
316 240418be Phillip Toohill
        protected Dialog onCreateDialog(int id) {
317 240418be Phillip Toohill
                switch (id) {
318 240418be Phillip Toohill
                case deleteContainer:
319 240418be Phillip Toohill
                        return new AlertDialog.Builder(ContainerObjectsActivity.this)
320 240418be Phillip Toohill
                                        .setIcon(R.drawable.alert_dialog_icon)
321 240418be Phillip Toohill
                                        .setTitle("Delete Container")
322 240418be Phillip Toohill
                                        .setMessage(
323 240418be Phillip Toohill
                                                        "Are you sure you want to delete this Container?")
324 240418be Phillip Toohill
                                        .setPositiveButton("Delete Container",
325 240418be Phillip Toohill
                                                        new DialogInterface.OnClickListener() {
326 240418be Phillip Toohill
                                                                public void onClick(DialogInterface dialog,
327 240418be Phillip Toohill
                                                                                int whichButton) {
328 240418be Phillip Toohill
                                                                        // User clicked OK so do some stuff
329 240418be Phillip Toohill
                                                                        new DeleteContainerTask()
330 240418be Phillip Toohill
                                                                                        .execute((Void[]) null);
331 240418be Phillip Toohill
                                                                }
332 240418be Phillip Toohill
                                                        })
333 240418be Phillip Toohill
                                        .setNegativeButton("Cancel",
334 240418be Phillip Toohill
                                                        new DialogInterface.OnClickListener() {
335 240418be Phillip Toohill
                                                                public void onClick(DialogInterface dialog,
336 240418be Phillip Toohill
                                                                                int whichButton) {
337 240418be Phillip Toohill
                                                                        // User clicked Cancel so do some stuff
338 240418be Phillip Toohill
                                                                }
339 240418be Phillip Toohill
                                                        }).create();
340 240418be Phillip Toohill
341 240418be Phillip Toohill
                }
342 240418be Phillip Toohill
                return null;
343 3d6041e8 Phillip Toohill
        }
344 3d6041e8 Phillip Toohill
345 240418be Phillip Toohill
        @Override
346 240418be Phillip Toohill
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
347 240418be Phillip Toohill
                super.onActivityResult(requestCode, resultCode, data);
348 240418be Phillip Toohill
349 240418be Phillip Toohill
                if (resultCode == RESULT_OK) {
350 240418be Phillip Toohill
                        // a sub-activity kicked back, so we want to refresh the server list
351 240418be Phillip Toohill
                        loadFiles();
352 240418be Phillip Toohill
                }
353 240418be Phillip Toohill
                if (requestCode == 56) {
354 240418be Phillip Toohill
                        if (resultCode == RESULT_OK) {
355 4daf0073 Phillip Toohill
                                Intent viewIntent1 = new Intent(this,
356 4daf0073 Phillip Toohill
                                                ListContainerActivity.class);
357 4daf0073 Phillip Toohill
                                startActivityForResult(viewIntent1, 56);
358 3d6041e8 Phillip Toohill
                        }
359 240418be Phillip Toohill
                }
360 240418be Phillip Toohill
        }
361 240418be Phillip Toohill
362 240418be Phillip Toohill
        private CloudServersException parseCloudServersException(
363 240418be Phillip Toohill
                        HttpResponse response) {
364 240418be Phillip Toohill
                CloudServersException cse = new CloudServersException();
365 240418be Phillip Toohill
                try {
366 240418be Phillip Toohill
                        BasicResponseHandler responseHandler = new BasicResponseHandler();
367 240418be Phillip Toohill
                        String body = responseHandler.handleResponse(response);
368 240418be Phillip Toohill
                        CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
369 240418be Phillip Toohill
                        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
370 240418be Phillip Toohill
                        XMLReader xmlReader = saxParser.getXMLReader();
371 240418be Phillip Toohill
                        xmlReader.setContentHandler(parser);
372 240418be Phillip Toohill
                        xmlReader.parse(new InputSource(new StringReader(body)));
373 240418be Phillip Toohill
                        cse = parser.getException();
374 240418be Phillip Toohill
                } catch (ClientProtocolException e) {
375 240418be Phillip Toohill
                        cse = new CloudServersException();
376 240418be Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
377 240418be Phillip Toohill
                } catch (IOException e) {
378 240418be Phillip Toohill
                        cse = new CloudServersException();
379 240418be Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
380 240418be Phillip Toohill
                } catch (ParserConfigurationException e) {
381 240418be Phillip Toohill
                        cse = new CloudServersException();
382 240418be Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
383 240418be Phillip Toohill
                } catch (SAXException e) {
384 240418be Phillip Toohill
                        cse = new CloudServersException();
385 240418be Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
386 240418be Phillip Toohill
                } catch (FactoryConfigurationError e) {
387 240418be Phillip Toohill
                        cse = new CloudServersException();
388 240418be Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
389 240418be Phillip Toohill
                }
390 240418be Phillip Toohill
                return cse;
391 240418be Phillip Toohill
        }
392 240418be Phillip Toohill
393 240418be Phillip Toohill
        private class DeleteContainerTask extends
394 240418be Phillip Toohill
                        AsyncTask<Void, Void, HttpResponse> {
395 240418be Phillip Toohill
396 3d6041e8 Phillip Toohill
                private CloudServersException exception;
397 3d6041e8 Phillip Toohill
398 3d6041e8 Phillip Toohill
                @Override
399 3d6041e8 Phillip Toohill
                protected HttpResponse doInBackground(Void... arg0) {
400 3d6041e8 Phillip Toohill
                        HttpResponse resp = null;
401 3d6041e8 Phillip Toohill
                        try {
402 b347d5e3 Chmouel Boudjnah
                                resp = (new ContainerManager(context)).delete(container.getName());
403 3d6041e8 Phillip Toohill
                                Log.v(LOG, "container's name " + container.getName());
404 3d6041e8 Phillip Toohill
                        } catch (CloudServersException e) {
405 3d6041e8 Phillip Toohill
                                exception = e;
406 3d6041e8 Phillip Toohill
                        }
407 3d6041e8 Phillip Toohill
                        return resp;
408 3d6041e8 Phillip Toohill
                }
409 240418be Phillip Toohill
410 3d6041e8 Phillip Toohill
                @Override
411 240418be Phillip Toohill
                protected void onPostExecute(HttpResponse response) {
412 3d6041e8 Phillip Toohill
                        if (response != null) {
413 3d6041e8 Phillip Toohill
                                int statusCode = response.getStatusLine().getStatusCode();
414 3d6041e8 Phillip Toohill
                                if (statusCode == 409) {
415 240418be Phillip Toohill
                                        showAlert("Error",
416 240418be Phillip Toohill
                                                        "Container must be empty in order to delete");
417 3d6041e8 Phillip Toohill
                                }
418 3d6041e8 Phillip Toohill
                                if (statusCode == 204) {
419 3d6041e8 Phillip Toohill
                                        setResult(Activity.RESULT_OK);
420 3d6041e8 Phillip Toohill
                                        finish();
421 240418be Phillip Toohill
422 3d6041e8 Phillip Toohill
                                } else {
423 3d6041e8 Phillip Toohill
                                        CloudServersException cse = parseCloudServersException(response);
424 3d6041e8 Phillip Toohill
                                        if ("".equals(cse.getMessage())) {
425 240418be Phillip Toohill
                                                showAlert("Error",
426 240418be Phillip Toohill
                                                                "There was a problem deleting your container.");
427 3d6041e8 Phillip Toohill
                                        } else {
428 240418be Phillip Toohill
                                                showAlert("Error",
429 240418be Phillip Toohill
                                                                "There was a problem deleting your container: "
430 240418be Phillip Toohill
                                                                                + cse.getMessage());
431 3d6041e8 Phillip Toohill
                                        }
432 3d6041e8 Phillip Toohill
                                }
433 3d6041e8 Phillip Toohill
                        } else if (exception != null) {
434 240418be Phillip Toohill
                                showAlert("Error", "There was a problem deleting your server: "
435 240418be Phillip Toohill
                                                + exception.getMessage());
436 240418be Phillip Toohill
                        }
437 3d6041e8 Phillip Toohill
                }
438 240418be Phillip Toohill
        }
439 3d6041e8 Phillip Toohill
440 3d6041e8 Phillip Toohill
}