SendShareDialog.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.TextView;
  19. import com.owncloud.android.R;
  20. import com.owncloud.android.datamodel.OCFile;
  21. import com.owncloud.android.lib.common.utils.Log_OC;
  22. import com.owncloud.android.ui.activity.FileDisplayActivity;
  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 TAG = SendShareDialog.class.getSimpleName();
  53. public static final String PACKAGE_NAME = "PACKAGE_NAME";
  54. public static final String ACTIVITY_NAME = "ACTIVITY_NAME";
  55. private View view;
  56. private OCFile file;
  57. private FileOperationsHelper fileOperationsHelper;
  58. private FileDisplayActivity fileDisplayActivity;
  59. public static SendShareDialog newInstance(OCFile file) {
  60. SendShareDialog dialogFragment = new SendShareDialog();
  61. Bundle args = new Bundle();
  62. args.putParcelable(KEY_OCFILE, file);
  63. dialogFragment.setArguments(args);
  64. return dialogFragment;
  65. }
  66. @Override
  67. public void onCreate(@Nullable Bundle savedInstanceState) {
  68. super.onCreate(savedInstanceState);
  69. // keep the state of the fragment on configuration changes
  70. setRetainInstance(true);
  71. view = null;
  72. file = getArguments().getParcelable(KEY_OCFILE);
  73. }
  74. @Nullable
  75. @Override
  76. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  77. view = inflater.inflate(R.layout.send_share_fragment, container, false);
  78. // Share with people
  79. TextView sharePeopleText = view.findViewById(R.id.share_people_button);
  80. sharePeopleText.setOnClickListener(v -> shareFile(file));
  81. ImageView sharePeopleImageView = view.findViewById(R.id.share_people_icon);
  82. themeShareButtonImage(sharePeopleImageView);
  83. sharePeopleImageView.setOnClickListener(v -> shareFile(file));
  84. // Share via link button
  85. TextView shareLinkText = view.findViewById(R.id.share_link_button);
  86. shareLinkText.setOnClickListener(v -> shareFile(file));
  87. ImageView shareLinkImageView = view.findViewById(R.id.share_link_icon);
  88. themeShareButtonImage(shareLinkImageView);
  89. shareLinkImageView.setOnClickListener(v -> shareFile(file));
  90. if (file.isSharedWithMe() && !file.canReshare()) {
  91. showResharingNotAllowedSnackbar();
  92. if (file.isFolder()) {
  93. shareLinkText.setVisibility(View.GONE);
  94. shareLinkImageView.setVisibility(View.GONE);
  95. sharePeopleText.setVisibility(View.GONE);
  96. sharePeopleImageView.setVisibility(View.GONE);
  97. getDialog().hide();
  98. } else {
  99. shareLinkText.setEnabled(false);
  100. shareLinkText.setAlpha(0.3f);
  101. shareLinkImageView.setEnabled(false);
  102. shareLinkImageView.setAlpha(0.3f);
  103. sharePeopleText.setEnabled(false);
  104. sharePeopleText.setAlpha(0.3f);
  105. sharePeopleImageView.setEnabled(false);
  106. sharePeopleImageView.setAlpha(0.3f);
  107. }
  108. }
  109. // populate send apps
  110. Intent sendIntent = createSendIntent();
  111. List<SendButtonData> sendButtonDataList = setupSendButtonData(sendIntent);
  112. if (getContext().getString(R.string.send_files_to_other_apps).equalsIgnoreCase("off")) {
  113. sharePeopleText.setVisibility(View.GONE);
  114. }
  115. SendButtonAdapter.ClickListener clickListener = setupSendButtonClickListener(sendIntent);
  116. RecyclerView sendButtonsView = view.findViewById(R.id.send_button_recycler_view);
  117. sendButtonsView.setHasFixedSize(true);
  118. sendButtonsView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
  119. sendButtonsView.setAdapter(new SendButtonAdapter(sendButtonDataList, clickListener));
  120. return view;
  121. }
  122. private void themeShareButtonImage(ImageView shareImageView) {
  123. shareImageView.getBackground().setColorFilter(ThemeUtils.elementColor(), PorterDuff.Mode.SRC_IN);
  124. shareImageView.getDrawable().setColorFilter(ThemeUtils.fontColor(), PorterDuff.Mode.SRC_IN);
  125. }
  126. private void showResharingNotAllowedSnackbar() {
  127. Snackbar snackbar = Snackbar.make(view, R.string.resharing_is_not_allowed, Snackbar.LENGTH_LONG);
  128. snackbar.addCallback(new Snackbar.Callback() {
  129. @Override
  130. public void onDismissed(Snackbar transientBottomBar, int event) {
  131. super.onDismissed(transientBottomBar, event);
  132. if (file.isFolder()) {
  133. dismiss();
  134. }
  135. }
  136. });
  137. snackbar.show();
  138. }
  139. @NonNull
  140. private SendButtonAdapter.ClickListener setupSendButtonClickListener(Intent sendIntent) {
  141. return sendButtonDataData -> {
  142. if (MimeTypeUtil.isImage(file) && !file.isDown()) {
  143. fileOperationsHelper.sendCachedImage(file);
  144. } else {
  145. String packageName = sendButtonDataData.getPackageName();
  146. String activityName = sendButtonDataData.getActivityName();
  147. // Obtain the file
  148. if (file.isDown()) {
  149. sendIntent.setComponent(new ComponentName(packageName, activityName));
  150. getActivity().startActivity(Intent.createChooser(sendIntent, getString(R.string.send)));
  151. } else { // Download the file
  152. Log_OC.d(TAG, file.getRemotePath() + ": File must be downloaded");
  153. ((SendShareDialog.SendShareDialogDownloader) getActivity()).downloadFile(file, packageName,
  154. activityName);
  155. }
  156. }
  157. dismiss();
  158. };
  159. }
  160. @NonNull
  161. private List<SendButtonData> setupSendButtonData(Intent sendIntent) {
  162. List<SendButtonData> sendButtonDataList = new ArrayList<>();
  163. for (ResolveInfo match : getActivity().getPackageManager().queryIntentActivities(sendIntent, 0)) {
  164. Drawable icon = match.loadIcon(getActivity().getPackageManager());
  165. CharSequence label = match.loadLabel(getActivity().getPackageManager());
  166. SendButtonData sendButtonData = new SendButtonData(icon, label,
  167. match.activityInfo.packageName,
  168. match.activityInfo.name);
  169. sendButtonDataList.add(sendButtonData);
  170. }
  171. return sendButtonDataList;
  172. }
  173. @NonNull
  174. private Intent createSendIntent() {
  175. Intent sendIntent = new Intent(Intent.ACTION_SEND);
  176. sendIntent.setType(file.getMimetype());
  177. sendIntent.putExtra(Intent.EXTRA_STREAM, file.getExposedFileUri(getActivity()));
  178. sendIntent.putExtra(Intent.ACTION_SEND, true);
  179. return sendIntent;
  180. }
  181. private void shareFile(OCFile file) {
  182. fileOperationsHelper.showShareFile(file);
  183. dismiss();
  184. }
  185. public void setFileOperationsHelper(FileOperationsHelper fileOperationsHelper) {
  186. this.fileOperationsHelper = fileOperationsHelper;
  187. }
  188. public interface SendShareDialogDownloader {
  189. void downloadFile(OCFile file, String packageName, String activityName);
  190. }
  191. }