From 7cc2595b6ffe3a6a3fcea8cccf5987d0e127212b Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Sun, 18 Dec 2011 00:58:47 +0900
Subject: [PATCH] Add LatinKeyboard.spacebarTextRatio attribute

Bug: 5776979
Change-Id: Ia568d1170ed33a000eb3bb02ecdf611e4b5ff2d5
---
 java/res/values-land/dimens.xml               |  1 +
 java/res/values-sw600dp-land/dimens.xml       |  1 +
 java/res/values-sw600dp/config.xml            |  1 -
 java/res/values-sw600dp/dimens.xml            |  1 +
 java/res/values-sw768dp-land/dimens.xml       |  1 +
 java/res/values-sw768dp/config.xml            |  1 -
 java/res/values-sw768dp/dimens.xml            |  1 +
 java/res/values/attrs.xml                     |  3 ++
 java/res/values/config.xml                    |  1 -
 java/res/values/dimens.xml                    |  1 +
 java/res/values/styles.xml                    |  1 +
 .../inputmethod/keyboard/LatinKeyboard.java   | 48 +++++--------------
 12 files changed, 21 insertions(+), 40 deletions(-)

diff --git a/java/res/values-land/dimens.xml b/java/res/values-land/dimens.xml
index 4a5c5a44c7..dcbfe463ed 100644
--- a/java/res/values-land/dimens.xml
+++ b/java/res/values-land/dimens.xml
@@ -53,6 +53,7 @@
     <fraction name="key_hint_label_ratio">52%</fraction>
     <fraction name="key_uppercase_letter_ratio">40%</fraction>
     <fraction name="key_preview_text_ratio">90%</fraction>
+    <fraction name="spacebar_text_ratio">40.000%</fraction>
     <dimen name="key_preview_offset">0.08in</dimen>
 
     <dimen name="key_preview_offset_ics">0.01in</dimen>
diff --git a/java/res/values-sw600dp-land/dimens.xml b/java/res/values-sw600dp-land/dimens.xml
index 1b8c8a64a4..1c725a4845 100644
--- a/java/res/values-sw600dp-land/dimens.xml
+++ b/java/res/values-sw600dp-land/dimens.xml
@@ -48,6 +48,7 @@
     <fraction name="key_hint_letter_ratio">23%</fraction>
     <fraction name="key_hint_label_ratio">34%</fraction>
     <fraction name="key_uppercase_letter_ratio">29%</fraction>
+    <fraction name="spacebar_text_ratio">33.33%</fraction>
 
     <dimen name="suggestions_strip_padding">40.0mm</dimen>
     <integer name="max_more_suggestions_row">5</integer>
diff --git a/java/res/values-sw600dp/config.xml b/java/res/values-sw600dp/config.xml
index 1854a86967..1dd93121d4 100644
--- a/java/res/values-sw600dp/config.xml
+++ b/java/res/values-sw600dp/config.xml
@@ -38,6 +38,5 @@
     <integer name="config_long_press_space_key_timeout">0</integer>
     <!-- This configuration is the index of the array {@link KeyboardSwitcher.KEYBOARD_THEMES}. -->
     <string name="config_default_keyboard_theme_id" translatable="false">5</string>
-    <string name="config_text_size_of_language_on_spacebar" translatable="false">medium</string>
     <integer name="config_max_more_keys_column">5</integer>
 </resources>
diff --git a/java/res/values-sw600dp/dimens.xml b/java/res/values-sw600dp/dimens.xml
index 31830239d7..e393be5791 100644
--- a/java/res/values-sw600dp/dimens.xml
+++ b/java/res/values-sw600dp/dimens.xml
@@ -59,6 +59,7 @@
     <fraction name="key_hint_label_ratio">28%</fraction>
     <fraction name="key_uppercase_letter_ratio">26%</fraction>
     <fraction name="key_preview_text_ratio">50%</fraction>
