From 73a46bfeb7a109b49be196e5d679e44c9e66a2e8 Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Wed, 14 Mar 2012 14:46:22 +0900
Subject: [PATCH] Gray out "alt-code-while-typing" key icon while typing

Bug: 6128215
Change-Id: Ie9c13d9d38aad6e92cc465e22f3ef20c80e0a36a
---
 .../anim/alt_code_key_while_typing_fadein.xml |  26 ++++
 .../alt_code_key_while_typing_fadeout.xml     |  26 ++++
 java/res/values/attrs.xml                     |   5 +-
 java/res/values/config.xml                    |   2 +-
 java/res/values/styles.xml                    |   4 +-
 .../inputmethod/keyboard/KeyboardView.java    |  17 ++-
 .../keyboard/LatinKeyboardView.java           | 112 ++++++++++++++++--
 .../inputmethod/keyboard/PointerTracker.java  |  14 +--
 8 files changed, 182 insertions(+), 24 deletions(-)
 create mode 100644 java/res/anim/alt_code_key_while_typing_fadein.xml
 create mode 100644 java/res/anim/alt_code_key_while_typing_fadeout.xml

diff --git a/java/res/anim/alt_code_key_while_typing_fadein.xml b/java/res/anim/alt_code_key_while_typing_fadein.xml
new file mode 100644
index 0000000000..3f5fd5d489
--- /dev/null
+++ b/java/res/anim/alt_code_key_while_typing_fadein.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2012, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<animator
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:valueType="intType"
+    android:duration="100"
+    android:valueFrom="128"
+    android:valueTo="255" />
diff --git a/java/res/anim/alt_code_key_while_typing_fadeout.xml b/java/res/anim/alt_code_key_while_typing_fadeout.xml
new file mode 100644
index 0000000000..88deb3120a
--- /dev/null
+++ b/java/res/anim/alt_code_key_while_typing_fadeout.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 2012, The Android Open Source Project
+**
+** Licensed under the Apache License, Version 2.0 (the "License");
+** you may not use this file except in compliance with the License.
+** You may obtain a copy of the License at
+**
+**     http://www.apache.org/licenses/LICENSE-2.0
+**
+** Unless required by applicable law or agreed to in writing, software
+** distributed under the License is distributed on an "AS IS" BASIS,
+** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+** See the License for the specific language governing permissions and
+** limitations under the License.
+*/
+-->
+
+<animator
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:valueType="intType"
+    android:duration="400"
+    android:valueFrom="255"
+    android:valueTo="128" />
diff --git a/java/res/values/attrs.xml b/java/res/values/attrs.xml
index e56778a69c..e619ad0a81 100644
--- a/java/res/values/attrs.xml
+++ b/java/res/values/attrs.xml
@@ -134,6 +134,9 @@
         <attr name="spacebarTextShadowColor" format="color" />
         <!-- Fadeout animator for spacebar language label. -->
         <attr name="languageOnSpacebarFadeoutAnimator" format="reference" />
+        <!-- Fadeout and fadein animator for altCodeWhileTyping keys. -->
+        <attr name="altCodeKeyWhileTypingFadeoutAnimator" format="reference" />
+        <attr name="altCodeKeyWhileTypingFadeinAnimator" format="reference" />
         <!-- Key detection hysteresis distance. -->
         <attr name="keyHysteresisDistance" format="dimension" />
         <!-- Touch noise threshold time in millisecond -->
@@ -153,7 +156,7 @@
         <!-- Long press timeout of space key in millisecond. -->
         <attr name="longPressSpaceKeyTimeout" format="integer" />
         <!-- Ignore special key timeout while typing in millisecond. -->
-        <attr name="ignoreSpecialKeyTimeout" format="integer" />
+        <attr name="ignoreAltCodeKeyTimeout" format="integer" />
         <!-- More keys keyboard will shown at touched point. -->
         <attr name="showMoreKeysKeyboardAtTouchedPoint" format="boolean" />
     </declare-styleable>
diff --git a/java/res/values/config.xml b/java/res/values/config.xml
index a02b84ff0f..c51800fb0f 100644
--- a/java/res/values/config.xml
+++ b/java/res/values/config.xml
@@ -65,7 +65,7 @@
     <!-- Long pressing space will invoke IME switcher if > 0, never invoke IME switcher if == 0 -->
     <integer name="config_long_press_space_key_timeout">
             @integer/config_long_press_key_timeout</integer>
