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