Refatcor InputMethodSubtype related stuff a bit

Change-Id: Iaded72331660afbaeddda085f2b633b681d4b6df
diff --git a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java
index ffed820..0438452 100644
--- a/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java
+++ b/java/src/com/android/inputmethod/compat/InputMethodManagerCompatWrapper.java
@@ -17,7 +17,6 @@
 package com.android.inputmethod.compat;
 
 import android.content.Context;
-import android.inputmethodservice.InputMethodService;
 import android.os.IBinder;
 import android.util.Log;
 import android.view.inputmethod.InputMethodInfo;
@@ -50,9 +49,8 @@
         return sInstance;
     }
 
-    public static void init(InputMethodService service) {
-        sInstance.mImm = (InputMethodManager) service.getSystemService(
-                Context.INPUT_METHOD_SERVICE);
+    public static void init(Context context) {
+        sInstance.mImm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
     }
 
     public InputMethodSubtype getCurrentInputMethodSubtype() {
@@ -86,6 +84,11 @@
                 onlyCurrentIme);
     }
 
+    public List<InputMethodInfo> getInputMethodList() {
+        if (mImm == null) return null;
+        return mImm.getInputMethodList();
+    }
+
     public List<InputMethodInfo> getEnabledInputMethodList() {
         if (mImm == null) return null;
         return mImm.getEnabledInputMethodList();
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java b/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
index 803a294..6d56c5c 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardLayoutSet.java
@@ -32,7 +32,6 @@
 import com.android.inputmethod.latin.InputTypeUtils;
 import com.android.inputmethod.latin.LatinIME;
 import com.android.inputmethod.latin.LatinImeLogger;
-import com.android.inputmethod.latin.LocaleUtils;
 import com.android.inputmethod.latin.R;
 import com.android.inputmethod.latin.StringUtils;
 import com.android.inputmethod.latin.SubtypeLocale;
@@ -61,8 +60,6 @@
     private static final String TAG_KEYBOARD_SET = "KeyboardLayoutSet";
     private static final String TAG_ELEMENT = "Element";
 
-    private static final String DEFAULT_KEYBOARD_LAYOUT_SET = "qwerty";
-    private static final char KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER = ':';
     private static final String KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX = "keyboard_layout_set_";
 
     private final Context mContext;
@@ -164,14 +161,13 @@
         }
         final KeyboardId id = getKeyboardId(keyboardLayoutSetElementId);
         try {
-            return getKeyboard(mContext, elementParams, id);
+            return getKeyboard(elementParams, id);
         } catch (RuntimeException e) {
             throw new KeyboardLayoutSetException(e, id);
         }
     }
 
