-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Use custom TextViews and EditTexts in all XML resources #7061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a55acd3
Use a custom TextView everywhere to be able to share with ShareUtils …
AudricV 411b312
Use a custom EditText everywhere to be able to share with ShareUtils …
AudricV c8802fe
Add JavaDocs on created views
AudricV 3ded6fe
Improve code of created views
AudricV aab09c0
Merge the Share process of the two classes into one
AudricV 50b85a7
Simplify code
AudricV a744775
Adress requested changes and remove an unused return value in NewPipe…
AudricV ddaafb6
Adress new requested changes
AudricV File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.java
This file contains hidden or 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,61 @@ | ||
| package org.schabi.newpipe.util; | ||
|
|
||
| import android.content.Context; | ||
| import android.text.Selection; | ||
| import android.text.Spannable; | ||
| import android.widget.TextView; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
|
|
||
| import org.schabi.newpipe.util.external_communication.ShareUtils; | ||
| import org.schabi.newpipe.views.NewPipeEditText; | ||
| import org.schabi.newpipe.views.NewPipeTextView; | ||
|
|
||
| public final class NewPipeTextViewHelper { | ||
| private NewPipeTextViewHelper() { | ||
| } | ||
|
|
||
| /** | ||
| * Share the selected text of {@link NewPipeTextView NewPipeTextViews} and | ||
| * {@link NewPipeEditText NewPipeEditTexts} with | ||
| * {@link ShareUtils#shareText(Context, String, String)}. | ||
| * | ||
| * <p> | ||
| * This allows EMUI users to get the Android share sheet instead of the EMUI share sheet when | ||
| * using the {@code Share} command of the popup menu which appears when selecting text. | ||
| * </p> | ||
| * | ||
| * @param textView the {@link TextView} on which sharing the selected text. It should be a | ||
| * {@link NewPipeTextView} or a {@link NewPipeEditText} (even if | ||
| * {@link TextView standard TextViews} are supported). | ||
| */ | ||
| public static void shareSelectedTextWithShareUtils(@NonNull final TextView textView) { | ||
| final CharSequence textViewText = textView.getText(); | ||
| shareSelectedTextIfNotNullAndNotEmpty(textView, getSelectedText(textView, textViewText)); | ||
| if (textViewText instanceof Spannable) { | ||
| Selection.setSelection((Spannable) textViewText, textView.getSelectionEnd()); | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private static CharSequence getSelectedText(@NonNull final TextView textView, | ||
| @Nullable final CharSequence text) { | ||
| if (!textView.hasSelection() || text == null) { | ||
| return null; | ||
| } | ||
|
|
||
| final int start = textView.getSelectionStart(); | ||
| final int end = textView.getSelectionEnd(); | ||
| return String.valueOf(start > end ? text.subSequence(end, start) | ||
| : text.subSequence(start, end)); | ||
| } | ||
|
|
||
| private static void shareSelectedTextIfNotNullAndNotEmpty( | ||
| @NonNull final TextView textView, | ||
| @Nullable final CharSequence selectedText) { | ||
| if (selectedText != null && selectedText.length() != 0) { | ||
| ShareUtils.shareText(textView.getContext(), "", selectedText.toString()); | ||
| } | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java
This file contains hidden or 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,45 @@ | ||
| package org.schabi.newpipe.views; | ||
|
|
||
| import android.content.Context; | ||
| import android.util.AttributeSet; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
| import androidx.appcompat.widget.AppCompatEditText; | ||
|
|
||
| import org.schabi.newpipe.util.NewPipeTextViewHelper; | ||
| import org.schabi.newpipe.util.external_communication.ShareUtils; | ||
|
|
||
| /** | ||
| * An {@link AppCompatEditText} which uses {@link ShareUtils#shareText(Context, String, String)} | ||
| * when sharing selected text by using the {@code Share} command of the floating actions. | ||
| * <p> | ||
| * This allows NewPipe to show Android share sheet instead of EMUI share sheet when sharing text | ||
| * from {@link AppCompatEditText} on EMUI devices. | ||
| * </p> | ||
| */ | ||
| 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) { | ||
| if (id == android.R.id.shareText) { | ||
| NewPipeTextViewHelper.shareSelectedTextWithShareUtils(this); | ||
| return true; | ||
| } | ||
| return super.onTextContextMenuItem(id); | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java
This file contains hidden or 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,45 @@ | ||
| package org.schabi.newpipe.views; | ||
|
|
||
| import android.content.Context; | ||
| import android.util.AttributeSet; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
| import androidx.appcompat.widget.AppCompatTextView; | ||
|
|
||
| import org.schabi.newpipe.util.NewPipeTextViewHelper; | ||
| import org.schabi.newpipe.util.external_communication.ShareUtils; | ||
|
|
||
| /** | ||
| * An {@link AppCompatTextView} which uses {@link ShareUtils#shareText(Context, String, String)} | ||
| * when sharing selected text by using the {@code Share} command of the floating actions. | ||
| * <p> | ||
| * This allows NewPipe to show Android share sheet instead of EMUI share sheet when sharing text | ||
| * from {@link AppCompatTextView} on EMUI devices. | ||
| * </p> | ||
| */ | ||
| 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) { | ||
| if (id == android.R.id.shareText) { | ||
| NewPipeTextViewHelper.shareSelectedTextWithShareUtils(this); | ||
| return true; | ||
| } | ||
| return super.onTextContextMenuItem(id); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.