EditShareFragment.java 22 KB

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