From 4bb806585f6ccb02a1f25a69dafd57f17f1c7411 Mon Sep 17 00:00:00 2001 From: lookshe Date: Wed, 12 Aug 2015 21:43:04 +0200 Subject: merged latest version from https://github.com/firexel/emojicon/ but only seems to work with lollipop, so switch back to the old pictures --- .../emojicon/RepeatTouchListener.java | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/RepeatTouchListener.java (limited to 'libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/RepeatTouchListener.java') diff --git a/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/RepeatTouchListener.java b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/RepeatTouchListener.java new file mode 100644 index 00000000..e8f8e0fb --- /dev/null +++ b/libs/emojicon/src/main/java/github/ankushsachdeva/emojicon/RepeatTouchListener.java @@ -0,0 +1,74 @@ +package github.ankushsachdeva.emojicon; + +import android.os.Handler; +import android.os.SystemClock; +import android.view.MotionEvent; +import android.view.View; + +/** + * A class, that can be used as a TouchListener on any view (e.g. a Button). + * It cyclically runs a clickListener, emulating keyboard-like behaviour. First + * click is fired immediately, next before initialInterval, and subsequent before + * normalInterval. + *

+ *

Interval is scheduled before the onClick completes, so it has to run fast. + * If it runs slow, it does not generate skipped onClicks. + */ +class RepeatTouchListener implements View.OnTouchListener { + + private Handler handler = new Handler(); + + private int initialInterval; + private final int normalInterval; + private final View.OnClickListener clickListener; + + private Runnable handlerRunnable = new Runnable() { + @Override + public void run() { + if (downView == null) { + return; + } + handler.removeCallbacksAndMessages(downView); + handler.postAtTime(this, downView, SystemClock.uptimeMillis() + normalInterval); + clickListener.onClick(downView); + } + }; + + private View downView; + + /** + * @param initialInterval The interval before first click event + * @param normalInterval The interval before second and subsequent click + * events + * @param clickListener The OnClickListener, that will be called + * periodically + */ + public RepeatTouchListener(int initialInterval, int normalInterval, View.OnClickListener clickListener) { + if (clickListener == null) + throw new IllegalArgumentException("null runnable"); + if (initialInterval < 0 || normalInterval < 0) + throw new IllegalArgumentException("negative interval"); + + this.initialInterval = initialInterval; + this.normalInterval = normalInterval; + this.clickListener = clickListener; + } + + public boolean onTouch(View view, MotionEvent motionEvent) { + switch (motionEvent.getAction()) { + case MotionEvent.ACTION_DOWN: + downView = view; + handler.removeCallbacks(handlerRunnable); + handler.postAtTime(handlerRunnable, downView, SystemClock.uptimeMillis() + initialInterval); + clickListener.onClick(view); + return true; + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + case MotionEvent.ACTION_OUTSIDE: + handler.removeCallbacksAndMessages(downView); + downView = null; + return true; + } + return false; + } +} -- cgit v1.2.3