parent
f07ce7b1f1
commit
c3164a8e84
@ -0,0 +1,73 @@
|
|||||||
|
package org.thoughtcrime.securesms.util;
|
||||||
|
|
||||||
|
import android.annotation.TargetApi;
|
||||||
|
import android.content.ClipData;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.annotation.ColorInt;
|
||||||
|
import android.text.TextPaint;
|
||||||
|
import android.text.style.URLSpan;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import org.thoughtcrime.securesms.R;
|
||||||
|
|
||||||
|
public class LongClickCopySpan extends URLSpan {
|
||||||
|
private static final String PREFIX_MAILTO = "mailto:";
|
||||||
|
private static final String PREFIX_TEL = "tel:";
|
||||||
|
|
||||||
|
private boolean isHighlighted;
|
||||||
|
@ColorInt
|
||||||
|
private int highlightColor;
|
||||||
|
|
||||||
|
public LongClickCopySpan(String url) {
|
||||||
|
super(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
void onLongClick(View widget) {
|
||||||
|
Context context = widget.getContext();
|
||||||
|
String preparedUrl = prepareUrl(getURL());
|
||||||
|
copyUrl(context, preparedUrl);
|
||||||
|
Toast.makeText(context,
|
||||||
|
context.getString(R.string.ConversationItem_copied_text, preparedUrl), Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateDrawState(TextPaint ds) {
|
||||||
|
super.updateDrawState(ds);
|
||||||
|
ds.bgColor = highlightColor;
|
||||||
|
ds.setUnderlineText(!isHighlighted);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setHighlighted(boolean highlighted, @ColorInt int highlightColor) {
|
||||||
|
this.isHighlighted = highlighted;
|
||||||
|
this.highlightColor = highlightColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void copyUrl(Context context, String url) {
|
||||||
|
int sdk = android.os.Build.VERSION.SDK_INT;
|
||||||
|
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
|
||||||
|
@SuppressWarnings("deprecation") android.text.ClipboardManager clipboard =
|
||||||
|
(android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
|
clipboard.setText(url);
|
||||||
|
} else {
|
||||||
|
copyUriSdk11(context, url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@TargetApi(android.os.Build.VERSION_CODES.HONEYCOMB)
|
||||||
|
private void copyUriSdk11(Context context, String url) {
|
||||||
|
android.content.ClipboardManager clipboard =
|
||||||
|
(android.content.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||||
|
ClipData clip = ClipData.newPlainText(context.getString(R.string.app_name), url);
|
||||||
|
clipboard.setPrimaryClip(clip);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String prepareUrl(String url) {
|
||||||
|
if (url.startsWith(PREFIX_MAILTO)) {
|
||||||
|
return url.substring(PREFIX_MAILTO.length());
|
||||||
|
} else if (url.startsWith(PREFIX_TEL)) {
|
||||||
|
return url.substring(PREFIX_TEL.length());
|
||||||
|
}
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,103 @@
|
|||||||
|
package org.thoughtcrime.securesms.util;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
|
import android.text.Layout;
|
||||||
|
import android.text.Selection;
|
||||||
|
import android.text.Spannable;
|
||||||
|
import android.text.method.LinkMovementMethod;
|
||||||
|
import android.view.GestureDetector;
|
||||||
|
import android.view.MotionEvent;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import org.thoughtcrime.securesms.R;
|
||||||
|
|
||||||
|
public class LongClickMovementMethod extends LinkMovementMethod {
|
||||||
|
@SuppressLint("StaticFieldLeak")
|
||||||
|
private static LongClickMovementMethod sInstance;
|
||||||
|
|
||||||
|
private final GestureDetector gestureDetector;
|
||||||
|
private View widget;
|
||||||
|
private LongClickCopySpan currentSpan;
|
||||||
|
|
||||||
|
private LongClickMovementMethod(final Context context) {
|
||||||
|
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
|
||||||
|
@Override
|
||||||
|
public void onLongPress(MotionEvent e) {
|
||||||
|
if (currentSpan != null && widget != null) {
|
||||||
|
currentSpan.onLongClick(widget);
|
||||||
|
widget = null;
|
||||||
|
currentSpan = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onSingleTapUp(MotionEvent e) {
|
||||||
|
if (currentSpan != null && widget != null) {
|
||||||
|
currentSpan.onClick(widget);
|
||||||
|
widget = null;
|
||||||
|
currentSpan = null;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
|
||||||
|
int action = event.getAction();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
LongClickCopySpan longClickCopySpan[] = buffer.getSpans(off, off, LongClickCopySpan.class);
|
||||||
|
if (longClickCopySpan.length != 0) {
|
||||||
|
LongClickCopySpan aSingleSpan = longClickCopySpan[0];
|
||||||
|
if (action == MotionEvent.ACTION_DOWN) {
|
||||||
|
Selection.setSelection(buffer, buffer.getSpanStart(aSingleSpan),
|
||||||
|
buffer.getSpanEnd(aSingleSpan));
|
||||||
|
aSingleSpan.setHighlighted(true,
|
||||||
|
ContextCompat.getColor(widget.getContext(), R.color.touch_highlight));
|
||||||
|
} else {
|
||||||
|
Selection.removeSelection(buffer);
|
||||||
|
aSingleSpan.setHighlighted(false, Color.TRANSPARENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.currentSpan = aSingleSpan;
|
||||||
|
this.widget = widget;
|
||||||
|
return gestureDetector.onTouchEvent(event);
|
||||||
|
}
|
||||||
|
} else if (action == MotionEvent.ACTION_CANCEL) {
|
||||||
|
// Remove Selections.
|
||||||
|
LongClickCopySpan[] spans = buffer.getSpans(Selection.getSelectionStart(buffer),
|
||||||
|
Selection.getSelectionEnd(buffer), LongClickCopySpan.class);
|
||||||
|
for (LongClickCopySpan aSpan : spans) {
|
||||||
|
aSpan.setHighlighted(false, Color.TRANSPARENT);
|
||||||
|
}
|
||||||
|
Selection.removeSelection(buffer);
|
||||||
|
}
|
||||||
|
return super.onTouchEvent(widget, buffer, event);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static LongClickMovementMethod getInstance(Context context) {
|
||||||
|
if (sInstance == null) {
|
||||||
|
sInstance = new LongClickMovementMethod(context.getApplicationContext());
|
||||||
|
}
|
||||||
|
return sInstance;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue