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