-    <integer name="config_ignore_special_key_timeout">700</integer>
+    <integer name="config_ignore_alt_code_key_timeout">700</integer>
     <!-- Showing more keys keyboard, just above the touched point if true, aligned to the key if
          false -->
     <bool name="config_show_more_keys_keyboard_at_touched_point">false</bool>
diff --git a/java/res/values/styles.xml b/java/res/values/styles.xml
index 741ad99c9a..7d8b7ddf2b 100644
--- a/java/res/values/styles.xml
+++ b/java/res/values/styles.xml
@@ -76,9 +76,11 @@
         <item name="longPressKeyTimeout">@integer/config_long_press_key_timeout</item>
         <item name="longPressShiftKeyTimeout">@integer/config_long_press_shift_key_timeout</item>
         <item name="longPressSpaceKeyTimeout">@integer/config_long_press_space_key_timeout</item>
-        <item name="ignoreSpecialKeyTimeout">@integer/config_ignore_special_key_timeout</item>
+        <item name="ignoreAltCodeKeyTimeout">@integer/config_ignore_alt_code_key_timeout</item>
         <item name="showMoreKeysKeyboardAtTouchedPoint">@bool/config_show_more_keys_keyboard_at_touched_point</item>
         <item name="languageOnSpacebarFadeoutAnimator">@anim/language_on_spacebar_fadeout</item>
+        <item name="altCodeKeyWhileTypingFadeoutAnimator">@anim/alt_code_key_while_typing_fadeout</item>
+        <item name="altCodeKeyWhileTypingFadeinAnimator">@anim/alt_code_key_while_typing_fadein</item>
     </style>
     <style
         name="LatinKeyboardView"
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
index 4c65522ece..847174c0a7 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
@@ -95,6 +95,8 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
     // The maximum key label width in the proportion to the key width.
     private static final float MAX_LABEL_RATIO = 0.90f;
 
+    private final static int ALPHA_OPAQUE = 255;
+
     // Main keyboard
     private Keyboard mKeyboard;
     private final KeyDrawParams mKeyDrawParams;
@@ -201,6 +203,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
         public int mKeyHintLetterSize;
         public int mKeyShiftedLetterHintSize;
         public int mKeyHintLabelSize;
+        public int mAnimAlpha;
 
         public KeyDrawParams(TypedArray a) {
             mKeyBackground = a.getDrawable(R.styleable.KeyboardView_keyBackground);
@@ -256,6 +259,12 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
             mKeyShiftedLetterHintSize = (int)(keyHeight * mKeyShiftedLetterHintRatio);
             mKeyHintLabelSize = (int)(keyHeight * mKeyHintLabelRatio);
         }
+
+        public void brendAlpha(Paint paint) {
+            final int color = paint.getColor();
+            paint.setARGB((paint.getAlpha() * mAnimAlpha) / ALPHA_OPAQUE,
+                    Color.red(color), Color.green(color), Color.blue(color));
+        }
     }
 
     /* package */ static class KeyPreviewDrawParams {
@@ -343,7 +352,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
 
         mPaint.setAntiAlias(true);
         mPaint.setTextAlign(Align.CENTER);
-        mPaint.setAlpha(255);
     }
 
     // Read fraction value in TypedArray as float.
@@ -492,6 +500,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
         final int keyDrawY = key.mY + getPaddingTop();
         canvas.translate(keyDrawX, keyDrawY);
 
+        params.mAnimAlpha = ALPHA_OPAQUE;
         if (!key.isSpacer()) {
             onDrawKeyBackground(key, canvas, params);
         }
@@ -535,6 +544,9 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
 
         // Draw key label.
         final Drawable icon = key.getIcon(mKeyboard.mIconsSet);
+        if (icon != null) {
+            icon.setAlpha(params.mAnimAlpha);
+        }
         float positionX = centerX;
         if (key.mLabel != null) {
             final String label = key.mLabel;
@@ -589,6 +601,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
                 // Make label invisible
                 paint.setColor(Color.TRANSPARENT);
             }
+            params.brendAlpha(paint);
             canvas.drawText(label, 0, label.length(), positionX, baseline, paint);
             // Turn off drop shadow and reset x-scale.
             paint.setShadowLayer(0, 0, 0, 0);
