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