FileDetailSharingFragment.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * Copyright (C) 2018 Andy Scherzinger
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  9. * License as published by the Free Software Foundation; either
  10. * version 3 of the License, or any later version.
  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 AFFERO GENERAL PUBLIC LICENSE for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public
  18. * License 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.app.SearchManager;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.content.res.Resources;
  26. import android.net.Uri;
  27. import android.os.Bundle;
  28. import android.support.annotation.NonNull;
  29. import android.support.design.widget.Snackbar;
  30. import android.support.v4.app.Fragment;
  31. import android.support.v7.widget.AppCompatCheckBox;
  32. import android.support.v7.widget.LinearLayoutManager;
  33. import android.support.v7.widget.RecyclerView;
  34. import android.support.v7.widget.SearchView;
  35. import android.view.LayoutInflater;
  36. import android.view.Menu;
  37. import android.view.MenuItem;
  38. import android.view.View;
  39. import android.view.ViewGroup;
  40. import android.widget.ImageView;
  41. import android.widget.LinearLayout;
  42. import android.widget.PopupMenu;
  43. import android.widget.TextView;
  44. import com.owncloud.android.R;
  45. import com.owncloud.android.datamodel.FileDataStorageManager;
  46. import com.owncloud.android.datamodel.OCFile;
  47. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  48. import com.owncloud.android.lib.resources.shares.OCShare;
  49. import com.owncloud.android.lib.resources.shares.SharePermissionsBuilder;
  50. import com.owncloud.android.lib.resources.shares.ShareType;
  51. import com.owncloud.android.lib.resources.status.OCCapability;
  52. import com.owncloud.android.ui.activity.FileActivity;
  53. import com.owncloud.android.ui.adapter.UserListAdapter;
  54. import com.owncloud.android.ui.decoration.SimpleListItemDividerDecoration;
  55. import com.owncloud.android.ui.dialog.ExpirationDatePickerDialogFragment;
  56. import com.owncloud.android.ui.dialog.SharePasswordDialogFragment;
  57. import com.owncloud.android.ui.fragment.util.FileDetailSharingFragmentHelper;
  58. import com.owncloud.android.ui.fragment.util.SharingMenuHelper;
  59. import com.owncloud.android.utils.ThemeUtils;
  60. import java.util.ArrayList;
  61. import butterknife.BindView;
  62. import butterknife.ButterKnife;
  63. import butterknife.OnClick;
  64. import butterknife.Unbinder;
  65. public class FileDetailSharingFragment extends Fragment implements UserListAdapter.ShareeListAdapterListener {
  66. private static final String ARG_FILE = "FILE";
  67. private static final String ARG_ACCOUNT = "ACCOUNT";
  68. // to show share with users/groups info
  69. private ArrayList<OCShare> shares;
  70. private OCFile file;
  71. private Account account;
  72. private OCCapability capabilities;
  73. private OCShare publicShare;
  74. private Unbinder unbinder;
  75. @BindView(R.id.searchView)
  76. SearchView searchView;
  77. @BindView(R.id.shareUsersList)
  78. RecyclerView usersList;
  79. @BindView(R.id.shareNoUsers)
  80. TextView noList;
  81. @BindView(R.id.share_by_link)
  82. AppCompatCheckBox shareByLink;
  83. @BindView(R.id.overflow_menu_share_link)
  84. ImageView overflowMenuShareLink;
  85. @BindView(R.id.share_by_link_allow_editing)
  86. AppCompatCheckBox shareByLinkAllowEditing;
  87. @BindView(R.id.share_by_link_container)
  88. LinearLayout shareByLinkContainer;
  89. public static FileDetailSharingFragment newInstance(OCFile file, Account account) {
  90. FileDetailSharingFragment fragment = new FileDetailSharingFragment();
  91. Bundle args = new Bundle();
  92. args.putParcelable(ARG_FILE, file);
  93. args.putParcelable(ARG_ACCOUNT, account);
  94. fragment.setArguments(args);
  95. return fragment;
  96. }
  97. @Override
  98. public void onActivityCreated(Bundle savedInstanceState) {
  99. super.onActivityCreated(savedInstanceState);
  100. refreshCapabilitiesFromDB();
  101. refreshPublicShareFromDB();
  102. }
  103. @Override
  104. public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  105. file = getArguments().getParcelable(ARG_FILE);
  106. account = getArguments().getParcelable(ARG_ACCOUNT);
  107. if (savedInstanceState != null) {
  108. file = savedInstanceState.getParcelable(FileActivity.EXTRA_FILE);
  109. account = savedInstanceState.getParcelable(FileActivity.EXTRA_ACCOUNT);
  110. }
  111. View view = inflater.inflate(R.layout.file_details_sharing_fragment, container, false);
  112. unbinder = ButterKnife.bind(this, view);
  113. setupView();
  114. return view;
  115. }
  116. @Override
  117. public void onDestroy() {
  118. super.onDestroy();
  119. unbinder.unbind();
  120. }
  121. @Override
  122. public void onAttach(Context context) {
  123. super.onAttach(context);
  124. if (!(getActivity() instanceof FileActivity)) {
  125. throw new IllegalArgumentException("Calling activity must be of type FileActivity");
  126. }
  127. }
  128. private void setupView() {
  129. setShareByLinkInfo(file.isSharedViaLink());
  130. setShareWithUserInfo();
  131. FileDetailSharingFragmentHelper.setupSearchView(
  132. (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE),
  133. searchView,
  134. getActivity().getComponentName()
  135. );
  136. }
  137. /**
  138. * Updates Share by link UI
  139. *
  140. * @param isShareByLink flag is share by link is enable
  141. */
  142. public void setShareByLinkInfo(boolean isShareByLink) {
  143. shareByLink.setChecked(isShareByLink);
  144. int accentColor = ThemeUtils.primaryAccentColor(getContext());
  145. ThemeUtils.tintCheckbox(shareByLink, accentColor);
  146. ThemeUtils.tintCheckbox(shareByLinkAllowEditing, accentColor);
  147. setLinkDetailVisible(isShareByLink);
  148. }
  149. private void setLinkDetailVisible(boolean visible) {
  150. if (visible) {
  151. if (file.isFolder()) {
  152. shareByLinkAllowEditing.setVisibility(View.VISIBLE);
  153. } else {
  154. shareByLinkAllowEditing.setVisibility(View.INVISIBLE);
  155. }
  156. overflowMenuShareLink.setVisibility(View.VISIBLE);
  157. } else {
  158. shareByLinkAllowEditing.setVisibility(View.INVISIBLE);
  159. overflowMenuShareLink.setVisibility(View.INVISIBLE);
  160. }
  161. }
  162. /**
  163. * Update Share With data
  164. */
  165. public void setShareWithUserInfo() {
  166. // Get Users and Groups
  167. if (((FileActivity) getActivity()).getStorageManager() != null) {
  168. FileDataStorageManager fileDataStorageManager = ((FileActivity) getActivity()).getStorageManager();
  169. shares = fileDataStorageManager.getSharesWithForAFile(file.getRemotePath(), account.name);
  170. // Update list of users/groups
  171. updateListOfUserGroups();
  172. }
  173. }
  174. private void updateListOfUserGroups() {
  175. // TODO Refactoring: create a new {@link ShareUserListAdapter} instance with every call should not be needed
  176. if (shares.size() > 0) {
  177. usersList.setVisibility(View.VISIBLE);
  178. usersList.setAdapter(new UserListAdapter(getActivity().getSupportFragmentManager(),
  179. getActivity().getApplicationContext(), shares, account, file, this));
  180. usersList.setLayoutManager(new LinearLayoutManager(getContext()));
  181. usersList.addItemDecoration(new SimpleListItemDividerDecoration(getContext()));
  182. noList.setVisibility(View.GONE);
  183. } else {
  184. usersList.setVisibility(View.GONE);
  185. noList.setVisibility(View.VISIBLE);
  186. }
  187. }
  188. @OnClick(R.id.share_by_link)
  189. public void toggleShareByLink() {
  190. if (shareByLink.isChecked()) {
  191. if (capabilities != null &&
  192. capabilities.getFilesSharingPublicPasswordEnforced().isTrue()) {
  193. // password enforced by server, request to the user before trying to create
  194. requestPasswordForShareViaLink(true);
  195. } else {
  196. // create without password if not enforced by server or we don't know if enforced;
  197. ((FileActivity) getActivity()).getFileOperationsHelper().shareFileViaLink(file, null);
  198. }
  199. } else {
  200. ((FileActivity) getActivity()).getFileOperationsHelper().unshareFileViaLink(file);
  201. }
  202. }
  203. @OnClick(R.id.share_link_label)
  204. public void showSendLinkTo() {
  205. if (file.isSharedViaLink()) {
  206. ((FileActivity) getActivity()).getFileOperationsHelper().getFileWithLink(file);
  207. }
  208. }
  209. @OnClick(R.id.share_by_link_allow_editing)
  210. public void toggleShareLinkAllowEditing() {
  211. if (file.isSharedViaLink()) {
  212. ((FileActivity) getActivity()).getFileOperationsHelper()
  213. .setUploadPermissionsToShare(file, shareByLinkAllowEditing.isChecked());
  214. }
  215. }
  216. @OnClick(R.id.overflow_menu_share_link)
  217. public void showLinkOverflowMenu() {
  218. Context context = getContext();
  219. if (context != null && ThemeUtils.themingEnabled(context)) {
  220. // use grey as fallback for elements where custom theming is not available
  221. context.getTheme().applyStyle(R.style.FallbackThemingTheme, true);
  222. } else {
  223. context = getActivity();
  224. }
  225. PopupMenu popup = new PopupMenu(context, overflowMenuShareLink);
  226. popup.inflate(R.menu.file_detail_sharing_link_menu);
  227. prepareOptionsMenu(popup.getMenu());
  228. popup.setOnMenuItemClickListener(this::optionsItemSelected);
  229. popup.show();
  230. }
  231. private void prepareOptionsMenu(Menu menu) {
  232. Resources res = getResources();
  233. SharingMenuHelper.setupHideFileListingMenuItem(
  234. menu.findItem(R.id.action_hide_file_listing),
  235. file.isFolder(),
  236. shareByLinkAllowEditing.isChecked(),
  237. publicShare.getPermissions()
  238. );
  239. SharingMenuHelper.setupPasswordMenuItem(
  240. menu.findItem(R.id.action_password),
  241. publicShare.isPasswordProtected()
  242. );
  243. SharingMenuHelper.setupExpirationDateMenuItem(
  244. menu.findItem(R.id.action_share_expiration_date),
  245. publicShare.getExpirationDate(),
  246. res
  247. );
  248. }
  249. private boolean optionsItemSelected(MenuItem item) {
  250. switch (item.getItemId()) {
  251. case R.id.action_hide_file_listing: {
  252. item.setChecked(!item.isChecked());
  253. if (capabilities.getFilesFileDrop().isTrue()) {
  254. setHideFileListingPermissionsToShare(publicShare, item.isChecked());
  255. } else {
  256. // not supported in ownCloud
  257. showNotSupportedByOcMessage();
  258. }
  259. return true;
  260. }
  261. case R.id.action_password: {
  262. requestPasswordForShareViaLink(false);
  263. return true;
  264. }
  265. case R.id.action_expiration_date: {
  266. ExpirationDatePickerDialogFragment dialog = ExpirationDatePickerDialogFragment.newInstance(file, -1);
  267. dialog.show(
  268. getActivity().getSupportFragmentManager(),
  269. ExpirationDatePickerDialogFragment.DATE_PICKER_DIALOG
  270. );
  271. return true;
  272. }
  273. default:
  274. return super.onOptionsItemSelected(item);
  275. }
  276. }
  277. @Override
  278. public void setHideFileListingPermissionsToShare(OCShare share, boolean hideFileListing) {
  279. ((FileActivity) getActivity()).getFileOperationsHelper().
  280. setHideFileListingPermissionsToShare(share, hideFileListing);
  281. }
  282. @Override
  283. public void showNotSupportedByOcMessage() {
  284. if (getView() != null) {
  285. Snackbar.make(getView(), R.string.files_drop_not_supported, Snackbar.LENGTH_LONG)
  286. .setAction(R.string.learn_more, v -> {
  287. Intent i = new Intent(Intent.ACTION_VIEW);
  288. i.setData(Uri.parse(getString(R.string.url_server_install)));
  289. startActivity(i);
  290. })
  291. .show();
  292. }
  293. }
  294. /**
  295. * Updates the UI after the result of an update operation on the edited {@link OCFile}.
  296. *
  297. * @param result {@link RemoteOperationResult} of an update on the edited {@link OCFile} sharing information.
  298. * @param file the edited {@link OCFile}
  299. */
  300. public void onUpdateShareInformation(RemoteOperationResult result, OCFile file) {
  301. this.file = file;
  302. if (result.isSuccess()) {
  303. refreshUiFromDB();
  304. } else {
  305. setupView();
  306. }
  307. }
  308. /**
  309. * Get {@link OCShare} instance from DB and updates the UI.
  310. *
  311. * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
  312. * instance ready to use. If not ready, does nothing.
  313. */
  314. private void refreshUiFromDB() {
  315. FileDataStorageManager storageManager = ((FileActivity) getActivity()).getStorageManager();
  316. if (storageManager != null) {
  317. if (publicShare != null) {
  318. // Get edited shared by link
  319. publicShare = storageManager.getShareById(publicShare.getId());
  320. }
  321. // Updates UI with new state
  322. setupView();
  323. }
  324. }
  325. @Override
  326. public void unshareWith(OCShare share) {
  327. ((FileActivity) getActivity()).getFileOperationsHelper()
  328. .unshareFileWithUserOrGroup(file, share.getShareType(), share.getShareWith());
  329. }
  330. @Override
  331. public int updatePermissionsToShare(OCShare share, boolean canReshare, boolean canEdit,
  332. boolean canEditCreate, boolean canEditChange,
  333. boolean canEditDelete) {
  334. SharePermissionsBuilder spb = new SharePermissionsBuilder();
  335. spb.setSharePermission(canReshare);
  336. if (file.isFolder()) {
  337. spb.setUpdatePermission(canEditChange)
  338. .setCreatePermission(canEditCreate)
  339. .setDeletePermission(canEditDelete);
  340. } else {
  341. spb.setUpdatePermission(canEdit);
  342. }
  343. int permissions = spb.build();
  344. ((FileActivity) getActivity()).getFileOperationsHelper().
  345. setPermissionsToShare(
  346. share,
  347. permissions
  348. )
  349. ;
  350. return permissions;
  351. }
  352. /**
  353. * Starts a dialog that requests a password to the user to protect a share link.
  354. *
  355. * @param createShare When 'true', the request for password will be followed by the creation of a new
  356. * public link; when 'false', a public share is assumed to exist, and the password
  357. * is bound to it.
  358. */
  359. public void requestPasswordForShareViaLink(boolean createShare) {
  360. SharePasswordDialogFragment dialog = SharePasswordDialogFragment.newInstance(file, createShare);
  361. dialog.show(getChildFragmentManager(), SharePasswordDialogFragment.PASSWORD_FRAGMENT);
  362. }
  363. @Override
  364. public void requestPasswordForShare(OCShare share) {
  365. SharePasswordDialogFragment dialog = SharePasswordDialogFragment.newInstance(share);
  366. dialog.show(getChildFragmentManager(), SharePasswordDialogFragment.PASSWORD_FRAGMENT);
  367. }
  368. /**
  369. * Get known server capabilities from DB
  370. *
  371. * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
  372. * instance ready to use. If not ready, does nothing.
  373. */
  374. public void refreshCapabilitiesFromDB() {
  375. if (((FileActivity) getActivity()).getStorageManager() != null) {
  376. capabilities = ((FileActivity) getActivity()).getStorageManager().getCapability(account.name);
  377. }
  378. }
  379. /**
  380. * Get public link from the DB to fill in the "Share link" section in the UI.
  381. *
  382. * Takes into account server capabilities before reading database.
  383. *
  384. * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
  385. * instance ready to use. If not ready, does nothing.
  386. */
  387. public void refreshPublicShareFromDB() {
  388. if (FileDetailSharingFragmentHelper.isPublicShareDisabled(capabilities)) {
  389. shareByLinkContainer.setVisibility(View.GONE);
  390. } else if (((FileActivity) getActivity()).getStorageManager() != null) {
  391. // Get public share
  392. publicShare = ((FileActivity) getActivity()).getStorageManager().getFirstShareByPathAndType(
  393. file.getRemotePath(),
  394. ShareType.PUBLIC_LINK,
  395. ""
  396. );
  397. // Update public share section
  398. updatePublicShareSection();
  399. }
  400. }
  401. /**
  402. * Updates in the UI the section about public share with the information
  403. * in the current public share bound to, if any.
  404. */
  405. private void updatePublicShareSection() {
  406. if (publicShare != null && ShareType.PUBLIC_LINK.equals(publicShare.getShareType())) {
  407. shareByLink.setChecked(true);
  408. if (publicShare.getPermissions() > OCShare.READ_PERMISSION_FLAG) {
  409. shareByLinkAllowEditing.setChecked(true);
  410. } else {
  411. shareByLinkAllowEditing.setChecked(false);
  412. }
  413. setShareByLinkInfo(true);
  414. } else {
  415. setShareByLinkInfo(false);
  416. }
  417. }
  418. @Override
  419. public void onSaveInstanceState(@NonNull Bundle outState) {
  420. super.onSaveInstanceState(outState);
  421. outState.putParcelable(FileActivity.EXTRA_FILE, file);
  422. outState.putParcelable(FileActivity.EXTRA_ACCOUNT, account);
  423. }
  424. }