From 61bc66b324f94a7b29b5388101cb622bf243d19e Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Wed, 29 Aug 2012 22:34:29 +0900
Subject: [PATCH] Revert "Revert "Fix key letter size of 5-rows Thai keyboard""

This reverts commit 7a5ca9982fae2c17df4e597d2c54c81782946ed1.
Depends: Ia4fa5046b3106e2710b9d5df1a6cc5f2cf995ec7

Change-Id: I0b9d0125380efe898943dc26ae884378145c44a3
---
 java/res/values-land/dimens.xml               |  2 +
 java/res/values-sw600dp-land/dimens.xml       |  2 +
 java/res/values-sw600dp/dimens.xml            |  2 +
 java/res/values-sw768dp-land/dimens.xml       |  2 +
 java/res/values-sw768dp/dimens.xml            |  2 +
 java/res/values/dimens.xml                    |  2 +
 java/res/xml-sw600dp-land/kbd_thai.xml        |  2 +
 java/res/xml-sw600dp/kbd_thai.xml             |  2 +
 java/res/xml-sw768dp-land/kbd_thai.xml        |  2 +
 java/res/xml-sw768dp/kbd_thai.xml             |  2 +
 .../inputmethod/keyboard/Keyboard.java        | 20 ++++++
 .../inputmethod/keyboard/KeyboardView.java    | 69 +++++++++++++------
 .../inputmethod/latin/ResourceUtils.java      | 10 +++
 13 files changed, 97 insertions(+), 22 deletions(-)

diff --git a/java/res/values-land/dimens.xml b/java/res/values-land/dimens.xml
index 62597258c7..fbfbb512ce 100644
--- a/java/res/values-land/dimens.xml
+++ b/java/res/values-land/dimens.xml
@@ -53,6 +53,8 @@
     <fraction name="key_uppercase_letter_ratio">40%</fraction>
     <fraction name="key_preview_text_ratio">90%</fraction>
     <fraction name="spacebar_text_ratio">40.000%</fraction>
+    <fraction name="key_letter_ratio_5rows">78%</fraction>
+    <fraction name="key_uppercase_letter_ratio_5rows">48%</fraction>
     <dimen name="key_preview_offset">0.0dp</dimen>
 
     <dimen name="key_preview_offset_ics">1.6dp</dimen>
diff --git a/java/res/values-sw600dp-land/dimens.xml b/java/res/values-sw600dp-land/dimens.xml
index a478df89a6..8283cd9fbe 100644
--- a/java/res/values-sw600dp-land/dimens.xml
+++ b/java/res/values-sw600dp-land/dimens.xml
@@ -51,6 +51,8 @@
     <fraction name="key_hint_label_ratio">34%</fraction>
     <fraction name="key_uppercase_letter_ratio">29%</fraction>
     <fraction name="spacebar_text_ratio">30.0%</fraction>
+    <fraction name="key_letter_ratio_5rows">62%</fraction>
+    <fraction name="key_uppercase_letter_ratio_5rows">36%</fraction>
     <dimen name="key_uppercase_letter_padding">4dp</dimen>
 
     <dimen name="suggestions_strip_padding">252.0dp</dimen>
diff --git a/java/res/values-sw600dp/dimens.xml b/java/res/values-sw600dp/dimens.xml
index 5596ba41ce..78aa60533a 100644
--- a/java/res/values-sw600dp/dimens.xml
+++ b/java/res/values-sw600dp/dimens.xml
@@ -63,6 +63,8 @@
     <fraction name="key_uppercase_letter_ratio">22%</fraction>
     <fraction name="key_preview_text_ratio">50%</fraction>
     <fraction name="spacebar_text_ratio">28.0%</fraction>
+    <fraction name="key_letter_ratio_5rows">52%</fraction>
+    <fraction name="key_uppercase_letter_ratio_5rows">27%</fraction>
     <dimen name="key_preview_height">94.5dp</dimen>
     <dimen name="key_preview_offset">16.0dp</dimen>
 
diff --git a/java/res/values-sw768dp-land/dimens.xml b/java/res/values-sw768dp-land/dimens.xml
index b95c858dc6..ac94c9293d 100644
--- a/java/res/values-sw768dp-land/dimens.xml
+++ b/java/res/values-sw768dp-land/dimens.xml
@@ -53,6 +53,8 @@
     <fraction name="key_hint_label_ratio">28%</fraction>
     <fraction name="key_uppercase_letter_ratio">24%</fraction>
     <fraction name="spacebar_text_ratio">24.00%</fraction>
