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