Skip to content

Commit 3ded6fe

Browse files
committed
Improve code of created views
Use the same logic as Android TextViews
1 parent c8802fe commit 3ded6fe

2 files changed

Lines changed: 27 additions & 35 deletions

File tree

app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,28 @@ public NewPipeEditText(@NonNull final Context context,
3737

3838
@Override
3939
public boolean onTextContextMenuItem(final int id) {
40-
final Spannable text = getText();
4140
if (id == android.R.id.shareText) {
42-
if (text != null) {
43-
final String selectedText = getSelectedText(text).toString();
44-
if (!selectedText.isEmpty()) {
45-
ShareUtils.shareText(getContext(), "", selectedText);
46-
}
47-
Selection.setSelection(text, getSelectionEnd());
41+
final Spannable text = getText();
42+
final CharSequence selectedText = getSelectedText(text);
43+
if (selectedText != null && selectedText.length() != 0) {
44+
ShareUtils.shareText(getContext(), "", selectedText.toString());
4845
}
46+
Selection.setSelection(text, getSelectionEnd());
4947
return true;
5048
} else {
5149
return super.onTextContextMenuItem(id);
5250
}
5351
}
5452

55-
@NonNull
56-
private CharSequence getSelectedText(@NonNull final CharSequence charSequence) {
57-
int min = 0;
58-
int max = charSequence.length();
59-
60-
if (isFocused()) {
61-
final int selStart = getSelectionStart();
62-
final int selEnd = getSelectionEnd();
63-
64-
min = Math.max(0, Math.min(selStart, selEnd));
65-
max = Math.max(0, Math.max(selStart, selEnd));
53+
@Nullable
54+
private CharSequence getSelectedText(@Nullable final CharSequence text) {
55+
if (!hasSelection() || text == null) {
56+
return null;
6657
}
67-
return charSequence.subSequence(min, max);
58+
59+
final int start = getSelectionStart();
60+
final int end = getSelectionEnd();
61+
return String.valueOf(start > end ? text.subSequence(end, start)
62+
: text.subSequence(start, end));
6863
}
6964
}

app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)