From 267e114354ba2d1ee210cfa2d3526665b5470469 Mon Sep 17 00:00:00 2001 From: adinilfeld Date: Wed, 10 Jun 2020 15:14:08 -0700 Subject: [PATCH] added a copyToClipboard method to ShareUtils, and modified CommentsMiniInfoItemHolder and VideoDetailFragment to use the new method. --- .../fragments/detail/VideoDetailFragment.java | 7 +---- .../holder/CommentsMiniInfoItemHolder.java | 11 ++------ .../org/schabi/newpipe/util/ShareUtils.java | 26 +++++++++++++++++++ 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java index a8b007bf4..44f18a4ea 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.java @@ -105,7 +105,6 @@ import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.CompositeDisposable; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers; -import us.shandian.giga.util.Utility; import static org.schabi.newpipe.extractor.StreamingService.ServiceInfo.MediaCapability.COMMENTS; import static org.schabi.newpipe.extractor.stream.StreamExtractor.NO_AGE_LIMIT; @@ -489,7 +488,7 @@ public class VideoDetailFragment extends BaseStateFragment } break; case R.id.detail_title_root_layout: - copyTitleText(); + ShareUtils.copyToClipboard(getContext(), videoTitleTextView.getText().toString()); break; } @@ -1428,8 +1427,4 @@ public class VideoDetailFragment extends BaseStateFragment animateView(detailPositionView, false, 500); }); } - - private void copyTitleText() { - Utility.copyToClipboard(getContext(), videoTitleTextView.getText().toString()); - } } diff --git a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java index 69f08f340..d94762c4d 100644 --- a/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java +++ b/app/src/main/java/org/schabi/newpipe/info_list/holder/CommentsMiniInfoItemHolder.java @@ -1,15 +1,11 @@ package org.schabi.newpipe.info_list.holder; -import android.content.ClipData; -import android.content.ClipboardManager; -import android.content.Context; import android.text.TextUtils; import android.text.method.LinkMovementMethod; import android.text.style.URLSpan; import android.text.util.Linkify; import android.view.ViewGroup; import android.widget.TextView; -import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; @@ -24,6 +20,7 @@ import org.schabi.newpipe.util.CommentTextOnTouchListener; import org.schabi.newpipe.util.ImageDisplayConstants; import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.NavigationHelper; +import org.schabi.newpipe.util.ShareUtils; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -130,11 +127,7 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder { itemView.setOnLongClickListener(view -> { if (!AndroidTvUtils.isTv(itemBuilder.getContext())) { - ClipboardManager clipboardManager = (ClipboardManager) itemBuilder.getContext() - .getSystemService(Context.CLIPBOARD_SERVICE); - clipboardManager.setPrimaryClip(ClipData.newPlainText(null, commentText)); - Toast.makeText(itemBuilder.getContext(), R.string.msg_copied, Toast.LENGTH_SHORT) - .show(); + ShareUtils.copyToClipboard(itemBuilder.getContext(), commentText); } else { openCommentAuthor(item); } diff --git a/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java index 0bf731a98..1b87f4f14 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/ShareUtils.java @@ -1,10 +1,13 @@ package org.schabi.newpipe.util; +import android.content.ClipData; +import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; +import android.widget.Toast; import org.schabi.newpipe.R; @@ -78,4 +81,27 @@ public final class ShareUtils { context.startActivity(Intent.createChooser( intent, context.getString(R.string.share_dialog_title))); } + + /** + * Copy the text to clipboard, and indicate to the user whether the operation was completed + * successfully using a Toast. + * + * @param context the context to use + * @param text the text to copy + */ + public static void copyToClipboard(final Context context, final String text) { + ClipboardManager clipboardManager = + (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); + + if (clipboardManager == null) { + Toast.makeText(context, + R.string.permission_denied, + Toast.LENGTH_LONG).show(); + return; + } + + clipboardManager.setPrimaryClip(ClipData.newPlainText(null, text)); + Toast.makeText(context, R.string.msg_copied, Toast.LENGTH_SHORT) + .show(); + } }