Remove reference of SubtypeSwitcher and SettingsValues from KeyboardSet

Change-Id: I6c1150eea5f0a931d01578b0157f06e0aff0cc65
diff --git a/java/src/com/android/inputmethod/keyboard/Keyboard.java b/java/src/com/android/inputmethod/keyboard/Keyboard.java
index c1d024c..be38d9e 100644
--- a/java/src/com/android/inputmethod/keyboard/Keyboard.java
+++ b/java/src/com/android/inputmethod/keyboard/Keyboard.java
@@ -440,6 +440,19 @@
         }
     }
 
+    public static String toThemeName(int themeId) {
+        // This should be aligned with theme-*.xml resource files' themeId attribute.
+        switch (themeId) {
+        case 0: return "Basic";
+        case 1: return "BasicHighContrast";
+        case 5: return "IceCreamSandwich";
+        case 6: return "Stone";
+        case 7: return "StoneBold";
+        case 8: return "GingerBread";
+        default: return null;
+        }
+    }
+
     /**
      * Keyboard Building helper.
      *
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSet.java b/java/src/com/android/inputmethod/keyboard/KeyboardSet.java
index a2e784c..4169605 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSet.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSet.java
@@ -17,6 +17,7 @@
 package com.android.inputmethod.keyboard;
 
 import android.content.Context;
+import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.content.res.XmlResourceParser;
@@ -28,8 +29,6 @@
 import com.android.inputmethod.latin.LatinImeLogger;
 import com.android.inputmethod.latin.LocaleUtils;
 import com.android.inputmethod.latin.R;
-import com.android.inputmethod.latin.SettingsValues;
-import com.android.inputmethod.latin.SubtypeSwitcher;
 import com.android.inputmethod.latin.Utils;
 import com.android.inputmethod.latin.XmlParseUtils;
 
@@ -61,6 +60,7 @@
         int mMode;
         int mInputType;
         int mImeOptions;
+        boolean mTouchPositionCorrectionEnabled;
         boolean mSettingsKeyEnabled;
         boolean mVoiceKeyEnabled;
         boolean mVoiceKeyOnMain;
@@ -115,9 +115,8 @@
         return Builder.getKeyboardId(elementState, false, mParams);
     }
 
-    private static Keyboard getKeyboard(Context context, int xmlId, KeyboardId id) {
+    private Keyboard getKeyboard(Context context, int xmlId, KeyboardId id) {
         final Resources res = context.getResources();
-        final SubtypeSwitcher subtypeSwitcher = SubtypeSwitcher.getInstance();
         final SoftReference<Keyboard> ref = sKeyboardCache.get(id);
         Keyboard keyboard = (ref == null) ? null : ref.get();
         if (keyboard == null) {
@@ -126,9 +125,7 @@
                 final Keyboard.Builder<Keyboard.Params> builder =
                         new Keyboard.Builder<Keyboard.Params>(context, new Keyboard.Params());
                 builder.load(xmlId, id);
-                builder.setTouchPositionCorrectionEnabled(
-                        subtypeSwitcher.currentSubtypeContainsExtraValueKey(
-                                LatinIME.SUBTYPE_EXTRA_VALUE_SUPPORT_TOUCH_POSITION_CORRECTION));
+                builder.setTouchPositionCorrectionEnabled(mParams.mTouchPositionCorrectionEnabled);
                 keyboard = builder.build();
             } finally {
                 LocaleUtils.setSystemLocale(res, savedLocale);
@@ -151,15 +148,17 @@
 
     public static class Builder {
         private final Context mContext;
+        private final String mPackageName;
         private final Resources mResources;
+        private final EditorInfo mEditorInfo;
 
         private final Params mParams = new Params();
 
-        public Builder(Context context, EditorInfo editorInfo, SettingsValues settingsValues) {
+        public Builder(Context context, EditorInfo editorInfo) {
             mContext = context;
+            mPackageName = context.getPackageName();
             mResources = context.getResources();
-            final SubtypeSwitcher subtypeSwitcher = SubtypeSwitcher.getInstance();
-            final String packageName = context.getPackageName();
+            mEditorInfo = editorInfo;
             final Params params = mParams;
 
             params.mMode = Utils.getKeyboardMode(editorInfo);
@@ -167,27 +166,45 @@
                 params.mInputType = editorInfo.inputType;
                 params.mImeOptions = editorInfo.imeOptions;
             }
-            params.mSettingsKeyEnabled = settingsValues.isSettingsKeyEnabled();
+            params.mNoSettingsKey = Utils.inPrivateImeOptions(
+                    mPackageName, LatinIME.IME_OPTION_NO_SETTINGS_KEY, mEditorInfo);
+        }
+
+        public Builder setScreenGeometry(int orientation, int widthPixels) {
+            mParams.mOrientation = orientation;
+            mParams.mWidth = widthPixels;
+            return this;
+        }
+
+        // TODO: Use InputMethodSubtype object as argument.
+        public Builder setSubtype(Locale inputLocale, boolean asciiCapable,
+                boolean touchPositionCorrectionEnabled) {
+            final boolean forceAscii = Utils.inPrivateImeOptions(
+                    mPackageName, LatinIME.IME_OPTION_FORCE_ASCII, mEditorInfo);
+            mParams.mLocale = (forceAscii && !asciiCapable) ? Locale.US : inputLocale;
+            mParams.mTouchPositionCorrectionEnabled = touchPositionCorrectionEnabled;
+            return this;
+        }
+
+        public Builder setOptions(boolean settingsKeyEnabled, boolean voiceKeyEnabled,
+                boolean voiceKeyOnMain) {
+            mParams.mSettingsKeyEnabled = settingsKeyEnabled;
             @SuppressWarnings("deprecation")
             final boolean noMicrophone = Utils.inPrivateImeOptions(
-                    packageName, LatinIME.IME_OPTION_NO_MICROPHONE, editorInfo)
+                    mPackageName, LatinIME.IME_OPTION_NO_MICROPHONE, mEditorInfo)
                     || Utils.inPrivateImeOptions(
-                            null, LatinIME.IME_OPTION_NO_MICROPHONE_COMPAT, editorInfo);
-            params.mVoiceKeyEnabled = settingsValues.isVoiceKeyEnabled(editorInfo) && !noMicrophone;
-            params.mVoiceKeyOnMain = settingsValues.isVoiceKeyOnMain();
-            params.mNoSettingsKey = Utils.inPrivateImeOptions(
-                    packageName, LatinIME.IME_OPTION_NO_SETTINGS_KEY, editorInfo);
-            final boolean forceAscii = Utils.inPrivateImeOptions(
-                    packageName, LatinIME.IME_OPTION_FORCE_ASCII, editorInfo);
-            final boolean asciiCapable = subtypeSwitcher.currentSubtypeContainsExtraValueKey(
-                    LatinIME.SUBTYPE_EXTRA_VALUE_ASCII_CAPABLE);
-            params.mLocale = (forceAscii && !asciiCapable)
-                    ? Locale.US : subtypeSwitcher.getInputLocale();
-            params.mOrientation = mResources.getConfiguration().orientation;
-            params.mWidth = mResources.getDisplayMetrics().widthPixels;
+                            null, LatinIME.IME_OPTION_NO_MICROPHONE_COMPAT, mEditorInfo);
+            mParams.mVoiceKeyEnabled = voiceKeyEnabled && !noMicrophone;
+            mParams.mVoiceKeyOnMain = voiceKeyOnMain;
+            return this;
         }
 
         public KeyboardSet build() {
+            if (mParams.mOrientation == Configuration.ORIENTATION_UNDEFINED)
+                throw new RuntimeException("Screen geometry is not specified");
+            if (mParams.mLocale == null)
+                throw new RuntimeException("KeyboardSet subtype is not specified");
+
             final Locale savedLocale = LocaleUtils.setSystemLocale(mResources, mParams.mLocale);
             try {
                 parseKeyboardSet(mResources, R.xml.keyboard_set);
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
index e509715..2add292 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardSwitcher.java
@@ -117,8 +117,20 @@
     }
 
     public void loadKeyboard(EditorInfo editorInfo, SettingsValues settingsValues) {
-        mKeyboardSet = new KeyboardSet.Builder(mThemeContext, editorInfo, settingsValues)
-                .build();
+        final KeyboardSet.Builder builder = new KeyboardSet.Builder(mThemeContext, editorInfo);
+        builder.setScreenGeometry(mThemeContext.getResources().getConfiguration().orientation,
+                mThemeContext.getResources().getDisplayMetrics().widthPixels);
+        builder.setSubtype(
+                mSubtypeSwitcher.getInputLocale(),
+                mSubtypeSwitcher.currentSubtypeContainsExtraValueKey(
+                        LatinIME.SUBTYPE_EXTRA_VALUE_ASCII_CAPABLE),
+                mSubtypeSwitcher.currentSubtypeContainsExtraValueKey(
+                        LatinIME.SUBTYPE_EXTRA_VALUE_SUPPORT_TOUCH_POSITION_CORRECTION));
+        builder.setOptions(
+                settingsValues.isSettingsKeyEnabled(),
+                settingsValues.isVoiceKeyEnabled(editorInfo),
+                settingsValues.isVoiceKeyOnMain());
+        mKeyboardSet = builder.build();
         final KeyboardId mainKeyboardId = mKeyboardSet.getMainKeyboardId();
         try {
             mState.onLoadKeyboard(mResources.getString(R.string.layout_switch_back_symbols),
@@ -418,17 +430,4 @@
             }
         }
     }
-
-    private static String themeName(int themeId) {
-        // This should be aligned with theme-*.xml resource files' themeId attribute.
-        switch (themeId) {
-        case 0: return "Basic";
-        case 1: return "BasicHighContrast";
-        case 5: return "IceCreamSandwich";
-        case 6: return "Stone";
-        case 7: return "StoneBold";
-        case 8: return "GingerBread";
-        default: return null;
-        }
-    }
 }