+    <fraction name="spacebar_text_ratio">32.14%</fraction>
     <dimen name="key_preview_height">15.0mm</dimen>
     <dimen name="key_preview_offset">0.1in</dimen>
 
diff --git a/java/res/values-sw768dp-land/dimens.xml b/java/res/values-sw768dp-land/dimens.xml
index 664e8c159a..ef39f4393e 100644
--- a/java/res/values-sw768dp-land/dimens.xml
+++ b/java/res/values-sw768dp-land/dimens.xml
@@ -52,6 +52,7 @@
     <fraction name="key_hint_letter_ratio">23%</fraction>
     <fraction name="key_hint_label_ratio">28%</fraction>
     <fraction name="key_uppercase_letter_ratio">24%</fraction>
+    <fraction name="spacebar_text_ratio">24.00%</fraction>
     <dimen name="key_preview_height">17.0mm</dimen>
 
     <dimen name="key_preview_height_ics">26.5mm</dimen>
diff --git a/java/res/values-sw768dp/config.xml b/java/res/values-sw768dp/config.xml
index c25139a42e..06553a7c99 100644
--- a/java/res/values-sw768dp/config.xml
+++ b/java/res/values-sw768dp/config.xml
@@ -36,7 +36,6 @@
     <integer name="config_long_press_space_key_timeout">0</integer>
     <!-- This configuration is the index of the array {@link KeyboardSwitcher.KEYBOARD_THEMES}. -->
     <string name="config_default_keyboard_theme_id" translatable="false">5</string>
-    <string name="config_text_size_of_language_on_spacebar" translatable="false">medium</string>
     <integer name="config_max_more_keys_column">5</integer>
     <!--  Screen metrics for logging.
             0 = "mdpi phone screen"
diff --git a/java/res/values-sw768dp/dimens.xml b/java/res/values-sw768dp/dimens.xml
index bb4937dd57..dbbd844f0c 100644
--- a/java/res/values-sw768dp/dimens.xml
+++ b/java/res/values-sw768dp/dimens.xml
@@ -62,6 +62,7 @@
     <fraction name="key_hint_label_ratio">28%</fraction>
     <fraction name="key_uppercase_letter_ratio">26%</fraction>
     <fraction name="key_preview_text_ratio">50%</fraction>
+    <fraction name="spacebar_text_ratio">29.03%</fraction>
     <dimen name="key_preview_height">15.0mm</dimen>
     <dimen name="key_preview_offset">0.1in</dimen>
 
diff --git a/java/res/values/attrs.xml b/java/res/values/attrs.xml
index d08564444a..2e8a0030e6 100644
--- a/java/res/values/attrs.xml
+++ b/java/res/values/attrs.xml
@@ -321,10 +321,13 @@
         <attr name="parentStyle" format="string" />
     </declare-styleable>
 
+    <!-- Move these attributes to LatinKeyboardView -->
     <declare-styleable name="LatinKeyboard">
         <attr name="autoCorrectionSpacebarLedEnabled" format="boolean" />
         <attr name="autoCorrectionSpacebarLedIcon" format="reference" />
         <attr name="disabledShortcutIcon" format="reference" />
+        <!-- Size of the text for spacebar language label, in the proportion of key height. -->
+        <attr name="spacebarTextRatio" format="fraction" />
         <attr name="spacebarTextColor" format="color" />
         <attr name="spacebarTextShadowColor" format="color" />
     </declare-styleable>
diff --git a/java/res/values/config.xml b/java/res/values/config.xml
index 8b99a1fcb1..55f35f0c5d 100644
--- a/java/res/values/config.xml
+++ b/java/res/values/config.xml
@@ -66,7 +66,6 @@
     <dimen name="config_touch_noise_threshold_distance">2.0mm</dimen>
     <!-- This configuration is the index of the array {@link KeyboardSwitcher.KEYBOARD_THEMES}. -->
     <string name="config_default_keyboard_theme_id" translatable="false">5</string>
