SendShareDialog.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package com.owncloud.android.ui.dialog;
  2. import android.content.ComponentName;
  3. import android.content.Intent;
  4. import android.content.pm.ResolveInfo;
  5. import android.graphics.PorterDuff;
  6. import android.graphics.drawable.Drawable;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ImageView;
  12. import android.widget.LinearLayout;
  13. import android.widget.TextView;
  14. import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
  15. import com.google.android.material.snackbar.Snackbar;
  16. import com.owncloud.android.R;
  17. import com.owncloud.android.datamodel.OCFile;
  18. import com.owncloud.android.lib.common.utils.Log_OC;
  19. import com.owncloud.android.lib.resources.status.OCCapability;
  20. import com.owncloud.android.ui.activity.FileActivity;
  21. import com.owncloud.android.ui.activity.FileDisplayActivity;
  22. import com.owncloud.android.ui.adapter.SendButtonAdapter;
  23. import com.owncloud.android.ui.components.SendButtonData;
  24. import com.owncloud.android.ui.helpers.FileOperationsHelper;
  25. import com.owncloud.android.utils.MimeTypeUtil;
  26. import com.owncloud.android.utils.ThemeUtils;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import androidx.annotation.NonNull;
  30. import androidx.annotation.Nullable;
  31. import androidx.recyclerview.widget.GridLayoutManager;
  32. import androidx.recyclerview.widget.RecyclerView;
  33. /*
  34. * Nextcloud Android client application
  35. *
  36. * @author Tobias Kaminsky
  37. * @author Andy Scherzinger
  38. * Copyright (C) 2017 Tobias Kaminsky
  39. * Copyright (C) 2017 Nextcloud GmbH.
  40. * Copyright (C) 2018 Andy Scherzinger
  41. *
  42. * This program is free software: you can redistribute it and/or modify
  43. * it under the terms of the GNU Affero General Public License as published by
  44. * the Free Software Foundation, either version 3 of the License, or
  45. * at your option) any later version.
  46. *
  47. * This program is distributed in the hope that it will be useful,
  48. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  49. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  50. * GNU Affero General Public License for more details.
  51. *
  52. * You should have received a copy of the GNU Affero General Public License
  53. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  54. */
  55. public class SendShareDialog extends BottomSheetDialogFragment {
  56. private static final String KEY_OCFILE = "KEY_OCFILE";
  57. private static final String KEY_SHARING_PUBLIC_PASSWORD_ENFORCED = "KEY_SHARING_PUBLIC_PASSWORD_ENFORCED";
  58. private static final String KEY_SHARING_PUBLIC_ASK_FOR_PASSWORD = "KEY_SHARING_PUBLIC_ASK_FOR_PASSWORD";
  59. private static final String KEY_HIDE_NCSHARING_OPTIONS = "KEY_HIDE_NCSHARING_OPTIONS";
  60. private static final String TAG = SendShareDialog.class.getSimpleName();
  61. public static final String PACKAGE_NAME = "PACKAGE_NAME";
  62. public static final String ACTIVITY_NAME = "ACTIVITY_NAME";
  63. private View view;
  64. private OCFile file;
  65. private boolean hideNcSharingOptions;
  66. private boolean sharingPublicPasswordEnforced;
  67. private boolean sharingPublicAskForPassword;
  68. private FileOperationsHelper fileOperationsHelper;
  69. public static SendShareDialog newInstance(OCFile file, boolean hideNcSharingOptions, OCCapability capability) {
  70. SendShareDialog dialogFragment = new SendShareDialog();
  71. Bundle args = new Bundle();
  72. args.putParcelable(KEY_OCFILE, file);
  73. args.putBoolean(KEY_HIDE_NCSHARING_OPTIONS, hideNcSharingOptions);
  74. args.putBoolean(KEY_SHARING_PUBLIC_PASSWORD_ENFORCED,
  75. capability.getFilesSharingPublicPasswordEnforced().isTrue());
  76. args.putBoolean(KEY_SHARING_PUBLIC_ASK_FOR_PASSWORD,
  77. capability.getFilesSharingPublicAskForOptionalPassword().isTrue());
  78. dialogFragment.setArguments(args);
  79. return dialogFragment;
  80. }
  81. @Override
  82. public void onCreate(@Nullable Bundle savedInstanceState) {
  83. super.onCreate(savedInstanceState);
  84. // keep the state of the fragment on configuration changes
  85. setRetainInstance(true);
  86. file = getArguments().getParcelable(KEY_OCFILE);
  87. hideNcSharingOptions = getArguments().getBoolean(KEY_HIDE_NCSHARING_OPTIONS, false);
  88. sharingPublicPasswordEnforced = getArguments().getBoolean(KEY_SHARING_PUBLIC_PASSWORD_ENFORCED, false);
  89. sharingPublicAskForPassword = getArguments().getBoolean(KEY_SHARING_PUBLIC_ASK_FOR_PASSWORD);
  90. }
  91. @Nullable
  92. @Override
  93. public View onCreateView(@NonNull LayoutInflater inflater,
  94. @Nullable ViewGroup container,
  95. @Nullable Bundle savedInstanceState) {
  96. view = inflater.inflate(R.layout.send_share_fragment, container, false);
  97. LinearLayout sendShareButtons = view.findViewById(R.id.send_share_buttons);
  98. View divider = view.findViewById(R.id.divider);
  99. // Share with people
  100. TextView sharePeopleText = view.findViewById(R.id.share_people_button);
  101. sharePeopleText.setOnClickListener(v -> shareFile(file));
  102. ImageView sharePeopleImageView = view.findViewById(R.id.share_people_icon);
  103. themeShareButtonImage(sharePeopleImageView);
  104. sharePeopleImageView.setOnClickListener(v -> shareFile(file));
  105. // Share via link button
  106. TextView shareLinkText = view.findViewById(R.id.share_link_button);
  107. shareLinkText.setOnClickListener(v -> shareByLink());
  108. ImageView shareLinkImageView = view.findViewById(R.id.share_link_icon);
  109. themeShareButtonImage(shareLinkImageView);
  110. shareLinkImageView.setOnClickListener(v -> shareByLink());
  111. if (hideNcSharingOptions) {
  112. sendShareButtons.setVisibility(View.GONE);
  113. divider.setVisibility(View.GONE);
  114. } else if (file.isSharedWithMe() && !file.canReshare()) {
  115. showResharingNotAllowedSnackbar();
  116. if (file.isFolder()) {
  117. shareLinkText.setVisibility(View.GONE);
  118. shareLinkImageView.setVisibility(View.GONE);
  119. sharePeopleText.setVisibility(View.GONE);
  120. sharePeopleImageView.setVisibility(View.GONE);
  121. getDialog().hide();
  122. } else {
  123. shareLinkText.setEnabled(false);
  124. shareLinkText.setAlpha(0.3f);
  125. shareLinkImageView.setEnabled(false);
  126. shareLinkImageView.setAlpha(0.3f);
  127. sharePeopleText.setEnabled(false);
  128. sharePeopleText.setAlpha(0.3f);
  129. sharePeopleImageView.setEnabled(false);
  130. sharePeopleImageView.setAlpha(0.3f);
  131. }
  132. }
  133. // populate send apps
  134. Intent sendIntent = createSendIntent();
  135. List<SendButtonData> sendButtonDataList = setupSendButtonData(sendIntent);
  136. if ("off".equalsIgnoreCase(getContext().getString(R.string.send_files_to_other_apps))) {
  137. sharePeopleText.setVisibility(View.GONE);
  138. }
  139. SendButtonAdapter.ClickListener clickListener = setupSendButtonClickListener(sendIntent);
  140. RecyclerView sendButtonsView = view.findViewById(R.id.send_button_recycler_view);
  141. sendButtonsView.setHasFixedSize(true);
  142. sendButtonsView.setLayoutManager(new GridLayoutManager(getActivity(), 4));
  143. sendButtonsView.setAdapter(new SendButtonAdapter(sendButtonDataList, clickListener));
  144. return view;
  145. }
  146. private void shareByLink() {
  147. if (file.isSharedViaLink()) {
  148. ((FileActivity) getActivity()).getFileOperationsHelper().getFileWithLink(file);
  149. } else if (sharingPublicPasswordEnforced) {
  150. // password enforced by server, request to the user before trying to create
  151. requestPasswordForShareViaLink();
  152. } else {
  153. // create without password if not enforced by server or we don't know if enforced;
  154. ((FileActivity) getActivity()).getFileOperationsHelper().shareFileViaPublicShare(file, null);
  155. }
  156. this.dismiss();
  157. }
  158. private void requestPasswordForShareViaLink() {
  159. SharePasswordDialogFragment dialog = SharePasswordDialogFragment.newInstance(file,
  160. true,
  161. sharingPublicAskForPassword);
  162. dialog.show(getFragmentManager(), SharePasswordDialogFragment.PASSWORD_FRAGMENT);
  163. }
  164. private void themeShareButtonImage(ImageView shareImageView) {
  165. shareImageView.getBackground().setColorFilter(ThemeUtils.primaryColor(getContext()), PorterDuff.Mode.SRC_IN);
  166. shareImageView.getDrawable().mutate().setColorFilter(ThemeUtils.fontColor(getContext()),
  167. PorterDuff.Mode.SRC_IN);
  168. }
  169. private void showResharingNotAllowedSnackbar() {
  170. Snackbar snackbar = Snackbar.make(view, R.string.resharing_is_not_allowed, Snackbar.LENGTH_LONG);
  171. snackbar.addCallback(new Snackbar.Callback() {
  172. @Override
  173. public void onDismissed(Snackbar transientBottomBar, int event) {
  174. super.onDismissed(transientBottomBar, event);
  175. if (file.isFolder()) {
  176. dismiss();
  177. }
  178. }
  179. });
  180. snackbar.show();
  181. }
  182. @NonNull
  183. private SendButtonAdapter.ClickListener setupSendButtonClickListener(Intent sendIntent) {
  184. return sendButtonDataData -> {
  185. String packageName = sendButtonDataData.getPackageName();
  186. String activityName = sendButtonDataData.getActivityName();
  187. if (MimeTypeUtil.isImage(file) && !file.isDown()) {
  188. fileOperationsHelper.sendCachedImage(file, packageName, activityName);
  189. } else {
  190. // Obtain the file
  191. if (file.isDown()) {
  192. sendIntent.setComponent(new ComponentName(packageName, activityName));
  193. getActivity().startActivity(Intent.createChooser(sendIntent, getString(R.string.send)));
  194. } else { // Download the file
  195. Log_OC.d(TAG, file.getRemotePath() + ": File must be downloaded");
  196. ((SendShareDialog.SendShareDialogDownloader) getActivity()).downloadFile(file, packageName,
  197. activityName);
  198. }
  199. }
  200. dismiss();
  201. };
  202. }
  203. @NonNull
  204. private List<SendButtonData> setupSendButtonData(Intent sendIntent) {
  205. List<SendButtonData> sendButtonDataList = new ArrayList<>();
  206. for (ResolveInfo match : getActivity().getPackageManager().queryIntentActivities(sendIntent, 0)) {
  207. Drawable icon = match.loadIcon(getActivity().getPackageManager());
  208. CharSequence label = match.loadLabel(getActivity().getPackageManager());
  209. SendButtonData sendButtonData = new SendButtonData(icon, label,
  210. match.activityInfo.packageName,
  211. match.activityInfo.name);
  212. sendButtonDataList.add(sendButtonData);
  213. }
  214. return sendButtonDataList;
  215. }
  216. @NonNull
  217. private Intent createSendIntent() {
  218. Intent sendIntent = new Intent(Intent.ACTION_SEND);
  219. sendIntent.setType(file.getMimeType());
  220. sendIntent.putExtra(Intent.EXTRA_STREAM, file.getExposedFileUri(getActivity()));
  221. sendIntent.putExtra(Intent.ACTION_SEND, true);
  222. return sendIntent;
  223. }
  224. private void shareFile(OCFile file) {
  225. if (getActivity() instanceof FileDisplayActivity) {
  226. ((FileDisplayActivity) getActivity()).showDetails(file, 1);
  227. } else {
  228. fileOperationsHelper.showShareFile(file);
  229. }
  230. dismiss();
  231. }
  232. public void setFileOperationsHelper(FileOperationsHelper fileOperationsHelper) {
  233. this.fileOperationsHelper = fileOperationsHelper;
  234. }
  235. public interface SendShareDialogDownloader {
  236. void downloadFile(OCFile file, String packageName, String activityName);
  237. }
  238. }