+    <fraction name="key_letter_ratio_5rows">53%</fraction>
+    <fraction name="key_uppercase_letter_ratio_5rows">30%</fraction>
     <dimen name="key_preview_height">107.1dp</dimen>
 
     <dimen name="key_preview_offset_ics">8.0dp</dimen>
diff --git a/java/res/values-sw768dp/dimens.xml b/java/res/values-sw768dp/dimens.xml
index ce33b73cb5..0fd9cedcbc 100644
--- a/java/res/values-sw768dp/dimens.xml
+++ b/java/res/values-sw768dp/dimens.xml
@@ -64,6 +64,8 @@
     <fraction name="key_uppercase_letter_ratio">26%</fraction>
     <fraction name="key_preview_text_ratio">50%</fraction>
     <fraction name="spacebar_text_ratio">29.03%</fraction>
+    <fraction name="key_letter_ratio_5rows">51%</fraction>
+    <fraction name="key_uppercase_letter_ratio_5rows">33%</fraction>
     <dimen name="key_preview_height">94.5dp</dimen>
     <dimen name="key_preview_offset">16.0dp</dimen>
 
diff --git a/java/res/values/dimens.xml b/java/res/values/dimens.xml
index 4fd942b144..e0fcd5e2d1 100644
--- a/java/res/values/dimens.xml
+++ b/java/res/values/dimens.xml
@@ -65,6 +65,8 @@
     <fraction name="key_uppercase_letter_ratio">35%</fraction>
     <fraction name="key_preview_text_ratio">82%</fraction>
     <fraction name="spacebar_text_ratio">33.735%</fraction>
+    <fraction name="key_letter_ratio_5rows">64%</fraction>
+    <fraction name="key_uppercase_letter_ratio_5rows">41%</fraction>
     <dimen name="key_preview_height">80dp</dimen>
     <dimen name="key_preview_offset">-8.0dp</dimen>
 
diff --git a/java/res/xml-sw600dp-land/kbd_thai.xml b/java/res/xml-sw600dp-land/kbd_thai.xml
index 3143061400..a7763f2c7a 100644
--- a/java/res/xml-sw600dp-land/kbd_thai.xml
+++ b/java/res/xml-sw600dp-land/kbd_thai.xml
@@ -23,6 +23,8 @@
     latin:rowHeight="20%p"
     latin:verticalGap="3.20%p"
     latin:keyTypeface="normal"
+    latin:keyLetterSize="@fraction/key_letter_ratio_5rows"
+    latin:keyShiftedLetterHintRatio="@fraction/key_uppercase_letter_ratio_5rows"
     latin:touchPositionCorrectionData="@null"
 >
     <include
diff --git a/java/res/xml-sw600dp/kbd_thai.xml b/java/res/xml-sw600dp/kbd_thai.xml
index 3143061400..a7763f2c7a 100644
--- a/java/res/xml-sw600dp/kbd_thai.xml
+++ b/java/res/xml-sw600dp/kbd_thai.xml
@@ -23,6 +23,8 @@
     latin:rowHeight="20%p"
     latin:verticalGap="3.20%p"
     latin:keyTypeface="normal"
+    latin:keyLetterSize="@fraction/key_letter_ratio_5rows"
+    latin:keyShiftedLetterHintRatio="@fraction/key_uppercase_letter_ratio_5rows"
     latin:touchPositionCorrectionData="@null"
 >
     <include
diff --git a/java/res/xml-sw768dp-land/kbd_thai.xml b/java/res/xml-sw768dp-land/kbd_thai.xml
index b7633df33e..0f8516f5df 100644
--- a/java/res/xml-sw768dp-land/kbd_thai.xml
+++ b/java/res/xml-sw768dp-land/kbd_thai.xml
@@ -23,6 +23,8 @@
     latin:rowHeight="20%p"
     latin:verticalGap="2.65%p"
     latin:keyTypeface="normal"
+    latin:keyLetterSize="@fraction/key_letter_ratio_5rows"
+    latin:keyShiftedLetterHintRatio="@fraction/key_uppercase_letter_ratio_5rows"
     latin:touchPositionCorrectionData="@null"
 >
     <include