-    <string name="config_text_size_of_language_on_spacebar" translatable="false">small</string>
     <integer name="config_max_more_keys_column">5</integer>
     <string-array name="auto_correction_threshold_values" translatable="false">
         <!-- Off, When auto correction setting is Off, this value is not used. -->
diff --git a/java/res/values/dimens.xml b/java/res/values/dimens.xml
index 352141ca6e..e46ff7718f 100644
--- a/java/res/values/dimens.xml
+++ b/java/res/values/dimens.xml
@@ -66,6 +66,7 @@
     <fraction name="key_hint_label_ratio">44%</fraction>
     <fraction name="key_uppercase_letter_ratio">35%</fraction>
     <fraction name="key_preview_text_ratio">82%</fraction>
+    <fraction name="spacebar_text_ratio">33.735%</fraction>
     <dimen name="key_preview_height">80sp</dimen>
     <dimen name="key_preview_offset">0.1in</dimen>
 
diff --git a/java/res/values/styles.xml b/java/res/values/styles.xml
index 2025a7dac5..ed8795ade4 100644
--- a/java/res/values/styles.xml
+++ b/java/res/values/styles.xml
@@ -33,6 +33,7 @@
     </style>
     <style name="LatinKeyboard">
         <item name="autoCorrectionSpacebarLedEnabled">true</item>
+        <item name="spacebarTextRatio">@fraction/spacebar_text_ratio</item>
         <item name="spacebarTextColor">#FFC0C0C0</item>
         <item name="spacebarTextShadowColor">#80000000</item>
     </style>
diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java
index d34e4108b2..78a02547bb 100644
--- a/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboard.java
@@ -18,7 +18,6 @@ package com.android.inputmethod.keyboard;
 
 import android.content.Context;
 import android.content.res.Resources;
