-
-
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.
Merge the Share process of the two classes into one
A new class has been added in the util package: NewPipeTextViewHelper. It shares the selected text of a TextView with ShareUtils#shareText (with the created shareSelectedTextWithShareUtils static method). Only this static method can be used by other classes, other methods are private.
- Loading branch information
Showing
3 changed files
with
92 additions
and
43 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
app/src/main/java/org/schabi/newpipe/util/NewPipeTextViewHelper.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,86 @@ | ||
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). | ||
* | ||
* @return true if no exceptions occurred when getting the selected text, sharing it and | ||
* deselecting it, otherwise an exception | ||
*/ | ||
public static boolean shareSelectedTextWithShareUtils(@NonNull final TextView textView) { | ||
if (textView instanceof NewPipeTextView) { | ||
final NewPipeTextView newPipeTextView = (NewPipeTextView) textView; | ||
final CharSequence text = newPipeTextView.getText(); | ||
final CharSequence selectedText = getSelectedText(newPipeTextView, text); | ||
|
||
shareSelectedTextIfNotNullAndNotEmpty(newPipeTextView, selectedText); | ||
|
||
final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null; | ||
Selection.setSelection(spannable, newPipeTextView.getSelectionEnd()); | ||
} else if (textView instanceof NewPipeEditText) { | ||
final NewPipeEditText editText = (NewPipeEditText) textView; | ||
final Spannable text = editText.getText(); | ||
|
||
final CharSequence selectedText = getSelectedText(textView, text); | ||
shareSelectedTextIfNotNullAndNotEmpty(textView, selectedText); | ||
Selection.setSelection(text, editText.getSelectionEnd()); | ||
} else { | ||
final CharSequence text = textView.getText(); | ||
final CharSequence selectedText = getSelectedText(textView, text); | ||
|
||
shareSelectedTextIfNotNullAndNotEmpty(textView, selectedText); | ||
|
||
final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null; | ||
Selection.setSelection(spannable, textView.getSelectionEnd()); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
@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()); | ||
} | ||
} | ||
} |
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