-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
7 changed files
with
66 additions
and
6 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.AppCompatEditText; | ||
|
||
import org.schabi.newpipe.util.external_communication.ShareUtils; | ||
|
||
public class NewPipeEditText extends AppCompatEditText { | ||
public NewPipeEditText(@NonNull final Context context) { | ||
super(context); | ||
} | ||
|
||
public NewPipeEditText(@NonNull final Context context, @Nullable final AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
public NewPipeEditText(@NonNull final Context context, | ||
@Nullable final AttributeSet attrs, | ||
final int defStyleAttr) { | ||
super(context, attrs, defStyleAttr); | ||
} | ||
|
||
@Override | ||
public boolean onTextContextMenuItem(final int id) { | ||
final Spannable text = getText(); | ||
if (id == android.R.id.shareText) { | ||
if (text != null) { | ||
final String selectedText = getSelectedText(text).toString(); | ||
if (!selectedText.isEmpty()) { | ||
ShareUtils.shareText(getContext(), "", selectedText); | ||
} | ||
Selection.setSelection(text, 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters