From aab09c0c656e9f74ada7736a58306d086a08d4b0 Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Fri, 24 Sep 2021 20:14:31 +0200 Subject: [PATCH] Merge the Share process of the two classes into one A new class has been added in the util package: NewPipeTextViewHelper. It shares the selected text of a TextView with ShareUtils#shareText (with the created shareSelectedTextWithShareUtils static method). Only this static method can be used by other classes, other methods are private. --- .../newpipe/util/NewPipeTextViewHelper.java | 86 +++++++++++++++++++ .../schabi/newpipe/views/NewPipeEditText.java | 27 +----- .../schabi/newpipe/views/NewPipeTextView.java | 28 +----- 3 files changed, 94 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java diff --git a/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java b/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java new file mode 100644 index 000000000..31c87f7df --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java @@ -0,0 +1,86 @@ +package org.schabi.newpipe.util; + +import android.content.Context; +import android.text.Selection; +import android.text.Spannable; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import org.schabi.newpipe.util.external_communication.ShareUtils; +import org.schabi.newpipe.views.NewPipeEditText; +import org.schabi.newpipe.views.NewPipeTextView; + +public final class NewPipeTextViewHelper { + private NewPipeTextViewHelper() { + } + + /** + * Share the selected text of {@link NewPipeTextView NewPipeTextViews} and + * {@link NewPipeEditText NewPipeEditTexts} with + * {@link ShareUtils#shareText(Context, String, String)}. + * + *

+ * This allows EMUI users to get the Android share sheet instead of the EMUI share sheet when + * using the {@code Share} command of the popup menu which appears when selecting text. + *

+ * + * @param textView the {@link TextView} on which sharing the selected text. It should be a + * {@link NewPipeTextView} or a {@link NewPipeEditText} (even if + * {@link TextView standard TextViews} are supported). + * + * @return true if no exceptions occurred when getting the selected text, sharing it and + * deselecting it, otherwise an exception + */ + public static boolean shareSelectedTextWithShareUtils(@NonNull final TextView textView) { + if (textView instanceof NewPipeTextView) { + final NewPipeTextView newPipeTextView = (NewPipeTextView) textView; + final CharSequence text = newPipeTextView.getText(); + final CharSequence selectedText = getSelectedText(newPipeTextView, text); + + shareSelectedTextIfNotNullAndNotEmpty(newPipeTextView, selectedText); + + final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null; + Selection.setSelection(spannable, newPipeTextView.getSelectionEnd()); + } else if (textView instanceof NewPipeEditText) { + final NewPipeEditText editText = (NewPipeEditText) textView; + final Spannable text = editText.getText(); + + final CharSequence selectedText = getSelectedText(textView, text); + shareSelectedTextIfNotNullAndNotEmpty(textView, selectedText); + Selection.setSelection(text, editText.getSelectionEnd()); + } else { + final CharSequence text = textView.getText(); + final CharSequence selectedText = getSelectedText(textView, text); + + shareSelectedTextIfNotNullAndNotEmpty(textView, selectedText); + + final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null; + Selection.setSelection(spannable, textView.getSelectionEnd()); + } + + return true; + } + + @Nullable + private static CharSequence getSelectedText(@NonNull final TextView textView, + @Nullable final CharSequence text) { + if (!textView.hasSelection() || text == null) { + return null; + } + + final int start = textView.getSelectionStart(); + final int end = textView.getSelectionEnd(); + return String.valueOf(start > end ? text.subSequence(end, start) + : text.subSequence(start, end)); + } + + private static void shareSelectedTextIfNotNullAndNotEmpty( + @NonNull final TextView textView, + @Nullable final CharSequence selectedText) { + if (selectedText != null && selectedText.length() != 0) { + ShareUtils.shareText(textView.getContext(), "", selectedText.toString()); + } + } +} diff --git a/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java b/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java index 41d7640b6..cdb4f0041 100644 --- a/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java +++ b/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java @@ -1,8 +1,6 @@ package org.schabi.newpipe.views; import android.content.Context; -import android.text.Selection; -import android.text.Spannable; import android.util.AttributeSet; import androidx.annotation.NonNull; @@ -11,6 +9,8 @@ import androidx.appcompat.widget.AppCompatEditText; import org.schabi.newpipe.util.external_communication.ShareUtils; +import static org.schabi.newpipe.util.NewPipeTextViewHelper.shareSelectedTextWithShareUtils; + /** * An {@link AppCompatEditText} which uses {@link ShareUtils#shareText(Context, String, String)} * when sharing selected text by using the {@code Share} command of the floating actions. @@ -38,27 +38,8 @@ public class NewPipeEditText extends AppCompatEditText { @Override public boolean onTextContextMenuItem(final int id) { if (id == android.R.id.shareText) { - final Spannable text = getText(); - final CharSequence selectedText = getSelectedText(text); - if (selectedText != null && selectedText.length() != 0) { - ShareUtils.shareText(getContext(), "", selectedText.toString()); - } - Selection.setSelection(text, getSelectionEnd()); - return true; - } else { - return super.onTextContextMenuItem(id); + return shareSelectedTextWithShareUtils(this); } - } - - @Nullable - private CharSequence getSelectedText(@Nullable final CharSequence text) { - if (!hasSelection() || text == null) { - return null; - } - - final int start = getSelectionStart(); - final int end = getSelectionEnd(); - return String.valueOf(start > end ? text.subSequence(end, start) - : text.subSequence(start, end)); + return super.onTextContextMenuItem(id); } } diff --git a/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java b/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java index f333aae5d..75fb8f161 100644 --- a/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java +++ b/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java @@ -1,8 +1,6 @@ package org.schabi.newpipe.views; import android.content.Context; -import android.text.Selection; -import android.text.Spannable; import android.util.AttributeSet; import androidx.annotation.NonNull; @@ -11,6 +9,8 @@ import androidx.appcompat.widget.AppCompatTextView; import org.schabi.newpipe.util.external_communication.ShareUtils; +import static org.schabi.newpipe.util.NewPipeTextViewHelper.shareSelectedTextWithShareUtils; + /** * An {@link AppCompatTextView} which uses {@link ShareUtils#shareText(Context, String, String)} * when sharing selected text by using the {@code Share} command of the floating actions. @@ -38,28 +38,8 @@ public class NewPipeTextView extends AppCompatTextView { @Override public boolean onTextContextMenuItem(final int id) { if (id == android.R.id.shareText) { - final CharSequence text = getText(); - final CharSequence selectedText = getSelectedText(text); - if (selectedText != null && selectedText.length() != 0) { - ShareUtils.shareText(getContext(), "", selectedText.toString()); - } - final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null; - Selection.setSelection(spannable, getSelectionEnd()); - return true; - } else { - return super.onTextContextMenuItem(id); + return shareSelectedTextWithShareUtils(this); } - } - - @Nullable - private CharSequence getSelectedText(@Nullable final CharSequence text) { - if (!hasSelection() || text == null) { - return null; - } - - final int start = getSelectionStart(); - final int end = getSelectionEnd(); - return String.valueOf(start > end ? text.subSequence(end, start) - : text.subSequence(start, end)); + return super.onTextContextMenuItem(id); } }