SendShareDialog.java 9.1 KB

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