Revision 1637229b

b/src/com/rackspace/cloud/android/ContainerObjectDetails.java
85 85
	private List<ObjectVersion> versions = new ArrayList<ObjectVersion>();
86 86
	private boolean isReadOnly = false;
87 87
	private final List<String> metadataRemoved = new ArrayList<String>();
88
	private final Map<String,String> metadataAdded = new HashMap<String, String>();
89
	
88
	private final Map<String, String> metadataAdded = new HashMap<String, String>();
89

  
90 90
	/** Called when the activity is first created. */
91 91
	@Override
92 92
	public void onCreate(Bundle savedInstanceState) {
......
95 95

  
96 96
		objects = (ContainerObjects) this.getIntent().getExtras()
97 97
				.get("container");
98
		otherUser = (String) this.getIntent().getExtras()
99
				.get("otherUser");
100
		Log.i(LOG,"OTHERUSER:"+otherUser);
98
		otherUser = (String) this.getIntent().getExtras().get("otherUser");
99
		Log.i(LOG, "OTHERUSER:" + otherUser);
101 100
		containerNames = (String) this.getIntent().getExtras()
102 101
				.get("containerNames");
103 102
		cdnURL = (String) this.getIntent().getExtras().get("cdnUrl");
104 103
		cdnEnabled = (String) this.getIntent().getExtras().get("isCdnEnabled");
105
		
104

  
106 105
		setContentView(R.layout.viewobject);
107 106
		TabHost tabs = (TabHost) findViewById(R.id.tabhost2);
108 107

  
......
132 131
		restoreState(savedInstanceState);
133 132
	}
134 133

  
135
	public boolean isFolder(){
136
		return objects.getContentType().startsWith("application/folder")||objects.getContentType().startsWith("application/directory");
134
	public boolean isFolder() {
135
		return objects.getContentType().startsWith("application/folder")
136
				|| objects.getContentType().startsWith("application/directory");
137 137
	}
138

  
138 139
	@Override
139 140
	protected void onSaveInstanceState(Bundle outState) {
140 141
		super.onSaveInstanceState(outState);
......
153 154
		if (state != null && state.containsKey("container")) {
154 155
			objects = (ContainerObjects) state.getSerializable("container");
155 156
		}
156
		if(containerNames.equals(Container.MYSHARED)||containerNames.equals(Container.OTHERS))
157
		if (containerNames.equals(Container.MYSHARED)
158
				|| containerNames.equals(Container.OTHERS))
157 159
			loadObjectData();
158 160
		else
159 161
			new ContainerObjectRefreshTask().execute();
......
208 210
	}
209 211

  
210 212
	private void loadObjectData() {
211
		setTitle("Details: " +objects.getCName());
213
		setTitle("Details: " + objects.getCName());
212 214
		metadataRemoved.clear();
213 215
		metadataAdded.clear();
214
		if(Container.MYSHARED.equals(objects.getContainerName())||Container.MYSHARED.equals(containerNames)){
216
		if (Container.MYSHARED.equals(objects.getContainerName())
217
				|| Container.MYSHARED.equals(containerNames)) {
215 218
			isReadOnly = true;
216
			
219

  
217 220
		}
218 221
		// Object Name
219 222
		TextView name = (TextView) findViewById(R.id.view_container_name);
......
235 238

  
236 239
		// Content Type
237 240
		TextView cType = (TextView) findViewById(R.id.view_content_type);
238
		if(objects.getContentType()!=null)
241
		if (objects.getContentType() != null)
239 242
			cType.setText(objects.getContentType().toString());
240 243
		else
241 244
			cType.setText("");
......
247 250
		TextView lastmod = (TextView) findViewById(R.id.view_file_modification);
248 251
		lastmod.setText(strDate);
249 252
		rebuildMetadataList();
250
		if(isFolder()){
253
		if (isFolder()) {
251 254
			findViewById(R.id.download_text).setVisibility(View.GONE);
252 255
			findViewById(R.id.linearLayout1).setVisibility(View.GONE);
253 256
			findViewById(R.id.layoutPublic).setVisibility(View.GONE);
254 257
		}
255 258
		rebuildPermissionList();
256 259
		try {
257
			versions = new ContainerObjectManager(getApplicationContext())
258
					.getObjectVersions(objects.getContainerName(),
259
							objects.getCName(), otherUser);
260
			rebuildVersionList();
261
		} catch (CloudServersException e) {
262
			// TODO Auto-generated catch block
263
			e.printStackTrace();
260
			new AsyncTask<Void, Void, List<ObjectVersion>>() {
261

  
262
				@Override
263
				protected List<ObjectVersion> doInBackground(Void... params) {
264
					try {
265
						return new ContainerObjectManager(
266
								getApplicationContext()).getObjectVersions(
267
								objects.getContainerName(), objects.getCName(),
268
								otherUser);
269
					} catch (CloudServersException e) {
270
						Log.e("ERROR", "", e);
271
						return null;
272
					}
273

  
274
				}
275

  
276
				@Override
277
				protected void onPostExecute(List<ObjectVersion> result) {
278
					if (result != null) {
279
						versions = result;
280
						rebuildVersionList();
281
					}
282
				}
283

  
284
			};
285
		} catch (Exception e) {
286
			Log.e("ERROR THERE", "", e);
264 287
		}
265
		
288

  
266 289
	}
267
	
290

  
268 291
	private void rebuildMetadataList() {
269
		Button newmetadata = (Button)findViewById(R.id.newMetadata);
270
		if(isReadOnly)
292
		Button newmetadata = (Button) findViewById(R.id.newMetadata);
293
		if (isReadOnly)
271 294
			newmetadata.setVisibility(View.GONE);
272 295
		newmetadata.setOnClickListener(new OnClickListener() {
273
			
296

  
274 297
			@Override
275 298
			public void onClick(View arg0) {
276
				final AlertDialog.Builder alert = new AlertDialog.Builder(ContainerObjectDetails.this);
299
				final AlertDialog.Builder alert = new AlertDialog.Builder(
300
						ContainerObjectDetails.this);
277 301
				alert.setTitle("Add Metadata");
278 302
				LinearLayout ll = new LinearLayout(ContainerObjectDetails.this);
279 303
				ll.setOrientation(LinearLayout.VERTICAL);
280 304

  
281 305
				final EditText input = new EditText(ContainerObjectDetails.this);
282
				final EditText input2 = new EditText(ContainerObjectDetails.this);
306
				final EditText input2 = new EditText(
307
						ContainerObjectDetails.this);
283 308
				ll.addView(input);
284 309
				ll.addView(input2);
285 310
				alert.setView(ll);
286
				alert.setPositiveButton("Create", new DialogInterface.OnClickListener() {
287
					public void onClick(DialogInterface dialog, int whichButton) {
288
						String key = input.getText().toString().trim();
289
						String value = input2.getText().toString().trim();
290
						addMetatadata(key, value);
291
					}
292
				});
293
				
311
				alert.setPositiveButton("Create",
312
						new DialogInterface.OnClickListener() {
313
							public void onClick(DialogInterface dialog,
314
									int whichButton) {
315
								String key = input.getText().toString().trim();
316
								String value = input2.getText().toString()
317
										.trim();
318
								addMetatadata(key, value);
319
							}
320
						});
321

  
294 322
				alert.setNegativeButton("Cancel",
295 323
						new DialogInterface.OnClickListener() {
296
					public void onClick(DialogInterface dialog, int whichButton) {
297
						dialog.cancel();
298
					}
299
				});
324
							public void onClick(DialogInterface dialog,
325
									int whichButton) {
326
								dialog.cancel();
327
							}
328
						});
300 329
				alert.show();
301 330
			}
302 331
		});
......
320 349
			}
321 350
		}