-    private Keyboard getKeyboard(Context context, ElementParams elementParams,
-            final KeyboardId id) {
+    private Keyboard getKeyboard(ElementParams elementParams, final KeyboardId id) {
         final SoftReference<Keyboard> ref = sKeyboardCache.get(id);
         Keyboard keyboard = (ref == null) ? null : ref.get();
         if (keyboard == null) {
@@ -215,30 +211,6 @@
                 voiceKeyEnabled, hasShortcutKey, params.mLanguageSwitchKeyEnabled);
     }
 
-    private static String getKeyboardLayoutSetName(InputMethodSubtype subtype) {
-        final String keyboardLayoutSet = subtype.getExtraValueOf(
-                LatinIME.SUBTYPE_EXTRA_VALUE_KEYBOARD_LAYOUT_SET);
-        // TODO: Remove this null check when InputMethodManager.getCurrentInputMethodSubtype is
-        // fixed.
-        if (keyboardLayoutSet == null) return DEFAULT_KEYBOARD_LAYOUT_SET;
-        final int pos = keyboardLayoutSet.indexOf(KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER);
-        return (pos > 0) ? keyboardLayoutSet.substring(0, pos) : keyboardLayoutSet;
-    }
-
-    public static String getKeyboardLayoutSetLocaleString(InputMethodSubtype subtype) {
-        final String keyboardLayoutSet = subtype.getExtraValueOf(
-                LatinIME.SUBTYPE_EXTRA_VALUE_KEYBOARD_LAYOUT_SET);
-        // TODO: Remove this null check when InputMethodManager.getCurrentInputMethodSubtype is
-        // fixed.
-        if (keyboardLayoutSet == null) return subtype.getLocale();
-        final int pos = keyboardLayoutSet.indexOf(KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER);
-        return (pos > 0) ? keyboardLayoutSet.substring(pos + 1) : subtype.getLocale();
-    }
-
-    public static Locale getKeyboardLayoutSetLocale(InputMethodSubtype subtype) {
-        return LocaleUtils.constructLocaleFromString(getKeyboardLayoutSetLocaleString(subtype));
-    }
-
     public static class Builder {
         private final Context mContext;
         private final String mPackageName;
@@ -279,9 +251,9 @@
             final InputMethodSubtype keyboardSubtype = (forceAscii && !asciiCapable)
                     ? SubtypeSwitcher.getInstance().getNoLanguageSubtype()
                     : subtype;
-            mParams.mLocale = getKeyboardLayoutSetLocale(keyboardSubtype);
+            mParams.mLocale = SubtypeLocale.getKeyboardLayoutSetLocale(keyboardSubtype);
             mParams.mKeyboardLayoutSetName = KEYBOARD_LAYOUT_SET_RESOURCE_PREFIX
-                    + getKeyboardLayoutSetName(keyboardSubtype);
+                    + SubtypeLocale.getKeyboardLayoutSetName(keyboardSubtype);
             return this;
         }
 
diff --git a/java/src/com/android/inputmethod/latin/SubtypeLocale.java b/java/src/com/android/inputmethod/latin/SubtypeLocale.java
index 2bc22a6..05f9168 100644
--- a/java/src/com/android/inputmethod/latin/SubtypeLocale.java
+++ b/java/src/com/android/inputmethod/latin/SubtypeLocale.java
@@ -18,6 +18,8 @@
 
 import android.content.Context;
 import android.content.res.Resources;
+import android.view.inputmethod.InputMethodSubtype;
+
 
 
 import java.util.Locale;
@@ -33,6 +35,9 @@
     private static String[] sExceptionKeys;
     private static String[] sExceptionValues;
 
+    private static final String DEFAULT_KEYBOARD_LAYOUT_SET = "qwerty";
+    private static final char KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER = ':';
+
     private SubtypeLocale() {
         // Intentional empty constructor for utility class.
     }
@@ -72,7 +77,8 @@
             return StringUtils.toTitleCase(locale.getDisplayName(locale), locale);
         }
         if (value.indexOf("%s") >= 0) {
-            final String languageName = StringUtils.toTitleCase(locale.getDisplayLanguage(locale), locale);
+            final String languageName = StringUtils.toTitleCase(
+                    locale.getDisplayLanguage(locale), locale);
             return String.format(value, languageName);
         }
         return value;
@@ -105,4 +111,28 @@
             return StringUtils.toTitleCase(locale.getLanguage(), locale);
         }
     }
+
+    public static String getKeyboardLayoutSetName(InputMethodSubtype subtype) {
+        final String keyboardLayoutSet = subtype.getExtraValueOf(
+                LatinIME.SUBTYPE_EXTRA_VALUE_KEYBOARD_LAYOUT_SET);
+        // TODO: Remove this null check when InputMethodManager.getCurrentInputMethodSubtype is
+        // fixed.
+        if (keyboardLayoutSet == null) return DEFAULT_KEYBOARD_LAYOUT_SET;
+        final int pos = keyboardLayoutSet.indexOf(KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER);
+        return (pos > 0) ? keyboardLayoutSet.substring(0, pos) : keyboardLayoutSet;
+    }
+
+    public static String getKeyboardLayoutSetLocaleString(InputMethodSubtype subtype) {
+        final String keyboardLayoutSet = subtype.getExtraValueOf(
+                LatinIME.SUBTYPE_EXTRA_VALUE_KEYBOARD_LAYOUT_SET);
+        // TODO: Remove this null check when InputMethodManager.getCurrentInputMethodSubtype is
+        // fixed.
+        if (keyboardLayoutSet == null) return subtype.getLocale();
+        final int pos = keyboardLayoutSet.indexOf(KEYBOARD_LAYOUT_SET_LOCALE_DELIMITER);
+        return (pos > 0) ? keyboardLayoutSet.substring(pos + 1) : subtype.getLocale();
+    }
+
+    public static Locale getKeyboardLayoutSetLocale(InputMethodSubtype subtype) {
+        return LocaleUtils.constructLocaleFromString(getKeyboardLayoutSetLocaleString(subtype));
+    }
 }
diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
index 99f0ee1..2ba7423 100644
--- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
+++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
@@ -31,7 +31,6 @@
 import android.view.inputmethod.InputMethodSubtype;
 
 import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
-import com.android.inputmethod.keyboard.KeyboardLayoutSet;
 import com.android.inputmethod.keyboard.KeyboardSwitcher;
 
 import java.util.ArrayList;
@@ -105,8 +104,8 @@
         mInputLocaleStr = null;
         mCurrentSubtype = mImm.getCurrentInputMethodSubtype();
         mAllEnabledSubtypesOfCurrentInputMethod = null;
