am 08d72c52: Merge "Introduce a delay for key-press feedback on the Emoji palette"

* commit '08d72c52f9f543968224e23758b8ac81bfba4115':
  Introduce a delay for key-press feedback on the Emoji palette
main
Yohei Yukawa 2014-02-02 18:16:59 -08:00 committed by Android Git Automerger
commit ffdb8be7ac
1 changed files with 42 additions and 10 deletions

View File

@ -17,6 +17,7 @@
package com.android.inputmethod.keyboard.internal; package com.android.inputmethod.keyboard.internal;
import android.content.Context; import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.MotionEvent; import android.view.MotionEvent;
@ -35,6 +36,9 @@ import com.android.inputmethod.latin.R;
// TODO: Implement key popup preview. // TODO: Implement key popup preview.
public final class EmojiPageKeyboardView extends KeyboardView implements public final class EmojiPageKeyboardView extends KeyboardView implements
GestureDetector.OnGestureListener { GestureDetector.OnGestureListener {
private static final long KEY_PRESS_DELAY_TIME = 250; // msec
private static final long KEY_RELEASE_DELAY_TIME = 30; // msec
public interface OnKeyEventListener { public interface OnKeyEventListener {
public void onPressKey(Key key); public void onPressKey(Key key);
public void onReleaseKey(Key key); public void onReleaseKey(Key key);
@ -60,6 +64,7 @@ public final class EmojiPageKeyboardView extends KeyboardView implements
super(context, attrs, defStyle); super(context, attrs, defStyle);
mGestureDetector = new GestureDetector(context, this); mGestureDetector = new GestureDetector(context, this);
mGestureDetector.setIsLongpressEnabled(false /* isLongpressEnabled */); mGestureDetector.setIsLongpressEnabled(false /* isLongpressEnabled */);
mHandler = new Handler();
} }
public void setOnKeyEventListener(final OnKeyEventListener listener) { public void setOnKeyEventListener(final OnKeyEventListener listener) {
@ -92,6 +97,8 @@ public final class EmojiPageKeyboardView extends KeyboardView implements
// {@link GestureEnabler#OnGestureListener} methods. // {@link GestureEnabler#OnGestureListener} methods.
private Key mCurrentKey; private Key mCurrentKey;
private Runnable mPendingKeyDown;
private final Handler mHandler;
private Key getKey(final MotionEvent e) { private Key getKey(final MotionEvent e) {
final int index = e.getActionIndex(); final int index = e.getActionIndex();
@ -101,6 +108,8 @@ public final class EmojiPageKeyboardView extends KeyboardView implements
} }
public void releaseCurrentKey() { public void releaseCurrentKey() {
mHandler.removeCallbacks(mPendingKeyDown);
mPendingKeyDown = null;
final Key currentKey = mCurrentKey; final Key currentKey = mCurrentKey;
if (currentKey == null) { if (currentKey == null) {
return; return;
@ -118,9 +127,17 @@ public final class EmojiPageKeyboardView extends KeyboardView implements
if (key == null) { if (key == null) {
return false; return false;
} }
// Do not trigger key-down effect right now in case this is actually a fling action.
mPendingKeyDown = new Runnable() {
@Override
public void run() {
mPendingKeyDown = null;
key.onPressed(); key.onPressed();
invalidateKey(key); invalidateKey(key);
mListener.onPressKey(key); mListener.onPressKey(key);
}
};
mHandler.postDelayed(mPendingKeyDown, KEY_PRESS_DELAY_TIME);
return false; return false;
} }
@ -132,13 +149,28 @@ public final class EmojiPageKeyboardView extends KeyboardView implements
@Override @Override
public boolean onSingleTapUp(final MotionEvent e) { public boolean onSingleTapUp(final MotionEvent e) {
final Key key = getKey(e); final Key key = getKey(e);
final Runnable pendingKeyDown = mPendingKeyDown;
final Key currentKey = mCurrentKey;
releaseCurrentKey(); releaseCurrentKey();
if (key == null) { if (key == null) {
return false; return false;
} }
if (key == currentKey && pendingKeyDown != null) {
pendingKeyDown.run();
// Trigger key-release event a little later so that a user can see visual feedback.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
key.onReleased(); key.onReleased();
invalidateKey(key); invalidateKey(key);
mListener.onReleaseKey(key); mListener.onReleaseKey(key);
}
}, KEY_RELEASE_DELAY_TIME);
} else {
key.onReleased();
invalidateKey(key);
mListener.onReleaseKey(key);
}
return true; return true;
} }