EditShareFragment.java 20 KB

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