FileDetailSharingFragment.java 20 KB

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