// Path: richtext/src/main/java/com/zzhoujay/richtext/spans/ClickableImageSpan.java
// public class ClickableImageSpan extends ImageSpan implements LongClickableSpan {
// 
//     private float x;
//     private final int position;
//     private final List<String> imageUrls;
//     private final WeakReference<OnImageLongClickListener> onImageLongClickListenerWeakReference;
//     private final WeakReference<OnImageClickListener> onImageClickListenerWeakReference;
// 
//     public ClickableImageSpan(Drawable drawable, ClickableImageSpan clickableImageSpan, OnImageClickListener onImageClickListener, OnImageLongClickListener onImageLongClickListener) {
//         super(drawable, clickableImageSpan.getSource());
//         this.imageUrls = clickableImageSpan.imageUrls;
//         this.position = clickableImageSpan.position;
//         this.onImageClickListenerWeakReference = new WeakReference<>(onImageClickListener);
//         this.onImageLongClickListenerWeakReference = new WeakReference<>(onImageLongClickListener);
//     }
// 
//     public ClickableImageSpan(Drawable drawable, List<String> imageUrls, int position, OnImageClickListener onImageClickListener, OnImageLongClickListener onImageLongClickListener) {
//         super(drawable, imageUrls.get(position));
//         this.imageUrls = imageUrls;
//         this.position = position;
//         this.onImageClickListenerWeakReference = new WeakReference<>(onImageClickListener);
//         this.onImageLongClickListenerWeakReference = new WeakReference<>(onImageLongClickListener);
//     }
// 
// 
//     @Override
//     public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
//         super.draw(canvas, text, start, end, x, top, y, bottom, paint);
//         this.x = x;
//     }
// 
//     public boolean clicked(int position) {
//         Drawable drawable = getDrawable();
//         if (drawable != null) {
//             Rect rect = drawable.getBounds();
//             if (position <= rect.right + x && position >= rect.left + x) {
//                 return true;
//             }
//         }
//         return false;
//     }
// 
// 
//     @Override
//     public void onClick(View widget) {
//         OnImageClickListener onImageClickListener = onImageClickListenerWeakReference.get();
//         if (onImageClickListener != null) {
//             boolean isDefaultDrawable = false;
//             
//             Drawable drawable;
//             if (getDrawable() instanceof DrawableWrapper) {
//                 drawable = ((DrawableWrapper) getDrawable()).getDrawable();
//                 isDefaultDrawable = drawable instanceof ColorDrawable;
//             }
//             onImageClickListener.imageClicked(imageUrls, position, isDefaultDrawable);
//         }
//     }
// 
//     @Override
//     public boolean onLongClick(View widget) {
//         OnImageLongClickListener onImageLongClickListener = onImageLongClickListenerWeakReference.get();
//         return onImageLongClickListener != null && onImageLongClickListener.imageLongClicked(imageUrls, position);
//     }
// }


import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.view.MotionEvent;
import android.widget.TextView;
import com.zzhoujay.richtext.spans.ClickableImageSpan;
import com.zzhoujay.richtext.spans.LongClickableSpan;
package com.zzhoujay.richtext.ext;



/**
 * Created by zhou on 16-8-4.
 * 支持长按的MovementMethod
 */
public class LongClickableLinkMovementMethod extends LinkMovementMethod {

    private static final int MIN_INTERVAL = 500;

    private long lastTime;

    @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);

            LongClickableSpan[] link = buffer.getSpans(off, off, LongClickableSpan.class);

            if (link.length != 0) {
                long currTime = System.currentTimeMillis();
                LongClickableSpan l = link[0];
                int ls = buffer.getSpanStart(l);
                int le = buffer.getSpanEnd(l);
                // 判断点击的点是否在Image范围内
// Path: richtext/src/main/java/com/zzhoujay/richtext/spans/ClickableImageSpan.java
// public class ClickableImageSpan extends ImageSpan implements LongClickableSpan {
// 
//     private float x;
//     private final int position;
//     private final List<String> imageUrls;
//     private final WeakReference<OnImageLongClickListener> onImageLongClickListenerWeakReference;
//     private final WeakReference<OnImageClickListener> onImageClickListenerWeakReference;
// 
//     public ClickableImageSpan(Drawable drawable, ClickableImageSpan clickableImageSpan, OnImageClickListener onImageClickListener, OnImageLongClickListener onImageLongClickListener) {
//         super(drawable, clickableImageSpan.getSource());
//         this.imageUrls = clickableImageSpan.imageUrls;
//         this.position = clickableImageSpan.position;
//         this.onImageClickListenerWeakReference = new WeakReference<>(onImageClickListener);
//         this.onImageLongClickListenerWeakReference = new WeakReference<>(onImageLongClickListener);
//     }
// 
//     public ClickableImageSpan(Drawable drawable, List<String> imageUrls, int position, OnImageClickListener onImageClickListener, OnImageLongClickListener onImageLongClickListener) {
//         super(drawable, imageUrls.get(position));
//         this.imageUrls = imageUrls;
//         this.position = position;
//         this.onImageClickListenerWeakReference = new WeakReference<>(onImageClickListener);
//         this.onImageLongClickListenerWeakReference = new WeakReference<>(onImageLongClickListener);
//     }
// 
// 
//     @Override
//     public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
//         super.draw(canvas, text, start, end, x, top, y, bottom, paint);
//         this.x = x;
//     }
// 
//     public boolean clicked(int position) {
//         Drawable drawable = getDrawable();
//         if (drawable != null) {
//             Rect rect = drawable.getBounds();
//             if (position <= rect.right + x && position >= rect.left + x) {
//                 return true;
//             }
//         }
//         return false;
//     }
// 
// 
//     @Override
//     public void onClick(View widget) {
//         OnImageClickListener onImageClickListener = onImageClickListenerWeakReference.get();
//         if (onImageClickListener != null) {
//             boolean isDefaultDrawable = false;
//             
//             Drawable drawable;
//             if (getDrawable() instanceof DrawableWrapper) {
//                 drawable = ((DrawableWrapper) getDrawable()).getDrawable();
//                 isDefaultDrawable = drawable instanceof ColorDrawable;
//             }
//             onImageClickListener.imageClicked(imageUrls, position, isDefaultDrawable);
//         }
//     }
// 
//     @Override
//     public boolean onLongClick(View widget) {
//         OnImageLongClickListener onImageLongClickListener = onImageLongClickListenerWeakReference.get();
//         return onImageLongClickListener != null && onImageLongClickListener.imageLongClicked(imageUrls, position);
//     }
// }


// Path: richtext/src/main/java/com/zzhoujay/richtext/ext/LongClickableLinkMovementMethod.java
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.method.LinkMovementMethod;
import android.view.MotionEvent;
import android.widget.TextView;
import com.zzhoujay.richtext.spans.ClickableImageSpan;
import com.zzhoujay.richtext.spans.LongClickableSpan;

package com.zzhoujay.richtext.ext;



/**
 * Created by zhou on 16-8-4.
 * 支持长按的MovementMethod
 */
public class LongClickableLinkMovementMethod extends LinkMovementMethod {

    private static final int MIN_INTERVAL = 500;

    private long lastTime;

    @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);

            LongClickableSpan[] link = buffer.getSpans(off, off, LongClickableSpan.class);

            if (link.length != 0) {
                long currTime = System.currentTimeMillis();
                LongClickableSpan l = link[0];
                int ls = buffer.getSpanStart(l);
                int le = buffer.getSpanEnd(l);
                // 判断点击的点是否在Image范围内
                ClickableImageSpan[] is = buffer.getSpans(ls, le, ClickableImageSpan.class);