322 351
	}
323
	
324
	private void addMetatadata(String key, String value){
325
		if(objects.getMetadata()==null)
352

  
353
	private void addMetatadata(String key, String value) {
354
		if (objects.getMetadata() == null)
326 355
			objects.setMetadata(new HashMap<String, String>());
327
		metadataAdded.put(key,value);
356
		metadataAdded.put(key, value);
328 357
		objects.getMetadata().put(key, value);
329 358
		rebuildMetadataList();
330 359
	}
......
335 364
		Log.d(LOG, i + " " + metadata.getKey() + " " + metadata.getValue());
336 365
		((TextView) v.findViewById(R.id.mkey)).setText(metadata.getKey());
337 366
		((TextView) v.findViewById(R.id.mvalue)).setText(metadata.getValue());
338
		if(isReadOnly)
367
		if (isReadOnly)
339 368
			v.findViewById(R.id.remove).setVisibility(View.GONE);
340 369
		((ImageButton) v.findViewById(R.id.remove))
341
		.setOnClickListener(new OnClickListener() {
370
				.setOnClickListener(new OnClickListener() {
342 371

  
343
			@Override
344
			public void onClick(View v1) {
345
				properties.removeView(v);
346
				metadataRemoved.add(metadata.getKey());
347
				objects.getMetadata().remove(metadata.getKey());
348
				
372
					@Override
373
					public void onClick(View v1) {
374
						properties.removeView(v);
375
						metadataRemoved.add(metadata.getKey());
376
						objects.getMetadata().remove(metadata.getKey());
349 377

  
350
			}
351
		});
378
					}
379
				});
352 380
	}
353 381

  
354 382
	private void rebuildPermissionList() {
355
		Button newmetadata = (Button)findViewById(R.id.newPermission);
356
		CheckBox readForAll = (CheckBox)findViewById(R.id.folderPublic);
357
		if(isReadOnly){
383
		Button newmetadata = (Button) findViewById(R.id.newPermission);
384
		CheckBox readForAll = (CheckBox) findViewById(R.id.folderPublic);
385
		if (isReadOnly) {
358 386
			newmetadata.setVisibility(View.GONE);
359 387
			readForAll.setEnabled(false);
360 388
		}
361
		objects.setPublicf(objects.getIsPublic()!=null);
389
		objects.setPublicf(objects.getIsPublic() != null);
362 390
		readForAll.setChecked(objects.isPublicf());
363 391

  
364 392
		readForAll.setOnCheckedChangeListener(new OnCheckedChangeListener() {
365 393

  
366
					@Override
367
					public void onCheckedChanged(CompoundButton buttonView,
368
							boolean isChecked) {
369
						objects.setPublicf(isChecked);
394
			@Override
395
			public void onCheckedChanged(CompoundButton buttonView,
396
					boolean isChecked) {
397
				objects.setPublicf(isChecked);
370 398

  
371
					}
372
				});
399
			}
400
		});
