EditShareFragment.java 19 KB

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