mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-06-27 07:33:20 +00:00
cdd5e89b86
This commit adds the ability to copy to clipboard hashtags, URLs and timestamps when long-pressing them. Some changes in our TextView class related to text setting have been required and metadata items are now using a NewPipeTextView instead of a standard TextView. Six new classes have been added: - a custom LinkMovementMethod class; - a custom ClickableSpan class, LongPressClickableSpan, in order to set a long press event; - a class to avoid code duplication in CommentTextOnTouchListener, TouchUtils; - three implementations of LongPressClickableSpan used when linkifying text: - HashtagLongPressClickableSpan for hashtags; - TimestampLongPressClickableSpan for timestamps; - UrlLongPressClickableSpan for URLs.
59 lines
2.1 KiB
Java
59 lines
2.1 KiB
Java
package org.schabi.newpipe.views;
|
|
|
|
import android.content.Context;
|
|
import android.text.method.MovementMethod;
|
|
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 class allows NewPipe to show Android share sheet instead of EMUI share sheet when sharing
|
|
* text from {@link AppCompatTextView} on EMUI devices and also to keep movement method set when a
|
|
* text change occurs, if the text cannot be selected and text links are clickable.
|
|
* </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 void setText(final CharSequence text, final BufferType type) {
|
|
// We need to set again the movement method after a text change because Android resets the
|
|
// movement method to the default one in the case where the text cannot be selected and
|
|
// text links are clickable (which is the default case in NewPipe).
|
|
final MovementMethod movementMethod = this.getMovementMethod();
|
|
super.setText(text, type);
|
|
setMovementMethod(movementMethod);
|
|
}
|
|
|
|
@Override
|
|
public boolean onTextContextMenuItem(final int id) {
|
|
if (id == android.R.id.shareText) {
|
|
NewPipeTextViewHelper.shareSelectedTextWithShareUtils(this);
|
|
return true;
|
|
}
|
|
return super.onTextContextMenuItem(id);
|
|
}
|
|
}
|