Skip to content

Commit 411b312

Browse files
committed
Use a custom EditText everywhere to be able to share with ShareUtils the selected text
This EditText class extends the AppCompatEditText 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.
1 parent a55acd3 commit 411b312

7 files changed

Lines changed: 66 additions & 6 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.schabi.newpipe.views;
2+
3+
import android.content.Context;
4+
import android.text.Selection;
5+
import android.text.Spannable;
6+
import android.util.AttributeSet;
7+
8+
import androidx.annotation.NonNull;
9+
import androidx.annotation.Nullable;
10+
import androidx.appcompat.widget.AppCompatEditText;
11+
12+
import org.schabi.newpipe.util.external_communication.ShareUtils;
13+
14+
public class NewPipeEditText extends AppCompatEditText {
15+
public NewPipeEditText(@NonNull final Context context) {
16+
super(context);
17+
}
18+
19+
public NewPipeEditText(@NonNull final Context context, @Nullable final AttributeSet attrs) {
20+
super(context, attrs);
21+
}
22+
23+
public NewPipeEditText(@NonNull final Context context,
24+
@Nullable final AttributeSet attrs,
25+
final int defStyleAttr) {
26+
super(context, attrs, defStyleAttr);
27+
}
28+
29+
@Override
30+
public boolean onTextContextMenuItem(final int id) {
31+
final Spannable text = getText();
32+
if (id == android.R.id.shareText) {
33+
if (text != null) {
34+
final String selectedText = getSelectedText(text).toString();
35+
if (!selectedText.isEmpty()) {
36+
ShareUtils.shareText(getContext(), "", selectedText);
37+
}
38+
Selection.setSelection(text, getSelectionEnd());
39+
}
40+
return true;
41+
} else {
42+
return super.onTextContextMenuItem(id);
43+
}
44+
}
45+
46+
@NonNull
47+
private CharSequence getSelectedText(@NonNull final CharSequence charSequence) {
48+
int min = 0;
49+
int max = charSequence.length();
50+
51+
if (isFocused()) {
52+
final int selStart = getSelectionStart();
53+
final int selEnd = getSelectionEnd();
54+
55+
min = Math.max(0, Math.min(selStart, selEnd));
56+
max = Math.max(0, Math.max(selStart, selEnd));
57+
}
58+
return charSequence.subSequence(min, max);
59+
}
60+
}

app/src/main/res/layout/activity_error.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
android:text="@string/your_comment"
110110
android:textAppearance="?android:attr/textAppearanceMedium" />
111111

112-
<EditText
112+
<org.schabi.newpipe.views.NewPipeEditText
113113
android:id="@+id/errorCommentBox"
114114
android:layout_width="match_parent"
115115
android:layout_height="wrap_content"

app/src/main/res/layout/dialog_edit_text.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
android:paddingTop="@dimen/video_item_search_padding"
88
android:paddingRight="@dimen/video_item_search_padding">
99

10-
<EditText
10+
<org.schabi.newpipe.views.NewPipeEditText
1111
android:id="@+id/dialogEditText"
1212
android:layout_width="match_parent"
1313
android:layout_height="wrap_content"

app/src/main/res/layout/dialog_feed_group_create.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
app:layout_constraintStart_toEndOf="@+id/icon_preview"
4646
app:layout_constraintTop_toTopOf="parent">
4747

48-
<EditText
48+
<org.schabi.newpipe.views.NewPipeEditText
4949
android:id="@+id/group_name_input"
5050
android:layout_width="match_parent"
5151
android:layout_height="match_parent"

app/src/main/res/layout/download_dialog.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
android:layout_marginBottom="6dp"
1919
android:text="@string/msg_name" />
2020

21-
<EditText
21+
<org.schabi.newpipe.views.NewPipeEditText
2222
android:id="@+id/file_name"
2323
android:layout_width="match_parent"
2424
android:layout_height="wrap_content"

app/src/main/res/layout/fragment_import.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
android:orientation="vertical"
2727
android:padding="16dp">
2828

29-
<EditText
29+
<org.schabi.newpipe.views.NewPipeEditText
3030
android:id="@+id/input_text"
3131
android:layout_width="match_parent"
3232
android:layout_height="wrap_content"

app/src/main/res/layout/toolbar_search_layout.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
android:layout_height="?attr/actionBarSize"
77
tools:background="?attr/colorPrimary">
88

9-
<EditText
9+
<org.schabi.newpipe.views.NewPipeEditText
1010
android:id="@+id/toolbar_search_edit_text"
1111
android:layout_width="match_parent"
1212
android:layout_height="match_parent"

0 commit comments

Comments
 (0)