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] 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 c7a397fbe..41d7640b6 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 class NewPipeEditText extends AppCompatEditText { @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 321f1e34d..f333aae5d 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 class NewPipeTextView extends AppCompatTextView { @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 class NewPipeTextView extends AppCompatTextView { } } - @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)); } }