package com.sipai.jspatrol.utils.DoubleClickUtils;

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

public class DoubleManager {
    private DoubleRunnable mDoubleRunnable = null;//负责双击事件的线程执行体
    private Thread mThread = null;//负责双击事件的线程
    private long mClickTime = 0;//记录点击时间

    private static final int DOUBLE_SPAN = 300;//用于判断双击的间隔时间

    /**
     * 双击事件管理器的构造函数
     * @param doubleRunnable
     *          传入负责双击事件的线程执行体
     */
    public DoubleManager(DoubleRunnable doubleRunnable){
        if (doubleRunnable == null){
            return;
        }
        if (mDoubleRunnable == null){
            doubleRunnable.setDoubleSpan(DOUBLE_SPAN);
            this.mDoubleRunnable = doubleRunnable;
        }
    }

    /**
     * 开启双击事件的线程执行
     */
    public void start(){
        if (mDoubleRunnable == null){
            return;
        }
        boolean isDoubleClicked = false;
        if ((System.currentTimeMillis() - mClickTime) < DOUBLE_SPAN) {
            isDoubleClicked = true;
        }
        else {
            isDoubleClicked = false;
            mClickTime = System.currentTimeMillis();
        }
        mDoubleRunnable.setPreparedState(true, isDoubleClicked);
        if (mThread == null){
            mThread = new Thread(mDoubleRunnable);
            mThread.start();
        }
    }

}
