Improve code of created views
Use the same logic as Android TextViews
This commit is contained in:
parent
c8802fe5d0
commit
3ded6feddb
|
@ -37,33 +37,28 @@ public class NewPipeEditText extends AppCompatEditText {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTextContextMenuItem(final int id) {
|
public boolean onTextContextMenuItem(final int id) {
|
||||||
final Spannable text = getText();
|
|
||||||
if (id == android.R.id.shareText) {
|
if (id == android.R.id.shareText) {
|
||||||
if (text != null) {
|
final Spannable text = getText();
|
||||||
final String selectedText = getSelectedText(text).toString();
|
final CharSequence selectedText = getSelectedText(text);
|
||||||
if (!selectedText.isEmpty()) {
|
if (selectedText != null && selectedText.length() != 0) {
|
||||||
ShareUtils.shareText(getContext(), "", selectedText);
|
ShareUtils.shareText(getContext(), "", selectedText.toString());
|
||||||
}
|
}
|
||||||
Selection.setSelection(text, getSelectionEnd());
|
Selection.setSelection(text, getSelectionEnd());
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return super.onTextContextMenuItem(id);
|
return super.onTextContextMenuItem(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@Nullable
|
||||||
private CharSequence getSelectedText(@NonNull final CharSequence charSequence) {
|
private CharSequence getSelectedText(@Nullable final CharSequence text) {
|
||||||
int min = 0;
|
if (!hasSelection() || text == null) {
|
||||||
int max = charSequence.length();
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (isFocused()) {
|
final int start = getSelectionStart();
|
||||||
final int selStart = getSelectionStart();
|
final int end = getSelectionEnd();
|
||||||
final int selEnd = getSelectionEnd();
|
return String.valueOf(start > end ? text.subSequence(end, start)
|
||||||
|
: text.subSequence(start, end));
|
||||||
min = Math.max(0, Math.min(selStart, selEnd));
|
|
||||||
max = Math.max(0, Math.max(selStart, selEnd));
|
|
||||||
}
|
|
||||||
return charSequence.subSequence(min, max);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,11 +37,11 @@ public class NewPipeTextView extends AppCompatTextView {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTextContextMenuItem(final int id) {
|
public boolean onTextContextMenuItem(final int id) {
|
||||||
final CharSequence text = getText();
|
|
||||||
if (id == android.R.id.shareText) {
|
if (id == android.R.id.shareText) {
|
||||||
final String selectedText = getSelectedText(text).toString();
|
final CharSequence text = getText();
|
||||||
if (!selectedText.isEmpty()) {
|
final CharSequence selectedText = getSelectedText(text);
|
||||||
ShareUtils.shareText(getContext(), "", selectedText);
|
if (selectedText != null && selectedText.length() != 0) {
|
||||||
|
ShareUtils.shareText(getContext(), "", selectedText.toString());
|
||||||
}
|
}
|
||||||
final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null;
|
final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null;
|
||||||
Selection.setSelection(spannable, getSelectionEnd());
|
Selection.setSelection(spannable, getSelectionEnd());
|
||||||
|
@ -51,18 +51,15 @@ public class NewPipeTextView extends AppCompatTextView {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@Nullable
|
||||||
private CharSequence getSelectedText(@NonNull final CharSequence charSequence) {
|
private CharSequence getSelectedText(@Nullable final CharSequence text) {
|
||||||
int min = 0;
|
if (!hasSelection() || text == null) {
|
||||||
int max = charSequence.length();
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (isFocused()) {
|
final int start = getSelectionStart();
|
||||||
final int selStart = getSelectionStart();
|
final int end = getSelectionEnd();
|
||||||
final int selEnd = getSelectionEnd();
|
return String.valueOf(start > end ? text.subSequence(end, start)
|
||||||
|
: text.subSequence(start, end));
|
||||||
min = Math.max(0, Math.min(selStart, selEnd));
|
|
||||||
max = Math.max(0, Math.max(selStart, selEnd));
|
|
||||||
}
|
|
||||||
return charSequence.subSequence(min, max);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue