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.
This commit is contained in:
parent
7edef8d5a2
commit
a55acd38df
69 changed files with 362 additions and 295 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue