From 4731b67629e72d6c7cb391e3d188df78ceaff1e7 Mon Sep 17 00:00:00 2001
From: "Tadashi G. Takaoka" <takaoka@google.com>
Date: Mon, 30 Jul 2012 13:35:06 +0900
Subject: [PATCH] Add device form factor to KeyboardId

This is a groundword for optimizing keyboard parsing.

Bug: 6860259
Change-Id: Ie65aa502b18c920e25cf2998b79120b3cc835952
---
 java/res/values-sw600dp/config.xml            |  2 ++
 java/res/values-sw768dp/config.xml            |  2 ++
 java/res/values/config.xml                    |  2 ++
 .../inputmethod/keyboard/KeyboardId.java      | 29 +++++++++++++++----
 .../keyboard/KeyboardLayoutSet.java           | 16 ++++++----
 .../keyboard/KeyboardSwitcher.java            |  5 ++--
 6 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/java/res/values-sw600dp/config.xml b/java/res/values-sw600dp/config.xml
index 619169c52c..e296623b21 100644
--- a/java/res/values-sw600dp/config.xml
+++ b/java/res/values-sw600dp/config.xml
@@ -19,6 +19,8 @@
 -->
 
 <resources>
+    <!-- Device form factor. This value must be aligned with {@link KeyboardId.DEVICE_FORM_FACTOR_TABLET7} -->
+    <integer name="config_device_form_factor">1</integer>
     <bool name="config_enable_show_voice_key_option">false</bool>
     <bool name="config_enable_show_popup_on_keypress_option">false</bool>
     <bool name="config_enable_bigram_suggestions_option">false</bool>
diff --git a/java/res/values-sw768dp/config.xml b/java/res/values-sw768dp/config.xml
index 27cb9ac210..346fa99792 100644
--- a/java/res/values-sw768dp/config.xml
+++ b/java/res/values-sw768dp/config.xml
@@ -19,6 +19,8 @@
 -->
 
 <resources>
+    <!-- Device form factor. This value must be aligned with {@link KeyboardId.DEVICE_FORM_FACTOR_TABLET10} -->
+    <integer name="config_device_form_factor">2</integer>
     <bool name="config_enable_show_voice_key_option">false</bool>
     <bool name="config_enable_show_popup_on_keypress_option">false</bool>
     <bool name="config_enable_bigram_suggestions_option">false</bool>
diff --git a/java/res/values/config.xml b/java/res/values/config.xml
index 50f46c3f56..e5575e7aec 100644
--- a/java/res/values/config.xml
+++ b/java/res/values/config.xml
@@ -19,6 +19,8 @@
 -->
 
 <resources>
