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