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