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