diff options
author | Scott Jackson <daneren2005@gmail.com> | 2015-04-25 17:03:02 -0700 |
---|---|---|
committer | Scott Jackson <daneren2005@gmail.com> | 2015-04-25 17:03:05 -0700 |
commit | cfd014d38cba03ba05f571597b361ab253bff578 (patch) | |
tree | 4256723561dec7ef3ed3507382eb7020724ec570 /app/src/main/java/github/daneren2005/dsub/view/AutoRepeatButton.java | |
parent | 8a332a20ec272d59fe74520825b18017a8f0cac3 (diff) | |
download | dsub-cfd014d38cba03ba05f571597b361ab253bff578.tar.gz dsub-cfd014d38cba03ba05f571597b361ab253bff578.tar.bz2 dsub-cfd014d38cba03ba05f571597b361ab253bff578.zip |
Update to gradle
Diffstat (limited to 'app/src/main/java/github/daneren2005/dsub/view/AutoRepeatButton.java')
-rw-r--r-- | app/src/main/java/github/daneren2005/dsub/view/AutoRepeatButton.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/app/src/main/java/github/daneren2005/dsub/view/AutoRepeatButton.java b/app/src/main/java/github/daneren2005/dsub/view/AutoRepeatButton.java new file mode 100644 index 00000000..3c59dd37 --- /dev/null +++ b/app/src/main/java/github/daneren2005/dsub/view/AutoRepeatButton.java @@ -0,0 +1,86 @@ +package github.daneren2005.dsub.view; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; +import android.widget.ImageButton; + +public class AutoRepeatButton extends ImageButton { + + private static final long initialRepeatDelay = 1000; + private static final long repeatIntervalInMilliseconds = 300; + private boolean doClick = true; + private Runnable repeatEvent = null; + + private Runnable repeatClickWhileButtonHeldRunnable = new Runnable() { + @Override + public void run() { + doClick = false; + //Perform the present repetition of the click action provided by the user + // in setOnClickListener(). + if(repeatEvent != null) + repeatEvent.run(); + + //Schedule the next repetitions of the click action, using a faster repeat + // interval than the initial repeat delay interval. + postDelayed(repeatClickWhileButtonHeldRunnable, repeatIntervalInMilliseconds); + } + }; + + private void commonConstructorCode() { + this.setOnTouchListener(new OnTouchListener() { + @Override + public boolean onTouch(View v, MotionEvent event) { + int action = event.getAction(); + if(action == MotionEvent.ACTION_DOWN) + { + doClick = true; + //Just to be sure that we removed all callbacks, + // which should have occurred in the ACTION_UP + removeCallbacks(repeatClickWhileButtonHeldRunnable); + + //Schedule the start of repetitions after a one half second delay. + postDelayed(repeatClickWhileButtonHeldRunnable, initialRepeatDelay); + + setPressed(true); + } + else if(action == MotionEvent.ACTION_UP) { + //Cancel any repetition in progress. + removeCallbacks(repeatClickWhileButtonHeldRunnable); + + if(doClick || repeatEvent == null) { + performClick(); + } + + setPressed(false); + } + + //Returning true here prevents performClick() from getting called + // in the usual manner, which would be redundant, given that we are + // already calling it above. + return true; + } + }); + } + + public void setOnRepeatListener(Runnable runnable) { + repeatEvent = runnable; + } + + public AutoRepeatButton(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + commonConstructorCode(); + } + + + public AutoRepeatButton(Context context, AttributeSet attrs) { + super(context, attrs); + commonConstructorCode(); + } + + public AutoRepeatButton(Context context) { + super(context); + commonConstructorCode(); + } +} |