package com.sipai.jspatrol.utils.DoubleClickUtils;

/**
 * Created by lvliming on 2018/6/12.
 */

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

/**
 * 连续点击事件监听器 可以用作双击事件
 *
 */
public class DoubleHandler extends Handler {
    private OnDoubleClickListener mListener;//对是否双击结果的回调监听器
    private DoubleRunnable mDoubleRunnable;//负责双击事件的线程执行体

    /**
     * 双击事件处理器的构造函数
     * @param listener
     *          对是否双击结果的回调监听器
     */
    public DoubleHandler(OnDoubleClickListener listener){
        super(Looper.getMainLooper());
        this.mListener = listener;
    }

    @Override
    public void handleMessage(Message msg) {
        if ((boolean)msg.obj){
            mListener.onDoublePress();
        }
        else {
            mListener.onSinglePress();
        }
        //通知双击事件线程执行体，事件已处理完
        mDoubleRunnable.setIsStarted(false);
    }

    /**
     * 设置handler处理所在的线程执行体
     * @param doubleRunnable
     *          传入的当前线程执行体
     */
    public void setDoubleRunnable(DoubleRunnable doubleRunnable){
        this.mDoubleRunnable = doubleRunnable;
    }
}
