From 411b3129f9989afa586453ae6e174148b45ae946 Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Sat, 4 Sep 2021 19:40:24 +0200 Subject: [PATCH] Use a custom EditText everywhere to be able to share with ShareUtils the selected text This EditText class extends the AppCompatEditText class from androidx. These changes (only in XML ressources) allow us to share the selected text by using ShareUtils.shareText, which opens the Android system chooser instead of the Huawei system chooser on EMUI devices. --- .../schabi/newpipe/views/NewPipeEditText.java | 60 +++++++++++++++++++ app/src/main/res/layout/activity_error.xml | 2 +- app/src/main/res/layout/dialog_edit_text.xml | 2 +- .../res/layout/dialog_feed_group_create.xml | 2 +- app/src/main/res/layout/download_dialog.xml | 2 +- app/src/main/res/layout/fragment_import.xml | 2 +- .../main/res/layout/toolbar_search_layout.xml | 2 +- 7 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java diff --git a/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java b/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java new file mode 100644 index 000000000..15a70f360 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java @@ -0,0 +1,60 @@ +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; +import androidx.annotation.Nullable; +import androidx.appcompat.widget.AppCompatEditText; + +import org.schabi.newpipe.util.external_communication.ShareUtils; + +public class NewPipeEditText extends AppCompatEditText { + public NewPipeEditText(@NonNull final Context context) { + super(context); + } + + public NewPipeEditText(@NonNull final Context context, @Nullable final AttributeSet attrs) { + super(context, attrs); + } + + public NewPipeEditText(@NonNull final Context context, + @Nullable final AttributeSet attrs, + final int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + public boolean onTextContextMenuItem(final int id) { + final Spannable text = getText(); + if (id == android.R.id.shareText) { + if (text != null) { + final String selectedText = getSelectedText(text).toString(); + if (!selectedText.isEmpty()) { + ShareUtils.shareText(getContext(), "", selectedText); + } + Selection.setSelection(text, getSelectionEnd()); + } + return true; + } else { + return super.onTextContextMenuItem(id); + } + } + + @NonNull + private CharSequence getSelectedText(@NonNull final CharSequence charSequence) { + int min = 0; + int max = charSequence.length(); + + if (isFocused()) { + final int selStart = getSelectionStart(); + final int selEnd = getSelectionEnd(); + + min = Math.max(0, Math.min(selStart, selEnd)); + max = Math.max(0, Math.max(selStart, selEnd)); + } + return charSequence.subSequence(min, max); + } +} diff --git a/app/src/main/res/layout/activity_error.xml b/app/src/main/res/layout/activity_error.xml index a7b3c48a9..45101c1a1 100644 --- a/app/src/main/res/layout/activity_error.xml +++ b/app/src/main/res/layout/activity_error.xml @@ -109,7 +109,7 @@ android:text="@string/your_comment" android:textAppearance="?android:attr/textAppearanceMedium" /> - - - - - -