EditShareFragment.java 22 KB

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