mirror of https://codeberg.org/tom79/Fedilab
parent
a8afc99401
commit
27ef0003d7
@ -0,0 +1,82 @@
|
||||
package app.fedilab.android.helper;
|
||||
|
||||
import android.os.Handler;
|
||||
import android.text.Layout;
|
||||
import android.text.Selection;
|
||||
import android.text.Spannable;
|
||||
import android.text.method.LinkMovementMethod;
|
||||
import android.text.method.MovementMethod;
|
||||
import android.view.MotionEvent;
|
||||
import android.widget.TextView;
|
||||
|
||||
|
||||
//https://stackoverflow.com/a/20435892
|
||||
public class LongClickLinkMovementMethod extends LinkMovementMethod {
|
||||
|
||||
private static LongClickLinkMovementMethod sInstance;
|
||||
private Handler mLongClickHandler;
|
||||
private boolean mIsLongPressed = false;
|
||||
|
||||
public static MovementMethod getInstance() {
|
||||
if (sInstance == null) {
|
||||
sInstance = new LongClickLinkMovementMethod();
|
||||
sInstance.mLongClickHandler = new Handler();
|
||||
}
|
||||
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onTouchEvent(final TextView widget, Spannable buffer,
|
||||
MotionEvent event) {
|
||||
int action = event.getAction();
|
||||
if (action == MotionEvent.ACTION_CANCEL) {
|
||||
if (mLongClickHandler != null) {
|
||||
mLongClickHandler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
}
|
||||
|
||||
if (action == MotionEvent.ACTION_UP ||
|
||||
action == MotionEvent.ACTION_DOWN) {
|
||||
int x = (int) event.getX();
|
||||
int y = (int) event.getY();
|
||||
|
||||
x -= widget.getTotalPaddingLeft();
|
||||
y -= widget.getTotalPaddingTop();
|
||||
|
||||
x += widget.getScrollX();
|
||||
y += widget.getScrollY();
|
||||
|
||||
Layout layout = widget.getLayout();
|
||||
int line = layout.getLineForVertical(y);
|
||||
int off = layout.getOffsetForHorizontal(line, x);
|
||||
|
||||
final LongClickableSpan[] link = buffer.getSpans(off, off, LongClickableSpan.class);
|
||||
|
||||
if (link.length != 0) {
|
||||
if (action == MotionEvent.ACTION_UP) {
|
||||
if (mLongClickHandler != null) {
|
||||
mLongClickHandler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
if (!mIsLongPressed) {
|
||||
link[0].onClick(widget);
|
||||
}
|
||||
mIsLongPressed = false;
|
||||
} else {
|
||||
Selection.setSelection(buffer,
|
||||
buffer.getSpanStart(link[0]),
|
||||
buffer.getSpanEnd(link[0]));
|
||||
int LONG_CLICK_TIME = 1000;
|
||||
mLongClickHandler.postDelayed(() -> {
|
||||
link[0].onLongClick(widget);
|
||||
mIsLongPressed = true;
|
||||
widget.invalidate();
|
||||
}, LONG_CLICK_TIME);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return super.onTouchEvent(widget, buffer, event);
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package app.fedilab.android.helper;
|
||||
|
||||
import android.text.style.ClickableSpan;
|
||||
import android.view.View;
|
||||
|
||||
public abstract class LongClickableSpan extends ClickableSpan {
|
||||
|
||||
abstract public void onLongClick(View view);
|
||||
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="@dimen/fab_margin"
|
||||
android:orientation="vertical"
|
||||
android:padding="@dimen/fab_margin">
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/display_full_link"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:drawableEnd="@drawable/ic_baseline_navigate_next_24"
|
||||
android:text="@string/display_full_link"
|
||||
android:textColor="@color/cyanea_accent_reference"
|
||||
android:textSize="16sp"
|
||||
app:drawableTint="@color/cyanea_accent_reference" />
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/share_link"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:drawableEnd="@drawable/ic_baseline_navigate_next_24"
|
||||
android:text="@string/share_link"
|
||||
android:textColor="@color/cyanea_accent_reference"
|
||||
android:textSize="16sp"
|
||||
app:drawableTint="@color/cyanea_accent_reference" />
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/open_other_app"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:drawableEnd="@drawable/ic_baseline_navigate_next_24"
|
||||
android:text="@string/open_other_app"
|
||||
android:textColor="@color/cyanea_accent_reference"
|
||||
android:textSize="16sp"
|
||||
app:drawableTint="@color/cyanea_accent_reference" />
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/copy_link"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:drawableEnd="@drawable/ic_baseline_navigate_next_24"
|
||||
android:text="@string/copy_link"
|
||||
android:textColor="@color/cyanea_accent_reference"
|
||||
android:textSize="16sp"
|
||||
app:drawableTint="@color/cyanea_accent_reference" />
|
||||
|
||||
<androidx.appcompat.widget.AppCompatTextView
|
||||
android:id="@+id/check_redirect"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="10dp"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:drawableEnd="@drawable/ic_baseline_navigate_next_24"
|
||||
android:text="@string/check_redirect"
|
||||
android:textColor="@color/cyanea_accent_reference"
|
||||
android:textSize="16sp"
|
||||
app:drawableTint="@color/cyanea_accent_reference" />
|
||||
</LinearLayout>
|
Loading…
Reference in new issue