From a55acd38dfad9027030f3668fbd895b947955448 Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Sat, 4 Sep 2021 19:32:38 +0200 Subject: [PATCH 1/8] Use a custom TextView everywhere to be able to share with ShareUtils the selected text This TextView class extends the AppCompatTextView 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/NewPipeTextView.java | 60 +++++++++ .../activity_player_queue_control.xml | 12 +- .../fragment_video_detail.xml | 40 +++--- app/src/main/res/layout-large-land/player.xml | 20 +-- app/src/main/res/layout/activity_error.xml | 20 +-- .../layout/activity_player_queue_control.xml | 122 +++++++++--------- app/src/main/res/layout/channel_header.xml | 6 +- .../res/layout/dialog_feed_group_create.xml | 8 +- .../res/layout/dialog_playback_parameter.xml | 35 ++--- app/src/main/res/layout/dialog_playlists.xml | 2 +- app/src/main/res/layout/dialog_title.xml | 4 +- app/src/main/res/layout/download_dialog.xml | 6 +- app/src/main/res/layout/drawer_header.xml | 4 +- app/src/main/res/layout/error_panel.xml | 6 +- .../res/layout/feed_group_add_new_item.xml | 2 +- .../main/res/layout/feed_group_card_item.xml | 2 +- .../res/layout/feed_group_reorder_item.xml | 2 +- .../res/layout/feed_import_export_group.xml | 6 +- app/src/main/res/layout/fragment_about.xml | 20 +-- app/src/main/res/layout/fragment_channel.xml | 6 +- .../main/res/layout/fragment_choose_tabs.xml | 2 +- app/src/main/res/layout/fragment_comments.xml | 4 +- .../main/res/layout/fragment_description.xml | 6 +- app/src/main/res/layout/fragment_feed.xml | 6 +- app/src/main/res/layout/fragment_import.xml | 2 +- .../res/layout/fragment_instance_list.xml | 2 +- app/src/main/res/layout/fragment_kiosk.xml | 4 +- app/src/main/res/layout/fragment_licenses.xml | 6 +- app/src/main/res/layout/fragment_playlist.xml | 4 +- .../res/layout/fragment_related_items.xml | 4 +- app/src/main/res/layout/fragment_search.xml | 8 +- .../main/res/layout/fragment_video_detail.xml | 40 +++--- .../main/res/layout/header_with_menu_item.xml | 2 +- .../main/res/layout/item_stream_segment.xml | 6 +- .../res/layout/list_choose_tabs_dialog.xml | 2 +- .../main/res/layout/list_comments_item.xml | 10 +- .../res/layout/list_comments_mini_item.xml | 8 +- app/src/main/res/layout/list_empty_view.xml | 4 +- .../res/layout/list_playlist_grid_item.xml | 6 +- .../main/res/layout/list_playlist_item.xml | 6 +- .../res/layout/list_playlist_mini_item.xml | 6 +- .../main/res/layout/list_stream_grid_item.xml | 8 +- app/src/main/res/layout/list_stream_item.xml | 8 +- .../main/res/layout/list_stream_mini_item.xml | 6 +- .../layout/list_stream_playlist_grid_item.xml | 6 +- .../res/layout/list_stream_playlist_item.xml | 6 +- .../main/res/layout/local_playlist_header.xml | 4 +- app/src/main/res/layout/main_bg.xml | 4 +- app/src/main/res/layout/mission_item.xml | 6 +- .../main/res/layout/mission_item_linear.xml | 6 +- app/src/main/res/layout/missions_header.xml | 2 +- .../res/layout/picker_subscription_item.xml | 2 +- app/src/main/res/layout/play_queue_item.xml | 6 +- app/src/main/res/layout/player.xml | 20 +-- app/src/main/res/layout/playlist_control.xml | 6 +- app/src/main/res/layout/playlist_header.xml | 6 +- .../main/res/layout/related_items_header.xml | 2 +- .../res/layout/select_channel_fragment.xml | 6 +- .../main/res/layout/select_channel_item.xml | 2 +- .../main/res/layout/select_kiosk_fragment.xml | 4 +- app/src/main/res/layout/select_kiosk_item.xml | 2 +- .../res/layout/select_playlist_fragment.xml | 4 +- .../settings_category_header_layout.xml | 2 +- .../layout/settings_category_header_title.xml | 2 +- .../main/res/layout/settings_notification.xml | 2 +- .../layout/settings_notification_action.xml | 4 +- .../res/layout/statistic_playlist_control.xml | 2 +- .../main/res/layout/stream_quality_item.xml | 6 +- .../subscription_import_export_item.xml | 2 +- 69 files changed, 362 insertions(+), 295 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java diff --git a/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java b/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java new file mode 100644 index 00000000000..f69dfb33bcf --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.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.AppCompatTextView; + +import org.schabi.newpipe.util.external_communication.ShareUtils; + +public class NewPipeTextView extends AppCompatTextView { + + public NewPipeTextView(@NonNull final Context context) { + super(context); + } + + public NewPipeTextView(@NonNull final Context context, @Nullable final AttributeSet attrs) { + super(context, attrs); + } + + public NewPipeTextView(@NonNull final Context context, + @Nullable final AttributeSet attrs, + final int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + public boolean onTextContextMenuItem(final int id) { + final CharSequence text = getText(); + if (id == android.R.id.shareText) { + final String selectedText = getSelectedText(text).toString(); + if (!selectedText.isEmpty()) { + ShareUtils.shareText(getContext(), "", selectedText); + } + final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null; + Selection.setSelection(spannable, 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-land/activity_player_queue_control.xml b/app/src/main/res/layout-land/activity_player_queue_control.xml index 4b79d92f60a..c2359552ecd 100644 --- a/app/src/main/res/layout-land/activity_player_queue_control.xml +++ b/app/src/main/res/layout-land/activity_player_queue_control.xml @@ -60,7 +60,7 @@ android:padding="8dp" tools:ignore="RtlHardcoded,RtlSymmetry"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -82,7 +82,7 @@ - - - - - - - + android:layout_above="@+id/playback_controls"> - + android:background="?attr/selectableItemBackground" + android:clickable="true" + android:focusable="true" + android:orientation="vertical" + android:padding="8dp" + tools:ignore="RtlHardcoded,RtlSymmetry"> - + + + + + - + android:layout_centerInParent="true" + android:background="#c0000000" + android:paddingLeft="30dp" + android:paddingTop="5dp" + android:paddingRight="30dp" + android:paddingBottom="5dp" + android:textColor="@android:color/white" + android:textSize="22sp" + android:textStyle="bold" + android:visibility="gone" + tools:ignore="RtlHardcoded" + tools:text="1:06:29" + tools:visibility="visible" /> + + android:paddingRight="12dp"> - - - + diff --git a/app/src/main/res/layout/channel_header.xml b/app/src/main/res/layout/channel_header.xml index aebb5d613c2..9366faf2c6e 100644 --- a/app/src/main/res/layout/channel_header.xml +++ b/app/src/main/res/layout/channel_header.xml @@ -49,7 +49,7 @@ tools:visibility="visible" /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -85,14 +85,14 @@ android:layout_gravity="end" android:text="@string/give_back" /> - - @@ -105,14 +105,14 @@ android:layout_gravity="end" android:text="@string/open_in_browser" /> - - diff --git a/app/src/main/res/layout/fragment_channel.xml b/app/src/main/res/layout/fragment_channel.xml index 873f3c88489..9e22575391d 100644 --- a/app/src/main/res/layout/fragment_channel.xml +++ b/app/src/main/res/layout/fragment_channel.xml @@ -30,7 +30,7 @@ android:visibility="gone" tools:visibility="visible"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --> - - - - --> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + tools:listitem="@layout/select_channel_item" /> - - - + tools:listitem="@layout/select_kiosk_item" /> diff --git a/app/src/main/res/layout/select_kiosk_item.xml b/app/src/main/res/layout/select_kiosk_item.xml index 6cd04ae345a..680767bba0c 100644 --- a/app/src/main/res/layout/select_kiosk_item.xml +++ b/app/src/main/res/layout/select_kiosk_item.xml @@ -22,7 +22,7 @@ app:tint="@color/contrastColor" tools:ignore="RtlHardcoded" /> - - - - diff --git a/app/src/main/res/layout/settings_category_header_title.xml b/app/src/main/res/layout/settings_category_header_title.xml index 679b9048c3e..c7d6920b049 100644 --- a/app/src/main/res/layout/settings_category_header_title.xml +++ b/app/src/main/res/layout/settings_category_header_title.xml @@ -1,5 +1,5 @@ - - - - - - - - - Date: Sat, 4 Sep 2021 19:40:24 +0200 Subject: [PATCH 2/8] 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 00000000000..15a70f36002 --- /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 a7b3c48a940..45101c1a149 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" /> - - - - - - Date: Tue, 14 Sep 2021 20:19:17 +0200 Subject: [PATCH 3/8] Add JavaDocs on created views --- .../java/org/schabi/newpipe/views/NewPipeEditText.java | 9 +++++++++ .../java/org/schabi/newpipe/views/NewPipeTextView.java | 8 ++++++++ 2 files changed, 17 insertions(+) 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 15a70f36002..c7a397fbed1 100644 --- a/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java +++ b/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java @@ -11,7 +11,16 @@ import org.schabi.newpipe.util.external_communication.ShareUtils; +/** + * 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. + *

+ * This allows NewPipe to show Android share sheet instead of EMUI share sheet when sharing text + * from {@link AppCompatEditText} on EMUI devices. + *

+ */ public class NewPipeEditText extends AppCompatEditText { + public NewPipeEditText(@NonNull final Context context) { super(context); } 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 f69dfb33bcf..321f1e34d2f 100644 --- a/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java +++ b/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java @@ -11,6 +11,14 @@ import org.schabi.newpipe.util.external_communication.ShareUtils; +/** + * 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. + *

+ * This allows NewPipe to show Android share sheet instead of EMUI share sheet when sharing text + * from {@link AppCompatTextView} on EMUI devices. + *

+ */ public class NewPipeTextView extends AppCompatTextView { public NewPipeTextView(@NonNull final Context context) { From 3ded6feddb3921eeecf2b5b3030573bd39017a3a Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Sat, 18 Sep 2021 14:14:36 +0200 Subject: [PATCH 4/8] Improve code of created views Use the same logic as Android TextViews --- .../schabi/newpipe/views/NewPipeEditText.java | 33 ++++++++----------- .../schabi/newpipe/views/NewPipeTextView.java | 29 ++++++++-------- 2 files changed, 27 insertions(+), 35 deletions(-) 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 c7a397fbed1..41d7640b630 100644 --- a/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java +++ b/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java @@ -37,33 +37,28 @@ public NewPipeEditText(@NonNull final Context context, @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()); + 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); } } - @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)); + @Nullable + private CharSequence getSelectedText(@Nullable final CharSequence text) { + if (!hasSelection() || text == null) { + return null; } - return charSequence.subSequence(min, max); + + final int start = getSelectionStart(); + final int end = getSelectionEnd(); + return String.valueOf(start > end ? text.subSequence(end, start) + : text.subSequence(start, end)); } } 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 321f1e34d2f..f333aae5dd1 100644 --- a/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java +++ b/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java @@ -37,11 +37,11 @@ public NewPipeTextView(@NonNull final Context context, @Override public boolean onTextContextMenuItem(final int id) { - final CharSequence text = getText(); if (id == android.R.id.shareText) { - final String selectedText = getSelectedText(text).toString(); - if (!selectedText.isEmpty()) { - ShareUtils.shareText(getContext(), "", selectedText); + 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()); @@ -51,18 +51,15 @@ public boolean onTextContextMenuItem(final int 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)); + @Nullable + private CharSequence getSelectedText(@Nullable final CharSequence text) { + if (!hasSelection() || text == null) { + return null; } - return charSequence.subSequence(min, max); + + final int start = getSelectionStart(); + final int end = getSelectionEnd(); + return String.valueOf(start > end ? text.subSequence(end, start) + : text.subSequence(start, end)); } } 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 5/8] 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 00000000000..31c87f7dfd8 --- /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 41d7640b630..cdb4f0041ec 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 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 NewPipeEditText(@NonNull final Context context, @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 f333aae5dd1..75fb8f161ef 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 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 NewPipeTextView(@NonNull final Context context, @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); } } From 50b85a7734f9d463007e6352c5230349df4744ac Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Fri, 1 Oct 2021 23:39:14 +0200 Subject: [PATCH 6/8] Simplify code --- .../newpipe/util/NewPipeTextViewHelper.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java b/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java index 31c87f7dfd8..5e3a790d091 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java @@ -34,30 +34,30 @@ private NewPipeTextViewHelper() { * 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); + if (!(textView instanceof NewPipeEditText)) { + final CharSequence textViewText; + if (textView instanceof NewPipeTextView) { + final NewPipeTextView newPipeTextView = (NewPipeTextView) textView; + textViewText = newPipeTextView.getText(); + } else { + textViewText = textView.getText(); + } - shareSelectedTextIfNotNullAndNotEmpty(newPipeTextView, selectedText); + final CharSequence selectedText = getSelectedText(textView, textViewText); + shareSelectedTextIfNotNullAndNotEmpty(textView, selectedText); - final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null; - Selection.setSelection(spannable, newPipeTextView.getSelectionEnd()); - } else if (textView instanceof NewPipeEditText) { + final Spannable spannable = (textViewText instanceof Spannable) + ? (Spannable) textViewText : null; + if (spannable != null) { + Selection.setSelection(spannable, textView.getSelectionEnd()); + } + } else { 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; @@ -65,7 +65,7 @@ public static boolean shareSelectedTextWithShareUtils(@NonNull final TextView te @Nullable private static CharSequence getSelectedText(@NonNull final TextView textView, - @Nullable final CharSequence text) { + @Nullable final CharSequence text) { if (!textView.hasSelection() || text == null) { return null; } From a744775fe771a9aad41668470ebe55193a83e5d7 Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Sat, 2 Oct 2021 19:53:17 +0200 Subject: [PATCH 7/8] Adress requested changes and remove an unused return value in NewPipeTextViewHelper --- .../org/schabi/newpipe/util/NewPipeTextViewHelper.java | 7 +------ .../java/org/schabi/newpipe/views/NewPipeEditText.java | 6 +++--- .../java/org/schabi/newpipe/views/NewPipeTextView.java | 6 +++--- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java b/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java index 5e3a790d091..6db0160904c 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java @@ -29,11 +29,8 @@ private NewPipeTextViewHelper() { * @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) { + public static void shareSelectedTextWithShareUtils(@NonNull final TextView textView) { if (!(textView instanceof NewPipeEditText)) { final CharSequence textViewText; if (textView instanceof NewPipeTextView) { @@ -59,8 +56,6 @@ public static boolean shareSelectedTextWithShareUtils(@NonNull final TextView te shareSelectedTextIfNotNullAndNotEmpty(textView, selectedText); Selection.setSelection(text, editText.getSelectionEnd()); } - - return true; } @Nullable 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 cdb4f0041ec..2adc28d0e5e 100644 --- a/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java +++ b/app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java @@ -7,10 +7,9 @@ import androidx.annotation.Nullable; import androidx.appcompat.widget.AppCompatEditText; +import org.schabi.newpipe.util.NewPipeTextViewHelper; 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,7 +37,8 @@ public NewPipeEditText(@NonNull final Context context, @Override public boolean onTextContextMenuItem(final int id) { if (id == android.R.id.shareText) { - return shareSelectedTextWithShareUtils(this); + NewPipeTextViewHelper.shareSelectedTextWithShareUtils(this); + return true; } 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 75fb8f161ef..8fdac32db7e 100644 --- a/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java +++ b/app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java @@ -7,10 +7,9 @@ import androidx.annotation.Nullable; import androidx.appcompat.widget.AppCompatTextView; +import org.schabi.newpipe.util.NewPipeTextViewHelper; 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,7 +37,8 @@ public NewPipeTextView(@NonNull final Context context, @Override public boolean onTextContextMenuItem(final int id) { if (id == android.R.id.shareText) { - return shareSelectedTextWithShareUtils(this); + NewPipeTextViewHelper.shareSelectedTextWithShareUtils(this); + return true; } return super.onTextContextMenuItem(id); } From ddaafb68c8561bb740c086d72c3b04ee69a9c22f Mon Sep 17 00:00:00 2001 From: TiA4f8R <74829229+TiA4f8R@users.noreply.github.com> Date: Sat, 16 Oct 2021 15:32:56 +0200 Subject: [PATCH 8/8] Adress new requested changes --- .../newpipe/util/NewPipeTextViewHelper.java | 28 +++---------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java b/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java index 6db0160904c..cf1a9a03ad9 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java @@ -31,30 +31,10 @@ private NewPipeTextViewHelper() { * {@link TextView standard TextViews} are supported). */ public static void shareSelectedTextWithShareUtils(@NonNull final TextView textView) { - if (!(textView instanceof NewPipeEditText)) { - final CharSequence textViewText; - if (textView instanceof NewPipeTextView) { - final NewPipeTextView newPipeTextView = (NewPipeTextView) textView; - textViewText = newPipeTextView.getText(); - } else { - textViewText = textView.getText(); - } - - final CharSequence selectedText = getSelectedText(textView, textViewText); - shareSelectedTextIfNotNullAndNotEmpty(textView, selectedText); - - final Spannable spannable = (textViewText instanceof Spannable) - ? (Spannable) textViewText : null; - if (spannable != null) { - Selection.setSelection(spannable, textView.getSelectionEnd()); - } - } else { - final NewPipeEditText editText = (NewPipeEditText) textView; - final Spannable text = editText.getText(); - - final CharSequence selectedText = getSelectedText(textView, text); - shareSelectedTextIfNotNullAndNotEmpty(textView, selectedText); - Selection.setSelection(text, editText.getSelectionEnd()); + final CharSequence textViewText = textView.getText(); + shareSelectedTextIfNotNullAndNotEmpty(textView, getSelectedText(textView, textViewText)); + if (textViewText instanceof Spannable) { + Selection.setSelection((Spannable) textViewText, textView.getSelectionEnd()); } }