Add ability to copy hashtags, URLs and timestamps in descriptions on long-press
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.
This commit is contained in:
parent
b5faaa7f75
commit
6de5a8cf04
16 changed files with 524 additions and 226 deletions
|
|
@ -0,0 +1,12 @@
|
|||
package org.schabi.newpipe.views;
|
||||
|
||||
import android.text.style.ClickableSpan;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
public abstract class LongPressClickableSpan extends ClickableSpan {
|
||||
|
||||
public abstract void onLongClick(@NonNull View view);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
package org.schabi.newpipe.views;
|
||||
|
||||
import static org.schabi.newpipe.util.TouchUtils.getOffsetForHorizontalLine;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.text.Selection;
|
||||
import android.text.Spannable;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.text.method.MovementMethod;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.ViewConfiguration;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
// Class adapted from https://stackoverflow.com/a/31786969
|
||||
|
||||
public class LongPressLinkMovementMethod extends LinkMovementMethod {
|
||||
|
||||
private static final int LONG_PRESS_TIME = ViewConfiguration.getLongPressTimeout();
|
||||
|
||||
private static LongPressLinkMovementMethod instance;
|
||||
|
||||
private Handler longClickHandler;
|
||||
private boolean isLongPressed = false;
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(@NonNull final TextView widget,
|
||||
@NonNull final Spannable buffer,
|
||||
@NonNull final MotionEvent event) {
|
||||
final int action = event.getAction();
|
||||
|
||||
if (action == MotionEvent.ACTION_CANCEL && longClickHandler != null) {
|
||||
longClickHandler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
|
||||
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
|
||||
final int offset = getOffsetForHorizontalLine(widget, event);
|
||||
final LongPressClickableSpan[] link = buffer.getSpans(offset, offset,
|
||||
LongPressClickableSpan.class);
|
||||
|
||||
if (link.length != 0) {
|
||||
if (action == MotionEvent.ACTION_UP) {
|
||||
if (longClickHandler != null) {
|
||||
longClickHandler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
if (!isLongPressed) {
|
||||
link[0].onClick(widget);
|
||||
}
|
||||
isLongPressed = false;
|
||||
} else {
|
||||
Selection.setSelection(buffer, buffer.getSpanStart(link[0]),
|
||||
buffer.getSpanEnd(link[0]));
|
||||
if (longClickHandler != null) {
|
||||
longClickHandler.postDelayed(() -> {
|
||||
link[0].onLongClick(widget);
|
||||
isLongPressed = true;
|
||||
}, LONG_PRESS_TIME);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return super.onTouchEvent(widget, buffer, event);
|
||||
}
|
||||
|
||||
public static MovementMethod getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new LongPressLinkMovementMethod();
|
||||
instance.longClickHandler = new Handler(Looper.myLooper());
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
|
@ -13,9 +13,10 @@ 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.
|
||||
* This class 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 {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package org.schabi.newpipe.views;
|
||||
|
||||
import android.content.Context;
|
||||
import android.text.method.MovementMethod;
|
||||
import android.util.AttributeSet;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
|
@ -13,9 +14,11 @@ 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.
|
||||
* 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 {
|
||||
|
|
@ -34,6 +37,16 @@ public class NewPipeTextView extends AppCompatTextView {
|
|||
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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue