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