changed url longclick implementation to GestureListener
This commit is contained in:
@@ -12,12 +12,15 @@ import android.graphics.RectF;
|
|||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.text.Layout;
|
import android.text.Layout;
|
||||||
import android.text.Spanned;
|
import android.text.Spanned;
|
||||||
|
import android.view.GestureDetector;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
import android.view.SoundEffectConstants;
|
import android.view.SoundEffectConstants;
|
||||||
import android.view.ViewConfiguration;
|
import android.view.ViewConfiguration;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
import org.joinmastodon.android.R;
|
import org.joinmastodon.android.R;
|
||||||
|
|
||||||
import me.grishka.appkit.utils.V;
|
import me.grishka.appkit.utils.V;
|
||||||
@@ -29,20 +32,7 @@ public class ClickableLinksDelegate {
|
|||||||
private LinkSpan selectedSpan;
|
private LinkSpan selectedSpan;
|
||||||
private TextView view;
|
private TextView view;
|
||||||
|
|
||||||
private final Runnable copyTextToClipboard = () -> {
|
GestureDetector gestureDetector;
|
||||||
//if target is not a link, don't copy
|
|
||||||
if (selectedSpan.getType() != LinkSpan.Type.URL) return;
|
|
||||||
//copy link text to clipboard
|
|
||||||
ClipboardManager clipboard = (ClipboardManager) view.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
|
|
||||||
clipboard.setPrimaryClip(ClipData.newPlainText("", selectedSpan.getLink()));
|
|
||||||
//show toast, android from S_V2 on has built-in popup, as documented in
|
|
||||||
//https://developer.android.com/develop/ui/views/touch-and-input/copy-paste#duplicate-notifications
|
|
||||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) {
|
|
||||||
Toast.makeText(view.getContext(), R.string.text_copied, Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
//reset view
|
|
||||||
resetAndInvalidate();
|
|
||||||
};
|
|
||||||
|
|
||||||
public ClickableLinksDelegate(TextView view) {
|
public ClickableLinksDelegate(TextView view) {
|
||||||
this.view=view;
|
this.view=view;
|
||||||
@@ -50,10 +40,34 @@ public class ClickableLinksDelegate {
|
|||||||
hlPaint.setAntiAlias(true);
|
hlPaint.setAntiAlias(true);
|
||||||
hlPaint.setPathEffect(new CornerPathEffect(V.dp(3)));
|
hlPaint.setPathEffect(new CornerPathEffect(V.dp(3)));
|
||||||
// view.setHighlightColor(view.getResources().getColor(android.R.color.holo_blue_light));
|
// view.setHighlightColor(view.getResources().getColor(android.R.color.holo_blue_light));
|
||||||
|
gestureDetector = new GestureDetector(view.getContext(), new LinkGestureListener(), view.getHandler());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean onTouch(MotionEvent event) {
|
public boolean onTouch(MotionEvent event) {
|
||||||
if(event.getAction()==MotionEvent.ACTION_DOWN){
|
if(event.getAction()==MotionEvent.ACTION_CANCEL){
|
||||||
|
resetAndInvalidate();
|
||||||
|
}
|
||||||
|
return gestureDetector.onTouchEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void resetAndInvalidate() {
|
||||||
|
hlPath=null;
|
||||||
|
selectedSpan=null;
|
||||||
|
view.invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onDraw(Canvas canvas){
|
||||||
|
if(hlPath!=null){
|
||||||
|
canvas.save();
|
||||||
|
canvas.translate(0, view.getPaddingTop());
|
||||||
|
canvas.drawPath(hlPath, hlPaint);
|
||||||
|
canvas.restore();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class LinkGestureListener extends GestureDetector.SimpleOnGestureListener {
|
||||||
|
@Override
|
||||||
|
public boolean onDown(@NonNull MotionEvent event) {
|
||||||
int line=-1;
|
int line=-1;
|
||||||
Rect rect=new Rect();
|
Rect rect=new Rect();
|
||||||
Layout l=view.getLayout();
|
Layout l=view.getLayout();
|
||||||
@@ -112,34 +126,36 @@ public class ClickableLinksDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return super.onDown(event);
|
||||||
}
|
}
|
||||||
if(event.getAction()==MotionEvent.ACTION_UP && selectedSpan!=null){
|
|
||||||
view.removeCallbacks(copyTextToClipboard);
|
@Override
|
||||||
view.playSoundEffect(SoundEffectConstants.CLICK);
|
public boolean onSingleTapUp(@NonNull MotionEvent event) {
|
||||||
selectedSpan.onClick(view.getContext());
|
if(selectedSpan!=null){
|
||||||
resetAndInvalidate();
|
view.removeCallbacks(copyTextToClipboard);
|
||||||
|
view.playSoundEffect(SoundEffectConstants.CLICK);
|
||||||
|
selectedSpan.onClick(view.getContext());
|
||||||
|
resetAndInvalidate();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if(event.getAction()==MotionEvent.ACTION_CANCEL){
|
|
||||||
|
@Override
|
||||||
|
public void onLongPress(@NonNull MotionEvent event) {
|
||||||
|
//if target is not a link, don't copy
|
||||||
|
if (selectedSpan == null) return;
|
||||||
|
if (selectedSpan.getType() != LinkSpan.Type.URL) return;
|
||||||
|
//copy link text to clipboard
|
||||||
|
ClipboardManager clipboard = (ClipboardManager) view.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
|
clipboard.setPrimaryClip(ClipData.newPlainText("", selectedSpan.getLink()));
|
||||||
|
//show toast, android from S_V2 on has built-in popup, as documented in
|
||||||
|
//https://developer.android.com/develop/ui/views/touch-and-input/copy-paste#duplicate-notifications
|
||||||
|
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) {
|
||||||
|
Toast.makeText(view.getContext(), R.string.text_copied, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
//reset view
|
||||||
resetAndInvalidate();
|
resetAndInvalidate();
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void resetAndInvalidate() {
|
|
||||||
hlPath=null;
|
|
||||||
selectedSpan=null;
|
|
||||||
view.invalidate();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void onDraw(Canvas canvas){
|
|
||||||
if(hlPath!=null){
|
|
||||||
canvas.save();
|
|
||||||
canvas.translate(0, view.getPaddingTop());
|
|
||||||
canvas.drawPath(hlPath, hlPaint);
|
|
||||||
canvas.restore();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user