-import android.content.res.Resources.Theme;
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
@@ -45,13 +44,13 @@ public class LatinKeyboard extends Keyboard {
     private static final int SPACE_LED_LENGTH_PERCENT = 80;
 
     private final Resources mRes;
-    private final Theme mTheme;
 
     /* Space key and its icons, drawables and colors. */
     private final Key mSpaceKey;
     private final Drawable mSpaceIcon;
     private final boolean mAutoCorrectionSpacebarLedEnabled;
     private final Drawable mAutoCorrectionSpacebarLedIcon;
+    private final float mSpacebarTextSize;
     private final int mSpacebarTextColor;
     private final int mSpacebarTextShadowColor;
     private final HashMap<Integer, BitmapDrawable> mSpaceDrawableCache =
@@ -74,13 +73,9 @@ public class LatinKeyboard extends Keyboard {
     // its short language name will be used instead.
     private static final float MINIMUM_SCALE_OF_LANGUAGE_NAME = 0.8f;
 
-    private static final String SMALL_TEXT_SIZE_OF_LANGUAGE_ON_SPACEBAR = "small";
-    private static final String MEDIUM_TEXT_SIZE_OF_LANGUAGE_ON_SPACEBAR = "medium";
-
     private LatinKeyboard(Context context, KeyboardParams params) {
         super(params);
         mRes = context.getResources();
-        mTheme = context.getTheme();
 
         // The index of space key is available only after Keyboard constructor has finished.
         mSpaceKey = getKey(CODE_SPACE);
@@ -99,6 +94,10 @@ public class LatinKeyboard extends Keyboard {
         mAutoCorrectionSpacebarLedIcon = a.getDrawable(
                 R.styleable.LatinKeyboard_autoCorrectionSpacebarLedIcon);
         mDisabledShortcutIcon = a.getDrawable(R.styleable.LatinKeyboard_disabledShortcutIcon);
+        final float spacebarTextRatio = a.getFraction(R.styleable.LatinKeyboard_spacebarTextRatio,
+                1000, 1000, 1) / 1000.0f;
+        final int keyHeight = mMostCommonKeyHeight - mVerticalGap;
+        mSpacebarTextSize = keyHeight * spacebarTextRatio;
         mSpacebarTextColor = a.getColor(R.styleable.LatinKeyboard_spacebarTextColor, 0);
         mSpacebarTextShadowColor = a.getColor(
                 R.styleable.LatinKeyboard_spacebarTextShadowColor, 0);
@@ -116,6 +115,8 @@ public class LatinKeyboard extends Keyboard {
         }
     }
 
+    // TODO: Move this drawing method to LatinKeyboardView.
+    // TODO: Use Key.keyLabel to draw language name of spacebar.
     public Key updateSpacebarLanguage(float fadeFactor, boolean multipleEnabledIMEsOrSubtypes,
             boolean needsToDisplayLanguage) {
         mSpacebarTextFadeFactor = fadeFactor;
@@ -131,6 +132,7 @@ public class LatinKeyboard extends Keyboard {
         return newColor;
     }
 
+    // TODO: Move this drawing method to LatinKeyboardView.
     public Key updateShortcutKey(boolean available) {
         if (mShortcutKey == null)
             return null;
@@ -139,10 +141,12 @@ public class LatinKeyboard extends Keyboard {
         return mShortcutKey;
     }
 
+    // TODO: Get rid of this method
     public boolean needsAutoCorrectionSpacebarLed() {
         return mAutoCorrectionSpacebarLedEnabled;
     }
 
+    // TODO: Move this drawing method to LatinKeyboardView.
     /**
      * @return a key which should be invalidated.
      */
@@ -238,7 +242,6 @@ public class LatinKeyboard extends Keyboard {
         final int height = mSpaceIcon != null ? mSpaceIcon.getIntrinsicHeight() : mSpaceKey.mHeight;
         final Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
         final Canvas canvas = new Canvas(buffer);
-        final Resources res = mRes;
         canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
 
         // If application locales are explicitly selected.
@@ -247,25 +250,7 @@ public class LatinKeyboard extends Keyboard {
             paint.setAntiAlias(true);
             paint.setTextAlign(Align.CENTER);
 
-            final String textSizeOfLanguageOnSpacebar = res.getString(
-                    R.string.config_text_size_of_language_on_spacebar,
-                    SMALL_TEXT_SIZE_OF_LANGUAGE_ON_SPACEBAR);
-            final int textStyle;
-            final int defaultTextSize;
-            if (MEDIUM_TEXT_SIZE_OF_LANGUAGE_ON_SPACEBAR.equals(textSizeOfLanguageOnSpacebar)) {
-                // TODO: TextAppearance_Medium depends on the current system wide font size
-                // settings.  Probably should stop using this.
-                textStyle = android.R.style.TextAppearance_Medium;
-                defaultTextSize = 18;
-            } else {
-                // TODO: TextAppearance_Small depends on the current system wide font size
-                // settings.  Probably should stop using this.
-                textStyle = android.R.style.TextAppearance_Small;
-                defaultTextSize = 14;
-            }
-
-            final String language = layoutSpacebar(paint, inputLocale, width, getTextSizeFromTheme(
-                    mTheme, textStyle, defaultTextSize));
+            final String language = layoutSpacebar(paint, inputLocale, width, mSpacebarTextSize);
 
             // Draw language text with shadow
             // In case there is no space icon, we will place the language text at the center of
@@ -305,15 +290,4 @@ public class LatinKeyboard extends Keyboard {
         return super.getNearestKeys(Math.max(0, Math.min(x, mOccupiedWidth - 1)),
                 Math.max(0, Math.min(y, mOccupiedHeight - 1)));
     }
-
-    private static final int[] ATTR_TEXT_SIZE = { android.R.attr.textSize };
-
-    private static int getTextSizeFromTheme(Theme theme, int style, int defValue) {
-        // TODO: This method should return text size that is independent from the current
-        // system wide font size settings.
-        final TypedArray a = theme.obtainStyledAttributes(style, ATTR_TEXT_SIZE);
-        final int textSize = a.getDimensionPixelSize(a.getResourceId(0, 0), defValue);
-        a.recycle();
-        return textSize;
-    }
 }
-- 
GitLab