diff --git a/java/res/xml-sw768dp/kbd_thai.xml b/java/res/xml-sw768dp/kbd_thai.xml
index 2be6a25498..7e4451455f 100644
--- a/java/res/xml-sw768dp/kbd_thai.xml
+++ b/java/res/xml-sw768dp/kbd_thai.xml
@@ -23,6 +23,8 @@
     latin:rowHeight="20%p"
     latin:verticalGap="2.95%p"
     latin:keyTypeface="normal"
+    latin:keyLetterSize="@fraction/key_letter_ratio_5rows"
+    latin:keyShiftedLetterHintRatio="@fraction/key_uppercase_letter_ratio_5rows"
     latin:touchPositionCorrectionData="@null"
 >
     <include
diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java
index aa757f153c..a32b3e7959 100644
--- a/java/src/com/android/inputmethod/keyboard/Keyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java
@@ -124,6 +124,10 @@ public class Keyboard {
 
     /** Per keyboard key visual parameters */
     public final Typeface mKeyTypeface;
+    public final float mKeyLetterRatio;
+    public final int mKeyLetterSize;
+    public final float mKeyHintLetterRatio;
+    public final float mKeyShiftedLetterHintRatio;
 
     public final int mMostCommonKeyHeight;
     public final int mMostCommonKeyWidth;
@@ -156,6 +160,10 @@ public class Keyboard {
         mMaxMoreKeysKeyboardColumn = params.mMaxMoreKeysKeyboardColumn;
 
         mKeyTypeface = params.mKeyTypeface;
+        mKeyLetterRatio = params.mKeyLetterRatio;
+        mKeyLetterSize = params.mKeyLetterSize;
+        mKeyHintLetterRatio = params.mKeyHintLetterRatio;
+        mKeyShiftedLetterHintRatio = params.mKeyShiftedLetterHintRatio;
 
         mTopPadding = params.mTopPadding;
         mVerticalGap = params.mVerticalGap;
@@ -253,6 +261,10 @@ public class Keyboard {
         public int mHorizontalCenterPadding;
 
         public Typeface mKeyTypeface = null;
+        public float mKeyLetterRatio = ResourceUtils.UNDEFINED_RATIO;
+        public int mKeyLetterSize = ResourceUtils.UNDEFINED_DIMENSION;
+        public float mKeyHintLetterRatio = ResourceUtils.UNDEFINED_RATIO;;
+        public float mKeyShiftedLetterHintRatio = ResourceUtils.UNDEFINED_RATIO;
 
         public int mDefaultRowHeight;
         public int mDefaultKeyWidth;
@@ -813,6 +825,14 @@ public class Keyboard {
                     params.mKeyTypeface = Typeface.defaultFromStyle(keyboardViewAttr.getInt(
                             R.styleable.KeyboardView_keyTypeface, Typeface.NORMAL));
                 }
+                params.mKeyLetterRatio = ResourceUtils.getFraction(keyboardViewAttr,
+                        R.styleable.KeyboardView_keyLetterSize);
+                params.mKeyLetterSize = ResourceUtils.getDimensionPixelSize(keyboardViewAttr,
+                        R.styleable.KeyboardView_keyLetterSize);
+                params.mKeyHintLetterRatio = ResourceUtils.getFraction(keyboardViewAttr,
+                        R.styleable.KeyboardView_keyHintLetterRatio);
+                params.mKeyShiftedLetterHintRatio = ResourceUtils.getFraction(keyboardViewAttr,
+                        R.styleable.KeyboardView_keyShiftedLetterHintRatio);
 
                 params.mMoreKeysTemplate = keyboardAttr.getResourceId(
                         R.styleable.Keyboard_moreKeysTemplate, 0);
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
index d4d4ce76b7..6a23c919b7 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
@@ -85,6 +85,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
 
     // Miscellaneous constants
     private static final int[] LONG_PRESSABLE_STATE_SET = { android.R.attr.state_long_pressable };
+
     // XML attributes
     protected final float mVerticalCorrection;
     protected final int mMoreKeysLayout;
@@ -197,6 +198,7 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
 
         private final Typeface mKeyTypefaceFromKeyboardView;
         private final float mKeyLetterRatio;
+        private final int mKeyLetterSizeFromKeyboardView;
         private final float mKeyLargeLetterRatio;
         private final float mKeyLabelRatio;
         private final float mKeyLargeLabelRatio;
@@ -217,16 +219,12 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
 
         public KeyDrawParams(final TypedArray a) {
             mKeyBackground = a.getDrawable(R.styleable.KeyboardView_keyBackground);
-            if (!ResourceUtils.isValidFraction(mKeyLetterRatio = ResourceUtils.getFraction(a,
-                    R.styleable.KeyboardView_keyLetterSize))) {
-                mKeyLetterSize = ResourceUtils.getDimensionPixelSize(a,
-                        R.styleable.KeyboardView_keyLetterSize);
-            }
-            if (!ResourceUtils.isValidFraction(mKeyLabelRatio = ResourceUtils.getFraction(a,
-                    R.styleable.KeyboardView_keyLabelSize))) {
-                mKeyLabelSize = ResourceUtils.getDimensionPixelSize(a,
-                        R.styleable.KeyboardView_keyLabelSize);
-            }
+            mKeyLetterRatio = ResourceUtils.getFraction(a, R.styleable.KeyboardView_keyLetterSize);
+            mKeyLetterSizeFromKeyboardView = ResourceUtils.getDimensionPixelSize(a,
+                    R.styleable.KeyboardView_keyLetterSize);
+            mKeyLabelRatio = ResourceUtils.getFraction(a, R.styleable.KeyboardView_keyLabelSize);
+            mKeyLabelSize = ResourceUtils.getDimensionPixelSize(a,
+                    R.styleable.KeyboardView_keyLabelSize);
             mKeyLargeLabelRatio = ResourceUtils.getFraction(a,
                     R.styleable.KeyboardView_keyLargeLabelRatio);
             mKeyLargeLetterRatio = ResourceUtils.getFraction(a,
@@ -245,19 +243,23 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
                     R.styleable.KeyboardView_keyPopupHintLetterPadding, 0);
             mKeyShiftedLetterHintPadding = a.getDimension(
                     R.styleable.KeyboardView_keyShiftedLetterHintPadding, 0);
-            mKeyTextColor = a.getColor(R.styleable.KeyboardView_keyTextColor, 0xFF000000);
+            mKeyTextColor = a.getColor(
+                    R.styleable.KeyboardView_keyTextColor, Color.WHITE);
             mKeyTextInactivatedColor = a.getColor(
-                    R.styleable.KeyboardView_keyTextInactivatedColor, 0xFF000000);
-            mKeyHintLetterColor = a.getColor(R.styleable.KeyboardView_keyHintLetterColor, 0);
-            mKeyHintLabelColor = a.getColor(R.styleable.KeyboardView_keyHintLabelColor, 0);
+                    R.styleable.KeyboardView_keyTextInactivatedColor, Color.WHITE);
+            mKeyHintLetterColor = a.getColor(
+                    R.styleable.KeyboardView_keyHintLetterColor, Color.TRANSPARENT);
+            mKeyHintLabelColor = a.getColor(
+                    R.styleable.KeyboardView_keyHintLabelColor, Color.TRANSPARENT);
             mKeyShiftedLetterHintInactivatedColor = a.getColor(
-                    R.styleable.KeyboardView_keyShiftedLetterHintInactivatedColor, 0);
+                    R.styleable.KeyboardView_keyShiftedLetterHintInactivatedColor,
+                    Color.TRANSPARENT);
             mKeyShiftedLetterHintActivatedColor = a.getColor(
-                    R.styleable.KeyboardView_keyShiftedLetterHintActivatedColor, 0);
+                    R.styleable.KeyboardView_keyShiftedLetterHintActivatedColor, Color.TRANSPARENT);
             mKeyTypefaceFromKeyboardView = Typeface.defaultFromStyle(
                     a.getInt(R.styleable.KeyboardView_keyTypeface, Typeface.NORMAL));
             mKeyTypeface = mKeyTypefaceFromKeyboardView;
-            mShadowColor = a.getColor(R.styleable.KeyboardView_shadowColor, 0);
+            mShadowColor = a.getColor(R.styleable.KeyboardView_shadowColor, Color.TRANSPARENT);
             mShadowRadius = a.getFloat(R.styleable.KeyboardView_shadowRadius, 0f);
 
             mKeyBackground.getPadding(mPadding);
@@ -267,19 +269,42 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
             mKeyTypeface = (keyboard.mKeyTypeface != null)
                     ? keyboard.mKeyTypeface : mKeyTypefaceFromKeyboardView;
             final int keyHeight = keyboard.mMostCommonKeyHeight - keyboard.mVerticalGap;
-            if (ResourceUtils.isValidFraction(mKeyLetterRatio)) {
-                mKeyLetterSize = (int)(keyHeight * mKeyLetterRatio);
-            }
+            mKeyLetterSize = selectTextSizeFromDimensionOrRatio(keyHeight,
+                    mKeyLetterSizeFromKeyboardView, mKeyLetterRatio,
+                    mKeyLetterSizeFromKeyboardView);
+            // Override if size/ratio is specified in Keyboard.
+            mKeyLetterSize = selectTextSizeFromDimensionOrRatio(keyHeight, keyboard.mKeyLetterSize,
+                    keyboard.mKeyLetterRatio, mKeyLetterSize);
             if (ResourceUtils.isValidFraction(mKeyLabelRatio)) {
                 mKeyLabelSize = (int)(keyHeight * mKeyLabelRatio);
             }
             mKeyLargeLabelSize = (int)(keyHeight * mKeyLargeLabelRatio);
             mKeyLargeLetterSize = (int)(keyHeight * mKeyLargeLetterRatio);
-            mKeyHintLetterSize = (int)(keyHeight * mKeyHintLetterRatio);
-            mKeyShiftedLetterHintSize = (int)(keyHeight * mKeyShiftedLetterHintRatio);
+            mKeyHintLetterSize = selectTextSizeFromKeyboardOrView(keyHeight,
+                    keyboard.mKeyHintLetterRatio, mKeyHintLetterRatio);
+            mKeyShiftedLetterHintSize = selectTextSizeFromKeyboardOrView(keyHeight,
+                    keyboard.mKeyShiftedLetterHintRatio, mKeyShiftedLetterHintRatio);
             mKeyHintLabelSize = (int)(keyHeight * mKeyHintLabelRatio);
         }
 