-        mNoLanguageSubtype = SubtypeUtils.findSubtypeByKeyboardLayoutSetLocale(
-                service, SubtypeLocale.LOCALE_NO_LANGUAGE_QWERTY);
+        mNoLanguageSubtype = SubtypeUtils.findSubtypeByLocaleAndKeyboardLayoutSet(
+                service, SubtypeLocale.LOCALE_NO_LANGUAGE_QWERTY, "qwerty");
 
         final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
         mIsNetworkConnected = (info != null && info.isConnected());
@@ -129,14 +128,14 @@
 
     // Reload enabledSubtypes from the framework.
     private void updateEnabledSubtypes() {
-        final String currentMode = getCurrentSubtypeMode();
+        final String currentMode = mCurrentSubtype.getMode();
         boolean foundCurrentSubtypeBecameDisabled = true;
         mAllEnabledSubtypesOfCurrentInputMethod = mImm.getEnabledInputMethodSubtypeList(
                 null, true);
         mEnabledLanguagesOfCurrentInputMethod.clear();
         mEnabledKeyboardSubtypesOfCurrentInputMethod.clear();
         for (InputMethodSubtype ims : mAllEnabledSubtypesOfCurrentInputMethod) {
-            final String locale = KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(ims);
+            final String locale = SubtypeLocale.getKeyboardLayoutSetLocaleString(ims);
             final String mode = ims.getMode();
             mLocaleSplitter.setString(locale);
             if (mLocaleSplitter.hasNext()) {
@@ -166,7 +165,7 @@
                     + (mShortcutInputMethodInfo == null
                             ? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
                     + (mShortcutSubtype == null ? "<null>" : (
-                            KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(mShortcutSubtype)
+                            SubtypeLocale.getKeyboardLayoutSetLocaleString(mShortcutSubtype)
                             + ", " + mShortcutSubtype.getMode())));
         }
         // TODO: Update an icon for shortcut IME
@@ -189,16 +188,16 @@
                     + (mShortcutInputMethodInfo == null
                             ? "<null>" : mShortcutInputMethodInfo.getId()) + ", "
                     + (mShortcutSubtype == null ? "<null>" : (
-                            KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(mShortcutSubtype)
+                            SubtypeLocale.getKeyboardLayoutSetLocaleString(mShortcutSubtype)
                             + ", " + mShortcutSubtype.getMode())));
         }
     }
 
     // Update the current subtype. LatinIME.onCurrentInputMethodSubtypeChanged calls this function.
     public void updateSubtype(InputMethodSubtype newSubtype) {
-        final String newLocale = KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(newSubtype);
+        final String newLocale = SubtypeLocale.getKeyboardLayoutSetLocaleString(newSubtype);
         final String newMode = newSubtype.getMode();
-        final String oldMode = getCurrentSubtypeMode();
+        final String oldMode = mCurrentSubtype.getMode();
         if (DBG) {
             Log.w(TAG, "Update subtype to:" + newLocale + "," + newMode
                     + ", from: " + mInputLocaleStr + ", " + oldMode);
@@ -218,7 +217,7 @@
         }
         mCurrentSubtype = newSubtype;
 
-        if (isKeyboardMode()) {
+        if (KEYBOARD_MODE.equals(mCurrentSubtype.getMode())) {
             if (modeChanged || languageChanged) {
                 updateShortcutIME();
                 mService.onRefreshKeyboard();
@@ -233,12 +232,10 @@
             }
             Log.w(TAG, "Unknown subtype mode: " + newMode + "," + version + ", " + packageName
                     + ". IME is already changed to other IME.");
-            if (newSubtype != null) {
-                Log.w(TAG, "Subtype mode:" + newSubtype.getMode());
-                Log.w(TAG, "Subtype locale:" + newSubtype.getLocale());
-                Log.w(TAG, "Subtype extra value:" + newSubtype.getExtraValue());
-                Log.w(TAG, "Subtype is auxiliary:" + newSubtype.isAuxiliary());
-            }
+            Log.w(TAG, "Subtype mode:" + newSubtype.getMode());
+            Log.w(TAG, "Subtype locale:" + newSubtype.getLocale());
+            Log.w(TAG, "Subtype extra value:" + newSubtype.getExtraValue());
+            Log.w(TAG, "Subtype is auxiliary:" + newSubtype.isAuxiliary());
         }
     }
 
@@ -282,10 +279,11 @@
         if (token == null) {
             return;
         }
+        final InputMethodManagerCompatWrapper imm = mImm;
         new AsyncTask<Void, Void, Void>() {
             @Override
             protected Void doInBackground(Void... params) {
-                mImm.setInputMethodAndSubtype(token, imiId, subtype);
+                imm.setInputMethodAndSubtype(token, imiId, subtype);
                 return null;
             }
         }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
@@ -385,22 +383,6 @@
         return mIsDictionaryAvailable;
     }
 