373 401
		newmetadata.setOnClickListener(new OnClickListener() {
374 402

  
375 403
			@Override
376 404
			public void onClick(View v) {
377
				showAddDialog();				
405
				showAddDialog();
378 406
			}
379
			
407

  
380 408
		});
381 409
		LayoutInflater layoutInflater = LayoutInflater
382 410
				.from(ContainerObjectDetails.this);
......
402 430
			final LinearLayout properties, int i) {
403 431

  
404 432
		properties.addView(v, i);
405
		if(isReadOnly){
433
		if (isReadOnly) {
406 434
			v.findViewById(R.id.remove).setVisibility(View.GONE);
407 435
			((CheckBox) v.findViewById(R.id.read)).setEnabled(false);
408 436
			((CheckBox) v.findViewById(R.id.write)).setEnabled(false);
409 437
		}
410
		
411
		((TextView) v.findViewById(R.id.ownerName)).setText(perm.getUser()==null?perm.getGroup()+":":perm.getUser());
438

  
439
		((TextView) v.findViewById(R.id.ownerName))
440
				.setText(perm.getUser() == null ? perm.getGroup() + ":" : perm
441
						.getUser());
412 442
		((CheckBox) v.findViewById(R.id.read)).setChecked(perm.isRead());
413 443
		((CheckBox) v.findViewById(R.id.write)).setChecked(perm.isWrite());
414 444

  
......
443 473
					}
444 474
				});
445 475
	}
446
	
447
	private void addPermission(boolean group, String userOrGroup){
448
		if(objects.getPermissions()==null)
476

  
477
	private void addPermission(boolean group, String userOrGroup) {
478
		if (objects.getPermissions() == null)
449 479
			objects.setPermissions(new ArrayList<Permission>());
450 480
		Permission np = new Permission();
451
		if(group)
481
		if (group)
452 482
			np.setGroup(userOrGroup);
453 483
		else
454 484
			np.setUser(userOrGroup);
455 485
		objects.getPermissions().add(np);
456 486
		rebuildPermissionList();
457 487
	}
458
	private void showAddDialog(){
459
		final CharSequence[] items = {"Add User", "Add Group"};
488

  
489
	private void showAddDialog() {
490
		final CharSequence[] items = { "Add User", "Add Group" };
460 491

  
461 492
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
462 493
		builder.setTitle("User Or Group");
463
		builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
464
		    public void onClick(DialogInterface dialog, int item) {
465
		    	boolean user;
466
		       if(item ==0){
467
		    	   user=true;
468
		       }
469
		       else
470
		    	   user=false;
471
		       populateAddDialog(user);
472
		       dialog.dismiss();
473
		       
474
		    }
475
		});
494
		builder.setSingleChoiceItems(items, -1,
495
				new DialogInterface.OnClickListener() {
496
					public void onClick(DialogInterface dialog, int item) {
497
						boolean user;
498
						if (item == 0) {
499
							user = true;
500
						} else
501
							user = false;
502
						populateAddDialog(user);
503
						dialog.dismiss();
504

  
505
					}
506
				});
476 507
		AlertDialog alert2 = builder.create();
477 508
		alert2.show();
478
		
509

  
479 510
	}
480
	
481
	private void populateAddDialog(final boolean user){
482
		final AlertDialog.Builder alert = new AlertDialog.Builder(ContainerObjectDetails.this);
483
       	if(user){
484
       		alert.setTitle("Add User");
485
       		final EditText input = new EditText(ContainerObjectDetails.this);
486
        	input.setTextColor(Color.BLACK);
487
    		
488
    		alert.setView(input);
489
    		
490
    		alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {
491
    			public void onClick(DialogInterface dialog, int whichButton) {
492
    				String value = input.getText().toString().trim();
493
    				addPermission(false, value);
494
    				
495
    			}
496
    		});
497

  
498
    		alert.setNegativeButton("Cancel",
499
    				new DialogInterface.OnClickListener() {
500
    					public void onClick(DialogInterface dialog, int whichButton) {
501
    						dialog.cancel();
502
    					
503
    					}
504
    				});
505
    		alert.show();
506
       	}
507
       	else{
508
       		AlertDialog.Builder builder = new AlertDialog.Builder(this);
509
    		builder.setTitle("Add Group");
510
    		builder.setSingleChoiceItems(getGroupNames().toArray(new String[0]), -1, new DialogInterface.OnClickListener() {
511
    		    public void onClick(DialogInterface dialog, int item) {
512
    		    	addPermission(true, getGroupNames().get(item));
513
    		       dialog.dismiss();
514
    		       
515
    		    }
516
    		});
517
    		AlertDialog alert2 = builder.create();
518
    		alert2.show();
519
       	}
