Skip to content
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

Use custom TextViews and EditTexts in all XML resources #7061

Merged
merged 8 commits into from
Oct 27, 2021
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 app/src/main/java/org/schabi/newpipe/views/NewPipeEditText.java
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 {
litetex marked this conversation as resolved.
Show resolved Hide resolved

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 app/src/main/java/org/schabi/newpipe/views/NewPipeTextView.java
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);
}
}
12 changes: 6 additions & 6 deletions app/src/main/res/layout-land/activity_player_queue_control.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
android:padding="8dp"
tools:ignore="RtlHardcoded,RtlSymmetry">

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/song_name"
style="@android:style/TextAppearance.StatusBar.EventContent.Title"
android:layout_width="match_parent"
Expand All @@ -71,7 +71,7 @@
android:textSize="14sp"
tools:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis nec aliquam augue, eget cursus est. Ut id tristique enim, ut scelerisque tellus. Sed ultricies ipsum non mauris ultricies, commodo malesuada velit porta." />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/artist_name"
style="@android:style/TextAppearance.StatusBar.EventContent"
android:layout_width="match_parent"
Expand All @@ -82,7 +82,7 @@
tools:text="Duis posuere arcu condimentum lobortis mattis." />
</LinearLayout>

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/seek_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -269,7 +269,7 @@
android:paddingLeft="16dp"
android:paddingRight="16dp">

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/current_time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
Expand All @@ -291,7 +291,7 @@
tools:progress="25"
tools:secondaryProgress="50" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/end_time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
Expand All @@ -301,7 +301,7 @@
tools:ignore="HardcodedText"
tools:text="1:23:49" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/live_sync"
android:layout_width="wrap_content"
android:layout_height="match_parent"
Expand Down
40 changes: 20 additions & 20 deletions app/src/main/res/layout-large-land/fragment_video_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
tools:ignore="ContentDescription"
tools:visibility="visible" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/touch_append_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -88,7 +88,7 @@
tools:ignore="RtlHardcoded"
tools:visibility="visible" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_duration_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -113,7 +113,7 @@
tools:text="12:38"
tools:visibility="visible" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_position_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -179,7 +179,7 @@
android:paddingStart="12dp"
tools:ignore="RtlSymmetry">

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_video_title_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down Expand Up @@ -291,7 +291,7 @@
android:gravity="center_vertical"
android:orientation="vertical">

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_sub_channel_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -307,7 +307,7 @@
tools:ignore="RtlHardcoded"
tools:text="Channel" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_uploader_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -348,7 +348,7 @@
android:paddingLeft="6dp"
android:paddingRight="6dp">

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_view_count_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -369,7 +369,7 @@
android:contentDescription="@string/detail_likes_img_view_description"
app:srcCompat="@drawable/ic_thumb_up" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_thumbs_up_count_view"
android:layout_width="wrap_content"
android:layout_height="@dimen/video_item_detail_like_image_height"
Expand All @@ -394,7 +394,7 @@
app:srcCompat="@drawable/ic_thumb_down"
tools:ignore="RtlHardcoded" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_thumbs_down_count_view"
android:layout_width="wrap_content"
android:layout_height="@dimen/video_item_detail_like_image_height"
Expand All @@ -408,7 +408,7 @@
tools:ignore="RtlHardcoded"
tools:text="10K" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_thumbs_disabled_view"
android:layout_width="wrap_content"
android:layout_height="@dimen/video_item_detail_like_image_height"
Expand Down Expand Up @@ -436,7 +436,7 @@
android:orientation="horizontal"
android:padding="@dimen/detail_control_padding">

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_controls_playlist_append"
android:layout_width="@dimen/detail_control_width"
android:layout_height="@dimen/detail_control_height"
Expand All @@ -452,7 +452,7 @@
android:textSize="@dimen/detail_control_text_size"
app:drawableTopCompat="@drawable/ic_playlist_add" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_controls_background"
android:layout_width="@dimen/detail_control_width"
android:layout_height="@dimen/detail_control_height"
Expand All @@ -468,7 +468,7 @@
android:textSize="@dimen/detail_control_text_size"
app:drawableTopCompat="@drawable/ic_headset" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_controls_popup"
android:layout_width="@dimen/detail_control_width"
android:layout_height="@dimen/detail_control_height"
Expand All @@ -484,7 +484,7 @@
android:textSize="@dimen/detail_control_text_size"
app:drawableTopCompat="@drawable/ic_picture_in_picture" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_controls_download"
android:layout_width="@dimen/detail_control_width"
android:layout_height="@dimen/detail_control_height"
Expand Down Expand Up @@ -515,7 +515,7 @@
android:visibility="gone"
tools:visibility="visible">

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_controls_share"
android:layout_width="@dimen/detail_control_width"
android:layout_height="@dimen/detail_control_height"
Expand All @@ -531,7 +531,7 @@
android:textSize="@dimen/detail_control_text_size"
app:drawableTopCompat="@drawable/ic_share" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_controls_open_in_browser"
android:layout_width="@dimen/detail_control_width"
android:layout_height="@dimen/detail_control_height"
Expand All @@ -547,7 +547,7 @@
android:textSize="@dimen/detail_control_text_size"
app:drawableTopCompat="@drawable/ic_language" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_controls_play_with_kodi"
android:layout_width="@dimen/detail_control_width"
android:layout_height="@dimen/detail_control_height"
Expand All @@ -573,7 +573,7 @@
android:layout_marginRight="8dp"
android:background="?attr/separator_color" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/detail_meta_info_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down Expand Up @@ -654,7 +654,7 @@
android:orientation="vertical"
tools:ignore="RtlHardcoded">

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/overlay_title_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -668,7 +668,7 @@
tools:ignore="RtlHardcoded"
tools:text="The Video Title LONG very LONVideo Title LONG very LONG" />

<TextView
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/overlay_channel_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
Loading