Consolidate TimerHandler.Callbacks with PointerTracker.DrawingProxy

Change-Id: I654f7483d1c013ac2685a45af4eb2af15fa219ad
main
Tadashi G. Takaoka 2014-10-29 19:05:36 +09:00
parent d9b1327c21
commit be708c4e59
4 changed files with 57 additions and 52 deletions

View File

@ -41,6 +41,8 @@ import com.android.inputmethod.latin.utils.TypefaceUtils;
import java.util.HashSet; import java.util.HashSet;
import javax.annotation.Nullable;
/** /**
* A view that renders a virtual {@link Keyboard}. * A view that renders a virtual {@link Keyboard}.
* *
@ -557,9 +559,10 @@ public class KeyboardView extends View {
* @param key key in the attached {@link Keyboard}. * @param key key in the attached {@link Keyboard}.
* @see #invalidateAllKeys * @see #invalidateAllKeys
*/ */
public void invalidateKey(final Key key) { public void invalidateKey(@Nullable final Key key) {
if (mInvalidateAllKeys) return; if (key == null || mInvalidateAllKeys) {
if (key == null) return; return;
}
mInvalidatedKeys.add(key); mInvalidatedKeys.add(key);
final int x = key.getX() + getPaddingLeft(); final int x = key.getX() + getPaddingLeft();
final int y = key.getY() + getPaddingTop(); final int y = key.getY() + getPaddingTop();

View File

@ -39,6 +39,7 @@ import android.view.ViewGroup;
import com.android.inputmethod.accessibility.AccessibilityUtils; import com.android.inputmethod.accessibility.AccessibilityUtils;
import com.android.inputmethod.accessibility.MainKeyboardAccessibilityDelegate; import com.android.inputmethod.accessibility.MainKeyboardAccessibilityDelegate;
import com.android.inputmethod.annotations.ExternallyReferenced; import com.android.inputmethod.annotations.ExternallyReferenced;
import com.android.inputmethod.keyboard.PointerTracker.DrawingProxy;
import com.android.inputmethod.keyboard.internal.DrawingPreviewPlacerView; import com.android.inputmethod.keyboard.internal.DrawingPreviewPlacerView;
import com.android.inputmethod.keyboard.internal.GestureFloatingTextDrawingPreview; import com.android.inputmethod.keyboard.internal.GestureFloatingTextDrawingPreview;
import com.android.inputmethod.keyboard.internal.GestureTrailsDrawingPreview; import com.android.inputmethod.keyboard.internal.GestureTrailsDrawingPreview;
@ -110,7 +111,7 @@ import javax.annotation.Nullable;
* @attr ref R.styleable#MainKeyboardView_suppressKeyPreviewAfterBatchInputDuration * @attr ref R.styleable#MainKeyboardView_suppressKeyPreviewAfterBatchInputDuration
*/ */
public final class MainKeyboardView extends KeyboardView implements PointerTracker.DrawingProxy, public final class MainKeyboardView extends KeyboardView implements PointerTracker.DrawingProxy,
MoreKeysPanel.Controller, TimerHandler.Callbacks { MoreKeysPanel.Controller {
private static final String TAG = MainKeyboardView.class.getSimpleName(); private static final String TAG = MainKeyboardView.class.getSimpleName();
/** Listener for {@link KeyboardActionListener}. */ /** Listener for {@link KeyboardActionListener}. */
@ -178,7 +179,8 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
public MainKeyboardView(final Context context, final AttributeSet attrs, final int defStyle) { public MainKeyboardView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle); super(context, attrs, defStyle);
mDrawingPreviewPlacerView = new DrawingPreviewPlacerView(context, attrs); final DrawingPreviewPlacerView drawingPreviewPlacerView =
new DrawingPreviewPlacerView(context, attrs);
final TypedArray mainKeyboardViewAttr = context.obtainStyledAttributes( final TypedArray mainKeyboardViewAttr = context.obtainStyledAttributes(
attrs, R.styleable.MainKeyboardView, defStyle, R.style.MainKeyboardView); attrs, R.styleable.MainKeyboardView, defStyle, R.style.MainKeyboardView);
@ -246,15 +248,17 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
mGestureFloatingTextDrawingPreview = new GestureFloatingTextDrawingPreview( mGestureFloatingTextDrawingPreview = new GestureFloatingTextDrawingPreview(
mainKeyboardViewAttr); mainKeyboardViewAttr);
mGestureFloatingTextDrawingPreview.setDrawingView(mDrawingPreviewPlacerView); mGestureFloatingTextDrawingPreview.setDrawingView(drawingPreviewPlacerView);
mGestureTrailsDrawingPreview = new GestureTrailsDrawingPreview(mainKeyboardViewAttr); mGestureTrailsDrawingPreview = new GestureTrailsDrawingPreview(mainKeyboardViewAttr);
mGestureTrailsDrawingPreview.setDrawingView(mDrawingPreviewPlacerView); mGestureTrailsDrawingPreview.setDrawingView(drawingPreviewPlacerView);
mSlidingKeyInputDrawingPreview = new SlidingKeyInputDrawingPreview(mainKeyboardViewAttr); mSlidingKeyInputDrawingPreview = new SlidingKeyInputDrawingPreview(mainKeyboardViewAttr);
mSlidingKeyInputDrawingPreview.setDrawingView(mDrawingPreviewPlacerView); mSlidingKeyInputDrawingPreview.setDrawingView(drawingPreviewPlacerView);
mainKeyboardViewAttr.recycle(); mainKeyboardViewAttr.recycle();
mDrawingPreviewPlacerView = drawingPreviewPlacerView;
final LayoutInflater inflater = LayoutInflater.from(getContext()); final LayoutInflater inflater = LayoutInflater.from(getContext());
mMoreKeysKeyboardContainer = inflater.inflate(moreKeysKeyboardLayoutId, null); mMoreKeysKeyboardContainer = inflater.inflate(moreKeysKeyboardLayoutId, null);
mMoreKeysKeyboardForActionContainer = inflater.inflate( mMoreKeysKeyboardForActionContainer = inflater.inflate(
@ -307,20 +311,20 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
animatorToStart.setCurrentPlayTime(startTime); animatorToStart.setCurrentPlayTime(startTime);
} }
// Implements {@link TimerHander.Callbacks#startWhileTypingAnimation(int)}. // Implements {@link DrawingProxy#startWhileTypingAnimation(int)}.
/** /**
* Called when a while-typing-animation should be started. * Called when a while-typing-animation should be started.
* @param fadeInOrOut {@link TimerHandler.Callbacks#FADE_IN} starts while-typing-fade-in * @param fadeInOrOut {@link DrawingProxy#FADE_IN} starts while-typing-fade-in animation.
* animation. {@link TimerHandler.Callbacks#FADE_OUT} starts while-typing-fade-out animation. * {@link DrawingProxy#FADE_OUT} starts while-typing-fade-out animation.
*/ */
@Override @Override
public void startWhileTypingAnimation(final int fadeInOrOut) { public void startWhileTypingAnimation(final int fadeInOrOut) {
switch (fadeInOrOut) { switch (fadeInOrOut) {
case TimerHandler.Callbacks.FADE_IN: case DrawingProxy.FADE_IN:
cancelAndStartAnimators( cancelAndStartAnimators(
mAltCodeKeyWhileTypingFadeoutAnimator, mAltCodeKeyWhileTypingFadeinAnimator); mAltCodeKeyWhileTypingFadeoutAnimator, mAltCodeKeyWhileTypingFadeinAnimator);
break; break;
case TimerHandler.Callbacks.FADE_OUT: case DrawingProxy.FADE_OUT:
cancelAndStartAnimators( cancelAndStartAnimators(
mAltCodeKeyWhileTypingFadeinAnimator, mAltCodeKeyWhileTypingFadeoutAnimator); mAltCodeKeyWhileTypingFadeinAnimator, mAltCodeKeyWhileTypingFadeoutAnimator);
break; break;
@ -459,9 +463,9 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
} }
@Override @Override
public void showKeyPreview(final Key key) { public void showKeyPreview(@Nonnull final Key key) {
// If the key is invalid or has no key preview, we must not show key preview. // If the key is invalid or has no key preview, we must not show key preview.
if (key == null || key.noKeyPreview()) { if (key.noKeyPreview()) {
return; return;
} }
final Keyboard keyboard = getKeyboard(); final Keyboard keyboard = getKeyboard();
@ -480,7 +484,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
getWidth(), mOriginCoords, mDrawingPreviewPlacerView, isHardwareAccelerated()); getWidth(), mOriginCoords, mDrawingPreviewPlacerView, isHardwareAccelerated());
} }
// Implements {@link TimerHandler.Callbacks#dismissKeyPreviewWithoutDelay(Key)}. // Implements {@link DrawingProxy#dismissKeyPreviewWithoutDelay(Key)}.
@Override @Override
public void dismissKeyPreviewWithoutDelay(@Nonnull final Key key) { public void dismissKeyPreviewWithoutDelay(@Nonnull final Key key) {
mKeyPreviewChoreographer.dismissKeyPreview(key, false /* withAnimation */); mKeyPreviewChoreographer.dismissKeyPreview(key, false /* withAnimation */);
@ -488,7 +492,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
} }
@Override @Override
public void dismissKeyPreview(final Key key) { public void dismissKeyPreview(@Nonnull final Key key) {
if (isHardwareAccelerated()) { if (isHardwareAccelerated()) {
mKeyPreviewChoreographer.dismissKeyPreview(key, true /* withAnimation */); mKeyPreviewChoreographer.dismissKeyPreview(key, true /* withAnimation */);
return; return;
@ -502,7 +506,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
} }
@Override @Override
public void showSlidingKeyInputPreview(final PointerTracker tracker) { public void showSlidingKeyInputPreview(@Nullable final PointerTracker tracker) {
locatePreviewPlacerView(); locatePreviewPlacerView();
if (tracker != null) { if (tracker != null) {
mSlidingKeyInputDrawingPreview.setPreviewPosition(tracker); mSlidingKeyInputDrawingPreview.setPreviewPosition(tracker);
@ -529,14 +533,14 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
} }
} }
// Implements {@link TimerHandler.Callbacks#dismissGestureFloatingPreviewTextWithoutDelay()}. // Implements {@link DrawingProxy#dismissGestureFloatingPreviewTextWithoutDelay()}.
@Override @Override
public void dismissGestureFloatingPreviewTextWithoutDelay() { public void dismissGestureFloatingPreviewTextWithoutDelay() {
mGestureFloatingTextDrawingPreview.dismissGestureFloatingPreviewText(); mGestureFloatingTextDrawingPreview.dismissGestureFloatingPreviewText();
} }
@Override @Override
public void showGestureTrail(final PointerTracker tracker, public void showGestureTrail(@Nonnull final PointerTracker tracker,
final boolean showsFloatingPreviewText) { final boolean showsFloatingPreviewText) {
locatePreviewPlacerView(); locatePreviewPlacerView();
if (showsFloatingPreviewText) { if (showsFloatingPreviewText) {
@ -603,7 +607,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
return moreKeysKeyboardView; return moreKeysKeyboardView;
} }
// Implements {@link TimerHandler.Callbacks#onLongPress(PointerTracker)}. // Implements {@link DrawingProxy@onLongPress(PointerTracker)}.
/** /**
* Called when a key is long pressed. * Called when a key is long pressed.
* @param tracker the pointer tracker which pressed the parent key * @param tracker the pointer tracker which pressed the parent key

View File

@ -53,12 +53,18 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
private static boolean DEBUG_MODE = DebugFlags.DEBUG_ENABLED || DEBUG_EVENT; private static boolean DEBUG_MODE = DebugFlags.DEBUG_ENABLED || DEBUG_EVENT;
public interface DrawingProxy { public interface DrawingProxy {
public void invalidateKey(Key key); public void invalidateKey(@Nullable Key key);
public void showKeyPreview(Key key); public void showKeyPreview(@Nonnull Key key);
public void dismissKeyPreview(Key key); public void dismissKeyPreview(@Nonnull Key key);
public void dismissKeyPreviewWithoutDelay(@Nonnull Key key);
public void onLongPress(@Nonnull PointerTracker tracker);
public static final int FADE_IN = 0;
public static final int FADE_OUT = 1;
public void startWhileTypingAnimation(final int fadeInOrOut);
public void showSlidingKeyInputPreview(@Nullable PointerTracker tracker); public void showSlidingKeyInputPreview(@Nullable PointerTracker tracker);
public void showGestureTrail(@Nonnull PointerTracker tracker, public void showGestureTrail(@Nonnull PointerTracker tracker,
boolean showsFloatingPreviewText); boolean showsFloatingPreviewText);
public void dismissGestureFloatingPreviewTextWithoutDelay();
} }
public interface TimerProxy { public interface TimerProxy {
@ -166,6 +172,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
// The position and time at which first down event occurred. // The position and time at which first down event occurred.
private long mDownTime; private long mDownTime;
@Nonnull
private int[] mDownCoordinates = CoordinateUtils.newInstance(); private int[] mDownCoordinates = CoordinateUtils.newInstance();
private long mUpTime; private long mUpTime;
@ -433,12 +440,12 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
return mKeyDetector.detectHitKey(x, y); return mKeyDetector.detectHitKey(x, y);
} }
private void setReleasedKeyGraphics(final Key key) { private void setReleasedKeyGraphics(@Nullable final Key key) {
sDrawingProxy.dismissKeyPreview(key);
if (key == null) { if (key == null) {
return; return;
} }
sDrawingProxy.dismissKeyPreview(key);
// Even if the key is disabled, update the key release graphics just in case. // Even if the key is disabled, update the key release graphics just in case.
updateReleaseKeyGraphics(key); updateReleaseKeyGraphics(key);
@ -522,7 +529,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
return mGestureStrokeDrawingPoints; return mGestureStrokeDrawingPoints;
} }
public void getLastCoordinates(final int[] outCoords) { public void getLastCoordinates(@Nonnull final int[] outCoords) {
CoordinateUtils.set(outCoords, mLastX, mLastY); CoordinateUtils.set(outCoords, mLastX, mLastY);
} }
@ -530,7 +537,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element,
return mDownTime; return mDownTime;
} }
public void getDownCoordinates(final int[] outCoords) { public void getDownCoordinates(@Nonnull final int[] outCoords) {
CoordinateUtils.copy(outCoords, mDownCoordinates); CoordinateUtils.copy(outCoords, mDownCoordinates);
} }

View File

@ -22,24 +22,15 @@ import android.view.ViewConfiguration;
import com.android.inputmethod.keyboard.Key; import com.android.inputmethod.keyboard.Key;
import com.android.inputmethod.keyboard.PointerTracker; import com.android.inputmethod.keyboard.PointerTracker;
import com.android.inputmethod.keyboard.PointerTracker.DrawingProxy;
import com.android.inputmethod.keyboard.PointerTracker.TimerProxy; import com.android.inputmethod.keyboard.PointerTracker.TimerProxy;
import com.android.inputmethod.keyboard.internal.TimerHandler.Callbacks;
import com.android.inputmethod.latin.common.Constants; import com.android.inputmethod.latin.common.Constants;
import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper; import com.android.inputmethod.latin.utils.LeakGuardHandlerWrapper;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
// TODO: Separate this class into KeyTimerHandler and BatchInputTimerHandler or so. public final class TimerHandler extends LeakGuardHandlerWrapper<DrawingProxy>
public final class TimerHandler extends LeakGuardHandlerWrapper<Callbacks> implements TimerProxy { implements TimerProxy {
public interface Callbacks {
public static final int FADE_IN = 0;
public static final int FADE_OUT = 1;
public void startWhileTypingAnimation(final int fadeInOrOut);
public void onLongPress(@Nonnull PointerTracker tracker);
public void dismissKeyPreviewWithoutDelay(@Nonnull Key key);
public void dismissGestureFloatingPreviewTextWithoutDelay();
}
private static final int MSG_TYPING_STATE_EXPIRED = 0; private static final int MSG_TYPING_STATE_EXPIRED = 0;
private static final int MSG_REPEAT_KEY = 1; private static final int MSG_REPEAT_KEY = 1;
private static final int MSG_LONGPRESS_KEY = 2; private static final int MSG_LONGPRESS_KEY = 2;
@ -52,8 +43,8 @@ public final class TimerHandler extends LeakGuardHandlerWrapper<Callbacks> imple
private final int mIgnoreAltCodeKeyTimeout; private final int mIgnoreAltCodeKeyTimeout;
private final int mGestureRecognitionUpdateTime; private final int mGestureRecognitionUpdateTime;
public TimerHandler(@Nonnull final Callbacks ownerInstance, final int ignoreAltCodeKeyTimeout, public TimerHandler(@Nonnull final DrawingProxy ownerInstance,
final int gestureRecognitionUpdateTime) { final int ignoreAltCodeKeyTimeout, final int gestureRecognitionUpdateTime) {
super(ownerInstance); super(ownerInstance);
mIgnoreAltCodeKeyTimeout = ignoreAltCodeKeyTimeout; mIgnoreAltCodeKeyTimeout = ignoreAltCodeKeyTimeout;
mGestureRecognitionUpdateTime = gestureRecognitionUpdateTime; mGestureRecognitionUpdateTime = gestureRecognitionUpdateTime;
@ -61,13 +52,13 @@ public final class TimerHandler extends LeakGuardHandlerWrapper<Callbacks> imple
@Override @Override
public void handleMessage(final Message msg) { public void handleMessage(final Message msg) {
final Callbacks callbacks = getOwnerInstance(); final DrawingProxy drawingProxy = getOwnerInstance();
if (callbacks == null) { if (drawingProxy == null) {
return; return;
} }
switch (msg.what) { switch (msg.what) {
case MSG_TYPING_STATE_EXPIRED: case MSG_TYPING_STATE_EXPIRED:
callbacks.startWhileTypingAnimation(Callbacks.FADE_IN); drawingProxy.startWhileTypingAnimation(DrawingProxy.FADE_IN);
break; break;
case MSG_REPEAT_KEY: case MSG_REPEAT_KEY:
final PointerTracker tracker1 = (PointerTracker) msg.obj; final PointerTracker tracker1 = (PointerTracker) msg.obj;
@ -77,7 +68,7 @@ public final class TimerHandler extends LeakGuardHandlerWrapper<Callbacks> imple
case MSG_LONGPRESS_SHIFT_KEY: case MSG_LONGPRESS_SHIFT_KEY:
cancelLongPressTimers(); cancelLongPressTimers();
final PointerTracker tracker2 = (PointerTracker) msg.obj; final PointerTracker tracker2 = (PointerTracker) msg.obj;
callbacks.onLongPress(tracker2); drawingProxy.onLongPress(tracker2);
break; break;
case MSG_UPDATE_BATCH_INPUT: case MSG_UPDATE_BATCH_INPUT:
final PointerTracker tracker3 = (PointerTracker) msg.obj; final PointerTracker tracker3 = (PointerTracker) msg.obj;
@ -86,10 +77,10 @@ public final class TimerHandler extends LeakGuardHandlerWrapper<Callbacks> imple
break; break;
case MSG_DISMISS_KEY_PREVIEW: case MSG_DISMISS_KEY_PREVIEW:
final Key key = (Key) msg.obj; final Key key = (Key) msg.obj;
callbacks.dismissKeyPreviewWithoutDelay(key); drawingProxy.dismissKeyPreviewWithoutDelay(key);
break; break;
case MSG_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT: case MSG_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT:
callbacks.dismissGestureFloatingPreviewTextWithoutDelay(); drawingProxy.dismissGestureFloatingPreviewTextWithoutDelay();
break; break;
} }
} }
@ -155,8 +146,8 @@ public final class TimerHandler extends LeakGuardHandlerWrapper<Callbacks> imple
final boolean isTyping = isTypingState(); final boolean isTyping = isTypingState();
removeMessages(MSG_TYPING_STATE_EXPIRED); removeMessages(MSG_TYPING_STATE_EXPIRED);
final Callbacks callbacks = getOwnerInstance(); final DrawingProxy drawingProxy = getOwnerInstance();
if (callbacks == null) { if (drawingProxy == null) {
return; return;
} }
@ -164,7 +155,7 @@ public final class TimerHandler extends LeakGuardHandlerWrapper<Callbacks> imple
final int typedCode = typedKey.getCode(); final int typedCode = typedKey.getCode();
if (typedCode == Constants.CODE_SPACE || typedCode == Constants.CODE_ENTER) { if (typedCode == Constants.CODE_SPACE || typedCode == Constants.CODE_ENTER) {
if (isTyping) { if (isTyping) {
callbacks.startWhileTypingAnimation(Callbacks.FADE_IN); drawingProxy.startWhileTypingAnimation(DrawingProxy.FADE_IN);
} }
return; return;
} }
@ -174,7 +165,7 @@ public final class TimerHandler extends LeakGuardHandlerWrapper<Callbacks> imple
if (isTyping) { if (isTyping) {
return; return;
} }
callbacks.startWhileTypingAnimation(Callbacks.FADE_OUT); drawingProxy.startWhileTypingAnimation(DrawingProxy.FADE_OUT);
} }
@Override @Override