520
    	
511

  
512
	private void populateAddDialog(final boolean user) {
513
		final AlertDialog.Builder alert = new AlertDialog.Builder(
514
				ContainerObjectDetails.this);
515
		if (user) {
516
			alert.setTitle("Add User");
517
			final EditText input = new EditText(ContainerObjectDetails.this);
518
			input.setTextColor(Color.BLACK);
519

  
520
			alert.setView(input);
521

  
522
			alert.setPositiveButton("Add",
523
					new DialogInterface.OnClickListener() {
524
						public void onClick(DialogInterface dialog,
525
								int whichButton) {
526
							String value = input.getText().toString().trim();
527
							addPermission(false, value);
528

  
529
						}
530
					});
531

  
532
			alert.setNegativeButton("Cancel",
533
					new DialogInterface.OnClickListener() {
534
						public void onClick(DialogInterface dialog,
535
								int whichButton) {
536
							dialog.cancel();
537

  
538
						}
539
					});
540
			alert.show();
541
		} else {
542
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
543
			builder.setTitle("Add Group");
544
			builder.setSingleChoiceItems(
545
					getGroupNames().toArray(new String[0]), -1,
546
					new DialogInterface.OnClickListener() {
547
						public void onClick(DialogInterface dialog, int item) {
548
							addPermission(true, getGroupNames().get(item));
549
							dialog.dismiss();
550

  
551
						}
552
					});
553
			AlertDialog alert2 = builder.create();
554
			alert2.show();
555
		}
556

  
521 557
	}
522
	
523
	public List<String> getGroupNames(){
558

  
559
	public List<String> getGroupNames() {
524 560
		List<String> result = new ArrayList<String>();
525
		List<GroupResource> groups = ((AndroidCloudApplication)getApplication()).getGroups();
526
		for(GroupResource g : groups){
561
		List<GroupResource> groups = ((AndroidCloudApplication) getApplication())
562
				.getGroups();
563
		for (GroupResource g : groups) {
527 564
			result.add(g.getName());
528 565
		}
529 566
		return result;
530 567
	}
531
	
568

  
532 569
	private void rebuildVersionList() {
533 570
		LayoutInflater layoutInflater = LayoutInflater
534 571
				.from(ContainerObjectDetails.this);
......
547 584
			i++;
548 585
		}
549 586
	}
550
	String selectedVersion=null;
587

  
588
	String selectedVersion = null;
589

  
551 590
	private void populateVersionList(final ObjectVersion perm, final View v,
552 591
			final LinearLayout properties, int i) {
553 592

  
......
555 594

  
556 595
		((TextView) v.findViewById(R.id.versionName)).setText("Version: "
557 596
				+ perm.getVersion());
558
		
597

  
559 598
		((TextView) v.findViewById(R.id.versionModified)).setText("Modified: "
560 599
				+ perm.getDateString());
561 600
		if (versions.size() == 1 || isReadOnly) {
......
595 634
					@Override
596 635
					public void onClick(View v1) {
597 636
						if (storageIsReady()) {
598
							new ContainerObjectDownloadTask().execute(String.valueOf(perm.getVersion()));
637
							new ContainerObjectDownloadTask().execute(String
638
									.valueOf(perm.getVersion()));
599 639
						} else {
600 640
							showAlert("Error", "Storage not found.");
601 641
						}
......
650 690
			showDialog(deleteObject);
651 691
			return true;
652 692
		case R.id.refresh:
653
			if(containerNames.equals(Container.MYSHARED)||containerNames.equals(Container.OTHERS))
693
			if (containerNames.equals(Container.MYSHARED)
694
					|| containerNames.equals(Container.OTHERS))
654 695
				loadObjectData();
655 696
			else
656 697
				new ContainerObjectRefreshTask().execute();
......
694 735
			return new AlertDialog.Builder(ContainerObjectDetails.this)
695 736
					.setIcon(R.drawable.alert_dialog_icon)
696 737
					.setTitle("Restore Version")
697
					.setMessage("Are you sure you want to restore this version?")
738
					.setMessage(
739
							"Are you sure you want to restore this version?")
698 740
					.setPositiveButton("Restore Version",
699 741
							new DialogInterface.OnClickListener() {
700 742
								public void onClick(DialogInterface dialog,
701 743
										int whichButton) {
702 744
									// User clicked OK so do some stuff
703
									/*trackEvent(GoogleAnalytics.CATEGORY_FILE,
704
											GoogleAnalytics.EVENT_DELETE, "",
705
											-1);*/
745
									/*
746
									 * trackEvent(GoogleAnalytics.CATEGORY_FILE,
747
									 * GoogleAnalytics.EVENT_DELETE, "", -1);
748
									 */
706 749
									new VersionRestoreTask()
707 750
											.execute(selectedVersion);
708 751
								}
......
714 757
									// User clicked Cancel so do some stuff
715 758
								}
716 759
							}).create();
717
		
760

  
718 761
		}
719
		
762

  
720 763
		return null;
721 764
	}
722 765

  
......
792 835
	}
