am f4271e51: Refactor key repeating related code

Merge commit 'f4271e51e713688a4e9f368956fb41aa45b76ff7' into gingerbread-plus-aosp

* commit 'f4271e51e713688a4e9f368956fb41aa45b76ff7':
  Refactor key repeating related code
main
Tadashi G. Takaoka 2010-08-24 23:14:00 -07:00 committed by Android Git Automerger
commit f8737965be
1 changed files with 50 additions and 35 deletions

View File

@ -21,11 +21,11 @@ import android.content.res.TypedArray;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.PorterDuff; import android.graphics.PorterDuff;
import android.graphics.Rect; import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.Paint.Align;
import android.graphics.Region.Op; import android.graphics.Region.Op;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.inputmethodservice.Keyboard; import android.inputmethodservice.Keyboard;
import android.inputmethodservice.Keyboard.Key; import android.inputmethodservice.Keyboard.Key;
@ -200,7 +200,6 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
private GestureDetector mGestureDetector; private GestureDetector mGestureDetector;
private int mPopupX; private int mPopupX;
private int mPopupY; private int mPopupY;
private int mRepeatKeyIndex = NOT_A_KEY;
private int mPopupLayout; private int mPopupLayout;
private boolean mAbortKey; private boolean mAbortKey;
private Key mInvalidatedKey; private Key mInvalidatedKey;
@ -251,6 +250,8 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
private static final int MSG_REPEAT_KEY = 3; private static final int MSG_REPEAT_KEY = 3;
private static final int MSG_LOGPRESS_KEY = 4; private static final int MSG_LOGPRESS_KEY = 4;
private boolean mInKeyRepeat;
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {
@ -261,9 +262,8 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
mPreviewText.setVisibility(INVISIBLE); mPreviewText.setVisibility(INVISIBLE);
break; break;
case MSG_REPEAT_KEY: case MSG_REPEAT_KEY:
if (repeatKey()) { repeatKey(msg.arg1);
startKeyRepeatTimer(REPEAT_INTERVAL); startKeyRepeatTimer(REPEAT_INTERVAL, msg.arg1);
}
break; break;
case MSG_LOGPRESS_KEY: case MSG_LOGPRESS_KEY:
openPopupIfRequired(msg.arg1); openPopupIfRequired(msg.arg1);
@ -288,8 +288,18 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
removeMessages(MSG_DISMISS_PREVIEW); removeMessages(MSG_DISMISS_PREVIEW);
} }
public void startKeyRepeatTimer(long delay) { public void startKeyRepeatTimer(long delay, int keyIndex) {
sendMessageDelayed(obtainMessage(MSG_REPEAT_KEY), delay); mInKeyRepeat = true;
sendMessageDelayed(obtainMessage(MSG_REPEAT_KEY, keyIndex, 0), delay);
}
public void cancelKeyRepeatTimer() {
mInKeyRepeat = false;
removeMessages(MSG_REPEAT_KEY);
}
public boolean isInKeyRepeat() {
return mInKeyRepeat;
} }
public void startLongPressTimer(int keyIndex, long delay) { public void startLongPressTimer(int keyIndex, long delay) {
@ -302,21 +312,14 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
} }
public void cancelKeyTimers() { public void cancelKeyTimers() {
removeMessages(MSG_REPEAT_KEY); cancelKeyRepeatTimer();
removeMessages(MSG_LOGPRESS_KEY); cancelLongPressTimer();
}
public void cancelKeyTimersAndPopupPreview() {
removeMessages(MSG_REPEAT_KEY);
removeMessages(MSG_LOGPRESS_KEY);
removeMessages(MSG_POPUP_PREVIEW);
} }
public void cancelAllMessages() { public void cancelAllMessages() {
removeMessages(MSG_REPEAT_KEY); cancelKeyTimers();
removeMessages(MSG_LOGPRESS_KEY); cancelPopupPreview();
removeMessages(MSG_POPUP_PREVIEW); cancelDismissPreview();
removeMessages(MSG_DISMISS_PREVIEW);
} }
}; };
@ -621,7 +624,8 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
showPreview(NOT_A_KEY); showPreview(NOT_A_KEY);
} }
// Remove any pending messages, except dismissing preview // Remove any pending messages, except dismissing preview
mHandler.cancelKeyTimersAndPopupPreview(); mHandler.cancelKeyTimers();
mHandler.cancelPopupPreview();
mKeyboard = keyboard; mKeyboard = keyboard;
List<Key> keys = mKeyboard.getKeys(); List<Key> keys = mKeyboard.getKeys();
mKeys = keys.toArray(new Key[keys.size()]); mKeys = keys.toArray(new Key[keys.size()]);
@ -1308,6 +1312,16 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
return true; return true;
} }
if (mHandler.isInKeyRepeat()) {
// Will keep being in the key repeating mode while the key is being pressed. It'll be
// canceled otherwise.
if (pointerCount == 1 && action == MotionEvent.ACTION_MOVE) {
return true;
} else {
mHandler.cancelKeyRepeatTimer();
}
}
if (pointerCount != mOldPointerCount) { if (pointerCount != mOldPointerCount) {
if (pointerCount == 1) { if (pointerCount == 1) {
// Send a down event for the latest pointer // Send a down event for the latest pointer
@ -1357,13 +1371,12 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
checkMultiTap(eventTime, keyIndex); checkMultiTap(eventTime, keyIndex);
mKeyboardActionListener.onPress(keyIndex != NOT_A_KEY ? mKeyboardActionListener.onPress(keyIndex != NOT_A_KEY ?
mKeys[keyIndex].codes[0] : 0); mKeys[keyIndex].codes[0] : 0);
if (mCurrentKey >= 0 && mKeys[mCurrentKey].repeatable) { if (keyIndex >= 0 && mKeys[keyIndex].repeatable) {
mRepeatKeyIndex = mCurrentKey; repeatKey(keyIndex);
mHandler.startKeyRepeatTimer(REPEAT_START_DELAY); mHandler.startKeyRepeatTimer(REPEAT_START_DELAY, keyIndex);
repeatKey();
// Delivering the key could have caused an abort // Delivering the key could have caused an abort
if (mAbortKey) { if (mAbortKey) {
mRepeatKeyIndex = NOT_A_KEY; mHandler.cancelKeyRepeatTimer();
break; break;
} }
} }
@ -1382,7 +1395,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
} else if (mDebouncer.isMinorMoveBounce(touchX, touchY, keyIndex, } else if (mDebouncer.isMinorMoveBounce(touchX, touchY, keyIndex,
mCurrentKey)) { mCurrentKey)) {
mDebouncer.updateTimeDebouncing(eventTime); mDebouncer.updateTimeDebouncing(eventTime);
} else if (mRepeatKeyIndex == NOT_A_KEY) { } else {
resetMultiTap(); resetMultiTap();
mDebouncer.resetTimeDebouncing(eventTime, mCurrentKey); mDebouncer.resetTimeDebouncing(eventTime, mCurrentKey);
mDebouncer.resetMoveDebouncing(); mDebouncer.resetMoveDebouncing();
@ -1402,7 +1415,8 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
mHandler.cancelKeyTimersAndPopupPreview(); mHandler.cancelKeyTimers();
mHandler.cancelPopupPreview();
if (mDebouncer.isMinorMoveBounce(touchX, touchY, keyIndex, mCurrentKey)) { if (mDebouncer.isMinorMoveBounce(touchX, touchY, keyIndex, mCurrentKey)) {
mDebouncer.updateTimeDebouncing(eventTime); mDebouncer.updateTimeDebouncing(eventTime);
} else { } else {
@ -1417,15 +1431,15 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
} }
showPreview(NOT_A_KEY); showPreview(NOT_A_KEY);
// If we're not on a repeating key (which sends on a DOWN event) // If we're not on a repeating key (which sends on a DOWN event)
if (mRepeatKeyIndex == NOT_A_KEY && !mMiniKeyboardOnScreen && !mAbortKey) { if (!mMiniKeyboardOnScreen && !mAbortKey) {
detectAndSendKey(mCurrentKey, touchX, touchY, eventTime); detectAndSendKey(mCurrentKey, touchX, touchY, eventTime);
} }
invalidateKey(keyIndex); invalidateKey(keyIndex);
mRepeatKeyIndex = NOT_A_KEY;
break; break;
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
mHandler.cancelKeyTimersAndPopupPreview(); mHandler.cancelKeyTimers();
mHandler.cancelPopupPreview();
dismissPopupKeyboard(); dismissPopupKeyboard();
mAbortKey = true; mAbortKey = true;
showPreview(NOT_A_KEY); showPreview(NOT_A_KEY);
@ -1436,10 +1450,11 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
return true; return true;
} }
private boolean repeatKey() { private void repeatKey(int keyIndex) {
Key key = mKeys[mRepeatKeyIndex]; Key key = mKeys[keyIndex];
detectAndSendKey(mCurrentKey, key.x, key.y, mLastTapTime); // While key is repeating, because there is no need to handle multi-tap key, we can pass
return true; // -1 as eventTime argument.
detectAndSendKey(keyIndex, key.x, key.y, -1);
} }
protected void swipeRight() { protected void swipeRight() {