+    <!-- Device form factor. This value must be aligned with {@link KeyboardId.DEVICE_FORM_FACTOR_PHONE} -->
+    <integer name="config_device_form_factor">0</integer>
     <bool name="config_use_fullscreen_mode">false</bool>
     <bool name="config_enable_show_voice_key_option">true</bool>
     <bool name="config_enable_show_popup_on_keypress_option">true</bool>
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardId.java b/java/src/com/android/inputmethod/keyboard/KeyboardId.java
index b54c726872..1e5277345f 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardId.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardId.java
@@ -55,10 +55,15 @@ public class KeyboardId {
     public static final int ELEMENT_PHONE_SYMBOLS = 8;
     public static final int ELEMENT_NUMBER = 9;
 
+    public static final int FORM_FACTOR_PHONE = 0;
+    public static final int FORM_FACTOR_TABLET7 = 1;
+    public static final int FORM_FACTOR_TABLET10 = 2;
+
     private static final int IME_ACTION_CUSTOM_LABEL = EditorInfo.IME_MASK_ACTION + 1;
 
     public final InputMethodSubtype mSubtype;
     public final Locale mLocale;
+    public final int mDeviceFormFactor;
     public final int mOrientation;
     public final int mWidth;
     public final int mMode;
@@ -72,11 +77,12 @@ public class KeyboardId {
 
     private final int mHashCode;
 
-    public KeyboardId(int elementId, InputMethodSubtype subtype, int orientation, int width,
-            int mode, EditorInfo editorInfo, boolean clobberSettingsKey, boolean shortcutKeyEnabled,
-            boolean hasShortcutKey, boolean languageSwitchKeyEnabled) {
+    public KeyboardId(int elementId, InputMethodSubtype subtype, int deviceFormFactor,
+            int orientation, int width, int mode, EditorInfo editorInfo, boolean clobberSettingsKey,
+            boolean shortcutKeyEnabled, boolean hasShortcutKey, boolean languageSwitchKeyEnabled) {
         mSubtype = subtype;
         mLocale = SubtypeLocale.getSubtypeLocale(subtype);
+        mDeviceFormFactor = deviceFormFactor;
         mOrientation = orientation;
         mWidth = width;
         mMode = mode;
@@ -94,6 +100,7 @@ public class KeyboardId {
 
     private static int computeHashCode(KeyboardId id) {
         return Arrays.hashCode(new Object[] {
+                id.mDeviceFormFactor,
                 id.mOrientation,
                 id.mElementId,
                 id.mMode,
@@ -115,7 +122,8 @@ public class KeyboardId {
     private boolean equals(KeyboardId other) {
         if (other == this)
             return true;
-        return other.mOrientation == mOrientation
+        return other.mDeviceFormFactor == mDeviceFormFactor
+                && other.mOrientation == mOrientation
                 && other.mElementId == mElementId
                 && other.mMode == mMode
                 && other.mWidth == mWidth
@@ -184,11 +192,11 @@ public class KeyboardId {
 
     @Override
     public String toString() {
-        return String.format("[%s %s:%s %s%d %s %s %s%s%s%s%s%s%s%s]",
+        return String.format("[%s %s:%s %s-%s:%d %s %s %s%s%s%s%s%s%s%s]",
                 elementIdToName(mElementId),
                 mLocale,
                 mSubtype.getExtraValueOf(KEYBOARD_LAYOUT_SET),
-                (mOrientation == 1 ? "port" : "land"), mWidth,
+                deviceFormFactor(mDeviceFormFactor), (mOrientation == 1 ? "port" : "land"), mWidth,
                 modeName(mMode),
                 imeAction(),
                 (navigateNext() ? "navigateNext" : ""),
@@ -226,6 +234,15 @@ public class KeyboardId {
         }
     }
 
+    public static String deviceFormFactor(int devoceFormFactor) {
+        switch (devoceFormFactor) {
+        case FORM_FACTOR_PHONE: return "phone";
+        case FORM_FACTOR_TABLET7: return "tablet7";
+        case FORM_FACTOR_TABLET10: return "tablet10";
+        default: return null;
+        }
+    }
+
     public static String modeName(int mode) {
         switch (mode) {
         case MODE_TEXT: return "text";
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java b/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
index aab89a3e5e..64b3f09524 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
@@ -115,6 +115,7 @@ public class KeyboardLayoutSet {
         boolean mNoSettingsKey;
         boolean mLanguageSwitchKeyEnabled;
         InputMethodSubtype mSubtype;
+        int mDeviceFormFactor;
         int mOrientation;
         int mWidth;
         // Sparse array of KeyboardLayoutSet element parameters indexed by element's id.
@@ -211,9 +212,10 @@ public class KeyboardLayoutSet {
         final boolean noLanguage = SubtypeLocale.isNoLanguage(params.mSubtype);
         final boolean voiceKeyEnabled = params.mVoiceKeyEnabled && !noLanguage;
         final boolean hasShortcutKey = voiceKeyEnabled && (isSymbols != params.mVoiceKeyOnMain);
-        return new KeyboardId(keyboardLayoutSetElementId, params.mSubtype, params.mOrientation,
-                params.mWidth, params.mMode, params.mEditorInfo, params.mNoSettingsKey,
-                voiceKeyEnabled, hasShortcutKey, params.mLanguageSwitchKeyEnabled);
+        return new KeyboardId(keyboardLayoutSetElementId, params.mSubtype, params.mDeviceFormFactor,
+                params.mOrientation, params.mWidth, params.mMode, params.mEditorInfo,
+                params.mNoSettingsKey, voiceKeyEnabled, hasShortcutKey,
+                params.mLanguageSwitchKeyEnabled);
     }
 
     public static class Builder {
@@ -239,9 +241,11 @@ public class KeyboardLayoutSet {
                     mPackageName, NO_SETTINGS_KEY, mEditorInfo);
         }
 
-        public Builder setScreenGeometry(int orientation, int widthPixels) {
-            mParams.mOrientation = orientation;
-            mParams.mWidth = widthPixels;
+        public Builder setScreenGeometry(int deviceFormFactor, int orientation, int widthPixels) {
+            final Params params = mParams;
+            params.mDeviceFormFactor = deviceFormFactor;
+            params.mOrientation = orientation;
+            params.mWidth = widthPixels;
             return this;
         }
 
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index f27d793824..4d952098c0 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -137,8 +137,9 @@ public class KeyboardSwitcher implements KeyboardState.SwitchActions {
     public void loadKeyboard(EditorInfo editorInfo, SettingsValues settingsValues) {
         final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder(
                 mThemeContext, editorInfo);
-        builder.setScreenGeometry(mThemeContext.getResources().getConfiguration().orientation,
-                mThemeContext.getResources().getDisplayMetrics().widthPixels);
+        final Resources res = mThemeContext.getResources();
+        builder.setScreenGeometry(res.getInteger(R.integer.config_device_form_factor),
+                res.getConfiguration().orientation, res.getDisplayMetrics().widthPixels);
         builder.setSubtype(mSubtypeSwitcher.getCurrentSubtype());
         builder.setOptions(
                 settingsValues.isVoiceKeyEnabled(editorInfo),
-- 
GitLab