EditShareFragment.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * @author David A. Velasco
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.ui.fragment;
  22. import android.accounts.Account;
  23. import android.os.Bundle;
  24. import android.support.v4.app.Fragment;
  25. import android.support.v7.widget.SwitchCompat;
  26. import android.view.LayoutInflater;
  27. import android.view.View;
  28. import android.view.ViewGroup;
  29. import android.widget.CheckBox;
  30. import android.widget.CompoundButton;
  31. import android.widget.TextView;
  32. import com.owncloud.android.R;
  33. import com.owncloud.android.datamodel.FileDataStorageManager;
  34. import com.owncloud.android.datamodel.OCFile;
  35. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  36. import com.owncloud.android.lib.common.utils.Log_OC;
  37. import com.owncloud.android.lib.resources.shares.OCShare;
  38. import com.owncloud.android.lib.resources.shares.SharePermissionsBuilder;
  39. import com.owncloud.android.lib.resources.shares.ShareType;
  40. import com.owncloud.android.ui.activity.FileActivity;
  41. public class EditShareFragment extends Fragment {
  42. private static final String TAG = EditShareFragment.class.getSimpleName();
  43. /** The fragment initialization parameters */
  44. private static final String ARG_SHARE = "SHARE";
  45. private static final String ARG_FILE = "FILE";
  46. private static final String ARG_ACCOUNT = "ACCOUNT";
  47. /** Ids of CheckBoxes depending on R.id.canEdit CheckBox */
  48. private static final int sSubordinateCheckBoxIds[] = {
  49. R.id.canEditCreateCheckBox,
  50. R.id.canEditChangeCheckBox,
  51. R.id.canEditDeleteCheckBox
  52. };
  53. /** Share to show & edit, received as a parameter in construction time */
  54. private OCShare mShare;
  55. /** File bound to mShare, received as a parameter in construction time */
  56. private OCFile mFile;
  57. /** Listener for changes on privilege checkboxes */
  58. private CompoundButton.OnCheckedChangeListener mOnPrivilegeChangeListener;
  59. /**
  60. * Public factory method to create new EditShareFragment instances.
  61. *
  62. * @param shareToEdit An {@link OCShare} to show and edit in the fragment
  63. * @param sharedFile The {@link OCFile} bound to 'shareToEdit'
  64. * @param account The ownCloud account holding 'sharedFile'
  65. * @return A new instance of fragment EditShareFragment.
  66. */
  67. public static EditShareFragment newInstance(OCShare shareToEdit, OCFile sharedFile, Account account) {
  68. EditShareFragment fragment = new EditShareFragment();
  69. Bundle args = new Bundle();
  70. args.putParcelable(ARG_SHARE, shareToEdit);
  71. args.putParcelable(ARG_FILE, sharedFile);
  72. args.putParcelable(ARG_ACCOUNT, account);
  73. fragment.setArguments(args);
  74. return fragment;
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. @Override
  80. public void onCreate(Bundle savedInstanceState) {
  81. super.onCreate(savedInstanceState);
  82. Log_OC.d(TAG, "onCreate");
  83. if (getArguments() != null) {
  84. mShare = getArguments().getParcelable(ARG_SHARE);
  85. mFile = getArguments().getParcelable(ARG_FILE);
  86. /* OC account holding the shared file, received as a parameter in construction time */
  87. //Account mAccount = getArguments().getParcelable(ARG_ACCOUNT);
  88. }
  89. }
  90. @Override
  91. public void onActivityCreated(Bundle savedInstanceState) {
  92. super.onActivityCreated(savedInstanceState);
  93. Log_OC.d(TAG, "onActivityCreated");
  94. getActivity().setTitle(mShare.getSharedWithDisplayName());
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. @Override
  100. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  101. Bundle savedInstanceState) {
  102. Log_OC.d(TAG, "onCreateView");
  103. // Inflate the layout for this fragment
  104. View view = inflater.inflate(R.layout.edit_share_layout, container, false);
  105. ((TextView)view.findViewById(R.id.editShareTitle)).setText(
  106. getResources().getString(R.string.share_with_edit_title, mShare.getSharedWithDisplayName()));
  107. // Setup layout
  108. refreshUiFromState(view);
  109. return view;
  110. }
  111. /**
  112. * Updates the UI with the current permissions in the edited {@OCShare}
  113. *
  114. * @param editShareView Root view in the fragment.
  115. */
  116. private void refreshUiFromState(View editShareView) {
  117. if (editShareView != null) {
  118. setPermissionsListening(editShareView, false);
  119. int sharePermissions = mShare.getPermissions();
  120. boolean isFederated = ShareType.FEDERATED.equals(mShare.getShareType());
  121. CompoundButton compound;
  122. compound = (CompoundButton) editShareView.findViewById(R.id.canShareSwitch);
  123. if(isFederated) {
  124. compound.setVisibility(View.INVISIBLE);
  125. }
  126. compound.setChecked((sharePermissions & OCShare.SHARE_PERMISSION_FLAG) > 0);
  127. compound = (CompoundButton) editShareView.findViewById(R.id.canEditSwitch);
  128. int anyUpdatePermission =
  129. OCShare.CREATE_PERMISSION_FLAG |
  130. OCShare.UPDATE_PERMISSION_FLAG |
  131. OCShare.DELETE_PERMISSION_FLAG;
  132. boolean canEdit = (sharePermissions & anyUpdatePermission) > 0;
  133. compound.setChecked(canEdit);
  134. if (mFile.isFolder() && !isFederated) {
  135. compound = (CompoundButton) editShareView.findViewById(R.id.canEditCreateCheckBox);
  136. compound.setChecked((sharePermissions & OCShare.CREATE_PERMISSION_FLAG) > 0);
  137. compound.setVisibility(canEdit ? View.VISIBLE : View.GONE);
  138. compound = (CompoundButton) editShareView.findViewById(R.id.canEditChangeCheckBox);
  139. compound.setChecked((sharePermissions & OCShare.UPDATE_PERMISSION_FLAG) > 0);
  140. compound.setVisibility(canEdit ? View.VISIBLE : View.GONE);
  141. compound = (CompoundButton) editShareView.findViewById(R.id.canEditDeleteCheckBox);
  142. compound.setChecked((sharePermissions & OCShare.DELETE_PERMISSION_FLAG) > 0);
  143. compound.setVisibility(canEdit ? View.VISIBLE : View.GONE);
  144. }
  145. setPermissionsListening(editShareView, true);
  146. }
  147. }
  148. /**
  149. * Binds or unbinds listener for user actions to enable or disable a permission on the edited share
  150. * to the views receiving the user events.
  151. *
  152. * @param editShareView Root view in the fragment.
  153. * @param enable When 'true', listener is bound to view; when 'false', it is unbound.
  154. */
  155. private void setPermissionsListening(View editShareView, boolean enable) {
  156. if (enable && mOnPrivilegeChangeListener == null) {
  157. mOnPrivilegeChangeListener = new OnPrivilegeChangeListener();
  158. }
  159. CompoundButton.OnCheckedChangeListener changeListener = enable ? mOnPrivilegeChangeListener : null;
  160. CompoundButton compound;
  161. compound = (CompoundButton) editShareView.findViewById(R.id.canShareSwitch);
  162. compound.setOnCheckedChangeListener(changeListener);
  163. compound = (CompoundButton) editShareView.findViewById(R.id.canEditSwitch);
  164. compound.setOnCheckedChangeListener(changeListener);
  165. if (mFile.isFolder()) {
  166. compound = (CompoundButton) editShareView.findViewById(R.id.canEditCreateCheckBox);
  167. compound.setOnCheckedChangeListener(changeListener);
  168. compound = (CompoundButton) editShareView.findViewById(R.id.canEditChangeCheckBox);
  169. compound.setOnCheckedChangeListener(changeListener);
  170. compound = (CompoundButton) editShareView.findViewById(R.id.canEditDeleteCheckBox);
  171. compound.setOnCheckedChangeListener(changeListener);
  172. }
  173. }
  174. /**
  175. * Listener for user actions that enable or disable a privilege
  176. */
  177. private class OnPrivilegeChangeListener
  178. implements CompoundButton.OnCheckedChangeListener {
  179. /**
  180. * Called by every {@link SwitchCompat} and {@link CheckBox} in the fragment to update
  181. * the state of its associated permission.
  182. *
  183. * @param compound {@link CompoundButton} toggled by the user
  184. * @param isChecked New switch state.
  185. */
  186. @Override
  187. public void onCheckedChanged(CompoundButton compound, boolean isChecked) {
  188. if (!isResumed()) {
  189. // very important, setCheched(...) is called automatically during
  190. // Fragment recreation on device rotations
  191. return;
  192. }
  193. /// else, getView() cannot be NULL
  194. CompoundButton subordinate;
  195. switch(compound.getId()) {
  196. case R.id.canShareSwitch:
  197. Log_OC.v(TAG, "canShareCheckBox toggled to " + isChecked);
  198. updatePermissionsToShare();
  199. break;
  200. case R.id.canEditSwitch:
  201. Log_OC.v(TAG, "canEditCheckBox toggled to " + isChecked);
  202. /// sync subordinate CheckBoxes
  203. boolean isFederated = ShareType.FEDERATED.equals(mShare.getShareType());
  204. if (mFile.isFolder()) {
  205. if (isChecked) {
  206. if (!isFederated) {
  207. /// not federated shares -> enable all the subpermisions
  208. for (int i = 0; i < sSubordinateCheckBoxIds.length; i++) {
  209. //noinspection ConstantConditions, prevented in the method beginning
  210. subordinate = (CompoundButton) getView().findViewById(sSubordinateCheckBoxIds[i]);
  211. subordinate.setVisibility(View.VISIBLE);
  212. if (!subordinate.isChecked() &&
  213. !mFile.isSharedWithMe()) { // see (1)
  214. toggleDisablingListener(subordinate);
  215. }
  216. }
  217. } else {
  218. /// federated share -> enable delete subpermission, as server side; TODO why?
  219. //noinspection ConstantConditions, prevented in the method beginning
  220. subordinate = (CompoundButton) getView().findViewById(R.id.canEditDeleteCheckBox);
  221. if (!subordinate.isChecked()) {
  222. toggleDisablingListener(subordinate);
  223. }
  224. }
  225. } else {
  226. for (int i = 0; i < sSubordinateCheckBoxIds.length; i++) {
  227. //noinspection ConstantConditions, prevented in the method beginning
  228. subordinate = (CompoundButton) getView().findViewById(sSubordinateCheckBoxIds[i]);
  229. subordinate.setVisibility(View.GONE);
  230. if (subordinate.isChecked()) {
  231. toggleDisablingListener(subordinate);
  232. }
  233. }
  234. }
  235. }
  236. if(!(mFile.isFolder() && isChecked && mFile.isSharedWithMe()) // see (1)
  237. || isFederated ) {
  238. updatePermissionsToShare();
  239. }
  240. // updatePermissionsToShare() // see (1)
  241. // (1) These modifications result in an exceptional UI behaviour for the case
  242. // where the switch 'can edit' is enabled for a *reshared folder*; if the same
  243. // behaviour was applied than for owned folder, and the user did not have full
  244. // permissions to update the folder, an error would be reported by the server
  245. // and the children checkboxes would be automatically hidden again
  246. break;
  247. case R.id.canEditCreateCheckBox:
  248. Log_OC.v(TAG, "canEditCreateCheckBox toggled to " + isChecked);
  249. syncCanEditSwitch(compound, isChecked);
  250. updatePermissionsToShare();
  251. break;
  252. case R.id.canEditChangeCheckBox:
  253. Log_OC.v(TAG, "canEditChangeCheckBox toggled to " + isChecked);
  254. syncCanEditSwitch(compound, isChecked);
  255. updatePermissionsToShare();
  256. break;
  257. case R.id.canEditDeleteCheckBox:
  258. Log_OC.v(TAG, "canEditDeleteCheckBox toggled to " + isChecked);
  259. syncCanEditSwitch(compound, isChecked);
  260. updatePermissionsToShare();
  261. break;
  262. }
  263. }
  264. /**
  265. * Sync value of "can edit" {@link SwitchCompat} according to a change in one of its subordinate checkboxes.
  266. *
  267. * If all the subordinates are disabled, "can edit" has to be disabled.
  268. *
  269. * If any subordinate is enabled, "can edit" has to be enabled.
  270. *
  271. * @param subordinateCheckBoxView Subordinate {@link CheckBox} that was changed.
  272. * @param isChecked 'true' iif subordinateCheckBoxView was checked.
  273. */
  274. private void syncCanEditSwitch(View subordinateCheckBoxView, boolean isChecked) {
  275. CompoundButton canEditCompound = (CompoundButton) getView().findViewById(R.id.canEditSwitch);
  276. if (isChecked) {
  277. if (!canEditCompound.isChecked()) {
  278. toggleDisablingListener(canEditCompound);
  279. }
  280. } else {
  281. boolean allDisabled = true;
  282. for (int i=0; allDisabled && i<sSubordinateCheckBoxIds.length; i++) {
  283. allDisabled &=
  284. sSubordinateCheckBoxIds[i] == subordinateCheckBoxView.getId() ||
  285. !((CheckBox) getView().findViewById(sSubordinateCheckBoxIds[i])).isChecked()
  286. ;
  287. }
  288. if (canEditCompound.isChecked() && allDisabled) {
  289. toggleDisablingListener(canEditCompound);
  290. for (int i=0; i<sSubordinateCheckBoxIds.length; i++) {
  291. getView().findViewById(sSubordinateCheckBoxIds[i]).setVisibility(View.GONE);
  292. }
  293. }
  294. }
  295. }
  296. /**
  297. * Toggle value of received {@link CompoundButton} granting that its change listener is not called.
  298. *
  299. * @param compound {@link CompoundButton} (switch or checkBox) to toggle without reporting to
  300. * the change listener
  301. */
  302. private void toggleDisablingListener(CompoundButton compound) {
  303. compound.setOnCheckedChangeListener(null);
  304. compound.toggle();
  305. compound.setOnCheckedChangeListener(this);
  306. }
  307. }
  308. /**
  309. * Updates the UI after the result of an update operation on the edited {@link OCShare} permissions.
  310. *
  311. * @param result Result of an update on the edited {@link OCShare} permissions.
  312. */
  313. public void onUpdateSharePermissionsFinished(RemoteOperationResult result) {
  314. if (result.isSuccess()) {
  315. refreshUiFromDB(getView());
  316. } else {
  317. refreshUiFromState(getView());
  318. }
  319. }
  320. /**
  321. * Get {@link OCShare} instance from DB and updates the UI.
  322. *
  323. * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
  324. * instance ready to use. If not ready, does nothing.
  325. */
  326. public void refreshUiFromDB() {
  327. if (getView() != null) {
  328. refreshUiFromDB(getView());
  329. }
  330. }
  331. /**
  332. * Get {@link OCShare} instance from DB and updates the UI.
  333. *
  334. * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
  335. * instance ready to use. If not ready, does nothing.
  336. *
  337. * @param editShareView Root view in the fragment.
  338. */
  339. private void refreshUiFromDB(View editShareView) {
  340. FileDataStorageManager storageManager = ((FileActivity) getActivity()).getStorageManager();
  341. if (storageManager != null) {
  342. // Get edited share
  343. mShare = storageManager.getShareById(mShare.getId());
  344. // Updates UI with new state
  345. refreshUiFromState(editShareView);
  346. }
  347. }
  348. /**
  349. * Updates the permissions of the {@link OCShare} according to the values set in the UI
  350. */
  351. private void updatePermissionsToShare() {
  352. SharePermissionsBuilder spb = new SharePermissionsBuilder();
  353. spb.setSharePermission(getCanShareSwitch().isChecked());
  354. if (mFile.isFolder()) {
  355. spb.setUpdatePermission(getCanEditChangeCheckBox().isChecked())
  356. .setCreatePermission(getCanEditCreateCheckBox().isChecked())
  357. .setDeletePermission(getCanEditDeleteCheckBox().isChecked());
  358. } else {
  359. spb.setUpdatePermission(getCanEditSwitch().isChecked());
  360. }
  361. int permissions = spb.build();
  362. ((FileActivity) getActivity()).getFileOperationsHelper().
  363. setPermissionsToShare(
  364. mShare,
  365. permissions
  366. )
  367. ;
  368. }
  369. /**
  370. * Shortcut to access {@link SwitchCompat} R.id.canShareSwitch
  371. *
  372. * @return {@link SwitchCompat} R.id.canShareCheckBox or null if called before
  373. * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} finished.
  374. */
  375. private SwitchCompat getCanShareSwitch() {
  376. return (SwitchCompat) getView().findViewById(R.id.canShareSwitch);
  377. }
  378. /**
  379. * Shortcut to access {@link SwitchCompat} R.id.canEditSwitch
  380. *
  381. * @return {@link SwitchCompat} R.id.canEditSwitch or null if called before
  382. * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} finished.
  383. */
  384. private SwitchCompat getCanEditSwitch() {
  385. return (SwitchCompat) getView().findViewById(R.id.canEditSwitch);
  386. }
  387. /**
  388. * Shortcut to access {@link CheckBox} R.id.canEditCreateCheckBox
  389. *
  390. * @return {@link CheckBox} R.id.canEditCreateCheckBox or null if called before
  391. * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} finished.
  392. */
  393. private CheckBox getCanEditCreateCheckBox() {
  394. return (CheckBox) getView().findViewById(R.id.canEditCreateCheckBox);
  395. }
  396. /**
  397. * Shortcut to access {@link CheckBox} R.id.canEditChangeCheckBox
  398. *
  399. * @return {@link CheckBox} R.id.canEditChangeCheckBox or null if called before
  400. * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} finished.
  401. */
  402. private CheckBox getCanEditChangeCheckBox() {
  403. return (CheckBox) getView().findViewById(R.id.canEditChangeCheckBox);
  404. }
  405. /**
  406. * Shortcut to access {@link CheckBox} R.id.canEditDeleteCheckBox
  407. *
  408. * @return {@link CheckBox} R.id.canEditDeleteCheckBox or null if called before
  409. * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} finished.
  410. */
  411. private CheckBox getCanEditDeleteCheckBox() {
  412. return (CheckBox) getView().findViewById(R.id.canEditDeleteCheckBox);
  413. }
  414. }