root / src / com / rackspace / cloud / android / ContainerObjectDetails.java @ 378fe36a
History | View | Annotate | Download (37.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.Container; |
49 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
50 |
import com.rackspace.cloud.files.api.client.ContainerObjects; |
51 |
import com.rackspace.cloud.files.api.client.GroupResource; |
52 |
import com.rackspace.cloud.files.api.client.ObjectVersion; |
53 |
import com.rackspace.cloud.files.api.client.Permission; |
54 |
import com.rackspace.cloud.servers.api.client.Account; |
55 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
56 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
57 |
import com.rackspace.cloud.utils.Utils; |
58 |
|
59 |
/**
|
60 |
*
|
61 |
* @author Phillip Toohill
|
62 |
*
|
63 |
*/
|
64 |
|
65 |
public class ContainerObjectDetails extends CloudActivity { |
66 |
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; |
67 |
private static final int deleteObject = 0; |
68 |
private static final int restoreVersion = 1; |
69 |
private final String DOWNLOAD_DIRECTORY = "PithosPlus"; |
70 |
|
71 |
private ContainerObjects objects;
|
72 |
private String containerNames; |
73 |
private String otherUser; |
74 |
private String cdnURL; |
75 |
private String cdnEnabled; |
76 |
public String LOG = "ViewObject"; |
77 |
private int bConver = 1048576; |
78 |
private int kbConver = 1024; |
79 |
private double megaBytes; |
80 |
private double kiloBytes; |
81 |
public Button previewButton; |
82 |
public Button downloadButton; |
83 |
private Boolean isDownloaded; |
84 |
private AndroidCloudApplication app;
|
85 |
private DeleteObjectListenerTask deleteObjTask;
|
86 |
private DownloadObjectListenerTask downloadObjTask;
|
87 |
private List<ObjectVersion> versions = new ArrayList<ObjectVersion>(); |
88 |
private boolean isReadOnly = false; |
89 |
private final List<String> metadataRemoved = new ArrayList<String>(); |
90 |
private final Map<String, String> metadataAdded = new HashMap<String, String>(); |
91 |
|
92 |
/** Called when the activity is first created. */
|
93 |
@Override
|
94 |
public void onCreate(Bundle savedInstanceState) { |
95 |
super.onCreate(savedInstanceState);
|
96 |
trackPageView(GoogleAnalytics.PAGE_STORAGE_OBJECT); |
97 |
|
98 |
objects = (ContainerObjects) this.getIntent().getExtras()
|
99 |
.get("container");
|
100 |
otherUser = (String) this.getIntent().getExtras().get("otherUser"); |
101 |
Log.d(LOG, "OTHERUSER:" + otherUser);
|
102 |
containerNames = (String) this.getIntent().getExtras() |
103 |
.get("containerNames");
|
104 |
cdnURL = (String) this.getIntent().getExtras().get("cdnUrl"); |
105 |
cdnEnabled = (String) this.getIntent().getExtras().get("isCdnEnabled"); |
106 |
|
107 |
setContentView(R.layout.viewobject); |
108 |
TabHost tabs = (TabHost) findViewById(R.id.tabhost2); |
109 |
|
110 |
tabs.setup(); |
111 |
|
112 |
TabHost.TabSpec spec = tabs.newTabSpec("tag1");
|
113 |
|
114 |
spec.setContent(R.id.details); |
115 |
spec.setIndicator("Details");
|
116 |
tabs.addTab(spec); |
117 |
|
118 |
spec = tabs.newTabSpec("tag2");
|
119 |
spec.setContent(R.id.metadata); |
120 |
spec.setIndicator("Metadata");
|
121 |
tabs.addTab(spec); |
122 |
|
123 |
spec = tabs.newTabSpec("tag3");
|
124 |
spec.setContent(R.id.sharing); |
125 |
spec.setIndicator("Sharing");
|
126 |
tabs.addTab(spec); |
127 |
|
128 |
spec = tabs.newTabSpec("tag4");
|
129 |
spec.setContent(R.id.versions); |
130 |
spec.setIndicator("Versions");
|
131 |
tabs.addTab(spec); |
132 |
|
133 |
restoreState(savedInstanceState); |
134 |
} |
135 |
|
136 |
public boolean isFolder() { |
137 |
return objects.getContentType().startsWith("application/folder") |
138 |
|| objects.getContentType().startsWith("application/directory");
|
139 |
} |
140 |
|
141 |
@Override
|
142 |
protected void onSaveInstanceState(Bundle outState) { |
143 |
super.onSaveInstanceState(outState);
|
144 |
outState.putSerializable("container", objects);
|
145 |
outState.putBoolean("isDownloaded", isDownloaded);
|
146 |
} |
147 |
|
148 |
protected void restoreState(Bundle state) { |
149 |
super.restoreState(state);
|
150 |
/*
|
151 |
* need reference to the app so you can access curDirFiles as well as
|
152 |
* processing status
|
153 |
*/
|
154 |
app = (AndroidCloudApplication) this.getApplication();
|
155 |
|
156 |
if (state != null && state.containsKey("container")) { |
157 |
objects = (ContainerObjects) state.getSerializable("container");
|
158 |
} |
159 |
if (containerNames.equals(Container.MYSHARED) |
160 |
|| containerNames.equals(Container.OTHERS))
|
161 |
loadObjectData(); |
162 |
else
|
163 |
new ContainerObjectRefreshTask().execute();
|
164 |
|
165 |
if (cdnEnabled.equals("true")) { |
166 |
this.previewButton = (Button) findViewById(R.id.preview_button); |
167 |
previewButton.setOnClickListener(new MyOnClickListener());
|
168 |
} else {
|
169 |
this.previewButton = (Button) findViewById(R.id.preview_button); |
170 |
previewButton.setVisibility(View.GONE);
|
171 |
} |
172 |
|
173 |
if (state != null && state.containsKey("isDownloaded")) { |
174 |
isDownloaded = state.getBoolean("isDownloaded");
|
175 |
} else {
|
176 |
isDownloaded = fileIsDownloaded(); |
177 |
} |
178 |
this.downloadButton = (Button) findViewById(R.id.download_button); |
179 |
if (isDownloaded) {
|
180 |
downloadButton.setText("Open File");
|
181 |
} else {
|
182 |
downloadButton.setText("Download File");
|
183 |
} |
184 |
downloadButton.setOnClickListener(new MyOnClickListener());
|
185 |
|
186 |
if (app.isDeletingObject()) {
|
187 |
deleteObjTask = new DeleteObjectListenerTask();
|
188 |
deleteObjTask.execute(); |
189 |
} |
190 |
|
191 |
if (app.isDownloadingObject()) {
|
192 |
downloadObjTask = new DownloadObjectListenerTask();
|
193 |
downloadObjTask.execute(); |
194 |
} |
195 |
|
196 |
} |
197 |
|
198 |
@Override
|
199 |
protected void onStop() { |
200 |
super.onStop();
|
201 |
|
202 |
/*
|
203 |
* Need to stop running listener task if we exit
|
204 |
*/
|
205 |
if (deleteObjTask != null) { |
206 |
deleteObjTask.cancel(true);
|
207 |
} |
208 |
|
209 |
if (downloadObjTask != null) { |
210 |
downloadObjTask.cancel(true);
|
211 |
} |
212 |
} |
213 |
|
214 |
private void loadObjectData() { |
215 |
setTitle("Details: " + objects.getCName());
|
216 |
metadataRemoved.clear(); |
217 |
metadataAdded.clear(); |
218 |
if (Container.MYSHARED.equals(objects.getContainerName()) |
219 |
|| Container.MYSHARED.equals(containerNames)) {
|
220 |
isReadOnly = true;
|
221 |
|
222 |
} |
223 |
// Object Name
|
224 |
TextView name = (TextView) findViewById(R.id.view_container_name); |
225 |
name.setText(objects.getCName().toString()); |
226 |
|
227 |
// File size
|
228 |
if (objects.getBytes() >= bConver) {
|
229 |
megaBytes = Math.abs(objects.getBytes() / bConver + 0.2); |
230 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
231 |
sublabel.setText(megaBytes + " MB");
|
232 |
} else if (objects.getBytes() >= kbConver) { |
233 |
kiloBytes = Math.abs(objects.getBytes() / kbConver + 0.2); |
234 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
235 |
sublabel.setText(kiloBytes + " KB");
|
236 |
} else {
|
237 |
TextView sublabel = (TextView) findViewById(R.id.view_file_bytes); |
238 |
sublabel.setText(objects.getBytes() + " B");
|
239 |
} |
240 |
|
241 |
// Content Type
|
242 |
TextView cType = (TextView) findViewById(R.id.view_content_type); |
243 |
if (objects.getContentType() != null) |
244 |
cType.setText(objects.getContentType().toString()); |
245 |
else
|
246 |
cType.setText("");
|
247 |
|
248 |
// Last Modification date
|
249 |
String strDate = objects.getLastMod();
|
250 |
strDate = strDate.substring(0, strDate.indexOf('T')); |
251 |
|
252 |
TextView lastmod = (TextView) findViewById(R.id.view_file_modification); |
253 |
lastmod.setText(strDate); |
254 |
rebuildMetadataList(); |
255 |
if (isFolder()) {
|
256 |
findViewById(R.id.download_text).setVisibility(View.GONE);
|
257 |
findViewById(R.id.linearLayout1).setVisibility(View.GONE);
|
258 |
findViewById(R.id.layoutPublic).setVisibility(View.GONE);
|
259 |
} |
260 |
rebuildPermissionList(); |
261 |
try {
|
262 |
new AsyncTask<Void, Void, List<ObjectVersion>>() { |
263 |
|
264 |
@Override
|
265 |
protected List<ObjectVersion> doInBackground(Void... params) { |
266 |
try {
|
267 |
return new ContainerObjectManager( |
268 |
getApplicationContext()).getObjectVersions( |
269 |
objects.getContainerName(), objects.getCName(), |
270 |
otherUser); |
271 |
} catch (CloudServersException e) {
|
272 |
Log.e("ERROR", "", e); |
273 |
return null; |
274 |
} |
275 |
|
276 |
} |
277 |
|
278 |
@Override
|
279 |
protected void onPostExecute(List<ObjectVersion> result) { |
280 |
if (result != null) { |
281 |
versions = result; |
282 |
rebuildVersionList(); |
283 |
} |
284 |
} |
285 |
|
286 |
}.execute(); |
287 |
} catch (Exception e) { |
288 |
Log.e("ERROR THERE", "", e); |
289 |
} |
290 |
|
291 |
} |
292 |
|
293 |
private void rebuildMetadataList() { |
294 |
Button newmetadata = (Button) findViewById(R.id.newMetadata); |
295 |
if (isReadOnly)
|
296 |
newmetadata.setVisibility(View.GONE);
|
297 |
newmetadata.setOnClickListener(new OnClickListener() {
|
298 |
|
299 |
@Override
|
300 |
public void onClick(View arg0) { |
301 |
final AlertDialog.Builder alert = new AlertDialog.Builder( |
302 |
ContainerObjectDetails.this); |
303 |
alert.setTitle("Add Metadata");
|
304 |
LinearLayout ll = new LinearLayout(ContainerObjectDetails.this);
|
305 |
ll.setOrientation(LinearLayout.VERTICAL); |
306 |
|
307 |
final EditText input = new EditText(ContainerObjectDetails.this); |
308 |
final EditText input2 = new EditText( |
309 |
ContainerObjectDetails.this); |
310 |
ll.addView(input); |
311 |
ll.addView(input2); |
312 |
alert.setView(ll); |
313 |
alert.setPositiveButton("Create",
|
314 |
new DialogInterface.OnClickListener() {
|
315 |
public void onClick(DialogInterface dialog, |
316 |
int whichButton) {
|
317 |
String key = input.getText().toString().trim();
|
318 |
String value = input2.getText().toString()
|
319 |
.trim(); |
320 |
addMetatadata(key, value); |
321 |
} |
322 |
}); |
323 |
|
324 |
alert.setNegativeButton("Cancel",
|
325 |
new DialogInterface.OnClickListener() {
|
326 |
public void onClick(DialogInterface dialog, |
327 |
int whichButton) {
|
328 |
dialog.cancel(); |
329 |
} |
330 |
}); |
331 |
alert.show(); |
332 |
} |
333 |
}); |
334 |
LayoutInflater layoutInflater = LayoutInflater |
335 |
.from(ContainerObjectDetails.this); |
336 |
final LinearLayout metadata = (LinearLayout) findViewById(R.id.metadataList);
|
337 |
if (metadata.getChildCount() > 0) |
338 |
metadata.removeViews(0, metadata.getChildCount());
|
339 |
metadata.removeAllViews(); |
340 |
|
341 |
if (objects.getMetadata() != null) { |
342 |
int i = 0; |
343 |
Iterator<Entry<String, String>> it = objects.getMetadata() |
344 |
.entrySet().iterator(); |
345 |
while (it.hasNext()) {
|
346 |
final Entry<String, String> perm = it.next(); |
347 |
final View v = layoutInflater.inflate(R.layout.metadatarow, |
348 |
null);
|
349 |
populateMetadataList(perm, v, metadata, i); |
350 |
i++; |
351 |
} |
352 |
} |
353 |
} |
354 |
|
355 |
private void addMetatadata(String key, String value) { |
356 |
if (objects.getMetadata() == null) |
357 |
objects.setMetadata(new HashMap<String, String>()); |
358 |
metadataAdded.put(key, value); |
359 |
objects.getMetadata().put(key, value); |
360 |
rebuildMetadataList(); |
361 |
} |
362 |
|
363 |
private void populateMetadataList(final Entry<String, String> metadata, |
364 |
final View v, final LinearLayout properties, int i) { |
365 |
properties.addView(v, i); |
366 |
Log.d(LOG, i + " " + metadata.getKey() + " " + metadata.getValue()); |
367 |
((TextView) v.findViewById(R.id.mkey)).setText(metadata.getKey()); |
368 |
((TextView) v.findViewById(R.id.mvalue)).setText(metadata.getValue()); |
369 |
if (isReadOnly)
|
370 |
v.findViewById(R.id.remove).setVisibility(View.GONE);
|
371 |
((ImageButton) v.findViewById(R.id.remove)) |
372 |
.setOnClickListener(new OnClickListener() {
|
373 |
|
374 |
@Override
|
375 |
public void onClick(View v1) { |
376 |
properties.removeView(v); |
377 |
metadataRemoved.add(metadata.getKey()); |
378 |
objects.getMetadata().remove(metadata.getKey()); |
379 |
|
380 |
} |
381 |
}); |
382 |
} |
383 |
|
384 |
private void rebuildPermissionList() { |
385 |
Button newmetadata = (Button) findViewById(R.id.newPermission); |
386 |
CheckBox readForAll = (CheckBox) findViewById(R.id.folderPublic); |
387 |
if (isReadOnly) {
|
388 |
newmetadata.setVisibility(View.GONE);
|
389 |
readForAll.setEnabled(false);
|
390 |
} |
391 |
objects.setPublicf(objects.getIsPublic() != null);
|
392 |
readForAll.setChecked(objects.isPublicf()); |
393 |
|
394 |
readForAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
395 |
|
396 |
@Override
|
397 |
public void onCheckedChanged(CompoundButton buttonView, |
398 |
boolean isChecked) {
|
399 |
objects.setPublicf(isChecked); |
400 |
|
401 |
} |
402 |
}); |
403 |
newmetadata.setOnClickListener(new OnClickListener() {
|
404 |
|
405 |
@Override
|
406 |
public void onClick(View v) { |
407 |
showAddDialog(); |
408 |
} |
409 |
|
410 |
}); |
411 |
LayoutInflater layoutInflater = LayoutInflater |
412 |
.from(ContainerObjectDetails.this); |
413 |
final LinearLayout properties = (LinearLayout) findViewById(R.id.permissionsList);
|
414 |
if (properties.getChildCount() > 0) |
415 |
properties.removeViews(0, properties.getChildCount());
|
416 |
properties.removeAllViews(); |
417 |
Iterator<Permission> it = null; |
418 |
if (objects.getPermissions() != null) { |
419 |
it = objects.getPermissions().iterator(); |
420 |
List<String> uuids = new ArrayList<String>(); |
421 |
while (it.hasNext()) {
|
422 |
Permission p = it.next();
|
423 |
if(p.getUser()!=null) |
424 |
uuids.add(p.getUser()); |
425 |
} |
426 |
if(uuids.size()>0){ |
427 |
Account.expandUUIDSOrDisplayNames(this, new ArrayList<String>(), uuids); |
428 |
} |
429 |
it = objects.getPermissions().iterator(); |
430 |
int i = 0; |
431 |
while (it.hasNext()) {
|
432 |
final Permission perm = it.next(); |
433 |
if(perm.getUser()!=null){ |
434 |
perm.setDisplayName(Account.getAccount().getDisplayNameForUUID(perm.getUser(), this));
|
435 |
} |
436 |
final View v = layoutInflater.inflate(R.layout.propertiesrow, |
437 |
null);
|
438 |
populatePermissionList(perm, v, properties, i); |
439 |
i++; |
440 |
} |
441 |
} |
442 |
} |
443 |
|
444 |
private void populatePermissionList(final Permission perm, final View v, |
445 |
final LinearLayout properties, int i) { |
446 |
|
447 |
properties.addView(v, i); |
448 |
if (isReadOnly) {
|
449 |
v.findViewById(R.id.remove).setVisibility(View.GONE);
|
450 |
((CheckBox) v.findViewById(R.id.read)).setEnabled(false);
|
451 |
((CheckBox) v.findViewById(R.id.write)).setEnabled(false);
|
452 |
} |
453 |
|
454 |
((TextView) v.findViewById(R.id.ownerName)) |
455 |
.setText(perm.getUser() == null ? perm.getGroup() + ":" : perm |
456 |
.getDisplayName() + ":");
|
457 |
((CheckBox) v.findViewById(R.id.read)).setChecked(perm.isRead()); |
458 |
((CheckBox) v.findViewById(R.id.write)).setChecked(perm.isWrite()); |
459 |
|
460 |
((CheckBox) v.findViewById(R.id.read)) |
461 |
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
462 |
|
463 |
@Override
|
464 |
public void onCheckedChanged(CompoundButton buttonView, |
465 |
boolean isChecked) {
|
466 |
perm.setRead(isChecked); |
467 |
|
468 |
} |
469 |
}); |
470 |
((CheckBox) v.findViewById(R.id.write)) |
471 |
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
472 |
|
473 |
@Override
|
474 |
public void onCheckedChanged(CompoundButton buttonView, |
475 |
boolean isChecked) {
|
476 |
perm.setWrite(isChecked); |
477 |
|
478 |
} |
479 |
}); |
480 |
((Button) v.findViewById(R.id.remove))
|
481 |
.setOnClickListener(new OnClickListener() {
|
482 |
|
483 |
@Override
|
484 |
public void onClick(View v1) { |
485 |
properties.removeView(v); |
486 |
objects.getPermissions().remove(perm); |
487 |
|
488 |
} |
489 |
}); |
490 |
} |
491 |
|
492 |
private void addPermission(boolean group, String userOrGroup) { |
493 |
if (objects.getPermissions() == null) |
494 |
objects.setPermissions(new ArrayList<Permission>()); |
495 |
Permission np = new Permission(); |
496 |
if (group)
|
497 |
np.setGroup(userOrGroup); |
498 |
else{
|
499 |
np.setDisplayName(userOrGroup); |
500 |
String uuid = Account.getAccount().getUUIDForDisplayName(userOrGroup, this); |
501 |
if(uuid==null){ |
502 |
showAlert("User does not exit", "The username you specified is invalid"); |
503 |
return;
|
504 |
} |
505 |
np.setUser(uuid); |
506 |
} |
507 |
objects.getPermissions().add(np); |
508 |
rebuildPermissionList(); |
509 |
} |
510 |
|
511 |
private void showAddDialog() { |
512 |
final CharSequence[] items = { "Add User", "Add Group" }; |
513 |
|
514 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
515 |
builder.setTitle("User Or Group");
|
516 |
builder.setSingleChoiceItems(items, -1,
|
517 |
new DialogInterface.OnClickListener() {
|
518 |
public void onClick(DialogInterface dialog, int item) { |
519 |
boolean user;
|
520 |
if (item == 0) { |
521 |
user = true;
|
522 |
} else
|
523 |
user = false;
|
524 |
populateAddDialog(user); |
525 |
dialog.dismiss(); |
526 |
|
527 |
} |
528 |
}); |
529 |
AlertDialog alert2 = builder.create(); |
530 |
alert2.show(); |
531 |
|
532 |
} |
533 |
|
534 |
private void populateAddDialog(final boolean user) { |
535 |
final AlertDialog.Builder alert = new AlertDialog.Builder( |
536 |
ContainerObjectDetails.this); |
537 |
if (user) {
|
538 |
alert.setTitle("Add User");
|
539 |
final EditText input = new EditText(ContainerObjectDetails.this); |
540 |
input.setTextColor(Color.BLACK);
|
541 |
|
542 |
alert.setView(input); |
543 |
|
544 |
alert.setPositiveButton("Add",
|
545 |
new DialogInterface.OnClickListener() {
|
546 |
public void onClick(DialogInterface dialog, |
547 |
int whichButton) {
|
548 |
String value = input.getText().toString().trim();
|
549 |
addPermission(false, value);
|
550 |
|
551 |
} |
552 |
}); |
553 |
|
554 |
alert.setNegativeButton("Cancel",
|
555 |
new DialogInterface.OnClickListener() {
|
556 |
public void onClick(DialogInterface dialog, |
557 |
int whichButton) {
|
558 |
dialog.cancel(); |
559 |
|
560 |
} |
561 |
}); |
562 |
alert.show(); |
563 |
} else {
|
564 |
AlertDialog.Builder builder = new AlertDialog.Builder(this); |
565 |
builder.setTitle("Add Group");
|
566 |
builder.setSingleChoiceItems( |
567 |
getGroupNames().toArray(new String[0]), -1, |
568 |
new DialogInterface.OnClickListener() {
|
569 |
public void onClick(DialogInterface dialog, int item) { |
570 |
addPermission(true, getGroupNames().get(item));
|
571 |
dialog.dismiss(); |
572 |
|
573 |
} |
574 |
}); |
575 |
AlertDialog alert2 = builder.create(); |
576 |
alert2.show(); |
577 |
} |
578 |
|
579 |
} |
580 |
|
581 |
public List<String> getGroupNames() { |
582 |
List<String> result = new ArrayList<String>(); |
583 |
List<GroupResource> groups = ((AndroidCloudApplication) getApplication())
|
584 |
.getGroups(); |
585 |
for (GroupResource g : groups) {
|
586 |
result.add(g.getName()); |
587 |
} |
588 |
return result;
|
589 |
} |
590 |
|
591 |
private void rebuildVersionList() { |
592 |
LayoutInflater layoutInflater = LayoutInflater |
593 |
.from(ContainerObjectDetails.this); |
594 |
final LinearLayout properties = (LinearLayout) findViewById(R.id.versionsList);
|
595 |
if (properties.getChildCount() > 0) |
596 |
properties.removeViews(0, properties.getChildCount());
|
597 |
properties.removeAllViews(); |
598 |
Iterator<ObjectVersion> it;
|
599 |
// Collections.reverse(versions);
|
600 |
it = versions.iterator(); |
601 |
int i = 0; |
602 |
while (it.hasNext()) {
|
603 |
final ObjectVersion perm = it.next();
|
604 |
final View v = layoutInflater.inflate(R.layout.versionsrow, null); |
605 |
populateVersionList(perm, v, properties, i); |
606 |
i++; |
607 |
} |
608 |
} |
609 |
|
610 |
String selectedVersion = null; |
611 |
|
612 |
private void populateVersionList(final ObjectVersion perm, final View v, |
613 |
final LinearLayout properties, int i) { |
614 |
|
615 |
properties.addView(v, i); |
616 |
|
617 |
((TextView) v.findViewById(R.id.versionName)).setText("Version: "
|
618 |
+ perm.getVersion()); |
619 |
|
620 |
((TextView) v.findViewById(R.id.versionModified)).setText("Modified: "
|
621 |
+ perm.getDateString()); |
622 |
if (versions.size() == 1 || isReadOnly) { |
623 |
// ((Button)
|
624 |
// v.findViewById(R.id.vremove)).setVisibility(View.INVISIBLE);
|
625 |
((Button) v.findViewById(R.id.vrestore))
|
626 |
.setVisibility(View.INVISIBLE);
|
627 |
} |
628 |
/*
|
629 |
* ((Button) v.findViewById(R.id.vremove)) .setOnClickListener(new
|
630 |
* OnClickListener() {
|
631 |
*
|
632 |
* @Override public void onClick(View v1) { Log.d("PERMS",
|
633 |
* perm.getUri()); try { new GssHttpCommands(getDroidApplication()
|
634 |
* .getUserDetails
|
635 |
* ()).deleteFolder(perm.getUri()+"?version="+perm.getVersion()); }
|
636 |
* catch (SystemErrorException e) { // TODO Auto-generated catch block
|
637 |
* e.printStackTrace(); } catch (GssHttpException e) { // TODO
|
638 |
* Auto-generated catch block e.printStackTrace(); }
|
639 |
* getFileTask().execute(res.getUri()); //properties.removeView(v);
|
640 |
*
|
641 |
* } });
|
642 |
*/
|
643 |
((Button) v.findViewById(R.id.vrestore))
|
644 |
.setOnClickListener(new OnClickListener() {
|
645 |
|
646 |
@Override
|
647 |
public void onClick(View v1) { |
648 |
selectedVersion = String.valueOf(perm.getVersion());
|
649 |
showDialog(restoreVersion); |
650 |
|
651 |
} |
652 |
}); |
653 |
((Button) v.findViewById(R.id.vdownload))
|
654 |
.setOnClickListener(new OnClickListener() {
|
655 |
|
656 |
@Override
|
657 |
public void onClick(View v1) { |
658 |
if (storageIsReady()) {
|
659 |
new ContainerObjectDownloadTask().execute(String |
660 |
.valueOf(perm.getVersion())); |
661 |
} else {
|
662 |
showAlert("Error", "Storage not found."); |
663 |
} |
664 |
// getDownloadTask().execute(perm.getUri()+"?version="+perm.getVersion());
|
665 |
// properties.removeView(v);
|
666 |
|
667 |
} |
668 |
}); |
669 |
} |
670 |
|
671 |
private class MyOnClickListener implements View.OnClickListener { |
672 |
@Override
|
673 |
public void onClick(View v) { |
674 |
if (v.equals(findViewById(R.id.preview_button))) {
|
675 |
Intent viewIntent = new Intent("android.intent.action.VIEW", |
676 |
Uri.parse(cdnURL + "/" + objects.getCName()));
|
677 |
startActivity(viewIntent); |
678 |
} |
679 |
/*
|
680 |
* need to perform different functions based on if the file is in
|
681 |
* the devices filesystem
|
682 |
*/
|
683 |
if (v.equals(findViewById(R.id.download_button))) {
|
684 |
if (!isDownloaded) {
|
685 |
if (storageIsReady()) {
|
686 |
new ContainerObjectDownloadTask().execute();
|
687 |
} else {
|
688 |
showAlert("Error", "Storage not found."); |
689 |
} |
690 |
} else {
|
691 |
openFile(); |
692 |
} |
693 |
} |
694 |
} |
695 |
} |
696 |
|
697 |
// Create the Menu options
|
698 |
@Override
|
699 |
public boolean onCreateOptionsMenu(Menu menu) { |
700 |
super.onCreateOptionsMenu(menu);
|
701 |
MenuInflater inflater = getMenuInflater(); |
702 |
inflater.inflate(R.menu.container_object_list_menu, menu); |
703 |
menu.findItem(R.id.delete_object).setVisible(!isReadOnly); |
704 |
menu.findItem(R.id.save).setVisible(!isReadOnly); |
705 |
return true; |
706 |
} |
707 |
|
708 |
@Override
|
709 |
public boolean onOptionsItemSelected(MenuItem item) { |
710 |
switch (item.getItemId()) {
|
711 |
case R.id.delete_object:
|
712 |
showDialog(deleteObject); |
713 |
return true; |
714 |
case R.id.refresh:
|
715 |
if (containerNames.equals(Container.MYSHARED) |
716 |
|| containerNames.equals(Container.OTHERS))
|
717 |
loadObjectData(); |
718 |
else
|
719 |
new ContainerObjectRefreshTask().execute();
|
720 |
|
721 |
return true; |
722 |
case R.id.save:
|
723 |
new ContainerObjectUpdateTask().execute();
|
724 |
return true; |
725 |
} |
726 |
return false; |
727 |
} |
728 |
|
729 |
@Override
|
730 |
protected Dialog onCreateDialog(int id) { |
731 |
switch (id) {
|
732 |
case deleteObject:
|
733 |
return new AlertDialog.Builder(ContainerObjectDetails.this) |
734 |
.setIcon(R.drawable.alert_dialog_icon) |
735 |
.setTitle("Delete File")
|
736 |
.setMessage("Are you sure you want to delete this file?")
|
737 |
.setPositiveButton("Delete File",
|
738 |
new DialogInterface.OnClickListener() {
|
739 |
public void onClick(DialogInterface dialog, |
740 |
int whichButton) {
|
741 |
// User clicked OK so do some stuff
|
742 |
trackEvent(GoogleAnalytics.CATEGORY_FILE, |
743 |
GoogleAnalytics.EVENT_DELETE, "",
|
744 |
-1);
|
745 |
new ContainerObjectDeleteTask()
|
746 |
.execute((Void[]) null); |
747 |
} |
748 |
}) |
749 |
.setNegativeButton("Cancel",
|
750 |
new DialogInterface.OnClickListener() {
|
751 |
public void onClick(DialogInterface dialog, |
752 |
int whichButton) {
|
753 |
// User clicked Cancel so do some stuff
|
754 |
} |
755 |
}).create(); |
756 |
case restoreVersion:
|
757 |
return new AlertDialog.Builder(ContainerObjectDetails.this) |
758 |
.setIcon(R.drawable.alert_dialog_icon) |
759 |
.setTitle("Restore Version")
|
760 |
.setMessage( |
761 |
"Are you sure you want to restore this version?")
|
762 |
.setPositiveButton("Restore Version",
|
763 |
new DialogInterface.OnClickListener() {
|
764 |
public void onClick(DialogInterface dialog, |
765 |
int whichButton) {
|
766 |
// User clicked OK so do some stuff
|
767 |
/*
|
768 |
* trackEvent(GoogleAnalytics.CATEGORY_FILE,
|
769 |
* GoogleAnalytics.EVENT_DELETE, "", -1);
|
770 |
*/
|
771 |
new VersionRestoreTask()
|
772 |
.execute(selectedVersion); |
773 |
} |
774 |
}) |
775 |
.setNegativeButton("Cancel",
|
776 |
new DialogInterface.OnClickListener() {
|
777 |
public void onClick(DialogInterface dialog, |
778 |
int whichButton) {
|
779 |
// User clicked Cancel so do some stuff
|
780 |
} |
781 |
}).create(); |
782 |
|
783 |
} |
784 |
|
785 |
return null; |
786 |
} |
787 |
|
788 |
/**
|
789 |
* @return the file
|
790 |
*/
|
791 |
public ContainerObjects getViewFile() {
|
792 |
return objects;
|
793 |
} |
794 |
|
795 |
/**
|
796 |
* @param File
|
797 |
* the file to set
|
798 |
*/
|
799 |
public void setViewFile(ContainerObjects object) { |
800 |
this.objects = object;
|
801 |
} |
802 |
|
803 |
/*
|
804 |
* returns false if external storage is not avaliable (if its mounted,
|
805 |
* missing, read-only, etc) from:
|
806 |
* http://developer.android.com/guide/topics/data
|
807 |
* /data-storage.html#filesExternal
|
808 |
*/
|
809 |
private boolean storageIsReady() { |
810 |
boolean mExternalStorageAvailable = false; |
811 |
boolean mExternalStorageWriteable = false; |
812 |
String state = Environment.getExternalStorageState();
|
813 |
|
814 |
if (Environment.MEDIA_MOUNTED.equals(state)) {
|
815 |
// We can read and write the media
|
816 |
mExternalStorageAvailable = mExternalStorageWriteable = true;
|
817 |
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { |
818 |
// We can only read the media
|
819 |
mExternalStorageAvailable = true;
|
820 |
mExternalStorageWriteable = false;
|
821 |
} else {
|
822 |
// Something else is wrong. It may be one of many other states, but
|
823 |
// all we need
|
824 |
// to know is we can neither read nor write
|
825 |
mExternalStorageAvailable = mExternalStorageWriteable = false;
|
826 |
} |
827 |
return mExternalStorageAvailable && mExternalStorageWriteable;
|
828 |
} |
829 |
|
830 |
private boolean fileIsDownloaded() { |
831 |
if (storageIsReady()) {
|
832 |
String fileName = Environment.getExternalStorageDirectory()
|
833 |
.getPath() + "/PithosPlus/" + objects.getCName();
|
834 |
File f = new File(fileName); |
835 |
return f.isFile();
|
836 |
} |
837 |
return false; |
838 |
} |
839 |
|
840 |
private void openFile() { |
841 |
File object = new File(Environment.getExternalStorageDirectory() |
842 |
.getPath() + "/PithosPlus/" + objects.getCName());
|
843 |
Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW);
|
844 |
File file = new File(object.getAbsolutePath()); |
845 |
String extension = android.webkit.MimeTypeMap
|
846 |
.getFileExtensionFromUrl(Uri.fromFile(file).toString()); |
847 |
String mimetype = android.webkit.MimeTypeMap.getSingleton()
|
848 |
.getMimeTypeFromExtension(extension); |
849 |
myIntent.setDataAndType(Uri.fromFile(file), mimetype); |
850 |
// myIntent.setData(Uri.fromFile(file));
|
851 |
try {
|
852 |
startActivity(myIntent); |
853 |
} catch (Exception e) { |
854 |
Toast.makeText(this, "Could not open file.", Toast.LENGTH_SHORT) |
855 |
.show(); |
856 |
} |
857 |
} |
858 |
|
859 |
// Task's
|
860 |
|
861 |
private class ContainerObjectRefreshTask extends |
862 |
AsyncTask<Void, Void, ContainerObjects> { |
863 |
|
864 |
private CloudServersException exception;
|
865 |
|
866 |
protected void onPreExecute() { |
867 |
showDialog(); |
868 |
} |
869 |
|
870 |
@Override
|
871 |
protected ContainerObjects doInBackground(Void... arg0) { |
872 |
ContainerObjects bundle = null;
|
873 |
try {
|
874 |
if (otherUser == null) |
875 |
bundle = (new ContainerObjectManager(getContext()))
|
876 |
.executeHead(containerNames, objects.getCName()); |
877 |
else
|
878 |
bundle = (new ContainerObjectManager(getContext()))
|
879 |
.executeHead(containerNames, objects.getCName(), |
880 |
otherUser); |
881 |
} catch (CloudServersException e) {
|
882 |
exception = e; |
883 |
} |
884 |
|
885 |
return bundle;
|
886 |
} |
887 |
|
888 |
@Override
|
889 |
protected void onPostExecute(ContainerObjects bundle) { |
890 |
hideDialog(); |
891 |
if (bundle != null) { |
892 |
objects = bundle; |
893 |
loadObjectData(); |
894 |
} else if (exception != null) { |
895 |
showToast("There was a problem refreshing your file: "
|
896 |
+ exception.getMessage()); |
897 |
} |
898 |
} |
899 |
} |
900 |
|
901 |
private class ContainerObjectDeleteTask extends |
902 |
AsyncTask<Void, Void, HttpBundle> { |
903 |
|
904 |
private CloudServersException exception;
|
905 |
|
906 |
protected void onPreExecute() { |
907 |
showDialog(); |
908 |
app.setDeleteingObject(true);
|
909 |
deleteObjTask = new DeleteObjectListenerTask();
|
910 |
Utils.execute(deleteObjTask); |
911 |
} |
912 |
|
913 |
@Override
|
914 |
protected HttpBundle doInBackground(Void... arg0) { |
915 |
HttpBundle bundle = null;
|
916 |
try {
|
917 |
bundle = (new ContainerObjectManager(getContext()))
|
918 |
.deleteObject(containerNames, objects.getCName(), |
919 |
otherUser); |
920 |
} catch (CloudServersException e) {
|
921 |
exception = e; |
922 |
} |
923 |
|
924 |
return bundle;
|
925 |
} |
926 |
|
927 |
@Override
|
928 |
protected void onPostExecute(HttpBundle bundle) { |
929 |
app.setDeleteingObject(false);
|
930 |
hideDialog(); |
931 |
HttpResponse response = bundle.getResponse(); |
932 |
if (response != null) { |
933 |
int statusCode = response.getStatusLine().getStatusCode();
|
934 |
if (statusCode == 204) { |
935 |
// handled by listener
|
936 |
} else {
|
937 |
CloudServersException cse = parseCloudServersException(response); |
938 |
if ("".equals(cse.getMessage())) { |
939 |
showError("There was a problem deleting your File.",
|
940 |
bundle); |
941 |
} else {
|
942 |
showError("There was a problem deleting your file: "
|
943 |
+ cse.getMessage(), bundle); |
944 |
} |
945 |
} |
946 |
} else if (exception != null) { |
947 |
showError("There was a problem deleting your file: "
|
948 |
+ exception.getMessage(), bundle); |
949 |
} |
950 |
} |
951 |
} |
952 |
|
953 |
private class ContainerObjectDownloadTask extends |
954 |
AsyncTask<String, Void, HttpBundle> { |
955 |
|
956 |
private CloudServersException exception;
|
957 |
|
958 |
@Override
|
959 |
protected void onPreExecute() { |
960 |
showDialog(); |
961 |
app.setDownloadingObject(true);
|
962 |
downloadObjTask = new DownloadObjectListenerTask();
|
963 |
Utils.execute(downloadObjTask); |
964 |
|
965 |
} |
966 |
|
967 |
@Override
|
968 |
protected HttpBundle doInBackground(String... arg0) { |
969 |
HttpBundle bundle = null;
|
970 |
try {
|
971 |
if (arg0.length == 1 && arg0[0] != null) |
972 |
bundle = (new ContainerObjectManager(getContext()))
|
973 |
.getObject(containerNames, objects.getCName(), |
974 |
arg0[0], otherUser);
|
975 |
else
|
976 |
bundle = (new ContainerObjectManager(getContext()))
|
977 |
.getObject(containerNames, objects.getCName(), |
978 |
otherUser); |
979 |
} catch (CloudServersException e) {
|
980 |
exception = e; |
981 |
} catch (Exception exx) { |
982 |
exx.printStackTrace(); |
983 |
} |
984 |
return bundle;
|
985 |
} |
986 |
|
987 |
@Override
|
988 |
protected void onPostExecute(HttpBundle bundle) { |
989 |
hideDialog(); |
990 |
HttpResponse response = bundle.getResponse(); |
991 |
if (response != null) { |
992 |
int statusCode = response.getStatusLine().getStatusCode();
|
993 |
if (statusCode == 200) { |
994 |
setResult(Activity.RESULT_OK); |
995 |
HttpEntity entity = response.getEntity(); |
996 |
app.setDownloadedEntity(entity); |
997 |
} else {
|
998 |
CloudServersException cse = parseCloudServersException(response); |
999 |
if ("".equals(cse.getMessage())) { |
1000 |
showError("There was a problem downloading your File.",
|
1001 |
bundle); |
1002 |
} else {
|
1003 |
showError("There was a problem downloading your file: "
|
1004 |
+ cse.getMessage(), bundle); |
1005 |
} |
1006 |
} |
1007 |
} else if (exception != null) { |
1008 |
showError("There was a problem downloading your file: "
|
1009 |
+ exception.getMessage(), bundle); |
1010 |
} |
1011 |
app.setDownloadingObject(false);
|
1012 |
} |
1013 |
} |
1014 |
|
1015 |
private class DeleteObjectListenerTask extends AsyncTask<Void, Void, Void> { |
1016 |
|
1017 |
@Override
|
1018 |
protected Void doInBackground(Void... arg1) { |
1019 |
|
1020 |
while (app.isDeletingObject()) {
|
1021 |
// wait for process to finish
|
1022 |
// or have it be canceled
|
1023 |
if (deleteObjTask.isCancelled()) {
|
1024 |
return null; |
1025 |
} |
1026 |
} |
1027 |
return null; |
1028 |
} |
1029 |
|
1030 |
/*
|
1031 |
* when no longer processing, time to load the new files
|
1032 |
*/
|
1033 |
@Override
|
1034 |
protected void onPostExecute(Void arg1) { |
1035 |
hideDialog(); |
1036 |
setResult(99);
|
1037 |
finish(); |
1038 |
} |
1039 |
} |
1040 |
|
1041 |
private class DownloadObjectListenerTask extends |
1042 |
AsyncTask<Void, Void, Void> { |
1043 |
|
1044 |
@Override
|
1045 |
protected Void doInBackground(Void... arg1) { |
1046 |
|
1047 |
while (app.isDownloadingObject()) {
|
1048 |
// wait for process to finish
|
1049 |
// or have it be canceled
|
1050 |
if (downloadObjTask.isCancelled()) {
|
1051 |
return null; |
1052 |
} |
1053 |
} |
1054 |
return null; |
1055 |
} |
1056 |
|
1057 |
/*
|
1058 |
* when no longer processing, time to load the new files
|
1059 |
*/
|
1060 |
@Override
|
1061 |
protected void onPostExecute(Void arg1) { |
1062 |
try {
|
1063 |
new WriteFileTask().execute(app.getDownloadedEntity()
|
1064 |
.getContent()); |
1065 |
} catch (IllegalStateException e) { |
1066 |
showAlert("Error", "There was a problem downloading your file."); |
1067 |
e.printStackTrace(); |
1068 |
} catch (IOException e) { |
1069 |
showAlert("Error", "There was a problem downloading your file."); |
1070 |
e.printStackTrace(); |
1071 |
} |
1072 |
hideDialog(); |
1073 |
} |
1074 |
} |
1075 |
|
1076 |
class WriteFileTask extends AsyncTask<InputStream, Void, Boolean> { |
1077 |
@Override
|
1078 |
protected void onPreExecute() { |
1079 |
showDialog(); |
1080 |
} |
1081 |
|
1082 |
@Override
|
1083 |
protected Boolean doInBackground(InputStream... params) { |
1084 |
return writeFile(params[0]); |
1085 |
} |
1086 |
|
1087 |
@Override
|
1088 |
protected void onPostExecute(Boolean result) { |
1089 |
hideDialog(); |
1090 |
if (result) {
|
1091 |
downloadButton.setText("Open File");
|
1092 |
isDownloaded = true;
|
1093 |
} else {
|
1094 |
showAlert("Error", "There was a problem downloading your file."); |
1095 |
} |
1096 |
} |
1097 |
|
1098 |
} |
1099 |
|
1100 |
private boolean writeFile(InputStream in) { |
1101 |
String directoryName = Environment.getExternalStorageDirectory()
|
1102 |
.getPath(); |
1103 |
File f = new File(directoryName, DOWNLOAD_DIRECTORY); |
1104 |
Log.d(LOG, directoryName); |
1105 |
if (!f.isDirectory()) {
|
1106 |
if (!f.mkdir()) {
|
1107 |
return false; |
1108 |
} |
1109 |
} |
1110 |
Log.d(LOG, objects.toString()); |
1111 |
// String filename = directoryName + "/" + objects.getName();
|
1112 |
StringTokenizer str = new StringTokenizer(objects.getCName(), "/"); |
1113 |
String path = ""; |
1114 |
String fname = ""; |
1115 |
int count = str.countTokens();
|
1116 |
Log.d(LOG, "object is: " + objects.getCName() + " " + count); |
1117 |
for (int i = 0; i < count; i++) { |
1118 |
if (i < (count - 1)) { |
1119 |
path = path + str.nextToken() + "/";
|
1120 |
} else
|
1121 |
fname = str.nextToken(); |
1122 |
} |
1123 |
Log.d(LOG, "Path is:" + path);
|
1124 |
Log.d(LOG, "Fname is:" + fname);
|
1125 |
File object;
|
1126 |
if ("".equals(path)) { |
1127 |
object = new File(f, fname); |
1128 |
} else {
|
1129 |
File t = new File(f, path); |
1130 |
t.mkdirs(); |
1131 |
object = new File(t, fname); |
1132 |
} |
1133 |
|
1134 |
BufferedOutputStream bos = null; |
1135 |
|
1136 |
try {
|
1137 |
FileOutputStream fos = new FileOutputStream(object); |
1138 |
copy(in, fos); |
1139 |
} catch (IOException e) { |
1140 |
e.printStackTrace(); |
1141 |
} finally {
|
1142 |
if (bos != null) { |
1143 |
try {
|
1144 |
bos.flush(); |
1145 |
bos.close(); |
1146 |
} catch (IOException e) { |
1147 |
// TODO Auto-generated catch block
|
1148 |
e.printStackTrace(); |
1149 |
} |
1150 |
} |
1151 |
} |
1152 |
return true; |
1153 |
} |
1154 |
|
1155 |
public static long copy(InputStream input, OutputStream output) |
1156 |
throws IOException { |
1157 |
|
1158 |
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; |
1159 |
long count = 0; |
1160 |
int n = 0; |
1161 |
try {
|
1162 |
while (-1 != (n = input.read(buffer))) { |
1163 |
output.write(buffer, 0, n);
|
1164 |
count += n; |
1165 |
// monitor.setCurrent(count);
|
1166 |
} |
1167 |
} finally {
|
1168 |
input.close(); |
1169 |
output.close(); |
1170 |
} |
1171 |
return count;
|
1172 |
} |
1173 |
|
1174 |
@Override
|
1175 |
public void onBackPressed() { |
1176 |
|
1177 |
super.onBackPressed();
|
1178 |
} |
1179 |
|
1180 |
private class ContainerObjectUpdateTask extends |
1181 |
AsyncTask<Void, Void, HttpBundle> { |
1182 |
|
1183 |
private CloudServersException exception;
|
1184 |
|
1185 |
protected void onPreExecute() { |
1186 |
showDialog(); |
1187 |
} |
1188 |
|
1189 |
@Override
|
1190 |
protected HttpBundle doInBackground(Void... arg0) { |
1191 |
HttpBundle bundle = null;
|
1192 |
try {
|
1193 |
bundle = saveObject(); |
1194 |
} catch (CloudServersException e) {
|
1195 |
exception = e; |
1196 |
} |
1197 |
|
1198 |
return bundle;
|
1199 |
} |
1200 |
|
1201 |
@Override
|
1202 |
protected void onPostExecute(HttpBundle bundle) { |
1203 |
hideDialog(); |
1204 |
HttpResponse response = bundle.getResponse(); |
1205 |
if (response != null) { |
1206 |
int statusCode = response.getStatusLine().getStatusCode();
|
1207 |
if (statusCode == 202) { |
1208 |
hideDialog(); |
1209 |
setResult(99);
|
1210 |
finish(); |
1211 |
} else {
|
1212 |
CloudServersException cse = parseCloudServersException(response); |
1213 |
if ("".equals(cse.getMessage())) { |
1214 |
showError("There was a problem deleting your File.",
|
1215 |
bundle); |
1216 |
} else {
|
1217 |
showError("There was a problem deleting your file: "
|
1218 |
+ cse.getMessage(), bundle); |
1219 |
} |
1220 |
} |
1221 |
} else if (exception != null) { |
1222 |
showError("There was a problem deleting your file: "
|
1223 |
+ exception.getMessage(), bundle); |
1224 |
} |
1225 |
} |
1226 |
} |
1227 |
|
1228 |
private class VersionRestoreTask extends |
1229 |
AsyncTask<String, Void, HttpBundle> { |
1230 |
|
1231 |
private CloudServersException exception;
|
1232 |
|
1233 |
protected void onPreExecute() { |
1234 |
showDialog(); |
1235 |
} |
1236 |
|
1237 |
@Override
|
1238 |
protected HttpBundle doInBackground(String... arg0) { |
1239 |
HttpBundle bundle = null;
|
1240 |
try {
|
1241 |
bundle = restoreVersion(arg0[0]);
|
1242 |
} catch (CloudServersException e) {
|
1243 |
e.printStackTrace(); |
1244 |
exception = e; |
1245 |
} |
1246 |
|
1247 |
return bundle;
|
1248 |
} |
1249 |
|
1250 |
@Override
|
1251 |
protected void onPostExecute(HttpBundle bundle) { |
1252 |
hideDialog(); |
1253 |
HttpResponse response = null;
|
1254 |
if (bundle != null) |
1255 |
response = bundle.getResponse(); |
1256 |
if (response != null) { |
1257 |
int statusCode = response.getStatusLine().getStatusCode();
|
1258 |
if (statusCode == 204 || statusCode == 202) { |
1259 |
hideDialog(); |
1260 |
setResult(99);
|
1261 |
finish(); |
1262 |
} else {
|
1263 |
CloudServersException cse = parseCloudServersException(response); |
1264 |
if ("".equals(cse.getMessage())) { |
1265 |
showError("There was a problem deleting your File.",
|
1266 |
bundle); |
1267 |
} else {
|
1268 |
showError("There was a problem deleting your file: "
|
1269 |
+ cse.getMessage(), bundle); |
1270 |
} |
1271 |
} |
1272 |
} else if (exception != null) { |
1273 |
showError("There was a problem deleting your file: "
|
1274 |
+ exception.getMessage(), bundle); |
1275 |
} |
1276 |
} |
1277 |
} |
1278 |
|
1279 |
public HttpBundle saveObject() throws CloudServersException { |
1280 |
Map<String, String> headers = new HashMap<String, String>(); |
1281 |
if (objects.getMetadata() != null) |
1282 |
for (Entry<String, String> entry : objects.getMetadata().entrySet()) { |
1283 |
headers.put("X-Object-Meta-" + entry.getKey(), entry.getValue());
|
1284 |
} |
1285 |
for (String s : metadataRemoved) { |
1286 |
headers.put("X-Object-Meta-" + s, "~"); |
1287 |
} |
1288 |
if (!isFolder())
|
1289 |
headers.put("X-Object-Public", String.valueOf(objects.isPublicf())); |
1290 |
String read = ""; |
1291 |
String write = ""; |
1292 |
for (Permission p : objects.getPermissions()) { |
1293 |
if (p.isWrite()) {
|
1294 |
if (!write.equals("")) { |
1295 |
write = write + ",";
|
1296 |
} |
1297 |
write = write |
1298 |
+ (p.getGroup() == null ? p.getUser() : p.getGroup());
|
1299 |
} else if (p.isRead()) { |
1300 |
if (!read.equals("")) { |
1301 |
read = read + ",";
|
1302 |
} |
1303 |
read = read |
1304 |
+ (p.getGroup() == null ? p.getUser() : p.getGroup());
|
1305 |
} |
1306 |
|
1307 |
} |
1308 |
Log.d(LOG, "read:" + read);
|
1309 |
Log.d(LOG, "write:" + write);
|
1310 |
String permString = ""; |
1311 |
if (!"".equals(read)) { |
1312 |
permString = "read=" + read;
|
1313 |
} |
1314 |
if (!"".equals(write)) { |
1315 |
if (!"".equals(permString)) |
1316 |
permString = permString + ";write=" + write;
|
1317 |
else
|
1318 |
permString = "write=" + write;
|
1319 |
} |
1320 |
Log.d(LOG, permString); |
1321 |
if (!"".equals(permString)) { |
1322 |
headers.put("X-Object-Sharing", permString);
|
1323 |
} |
1324 |
HttpBundle b = new ContainerObjectManager(getApplicationContext())
|
1325 |
.updateObject(objects.getContainerName(), objects.getCName(), |
1326 |
"", null, headers, otherUser); |
1327 |
Log.d(LOG, "response:"
|
1328 |
+ b.getResponse().getStatusLine().getStatusCode()); |
1329 |
return b;
|
1330 |
} |
1331 |
|
1332 |
public HttpBundle restoreVersion(String version) |
1333 |
throws CloudServersException {
|
1334 |
Log.d(LOG, "Update version:" + version);
|
1335 |
Map<String, String> headers = new HashMap<String, String>(); |
1336 |
|
1337 |
headers.put("X-Source-Object", "/" + objects.getContainerName() + "/" |
1338 |
+ objects.getCName()); |
1339 |
headers.put("X-Source-Version", version);
|
1340 |
headers.put("Content-Range", "bytes 0-/*"); |
1341 |
HttpBundle b = new ContainerObjectManager(getApplicationContext())
|
1342 |
.updateObject(objects.getContainerName(), objects.getCName(), |
1343 |
"", "text/plain; charset=UTF-8", headers, otherUser); |
1344 |
Log.d(LOG, "response:"
|
1345 |
+ b.getResponse().getStatusLine().getStatusCode()); |
1346 |
return b;
|
1347 |
} |
1348 |
|
1349 |
} |