FileDetailSharingFragment.java 17 KB

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