root / src / com / rackspacecloud / android / ContainerObjectDetails.java @ 006434d8
History | View | Annotate | Download (16.7 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.io.BufferedOutputStream; |
4 |
import java.io.File; |
5 |
import java.io.FileNotFoundException; |
6 |
import java.io.FileOutputStream; |
7 |
import java.io.IOException; |
8 |
import java.io.StringReader; |
9 |
import java.text.ParseException; |
10 |
import java.text.SimpleDateFormat; |
11 |
import java.util.Date; |
12 |
|
13 |
import javax.xml.parsers.FactoryConfigurationError; |
14 |
import javax.xml.parsers.ParserConfigurationException; |
15 |
import javax.xml.parsers.SAXParser; |
16 |
import javax.xml.parsers.SAXParserFactory; |
17 |
|
18 |
import org.apache.http.HttpEntity; |
19 |
import org.apache.http.HttpResponse; |
20 |
import org.apache.http.client.ClientProtocolException; |
21 |
import org.apache.http.impl.client.BasicResponseHandler; |
22 |
import org.apache.http.util.EntityUtils; |
23 |
import org.xml.sax.InputSource; |
24 |
import org.xml.sax.SAXException; |
25 |
import org.xml.sax.XMLReader; |
26 |
|
27 |
import android.app.Activity; |
28 |
import android.app.AlertDialog; |
29 |
import android.app.Dialog; |
30 |
import android.app.ProgressDialog; |
31 |
import android.content.Context; |
32 |
import android.content.DialogInterface; |
33 |
import android.content.Intent; |
34 |
import android.net.Uri; |
35 |
import android.os.AsyncTask; |
36 |
import android.os.Bundle; |
37 |
import android.os.Environment; |
38 |
import android.util.Log; |
39 |
import android.view.Menu; |
40 |
import android.view.MenuInflater; |
41 |
import android.view.MenuItem; |
42 |
import android.view.View; |
43 |
import android.widget.Button; |
44 |
import android.widget.TextView; |
45 |
|
46 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
47 |
import com.rackspace.cloud.files.api.client.ContainerObjects; |
48 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
49 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
50 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
51 |
|
52 |
|
53 |
/**
|
54 |
*
|
55 |
* @author Phillip Toohill
|
56 |
*
|
57 |
*/
|
58 |
|
59 |
public class ContainerObjectDetails extends Activity { |
60 |
|
61 |
private static final int deleteObject = 0; |
62 |
private final String DOWNLOAD_DIRECTORY = "/RackspaceCloud"; |
63 |
private ContainerObjects objects;
|
64 |
private String containerNames; |
65 |
private String cdnURL; |
66 |
private String cdnEnabled; |
67 |
public String LOG = "ViewObject"; |
68 |
private int bConver = 1048576; |
69 |
private int kbConver = 1024; |
70 |
private double megaBytes; |
71 |
private double kiloBytes; |
72 |
public Button previewButton; |
73 |
public Button downloadButton; |
74 |
public Context context; |
75 |
private ProgressDialog dialog;
|
76 |
private Boolean isDownloaded; |
77 |
|
78 |
/** Called when the activity is first created. */
|
79 |
@Override
|
80 |
public void onCreate(Bundle savedInstanceState) { |
81 |
super.onCreate(savedInstanceState);
|
82 |
|
83 |
context = getApplicationContext(); |
84 |
|
85 |
objects = (ContainerObjects) this.getIntent().getExtras().get("container"); |
86 |
containerNames = (String) this.getIntent().getExtras().get("containerNames"); |
87 |
cdnURL = (String) this.getIntent().getExtras().get("cdnUrl"); |
88 |
cdnEnabled = (String) this.getIntent().getExtras().get("isCdnEnabled"); |
89 |
|
90 |
|
91 |
setContentView(R.layout.viewobject); |
92 |
restoreState(savedInstanceState); |
93 |
|
94 |
} |
95 |
|
96 |
@Override
|
97 |
protected void onSaveInstanceState(Bundle outState) { |
98 |
super.onSaveInstanceState(outState);
|
99 |
outState.putSerializable("container", objects);
|
100 |
outState.putBoolean("isDownloaded", isDownloaded);
|
101 |
} |
102 |
|
103 |
private void restoreState(Bundle state) { |
104 |
if (state != null && state.containsKey("container")) { |
105 |
objects = (ContainerObjects) state.getSerializable("container");
|
106 |
} |
107 |
loadObjectData(); |
108 |
|
109 |
if ( cdnEnabled.equals("true")) { |
110 |
this.previewButton = (Button) findViewById(R.id.preview_button); |
111 |
previewButton.setOnClickListener(new MyOnClickListener());
|
112 |
} else {
|
113 |
this.previewButton = (Button) findViewById(R.id.preview_button); |
114 |
previewButton.setVisibility(View.GONE);
|
115 |
} |
116 |
|
117 |
if (state != null && state.containsKey("isDownloaded")){ |
118 |
isDownloaded = state.getBoolean("isDownloaded");
|
119 |
} |
120 |
else{
|
121 |
isDownloaded = fileIsDownloaded(); |
122 |
} |
123 |
this.downloadButton = (Button) findViewById(R.id.download_button); |
124 |
if ( isDownloaded ) {
|
125 |
downloadButton.setText("Open File");
|
126 |
} else {
|
127 |
downloadButton.setText("Download File");
|
128 |
} |
129 |
downloadButton.setOnClickListener(new MyOnClickListener());
|
130 |
} |
131 |
|
132 |
|
133 |
private void loadObjectData() { |
134 |
//Object Name
|
135 |
TextView name = (TextView) findViewById(R.id.view_container_name); |
136 |
name.setText(objects.getCName().toString()); |
137 |
|
138 |
//File size
|
139 |
if (objects.getBytes() >= bConver) {
|
140 |
megaBytes = Math.abs(objects.getBytes()/bConver + 0.2); |
141 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
142 |
sublabel.setText(megaBytes + " MB");
|
143 |
} else if (objects.getBytes() >= kbConver){ |
144 |
kiloBytes = Math.abs(objects.getBytes()/kbConver + 0.2); |
145 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
146 |
sublabel.setText(kiloBytes + " KB");
|
147 |
} else {
|
148 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
149 |
sublabel.setText(objects.getBytes() + " B");
|
150 |
} |
151 |
|
152 |
//Content Type
|
153 |
TextView cType = (TextView) findViewById(R.id.view_content_type); |
154 |
cType.setText(objects.getContentType().toString()); |
155 |
|
156 |
//Last Modification date
|
157 |
String strDate = objects.getLastMod();
|
158 |
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.ssssss"); |
159 |
Date dateStr = null; |
160 |
try {
|
161 |
dateStr = formatter.parse(strDate); |
162 |
} catch (ParseException e1) { |
163 |
e1.printStackTrace(); |
164 |
} |
165 |
String formattedDate = formatter.format(dateStr);
|
166 |
Date date1 = null; |
167 |
try {
|
168 |
date1 = formatter.parse(formattedDate); |
169 |
} catch (ParseException e) { |
170 |
e.printStackTrace(); |
171 |
} |
172 |
formatter = new SimpleDateFormat("MMM-dd-yyyy"); |
173 |
formattedDate = formatter.format(date1); |
174 |
TextView lastmod = (TextView) findViewById(R.id.view_file_modification); |
175 |
lastmod.setText(formattedDate); |
176 |
|
177 |
} |
178 |
|
179 |
private class MyOnClickListener implements View.OnClickListener { |
180 |
@Override
|
181 |
public void onClick(View v) { |
182 |
if(v.equals(findViewById(R.id.preview_button))){
|
183 |
Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(cdnURL + "/" + objects.getCName())); |
184 |
startActivity(viewIntent); |
185 |
} |
186 |
/*
|
187 |
* need to perform different functions based on if
|
188 |
* the file is in the devices filesystem
|
189 |
*/
|
190 |
if(v.equals(findViewById(R.id.download_button))){
|
191 |
if(!isDownloaded){
|
192 |
if(storageIsReady()){
|
193 |
new ContainerObjectDownloadTask().execute();
|
194 |
} |
195 |
else{
|
196 |
showAlert("Error", "Storage not found."); |
197 |
} |
198 |
} |
199 |
else{
|
200 |
openFile(); |
201 |
} |
202 |
} |
203 |
} |
204 |
} |
205 |
|
206 |
private void showAlert(String title, String message) { |
207 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
208 |
alert.setTitle(title); |
209 |
alert.setMessage(message); |
210 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
211 |
public void onClick(DialogInterface dialog, int which) { |
212 |
return;
|
213 |
} }); |
214 |
alert.show(); |
215 |
} |
216 |
//Create the Menu options
|
217 |
@Override
|
218 |
public boolean onCreateOptionsMenu(Menu menu) { |
219 |
super.onCreateOptionsMenu(menu);
|
220 |
MenuInflater inflater = getMenuInflater(); |
221 |
inflater.inflate(R.menu.container_object_list_menu, menu); |
222 |
return true; |
223 |
} |
224 |
|
225 |
@Override
|
226 |
public boolean onOptionsItemSelected(MenuItem item) { |
227 |
switch (item.getItemId()) {
|
228 |
case R.id.delete_object:
|
229 |
showDialog(deleteObject); |
230 |
return true; |
231 |
case R.id.refresh:
|
232 |
loadObjectData(); |
233 |
return true; |
234 |
} |
235 |
return false; |
236 |
} |
237 |
|
238 |
@Override
|
239 |
protected Dialog onCreateDialog(int id ) { |
240 |
switch (id) {
|
241 |
case deleteObject:
|
242 |
return new AlertDialog.Builder(ContainerObjectDetails.this) |
243 |
.setIcon(R.drawable.alert_dialog_icon) |
244 |
.setTitle("Delete File")
|
245 |
.setMessage("Are you sure you want to delete this file?")
|
246 |
.setPositiveButton("Delete File", new DialogInterface.OnClickListener() { |
247 |
public void onClick(DialogInterface dialog, int whichButton) { |
248 |
// User clicked OK so do some stuff
|
249 |
new ContainerObjectDeleteTask().execute((Void[]) null); |
250 |
} |
251 |
}) |
252 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
253 |
public void onClick(DialogInterface dialog, int whichButton) { |
254 |
// User clicked Cancel so do some stuff
|
255 |
} |
256 |
}) |
257 |
.create(); |
258 |
} |
259 |
return null; |
260 |
} |
261 |
/**
|
262 |
* @return the file
|
263 |
*/
|
264 |
public ContainerObjects getViewFile() {
|
265 |
return objects;
|
266 |
} |
267 |
|
268 |
/**
|
269 |
* @param File the file to set
|
270 |
*/
|
271 |
public void setViewFile(ContainerObjects object) { |
272 |
this.objects = object;
|
273 |
} |
274 |
|
275 |
/*
|
276 |
* returns false if external storage is not avaliable
|
277 |
* (if its mounted, missing, read-only, etc)
|
278 |
* from: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
|
279 |
*/
|
280 |
private boolean storageIsReady(){ |
281 |
boolean mExternalStorageAvailable = false; |
282 |
boolean mExternalStorageWriteable = false; |
283 |
String state = Environment.getExternalStorageState();
|
284 |
|
285 |
if (Environment.MEDIA_MOUNTED.equals(state)) {
|
286 |
// We can read and write the media
|
287 |
mExternalStorageAvailable = mExternalStorageWriteable = true;
|
288 |
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { |
289 |
// We can only read the media
|
290 |
mExternalStorageAvailable = true;
|
291 |
mExternalStorageWriteable = false;
|
292 |
} else {
|
293 |
// Something else is wrong. It may be one of many other states, but all we need
|
294 |
// to know is we can neither read nor write
|
295 |
mExternalStorageAvailable = mExternalStorageWriteable = false;
|
296 |
} |
297 |
return mExternalStorageAvailable && mExternalStorageWriteable;
|
298 |
} |
299 |
|
300 |
private boolean fileIsDownloaded(){ |
301 |
if(storageIsReady()){
|
302 |
String fileName = Environment.getExternalStorageDirectory().getPath() + "/RackspaceCloud/" + objects.getCName(); |
303 |
File f = new File(fileName); |
304 |
return f.isFile();
|
305 |
} |
306 |
return false; |
307 |
} |
308 |
|
309 |
private void openFile(){ |
310 |
File object = new File(Environment.getExternalStorageDirectory().getPath() + "/RackspaceCloud/" + objects.getCName()); |
311 |
Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
|
312 |
File file = new File(object.getAbsolutePath()); |
313 |
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
|
314 |
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
315 |
Log.d("info", "captin the file name is " + object.getName()); |
316 |
Log.d("info", "captin the uri is " + Uri.fromFile(object).toString()); |
317 |
myIntent.setDataAndType(Uri.fromFile(file),mimetype); |
318 |
startActivity(myIntent); |
319 |
} |
320 |
|
321 |
private boolean writeFile(byte[] data){ |
322 |
String directoryName = Environment.getExternalStorageDirectory().getPath() + DOWNLOAD_DIRECTORY;
|
323 |
File f = new File(directoryName); |
324 |
|
325 |
if(!f.isDirectory()){
|
326 |
if(!f.mkdir()){
|
327 |
return false; |
328 |
} |
329 |
} |
330 |
|
331 |
String filename = directoryName + "/" + objects.getCName(); |
332 |
File object = new File(filename); |
333 |
BufferedOutputStream bos = null; |
334 |
|
335 |
try{
|
336 |
FileOutputStream fos = new FileOutputStream(object); |
337 |
bos = new BufferedOutputStream(fos); |
338 |
bos.write(data); |
339 |
} |
340 |
catch(FileNotFoundException e){ |
341 |
e.printStackTrace(); |
342 |
} |
343 |
catch(IOException e){ |
344 |
e.printStackTrace(); |
345 |
} |
346 |
finally{
|
347 |
if(bos != null){ |
348 |
try {
|
349 |
bos.flush(); |
350 |
bos.close(); |
351 |
} catch (IOException e) { |
352 |
// TODO Auto-generated catch block
|
353 |
e.printStackTrace(); |
354 |
} |
355 |
} |
356 |
} |
357 |
return true; |
358 |
} |
359 |
|
360 |
//Task's
|
361 |
|
362 |
private CloudServersException parseCloudServersException(HttpResponse response) {
|
363 |
CloudServersException cse = new CloudServersException();
|
364 |
try {
|
365 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
366 |
String body = responseHandler.handleResponse(response);
|
367 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
368 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
369 |
XMLReader xmlReader = saxParser.getXMLReader(); |
370 |
xmlReader.setContentHandler(parser); |
371 |
xmlReader.parse(new InputSource(new StringReader(body))); |
372 |
cse = parser.getException(); |
373 |
} catch (ClientProtocolException e) {
|
374 |
cse = new CloudServersException();
|
375 |
cse.setMessage(e.getLocalizedMessage()); |
376 |
} catch (IOException e) { |
377 |
cse = new CloudServersException();
|
378 |
cse.setMessage(e.getLocalizedMessage()); |
379 |
} catch (ParserConfigurationException e) { |
380 |
cse = new CloudServersException();
|
381 |
cse.setMessage(e.getLocalizedMessage()); |
382 |
} catch (SAXException e) {
|
383 |
cse = new CloudServersException();
|
384 |
cse.setMessage(e.getLocalizedMessage()); |
385 |
} catch (FactoryConfigurationError e) { |
386 |
cse = new CloudServersException();
|
387 |
cse.setMessage(e.getLocalizedMessage()); |
388 |
} |
389 |
return cse;
|
390 |
} |
391 |
|
392 |
private void startFileError(String message, HttpBundle bundle){ |
393 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
|
394 |
viewIntent.putExtra("errorMessage", message);
|
395 |
viewIntent.putExtra("response", bundle.getResponseText());
|
396 |
viewIntent.putExtra("request", bundle.getCurlRequest());
|
397 |
startActivity(viewIntent); |
398 |
} |
399 |
|
400 |
private class ContainerObjectDeleteTask extends AsyncTask<Void, Void, HttpBundle> { |
401 |
|
402 |
private CloudServersException exception;
|
403 |
|
404 |
protected void onPreExecute(){ |
405 |
dialog = ProgressDialog.show(ContainerObjectDetails.this, "", "Deleting...", true); |
406 |
} |
407 |
|
408 |
@Override
|
409 |
protected HttpBundle doInBackground(Void... arg0) { |
410 |
HttpBundle bundle = null;
|
411 |
try {
|
412 |
bundle = (new ContainerObjectManager(context)).deleteObject(containerNames, objects.getCName() );
|
413 |
} catch (CloudServersException e) {
|
414 |
exception = e; |
415 |
} |
416 |
return bundle;
|
417 |
} |
418 |
|
419 |
@Override
|
420 |
protected void onPostExecute(HttpBundle bundle) { |
421 |
dialog.dismiss(); |
422 |
HttpResponse response = bundle.getResponse(); |
423 |
if (response != null) { |
424 |
int statusCode = response.getStatusLine().getStatusCode();
|
425 |
if (statusCode == 204) { |
426 |
setResult(Activity.RESULT_OK); |
427 |
finish(); |
428 |
|
429 |
} else {
|
430 |
CloudServersException cse = parseCloudServersException(response); |
431 |
if ("".equals(cse.getMessage())) { |
432 |
startFileError("There was a problem deleting your File.", bundle);
|
433 |
} else {
|
434 |
startFileError("There was a problem deleting your file: " + cse.getMessage(), bundle);
|
435 |
} |
436 |
} |
437 |
} else if (exception != null) { |
438 |
startFileError("There was a problem deleting your file: " + exception.getMessage(), bundle);
|
439 |
} |
440 |
} |
441 |
} |
442 |
|
443 |
private class ContainerObjectDownloadTask extends AsyncTask<Void, Void, HttpBundle> { |
444 |
|
445 |
private CloudServersException exception;
|
446 |
|
447 |
protected void onPreExecute(){ |
448 |
dialog = ProgressDialog.show(ContainerObjectDetails.this, "", "Downloading...", true); |
449 |
} |
450 |
|
451 |
@Override
|
452 |
protected HttpBundle doInBackground(Void... arg0) { |
453 |
HttpBundle bundle = null;
|
454 |
try {
|
455 |
bundle = (new ContainerObjectManager(context)).getObject(containerNames, objects.getCName());
|
456 |
} catch (CloudServersException e) {
|
457 |
exception = e; |
458 |
} |
459 |
return bundle;
|
460 |
} |
461 |
|
462 |
@Override
|
463 |
protected void onPostExecute(HttpBundle bundle) { |
464 |
dialog.dismiss(); |
465 |
HttpResponse response = bundle.getResponse(); |
466 |
if (response != null) { |
467 |
int statusCode = response.getStatusLine().getStatusCode();
|
468 |
if (statusCode == 200) { |
469 |
setResult(Activity.RESULT_OK); |
470 |
HttpEntity entity = response.getEntity(); |
471 |
|
472 |
try {
|
473 |
if(writeFile(EntityUtils.toByteArray(entity))){
|
474 |
downloadButton.setText("Open File");
|
475 |
isDownloaded = true;
|
476 |
} |
477 |
else{
|
478 |
showAlert("Error", "There was a problem downloading your file."); |
479 |
} |
480 |
|
481 |
} catch (IOException e) { |
482 |
showAlert("Error", "There was a problem downloading your file."); |
483 |
e.printStackTrace(); |
484 |
} |
485 |
|
486 |
} else {
|
487 |
CloudServersException cse = parseCloudServersException(response); |
488 |
if ("".equals(cse.getMessage())) { |
489 |
startFileError("There was a problem downloading your File.", bundle);
|
490 |
} else {
|
491 |
startFileError("There was a problem downloading your file: " + cse.getMessage(), bundle);
|
492 |
} |
493 |
} |
494 |
} else if (exception != null) { |
495 |
startFileError("There was a problem downloading your file: " + exception.getMessage(), bundle);
|
496 |
} |
497 |
} |
498 |
} |
499 |
|
500 |
} |