Skip to content

Commit aab09c0

Browse files
committed
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.
1 parent 3ded6fe commit aab09c0

3 files changed

Lines changed: 94 additions & 47 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.schabi.newpipe.util;
2+
3+
import android.content.Context;
4+
import android.text.Selection;
5+
import android.text.Spannable;
6+
import android.widget.TextView;
7+
8+
import androidx.annotation.NonNull;
9+
import androidx.annotation.Nullable;
10+
11+
import org.schabi.newpipe.util.external_communication.ShareUtils;
12+
import org.schabi.newpipe.views.NewPipeEditText;
13+
import org.schabi.newpipe.views.NewPipeTextView;
14+
15+
public final class NewPipeTextViewHelper {
16+
private NewPipeTextViewHelper() {
17+
}
18+
19+
/**
20+
* Share the selected text of {@link NewPipeTextView NewPipeTextViews} and
21+
* {@link NewPipeEditText NewPipeEditTexts} with
22+
* {@link ShareUtils#shareText(Context, String, String)}.
23+
*
24+
* <p>
25+
* This allows EMUI users to get the Android share sheet instead of the EMUI share sheet when
26+
* using the {@code Share} command of the popup menu which appears when selecting text.
27+
* </p>
28+
*
29+
* @param textView the {@link TextView} on which sharing the selected text. It should be a
30+
* {@link NewPipeTextView} or a {@link NewPipeEditText} (even if
31+
* {@link TextView standard TextViews} are supported).
32+
*
33+
* @return true if no exceptions occurred when getting the selected text, sharing it and
34+
* deselecting it, otherwise an exception
35+
*/
36+
public static boolean shareSelectedTextWithShareUtils(@NonNull final TextView textView) {
37+
if (textView instanceof NewPipeTextView) {
38+
final NewPipeTextView newPipeTextView = (NewPipeTextView) textView;
39+
final CharSequence text = newPipeTextView.getText();
40+
final CharSequence selectedText = getSelectedText(newPipeTextView, text);
41+
42+
shareSelectedTextIfNotNullAndNotEmpty(newPipeTextView, selectedText);
43+
44+
final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null;
45+
Selection.setSelection(spannable, newPipeTextView.getSelectionEnd());
46+
} else if (textView instanceof NewPipeEditText) {
47+
final NewPipeEditText editText = (NewPipeEditText) textView;
48+
final Spannable text = editText.getText();
49+
50+
final CharSequence selectedText = getSelectedText(textView, text);
51+
shareSelectedTextIfNotNullAndNotEmpty(textView, selectedText);
52+
Selection.setSelection(text, editText.getSelectionEnd());
53+
} else {
54+
final CharSequence text = textView.getText();
55+
final CharSequence selectedText = getSelectedText(textView, text);
56+
57+
shareSelectedTextIfNotNullAndNotEmpty(textView, selectedText);
58+
59+
final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null;
60+
Selection.setSelection(spannable, textView.getSelectionEnd());
61+
}
62+
63+
return true;
64+
}
65+
66+
@Nullable
67+
private static CharSequence getSelectedText(@NonNull final TextView textView,
68+
@Nullable final CharSequence text) {
69+
if (!textView.hasSelection() || text == null) {
70+
return null;
71+
}
72+
73+
final int start = textView.getSelectionStart();
74+
final int end = textView.getSelectionEnd();
75+
return String.valueOf(start > end ? text.subSequence(end, start)
76+
: text.subSequence(start, end));
77+
}
78+
79+
private static void shareSelectedTextIfNotNullAndNotEmpty(
80+
@NonNull final TextView textView,
81+
@Nullable final CharSequence selectedText) {
82+
if (selectedText != null && selectedText.length() != 0) {
83+
ShareUtils.shareText(textView.getContext(), "", selectedText.toString());
84+
}
85+
}
86+
}
Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.schabi.newpipe.views;
22

33
import android.content.Context;
4-
import android.text.Selection;
5-
import android.text.Spannable;
64
import android.util.AttributeSet;
75

86
import androidx.annotation.NonNull;
@@ -11,6 +9,8 @@
119

1210
import org.schabi.newpipe.util.external_communication.ShareUtils;
1311

12+
import static org.schabi.newpipe.util.NewPipeTextViewHelper.shareSelectedTextWithShareUtils;
13+
1414
/**
1515
* An {@link AppCompatEditText} which uses {@link ShareUtils#shareText(Context, String, String)}
1616
* when sharing selected text by using the {@code Share} command of the floating actions.
@@ -38,27 +38,8 @@ public NewPipeEditText(@NonNull final Context context,
3838
@Override
3939
public boolean onTextContextMenuItem(final int id) {
4040
if (id == android.R.id.shareText) {
41-
final Spannable text = getText();
42-
final CharSequence selectedText = getSelectedText(text);
43-
if (selectedText != null && selectedText.length() != 0) {
44-
ShareUtils.shareText(getContext(), "", selectedText.toString());
45-
}
46-
Selection.setSelection(text, getSelectionEnd());
47-
return true;
48-
} else {
49-
return super.onTextContextMenuItem(id);
41+
return shareSelectedTextWithShareUtils(this);
5042
}
51-
}
52-
53-
@Nullable
54-
private CharSequence getSelectedText(@Nullable final CharSequence text) {
55-
if (!hasSelection() || text == null) {
56-
return null;
57-
}
58-
59-
final int start = getSelectionStart();
60-
final int end = getSelectionEnd();
61-
return String.valueOf(start > end ? text.subSequence(end, start)
62-
: text.subSequence(start, end));
43+
return super.onTextContextMenuItem(id);
6344
}
6445
}
Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package org.schabi.newpipe.views;
22

33
import android.content.Context;
4-
import android.text.Selection;
5-
import android.text.Spannable;
64
import android.util.AttributeSet;
75

86
import androidx.annotation.NonNull;
@@ -11,6 +9,8 @@
119

1210
import org.schabi.newpipe.util.external_communication.ShareUtils;
1311

12+
import static org.schabi.newpipe.util.NewPipeTextViewHelper.shareSelectedTextWithShareUtils;
13+
1414
/**
1515
* An {@link AppCompatTextView} which uses {@link ShareUtils#shareText(Context, String, String)}
1616
* when sharing selected text by using the {@code Share} command of the floating actions.
@@ -38,28 +38,8 @@ public NewPipeTextView(@NonNull final Context context,
3838
@Override
3939
public boolean onTextContextMenuItem(final int id) {
4040
if (id == android.R.id.shareText) {
41-
final CharSequence text = getText();
42-
final CharSequence selectedText = getSelectedText(text);
43-
if (selectedText != null && selectedText.length() != 0) {
44-
ShareUtils.shareText(getContext(), "", selectedText.toString());
45-
}
46-
final Spannable spannable = (text instanceof Spannable) ? (Spannable) text : null;
47-
Selection.setSelection(spannable, getSelectionEnd());
48-
return true;
49-
} else {
50-
return super.onTextContextMenuItem(id);
41+
return shareSelectedTextWithShareUtils(this);
5142
}
52-
}
53-
54-
@Nullable
55-
private CharSequence getSelectedText(@Nullable final CharSequence text) {
56-
if (!hasSelection() || text == null) {
57-
return null;
58-
}
59-
60-
final int start = getSelectionStart();
61-
final int end = getSelectionEnd();
62-
return String.valueOf(start > end ? text.subSequence(end, start)
63-
: text.subSequence(start, end));
43+
return super.onTextContextMenuItem(id);
6444
}
6545
}

0 commit comments

Comments
 (0)