-
-
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.
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.
- Loading branch information
Showing
16 changed files
with
507 additions
and
209 deletions.
There are no files selected for viewing
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
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,38 @@ | ||
package org.schabi.newpipe.util; | ||
|
||
import android.text.Layout; | ||
import android.view.MotionEvent; | ||
import android.widget.TextView; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
public final class TouchUtils { | ||
|
||
private TouchUtils() { | ||
} | ||
|
||
/** | ||
* Get the character offset on the closest line to the position pressed by the user of a | ||
* {@link TextView} from a {@link MotionEvent} which was fired on this {@link TextView}. | ||
* | ||
* @param textView the {@link TextView} on which the {@link MotionEvent} was fired | ||
* @param event the {@link MotionEvent} which was fired | ||
* @return the character offset on the closest line to the position pressed by the user | ||
*/ | ||
public static int getOffsetForHorizontalLine(@NonNull final TextView textView, | ||
@NonNull final MotionEvent event) { | ||
|
||
int x = (int) event.getX(); | ||
int y = (int) event.getY(); | ||
|
||
x -= textView.getTotalPaddingLeft(); | ||
y -= textView.getTotalPaddingTop(); | ||
|
||
x += textView.getScrollX(); | ||
y += textView.getScrollY(); | ||
|
||
final Layout layout = textView.getLayout(); | ||
final int line = layout.getLineForVertical(y); | ||
return layout.getOffsetForHorizontal(line, x); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...in/java/org/schabi/newpipe/util/external_communication/HashtagLongPressClickableSpan.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,38 @@ | ||
package org.schabi.newpipe.util.external_communication; | ||
|
||
import android.content.Context; | ||
import android.view.View; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
import org.schabi.newpipe.extractor.Info; | ||
import org.schabi.newpipe.util.NavigationHelper; | ||
import org.schabi.newpipe.views.LongPressClickableSpan; | ||
|
||
final class HashtagLongPressClickableSpan extends LongPressClickableSpan { | ||
|
||
@NonNull | ||
private final Context context; | ||
@NonNull | ||
private final String parsedHashtag; | ||
@NonNull | ||
private final Info relatedInfo; | ||
|
||
HashtagLongPressClickableSpan(@NonNull final Context context, | ||
@NonNull final String parsedHashtag, | ||
@NonNull final Info relatedInfo) { | ||
this.context = context; | ||
this.parsedHashtag = parsedHashtag; | ||
this.relatedInfo = relatedInfo; | ||
} | ||
|
||
@Override | ||
public void onClick(@NonNull final View view) { | ||
NavigationHelper.openSearch(context, relatedInfo.getServiceId(), parsedHashtag); | ||
} | ||
|
||
@Override | ||
public void onLongClick(@NonNull final View view) { | ||
ShareUtils.copyToClipboard(context, parsedHashtag); | ||
} | ||
} |
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
Oops, something went wrong.