root / src / com / rackspace / cloud / android / ContainerObjectDetails.java @ 802fbd9e
History | View | Annotate | Download (21 kB)
1 |
package com.rackspace.cloud.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.util.ArrayList; |
9 |
import java.util.Iterator; |
10 |
import java.util.List; |
11 |
import java.util.Map.Entry; |
12 |
|
13 |
import org.apache.http.HttpEntity; |
14 |
import org.apache.http.HttpResponse; |
15 |
import org.apache.http.util.EntityUtils; |
16 |
|
17 |
import android.app.Activity; |
18 |
import android.app.AlertDialog; |
19 |
import android.app.Dialog; |
20 |
import android.content.DialogInterface; |
21 |
import android.content.Intent; |
22 |
import android.net.Uri; |
23 |
import android.os.AsyncTask; |
24 |
import android.os.Bundle; |
25 |
import android.os.Environment; |
26 |
import android.util.Log; |
27 |
import android.view.LayoutInflater; |
28 |
import android.view.Menu; |
29 |
import android.view.MenuInflater; |
30 |
import android.view.MenuItem; |
31 |
import android.view.View; |
32 |
import android.view.View.OnClickListener; |
33 |
import android.widget.Button; |
34 |
import android.widget.CheckBox; |
35 |
import android.widget.CompoundButton; |
36 |
import android.widget.CompoundButton.OnCheckedChangeListener; |
37 |
import android.widget.ArrayAdapter; |
38 |
import android.widget.LinearLayout; |
39 |
import android.widget.TabHost; |
40 |
import android.widget.TextView; |
41 |
import android.widget.Toast; |
42 |
|
43 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
44 |
import com.rackspace.cloud.files.api.client.ContainerObjects; |
45 |
import com.rackspace.cloud.files.api.client.ObjectVersion; |
46 |
import com.rackspace.cloud.files.api.client.Permission; |
47 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
48 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
49 |
|
50 |
/**
|
51 |
*
|
52 |
* @author Phillip Toohill
|
53 |
*
|
54 |
*/
|
55 |
|
56 |
public class ContainerObjectDetails extends CloudActivity { |
57 |
|
58 |
private static final int deleteObject = 0; |
59 |
private final String DOWNLOAD_DIRECTORY = "/PithosPlus"; |
60 |
|
61 |
private ContainerObjects objects;
|
62 |
private String containerNames; |
63 |
private String cdnURL; |
64 |
private String cdnEnabled; |
65 |
public String LOG = "ViewObject"; |
66 |
private int bConver = 1048576; |
67 |
private int kbConver = 1024; |
68 |
private double megaBytes; |
69 |
private double kiloBytes; |
70 |
public Button previewButton; |
71 |
public Button downloadButton; |
72 |
private Boolean isDownloaded; |
73 |
private AndroidCloudApplication app;
|
74 |
private DeleteObjectListenerTask deleteObjTask;
|
75 |
private DownloadObjectListenerTask downloadObjTask;
|
76 |
private List<ObjectVersion> versions=new ArrayList<ObjectVersion>(); |
77 |
|
78 |
/** Called when the activity is first created. */
|
79 |
@Override
|
80 |
public void onCreate(Bundle savedInstanceState) { |
81 |
super.onCreate(savedInstanceState);
|
82 |
trackPageView(GoogleAnalytics.PAGE_STORAGE_OBJECT); |
83 |
|
84 |
objects = (ContainerObjects) this.getIntent().getExtras().get("container"); |
85 |
containerNames = (String) this.getIntent().getExtras().get("containerNames"); |
86 |
cdnURL = (String) this.getIntent().getExtras().get("cdnUrl"); |
87 |
cdnEnabled = (String) this.getIntent().getExtras().get("isCdnEnabled"); |
88 |
|
89 |
setContentView(R.layout.viewobject); |
90 |
TabHost tabs = (TabHost)findViewById(R.id.tabhost2); |
91 |
|
92 |
tabs.setup(); |
93 |
|
94 |
TabHost.TabSpec spec = tabs.newTabSpec("tag1");
|
95 |
|
96 |
spec.setContent(R.id.details); |
97 |
spec.setIndicator("Details");
|
98 |
tabs.addTab(spec); |
99 |
|
100 |
spec = tabs.newTabSpec("tag2");
|
101 |
spec.setContent(R.id.metadata); |
102 |
spec.setIndicator("Metadata");
|
103 |
tabs.addTab(spec); |
104 |
|
105 |
spec = tabs.newTabSpec("tag3");
|
106 |
spec.setContent(R.id.sharing); |
107 |
spec.setIndicator("Sharing");
|
108 |
tabs.addTab(spec); |
109 |
|
110 |
spec = tabs.newTabSpec("tag4");
|
111 |
spec.setContent(R.id.versions); |
112 |
spec.setIndicator("Versions");
|
113 |
tabs.addTab(spec); |
114 |
|
115 |
restoreState(savedInstanceState); |
116 |
} |
117 |
|
118 |
@Override
|
119 |
protected void onSaveInstanceState(Bundle outState) { |
120 |
super.onSaveInstanceState(outState);
|
121 |
outState.putSerializable("container", objects);
|
122 |
outState.putBoolean("isDownloaded", isDownloaded);
|
123 |
} |
124 |
|
125 |
protected void restoreState(Bundle state) { |
126 |
super.restoreState(state);
|
127 |
/*
|
128 |
* need reference to the app so you can access curDirFiles
|
129 |
* as well as processing status
|
130 |
*/
|
131 |
app = (AndroidCloudApplication)this.getApplication();
|
132 |
|
133 |
if (state != null && state.containsKey("container")) { |
134 |
objects = (ContainerObjects) state.getSerializable("container");
|
135 |
} |
136 |
loadObjectData(); |
137 |
|
138 |
if ( cdnEnabled.equals("true")) { |
139 |
this.previewButton = (Button) findViewById(R.id.preview_button); |
140 |
previewButton.setOnClickListener(new MyOnClickListener());
|
141 |
} else {
|
142 |
this.previewButton = (Button) findViewById(R.id.preview_button); |
143 |
previewButton.setVisibility(View.GONE);
|
144 |
} |
145 |
|
146 |
if (state != null && state.containsKey("isDownloaded")){ |
147 |
isDownloaded = state.getBoolean("isDownloaded");
|
148 |
} |
149 |
else{
|
150 |
isDownloaded = fileIsDownloaded(); |
151 |
} |
152 |
this.downloadButton = (Button) findViewById(R.id.download_button); |
153 |
if ( isDownloaded ) {
|
154 |
downloadButton.setText("Open File");
|
155 |
} else {
|
156 |
downloadButton.setText("Download File");
|
157 |
} |
158 |
downloadButton.setOnClickListener(new MyOnClickListener());
|
159 |
|
160 |
if(app.isDeletingObject()){
|
161 |
deleteObjTask = new DeleteObjectListenerTask();
|
162 |
deleteObjTask.execute(); |
163 |
} |
164 |
|
165 |
if(app.isDownloadingObject()){
|
166 |
downloadObjTask = new DownloadObjectListenerTask();
|
167 |
downloadObjTask.execute(); |
168 |
} |
169 |
|
170 |
} |
171 |
|
172 |
|
173 |
|
174 |
@Override
|
175 |
protected void onStop(){ |
176 |
super.onStop();
|
177 |
|
178 |
/*
|
179 |
* Need to stop running listener task
|
180 |
* if we exit
|
181 |
*/
|
182 |
if(deleteObjTask != null){ |
183 |
deleteObjTask.cancel(true);
|
184 |
} |
185 |
|
186 |
if(downloadObjTask != null){ |
187 |
downloadObjTask.cancel(true);
|
188 |
} |
189 |
} |
190 |
|
191 |
|
192 |
private void loadObjectData() { |
193 |
//Object Name
|
194 |
TextView name = (TextView) findViewById(R.id.view_container_name); |
195 |
name.setText(objects.getCName().toString()); |
196 |
|
197 |
//File size
|
198 |
if (objects.getBytes() >= bConver) {
|
199 |
megaBytes = Math.abs(objects.getBytes()/bConver + 0.2); |
200 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
201 |
sublabel.setText(megaBytes + " MB");
|
202 |
} else if (objects.getBytes() >= kbConver){ |
203 |
kiloBytes = Math.abs(objects.getBytes()/kbConver + 0.2); |
204 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
205 |
sublabel.setText(kiloBytes + " KB");
|
206 |
} else {
|
207 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
208 |
sublabel.setText(objects.getBytes() + " B");
|
209 |
} |
210 |
|
211 |
//Content Type
|
212 |
TextView cType = (TextView) findViewById(R.id.view_content_type); |
213 |
cType.setText(objects.getContentType().toString()); |
214 |
|
215 |
//Last Modification date
|
216 |
String strDate = objects.getLastMod();
|
217 |
strDate = strDate.substring(0, strDate.indexOf('T')); |
218 |
|
219 |
TextView lastmod = (TextView) findViewById(R.id.view_file_modification); |
220 |
lastmod.setText(strDate); |
221 |
rebuildMetadataList(); |
222 |
rebuildPermissionList(); |
223 |
try {
|
224 |
versions = new ContainerObjectManager(getApplicationContext()).getObjectVersions(objects.getContainerName(), objects.getCName());
|
225 |
rebuildVersionList(); |
226 |
} catch (CloudServersException e) {
|
227 |
// TODO Auto-generated catch block
|
228 |
e.printStackTrace(); |
229 |
} |
230 |
} |
231 |
|
232 |
private void rebuildMetadataList(){ |
233 |
LayoutInflater layoutInflater = LayoutInflater.from(ContainerObjectDetails.this); |
234 |
final LinearLayout metadata = (LinearLayout) findViewById(R.id.metadataList);
|
235 |
if(metadata.getChildCount()>0) |
236 |
metadata.removeViews(0,metadata.getChildCount());
|
237 |
metadata.removeAllViews(); |
238 |
|
239 |
if(objects.getMetadata()!=null){ |
240 |
int i=0; |
241 |
Iterator<Entry<String,String>> it = objects.getMetadata().entrySet().iterator(); |
242 |
while (it.hasNext()) {
|
243 |
final Entry<String,String> perm = it.next(); |
244 |
final View v = layoutInflater.inflate(R.layout.metadatarow, null); |
245 |
populateMetadataList(perm, v, metadata, i); |
246 |
i++; |
247 |
} |
248 |
} |
249 |
} |
250 |
|
251 |
private void populateMetadataList(final Entry<String,String> metadata, final View v, final LinearLayout properties, int i){ |
252 |
properties.addView(v, i); |
253 |
Log.d(LOG,i+" "+metadata.getKey()+" "+metadata.getValue()); |
254 |
((TextView) v.findViewById(R.id.mkey)).setText(metadata.getKey()); |
255 |
((TextView) v.findViewById(R.id.mvalue)).setText(metadata.getValue()); |
256 |
} |
257 |
|
258 |
|
259 |
private void rebuildPermissionList(){ |
260 |
LayoutInflater layoutInflater = LayoutInflater.from(ContainerObjectDetails.this); |
261 |
final LinearLayout properties = (LinearLayout) findViewById(R.id.permissionsList);
|
262 |
if(properties.getChildCount()>0) |
263 |
properties.removeViews(0,properties.getChildCount());
|
264 |
properties.removeAllViews(); |
265 |
Iterator<Permission> it=null; |
266 |
if(objects.getPermissions()!=null){ |
267 |
it= objects.getPermissions().iterator(); |
268 |
int i = 0; |
269 |
while (it.hasNext()) {
|
270 |
final Permission perm = it.next(); |
271 |
final View v = layoutInflater.inflate(R.layout.propertiesrow, null); |
272 |
populatePermissionList(perm, v, properties, i); |
273 |
i++; |
274 |
} |
275 |
} |
276 |
} |
277 |
|
278 |
private void populatePermissionList(final Permission perm, final View v, final LinearLayout properties, int i){ |
279 |
|
280 |
|
281 |
properties.addView(v, i); |
282 |
|
283 |
|
284 |
|
285 |
((TextView) v.findViewById(R.id.ownerName)).setText(perm.getUser()); |
286 |
((CheckBox) v.findViewById(R.id.read)).setChecked(perm.isRead()); |
287 |
((CheckBox) v.findViewById(R.id.write)).setChecked(perm.isWrite()); |
288 |
|
289 |
((CheckBox) v.findViewById(R.id.read)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
290 |
|
291 |
@Override
|
292 |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
293 |
perm.setRead(isChecked); |
294 |
|
295 |
} |
296 |
}); |
297 |
((CheckBox) v.findViewById(R.id.write)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
298 |
|
299 |
@Override
|
300 |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { |
301 |
perm.setWrite(isChecked); |
302 |
|
303 |
} |
304 |
}); |
305 |
((Button) v.findViewById(R.id.remove)).setOnClickListener(new OnClickListener() { |
306 |
|
307 |
@Override
|
308 |
public void onClick(View v1) { |
309 |
properties.removeView(v); |
310 |
objects.getPermissions().remove(perm); |
311 |
|
312 |
} |
313 |
}); |
314 |
} |
315 |
|
316 |
private void rebuildVersionList() { |
317 |
LayoutInflater layoutInflater = LayoutInflater.from(ContainerObjectDetails.this); |
318 |
final LinearLayout properties = (LinearLayout) findViewById(R.id.versionsList);
|
319 |
if (properties.getChildCount() > 0) |
320 |
properties.removeViews(0, properties.getChildCount());
|
321 |
properties.removeAllViews(); |
322 |
Iterator<ObjectVersion> it;
|
323 |
//Collections.reverse(versions);
|
324 |
it = versions.iterator(); |
325 |
int i = 0; |
326 |
while (it.hasNext()) {
|
327 |
final ObjectVersion perm = it.next();
|
328 |
final View v = layoutInflater.inflate(R.layout.versionsrow, null); |
329 |
populateVersionList(perm, v, properties, i); |
330 |
i++; |
331 |
} |
332 |
} |
333 |
|
334 |
private void populateVersionList(final ObjectVersion perm, final View v, |
335 |
final LinearLayout properties, int i) { |
336 |
|
337 |
properties.addView(v, i); |
338 |
|
339 |
((TextView) v.findViewById(R.id.versionName)).setText("Version: "
|
340 |
+ perm.getVersion()); |
341 |
|
342 |
((TextView) v.findViewById(R.id.versionModified)).setText("Modified: "
|
343 |
+ perm.getDateString()); |
344 |
if(versions.size()==1){ |
345 |
//((Button) v.findViewById(R.id.vremove)).setVisibility(View.INVISIBLE);
|
346 |
((Button) v.findViewById(R.id.vrestore)).setVisibility(View.INVISIBLE); |
347 |
} |
348 |
/*
|
349 |
((Button) v.findViewById(R.id.vremove))
|
350 |
.setOnClickListener(new OnClickListener() {
|
351 |
|
352 |
@Override
|
353 |
public void onClick(View v1) {
|
354 |
Log.d("PERMS", perm.getUri());
|
355 |
try {
|
356 |
new GssHttpCommands(getDroidApplication()
|
357 |
.getUserDetails()).deleteFolder(perm.getUri()+"?version="+perm.getVersion());
|
358 |
} catch (SystemErrorException e) {
|
359 |
// TODO Auto-generated catch block
|
360 |
e.printStackTrace();
|
361 |
} catch (GssHttpException e) {
|
362 |
// TODO Auto-generated catch block
|
363 |
e.printStackTrace();
|
364 |
}
|
365 |
getFileTask().execute(res.getUri());
|
366 |
//properties.removeView(v);
|
367 |
|
368 |
}
|
369 |
});*/
|
370 |
((Button) v.findViewById(R.id.vrestore))
|
371 |
.setOnClickListener(new OnClickListener() {
|
372 |
|
373 |
@Override
|
374 |
public void onClick(View v1) { |
375 |
//Log.d("PERMS", perm.getUri());
|
376 |
//geRestoreVersionTask().execute(perm.getUri()+"?restoreVersion="+perm.getVersion());
|
377 |
|
378 |
} |
379 |
}); |
380 |
((Button) v.findViewById(R.id.vdownload))
|
381 |
.setOnClickListener(new OnClickListener() {
|
382 |
|
383 |
@Override
|
384 |
public void onClick(View v1) { |
385 |
|
386 |
//getDownloadTask().execute(perm.getUri()+"?version="+perm.getVersion());
|
387 |
//properties.removeView(v);
|
388 |
|
389 |
} |
390 |
}); |
391 |
} |
392 |
|
393 |
|
394 |
private class MyOnClickListener implements View.OnClickListener { |
395 |
@Override
|
396 |
public void onClick(View v) { |
397 |
if(v.equals(findViewById(R.id.preview_button))){
|
398 |
Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(cdnURL + "/" + objects.getCName())); |
399 |
startActivity(viewIntent); |
400 |
} |
401 |
/*
|
402 |
* need to perform different functions based on if
|
403 |
* the file is in the devices filesystem
|
404 |
*/
|
405 |
if(v.equals(findViewById(R.id.download_button))){
|
406 |
if(!isDownloaded){
|
407 |
if(storageIsReady()){
|
408 |
new ContainerObjectDownloadTask().execute();
|
409 |
} |
410 |
else{
|
411 |
showAlert("Error", "Storage not found."); |
412 |
} |
413 |
} |
414 |
else{
|
415 |
openFile(); |
416 |
} |
417 |
} |
418 |
} |
419 |
} |
420 |
|
421 |
//Create the Menu options
|
422 |
@Override
|
423 |
public boolean onCreateOptionsMenu(Menu menu) { |
424 |
super.onCreateOptionsMenu(menu);
|
425 |
MenuInflater inflater = getMenuInflater(); |
426 |
inflater.inflate(R.menu.container_object_list_menu, menu); |
427 |
return true; |
428 |
} |
429 |
|
430 |
@Override
|
431 |
public boolean onOptionsItemSelected(MenuItem item) { |
432 |
switch (item.getItemId()) {
|
433 |
case R.id.delete_object:
|
434 |
showDialog(deleteObject); |
435 |
return true; |
436 |
case R.id.refresh:
|
437 |
loadObjectData(); |
438 |
return true; |
439 |
} |
440 |
return false; |
441 |
} |
442 |
|
443 |
@Override
|
444 |
protected Dialog onCreateDialog(int id ) { |
445 |
switch (id) {
|
446 |
case deleteObject:
|
447 |
return new AlertDialog.Builder(ContainerObjectDetails.this) |
448 |
.setIcon(R.drawable.alert_dialog_icon) |
449 |
.setTitle("Delete File")
|
450 |
.setMessage("Are you sure you want to delete this file?")
|
451 |
.setPositiveButton("Delete File", new DialogInterface.OnClickListener() { |
452 |
public void onClick(DialogInterface dialog, int whichButton) { |
453 |
// User clicked OK so do some stuff
|
454 |
trackEvent(GoogleAnalytics.CATEGORY_FILE, GoogleAnalytics.EVENT_DELETE, "", -1); |
455 |
new ContainerObjectDeleteTask().execute((Void[]) null); |
456 |
} |
457 |
}) |
458 |
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { |
459 |
public void onClick(DialogInterface dialog, int whichButton) { |
460 |
// User clicked Cancel so do some stuff
|
461 |
} |
462 |
}) |
463 |
.create(); |
464 |
} |
465 |
return null; |
466 |
} |
467 |
/**
|
468 |
* @return the file
|
469 |
*/
|
470 |
public ContainerObjects getViewFile() {
|
471 |
return objects;
|
472 |
} |
473 |
|
474 |
/**
|
475 |
* @param File the file to set
|
476 |
*/
|
477 |
public void setViewFile(ContainerObjects object) { |
478 |
this.objects = object;
|
479 |
} |
480 |
|
481 |
/*
|
482 |
* returns false if external storage is not avaliable
|
483 |
* (if its mounted, missing, read-only, etc)
|
484 |
* from: http://developer.android.com/guide/topics/data/data-storage.html#filesExternal
|
485 |
*/
|
486 |
private boolean storageIsReady(){ |
487 |
boolean mExternalStorageAvailable = false; |
488 |
boolean mExternalStorageWriteable = false; |
489 |
String state = Environment.getExternalStorageState();
|
490 |
|
491 |
if (Environment.MEDIA_MOUNTED.equals(state)) {
|
492 |
// We can read and write the media
|
493 |
mExternalStorageAvailable = mExternalStorageWriteable = true;
|
494 |
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { |
495 |
// We can only read the media
|
496 |
mExternalStorageAvailable = true;
|
497 |
mExternalStorageWriteable = false;
|
498 |
} else {
|
499 |
// Something else is wrong. It may be one of many other states, but all we need
|
500 |
// to know is we can neither read nor write
|
501 |
mExternalStorageAvailable = mExternalStorageWriteable = false;
|
502 |
} |
503 |
return mExternalStorageAvailable && mExternalStorageWriteable;
|
504 |
} |
505 |
|
506 |
private boolean fileIsDownloaded(){ |
507 |
if(storageIsReady()){
|
508 |
String fileName = Environment.getExternalStorageDirectory().getPath() + "/PithosPlus/" + objects.getCName(); |
509 |
File f = new File(fileName); |
510 |
return f.isFile();
|
511 |
} |
512 |
return false; |
513 |
} |
514 |
|
515 |
private void openFile(){ |
516 |
File object = new File(Environment.getExternalStorageDirectory().getPath() + "/PithosPlus/" + objects.getCName()); |
517 |
Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
|
518 |
File file = new File(object.getAbsolutePath()); |
519 |
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString());
|
520 |
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
521 |
myIntent.setDataAndType(Uri.fromFile(file),mimetype); |
522 |
//myIntent.setData(Uri.fromFile(file));
|
523 |
try{
|
524 |
startActivity(myIntent); |
525 |
} |
526 |
catch(Exception e){ |
527 |
Toast.makeText(this, "Could not open file.", Toast.LENGTH_SHORT).show(); |
528 |
} |
529 |
} |
530 |
|
531 |
private boolean writeFile(byte[] data){ |
532 |
String directoryName = Environment.getExternalStorageDirectory().getPath() + DOWNLOAD_DIRECTORY;
|
533 |
File f = new File(directoryName); |
534 |
|
535 |
if(!f.isDirectory()){
|
536 |
if(!f.mkdir()){
|
537 |
return false; |
538 |
} |
539 |
} |
540 |
|
541 |
String filename = directoryName + "/" + objects.getCName(); |
542 |
File object = new File(filename); |
543 |
BufferedOutputStream bos = null; |
544 |
|
545 |
try{
|
546 |
FileOutputStream fos = new FileOutputStream(object); |
547 |
bos = new BufferedOutputStream(fos); |
548 |
bos.write(data); |
549 |
} |
550 |
catch(FileNotFoundException e){ |
551 |
e.printStackTrace(); |
552 |
} |
553 |
catch(IOException e){ |
554 |
e.printStackTrace(); |
555 |
} |
556 |
finally{
|
557 |
if(bos != null){ |
558 |
try {
|
559 |
bos.flush(); |
560 |
bos.close(); |
561 |
} catch (IOException e) { |
562 |
// TODO Auto-generated catch block
|
563 |
e.printStackTrace(); |
564 |
} |
565 |
} |
566 |
} |
567 |
return true; |
568 |
} |
569 |
|
570 |
//Task's
|
571 |
|
572 |
private class ContainerObjectDeleteTask extends AsyncTask<Void, Void, HttpBundle> { |
573 |
|
574 |
private CloudServersException exception;
|
575 |
|
576 |
protected void onPreExecute(){ |
577 |
showDialog(); |
578 |
app.setDeleteingObject(true);
|
579 |
deleteObjTask = new DeleteObjectListenerTask();
|
580 |
deleteObjTask.execute(); |
581 |
} |
582 |
|
583 |
@Override
|
584 |
protected HttpBundle doInBackground(Void... arg0) { |
585 |
HttpBundle bundle = null;
|
586 |
try {
|
587 |
bundle = (new ContainerObjectManager(getContext())).deleteObject(containerNames, objects.getCName() );
|
588 |
} catch (CloudServersException e) {
|
589 |
exception = e; |
590 |
} |
591 |
|
592 |
return bundle;
|
593 |
} |
594 |
|
595 |
@Override
|
596 |
protected void onPostExecute(HttpBundle bundle) { |
597 |
app.setDeleteingObject(false);
|
598 |
hideDialog(); |
599 |
HttpResponse response = bundle.getResponse(); |
600 |
if (response != null) { |
601 |
int statusCode = response.getStatusLine().getStatusCode();
|
602 |
if (statusCode == 204) { |
603 |
//handled by listener
|
604 |
} else {
|
605 |
CloudServersException cse = parseCloudServersException(response); |
606 |
if ("".equals(cse.getMessage())) { |
607 |
showError("There was a problem deleting your File.", bundle);
|
608 |
} else {
|
609 |
showError("There was a problem deleting your file: " + cse.getMessage(), bundle);
|
610 |
} |
611 |
} |
612 |
} else if (exception != null) { |
613 |
showError("There was a problem deleting your file: " + exception.getMessage(), bundle);
|
614 |
} |
615 |
} |
616 |
} |
617 |
|
618 |
private class ContainerObjectDownloadTask extends AsyncTask<Void, Void, HttpBundle> { |
619 |
|
620 |
private CloudServersException exception;
|
621 |
|
622 |
@Override
|
623 |
protected void onPreExecute(){ |
624 |
showDialog(); |
625 |
app.setDownloadingObject(true);
|
626 |
downloadObjTask = new DownloadObjectListenerTask();
|
627 |
downloadObjTask.execute(); |
628 |
} |
629 |
|
630 |
@Override
|
631 |
protected HttpBundle doInBackground(Void... arg0) { |
632 |
HttpBundle bundle = null;
|
633 |
try {
|
634 |
bundle = (new ContainerObjectManager(getContext())).getObject(containerNames, objects.getCName());
|
635 |
} catch (CloudServersException e) {
|
636 |
exception = e; |
637 |
} |
638 |
return bundle;
|
639 |
} |
640 |
|
641 |
@Override
|
642 |
protected void onPostExecute(HttpBundle bundle) { |
643 |
app.setDownloadingObject(false);
|
644 |
hideDialog(); |
645 |
HttpResponse response = bundle.getResponse(); |
646 |
if (response != null) { |
647 |
int statusCode = response.getStatusLine().getStatusCode();
|
648 |
if (statusCode == 200) { |
649 |
setResult(Activity.RESULT_OK); |
650 |
HttpEntity entity = response.getEntity(); |
651 |
app.setDownloadedEntity(entity); |
652 |
} else {
|
653 |
CloudServersException cse = parseCloudServersException(response); |
654 |
if ("".equals(cse.getMessage())) { |
655 |
showError("There was a problem downloading your File.", bundle);
|
656 |
} else {
|
657 |
showError("There was a problem downloading your file: " + cse.getMessage(), bundle);
|
658 |
} |
659 |
} |
660 |
} else if (exception != null) { |
661 |
showError("There was a problem downloading your file: " + exception.getMessage(), bundle);
|
662 |
} |
663 |
} |
664 |
} |
665 |
|
666 |
private class DeleteObjectListenerTask extends |
667 |
AsyncTask<Void, Void, Void> { |
668 |
|
669 |
@Override
|
670 |
protected Void doInBackground(Void... arg1) { |
671 |
|
672 |
while(app.isDeletingObject()){
|
673 |
// wait for process to finish
|
674 |
// or have it be canceled
|
675 |
if(deleteObjTask.isCancelled()){
|
676 |
return null; |
677 |
} |
678 |
} |
679 |
return null; |
680 |
} |
681 |
|
682 |
/*
|
683 |
* when no longer processing, time to load
|
684 |
* the new files
|
685 |
*/
|
686 |
@Override
|
687 |
protected void onPostExecute(Void arg1) { |
688 |
hideDialog(); |
689 |
setResult(99);
|
690 |
finish(); |
691 |
} |
692 |
} |
693 |
|
694 |
private class DownloadObjectListenerTask extends |
695 |
AsyncTask<Void, Void, Void> { |
696 |
|
697 |
@Override
|
698 |
protected Void doInBackground(Void... arg1) { |
699 |
|
700 |
while(app.isDownloadingObject()){
|
701 |
// wait for process to finish
|
702 |
// or have it be canceled
|
703 |
if(downloadObjTask.isCancelled()){
|
704 |
return null; |
705 |
} |
706 |
} |
707 |
return null; |
708 |
} |
709 |
|
710 |
/*
|
711 |
* when no longer processing, time to load
|
712 |
* the new files
|
713 |
*/
|
714 |
@Override
|
715 |
protected void onPostExecute(Void arg1) { |
716 |
hideDialog(); |
717 |
try {
|
718 |
if(writeFile(EntityUtils.toByteArray(app.getDownloadedEntity()))){
|
719 |
downloadButton.setText("Open File");
|
720 |
isDownloaded = true;
|
721 |
} |
722 |
else{
|
723 |
showAlert("Error", "There was a problem downloading your file."); |
724 |
} |
725 |
|
726 |
} catch (IOException e) { |
727 |
showAlert("Error", "There was a problem downloading your file."); |
728 |
e.printStackTrace(); |
729 |
} catch (Exception e) { |
730 |
showAlert("Error", "There was a problem downloading your file."); |
731 |
e.printStackTrace(); |
732 |
} |
733 |
} |
734 |
} |
735 |
|
736 |
} |