FileDetailSharingFragment.java 20 KB

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