+        private static final int selectTextSizeFromDimensionOrRatio(final int keyHeight,
+                final int dimens, final float ratio, final int defaultDimens) {
+            if (ResourceUtils.isValidDimensionPixelSize(dimens)) {
+                return dimens;
+            }
+            if (ResourceUtils.isValidFraction(ratio)) {
+                return (int)(keyHeight * ratio);
+            }
+            return defaultDimens;
+        }
+
+        private static final int selectTextSizeFromKeyboardOrView(final int keyHeight,
+                final float ratioFromKeyboard, final float ratioFromView) {
+            final float ratio = ResourceUtils.isValidFraction(ratioFromKeyboard)
+                    ? ratioFromKeyboard : ratioFromView;
+            return (int)(keyHeight * ratio);
+        }
+
         public void blendAlpha(final Paint paint) {
             final int color = paint.getColor();
             paint.setARGB((paint.getAlpha() * mAnimAlpha) / Constants.Color.ALPHA_OPAQUE,
diff --git a/java/src/com/android/inputmethod/latin/ResourceUtils.java b/java/src/com/android/inputmethod/latin/ResourceUtils.java
index b3d7ac5bf2..5021ad3842 100644
--- a/java/src/com/android/inputmethod/latin/ResourceUtils.java
+++ b/java/src/com/android/inputmethod/latin/ResourceUtils.java
@@ -55,6 +55,16 @@ public final class ResourceUtils {
         return fraction >= 0.0f;
     }
 
+    // {@link Resources#getDimensionPixelSize(int)} returns at least one pixel size.
+    public static boolean isValidDimensionPixelSize(final int dimension) {
+        return dimension > 0;
+    }
+
+    // {@link Resources#getDimensionPixelOffset(int)} may return zero pixel offset.
+    public static boolean isValidDimensionPixelOffset(final int dimension) {
+        return dimension >= 0;
+    }
+
     public static float getFraction(final TypedArray a, final int index, final float defValue) {
         final TypedValue value = a.peekValue(index);
         if (value == null || !isFractionValue(value)) {
-- 
GitLab