@@ -37,11 +37,11 @@ public NewPipeTextView(@NonNull final Context context,
3737
3838 @ Override
3939 public boolean onTextContextMenuItem (final int id ) {
40- final CharSequence text = getText ();
4140 if (id == android .R .id .shareText ) {
42- final String selectedText = getSelectedText (text ).toString ();
43- if (!selectedText .isEmpty ()) {
44- ShareUtils .shareText (getContext (), "" , selectedText );
41+ final CharSequence text = getText ();
42+ final CharSequence selectedText = getSelectedText (text );
43+ if (selectedText != null && selectedText .length () != 0 ) {
44+ ShareUtils .shareText (getContext (), "" , selectedText .toString ());
4545 }
4646 final Spannable spannable = (text instanceof Spannable ) ? (Spannable ) text : null ;
4747 Selection .setSelection (spannable , getSelectionEnd ());
@@ -51,18 +51,15 @@ public boolean onTextContextMenuItem(final int id) {
5151 }
5252 }
5353
54- @ NonNull
55- private CharSequence getSelectedText (@ NonNull final CharSequence charSequence ) {
56- int min = 0 ;
57- int max = charSequence .length ();
58-
59- if (isFocused ()) {
60- final int selStart = getSelectionStart ();
61- final int selEnd = getSelectionEnd ();
62-
63- min = Math .max (0 , Math .min (selStart , selEnd ));
64- max = Math .max (0 , Math .max (selStart , selEnd ));
54+ @ Nullable
55+ private CharSequence getSelectedText (@ Nullable final CharSequence text ) {
56+ if (!hasSelection () || text == null ) {
57+ return null ;
6558 }
66- return charSequence .subSequence (min , max );
59+
60+ final int start = getSelectionStart ();
61+ final int end = getSelectionEnd ();
62+ return String .valueOf (start > end ? text .subSequence (end , start )
63+ : text .subSequence (start , end ));
6764 }
6865}
0 commit comments