Android Open Source - BluetoothSppPro Repeating Button






From Project

Back to project page BluetoothSppPro.

License

The source code is released under:

Apache License

If you think the Android project BluetoothSppPro listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package mobi.dzs.android.control.button;
//from  w  w w  .  j a  v a2s  .c  om
import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.Button;

/**
 * ???????
 *   ???????????????
 *   @author jerryli lijian@dzs.mobi
 *   @see ??????????????bindListener()?????????????????????
 *        ??????????
 * */
public class RepeatingButton extends Button{
  /**???????? */
    private long mStartTime;
    /**?????????????????*/
    private int mRepeatCount = 0;
    /**?????????????????*/
    private ButtonPassListener mListener = null;
    /**Timer?????????????????500ms????*/
    private long mInterval = 500;

  public RepeatingButton(Context context)  {
    this(context, null);
  }

  public RepeatingButton(Context context, AttributeSet attrs)  {
      this(context, attrs, android.R.attr.buttonStyle);
  }

  public RepeatingButton(Context context, AttributeSet attrs, int defStyle){
      super(context, attrs, defStyle);
      setFocusable(true); //???????
      setLongClickable(true); //????????
  }
  /**
   * ?????????????
   * @param long interval ??????ms
   * @param ButtonPassListener l ?????????????
   * @return void
   * */
    public void bindListener(ButtonPassListener l, long lHold_feq){
        this.mListener = l;
        this.mInterval = lHold_feq;
    }
    
    /**???????????*/
    public void setRepeatFreq(long interval){
      this.mInterval = interval;
    }

    /**
     * ??????????
     * @return boolean
     * */
    @Override
    public boolean performLongClick(){
      this.mStartTime = SystemClock.elapsedRealtime(); //???????????
      this.mRepeatCount = 0;
      post(this.mRepeater); //??????????????
        return true;
    }

    /**
     * ?????????????
     * @param event MotionEvent  ???????
     * @return boolean
     * */
    @Override
    public boolean onTouchEvent(MotionEvent event){
        if (event.getAction() == MotionEvent.ACTION_UP){
          removeCallbacks(this.mRepeater); //??????????
          this.doUp(); //??????????
        }else if (event.getAction() == MotionEvent.ACTION_DOWN ){ //???????????
          this.doDown();
        }
        return super.onTouchEvent(event);
    }

    /**?????????????*/
    private Runnable mRepeater = new Runnable(){  //??????????
      /** ??????????????????????????????????? */
      private long lFirstRunTime = mStartTime + mInterval;
      /** ????? */
        public void run(){
          if (SystemClock.elapsedRealtime() > lFirstRunTime)//?????????????????????
            doRepeat(false); //??????
            if (isPressed()) //?????????????????????????????????
                postDelayed(this, mInterval); //??????????????
        }
    };

    /**
     * ???????????????
     * @param last boolean ????????????????
     * */
    public void doRepeat(boolean last){
        if (this.mListener != null)
          this.mListener.onRepeat(
          this,
          SystemClock.elapsedRealtime() - this.mStartTime,
          last ? -1 : this.mRepeatCount++
          );
    }

    /**
     * ??????????????
     * */
    public void doUp(){
        if (this.mListener != null)
          this.mListener.onUp(this);
    }

    /**
     * ??????????????
     * */
    public void doDown(){
        if (this.mListener != null)
          this.mListener.onDown(this);
    }
}




Java Source Code List

mobi.dzs.android.BLE_SPP_PRO.BaseActivity.java
mobi.dzs.android.BLE_SPP_PRO.BaseCommActivity.java
mobi.dzs.android.BLE_SPP_PRO.actAbout.java
mobi.dzs.android.BLE_SPP_PRO.actByteStream.java
mobi.dzs.android.BLE_SPP_PRO.actCmdLine.java
mobi.dzs.android.BLE_SPP_PRO.actDiscovery.java
mobi.dzs.android.BLE_SPP_PRO.actKeyBoard.java
mobi.dzs.android.BLE_SPP_PRO.actMain.java
mobi.dzs.android.BLE_SPP_PRO.globalPool.java
mobi.dzs.android.bluetooth.BTSerialComm.java
mobi.dzs.android.bluetooth.BluetoothCtrl.java
mobi.dzs.android.bluetooth.BluetoothSppClient.java
mobi.dzs.android.bluetooth.CResourcePV.java
mobi.dzs.android.control.button.ButtonPassListener.java
mobi.dzs.android.control.button.RepeatingButton.java
mobi.dzs.android.storage.CJsonStorage.java
mobi.dzs.android.storage.CKVStorage.java
mobi.dzs.android.storage.CSharedPreferences.java
mobi.dzs.android.util.CHexConver.java
mobi.dzs.android.util.LocalIOTools.java