SendShareDialog.java 9.5 KB

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