@@ -633,6 +646,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
                 hintSize = params.mKeyHintLetterSize;
             }
             paint.setColor(hintColor);
+            params.brendAlpha(paint);
             paint.setTextSize(hintSize);
             final float hintX, hintY;
             if (key.hasHintLabel()) {
@@ -701,6 +715,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
 
         paint.setTextSize(params.mKeyHintLetterSize);
         paint.setColor(params.mKeyHintLabelColor);
+        params.brendAlpha(paint);
         paint.setTextAlign(Align.CENTER);
         final float hintX = keyWidth - params.mKeyHintLetterPadding
                 - getCharWidth(KEY_LABEL_REFERENCE_CHAR, paint) / 2;
diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
index e320330a18..df8a10a351 100644
--- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
@@ -98,6 +98,11 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
     private final Drawable mAutoCorrectionSpacebarLedIcon;
     private static final int SPACE_LED_LENGTH_PERCENT = 80;
 
+    // Stuff to draw altCodeWhileTyping keys.
+    private ValueAnimator mAltCodeKeyWhileTypingFadeoutAnimator;
+    private ValueAnimator mAltCodeKeyWhileTypingFadeinAnimator;
+    private int mAltCodeKeyWhileTypingAnimAlpha;
+
     // More keys keyboard
     private PopupWindow mMoreKeysWindow;
     private MoreKeysPanel mMoreKeysPanel;
@@ -122,7 +127,7 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
         private static final int MSG_REPEAT_KEY = 1;
         private static final int MSG_LONGPRESS_KEY = 2;
         private static final int MSG_DOUBLE_TAP = 3;
-        private static final int MSG_KEY_TYPED = 4;
+        private static final int MSG_TYPING_STATE_EXPIRED = 4;
 
         private final KeyTimerParams mParams;
         private boolean mInKeyRepeat;
@@ -148,6 +153,18 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
                     KeyboardSwitcher.getInstance().onLongPressTimeout(msg.arg1);
                 }
                 break;
+            case MSG_TYPING_STATE_EXPIRED:
+                final ValueAnimator fadeout = keyboardView.mAltCodeKeyWhileTypingFadeoutAnimator;
+                if (fadeout != null && fadeout.isStarted()) {
+                    fadeout.cancel();
+                }
+                // TODO: Start the fade in animation with an initial value that is the same as the
+                // final value when the above fade out animation gets cancelled.
+                final ValueAnimator fadein = keyboardView.mAltCodeKeyWhileTypingFadeinAnimator;
+                if (fadein != null && !fadein.isStarted()) {
+                    fadein.start();
+                }
+                break;
             }
         }
 
@@ -222,14 +239,30 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
         }
 
         @Override
-        public void startKeyTypedTimer() {
-            removeMessages(MSG_KEY_TYPED);
-            sendMessageDelayed(obtainMessage(MSG_KEY_TYPED), mParams.mIgnoreSpecialKeyTimeout);
+        public void startTypingStateTimer() {
+            final boolean isTyping = isTypingState();
+            removeMessages(MSG_TYPING_STATE_EXPIRED);
+            sendMessageDelayed(
+                    obtainMessage(MSG_TYPING_STATE_EXPIRED), mParams.mIgnoreAltCodeKeyTimeout);
+            final LatinKeyboardView keyboardView = getOuterInstance();
+            if (isTyping) {
+                return;
+            }
+            final ValueAnimator fadein = keyboardView.mAltCodeKeyWhileTypingFadeinAnimator;
+            if (fadein != null && fadein.isStarted()) {
+                fadein.cancel();
+            }
+            // TODO: Start the fade out animation with an initial value that is the same as the
+            // final value when the above fade in animation gets cancelled.
+            final ValueAnimator fadeout = keyboardView.mAltCodeKeyWhileTypingFadeoutAnimator;
+            if (fadeout != null && !fadeout.isStarted()) {
+                fadeout.start();
+            }
         }
 
         @Override
-        public boolean isTyping() {
-            return hasMessages(MSG_KEY_TYPED);
+        public boolean isTypingState() {
+            return hasMessages(MSG_TYPING_STATE_EXPIRED);
         }
 
         @Override
@@ -288,7 +321,7 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
         public final int mLongPressKeyTimeout;
         public final int mLongPressShiftKeyTimeout;
         public final int mLongPressSpaceKeyTimeout;
-        public final int mIgnoreSpecialKeyTimeout;
+        public final int mIgnoreAltCodeKeyTimeout;
 
         KeyTimerParams() {
             mKeyRepeatStartTimeout = 0;
@@ -296,7 +329,7 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
             mLongPressKeyTimeout = 0;
             mLongPressShiftKeyTimeout = 0;
             mLongPressSpaceKeyTimeout = 0;
-            mIgnoreSpecialKeyTimeout = 0;
+            mIgnoreAltCodeKeyTimeout = 0;
         }
 
         public KeyTimerParams(TypedArray latinKeyboardViewAttr) {
@@ -310,8 +343,8 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
                     R.styleable.LatinKeyboardView_longPressShiftKeyTimeout, 0);
             mLongPressSpaceKeyTimeout = latinKeyboardViewAttr.getInt(
                     R.styleable.LatinKeyboardView_longPressSpaceKeyTimeout, 0);
-            mIgnoreSpecialKeyTimeout = latinKeyboardViewAttr.getInt(
-                    R.styleable.LatinKeyboardView_ignoreSpecialKeyTimeout, 0);
+            mIgnoreAltCodeKeyTimeout = latinKeyboardViewAttr.getInt(
+                    R.styleable.LatinKeyboardView_ignoreAltCodeKeyTimeout, 0);
         }
     }
 
