Merge "Refactor UI message handling class to be more readable"

This commit is contained in:
Tadashi G. Takaoka 2010-07-26 22:44:36 -07:00 committed by Android (Google) Code Review
commit 994d4000f4

View file

@ -171,11 +171,6 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
/** Listener for {@link OnKeyboardActionListener}. */ /** Listener for {@link OnKeyboardActionListener}. */
private OnKeyboardActionListener mKeyboardActionListener; private OnKeyboardActionListener mKeyboardActionListener;
private static final int MSG_SHOW_PREVIEW = 1;
private static final int MSG_REMOVE_PREVIEW = 2;
private static final int MSG_REPEAT = 3;
private static final int MSG_LONGPRESS = 4;
private static final int DELAY_BEFORE_PREVIEW = 0; private static final int DELAY_BEFORE_PREVIEW = 0;
private static final int DELAY_AFTER_PREVIEW = 70; private static final int DELAY_AFTER_PREVIEW = 70;
private static final int DEBOUNCE_TIME = 70; private static final int DEBOUNCE_TIME = 70;
@ -258,27 +253,79 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
/** The canvas for the above mutable keyboard bitmap */ /** The canvas for the above mutable keyboard bitmap */
private Canvas mCanvas; private Canvas mCanvas;
Handler mHandler = new Handler() { UIHandler mHandler = new UIHandler();
class UIHandler extends Handler {
private static final int MSG_POPUP_PREVIEW = 1;
private static final int MSG_DISMISS_PREVIEW = 2;
private static final int MSG_REPEAT_KEY = 3;
private static final int MSG_LOGPRESS_KEY = 4;
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {
case MSG_SHOW_PREVIEW: case MSG_POPUP_PREVIEW:
showKey(msg.arg1); showKey(msg.arg1);
break; break;
case MSG_REMOVE_PREVIEW: case MSG_DISMISS_PREVIEW:
mPreviewText.setVisibility(INVISIBLE); mPreviewText.setVisibility(INVISIBLE);
break; break;
case MSG_REPEAT: case MSG_REPEAT_KEY:
if (repeatKey()) { if (repeatKey()) {
Message repeat = Message.obtain(this, MSG_REPEAT); startKeyRepeatTimer(REPEAT_INTERVAL);
sendMessageDelayed(repeat, REPEAT_INTERVAL);
} }
break; break;
case MSG_LONGPRESS: case MSG_LOGPRESS_KEY:
openPopupIfRequired((MotionEvent) msg.obj); openPopupIfRequired((MotionEvent) msg.obj);
break; break;
} }
} }
public void popupPreview(int keyIndex, long delay) {
sendMessageDelayed(obtainMessage(MSG_POPUP_PREVIEW, keyIndex, 0), delay);
}
public void cancelPopupPreview() {
removeMessages(MSG_POPUP_PREVIEW);
}
public void dismissPreview(long delay) {
sendMessageDelayed(obtainMessage(MSG_DISMISS_PREVIEW), delay);
}
public void cancelDismissPreview() {
removeMessages(MSG_DISMISS_PREVIEW);
}
public void startKeyRepeatTimer(long delay) {
sendMessageDelayed(obtainMessage(MSG_REPEAT_KEY), delay);
}
public void startLongPressTimer(MotionEvent me, long delay) {
sendMessageDelayed(obtainMessage(MSG_LOGPRESS_KEY, me), delay);
}
public void cancelLongPressTimer() {
removeMessages(MSG_LOGPRESS_KEY);
}
public void cancelKeyTimers() {
removeMessages(MSG_REPEAT_KEY);
removeMessages(MSG_LOGPRESS_KEY);
}
public void cancelKeyTimersAndPopupPreview() {
removeMessages(MSG_REPEAT_KEY);
removeMessages(MSG_LOGPRESS_KEY);
removeMessages(MSG_POPUP_PREVIEW);
}
public void cancelAllMessages() {
removeMessages(MSG_REPEAT_KEY);
removeMessages(MSG_LOGPRESS_KEY);
removeMessages(MSG_POPUP_PREVIEW);
removeMessages(MSG_DISMISS_PREVIEW);
}
}; };
public LatinKeyboardBaseView(Context context, AttributeSet attrs) { public LatinKeyboardBaseView(Context context, AttributeSet attrs) {
@ -475,8 +522,8 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
if (mKeyboard != null) { if (mKeyboard != null) {
showPreview(NOT_A_KEY); showPreview(NOT_A_KEY);
} }
// Remove any pending messages // Remove any pending messages, except dismissing preview
removeMessages(); mHandler.cancelKeyTimersAndPopupPreview();
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()]);
@ -885,12 +932,10 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
} }
// If key changed and preview is on ... // If key changed and preview is on ...
if (oldKeyIndex != mCurrentKeyIndex && mShowPreview) { if (oldKeyIndex != mCurrentKeyIndex && mShowPreview) {
mHandler.removeMessages(MSG_SHOW_PREVIEW); mHandler.cancelPopupPreview();
if (previewPopup.isShowing()) { if (previewPopup.isShowing()) {
if (keyIndex == NOT_A_KEY) { if (keyIndex == NOT_A_KEY) {
mHandler.sendMessageDelayed(mHandler mHandler.dismissPreview(DELAY_AFTER_PREVIEW);
.obtainMessage(MSG_REMOVE_PREVIEW),
DELAY_AFTER_PREVIEW);
} }
} }
if (keyIndex != NOT_A_KEY) { if (keyIndex != NOT_A_KEY) {
@ -898,9 +943,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
// Show right away, if it's already visible and finger is moving around // Show right away, if it's already visible and finger is moving around
showKey(keyIndex); showKey(keyIndex);
} else { } else {
mHandler.sendMessageDelayed( mHandler.popupPreview(keyIndex, DELAY_BEFORE_PREVIEW);
mHandler.obtainMessage(MSG_SHOW_PREVIEW, keyIndex, 0),
DELAY_BEFORE_PREVIEW);
} }
} }
} }
@ -944,7 +987,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2; mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
mPopupPreviewY = - mPreviewText.getMeasuredHeight(); mPopupPreviewY = - mPreviewText.getMeasuredHeight();
} }
mHandler.removeMessages(MSG_REMOVE_PREVIEW); mHandler.cancelDismissPreview();
if (mOffsetInWindow == null) { if (mOffsetInWindow == null) {
mOffsetInWindow = new int[2]; mOffsetInWindow = new int[2];
getLocationInWindow(mOffsetInWindow); getLocationInWindow(mOffsetInWindow);
@ -1190,8 +1233,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
if (mGestureDetector.onTouchEvent(me)) { if (mGestureDetector.onTouchEvent(me)) {
showPreview(NOT_A_KEY); showPreview(NOT_A_KEY);
mHandler.removeMessages(MSG_REPEAT); mHandler.cancelKeyTimers();
mHandler.removeMessages(MSG_LONGPRESS);
return true; return true;
} }
@ -1220,8 +1262,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
mKeys[keyIndex].codes[0] : 0); mKeys[keyIndex].codes[0] : 0);
if (mCurrentKey >= 0 && mKeys[mCurrentKey].repeatable) { if (mCurrentKey >= 0 && mKeys[mCurrentKey].repeatable) {
mRepeatKeyIndex = mCurrentKey; mRepeatKeyIndex = mCurrentKey;
Message msg = mHandler.obtainMessage(MSG_REPEAT); mHandler.startKeyRepeatTimer(REPEAT_START_DELAY);
mHandler.sendMessageDelayed(msg, REPEAT_START_DELAY);
repeatKey(); repeatKey();
// Delivering the key could have caused an abort // Delivering the key could have caused an abort
if (mAbortKey) { if (mAbortKey) {
@ -1230,8 +1271,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
} }
} }
if (mCurrentKey != NOT_A_KEY) { if (mCurrentKey != NOT_A_KEY) {
Message msg = mHandler.obtainMessage(MSG_LONGPRESS, me); mHandler.startLongPressTimer(me, LONGPRESS_TIMEOUT);
mHandler.sendMessageDelayed(msg, LONGPRESS_TIMEOUT);
} }
showPreview(keyIndex); showPreview(keyIndex);
break; break;
@ -1261,11 +1301,10 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
} }
if (!continueLongPress) { if (!continueLongPress) {
// Cancel old longpress // Cancel old longpress
mHandler.removeMessages(MSG_LONGPRESS); mHandler.cancelLongPressTimer();
// Start new longpress if key has changed // Start new longpress if key has changed
if (keyIndex != NOT_A_KEY) { if (keyIndex != NOT_A_KEY) {
Message msg = mHandler.obtainMessage(MSG_LONGPRESS, me); mHandler.startLongPressTimer(me, LONGPRESS_TIMEOUT);
mHandler.sendMessageDelayed(msg, LONGPRESS_TIMEOUT);
} }
} }
showPreview(mCurrentKey); showPreview(mCurrentKey);
@ -1273,7 +1312,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
break; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
removeMessages(); mHandler.cancelKeyTimersAndPopupPreview();
if (keyIndex == mCurrentKey) { if (keyIndex == mCurrentKey) {
mCurrentKeyTime += eventTime - mLastMoveTime; mCurrentKeyTime += eventTime - mLastMoveTime;
} else { } else {
@ -1299,7 +1338,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
mRepeatKeyIndex = NOT_A_KEY; mRepeatKeyIndex = NOT_A_KEY;
break; break;
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_CANCEL:
removeMessages(); mHandler.cancelKeyTimersAndPopupPreview();
dismissPopupKeyboard(); dismissPopupKeyboard();
mAbortKey = true; mAbortKey = true;
showPreview(NOT_A_KEY); showPreview(NOT_A_KEY);
@ -1337,7 +1376,7 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
if (mPreviewPopup.isShowing()) { if (mPreviewPopup.isShowing()) {
mPreviewPopup.dismiss(); mPreviewPopup.dismiss();
} }
removeMessages(); mHandler.cancelAllMessages();
dismissPopupKeyboard(); dismissPopupKeyboard();
mBuffer = null; mBuffer = null;
@ -1345,12 +1384,6 @@ public class LatinKeyboardBaseView extends View implements View.OnClickListener
mMiniKeyboardCache.clear(); mMiniKeyboardCache.clear();
} }
private void removeMessages() {
mHandler.removeMessages(MSG_REPEAT);
mHandler.removeMessages(MSG_LONGPRESS);
mHandler.removeMessages(MSG_SHOW_PREVIEW);
}
@Override @Override
public void onDetachedFromWindow() { public void onDetachedFromWindow() {
super.onDetachedFromWindow(); super.onDetachedFromWindow();