793 836

  
794 837
	// Task's
795
	
838

  
796 839
	private class ContainerObjectRefreshTask extends
797
				AsyncTask<Void, Void, ContainerObjects> {
798
			
799
			private CloudServersException exception;
800
			
801
			protected void onPreExecute() {
802
				showDialog();
803
				app.setDeleteingObject(true);
804
				deleteObjTask = new DeleteObjectListenerTask();
805
				deleteObjTask.execute();
806
			}
807
			
808
			@Override
809
			protected ContainerObjects doInBackground(Void... arg0) {
810
				ContainerObjects bundle = null;
811
				try {
812
					if(otherUser==null)
813
						bundle = (new ContainerObjectManager(getContext()))
840
			AsyncTask<Void, Void, ContainerObjects> {
841

  
842
		private CloudServersException exception;
843

  
844
		protected void onPreExecute() {
845
			showDialog();
846
		}
847

  
848
		@Override
849
		protected ContainerObjects doInBackground(Void... arg0) {
850
			ContainerObjects bundle = null;
851
			try {
852
				if (otherUser == null)
853
					bundle = (new ContainerObjectManager(getContext()))
814 854
							.executeHead(containerNames, objects.getCName());
815
					else
816
						bundle = (new ContainerObjectManager(getContext()))
817
						.executeHead(containerNames, objects.getCName(), otherUser);
818
				} catch (CloudServersException e) {
819
					exception = e;
820
				}
821
			
822
				return bundle;
855
				else
856
					bundle = (new ContainerObjectManager(getContext()))
857
							.executeHead(containerNames, objects.getCName(),
858
									otherUser);
859
			} catch (CloudServersException e) {
860
				exception = e;
823 861
			}
824
			
825
			@Override
826
			protected void onPostExecute(ContainerObjects bundle) {
827
				hideDialog();
828
				if (bundle != null) {
829
					objects=bundle;
830
					loadObjectData();
831
				} else if (exception != null) {
832
					showToast("There was a problem refreshing your file: "
833
							+ exception.getMessage());
834
				}
862

  
863
			return bundle;
864
		}
865

  
866
		@Override
867
		protected void onPostExecute(ContainerObjects bundle) {
868
			hideDialog();
869
			if (bundle != null) {
870
				objects = bundle;
871
				loadObjectData();
872
			} else if (exception != null) {
873
				showToast("There was a problem refreshing your file: "
874
						+ exception.getMessage());
835 875
			}
876
		}
836 877
	}
837 878

  
838 879
	private class ContainerObjectDeleteTask extends
......
852 893
			HttpBundle bundle = null;
853 894
			try {
854 895
				bundle = (new ContainerObjectManager(getContext()))
855
						.deleteObject(containerNames, objects.getCName(),otherUser);
896
						.deleteObject(containerNames, objects.getCName(),
897
								otherUser);
856 898
			} catch (CloudServersException e) {
857 899
				exception = e;
858 900
			}
......
895 937
		protected void onPreExecute() {
896 938
			showDialog();
897 939
			app.setDownloadingObject(true);
898
			downloadObjTask = new DownloadObjectListenerTask();
899
			downloadObjTask.execute();
940
			
900 941
		}
901 942

  
902 943
		@Override
903 944
		protected HttpBundle doInBackground(String... arg0) {
904 945
			HttpBundle bundle = null;
905 946
			try {
906
				if(arg0.length==1 && arg0[0]!=null)
907
					bundle = (new ContainerObjectManager(getContext())).getObject(
908
							containerNames, objects.getCName(),arg0[0], otherUser);
947
				if (arg0.length == 1 && arg0[0] != null)
948
					bundle = (new ContainerObjectManager(getContext()))
949
							.getObject(containerNames, objects.getCName(),
950
									arg0[0], otherUser);
909 951
				else
910
					bundle = (new ContainerObjectManager(getContext())).getObject(
911
						containerNames, objects.getCName(),otherUser);
952
					bundle = (new ContainerObjectManager(getContext()))
953
							.getObject(containerNames, objects.getCName(),
954
									otherUser);
912 955
			} catch (CloudServersException e) {
956
				e.printStackTrace();
913 957
				exception = e;
958
			} catch (Exception exx) {
959
				exx.printStackTrace();
914 960
			}
915 961
			return bundle;
916 962
		}
917 963

  
918 964
		@Override
919 965
		protected void onPostExecute(HttpBundle bundle) {
920
			
921
			
966
			hideDialog();
922 967
			HttpResponse response = bundle.getResponse();
923 968
			if (response != null) {
924 969
				int statusCode = response.getStatusLine().getStatusCode();
......
926 971
					setResult(Activity.RESULT_OK);
927 972
					HttpEntity entity = response.getEntity();
928 973
					app.setDownloadedEntity(entity);
974
					downloadObjTask = new DownloadObjectListenerTask();
975
					downloadObjTask.execute();
929 976
				} else {
930 977
					CloudServersException cse = parseCloudServersException(response);
931 978
					if ("".equals(cse.getMessage())) {
......
991 1038
		 */
992 1039
		@Override
993 1040
		protected void onPostExecute(Void arg1) {
994
			
995 1041
			try {
996
				//TODO: run in background
997
				if (writeFile(app.getDownloadedEntity().getContent())) {
998
					downloadButton.setText("Open File");
999
					isDownloaded = true;
1000
				} else {
1001
					showAlert("Error",
1002
							"There was a problem downloading your file.");
1003
				}
1004

  
1005
			} catch (IOException e) {
1042
				new WriteFileTask().execute(app.getDownloadedEntity()
1043
						.getContent());
1044
			} catch (IllegalStateException e) {
1006 1045
				showAlert("Error", "There was a problem downloading your file.");
1007 1046
				e.printStackTrace();
1008
			} catch (Exception e) {
1047
			} catch (IOException e) {
1009 1048
				showAlert("Error", "There was a problem downloading your file.");
1010 1049
				e.printStackTrace();
1011 1050
			}
......
1013 1052
		}
1014 1053
	}
1015 1054

  
1055
	class WriteFileTask extends AsyncTask<InputStream, Void, Boolean> {
1056
		@Override
1057
		protected void onPreExecute() {
1058
			showDialog();
1059
		}
1060

  
1061
		@Override
1062
		protected Boolean doInBackground(InputStream... params) {
1063
			return writeFile(params[0]);
1064
		}
1065

  
1066
		@Override
1067
		protected void onPostExecute(Boolean result) {
1068
			hideDialog();
1069
			if (result) {
1070
				downloadButton.setText("Open File");
1071
				isDownloaded = true;
1072
			} else {
1073
				showAlert("Error", "There was a problem downloading your file.");
1074
			}
1075
		}
1076

  
1077
	}
1078

  
1016 1079
	private boolean writeFile(InputStream in) {
1017 1080
		String directoryName = Environment.getExternalStorageDirectory()
1018 1081
				.getPath();
1019 1082
		File f = new File(directoryName, DOWNLOAD_DIRECTORY);
1020
		Log.i(LOG,directoryName);
1083
		Log.i(LOG, directoryName);
1021 1084
		if (!f.isDirectory()) {
1022 1085
			if (!f.mkdir()) {
1023 1086
				return false;
1024 1087
			}
1025 1088
		}
1026
		Log.i(LOG,objects.toString());
1027
		//String filename = directoryName + "/" + objects.getName();
1028
		StringTokenizer str = new StringTokenizer(objects.getCName(),"/");
1029
		String path="";
1030
		String fname="";
1089
		Log.i(LOG, objects.toString());
1090
		// String filename = directoryName + "/" + objects.getName();
1091
		StringTokenizer str = new StringTokenizer(objects.getCName(), "/");
1092
		String path = "";
1093
		String fname = "";
1031 1094
		int count = str.countTokens();
1032
		Log.i(LOG,"object is: "+objects.getCName()+" "+count);
1033
		for(int i=0;i<count;i++){
1034
			if(i<(count-1)){
1035
				path = path+str.nextToken()+"/";
1036
			}
1037
			else
1038
				fname=str.nextToken();
1095
		Log.i(LOG, "object is: " + objects.getCName() + " " + count);
1096
		for (int i = 0; i < count; i++) {
1097
			if (i < (count - 1)) {
1098
				path = path + str.nextToken() + "/";
1099
			} else
1100
				fname = str.nextToken();
1039 1101
		}
1040
		Log.i(LOG,"Path is:"+path);
1041
		Log.i(LOG,"Fname is:"+fname);
1102
		Log.i(LOG, "Path is:" + path);
1103
		Log.i(LOG, "Fname is:" + fname);
1042 1104
		File object;
1043
		if("".equals(path)){
1044
			object = new File(f,fname);
1045
		}
1046
		else{
1047
			File t = new File(f,path);
1105
		if ("".equals(path)) {
1106
			object = new File(f, fname);
1107
		} else {
1108
			File t = new File(f, path);
1048 1109
			t.mkdirs();
1049
			object = new File(t,fname);
1110
			object = new File(t, fname);
1050 1111
		}
1051
		
1052
		
1112

  
1053 1113
		BufferedOutputStream bos = null;
1054 1114

  
1055 1115
		try {
......
1071 1131
		return true;
1072 1132
	}
1073 1133

  
1074
	public static long copy(InputStream input, OutputStream output) throws IOException {
1075
		
1134
	public static long copy(InputStream input, OutputStream output)
1135
			throws IOException {
1136

  
1076 1137
		byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
1077 1138
		long count = 0;
1078 1139
		int n = 0;
1079
		try{
1140
		try {
1080 1141
			while (-1 != (n = input.read(buffer))) {
1081 1142
				output.write(buffer, 0, n);
1082 1143
				count += n;
1083
				//monitor.setCurrent(count);
1144
				// monitor.setCurrent(count);
1084 1145
			}
1085
		}
1086
		finally{
1146
		} finally {
1087 1147
			input.close();
1088 1148
			output.close();
1089 1149
		}
1090 1150
		return count;
1091 1151
	}
1092
	
1152

  
1093 1153
	@Override
1094 1154
	public void onBackPressed() {
1095
		
1096
		//TODO: perform update
1155

  
1156
		// TODO: perform update
1097 1157
		super.onBackPressed();
1098 1158
	}
1099
	
1100
	
1101
	
1102
	
1159

  
1103 1160
	private class ContainerObjectUpdateTask extends
1104 1161
			AsyncTask<Void, Void, HttpBundle> {
1105
		
1162

  
1106 1163
		private CloudServersException exception;
1107
		
1164

  
1108 1165
		protected void onPreExecute() {
1109 1166
			showDialog();
1110 1167
		}
1111
		
1168

  
1112 1169
		@Override
1113 1170
		protected HttpBundle doInBackground(Void... arg0) {
1114 1171
			HttpBundle bundle = null;
......
1117 1174
			} catch (CloudServersException e) {
1118 1175
				exception = e;
1119 1176
			}
1120
		
1177

  
1121 1178
			return bundle;
1122 1179
		}
1123
		
1180

  
1124 1181
		@Override
1125 1182
		protected void onPostExecute(HttpBundle bundle) {
1126 1183
			hideDialog();
......
1147 1204
			}
1148 1205
		}
1149 1206
	}
1150
	
1207

  
1151 1208
	private class VersionRestoreTask extends
1152
				AsyncTask<String, Void, HttpBundle> {
1153
			
1154
			private CloudServersException exception;
1155
			
1156
			protected void onPreExecute() {
1157
				showDialog();
1158
			}
1159
			
1160
			@Override
1161
			protected HttpBundle doInBackground(String... arg0) {
1162
				HttpBundle bundle = null;
1163
				try {
1164
					bundle = restoreVersion(arg0[0]);
1165
				} catch (CloudServersException e) {
1166
					e.printStackTrace();
1167
					exception = e;
1168
				}
1169
			
1170
				return bundle;
1209
			AsyncTask<String, Void, HttpBundle> {
1210

  
1211
		private CloudServersException exception;
1212

  
1213
		protected void onPreExecute() {
1214
			showDialog();
1215
		}
1216

  
1217
		@Override
1218
		protected HttpBundle doInBackground(String... arg0) {
1219
			HttpBundle bundle = null;
1220
			try {
1221
				bundle = restoreVersion(arg0[0]);
1222
			} catch (CloudServersException e) {
1223
				e.printStackTrace();
1224
				exception = e;
1171 1225
			}
1172
			
1173
			@Override
1174
			protected void onPostExecute(HttpBundle bundle) {
1175
				hideDialog();
1176
				HttpResponse response = null;
1177
				if(bundle!=null)
1178
					response= bundle.getResponse();
1179
				if (response != null) {
1180
					int statusCode = response.getStatusLine().getStatusCode();
1181
					if (statusCode == 204||statusCode==202) {
1182
						hideDialog();
1183
						setResult(99);
1184
						finish();
1226

  
1227
			return bundle;
1228
		}
1229

  
1230
		@Override
1231
		protected void onPostExecute(HttpBundle bundle) {
1232
			hideDialog();
1233
			HttpResponse response = null;
1234
			if (bundle != null)
1235
				response = bundle.getResponse();
1236
			if (response != null) {
1237
				int statusCode = response.getStatusLine().getStatusCode();
1238
				if (statusCode == 204 || statusCode == 202) {
1239
					hideDialog();
1240
					setResult(99);
1241
					finish();
1242
				} else {
1243
					CloudServersException cse = parseCloudServersException(response);
1244
					if ("".equals(cse.getMessage())) {
1245
						showError("There was a problem deleting your File.",
1246
								bundle);
1185 1247
					} else {
1186
						CloudServersException cse = parseCloudServersException(response);
1187
						if ("".equals(cse.getMessage())) {
1188
							showError("There was a problem deleting your File.",
1189
									bundle);
1190
						} else {
1191
							showError("There was a problem deleting your file: "
1192
									+ cse.getMessage(), bundle);
1193
						}
1248
						showError("There was a problem deleting your file: "
1249
								+ cse.getMessage(), bundle);
1194 1250
					}
1195
				} else if (exception != null) {
1196
					showError("There was a problem deleting your file: "
1197
							+ exception.getMessage(), bundle);
1198 1251
				}
1252
			} else if (exception != null) {
1253
				showError("There was a problem deleting your file: "
1254
						+ exception.getMessage(), bundle);
1199 1255
			}
1200 1256
		}
1201
	
1202
	public HttpBundle saveObject() throws CloudServersException{
1203
		Map<String,String> headers = new HashMap<String,String>();
1204
		if(objects.getMetadata()!=null)
1205
			for(Entry<String,String> entry : objects.getMetadata().entrySet()){
1206
				headers.put("X-Object-Meta-"+entry.getKey(), entry.getValue());
1257
	}
1258

  
1259
	public HttpBundle saveObject() throws CloudServersException {
1260
		Map<String, String> headers = new HashMap<String, String>();
1261
		if (objects.getMetadata() != null)
1262
			for (Entry<String, String> entry : objects.getMetadata().entrySet()) {
1263
				headers.put("X-Object-Meta-" + entry.getKey(), entry.getValue());
1207 1264
			}
1208
		for(String s : metadataRemoved){
1209
			headers.put("X-Object-Meta-"+s, "~");
1265
		for (String s : metadataRemoved) {
1266
			headers.put("X-Object-Meta-" + s, "~");
1210 1267
		}
1211
		if(!isFolder())
1268
		if (!isFolder())
1212 1269
			headers.put("X-Object-Public", String.valueOf(objects.isPublicf()));
1213
		String read ="";
1214
		String write="";
1215
		for(Permission p : objects.getPermissions()){
1216
			if(p.isWrite()){
1217
				if(!write.equals("")){
1218
					write = write+",";
1270
		String read = "";
1271
		String write = "";
1272
		for (Permission p : objects.getPermissions()) {
1273
			if (p.isWrite()) {
1274
				if (!write.equals("")) {
1275
					write = write + ",";
1219 1276
				}
1220
				write = write + (p.getGroup()==null?p.getUser():p.getGroup());
1221
			}
1222
			else if(p.isRead()){
1223
				if(!read.equals("")){
1224
					read = read+",";
1277
				write = write
1278
						+ (p.getGroup() == null ? p.getUser() : p.getGroup());
1279
			} else if (p.isRead()) {
1280
				if (!read.equals("")) {
1281
					read = read + ",";
1225 1282
				}
1226
				read = read + (p.getGroup()==null?p.getUser():p.getGroup());
1283
				read = read
1284
						+ (p.getGroup() == null ? p.getUser() : p.getGroup());
1227 1285
			}
1228
			
1229
			
1286

  
1230 1287
		}
1231
		Log.d(LOG,"read:"+read);
1232
		Log.d(LOG,"write:"+write);
1288
		Log.d(LOG, "read:" + read);
1289
		Log.d(LOG, "write:" + write);
1233 1290
		String permString = "";
1234
		if(!"".equals(read)){
1235
			permString = "read="+read;
1291
		if (!"".equals(read)) {
1292
			permString = "read=" + read;
1236 1293
		}
1237
		if(!"".equals(write)){
1238
			if(!"".equals(permString))
1239
				permString = permString+";write="+write;
1294
		if (!"".equals(write)) {
1295
			if (!"".equals(permString))
1296
				permString = permString + ";write=" + write;
1240 1297
			else
1241
				permString = "write="+write;
1298
				permString = "write=" + write;
1242 1299
		}
1243
		Log.d(LOG,permString);
1244
		if(!"".equals(permString)){
1300
		Log.d(LOG, permString);
1301
		if (!"".equals(permString)) {
1245 1302
			headers.put("X-Object-Sharing", permString);
1246 1303
		}
1247
		HttpBundle b = new ContainerObjectManager(getApplicationContext()).updateObject(objects.getContainerName(), objects.getCName(), "", null, headers,otherUser);
1248
		Log.i(LOG,"response:"+b.getResponse().getStatusLine().getStatusCode());
1304
		HttpBundle b = new ContainerObjectManager(getApplicationContext())
1305
				.updateObject(objects.getContainerName(), objects.getCName(),
1306
						"", null, headers, otherUser);
1307
		Log.i(LOG, "response:"
1308
				+ b.getResponse().getStatusLine().getStatusCode());
1249 1309
		return b;
1250 1310
	}
1251
	
1252
	public HttpBundle restoreVersion(String version) throws CloudServersException{
1253
		Log.i(LOG,"Update version:"+version);
1254
		Map<String,String> headers=new HashMap<String, String>();
1255
		
1256
		headers.put("X-Source-Object","/"+objects.getContainerName()+"/"+objects.getCName());
1257
		headers.put("X-Source-Version",version);
1258
		headers.put("Content-Range","bytes 0-/*");
1259
		HttpBundle b = new ContainerObjectManager(getApplicationContext()).updateObject(objects.getContainerName(), objects.getCName(), "", "text/plain; charset=UTF-8", headers,otherUser);
1260
		Log.i(LOG,"response:"+b.getResponse().getStatusLine().getStatusCode());
1311

  
1312
	public HttpBundle restoreVersion(String version)
1313
			throws CloudServersException {
1314
		Log.i(LOG, "Update version:" + version);
1315
		Map<String, String> headers = new HashMap<String, String>();
1316

  
1317
		headers.put("X-Source-Object", "/" + objects.getContainerName() + "/"
1318
				+ objects.getCName());
1319
		headers.put("X-Source-Version", version);
1320
		headers.put("Content-Range", "bytes 0-/*");
1321
		HttpBundle b = new ContainerObjectManager(getApplicationContext())
1322
				.updateObject(objects.getContainerName(), objects.getCName(),
1323
						"", "text/plain; charset=UTF-8", headers, otherUser);
1324
		Log.i(LOG, "response:"
1325
				+ b.getResponse().getStatusLine().getStatusCode());
1261 1326
		return b;
1262 1327
	}
1263
	
1264 1328

  
1265 1329
}

Also available in: Unified diff