@@ -342,6 +375,10 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
                 R.styleable.LatinKeyboardView_spacebarTextShadowColor, 0);
         final int languageOnSpacebarFadeoutAnimatorResId = a.getResourceId(
                 R.styleable.LatinKeyboardView_languageOnSpacebarFadeoutAnimator, 0);
+        final int altCodeKeyWhileTypingFadeoutAnimatorResId = a.getResourceId(
+                R.styleable.LatinKeyboardView_altCodeKeyWhileTypingFadeoutAnimator, 0);
+        final int altCodeKeyWhileTypingFadeinAnimatorResId = a.getResourceId(
+                R.styleable.LatinKeyboardView_altCodeKeyWhileTypingFadeinAnimator, 0);
 
         final KeyTimerParams keyTimerParams = new KeyTimerParams(a);
         mPointerTrackerParams = new PointerTrackerParams(a);
@@ -357,7 +394,7 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
 
         PointerTracker.setParameters(mPointerTrackerParams);
 
-        ValueAnimator animator = loadValueAnimator(context, languageOnSpacebarFadeoutAnimatorResId);
+        final ValueAnimator animator = loadValueAnimator(languageOnSpacebarFadeoutAnimatorResId);
         if (animator != null) {
             animator.addUpdateListener(new AnimatorUpdateListener() {
                 @Override
@@ -377,11 +414,47 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
             animator.end();
         }
         mLanguageOnSpacebarFadeoutAnimator = animator;
+
+        final ValueAnimator fadeout = loadValueAnimator(altCodeKeyWhileTypingFadeoutAnimatorResId);
+        if (fadeout != null) {
+            fadeout.addUpdateListener(new AnimatorUpdateListener() {
+                @Override
+                public void onAnimationUpdate(ValueAnimator animation) {
+                    mAltCodeKeyWhileTypingAnimAlpha = (Integer)animation.getAnimatedValue();
+                    updateAltCodeKeyWhileTyping();
+                }
+            });
+            fadeout.addListener(new AnimatorListenerAdapter() {
+                @Override
+                public void onAnimationEnd(Animator a) {
+                    final ValueAnimator valueAnimator = (ValueAnimator)a;
+                }
+            });
+        }
+        mAltCodeKeyWhileTypingFadeoutAnimator = fadeout;
+
+        final ValueAnimator fadein = loadValueAnimator(altCodeKeyWhileTypingFadeinAnimatorResId);
+        if (fadein != null) {
+            fadein.addUpdateListener(new AnimatorUpdateListener() {
+                @Override
+                public void onAnimationUpdate(ValueAnimator animation) {
+                    mAltCodeKeyWhileTypingAnimAlpha = (Integer)animation.getAnimatedValue();
+                    updateAltCodeKeyWhileTyping();
+                }
+            });
+            fadein.addListener(new AnimatorListenerAdapter() {
+                @Override
+                public void onAnimationEnd(Animator a) {
+                    final ValueAnimator valueAnimator = (ValueAnimator)a;
+                }
+            });
+        }
+        mAltCodeKeyWhileTypingFadeinAnimator = fadein;
     }
 
-    private static ValueAnimator loadValueAnimator(Context context, int resId) {
+    private ValueAnimator loadValueAnimator(int resId) {
         if (resId == 0) return null;
-        return (ValueAnimator)AnimatorInflater.loadAnimator(context, resId);
+        return (ValueAnimator)AnimatorInflater.loadAnimator(getContext(), resId);
     }
 
     public void setKeyboardActionListener(KeyboardActionListener listener) {
@@ -437,6 +510,8 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
         final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
         mSpacebarTextSize = keyHeight * mSpacebarTextRatio;
         mSpacebarLocale = keyboard.mId.mLocale;
+        mSpacebarTextAlpha = ALPHA_OPAQUE;
+        mAltCodeKeyWhileTypingAnimAlpha = ALPHA_OPAQUE;
     }
 
     /**
@@ -797,6 +872,14 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
         invalidateKey(shortcutKey);
     }
 
+    private void updateAltCodeKeyWhileTyping() {
+        final Keyboard keyboard = getKeyboard();
+        if (keyboard == null) return;
+        for (final Key key : keyboard.mAltCodeKeysWhileTyping) {
+            invalidateKey(key);
+        }
+    }
+
     public void startDisplayLanguageOnSpacebar(boolean subtypeChanged,
             boolean needsToDisplayLanguage) {
         final ValueAnimator animator = mLanguageOnSpacebarFadeoutAnimator;
@@ -825,6 +908,9 @@ public class LatinKeyboardView extends KeyboardView implements PointerTracker.Ke
 
     @Override
     protected void onDrawKeyTopVisuals(Key key, Canvas canvas, Paint paint, KeyDrawParams params) {
+        if (key.altCodeWhileTyping()) {
+            params.mAnimAlpha = mAltCodeKeyWhileTypingAnimAlpha;
+        }
         if (key.mCode == Keyboard.CODE_SPACE) {
             drawSpacebar(key, canvas, paint);
 
diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
index 607b33bb4a..ea97ba8516 100644
--- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java
+++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java
@@ -68,8 +68,8 @@ public class PointerTracker {
     }
 
     public interface TimerProxy {
-        public void startKeyTypedTimer();
-        public boolean isTyping();
+        public void startTypingStateTimer();
+        public boolean isTypingState();
         public void startKeyRepeatTimer(PointerTracker tracker);
         public void startLongPressTimer(PointerTracker tracker);
         public void startLongPressTimer(int code);
@@ -81,9 +81,9 @@ public class PointerTracker {
 
         public static class Adapter implements TimerProxy {
             @Override
-            public void startKeyTypedTimer() {}
+            public void startTypingStateTimer() {}
             @Override
-            public boolean isTyping() { return false; }
+            public boolean isTypingState() { return false; }
             @Override
             public void startKeyRepeatTimer(PointerTracker tracker) {}
             @Override
@@ -251,7 +251,7 @@ public class PointerTracker {
     // primaryCode is different from {@link Key#mCode}.
     private void callListenerOnCodeInput(Key key, int primaryCode, int x, int y) {
         final boolean ignoreModifierKey = mIgnoreModifierKey && key.isModifier();
-        final boolean alterCode = key.altCodeWhileTyping() && mTimerProxy.isTyping();
+        final boolean alterCode = key.altCodeWhileTyping() && mTimerProxy.isTypingState();
         final int code = alterCode ? key.mAltCode : primaryCode;
         if (DEBUG_LISTENER) {
             Log.d(TAG, "onCodeInput: " + Keyboard.printableCode(code) + " text=" + key.mOutputText
@@ -269,7 +269,7 @@ public class PointerTracker {
                 mListener.onCodeInput(code, x, y);
             }
             if (!key.altCodeWhileTyping() && !key.isModifier()) {
-                mTimerProxy.startKeyTypedTimer();
+                mTimerProxy.startTypingStateTimer();
             }
         }
     }
@@ -368,7 +368,7 @@ public class PointerTracker {
             }
         }
 
-        if (key.altCodeWhileTyping() && mTimerProxy.isTyping()) {
+        if (key.altCodeWhileTyping() && mTimerProxy.isTypingState()) {
             final int altCode = key.mAltCode;
             final Key altKey = mKeyboard.getKey(altCode);
             if (altKey != null) {
-- 
GitLab