-    // TODO: Remove this method
-    private boolean isKeyboardMode() {
-        return KEYBOARD_MODE.equals(getCurrentSubtypeMode());
-    }
-
-    // TODO: Remove this method
-    private String getCurrentSubtypeMode() {
-        return mCurrentSubtype.getMode();
-    }
-
-    // TODO: Remove this method
-    public boolean currentSubtypeContainsExtraValueKey(String key) {
-        // If null, return what an empty ExtraValue would return : false.
-        return mCurrentSubtype.containsExtraValueKey(key);
-    }
-
     public InputMethodSubtype getCurrentSubtype() {
         return mCurrentSubtype;
     }
diff --git a/java/src/com/android/inputmethod/latin/SubtypeUtils.java b/java/src/com/android/inputmethod/latin/SubtypeUtils.java
index a747c9a..4ad72fb 100644
--- a/java/src/com/android/inputmethod/latin/SubtypeUtils.java
+++ b/java/src/com/android/inputmethod/latin/SubtypeUtils.java
@@ -21,7 +21,6 @@
 import android.view.inputmethod.InputMethodSubtype;
 
 import com.android.inputmethod.compat.InputMethodManagerCompatWrapper;
-import com.android.inputmethod.keyboard.KeyboardLayoutSet;
 
 import java.util.Collections;
 import java.util.List;
@@ -125,21 +124,22 @@
             throw new RuntimeException("Input method manager not found");
         }
 
-        for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) {
+        for (final InputMethodInfo imi : imm.getInputMethodList()) {
             if (imi.getPackageName().equals(packageName))
                 return imi;
         }
         throw new RuntimeException("Can not find input method id for " + packageName);
     }
 
-    public static InputMethodSubtype findSubtypeByKeyboardLayoutSetLocale(
-            Context context, Locale locale) {
+    public static InputMethodSubtype findSubtypeByLocaleAndKeyboardLayoutSet(
+            Context context, Locale locale, String keyoardLayoutSet) {
         final String localeString = locale.toString();
-        final InputMethodInfo imi = SubtypeUtils.getInputMethodInfo(context.getPackageName());
+        final InputMethodInfo imi = getInputMethodInfo(context.getPackageName());
         final int count = imi.getSubtypeCount();
         for (int i = 0; i < count; i++) {
             final InputMethodSubtype subtype = imi.getSubtypeAt(i);
-            if (localeString.equals(KeyboardLayoutSet.getKeyboardLayoutSetLocaleString(subtype))) {
+            final String layout = SubtypeLocale.getKeyboardLayoutSetName(subtype);
+            if (localeString.equals(subtype.getLocale()) && keyoardLayoutSet.equals(layout)) {
                 return subtype;
             }
         }
diff --git a/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java b/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java
index 4ac7657..911429d 100644
--- a/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java
+++ b/tests/src/com/android/inputmethod/latin/SubtypeLocaleTests.java
@@ -22,8 +22,6 @@
 import android.view.inputmethod.InputMethodManager;
 import android.view.inputmethod.InputMethodSubtype;
 
-import com.android.inputmethod.keyboard.KeyboardLayoutSet;
-
 import java.util.ArrayList;
 import java.util.Locale;
 
@@ -64,7 +62,7 @@
         final StringBuilder messages = new StringBuilder();
         int failedCount = 0;
         for (final InputMethodSubtype subtype : mSubtypesList) {
-            final Locale locale = KeyboardLayoutSet.getKeyboardLayoutSetLocale(subtype);
+            final Locale locale = SubtypeLocale.getKeyboardLayoutSetLocale(subtype);
             if (locale.getLanguage().equals(SubtypeLocale.NO_LANGUAGE)) {
                 // This is special language name for language agnostic usage.
                 continue;
@@ -94,7 +92,7 @@
         final StringBuilder messages = new StringBuilder();
         int failedCount = 0;
         for (final InputMethodSubtype subtype : mSubtypesList) {
-            final Locale locale = KeyboardLayoutSet.getKeyboardLayoutSetLocale(subtype);
+            final Locale locale = SubtypeLocale.getKeyboardLayoutSetLocale(subtype);
             if (locale.getLanguage().equals(SubtypeLocale.NO_LANGUAGE)) {
                 // This is special language name for language agnostic usage.
                 continue;
@@ -121,7 +119,7 @@
         final StringBuilder messages = new StringBuilder();
         int failedCount = 0;
         for (final InputMethodSubtype subtype : mSubtypesList) {
-            final Locale locale = KeyboardLayoutSet.getKeyboardLayoutSetLocale(subtype);
+            final Locale locale = SubtypeLocale.getKeyboardLayoutSetLocale(subtype);
             if (locale.getCountry().equals(SubtypeLocale.QWERTY)) {
                 // This is special country code for QWERTY keyboard.
                 continue;