MainKeyboardView.java revision 9c3860ce461c3791891bf667edc77fe798c8d332
15f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka/*
25f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka * Copyright (C) 2011 The Android Open Source Project
35f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka *
45f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka * Licensed under the Apache License, Version 2.0 (the "License");
55f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka * you may not use this file except in compliance with the License.
65f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka * You may obtain a copy of the License at
75f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka *
85f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka *      http://www.apache.org/licenses/LICENSE-2.0
95f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka *
105f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka * Unless required by applicable law or agreed to in writing, software
115f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka * distributed under the License is distributed on an "AS IS" BASIS,
125f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka * See the License for the specific language governing permissions and
145f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka * limitations under the License.
155f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka */
165f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
175f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokapackage com.android.inputmethod.keyboard;
185f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
19d7c4ba170982ddce5ac12ea92c3c3d8b53d524baTadashi G. Takaokaimport android.animation.AnimatorInflater;
2031c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaokaimport android.animation.ObjectAnimator;
215f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport android.content.Context;
2213ae76d7a342581160c172cd21706b3d57d32dadTadashi G. Takaokaimport android.content.SharedPreferences;
235f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport android.content.pm.PackageManager;
2427b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaokaimport android.content.res.Resources;
254112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaokaimport android.content.res.TypedArray;
266dde878d515f7bf5268d16a8fe4921d8821c5ae7Tadashi G. Takaokaimport android.graphics.Canvas;
27f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaokaimport android.graphics.Color;
2822b48de11ce6f31a0edf90e1308073e67a7a2adbTadashi G. Takaokaimport android.graphics.Paint;
294112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaokaimport android.graphics.Paint.Align;
30bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaokaimport android.graphics.Typeface;
314112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaokaimport android.graphics.drawable.Drawable;
325f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport android.os.Message;
3372fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaokaimport android.os.SystemClock;
3413ae76d7a342581160c172cd21706b3d57d32dadTadashi G. Takaokaimport android.preference.PreferenceManager;
355f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport android.util.AttributeSet;
36375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaokaimport android.util.DisplayMetrics;
375f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport android.util.Log;
38375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaokaimport android.util.SparseArray;
390e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaokaimport android.util.TypedValue;
405f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport android.view.LayoutInflater;
415f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport android.view.MotionEvent;
425f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport android.view.View;
435f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport android.view.ViewConfiguration;
44b8dc67466339dc14653ad634c86851025373326bTadashi G. Takaokaimport android.view.ViewGroup;
45f6972561fcb45310f18230ce217f0c6bb57e7eeeTadashi G. Takaokaimport android.view.inputmethod.InputMethodSubtype;
460e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaokaimport android.widget.TextView;
475f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
485f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport com.android.inputmethod.accessibility.AccessibilityUtils;
495f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy;
5015f6d4ae34664ea3d92827a2c3003198c0bac70bTadashi G. Takaokaimport com.android.inputmethod.annotations.ExternallyReferenced;
51f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaokaimport com.android.inputmethod.keyboard.PointerTracker.DrawingProxy;
522321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaokaimport com.android.inputmethod.keyboard.PointerTracker.TimerProxy;
53fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaokaimport com.android.inputmethod.keyboard.internal.GestureFloatingPreviewText;
54fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaokaimport com.android.inputmethod.keyboard.internal.GestureTrailsPreview;
5549d8af8a4e900f3c68c333aba7fde0a11fd368b1Tadashi G. Takaokaimport com.android.inputmethod.keyboard.internal.KeyDrawParams;
560e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaokaimport com.android.inputmethod.keyboard.internal.KeyPreviewDrawParams;
57375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaokaimport com.android.inputmethod.keyboard.internal.PreviewPlacerView;
58fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaokaimport com.android.inputmethod.keyboard.internal.SlidingKeyInputPreview;
59081fedc42269845a31e962b2ac1aa017b3a2efeeTadashi G. Takaokaimport com.android.inputmethod.keyboard.internal.TouchScreenRegulator;
60375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaokaimport com.android.inputmethod.latin.CollectionUtils;
614daf32b6c0358f0273a99b622a259ecdf6b44fa4Tom Ouyangimport com.android.inputmethod.latin.Constants;
62547b638194c05f971003edb06c3c6c489a76da5fTadashi G. Takaokaimport com.android.inputmethod.latin.CoordinateUtils;
6313ae76d7a342581160c172cd21706b3d57d32dadTadashi G. Takaokaimport com.android.inputmethod.latin.DebugSettings;
646dde878d515f7bf5268d16a8fe4921d8821c5ae7Tadashi G. Takaokaimport com.android.inputmethod.latin.LatinIME;
6515d4793911fa305e0a58aced925961e948582979satokimport com.android.inputmethod.latin.LatinImeLogger;
665f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport com.android.inputmethod.latin.R;
679879f65651a748e4c0a45715eb7d5663652f1127Tadashi G. Takaokaimport com.android.inputmethod.latin.ResourceUtils;
68cc5e4a444524f73a112d7223d7940d41cf60d1f4Tadashi G. Takaokaimport com.android.inputmethod.latin.Settings;
695f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport com.android.inputmethod.latin.StaticInnerHandlerWrapper;
7027b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaokaimport com.android.inputmethod.latin.StringUtils;
713bf57a5624679a20db26df912077a53b9f90ad36Tadashi G. Takaokaimport com.android.inputmethod.latin.SubtypeLocale;
72375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaokaimport com.android.inputmethod.latin.SuggestedWords;
7315d4793911fa305e0a58aced925961e948582979satokimport com.android.inputmethod.latin.Utils.UsabilityStudyLogUtils;
74c166697e3f5ec600089987dbbff0be7f3e308565Ken Wakasaimport com.android.inputmethod.latin.define.ProductionFlag;
756b966160ac8570271547bf63217efa5e228d4accKurt Partridgeimport com.android.inputmethod.research.ResearchLogger;
765f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
7727b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaokaimport java.util.Locale;
785f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaokaimport java.util.WeakHashMap;
795f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
805f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka/**
815f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka * A view that is responsible for detecting key presses and touch movements.
825f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka *
83dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_autoCorrectionSpacebarLedEnabled
84dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_autoCorrectionSpacebarLedIcon
85dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_spacebarTextRatio
86dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_spacebarTextColor
87dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_spacebarTextShadowColor
88dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_languageOnSpacebarFinalAlpha
89dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_languageOnSpacebarFadeoutAnimator
90dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_altCodeKeyWhileTypingFadeoutAnimator
91dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_altCodeKeyWhileTypingFadeinAnimator
92dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_keyHysteresisDistance
93dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_touchNoiseThresholdTime
94dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_touchNoiseThresholdDistance
95dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_slidingKeyInputEnable
96dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_keyRepeatStartTimeout
97dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_keyRepeatInterval
98dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_longPressKeyTimeout
99dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_longPressShiftKeyTimeout
100dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_ignoreAltCodeKeyTimeout
101375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_keyPreviewLayout
1020e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_keyPreviewOffset
1030e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_keyPreviewHeight
1040e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_keyPreviewLingerTimeout
105d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_moreKeysKeyboardLayout
106f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_backgroundDimAlpha
107dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_showMoreKeysKeyboardAtTouchPoint
108375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_gestureFloatingPreviewTextLingerTimeout
1093623b9767b3d5f122f574d4c4d14aa79ed305752Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_gestureStaticTimeThresholdAfterFastTyping
1103623b9767b3d5f122f574d4c4d14aa79ed305752Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_gestureDetectFastMoveSpeedThreshold
1113623b9767b3d5f122f574d4c4d14aa79ed305752Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_gestureDynamicThresholdDecayDuration
1123623b9767b3d5f122f574d4c4d14aa79ed305752Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_gestureDynamicTimeThresholdFrom
1133623b9767b3d5f122f574d4c4d14aa79ed305752Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_gestureDynamicTimeThresholdTo
1143623b9767b3d5f122f574d4c4d14aa79ed305752Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_gestureDynamicDistanceThresholdFrom
1153623b9767b3d5f122f574d4c4d14aa79ed305752Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_gestureDynamicDistanceThresholdTo
1163623b9767b3d5f122f574d4c4d14aa79ed305752Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_gestureSamplingMinimumDistance
1173623b9767b3d5f122f574d4c4d14aa79ed305752Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_gestureRecognitionMinimumTime
1183623b9767b3d5f122f574d4c4d14aa79ed305752Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_gestureRecognitionSpeedThreshold
1193623b9767b3d5f122f574d4c4d14aa79ed305752Tadashi G. Takaoka * @attr ref R.styleable#MainKeyboardView_suppressKeyPreviewAfterBatchInputDuration
1205f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka */
121a28a05e971cc242b338331a3b78276fa95188d19Tadashi G. Takaokapublic final class MainKeyboardView extends KeyboardView implements PointerTracker.KeyEventHandler,
1220e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        PointerTracker.DrawingProxy, MoreKeysPanel.Controller,
1230e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        TouchScreenRegulator.ProcessMotionEvent {
124c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka    private static final String TAG = MainKeyboardView.class.getSimpleName();
1255f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
126d05afa3f4c59641c8fabed034e457cb25f0c57f0Kurt Partridge    // TODO: Kill process when the usability study mode was changed.
127d05afa3f4c59641c8fabed034e457cb25f0c57f0Kurt Partridge    private static final boolean ENABLE_USABILITY_STUDY_LOG = LatinImeLogger.sUsabilityStudy;
128d05afa3f4c59641c8fabed034e457cb25f0c57f0Kurt Partridge
129bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka    /** Listener for {@link KeyboardActionListener}. */
130bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka    private KeyboardActionListener mKeyboardActionListener;
131bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka
132bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka    /* Space key and its icons */
1334112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka    private Key mSpaceKey;
1344112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka    private Drawable mSpaceIcon;
135bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka    // Stuff to draw language name on spacebar.
13631c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka    private final int mLanguageOnSpacebarFinalAlpha;
13731c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka    private ObjectAnimator mLanguageOnSpacebarFadeoutAnimator;
138bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka    private boolean mNeedsToDisplayLanguage;
139fd60b2f97035382b14dce207b3613711982a613eTadashi G. Takaoka    private boolean mHasMultipleEnabledIMEsOrSubtypes;
1404daf32b6c0358f0273a99b622a259ecdf6b44fa4Tom Ouyang    private int mLanguageOnSpacebarAnimAlpha = Constants.Color.ALPHA_OPAQUE;
1414112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka    private final float mSpacebarTextRatio;
1424112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka    private float mSpacebarTextSize;
1434112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka    private final int mSpacebarTextColor;
1444112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka    private final int mSpacebarTextShadowColor;
145bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka    // The minimum x-scale to fit the language name on spacebar.
146bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka    private static final float MINIMUM_XSCALE_OF_LANGUAGE_NAME = 0.8f;
147bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka    // Stuff to draw auto correction LED on spacebar.
148bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka    private boolean mAutoCorrectionSpacebarLedOn;
149bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka    private final boolean mAutoCorrectionSpacebarLedEnabled;
150bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka    private final Drawable mAutoCorrectionSpacebarLedIcon;
151bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka    private static final int SPACE_LED_LENGTH_PERCENT = 80;
15215d4793911fa305e0a58aced925961e948582979satok
15373a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka    // Stuff to draw altCodeWhileTyping keys.
15431c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka    private ObjectAnimator mAltCodeKeyWhileTypingFadeoutAnimator;
15531c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka    private ObjectAnimator mAltCodeKeyWhileTypingFadeinAnimator;
1564daf32b6c0358f0273a99b622a259ecdf6b44fa4Tom Ouyang    private int mAltCodeKeyWhileTypingAnimAlpha = Constants.Color.ALPHA_OPAQUE;
15773a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka
158375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    // Preview placer view
159375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    private final PreviewPlacerView mPreviewPlacerView;
160375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    private final int[] mOriginCoords = CoordinateUtils.newInstance();
161fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka    private final GestureFloatingPreviewText mGestureFloatingPreviewText;
162fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka    private final GestureTrailsPreview mGestureTrailsPreview;
163fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka    private final SlidingKeyInputPreview mSlidingKeyInputPreview;
164375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
1650e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    // Key preview
1660e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private static final int PREVIEW_ALPHA = 240;
167375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    private final int mKeyPreviewLayoutId;
1680e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private final int mKeyPreviewOffset;
1690e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private final int mKeyPreviewHeight;
170375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    private final SparseArray<TextView> mKeyPreviewTexts = CollectionUtils.newSparseArray();
1710e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private final KeyPreviewDrawParams mKeyPreviewDrawParams = new KeyPreviewDrawParams();
1720e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private boolean mShowKeyPreviewPopup = true;
1730e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private int mKeyPreviewLingerTimeout;
1740e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
1752affaf91a04d63e0994102299816014a8bbe11e1Tadashi G. Takaoka    // More keys keyboard
176f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka    private final Paint mBackgroundDimAlphaPaint = new Paint();
177f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka    private boolean mNeedsToDimEntireKeyboard;
178a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka    private final View mMoreKeysKeyboardContainer;
179a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka    private final WeakHashMap<Key, Keyboard> mMoreKeysKeyboardCache =
180a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka            CollectionUtils.newWeakHashMap();
1812affaf91a04d63e0994102299816014a8bbe11e1Tadashi G. Takaoka    private final boolean mConfigShowMoreKeysKeyboardAtTouchedPoint;
182d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    // More keys panel (used by both more keys keyboard and more suggestions view)
183d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    // TODO: Consider extending to support multiple more keys panels
184d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    private MoreKeysPanel mMoreKeysPanel;
1855f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
186375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    // Gesture floating preview text
187375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    // TODO: Make this parameter customizable by user via settings.
188375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    private int mGestureFloatingPreviewTextLingerTimeout;
189375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
190081fedc42269845a31e962b2ac1aa017b3a2efeeTadashi G. Takaoka    private final TouchScreenRegulator mTouchScreenRegulator;
1915f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
192d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    private KeyDetector mKeyDetector;
19313ae76d7a342581160c172cd21706b3d57d32dadTadashi G. Takaoka    private final boolean mHasDistinctMultitouch;
1945f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    private int mOldPointerCount = 1;
195e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka    private Key mOldKey;
1965f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
197160f01211d169d64102205e80e9ac8d46c7d674bTadashi G. Takaoka    private final KeyTimerHandler mKeyTimerHandler;
1985f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
199a28a05e971cc242b338331a3b78276fa95188d19Tadashi G. Takaoka    private static final class KeyTimerHandler extends StaticInnerHandlerWrapper<MainKeyboardView>
2002321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka            implements TimerProxy {
20127e48447a449d2eb534dfa2dc07060727e1a8fb0Tadashi G. Takaoka        private static final int MSG_TYPING_STATE_EXPIRED = 0;
202f60d09ac3086f308cafcee13ebcb94c562f9e58eTadashi G. Takaoka        private static final int MSG_REPEAT_KEY = 1;
203f60d09ac3086f308cafcee13ebcb94c562f9e58eTadashi G. Takaoka        private static final int MSG_LONGPRESS_KEY = 2;
2040ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka        private static final int MSG_DOUBLE_TAP = 3;
20572fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka        private static final int MSG_UPDATE_BATCH_INPUT = 4;
2065f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
2075509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka        private final int mKeyRepeatStartTimeout;
2085509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka        private final int mKeyRepeatInterval;
209cc5e4a444524f73a112d7223d7940d41cf60d1f4Tadashi G. Takaoka        private final int mLongPressShiftLockTimeout;
2105509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka        private final int mIgnoreAltCodeKeyTimeout;
21172fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka        private final int mGestureRecognitionUpdateTime;
2125f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
2135509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka        public KeyTimerHandler(final MainKeyboardView outerInstance,
2145509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka                final TypedArray mainKeyboardViewAttr) {
2155f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            super(outerInstance);
2165509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka
2175509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka            mKeyRepeatStartTimeout = mainKeyboardViewAttr.getInt(
2185509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka                    R.styleable.MainKeyboardView_keyRepeatStartTimeout, 0);
2195509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka            mKeyRepeatInterval = mainKeyboardViewAttr.getInt(
2205509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka                    R.styleable.MainKeyboardView_keyRepeatInterval, 0);
221cc5e4a444524f73a112d7223d7940d41cf60d1f4Tadashi G. Takaoka            mLongPressShiftLockTimeout = mainKeyboardViewAttr.getInt(
222cc5e4a444524f73a112d7223d7940d41cf60d1f4Tadashi G. Takaoka                    R.styleable.MainKeyboardView_longPressShiftLockTimeout, 0);
2235509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka            mIgnoreAltCodeKeyTimeout = mainKeyboardViewAttr.getInt(
2245509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka                    R.styleable.MainKeyboardView_ignoreAltCodeKeyTimeout, 0);
22572fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka            mGestureRecognitionUpdateTime = mainKeyboardViewAttr.getInt(
22672fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka                    R.styleable.MainKeyboardView_gestureRecognitionUpdateTime, 0);
2275f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
2285f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
2295f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        @Override
230dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka        public void handleMessage(final Message msg) {
231c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka            final MainKeyboardView keyboardView = getOuterInstance();
2325f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            final PointerTracker tracker = (PointerTracker) msg.obj;
2335f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            switch (msg.what) {
23427e48447a449d2eb534dfa2dc07060727e1a8fb0Tadashi G. Takaoka            case MSG_TYPING_STATE_EXPIRED:
23527e48447a449d2eb534dfa2dc07060727e1a8fb0Tadashi G. Takaoka                startWhileTypingFadeinAnimation(keyboardView);
23627e48447a449d2eb534dfa2dc07060727e1a8fb0Tadashi G. Takaoka                break;
2375f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            case MSG_REPEAT_KEY:
2388a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka                final Key currentKey = tracker.getKey();
2398a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka                if (currentKey != null && currentKey.mCode == msg.arg1) {
2408a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka                    tracker.onRegisterKey(currentKey);
2415509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka                    startKeyRepeatTimer(tracker, mKeyRepeatInterval);
2428a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka                }
2435f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                break;
2445f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            case MSG_LONGPRESS_KEY:
245a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka                if (tracker != null) {
246a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka                    keyboardView.onLongPress(tracker);
247a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka                } else {
248a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka                    KeyboardSwitcher.getInstance().onLongPressTimeout(msg.arg1);
249a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka                }
2505f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                break;
25172fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka            case MSG_UPDATE_BATCH_INPUT:
25272fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka                tracker.updateBatchInputByTimer(SystemClock.uptimeMillis());
25372fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka                startUpdateBatchInputTimer(tracker);
25472fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka                break;
2555f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            }
2565f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
2575f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
258dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka        private void startKeyRepeatTimer(final PointerTracker tracker, final long delay) {
2598a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka            final Key key = tracker.getKey();
26020dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            if (key == null) {
26120dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka                return;
26220dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            }
2638a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka            sendMessageDelayed(obtainMessage(MSG_REPEAT_KEY, key.mCode, 0, tracker), delay);
26432c54c4dbed0b27b7177f796d90a2ebb9566c9c9Tadashi G. Takaoka        }
26532c54c4dbed0b27b7177f796d90a2ebb9566c9c9Tadashi G. Takaoka
2662321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka        @Override
267dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka        public void startKeyRepeatTimer(final PointerTracker tracker) {
2685509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka            startKeyRepeatTimer(tracker, mKeyRepeatStartTimeout);
2695f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
2705f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
2715f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        public void cancelKeyRepeatTimer() {
2725f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            removeMessages(MSG_REPEAT_KEY);
2735f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
2745f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
2750d9d37cec2b3c4b4c3747baeb529bd2a70cbafb8Tadashi G. Takaoka        // TODO: Suppress layout changes in key repeat mode
2765f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        public boolean isInKeyRepeat() {
2770d9d37cec2b3c4b4c3747baeb529bd2a70cbafb8Tadashi G. Takaoka            return hasMessages(MSG_REPEAT_KEY);
2785f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
2795f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
2802321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka        @Override
281dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka        public void startLongPressTimer(final int code) {
28298b5c982b93cbfc74b221af30079ecb69dd4e0a1Tadashi G. Takaoka            cancelLongPressTimer();
283a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            final int delay;
284a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            switch (code) {
285240871ecafde7834ebb4270cd7758fc904a5f3a7Tadashi G. Takaoka            case Constants.CODE_SHIFT:
286cc5e4a444524f73a112d7223d7940d41cf60d1f4Tadashi G. Takaoka                delay = mLongPressShiftLockTimeout;
287a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka                break;
288a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            default:
289a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka                delay = 0;
290a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka                break;
291a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            }
292a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            if (delay > 0) {
293a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka                sendMessageDelayed(obtainMessage(MSG_LONGPRESS_KEY, code, 0), delay);
294a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            }
295a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka        }
296a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka
297a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka        @Override
298dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka        public void startLongPressTimer(final PointerTracker tracker) {
299a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            cancelLongPressTimer();
3007b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka            if (tracker == null) {
3017b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka                return;
3027b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka            }
3037b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka            final Key key = tracker.getKey();
3047b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka            final int delay;
3057b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka            switch (key.mCode) {
306240871ecafde7834ebb4270cd7758fc904a5f3a7Tadashi G. Takaoka            case Constants.CODE_SHIFT:
307cc5e4a444524f73a112d7223d7940d41cf60d1f4Tadashi G. Takaoka                delay = mLongPressShiftLockTimeout;
3087b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka                break;
3097b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka            default:
310cc5e4a444524f73a112d7223d7940d41cf60d1f4Tadashi G. Takaoka                final int longpressTimeout =
311cc5e4a444524f73a112d7223d7940d41cf60d1f4Tadashi G. Takaoka                        Settings.getInstance().getCurrent().mKeyLongpressTimeout;
3127b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka                if (KeyboardSwitcher.getInstance().isInMomentarySwitchState()) {
3137b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka                    // We use longer timeout for sliding finger input started from the symbols
3147b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka                    // mode key.
315cc5e4a444524f73a112d7223d7940d41cf60d1f4Tadashi G. Takaoka                    delay = longpressTimeout * 3;
3167b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka                } else {
317cc5e4a444524f73a112d7223d7940d41cf60d1f4Tadashi G. Takaoka                    delay = longpressTimeout;
318a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka                }
3197b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka                break;
3207b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka            }
3217b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka            if (delay > 0) {
3227b6afb1287fb6d5edfebed7403eb31ed23a8348dTadashi G. Takaoka                sendMessageDelayed(obtainMessage(MSG_LONGPRESS_KEY, tracker), delay);
323a5c96f376ad57e78a88942bb618e067054ed818aTadashi G. Takaoka            }
3245f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
3255f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
3262321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka        @Override
32798b5c982b93cbfc74b221af30079ecb69dd4e0a1Tadashi G. Takaoka        public void cancelLongPressTimer() {
3285f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            removeMessages(MSG_LONGPRESS_KEY);
3295f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
3305f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
331d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka        private static void cancelAndStartAnimators(final ObjectAnimator animatorToCancel,
33245213ed2a6e9a940ec540ff43ded7e877cb20dc9Tadashi G. Takaoka                final ObjectAnimator animatorToStart) {
33345213ed2a6e9a940ec540ff43ded7e877cb20dc9Tadashi G. Takaoka            float startFraction = 0.0f;
33445213ed2a6e9a940ec540ff43ded7e877cb20dc9Tadashi G. Takaoka            if (animatorToCancel.isStarted()) {
335b9720a55b47684589e3176434cd2b1a08942d112Tadashi G. Takaoka                animatorToCancel.cancel();
33645213ed2a6e9a940ec540ff43ded7e877cb20dc9Tadashi G. Takaoka                startFraction = 1.0f - animatorToCancel.getAnimatedFraction();
337b9720a55b47684589e3176434cd2b1a08942d112Tadashi G. Takaoka            }
33845213ed2a6e9a940ec540ff43ded7e877cb20dc9Tadashi G. Takaoka            final long startTime = (long)(animatorToStart.getDuration() * startFraction);
33945213ed2a6e9a940ec540ff43ded7e877cb20dc9Tadashi G. Takaoka            animatorToStart.start();
34045213ed2a6e9a940ec540ff43ded7e877cb20dc9Tadashi G. Takaoka            animatorToStart.setCurrentPlayTime(startTime);
341b9720a55b47684589e3176434cd2b1a08942d112Tadashi G. Takaoka        }
342b9720a55b47684589e3176434cd2b1a08942d112Tadashi G. Takaoka
343d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka        private static void startWhileTypingFadeinAnimation(final MainKeyboardView keyboardView) {
344d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            cancelAndStartAnimators(keyboardView.mAltCodeKeyWhileTypingFadeoutAnimator,
345d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka                    keyboardView.mAltCodeKeyWhileTypingFadeinAnimator);
346d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka        }
347d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka
348d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka        private static void startWhileTypingFadeoutAnimation(final MainKeyboardView keyboardView) {
349d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            cancelAndStartAnimators(keyboardView.mAltCodeKeyWhileTypingFadeinAnimator,
350d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka                    keyboardView.mAltCodeKeyWhileTypingFadeoutAnimator);
351d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka        }
352d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka
3532321caa1f9eb6c2d616bc36f11f5b48eebf144feTadashi G. Takaoka        @Override
354dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka        public void startTypingStateTimer(final Key typedKey) {
355d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            if (typedKey.isModifier() || typedKey.altCodeWhileTyping()) {
356d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka                return;
357d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            }
358d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka
35973a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka            final boolean isTyping = isTypingState();
360f1678ba8024606349bc184cfeaead2be059f7b5bTadashi G. Takaoka            removeMessages(MSG_TYPING_STATE_EXPIRED);
361d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            final MainKeyboardView keyboardView = getOuterInstance();
362d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka
363d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            // When user hits the space or the enter key, just cancel the while-typing timer.
364d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            final int typedCode = typedKey.mCode;
365240871ecafde7834ebb4270cd7758fc904a5f3a7Tadashi G. Takaoka            if (typedCode == Constants.CODE_SPACE || typedCode == Constants.CODE_ENTER) {
366d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka                startWhileTypingFadeinAnimation(keyboardView);
367d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka                return;
368d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            }
369d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka
37073a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka            sendMessageDelayed(
3715509798977a61dcb4a9dde9030f31bb138b71e3bTadashi G. Takaoka                    obtainMessage(MSG_TYPING_STATE_EXPIRED), mIgnoreAltCodeKeyTimeout);
37273a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka            if (isTyping) {
37373a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka                return;
37473a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka            }
375d2173b5737bf791a65f6b1e2980f26ebd94369c5Tadashi G. Takaoka            startWhileTypingFadeoutAnimation(keyboardView);
37693246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka        }
37793246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka
37893246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka        @Override
37973a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka        public boolean isTypingState() {
38073a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka            return hasMessages(MSG_TYPING_STATE_EXPIRED);
38193246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka        }
38293246652638f423d5220449f65495dea0639c750Tadashi G. Takaoka
3839f09c6fbc81ed40a41c0a23bd5913214ec7f6a9bTadashi G. Takaoka        @Override
3840ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka        public void startDoubleTapTimer() {
3850ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka            sendMessageDelayed(obtainMessage(MSG_DOUBLE_TAP),
3865f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                    ViewConfiguration.getDoubleTapTimeout());
3875f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
3885f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
3890ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka        @Override
390beb08b398fa73a26f2d42d6feec87e34a96ca2d9Tadashi G. Takaoka        public void cancelDoubleTapTimer() {
391beb08b398fa73a26f2d42d6feec87e34a96ca2d9Tadashi G. Takaoka            removeMessages(MSG_DOUBLE_TAP);
392beb08b398fa73a26f2d42d6feec87e34a96ca2d9Tadashi G. Takaoka        }
393beb08b398fa73a26f2d42d6feec87e34a96ca2d9Tadashi G. Takaoka
394beb08b398fa73a26f2d42d6feec87e34a96ca2d9Tadashi G. Takaoka        @Override
3950ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka        public boolean isInDoubleTapTimeout() {
3960ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka            return hasMessages(MSG_DOUBLE_TAP);
3975f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
398c71854a6614d1945739dcf40db61b0e887442b67Tadashi G. Takaoka
399c71854a6614d1945739dcf40db61b0e887442b67Tadashi G. Takaoka        @Override
4000ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka        public void cancelKeyTimers() {
4010ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka            cancelKeyRepeatTimer();
4020ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka            cancelLongPressTimer();
403c71854a6614d1945739dcf40db61b0e887442b67Tadashi G. Takaoka        }
404c71854a6614d1945739dcf40db61b0e887442b67Tadashi G. Takaoka
40572fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka        @Override
40672fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka        public void startUpdateBatchInputTimer(final PointerTracker tracker) {
40772fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka            if (mGestureRecognitionUpdateTime <= 0) {
40872fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka                return;
40972fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka            }
41072fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka            removeMessages(MSG_UPDATE_BATCH_INPUT, tracker);
41172fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka            sendMessageDelayed(obtainMessage(MSG_UPDATE_BATCH_INPUT, tracker),
41272fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka                    mGestureRecognitionUpdateTime);
41372fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka        }
41472fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka
41572fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka        @Override
416915f348b35cb66ed9696a51c9250f9b25799fb82Tadashi G. Takaoka        public void cancelUpdateBatchInputTimer(final PointerTracker tracker) {
417915f348b35cb66ed9696a51c9250f9b25799fb82Tadashi G. Takaoka            removeMessages(MSG_UPDATE_BATCH_INPUT, tracker);
418915f348b35cb66ed9696a51c9250f9b25799fb82Tadashi G. Takaoka        }
419915f348b35cb66ed9696a51c9250f9b25799fb82Tadashi G. Takaoka
420915f348b35cb66ed9696a51c9250f9b25799fb82Tadashi G. Takaoka        @Override
4212db9e1c447a71f0aec3067697cf294f711a9e4e0Tadashi G. Takaoka        public void cancelAllUpdateBatchInputTimers() {
42272fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka            removeMessages(MSG_UPDATE_BATCH_INPUT);
42372fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka        }
42472fd0968e5227ffc383b1f9d096872ba39cfdce8Tadashi G. Takaoka
4250ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka        public void cancelAllMessages() {
4260ed2d3a4491cb0f6142975a15b653be6079b6a4eTadashi G. Takaoka            cancelKeyTimers();
4272db9e1c447a71f0aec3067697cf294f711a9e4e0Tadashi G. Takaoka            cancelAllUpdateBatchInputTimers();
428c71854a6614d1945739dcf40db61b0e887442b67Tadashi G. Takaoka        }
429c71854a6614d1945739dcf40db61b0e887442b67Tadashi G. Takaoka    }
430c71854a6614d1945739dcf40db61b0e887442b67Tadashi G. Takaoka
431375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    private final DrawingHandler mDrawingHandler = new DrawingHandler(this);
432375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
433375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    public static class DrawingHandler extends StaticInnerHandlerWrapper<MainKeyboardView> {
434375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        private static final int MSG_DISMISS_KEY_PREVIEW = 0;
435375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        private static final int MSG_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT = 1;
436375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
437375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        public DrawingHandler(final MainKeyboardView outerInstance) {
438375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            super(outerInstance);
439375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
440375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
441375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        @Override
442375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        public void handleMessage(final Message msg) {
443375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            final MainKeyboardView mainKeyboardView = getOuterInstance();
444375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            if (mainKeyboardView == null) return;
445375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            final PointerTracker tracker = (PointerTracker) msg.obj;
446375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            switch (msg.what) {
447375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            case MSG_DISMISS_KEY_PREVIEW:
448375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka                final TextView previewText = mainKeyboardView.mKeyPreviewTexts.get(
449375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka                        tracker.mPointerId);
450375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka                if (previewText != null) {
451375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka                    previewText.setVisibility(INVISIBLE);
452375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka                }
453375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka                break;
454375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            case MSG_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT:
455fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka                mainKeyboardView.showGestureFloatingPreviewText(SuggestedWords.EMPTY);
456375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka                break;
457375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            }
458375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
459375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
460375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        public void dismissKeyPreview(final long delay, final PointerTracker tracker) {
461375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            sendMessageDelayed(obtainMessage(MSG_DISMISS_KEY_PREVIEW, tracker), delay);
462375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
463375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
464375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        public void cancelDismissKeyPreview(final PointerTracker tracker) {
465375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            removeMessages(MSG_DISMISS_KEY_PREVIEW, tracker);
466375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
467375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
468375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        private void cancelAllDismissKeyPreviews() {
469375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            removeMessages(MSG_DISMISS_KEY_PREVIEW);
470375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
471375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
472375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        public void dismissGestureFloatingPreviewText(final long delay) {
473375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            sendMessageDelayed(obtainMessage(MSG_DISMISS_GESTURE_FLOATING_PREVIEW_TEXT), delay);
474375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
475375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
476375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        public void cancelAllMessages() {
477375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            cancelAllDismissKeyPreviews();
478375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
479375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    }
480375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
481dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public MainKeyboardView(final Context context, final AttributeSet attrs) {
482c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka        this(context, attrs, R.attr.mainKeyboardViewStyle);
4835f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
4845f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
485dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public MainKeyboardView(final Context context, final AttributeSet attrs, final int defStyle) {
4865f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        super(context, attrs, defStyle);
4875f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
488081fedc42269845a31e962b2ac1aa017b3a2efeeTadashi G. Takaoka        mTouchScreenRegulator = new TouchScreenRegulator(context, this);
489c403a46f6d787b79768895272d53d296100677ddTadashi G. Takaoka
49013ae76d7a342581160c172cd21706b3d57d32dadTadashi G. Takaoka        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
49113ae76d7a342581160c172cd21706b3d57d32dadTadashi G. Takaoka        final boolean forceNonDistinctMultitouch = prefs.getBoolean(
492a5ff9f0c77005769f92ca1131882bb4e3ca18980Tadashi G. Takaoka                DebugSettings.PREF_FORCE_NON_DISTINCT_MULTITOUCH, false);
49313ae76d7a342581160c172cd21706b3d57d32dadTadashi G. Takaoka        final boolean hasDistinctMultitouch = context.getPackageManager()
4945f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                .hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT);
49513ae76d7a342581160c172cd21706b3d57d32dadTadashi G. Takaoka        mHasDistinctMultitouch = hasDistinctMultitouch && !forceNonDistinctMultitouch;
496918e420d1becc1389b9895538eceff85fe882c99Tadashi G. Takaoka        final Resources res = getResources();
497d438fcaca2a35ace4fee5b7a469596bfe2d1b025Tadashi G. Takaoka        final boolean needsPhantomSuddenMoveEventHack = Boolean.parseBoolean(
498fa7f08b37327c825baf0a6455d6f7fdf8b37da57Tadashi G. Takaoka                ResourceUtils.getDeviceOverrideValue(
499fa7f08b37327c825baf0a6455d6f7fdf8b37da57Tadashi G. Takaoka                        res, R.array.phantom_sudden_move_event_device_list));
50093b5c2ce63705e7ebffd9bdb7358100e8d5b5235Tadashi G. Takaoka        PointerTracker.init(needsPhantomSuddenMoveEventHack);
501fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mPreviewPlacerView = new PreviewPlacerView(context, attrs);
50222b48de11ce6f31a0edf90e1308073e67a7a2adbTadashi G. Takaoka
503d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        final TypedArray mainKeyboardViewAttr = context.obtainStyledAttributes(
504c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka                attrs, R.styleable.MainKeyboardView, defStyle, R.style.MainKeyboardView);
505f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka        final int backgroundDimAlpha = mainKeyboardViewAttr.getInt(
506f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka                R.styleable.MainKeyboardView_backgroundDimAlpha, 0);
507f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka        mBackgroundDimAlphaPaint.setColor(Color.BLACK);
508f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka        mBackgroundDimAlphaPaint.setAlpha(backgroundDimAlpha);
509d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        mAutoCorrectionSpacebarLedEnabled = mainKeyboardViewAttr.getBoolean(
510c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka                R.styleable.MainKeyboardView_autoCorrectionSpacebarLedEnabled, false);
511d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        mAutoCorrectionSpacebarLedIcon = mainKeyboardViewAttr.getDrawable(
512c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka                R.styleable.MainKeyboardView_autoCorrectionSpacebarLedIcon);
513d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        mSpacebarTextRatio = mainKeyboardViewAttr.getFraction(
514ef2bfad5903fb55adca61dbea51984fbc7e4375fTadashi G. Takaoka                R.styleable.MainKeyboardView_spacebarTextRatio, 1, 1, 1.0f);
515d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        mSpacebarTextColor = mainKeyboardViewAttr.getColor(
516d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka                R.styleable.MainKeyboardView_spacebarTextColor, 0);
517d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        mSpacebarTextShadowColor = mainKeyboardViewAttr.getColor(
518c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka                R.styleable.MainKeyboardView_spacebarTextShadowColor, 0);
519d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        mLanguageOnSpacebarFinalAlpha = mainKeyboardViewAttr.getInt(
520c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka                R.styleable.MainKeyboardView_languageOnSpacebarFinalAlpha,
5214daf32b6c0358f0273a99b622a259ecdf6b44fa4Tom Ouyang                Constants.Color.ALPHA_OPAQUE);
522d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        final int languageOnSpacebarFadeoutAnimatorResId = mainKeyboardViewAttr.getResourceId(
523c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka                R.styleable.MainKeyboardView_languageOnSpacebarFadeoutAnimator, 0);
524d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        final int altCodeKeyWhileTypingFadeoutAnimatorResId = mainKeyboardViewAttr.getResourceId(
525c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka                R.styleable.MainKeyboardView_altCodeKeyWhileTypingFadeoutAnimator, 0);
526d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        final int altCodeKeyWhileTypingFadeinAnimatorResId = mainKeyboardViewAttr.getResourceId(
527c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka                R.styleable.MainKeyboardView_altCodeKeyWhileTypingFadeinAnimator, 0);
528160f01211d169d64102205e80e9ac8d46c7d674bTadashi G. Takaoka
529d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        final float keyHysteresisDistance = mainKeyboardViewAttr.getDimension(
530c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka                R.styleable.MainKeyboardView_keyHysteresisDistance, 0);
531d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        final float keyHysteresisDistanceForSlidingModifier = mainKeyboardViewAttr.getDimension(
532f731eb1760a5693492a34bc11aa755053aa65c19Tadashi G. Takaoka                R.styleable.MainKeyboardView_keyHysteresisDistanceForSlidingModifier, 0);
533f731eb1760a5693492a34bc11aa755053aa65c19Tadashi G. Takaoka        mKeyDetector = new KeyDetector(
534f731eb1760a5693492a34bc11aa755053aa65c19Tadashi G. Takaoka                keyHysteresisDistance, keyHysteresisDistanceForSlidingModifier);
535d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        mKeyTimerHandler = new KeyTimerHandler(this, mainKeyboardViewAttr);
5360e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        mKeyPreviewOffset = mainKeyboardViewAttr.getDimensionPixelOffset(
5370e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka                R.styleable.MainKeyboardView_keyPreviewOffset, 0);
5380e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        mKeyPreviewHeight = mainKeyboardViewAttr.getDimensionPixelSize(
5390e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka                R.styleable.MainKeyboardView_keyPreviewHeight, 0);
5400e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        mKeyPreviewLingerTimeout = mainKeyboardViewAttr.getInt(
5410e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka                R.styleable.MainKeyboardView_keyPreviewLingerTimeout, 0);
542375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        mKeyPreviewLayoutId = mainKeyboardViewAttr.getResourceId(
543375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka                R.styleable.MainKeyboardView_keyPreviewLayout, 0);
5440e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        if (mKeyPreviewLayoutId == 0) {
5450e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            mShowKeyPreviewPopup = false;
5460e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        }
547a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        final int moreKeysKeyboardLayoutId = mainKeyboardViewAttr.getResourceId(
548d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka                R.styleable.MainKeyboardView_moreKeysKeyboardLayout, 0);
549d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        mConfigShowMoreKeysKeyboardAtTouchedPoint = mainKeyboardViewAttr.getBoolean(
550c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka                R.styleable.MainKeyboardView_showMoreKeysKeyboardAtTouchedPoint, false);
551375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
552375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        mGestureFloatingPreviewTextLingerTimeout = mainKeyboardViewAttr.getInt(
553375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka                R.styleable.MainKeyboardView_gestureFloatingPreviewTextLingerTimeout, 0);
554d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        PointerTracker.setParameters(mainKeyboardViewAttr);
555fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka
556fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mGestureFloatingPreviewText = new GestureFloatingPreviewText(
557fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka                mPreviewPlacerView, mainKeyboardViewAttr);
558fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mPreviewPlacerView.addPreview(mGestureFloatingPreviewText);
559fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka
560fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mGestureTrailsPreview = new GestureTrailsPreview(
561fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka                mPreviewPlacerView, mainKeyboardViewAttr);
562fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mPreviewPlacerView.addPreview(mGestureTrailsPreview);
563fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka
564fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mSlidingKeyInputPreview = new SlidingKeyInputPreview(
565fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka                mPreviewPlacerView, mainKeyboardViewAttr);
566fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mPreviewPlacerView.addPreview(mSlidingKeyInputPreview);
567d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        mainKeyboardViewAttr.recycle();
568160f01211d169d64102205e80e9ac8d46c7d674bTadashi G. Takaoka
569a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        mMoreKeysKeyboardContainer = LayoutInflater.from(getContext())
570a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka                .inflate(moreKeysKeyboardLayoutId, null);
57131c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka        mLanguageOnSpacebarFadeoutAnimator = loadObjectAnimator(
57231c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka                languageOnSpacebarFadeoutAnimatorResId, this);
57331c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka        mAltCodeKeyWhileTypingFadeoutAnimator = loadObjectAnimator(
57431c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka                altCodeKeyWhileTypingFadeoutAnimatorResId, this);
57531c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka        mAltCodeKeyWhileTypingFadeinAnimator = loadObjectAnimator(
57631c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka                altCodeKeyWhileTypingFadeinAnimatorResId, this);
57731c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka    }
57831c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka
579dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    private ObjectAnimator loadObjectAnimator(final int resId, final Object target) {
58020dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        if (resId == 0) {
58120dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            return null;
58220dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        }
58331c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka        final ObjectAnimator animator = (ObjectAnimator)AnimatorInflater.loadAnimator(
58431c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka                getContext(), resId);
585d7c4ba170982ddce5ac12ea92c3c3d8b53d524baTadashi G. Takaoka        if (animator != null) {
58631c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka            animator.setTarget(target);
58773a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka        }
58831c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka        return animator;
58931c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka    }
59073a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka
59115f6d4ae34664ea3d92827a2c3003198c0bac70bTadashi G. Takaoka    @ExternallyReferenced
59231c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka    public int getLanguageOnSpacebarAnimAlpha() {
59331c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka        return mLanguageOnSpacebarAnimAlpha;
594d7c4ba170982ddce5ac12ea92c3c3d8b53d524baTadashi G. Takaoka    }
595d7c4ba170982ddce5ac12ea92c3c3d8b53d524baTadashi G. Takaoka
59615f6d4ae34664ea3d92827a2c3003198c0bac70bTadashi G. Takaoka    @ExternallyReferenced
597dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public void setLanguageOnSpacebarAnimAlpha(final int alpha) {
59831c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka        mLanguageOnSpacebarAnimAlpha = alpha;
59931c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka        invalidateKey(mSpaceKey);
60031c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka    }
60131c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka
60215f6d4ae34664ea3d92827a2c3003198c0bac70bTadashi G. Takaoka    @ExternallyReferenced
60331c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka    public int getAltCodeKeyWhileTypingAnimAlpha() {
60431c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka        return mAltCodeKeyWhileTypingAnimAlpha;
60531c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka    }
60631c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka
60715f6d4ae34664ea3d92827a2c3003198c0bac70bTadashi G. Takaoka    @ExternallyReferenced
608dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public void setAltCodeKeyWhileTypingAnimAlpha(final int alpha) {
60931c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka        mAltCodeKeyWhileTypingAnimAlpha = alpha;
61031c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka        updateAltCodeKeyWhileTyping();
6115f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
6125f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
613dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public void setKeyboardActionListener(final KeyboardActionListener listener) {
6145f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        mKeyboardActionListener = listener;
6155c73ed628b22fdfa59585803ee86e383c579a7d4Tadashi G. Takaoka        PointerTracker.setKeyboardActionListener(listener);
6165f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
6175f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
6185f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    /**
6195f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     * Returns the {@link KeyboardActionListener} object.
6205f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     * @return the listener attached to this keyboard
6215f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     */
6220efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka    @Override
6230efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka    public KeyboardActionListener getKeyboardActionListener() {
6245f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        return mKeyboardActionListener;
6255f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
6265f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
627bb4be5444b845655c0eb80bcfbb66f93603802eaTadashi G. Takaoka    @Override
6280efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka    public KeyDetector getKeyDetector() {
6290efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka        return mKeyDetector;
6300efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka    }
6310efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka
6320efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka    @Override
633f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka    public DrawingProxy getDrawingProxy() {
634f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka        return this;
635f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka    }
636f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka
637f426cdd5c62452224ac4bb833c3ccf7b26d1a2a8Tadashi G. Takaoka    @Override
6380efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka    public TimerProxy getTimerProxy() {
6390efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka        return mKeyTimerHandler;
6400efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka    }
6410efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka
6425f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    /**
6435f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     * Attaches a keyboard to this view. The keyboard can be switched at any time and the
6445f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     * view will re-layout itself to accommodate the keyboard.
6455f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     * @see Keyboard
6465f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     * @see #getKeyboard()
6475f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     * @param keyboard the keyboard to display in this view
6485f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     */
6495f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    @Override
650dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public void setKeyboard(final Keyboard keyboard) {
6518a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka        // Remove any pending messages, except dismissing preview and key repeat.
6528a092b4ede02b79422deae51f0a416b034777fb3Tadashi G. Takaoka        mKeyTimerHandler.cancelLongPressTimer();
6535f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        super.setKeyboard(keyboard);
6545a7a696aff6718d4e0250c394a9d01cbf2a16916Tadashi G. Takaoka        mKeyDetector.setKeyboard(
6555a7a696aff6718d4e0250c394a9d01cbf2a16916Tadashi G. Takaoka                keyboard, -getPaddingLeft(), -getPaddingTop() + mVerticalCorrection);
6568335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka        PointerTracker.setKeyDetector(mKeyDetector);
657081fedc42269845a31e962b2ac1aa017b3a2efeeTadashi G. Takaoka        mTouchScreenRegulator.setKeyboardGeometry(keyboard.mOccupiedWidth);
658a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        mMoreKeysKeyboardCache.clear();
6594112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka
660240871ecafde7834ebb4270cd7758fc904a5f3a7Tadashi G. Takaoka        mSpaceKey = keyboard.getKey(Constants.CODE_SPACE);
6611f2d0aa6c9b343848ee51e5bc13ccaaadf3ba4feTadashi G. Takaoka        mSpaceIcon = (mSpaceKey != null)
6624daf32b6c0358f0273a99b622a259ecdf6b44fa4Tom Ouyang                ? mSpaceKey.getIcon(keyboard.mIconsSet, Constants.Color.ALPHA_OPAQUE) : null;
6634112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
6644112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        mSpacebarTextSize = keyHeight * mSpacebarTextRatio;
6659c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa        if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
666c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka            ResearchLogger.mainKeyboardView_setKeyboard(keyboard);
66748a7681e064ae259b840f0e757da2d716043d893Kurt Partridge        }
668f147794fd41491a3383e6aca6d49007f58124068alanv
669f147794fd41491a3383e6aca6d49007f58124068alanv        // This always needs to be set since the accessibility state can
670f147794fd41491a3383e6aca6d49007f58124068alanv        // potentially change without the keyboard being set again.
671b6ca354431367b625daf9fff5fbe4b1f5ef996abKen Wakasa        AccessibleKeyboardViewProxy.getInstance().setKeyboard();
6725f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
6735f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
6740e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    /**
6750e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka     * Enables or disables the key feedback popup. This is a popup that shows a magnified
6760e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka     * version of the depressed key. By default the preview is enabled.
6770e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka     * @param previewEnabled whether or not to enable the key feedback preview
6780e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka     * @param delay the delay after which the preview is dismissed
6790e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka     * @see #isKeyPreviewPopupEnabled()
6800e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka     */
6810e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    public void setKeyPreviewPopupEnabled(final boolean previewEnabled, final int delay) {
6820e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        mShowKeyPreviewPopup = previewEnabled;
6830e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        mKeyPreviewLingerTimeout = delay;
6840e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    }
6850e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
686375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
687375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    private void locatePreviewPlacerView() {
688375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        if (mPreviewPlacerView.getParent() != null) {
689375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            return;
690375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
691375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        final int width = getWidth();
692375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        final int height = getHeight();
693375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        if (width == 0 || height == 0) {
694375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            // In transient state.
695375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            return;
696375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
697375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        getLocationInWindow(mOriginCoords);
698375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        final DisplayMetrics dm = getResources().getDisplayMetrics();
699375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        if (CoordinateUtils.y(mOriginCoords) < dm.heightPixels / 4) {
700375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            // In transient state.
701375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            return;
702375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
703375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        final View rootView = getRootView();
704375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        if (rootView == null) {
705375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            Log.w(TAG, "Cannot find root view");
706375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            return;
707375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
708375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        final ViewGroup windowContentView = (ViewGroup)rootView.findViewById(android.R.id.content);
709375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        // Note: It'd be very weird if we get null by android.R.id.content.
710375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        if (windowContentView == null) {
711375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            Log.w(TAG, "Cannot find android.R.id.content view to add PreviewPlacerView");
712375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        } else {
713375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            windowContentView.addView(mPreviewPlacerView);
714375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            mPreviewPlacerView.setKeyboardViewGeometry(mOriginCoords, width, height);
715375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
716375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    }
717375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
7180e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    /**
7190e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka     * Returns the enabled state of the key feedback preview
7200e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka     * @return whether or not the key feedback preview is enabled
7210e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka     * @see #setKeyPreviewPopupEnabled(boolean, int)
7220e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka     */
7230e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    public boolean isKeyPreviewPopupEnabled() {
7240e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        return mShowKeyPreviewPopup;
7250e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    }
7260e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
727375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    private void addKeyPreview(final TextView keyPreview) {
728375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        locatePreviewPlacerView();
729375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        mPreviewPlacerView.addView(
730375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka                keyPreview, ViewLayoutUtils.newLayoutParam(mPreviewPlacerView, 0, 0));
731375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    }
732375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
733375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    private TextView getKeyPreviewText(final int pointerId) {
734375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        TextView previewText = mKeyPreviewTexts.get(pointerId);
735375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        if (previewText != null) {
736375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            return previewText;
737375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
738375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        final Context context = getContext();
739375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        if (mKeyPreviewLayoutId != 0) {
740375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            previewText = (TextView)LayoutInflater.from(context).inflate(mKeyPreviewLayoutId, null);
741375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        } else {
742375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            previewText = new TextView(context);
743375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
744375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        mKeyPreviewTexts.put(pointerId, previewText);
745375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        return previewText;
746375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    }
747375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
748375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    private void dismissAllKeyPreviews() {
749375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        final int pointerCount = mKeyPreviewTexts.size();
750375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        for (int id = 0; id < pointerCount; id++) {
751375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            final TextView previewText = mKeyPreviewTexts.get(id);
752375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            if (previewText != null) {
753375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka                previewText.setVisibility(INVISIBLE);
754375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            }
755375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        }
756375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        PointerTracker.setReleasedKeyGraphicsToAllKeys();
757375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    }
758375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
7590e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    // Background state set
7600e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private static final int[][][] KEY_PREVIEW_BACKGROUND_STATE_TABLE = {
7610e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        { // STATE_MIDDLE
7620e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            EMPTY_STATE_SET,
7630e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            { R.attr.state_has_morekeys }
7640e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        },
7650e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        { // STATE_LEFT
7660e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            { R.attr.state_left_edge },
7670e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            { R.attr.state_left_edge, R.attr.state_has_morekeys }
7680e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        },
7690e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        { // STATE_RIGHT
7700e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            { R.attr.state_right_edge },
7710e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            { R.attr.state_right_edge, R.attr.state_has_morekeys }
7720e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        }
7730e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    };
7740e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private static final int STATE_MIDDLE = 0;
7750e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private static final int STATE_LEFT = 1;
7760e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private static final int STATE_RIGHT = 2;
7770e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private static final int STATE_NORMAL = 0;
7780e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private static final int STATE_HAS_MOREKEYS = 1;
7790e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    private static final int[] KEY_PREVIEW_BACKGROUND_DEFAULT_STATE =
7800e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            KEY_PREVIEW_BACKGROUND_STATE_TABLE[STATE_MIDDLE][STATE_NORMAL];
7810e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
7820e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    @Override
7830e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    public void showKeyPreview(final PointerTracker tracker) {
7840e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        final KeyPreviewDrawParams previewParams = mKeyPreviewDrawParams;
7850e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        final Keyboard keyboard = getKeyboard();
7860e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        if (!mShowKeyPreviewPopup) {
7870e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            previewParams.mPreviewVisibleOffset = -keyboard.mVerticalGap;
7880e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            return;
7890e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        }
7900e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
7910e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        final TextView previewText = getKeyPreviewText(tracker.mPointerId);
7920e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // If the key preview has no parent view yet, add it to the ViewGroup which can place
7930e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // key preview absolutely in SoftInputWindow.
7940e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        if (previewText.getParent() == null) {
7950e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            addKeyPreview(previewText);
7960e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        }
7970e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
7980e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        mDrawingHandler.cancelDismissKeyPreview(tracker);
7990e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        final Key key = tracker.getKey();
8000e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // If key is invalid or IME is already closed, we must not show key preview.
8010e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // Trying to show key preview while root window is closed causes
8020e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // WindowManager.BadTokenException.
8030e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        if (key == null) {
8040e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            return;
8050e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        }
8060e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
8070e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        final KeyDrawParams drawParams = mKeyDrawParams;
8080e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        previewText.setTextColor(drawParams.mPreviewTextColor);
8090e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        final Drawable background = previewText.getBackground();
8100e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        if (background != null) {
8110e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            background.setState(KEY_PREVIEW_BACKGROUND_DEFAULT_STATE);
8120e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            background.setAlpha(PREVIEW_ALPHA);
8130e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        }
814914d054ea26d18a1ba6a60f8c5e749136477bbf2Tadashi G. Takaoka        final String label = key.getPreviewLabel();
8150e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // What we show as preview should match what we show on a key top in onDraw().
8160e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        if (label != null) {
8170e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            // TODO Should take care of temporaryShiftLabel here.
8180e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            previewText.setCompoundDrawables(null, null, null, null);
819914d054ea26d18a1ba6a60f8c5e749136477bbf2Tadashi G. Takaoka            previewText.setTextSize(TypedValue.COMPLEX_UNIT_PX,
820914d054ea26d18a1ba6a60f8c5e749136477bbf2Tadashi G. Takaoka                    key.selectPreviewTextSize(drawParams));
821914d054ea26d18a1ba6a60f8c5e749136477bbf2Tadashi G. Takaoka            previewText.setTypeface(key.selectPreviewTypeface(drawParams));
8220e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            previewText.setText(label);
8230e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        } else {
8240e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            previewText.setCompoundDrawables(null, null, null,
8250e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka                    key.getPreviewIcon(keyboard.mIconsSet));
8260e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            previewText.setText(null);
8270e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        }
8280e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
8290e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        previewText.measure(
8300e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
8310e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        final int keyDrawWidth = key.getDrawWidth();
8320e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        final int previewWidth = previewText.getMeasuredWidth();
8330e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        final int previewHeight = mKeyPreviewHeight;
8340e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // The width and height of visible part of the key preview background. The content marker
8350e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // of the background 9-patch have to cover the visible part of the background.
8360e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        previewParams.mPreviewVisibleWidth = previewWidth - previewText.getPaddingLeft()
8370e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka                - previewText.getPaddingRight();
8380e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        previewParams.mPreviewVisibleHeight = previewHeight - previewText.getPaddingTop()
8390e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka                - previewText.getPaddingBottom();
8400e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // The distance between the top edge of the parent key and the bottom of the visible part
8410e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // of the key preview background.
8420e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        previewParams.mPreviewVisibleOffset = mKeyPreviewOffset - previewText.getPaddingBottom();
8430e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        getLocationInWindow(mOriginCoords);
8440e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // The key preview is horizontally aligned with the center of the visible part of the
8450e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // parent key. If it doesn't fit in this {@link KeyboardView}, it is moved inward to fit and
8460e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // the left/right background is used if such background is specified.
8470e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        final int statePosition;
8480e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        int previewX = key.getDrawX() - (previewWidth - keyDrawWidth) / 2
8490e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka                + CoordinateUtils.x(mOriginCoords);
8500e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        if (previewX < 0) {
8510e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            previewX = 0;
8520e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            statePosition = STATE_LEFT;
8530e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        } else if (previewX > getWidth() - previewWidth) {
8540e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            previewX = getWidth() - previewWidth;
8550e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            statePosition = STATE_RIGHT;
8560e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        } else {
8570e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            statePosition = STATE_MIDDLE;
8580e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        }
8590e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // The key preview is placed vertically above the top edge of the parent key with an
8600e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        // arbitrary offset.
8610e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        final int previewY = key.mY - previewHeight + mKeyPreviewOffset
8620e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka                + CoordinateUtils.y(mOriginCoords);
8630e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
8640e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        if (background != null) {
8650e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            final int hasMoreKeys = (key.mMoreKeys != null) ? STATE_HAS_MOREKEYS : STATE_NORMAL;
8660e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka            background.setState(KEY_PREVIEW_BACKGROUND_STATE_TABLE[statePosition][hasMoreKeys]);
8670e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        }
8680e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        ViewLayoutUtils.placeViewAt(
8690e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka                previewText, previewX, previewY, previewWidth, previewHeight);
8700e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        previewText.setVisibility(VISIBLE);
8710e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    }
8720e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
8730e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    @Override
8740e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    public void dismissKeyPreview(final PointerTracker tracker) {
8750e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        mDrawingHandler.dismissKeyPreview(mKeyPreviewLingerTimeout, tracker);
8760e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    }
8770e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
878ff961ddf8c58df569c97684bfd83a01b2a9470aaTadashi G. Takaoka    public void setSlidingKeyInputPreviewEnabled(final boolean enabled) {
879ff961ddf8c58df569c97684bfd83a01b2a9470aaTadashi G. Takaoka        mSlidingKeyInputPreview.setPreviewEnabled(enabled);
880ff961ddf8c58df569c97684bfd83a01b2a9470aaTadashi G. Takaoka    }
881ff961ddf8c58df569c97684bfd83a01b2a9470aaTadashi G. Takaoka
8820e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    @Override
8830e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    public void showSlidingKeyInputPreview(final PointerTracker tracker) {
8840e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka        locatePreviewPlacerView();
885fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mSlidingKeyInputPreview.setPreviewPosition(tracker);
8860e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    }
8870e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
8880e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    @Override
8890e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    public void dismissSlidingKeyInputPreview() {
890fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mSlidingKeyInputPreview.dismissSlidingKeyInputPreview();
8910e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka    }
8920e4f0da449d0fc4f0c4c6b49b4c24961da36a5d1Tadashi G. Takaoka
893375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    public void setGesturePreviewMode(final boolean drawsGesturePreviewTrail,
894375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka            final boolean drawsGestureFloatingPreviewText) {
895fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mGestureFloatingPreviewText.setPreviewEnabled(drawsGestureFloatingPreviewText);
896fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mGestureTrailsPreview.setPreviewEnabled(drawsGesturePreviewTrail);
897375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    }
898375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
899375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    public void showGestureFloatingPreviewText(final SuggestedWords suggestedWords) {
900375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        locatePreviewPlacerView();
901fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mGestureFloatingPreviewText.setSuggetedWords(suggestedWords);
902375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    }
903375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
904375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    public void dismissGestureFloatingPreviewText() {
905375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        locatePreviewPlacerView();
906375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        mDrawingHandler.dismissGestureFloatingPreviewText(mGestureFloatingPreviewTextLingerTimeout);
907375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    }
908375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
909fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka    @Override
910ab334eb64669e909f0a401fddffa891962002602Tadashi G. Takaoka    public void showGesturePreviewTrail(final PointerTracker tracker) {
911375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        locatePreviewPlacerView();
912fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mGestureFloatingPreviewText.setPreviewPosition(tracker);
913fb523d7f5d40123a49a9128c455815db320c90fcTadashi G. Takaoka        mGestureTrailsPreview.setPreviewPosition(tracker);
914375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    }
915375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
9168335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka    // Note that this method is called from a non-UI thread.
917dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public void setMainDictionaryAvailability(final boolean mainDictionaryAvailable) {
9188335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka        PointerTracker.setMainDictionaryAvailability(mainDictionaryAvailable);
9198335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka    }
9208335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka
921dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public void setGestureHandlingEnabledByUser(final boolean gestureHandlingEnabledByUser) {
9228335c59ea7715f3dbc6625f128a7a038f314a89fTadashi G. Takaoka        PointerTracker.setGestureHandlingEnabledByUser(gestureHandlingEnabledByUser);
9230657b9698a110f8e895448d829478982ce37b6d1Tadashi G. Takaoka    }
9240657b9698a110f8e895448d829478982ce37b6d1Tadashi G. Takaoka
9255f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    @Override
9264331012a9e7779ff7c8359a443dc5815ee6ea8d9Kurt Partridge    protected void onAttachedToWindow() {
9274331012a9e7779ff7c8359a443dc5815ee6ea8d9Kurt Partridge        super.onAttachedToWindow();
9289c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa        // Notify the ResearchLogger (development only diagnostics) that the keyboard view has
9299c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa        // been attached.  This is needed to properly show the splash screen, which requires that
9309c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa        // the window token of the KeyboardView be non-null.
9319c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa        if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
932c3f78c9057a5710898feaf8027659484477e5821Tadashi G. Takaoka            ResearchLogger.getInstance().mainKeyboardView_onAttachedToWindow(this);
933c3f78c9057a5710898feaf8027659484477e5821Tadashi G. Takaoka        }
934c3f78c9057a5710898feaf8027659484477e5821Tadashi G. Takaoka    }
935c3f78c9057a5710898feaf8027659484477e5821Tadashi G. Takaoka
936c3f78c9057a5710898feaf8027659484477e5821Tadashi G. Takaoka    @Override
937c3f78c9057a5710898feaf8027659484477e5821Tadashi G. Takaoka    protected void onDetachedFromWindow() {
938c3f78c9057a5710898feaf8027659484477e5821Tadashi G. Takaoka        super.onDetachedFromWindow();
939375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        mPreviewPlacerView.removeAllViews();
9409c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa        // Notify the ResearchLogger (development only diagnostics) that the keyboard view has
9419c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa        // been detached.  This is needed to invalidate the reference of {@link MainKeyboardView}
9429c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa        // to null.
9439c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa        if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
944c3f78c9057a5710898feaf8027659484477e5821Tadashi G. Takaoka            ResearchLogger.getInstance().mainKeyboardView_onDetachedFromWindow();
9454331012a9e7779ff7c8359a443dc5815ee6ea8d9Kurt Partridge        }
9464331012a9e7779ff7c8359a443dc5815ee6ea8d9Kurt Partridge    }
9474331012a9e7779ff7c8359a443dc5815ee6ea8d9Kurt Partridge
948a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka    private MoreKeysPanel onCreateMoreKeysPanel(final Key key, final Context context) {
949a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        if (key.mMoreKeys == null) {
9505f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            return null;
95120dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        }
952a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        Keyboard moreKeysKeyboard = mMoreKeysKeyboardCache.get(key);
953a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        if (moreKeysKeyboard == null) {
954a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka            moreKeysKeyboard = new MoreKeysKeyboard.Builder(
955a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka                    context, key, this, mKeyPreviewDrawParams).build();
956a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka            mMoreKeysKeyboardCache.put(key, moreKeysKeyboard);
95720dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        }
9585f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
959a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        final View container = mMoreKeysKeyboardContainer;
9602affaf91a04d63e0994102299816014a8bbe11e1Tadashi G. Takaoka        final MoreKeysKeyboardView moreKeysKeyboardView =
9612affaf91a04d63e0994102299816014a8bbe11e1Tadashi G. Takaoka                (MoreKeysKeyboardView)container.findViewById(R.id.more_keys_keyboard_view);
9622affaf91a04d63e0994102299816014a8bbe11e1Tadashi G. Takaoka        moreKeysKeyboardView.setKeyboard(moreKeysKeyboard);
963b8dc67466339dc14653ad634c86851025373326bTadashi G. Takaoka        container.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
9642affaf91a04d63e0994102299816014a8bbe11e1Tadashi G. Takaoka        return moreKeysKeyboardView;
9655f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
9665f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
9675f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    /**
968d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka     * Called when a key is long pressed.
9695f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     * @param tracker the pointer tracker which pressed the parent key
9705f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     * @return true if the long press is handled, false otherwise. Subclasses should call the
9715f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     * method on the base class if the subclass doesn't wish to handle the call.
9725f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka     */
973a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka    private boolean onLongPress(final PointerTracker tracker) {
974a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        if (isShowingMoreKeysPanel()) {
975a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka            return false;
976a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        }
977a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        final Key key = tracker.getKey();
978a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        if (key == null) {
979a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka            return false;
980a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        }
9819c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa        if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
982c8e45ddb032554f4e9d4411d8ef47d98db62d77bTadashi G. Takaoka            ResearchLogger.mainKeyboardView_onLongPress();
9839bc29d78a6ce83f77869aa63748176241e29d43cKurt Partridge        }
984a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        final int code = key.mCode;
985a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        if (key.hasEmbeddedMoreKey()) {
986a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka            final int embeddedCode = key.mMoreKeys[0].mCode;
9873708787fe91227083d2a1874fa41493d3bc9fe10Tadashi G. Takaoka            tracker.onLongPressed();
988e491be6e8690ffb8359887838fa12d5873346be5Tadashi G. Takaoka            invokeCodeInput(embeddedCode);
989a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka            invokeReleaseKey(code);
990a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka            KeyboardSwitcher.getInstance().hapticAndAudioFeedback(code);
9913708787fe91227083d2a1874fa41493d3bc9fe10Tadashi G. Takaoka            return true;
9923708787fe91227083d2a1874fa41493d3bc9fe10Tadashi G. Takaoka        }
993a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        if (code == Constants.CODE_SPACE || code == Constants.CODE_LANGUAGE_SWITCH) {
9943708787fe91227083d2a1874fa41493d3bc9fe10Tadashi G. Takaoka            // Long pressing the space key invokes IME switcher dialog.
9953708787fe91227083d2a1874fa41493d3bc9fe10Tadashi G. Takaoka            if (invokeCustomRequest(LatinIME.CODE_SHOW_INPUT_METHOD_PICKER)) {
9966dde878d515f7bf5268d16a8fe4921d8821c5ae7Tadashi G. Takaoka                tracker.onLongPressed();
997a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka                invokeReleaseKey(code);
99842e8c64a042476f555da5015558d51f96aaeb7fdTadashi G. Takaoka                return true;
9996dde878d515f7bf5268d16a8fe4921d8821c5ae7Tadashi G. Takaoka            }
10006dde878d515f7bf5268d16a8fe4921d8821c5ae7Tadashi G. Takaoka        }
1001a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        return openMoreKeysPanel(key, tracker);
10026dde878d515f7bf5268d16a8fe4921d8821c5ae7Tadashi G. Takaoka    }
10036dde878d515f7bf5268d16a8fe4921d8821c5ae7Tadashi G. Takaoka
1004a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka    private boolean invokeCustomRequest(final int requestCode) {
1005a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        return mKeyboardActionListener.onCustomRequest(requestCode);
100642e8c64a042476f555da5015558d51f96aaeb7fdTadashi G. Takaoka    }
100742e8c64a042476f555da5015558d51f96aaeb7fdTadashi G. Takaoka
1008a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka    private void invokeCodeInput(final int code) {
1009ac78633be28e8990fc3b3a8de192c80966e746e3Tadashi G. Takaoka        mKeyboardActionListener.onCodeInput(
1010a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka                code, Constants.NOT_A_COORDINATE, Constants.NOT_A_COORDINATE);
101142e8c64a042476f555da5015558d51f96aaeb7fdTadashi G. Takaoka    }
101242e8c64a042476f555da5015558d51f96aaeb7fdTadashi G. Takaoka
1013a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka    private void invokeReleaseKey(final int code) {
1014a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        mKeyboardActionListener.onReleaseKey(code, false);
10156dde878d515f7bf5268d16a8fe4921d8821c5ae7Tadashi G. Takaoka    }
10166dde878d515f7bf5268d16a8fe4921d8821c5ae7Tadashi G. Takaoka
1017a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka    private boolean openMoreKeysPanel(final Key key, final PointerTracker tracker) {
1018a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        final MoreKeysPanel moreKeysPanel = onCreateMoreKeysPanel(key, getContext());
10199d5601e9013c5ec9a7ac75db16f4a0a8218b02bfTadashi G. Takaoka        if (moreKeysPanel == null) {
1020a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka            return false;
10215f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
102263c233ab9f50d844be6e52e382c6664475606760Tadashi G. Takaoka
1023547b638194c05f971003edb06c3c6c489a76da5fTadashi G. Takaoka        final int[] lastCoords = CoordinateUtils.newInstance();
1024547b638194c05f971003edb06c3c6c489a76da5fTadashi G. Takaoka        tracker.getLastCoordinates(lastCoords);
1025a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        final boolean keyPreviewEnabled = isKeyPreviewPopupEnabled() && !key.noKeyPreview();
10267ecc1081ab9b4e41e4b2aec7877aaaf8df29e611Tadashi G. Takaoka        // The more keys keyboard is usually horizontally aligned with the center of the parent key.
10277ecc1081ab9b4e41e4b2aec7877aaaf8df29e611Tadashi G. Takaoka        // If showMoreKeysKeyboardAtTouchedPoint is true and the key preview is disabled, the more
10287ecc1081ab9b4e41e4b2aec7877aaaf8df29e611Tadashi G. Takaoka        // keys keyboard is placed at the touch point of the parent key.
10297ecc1081ab9b4e41e4b2aec7877aaaf8df29e611Tadashi G. Takaoka        final int pointX = (mConfigShowMoreKeysKeyboardAtTouchedPoint && !keyPreviewEnabled)
1030547b638194c05f971003edb06c3c6c489a76da5fTadashi G. Takaoka                ? CoordinateUtils.x(lastCoords)
1031a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka                : key.mX + key.mWidth / 2;
10327ecc1081ab9b4e41e4b2aec7877aaaf8df29e611Tadashi G. Takaoka        // The more keys keyboard is usually vertically aligned with the top edge of the parent key
10337ecc1081ab9b4e41e4b2aec7877aaaf8df29e611Tadashi G. Takaoka        // (plus vertical gap). If the key preview is enabled, the more keys keyboard is vertically
10347ecc1081ab9b4e41e4b2aec7877aaaf8df29e611Tadashi G. Takaoka        // aligned with the bottom edge of the visible part of the key preview.
103529d5973fd35438a83acf7f44b5d55d5620278ee3Tadashi G. Takaoka        // {@code mPreviewVisibleOffset} has been set appropriately in
103629d5973fd35438a83acf7f44b5d55d5620278ee3Tadashi G. Takaoka        // {@link KeyboardView#showKeyPreview(PointerTracker)}.
1037a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        final int pointY = key.mY + mKeyPreviewDrawParams.mPreviewVisibleOffset;
1038fa2d543785c52f639ad3157c57420f58a199c550Tom Ouyang        moreKeysPanel.showMoreKeysPanel(this, this, pointX, pointY, mKeyboardActionListener);
1039547b638194c05f971003edb06c3c6c489a76da5fTadashi G. Takaoka        final int translatedX = moreKeysPanel.translateX(CoordinateUtils.x(lastCoords));
1040547b638194c05f971003edb06c3c6c489a76da5fTadashi G. Takaoka        final int translatedY = moreKeysPanel.translateY(CoordinateUtils.y(lastCoords));
1041e51d164482c7896892d6eccb80f1e1e6fe6d50dbTadashi G. Takaoka        tracker.onShowMoreKeysPanel(translatedX, translatedY, moreKeysPanel);
10425f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        return true;
10435f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
10445f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
10455f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    public boolean isInSlidingKeyInput() {
1046a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        if (isShowingMoreKeysPanel()) {
104763c233ab9f50d844be6e52e382c6664475606760Tadashi G. Takaoka            return true;
10485f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
104920dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        return PointerTracker.isAnyInSlidingKeyInput();
10505f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
10515f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
1052d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    @Override
1053d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    public void onShowMoreKeysPanel(final MoreKeysPanel panel) {
1054d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        if (isShowingMoreKeysPanel()) {
1055d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka            onDismissMoreKeysPanel();
1056d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        }
1057a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        mPreviewPlacerView.addView(panel.getContainerView());
1058d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        mMoreKeysPanel = panel;
1059a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        dimEntireKeyboard(true /* dimmed */);
1060d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    }
1061d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka
1062d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    public boolean isShowingMoreKeysPanel() {
1063a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        return mMoreKeysPanel != null && mMoreKeysPanel.isShowingInParent();
1064d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    }
1065d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka
1066d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    @Override
1067d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    public void onCancelMoreKeysPanel() {
1068d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        PointerTracker.dismissAllMoreKeysPanels();
1069d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    }
1070d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka
1071d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    @Override
1072d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    public boolean onDismissMoreKeysPanel() {
1073d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        dimEntireKeyboard(false /* dimmed */);
1074d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        if (isShowingMoreKeysPanel()) {
1075d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka            mPreviewPlacerView.removeView(mMoreKeysPanel.getContainerView());
1076d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka            mMoreKeysPanel = null;
1077d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka            return true;
1078d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        }
1079d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka        return false;
1080d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka    }
1081d9449ccf3ce9e5eabb022aed6a45bba680bce115Tadashi G. Takaoka
10825f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    public int getPointerCount() {
10835f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        return mOldPointerCount;
10845f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
10855f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
10865f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    @Override
1087dc2ee7772402633817702e95c2a5b17f6dec03ebalanv    public boolean dispatchTouchEvent(MotionEvent event) {
1088dc2ee7772402633817702e95c2a5b17f6dec03ebalanv        if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) {
1089dc2ee7772402633817702e95c2a5b17f6dec03ebalanv            return AccessibleKeyboardViewProxy.getInstance().dispatchTouchEvent(event);
1090dc2ee7772402633817702e95c2a5b17f6dec03ebalanv        }
1091dc2ee7772402633817702e95c2a5b17f6dec03ebalanv        return super.dispatchTouchEvent(event);
1092dc2ee7772402633817702e95c2a5b17f6dec03ebalanv    }
1093dc2ee7772402633817702e95c2a5b17f6dec03ebalanv
1094dc2ee7772402633817702e95c2a5b17f6dec03ebalanv    @Override
1095dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public boolean onTouchEvent(final MotionEvent me) {
109646286874f30c4a6ef44646c4e4adf36fe55c74b9Tadashi G. Takaoka        if (getKeyboard() == null) {
109746286874f30c4a6ef44646c4e4adf36fe55c74b9Tadashi G. Takaoka            return false;
109846286874f30c4a6ef44646c4e4adf36fe55c74b9Tadashi G. Takaoka        }
1099c403a46f6d787b79768895272d53d296100677ddTadashi G. Takaoka        return mTouchScreenRegulator.onTouchEvent(me);
1100c403a46f6d787b79768895272d53d296100677ddTadashi G. Takaoka    }
1101c403a46f6d787b79768895272d53d296100677ddTadashi G. Takaoka
1102c403a46f6d787b79768895272d53d296100677ddTadashi G. Takaoka    @Override
1103dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public boolean processMotionEvent(final MotionEvent me) {
1104f60d09ac3086f308cafcee13ebcb94c562f9e58eTadashi G. Takaoka        final boolean nonDistinctMultitouch = !mHasDistinctMultitouch;
11055f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        final int action = me.getActionMasked();
11065f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        final int pointerCount = me.getPointerCount();
11075f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        final int oldPointerCount = mOldPointerCount;
11085f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        mOldPointerCount = pointerCount;
11095f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
11105f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        // TODO: cleanup this code into a multi-touch to single-touch event converter class?
11115f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        // If the device does not have distinct multi-touch support panel, ignore all multi-touch
11125f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        // events except a transition from/to single-touch.
1113f60d09ac3086f308cafcee13ebcb94c562f9e58eTadashi G. Takaoka        if (nonDistinctMultitouch && pointerCount > 1 && oldPointerCount > 1) {
11145f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            return true;
11155f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
11165f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
11175f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        final long eventTime = me.getEventTime();
11185f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        final int index = me.getActionIndex();
11195f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        final int id = me.getPointerId(index);
112035580bad6f3da3b204653825bbb6871563e70728Tom Ouyang        final int x = (int)me.getX(index);
112135580bad6f3da3b204653825bbb6871563e70728Tom Ouyang        final int y = (int)me.getY(index);
112235580bad6f3da3b204653825bbb6871563e70728Tom Ouyang
1123fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        // TODO: This might be moved to the tracker.processMotionEvent() call below.
1124fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        if (ENABLE_USABILITY_STUDY_LOG && action != MotionEvent.ACTION_MOVE) {
1125fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            writeUsabilityStudyLog(me, action, eventTime, index, id, x, y);
1126d05afa3f4c59641c8fabed034e457cb25f0c57f0Kurt Partridge        }
1127fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        // TODO: This should be moved to the tracker.processMotionEvent() call below.
1128fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        // Currently the same "move" event is being logged twice.
11299c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa        if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
1130fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            ResearchLogger.mainKeyboardView_processMotionEvent(
1131fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka                    me, action, eventTime, index, id, x, y);
113215d4793911fa305e0a58aced925961e948582979satok        }
11335f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
1134f60d09ac3086f308cafcee13ebcb94c562f9e58eTadashi G. Takaoka        if (mKeyTimerHandler.isInKeyRepeat()) {
1135e88e1b22c87a075554fb3f10cee492e169570958Tadashi G. Takaoka            final PointerTracker tracker = PointerTracker.getPointerTracker(id, this);
11365f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            // Key repeating timer will be canceled if 2 or more keys are in action, and current
11375f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            // event (UP or DOWN) is non-modifier key.
11385f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            if (pointerCount > 1 && !tracker.isModifier()) {
1139f60d09ac3086f308cafcee13ebcb94c562f9e58eTadashi G. Takaoka                mKeyTimerHandler.cancelKeyRepeatTimer();
11405f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            }
11415f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            // Up event will pass through.
11425f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
11435f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
11445f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        // TODO: cleanup this code into a multi-touch to single-touch event converter class?
11455f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        // Translate mutli-touch event to single-touch events on the device that has no distinct
11465f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        // multi-touch panel.
1147f60d09ac3086f308cafcee13ebcb94c562f9e58eTadashi G. Takaoka        if (nonDistinctMultitouch) {
11485f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            // Use only main (id=0) pointer tracker.
1149e88e1b22c87a075554fb3f10cee492e169570958Tadashi G. Takaoka            final PointerTracker tracker = PointerTracker.getPointerTracker(0, this);
11505f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            if (pointerCount == 1 && oldPointerCount == 2) {
11515f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                // Multi-touch to single touch transition.
11525f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                // Send a down event for the latest pointer if the key is different from the
11535f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                // previous key.
1154e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                final Key newKey = tracker.getKeyOn(x, y);
1155e22baaadd314c80f835e2e96fb0dfc73838ac2cdTadashi G. Takaoka                if (mOldKey != newKey) {
11560efe174ea43fe576683102effbaef5be27575706Tadashi G. Takaoka                    tracker.onDownEvent(x, y, eventTime, this);
1157fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka                    if (action == MotionEvent.ACTION_UP) {
1158906f03121b6c6a795f35dbc24d2eceac0665f35fTadashi G. Takaoka                        tracker.onUpEvent(x, y, eventTime);
1159fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka                    }
11605f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                }
11615f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            } else if (pointerCount == 2 && oldPointerCount == 1) {
11625f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                // Single-touch to multi-touch transition.
11635f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                // Send an up event for the last pointer.
1164547b638194c05f971003edb06c3c6c489a76da5fTadashi G. Takaoka                final int[] lastCoords = CoordinateUtils.newInstance();
1165547b638194c05f971003edb06c3c6c489a76da5fTadashi G. Takaoka                mOldKey = tracker.getKeyOn(
1166547b638194c05f971003edb06c3c6c489a76da5fTadashi G. Takaoka                        CoordinateUtils.x(lastCoords), CoordinateUtils.y(lastCoords));
1167547b638194c05f971003edb06c3c6c489a76da5fTadashi G. Takaoka                tracker.onUpEvent(
1168547b638194c05f971003edb06c3c6c489a76da5fTadashi G. Takaoka                        CoordinateUtils.x(lastCoords), CoordinateUtils.y(lastCoords), eventTime);
11695f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            } else if (pointerCount == 1 && oldPointerCount == 1) {
11708ac6d505b7ceab020a4085b3dfbea5b47362b030Tadashi G. Takaoka                tracker.processMotionEvent(action, x, y, eventTime, this);
11715f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            } else {
11725f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                Log.w(TAG, "Unknown touch panel behavior: pointer count is " + pointerCount
11735f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka                        + " (old " + oldPointerCount + ")");
11745f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            }
11755f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            return true;
11765f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
11775f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
11785f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        if (action == MotionEvent.ACTION_MOVE) {
11795f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            for (int i = 0; i < pointerCount; i++) {
1180d05afa3f4c59641c8fabed034e457cb25f0c57f0Kurt Partridge                final int pointerId = me.getPointerId(i);
1181e88e1b22c87a075554fb3f10cee492e169570958Tadashi G. Takaoka                final PointerTracker tracker = PointerTracker.getPointerTracker(
1182d05afa3f4c59641c8fabed034e457cb25f0c57f0Kurt Partridge                        pointerId, this);
118335580bad6f3da3b204653825bbb6871563e70728Tom Ouyang                final int px = (int)me.getX(i);
118435580bad6f3da3b204653825bbb6871563e70728Tom Ouyang                final int py = (int)me.getY(i);
118535580bad6f3da3b204653825bbb6871563e70728Tom Ouyang                tracker.onMoveEvent(px, py, eventTime, me);
1186d05afa3f4c59641c8fabed034e457cb25f0c57f0Kurt Partridge                if (ENABLE_USABILITY_STUDY_LOG) {
1187fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka                    writeUsabilityStudyLog(me, action, eventTime, i, pointerId, px, py);
1188d05afa3f4c59641c8fabed034e457cb25f0c57f0Kurt Partridge                }
11899c3860ce461c3791891bf667edc77fe798c8d332Ken Wakasa                if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) {
1190fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka                    ResearchLogger.mainKeyboardView_processMotionEvent(
1191fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka                            me, action, eventTime, i, pointerId, px, py);
119215d4793911fa305e0a58aced925961e948582979satok                }
11935f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka            }
11945f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        } else {
1195e88e1b22c87a075554fb3f10cee492e169570958Tadashi G. Takaoka            final PointerTracker tracker = PointerTracker.getPointerTracker(id, this);
1196e88e1b22c87a075554fb3f10cee492e169570958Tadashi G. Takaoka            tracker.processMotionEvent(action, x, y, eventTime, this);
11975f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
11985f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
11995f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        return true;
12005f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
12015f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
1202fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka    private static void writeUsabilityStudyLog(final MotionEvent me, final int action,
1203fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            final long eventTime, final int index, final int id, final int x, final int y) {
1204fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        final String eventTag;
1205fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        switch (action) {
1206fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        case MotionEvent.ACTION_UP:
1207fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            eventTag = "[Up]";
1208fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            break;
1209fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        case MotionEvent.ACTION_DOWN:
1210fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            eventTag = "[Down]";
1211fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            break;
1212fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        case MotionEvent.ACTION_POINTER_UP:
1213fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            eventTag = "[PointerUp]";
1214fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            break;
1215fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        case MotionEvent.ACTION_POINTER_DOWN:
1216fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            eventTag = "[PointerDown]";
1217fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            break;
1218fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        case MotionEvent.ACTION_MOVE:
1219fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            eventTag = "[Move]";
1220fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            break;
1221fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        default:
1222fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            eventTag = "[Action" + action + "]";
1223fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka            break;
1224fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        }
1225fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        final float size = me.getSize(index);
1226fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        final float pressure = me.getPressure(index);
1227fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka        UsabilityStudyLogUtils.getInstance().write(
1228fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka                eventTag + eventTime + "," + id + "," + x + "," + y + "," + size + "," + pressure);
1229fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka    }
1230fca71cfddb7a5062e7a4fb512e1faab401c65cd3Tadashi G. Takaoka
1231375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    public void cancelAllMessages() {
1232375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        mKeyTimerHandler.cancelAllMessages();
1233375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        mDrawingHandler.cancelAllMessages();
1234375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka    }
1235375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka
12365f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    public void closing() {
1237375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        dismissAllKeyPreviews();
1238375982106bf073971fccd14b2c65ec8e089bb2aeTadashi G. Takaoka        cancelAllMessages();
1239a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        onDismissMoreKeysPanel();
1240a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka        mMoreKeysKeyboardCache.clear();
12415f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
12425f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
1243586a15c3f0d44590a5162e0ab4c3c52511f13f26Alan Viverette    /**
1244c6435f92a80c6664870f9d1a4bb2a1c5153ef2c3Tadashi G. Takaoka     * Receives hover events from the input framework.
1245586a15c3f0d44590a5162e0ab4c3c52511f13f26Alan Viverette     *
1246586a15c3f0d44590a5162e0ab4c3c52511f13f26Alan Viverette     * @param event The motion event to be dispatched.
1247586a15c3f0d44590a5162e0ab4c3c52511f13f26Alan Viverette     * @return {@code true} if the event was handled by the view, {@code false}
1248586a15c3f0d44590a5162e0ab4c3c52511f13f26Alan Viverette     *         otherwise
1249586a15c3f0d44590a5162e0ab4c3c52511f13f26Alan Viverette     */
1250c6435f92a80c6664870f9d1a4bb2a1c5153ef2c3Tadashi G. Takaoka    @Override
1251dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public boolean dispatchHoverEvent(final MotionEvent event) {
12525f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) {
1253e88e1b22c87a075554fb3f10cee492e169570958Tadashi G. Takaoka            final PointerTracker tracker = PointerTracker.getPointerTracker(0, this);
1254586a15c3f0d44590a5162e0ab4c3c52511f13f26Alan Viverette            return AccessibleKeyboardViewProxy.getInstance().dispatchHoverEvent(event, tracker);
12555f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        }
12565f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka
1257586a15c3f0d44590a5162e0ab4c3c52511f13f26Alan Viverette        // Reflection doesn't support calling superclass methods.
12585f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka        return false;
12595f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka    }
126022b48de11ce6f31a0edf90e1308073e67a7a2adbTadashi G. Takaoka
1261dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public void updateShortcutKey(final boolean available) {
12627bd714c086a78e2058543b0971ac92f5a30b2362Tadashi G. Takaoka        final Keyboard keyboard = getKeyboard();
126320dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        if (keyboard == null) {
126420dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            return;
126520dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        }
1266240871ecafde7834ebb4270cd7758fc904a5f3a7Tadashi G. Takaoka        final Key shortcutKey = keyboard.getKey(Constants.CODE_SHORTCUT);
126720dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        if (shortcutKey == null) {
126820dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            return;
126920dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        }
12707bd714c086a78e2058543b0971ac92f5a30b2362Tadashi G. Takaoka        shortcutKey.setEnabled(available);
12717bd714c086a78e2058543b0971ac92f5a30b2362Tadashi G. Takaoka        invalidateKey(shortcutKey);
12725afc3ae2d9df0c2c93f2c66af13b128889ac3b5dTadashi G. Takaoka    }
12735afc3ae2d9df0c2c93f2c66af13b128889ac3b5dTadashi G. Takaoka
127473a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka    private void updateAltCodeKeyWhileTyping() {
127573a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka        final Keyboard keyboard = getKeyboard();
127620dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        if (keyboard == null) {
127720dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            return;
127820dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        }
127973a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka        for (final Key key : keyboard.mAltCodeKeysWhileTyping) {
128073a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka            invalidateKey(key);
128173a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka        }
128273a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka    }
128373a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka
1284dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public void startDisplayLanguageOnSpacebar(final boolean subtypeChanged,
1285dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka            final boolean needsToDisplayLanguage, final boolean hasMultipleEnabledIMEsOrSubtypes) {
12864112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        mNeedsToDisplayLanguage = needsToDisplayLanguage;
1287fd60b2f97035382b14dce207b3613711982a613eTadashi G. Takaoka        mHasMultipleEnabledIMEsOrSubtypes = hasMultipleEnabledIMEsOrSubtypes;
1288fd60b2f97035382b14dce207b3613711982a613eTadashi G. Takaoka        final ObjectAnimator animator = mLanguageOnSpacebarFadeoutAnimator;
1289d7c4ba170982ddce5ac12ea92c3c3d8b53d524baTadashi G. Takaoka        if (animator == null) {
1290aee5f03d6ebf9cb03b52cbea003556f38745b4feTadashi G. Takaoka            mNeedsToDisplayLanguage = false;
12914c0c638a189c1073b1fb6e43fe5fddb6f9932038Tadashi G. Takaoka        } else {
1292dabf96896ef4c304c6dad36b307a2a458a58209dTadashi G. Takaoka            if (subtypeChanged && needsToDisplayLanguage) {
12934daf32b6c0358f0273a99b622a259ecdf6b44fa4Tom Ouyang                setLanguageOnSpacebarAnimAlpha(Constants.Color.ALPHA_OPAQUE);
129431c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka                if (animator.isStarted()) {
129531c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka                    animator.cancel();
129631c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka                }
1297d7c4ba170982ddce5ac12ea92c3c3d8b53d524baTadashi G. Takaoka                animator.start();
1298dabf96896ef4c304c6dad36b307a2a458a58209dTadashi G. Takaoka            } else {
129931c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka                if (!animator.isStarted()) {
130031c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka                    mLanguageOnSpacebarAnimAlpha = mLanguageOnSpacebarFinalAlpha;
130131c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka                }
1302dabf96896ef4c304c6dad36b307a2a458a58209dTadashi G. Takaoka            }
13034c0c638a189c1073b1fb6e43fe5fddb6f9932038Tadashi G. Takaoka        }
13044112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        invalidateKey(mSpaceKey);
13054112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka    }
13064112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka
1307dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    public void updateAutoCorrectionState(final boolean isAutoCorrection) {
130820dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        if (!mAutoCorrectionSpacebarLedEnabled) {
130920dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            return;
131020dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        }
13114112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        mAutoCorrectionSpacebarLedOn = isAutoCorrection;
13124112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        invalidateKey(mSpaceKey);
131322b48de11ce6f31a0edf90e1308073e67a7a2adbTadashi G. Takaoka    }
131422b48de11ce6f31a0edf90e1308073e67a7a2adbTadashi G. Takaoka
1315a729377395967f7652d93992cbcf50cd2ff522d1Tadashi G. Takaoka    private void dimEntireKeyboard(final boolean dimmed) {
1316f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka        final boolean needsRedrawing = mNeedsToDimEntireKeyboard != dimmed;
1317f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka        mNeedsToDimEntireKeyboard = dimmed;
1318f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka        if (needsRedrawing) {
1319f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka            invalidateAllKeys();
1320f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka        }
1321f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka    }
1322f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka
1323f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka    @Override
1324f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka    protected void onDraw(final Canvas canvas) {
1325f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka        super.onDraw(canvas);
1326f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka
1327f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka        // Overlay a dark rectangle to dim.
1328f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka        if (mNeedsToDimEntireKeyboard) {
1329f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka            canvas.drawRect(0, 0, getWidth(), getHeight(), mBackgroundDimAlphaPaint);
1330f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka        }
1331f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka    }
1332f3c4cd45a5df6f90b922ea44db259e53117bb25aTadashi G. Takaoka
133322b48de11ce6f31a0edf90e1308073e67a7a2adbTadashi G. Takaoka    @Override
1334dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    protected void onDrawKeyTopVisuals(final Key key, final Canvas canvas, final Paint paint,
1335dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka            final KeyDrawParams params) {
13366bc9186457219daeb3734531a01271b0e4fa37fbTadashi G. Takaoka        if (key.altCodeWhileTyping() && key.isEnabled()) {
133773a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka            params.mAnimAlpha = mAltCodeKeyWhileTypingAnimAlpha;
133873a46bfeb7a109b49be196e5d679e44c9e66a2e8Tadashi G. Takaoka        }
1339240871ecafde7834ebb4270cd7758fc904a5f3a7Tadashi G. Takaoka        if (key.mCode == Constants.CODE_SPACE) {
1340bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka            drawSpacebar(key, canvas, paint);
13414112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            // Whether space key needs to show the "..." popup hint for special purposes
1342fd60b2f97035382b14dce207b3613711982a613eTadashi G. Takaoka            if (key.isLongPressEnabled() && mHasMultipleEnabledIMEsOrSubtypes) {
1343bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka                drawKeyPopupHint(key, canvas, paint, params);
13444112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            }
1345240871ecafde7834ebb4270cd7758fc904a5f3a7Tadashi G. Takaoka        } else if (key.mCode == Constants.CODE_LANGUAGE_SWITCH) {
134681d4e3cd66a9388c47c7dba55240ddf849b31934Tadashi G. Takaoka            super.onDrawKeyTopVisuals(key, canvas, paint, params);
1347fd60b2f97035382b14dce207b3613711982a613eTadashi G. Takaoka            drawKeyPopupHint(key, canvas, paint, params);
1348b19a6b9fc55910bd241bee3b312169a818cb721dTadashi G. Takaoka        } else {
1349b19a6b9fc55910bd241bee3b312169a818cb721dTadashi G. Takaoka            super.onDrawKeyTopVisuals(key, canvas, paint, params);
135022b48de11ce6f31a0edf90e1308073e67a7a2adbTadashi G. Takaoka        }
135122b48de11ce6f31a0edf90e1308073e67a7a2adbTadashi G. Takaoka    }
13524112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka
1353dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    private boolean fitsTextIntoWidth(final int width, final String text, final Paint paint) {
1354bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka        paint.setTextScaleX(1.0f);
1355bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka        final float textWidth = getLabelWidth(text, paint);
135620dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        if (textWidth < width) {
135720dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            return true;
135820dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        }
1359bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka
1360bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka        final float scaleX = width / textWidth;
136120dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        if (scaleX < MINIMUM_XSCALE_OF_LANGUAGE_NAME) {
136220dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            return false;
136320dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        }
1364bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka
1365bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka        paint.setTextScaleX(scaleX);
1366bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka        return getLabelWidth(text, paint) < width;
1367bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka    }
1368bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka
1369bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka    // Layout language name on spacebar.
1370dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    private String layoutLanguageOnSpacebar(final Paint paint, final InputMethodSubtype subtype,
1371bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka            final int width) {
1372bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka        // Choose appropriate language name to fit into the width.
13734b3cae9b0cbd5bf30a1c8da383ff247f9c2afc5aTadashi G. Takaoka        final String fullText = getFullDisplayName(subtype);
137420dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        if (fitsTextIntoWidth(width, fullText, paint)) {
137520dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            return fullText;
1376bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka        }
1377bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka
137820dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        final String middleText = getMiddleDisplayName(subtype);
137920dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        if (fitsTextIntoWidth(width, middleText, paint)) {
138020dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            return middleText;
13814112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        }
13824112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka
138320dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        final String shortText = getShortDisplayName(subtype);
138420dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka        if (fitsTextIntoWidth(width, shortText, paint)) {
138520dd1bc090abdd3e88855fe54b1865949aa1168dTadashi G. Takaoka            return shortText;
13864112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        }
13874112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka
1388bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka        return "";
13894112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka    }
13904112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka
1391dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    private void drawSpacebar(final Key key, final Canvas canvas, final Paint paint) {
1392bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka        final int width = key.mWidth;
1393b19a6b9fc55910bd241bee3b312169a818cb721dTadashi G. Takaoka        final int height = key.mHeight;
13944112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka
1395f6972561fcb45310f18230ce217f0c6bb57e7eeeTadashi G. Takaoka        // If input language are explicitly selected.
1396bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka        if (mNeedsToDisplayLanguage) {
1397bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka            paint.setTextAlign(Align.CENTER);
1398bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka            paint.setTypeface(Typeface.DEFAULT);
1399bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka            paint.setTextSize(mSpacebarTextSize);
1400bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka            final InputMethodSubtype subtype = getKeyboard().mId.mSubtype;
1401bd2ca9c0214ea80fa860f4a9d118f866e16b03caTadashi G. Takaoka            final String language = layoutLanguageOnSpacebar(paint, subtype, width);
14024112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            // Draw language text with shadow
14034112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            final float descent = paint.descent();
14044112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            final float textHeight = -paint.ascent() + descent;
1405b19a6b9fc55910bd241bee3b312169a818cb721dTadashi G. Takaoka            final float baseline = height / 2 + textHeight / 2;
1406aee5f03d6ebf9cb03b52cbea003556f38745b4feTadashi G. Takaoka            paint.setColor(mSpacebarTextShadowColor);
140731c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka            paint.setAlpha(mLanguageOnSpacebarAnimAlpha);
14084112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            canvas.drawText(language, width / 2, baseline - descent - 1, paint);
1409aee5f03d6ebf9cb03b52cbea003556f38745b4feTadashi G. Takaoka            paint.setColor(mSpacebarTextColor);
141031c94cea82f1788e3a04f2a1e012945f35497f0aTadashi G. Takaoka            paint.setAlpha(mLanguageOnSpacebarAnimAlpha);
14114112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            canvas.drawText(language, width / 2, baseline - descent, paint);
14124112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        }
14134112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka
14144112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        // Draw the spacebar icon at the bottom
1415bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka        if (mAutoCorrectionSpacebarLedOn) {
14164112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            final int iconWidth = width * SPACE_LED_LENGTH_PERCENT / 100;
14174112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            final int iconHeight = mAutoCorrectionSpacebarLedIcon.getIntrinsicHeight();
14184112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            int x = (width - iconWidth) / 2;
14194112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            int y = height - iconHeight;
1420bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka            drawIcon(canvas, mAutoCorrectionSpacebarLedIcon, x, y, iconWidth, iconHeight);
14214112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        } else if (mSpaceIcon != null) {
14224112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            final int iconWidth = mSpaceIcon.getIntrinsicWidth();
14234112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            final int iconHeight = mSpaceIcon.getIntrinsicHeight();
14244112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            int x = (width - iconWidth) / 2;
14254112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka            int y = height - iconHeight;
1426bd93eddb52816acedd5242864e467781d4adfd71Tadashi G. Takaoka            drawIcon(canvas, mSpaceIcon, x, y, iconWidth, iconHeight);
14274112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka        }
14284112dc05002d7a880e558418639cf25c4bd02a5aTadashi G. Takaoka    }
142927b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka
143027b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka    // InputMethodSubtype's display name for spacebar text in its locale.
143127b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka    //        isAdditionalSubtype (T=true, F=false)
143276d4ffeebfd084913a8c1b7433dff48f5b2063dfTadashi G. Takaoka    // locale layout  | Short  Middle      Full
143376d4ffeebfd084913a8c1b7433dff48f5b2063dfTadashi G. Takaoka    // ------ ------- - ---- --------- ----------------------
143476d4ffeebfd084913a8c1b7433dff48f5b2063dfTadashi G. Takaoka    //  en_US qwerty  F  En  English   English (US)           exception
143576d4ffeebfd084913a8c1b7433dff48f5b2063dfTadashi G. Takaoka    //  en_GB qwerty  F  En  English   English (UK)           exception
143676d4ffeebfd084913a8c1b7433dff48f5b2063dfTadashi G. Takaoka    //  es_US spanish F  Es  Español   Español (EE.UU.)       exception
143776d4ffeebfd084913a8c1b7433dff48f5b2063dfTadashi G. Takaoka    //  fr    azerty  F  Fr  Français  Français
143876d4ffeebfd084913a8c1b7433dff48f5b2063dfTadashi G. Takaoka    //  fr_CA qwerty  F  Fr  Français  Français (Canada)
143976d4ffeebfd084913a8c1b7433dff48f5b2063dfTadashi G. Takaoka    //  de    qwertz  F  De  Deutsch   Deutsch
144076d4ffeebfd084913a8c1b7433dff48f5b2063dfTadashi G. Takaoka    //  zz    qwerty  F      QWERTY    QWERTY
14418630cd9d1708d42b3ed5d07d89ade2f3c1f85e47Tadashi G. Takaoka    //  fr    qwertz  T  Fr  Français  Français
14428630cd9d1708d42b3ed5d07d89ade2f3c1f85e47Tadashi G. Takaoka    //  de    qwerty  T  De  Deutsch   Deutsch
14438630cd9d1708d42b3ed5d07d89ade2f3c1f85e47Tadashi G. Takaoka    //  en_US azerty  T  En  English   English (US)
144476d4ffeebfd084913a8c1b7433dff48f5b2063dfTadashi G. Takaoka    //  zz    azerty  T      AZERTY    AZERTY
144527b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka
144627b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka    // Get InputMethodSubtype's full display name in its locale.
14474b3cae9b0cbd5bf30a1c8da383ff247f9c2afc5aTadashi G. Takaoka    static String getFullDisplayName(final InputMethodSubtype subtype) {
144827b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka        if (SubtypeLocale.isNoLanguage(subtype)) {
144927b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka            return SubtypeLocale.getKeyboardLayoutSetDisplayName(subtype);
145027b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka        }
14518630cd9d1708d42b3ed5d07d89ade2f3c1f85e47Tadashi G. Takaoka        return SubtypeLocale.getSubtypeLocaleDisplayName(subtype.getLocale());
145227b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka    }
145327b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka
145427b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka    // Get InputMethodSubtype's short display name in its locale.
1455dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    static String getShortDisplayName(final InputMethodSubtype subtype) {
145627b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka        if (SubtypeLocale.isNoLanguage(subtype)) {
145727b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka            return "";
145827b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka        }
145927b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka        final Locale locale = SubtypeLocale.getSubtypeLocale(subtype);
146027b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka        return StringUtils.toTitleCase(locale.getLanguage(), locale);
146127b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka    }
146227b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka
146327b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka    // Get InputMethodSubtype's middle display name in its locale.
1464dc34da218a22489d92d1015e9e5dac8d951b89f4Tadashi G. Takaoka    static String getMiddleDisplayName(final InputMethodSubtype subtype) {
146527b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka        if (SubtypeLocale.isNoLanguage(subtype)) {
146627b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka            return SubtypeLocale.getKeyboardLayoutSetDisplayName(subtype);
146727b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka        }
146827b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka        final Locale locale = SubtypeLocale.getSubtypeLocale(subtype);
14698630cd9d1708d42b3ed5d07d89ade2f3c1f85e47Tadashi G. Takaoka        return SubtypeLocale.getSubtypeLocaleDisplayName(locale.getLanguage());
147027b42ced86e1c85de3d59d91a9e5c577fa552569Tadashi G. Takaoka    }
14715f6816fa8bf259f0340a3d12c551d1532f647d66Tadashi G. Takaoka}
1472