Merge "Introduce a delay for key-press feedback on the Emoji palette"

main
Yohei Yukawa 2014-02-03 02:14:58 +00:00 committed by Android (Google) Code Review
commit 08d72c52f9
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,16 +36,19 @@ 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);
} }
private static final OnKeyEventListener EMPTY_LISTENER = new OnKeyEventListener() { private static final OnKeyEventListener EMPTY_LISTENER = new OnKeyEventListener() {
@Override @Override
public void onPressKey(final Key key) {} public void onPressKey(final Key key) {}
@Override @Override
public void onReleaseKey(final Key key) {} public void onReleaseKey(final Key key) {}
}; };
private OnKeyEventListener mListener = EMPTY_LISTENER; private OnKeyEventListener mListener = EMPTY_LISTENER;
@ -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;
} }
key.onPressed(); // Do not trigger key-down effect right now in case this is actually a fling action.
invalidateKey(key); mPendingKeyDown = new Runnable() {
mListener.onPressKey(key); @Override
public void run() {
mPendingKeyDown = null;
key.onPressed();
invalidateKey(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;
} }
key.onReleased(); if (key == currentKey && pendingKeyDown != null) {
invalidateKey(key); pendingKeyDown.run();
mListener.onReleaseKey(key); // 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();
invalidateKey(key);
mListener.onReleaseKey(key);
}
}, KEY_RELEASE_DELAY_TIME);
} else {
key.onReleased();
invalidateKey(key);
mListener.onReleaseKey(key);
}
return true; return true;
} }