EditShareFragment.java 20 KB

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