EditShareFragment.java 22 KB

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