Move functions in LatinKeyboard related to LanguageSwitcher to SubtypeSwitcher

Change-Id: I777db896bd0287931ce7c810b080ccee1121d34e
diff --git a/java/src/com/android/inputmethod/latin/BaseKeyboardView.java b/java/src/com/android/inputmethod/latin/BaseKeyboardView.java
index 61978e4..5192441 100644
--- a/java/src/com/android/inputmethod/latin/BaseKeyboardView.java
+++ b/java/src/com/android/inputmethod/latin/BaseKeyboardView.java
@@ -932,7 +932,7 @@
                 || tracker.isSpaceKey(keyIndex) || tracker.isSpaceKey(oldKeyIndex);
         // If key changed and preview is on or the key is space (language switch is enabled)
         if (oldKeyIndex != keyIndex && (mShowPreview || (hidePreviewOrShowSpaceKeyPreview
-                && SubtypeSwitcher.getInstance().isLanguageSwitchEnabled()))) {
+                && SubtypeSwitcher.getInstance().needsToDisplayLanguage()))) {
             if (keyIndex == NOT_A_KEY) {
                 mHandler.cancelPopupPreview();
                 mHandler.dismissPreview(mDelayAfterPreview);
diff --git a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java b/java/src/com/android/inputmethod/latin/InputLanguageSelection.java
index e811a2c..4f4f7e3 100644
--- a/java/src/com/android/inputmethod/latin/InputLanguageSelection.java
+++ b/java/src/com/android/inputmethod/latin/InputLanguageSelection.java
@@ -74,7 +74,7 @@
         for (int i = 0; i < mAvailableLanguages.size(); i++) {
             CheckBoxPreference pref = new CheckBoxPreference(this);
             Locale locale = mAvailableLanguages.get(i).locale;
-            pref.setTitle(LanguageSwitcher.toTitleCase(locale.getDisplayName(locale)));
+            pref.setTitle(SubtypeSwitcher.getLanguageName(locale));
             boolean checked = isLocaleIn(locale, languageList);
             pref.setChecked(checked);
             if (hasDictionary(locale)) {
@@ -167,7 +167,7 @@
 
                 if (finalSize == 0) {
                     preprocess[finalSize++] =
-                            new Loc(LanguageSwitcher.toTitleCase(l.getDisplayName(l)), l);
+                            new Loc(SubtypeSwitcher.getLanguageName(l), l);
                 } else {
                     // check previous entry:
                     //  same lang and a country -> upgrade to full name and
@@ -175,15 +175,15 @@
                     //  diff lang -> insert ours with lang-only name
                     if (preprocess[finalSize-1].locale.getLanguage().equals(
                             language)) {
-                        preprocess[finalSize-1].label = LanguageSwitcher.toTitleCase(
-                                preprocess[finalSize-1].locale.getDisplayName());
+                        preprocess[finalSize-1].label = SubtypeSwitcher.getLanguageName(
+                                preprocess[finalSize-1].locale);
                         preprocess[finalSize++] =
-                                new Loc(LanguageSwitcher.toTitleCase(l.getDisplayName()), l);
+                                new Loc(SubtypeSwitcher.getLanguageName(l), l);
                     } else {
                         String displayName;
                         if (s.equals("zz_ZZ")) {
                         } else {
-                            displayName = LanguageSwitcher.toTitleCase(l.getDisplayName(l));
+                            displayName = SubtypeSwitcher.getLanguageName(l);
                             preprocess[finalSize++] = new Loc(displayName, l);
                         }
                     }
diff --git a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
index bc0a40d..3f6b6ca 100644
--- a/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
+++ b/java/src/com/android/inputmethod/latin/KeyboardSwitcher.java
@@ -297,7 +297,6 @@
             keyboard.setVoiceMode(
                     hasVoiceKey(xml == R.xml.kbd_symbols || xml == R.xml.kbd_symbols_black),
                     mVoiceButtonEnabled);
-            keyboard.setLanguageSwitcher(mSubtypeSwitcher.getLanguageSwitcher());
             keyboard.setImeOptions(res, id.mMode, id.mImeOptions);
             keyboard.setColorOfSymbolIcons(isBlackSym(id.mColorScheme));
 
diff --git a/java/src/com/android/inputmethod/latin/LanguageSwitcher.java b/java/src/com/android/inputmethod/latin/LanguageSwitcher.java
index cc1a295..c2805f5 100644
--- a/java/src/com/android/inputmethod/latin/LanguageSwitcher.java
+++ b/java/src/com/android/inputmethod/latin/LanguageSwitcher.java
@@ -192,12 +192,4 @@
         editor.putString(LatinIME.PREF_INPUT_LANGUAGE, getInputLanguage());
         SharedPreferencesCompat.apply(editor);
     }
-
-    static String toTitleCase(String s) {
-        if (s.length() == 0) {
-            return s;
-        }
-
-        return Character.toUpperCase(s.charAt(0)) + s.substring(1);
-    }
 }
diff --git a/java/src/com/android/inputmethod/latin/LatinKeyboard.java b/java/src/com/android/inputmethod/latin/LatinKeyboard.java
index 54d8c47..9473eaf 100644
--- a/java/src/com/android/inputmethod/latin/LatinKeyboard.java
+++ b/java/src/com/android/inputmethod/latin/LatinKeyboard.java
@@ -66,8 +66,6 @@
     private int mSpaceKeyIndex = -1;
     private int mSpaceDragStartX;
     private int mSpaceDragLastDiff;
-    private Locale mLocale;
-    private LanguageSwitcher mLanguageSwitcher;
     private final Resources mRes;
     private final Context mContext;
     private int mMode; // TODO: remove this and use the corresponding mode in the parent class
@@ -389,7 +387,7 @@
     private void updateSpaceBarForLocale(boolean isAutoCompletion, boolean isBlack) {
         final Resources res = mRes;
         // If application locales are explicitly selected.
-        if (mLocale != null) {
+        if (SubtypeSwitcher.getInstance().needsToDisplayLanguage()) {
             mSpaceKey.icon = new BitmapDrawable(res,
                     drawSpaceBar(OPACITY_FULLY_OPAQUE, isAutoCompletion, isBlack));
         } else {
@@ -421,7 +419,7 @@
         final Rect bounds = new Rect();
 
         // Estimate appropriate language name text size to fit in maxTextWidth.
-        String language = LanguageSwitcher.toTitleCase(locale.getDisplayLanguage(locale));
+        String language = SubtypeSwitcher.getLanguageName(locale);
         int textWidth = getTextWidth(paint, language, origTextSize, bounds);
         // Assuming text width and text size are proportional to each other.
         float textSize = origTextSize * Math.min(maxTextWidth / textWidth, 1.0f);
@@ -437,7 +435,7 @@
             textSize = origTextSize;
         }
         if (useShortName) {
-            language = LanguageSwitcher.toTitleCase(locale.getLanguage());
+            language = SubtypeSwitcher.getShortLanguageName(locale);
             textWidth = getTextWidth(paint, language, origTextSize, bounds);
             textSize = origTextSize * Math.min(maxTextWidth / textWidth, 1.0f);
         }
@@ -462,15 +460,16 @@
         final Resources res = mRes;
         canvas.drawColor(res.getColor(R.color.latinkeyboard_transparent), PorterDuff.Mode.CLEAR);
 
+        SubtypeSwitcher subtypeSwitcher = SubtypeSwitcher.getInstance();
         // If application locales are explicitly selected.
-        if (mLocale != null) {
+        if (subtypeSwitcher.needsToDisplayLanguage()) {
             final Paint paint = new Paint();
             paint.setAlpha(opacity);
             paint.setAntiAlias(true);
             paint.setTextAlign(Align.CENTER);
 
             final boolean allowVariableTextSize = true;
-            final String language = layoutSpaceBar(paint, mLanguageSwitcher.getInputLocale(),
+            final String language = layoutSpaceBar(paint, subtypeSwitcher.getInputLocale(),
                     mButtonArrowLeftIcon, mButtonArrowRightIcon, width, height,
                     getTextSizeFromTheme(android.R.style.TextAppearance_Small, 14),
                     allowVariableTextSize);
@@ -487,7 +486,8 @@
             canvas.drawText(language, width / 2, baseline - descent, paint);
 
             // Put arrows that are already layed out on either side of the text
-            if (mLanguageSwitcher.getLocaleCount() > 1) {
+            if (SubtypeSwitcher.USE_SPACEBAR_LANGUAGE_SWITCHER
+                    && subtypeSwitcher.getEnabledKeyboardLocaleCount() > 1) {
                 mButtonArrowLeftIcon.draw(canvas);
                 mButtonArrowRightIcon.draw(canvas);
             }
@@ -531,28 +531,13 @@
     }
 
     public int getLanguageChangeDirection() {
-        if (mSpaceKey == null || mLanguageSwitcher.getLocaleCount() < 2
-                || Math.abs(mSpaceDragLastDiff) < mSpaceKey.width * SPACEBAR_DRAG_THRESHOLD ) {
+        if (mSpaceKey == null || SubtypeSwitcher.getInstance().getEnabledKeyboardLocaleCount() <= 1
+                || Math.abs(mSpaceDragLastDiff) < mSpaceKey.width * SPACEBAR_DRAG_THRESHOLD) {
             return 0; // No change
         }
         return mSpaceDragLastDiff > 0 ? 1 : -1;
     }
 
-    public void setLanguageSwitcher(LanguageSwitcher switcher) {
-        mLanguageSwitcher = switcher;
-        Locale locale = mLanguageSwitcher.getLocaleCount() > 0
-                ? mLanguageSwitcher.getInputLocale()
-                : null;
-        // If the language count is 1 and is the same as the system language, don't show it.
-        if (locale != null
-                && mLanguageSwitcher.getLocaleCount() == 1
-                && mLanguageSwitcher.getSystemLocale().getLanguage()
-                   .equalsIgnoreCase(locale.getLanguage())) {
-            locale = null;
-        }
-        mLocale = locale;
-    }
-
     boolean isCurrentlyInSpace() {
         return mCurrentlyInSpace;
     }
@@ -586,7 +571,8 @@
             if (code == KEYCODE_DELETE) x -= key.width / 6;
         } else if (code == LatinIME.KEYCODE_SPACE) {
             y += LatinKeyboard.sSpacebarVerticalCorrection;
-            if (mLanguageSwitcher.getLocaleCount() > 1) {
+            if (SubtypeSwitcher.USE_SPACEBAR_LANGUAGE_SWITCHER
+                    && SubtypeSwitcher.getInstance().getEnabledKeyboardLocaleCount() > 1) {
                 if (mCurrentlyInSpace) {
                     int diff = x - mSpaceDragStartX;
                     if (Math.abs(diff - mSpaceDragLastDiff) > 0) {
@@ -851,9 +837,6 @@
             invalidateSelf();
         }
 
-        private String getLanguageName(Locale locale) {
-            return LanguageSwitcher.toTitleCase(locale.getDisplayLanguage(locale));
-        }
 
         @Override
         public void draw(Canvas canvas) {
@@ -867,10 +850,10 @@
                 final Drawable rArrow = mRightDrawable;
                 canvas.clipRect(0, 0, width, height);
                 if (mCurrentLanguage == null) {
-                    final LanguageSwitcher languageSwitcher = mLanguageSwitcher;
-                    mCurrentLanguage = getLanguageName(languageSwitcher.getInputLocale());
-                    mNextLanguage = getLanguageName(languageSwitcher.getNextInputLocale());
-                    mPrevLanguage = getLanguageName(languageSwitcher.getPrevInputLocale());
+                    SubtypeSwitcher subtypeSwitcher = SubtypeSwitcher.getInstance();
+                    mCurrentLanguage = subtypeSwitcher.getInputLanguageName();
+                    mNextLanguage = subtypeSwitcher.getNextInputLanguageName();
+                    mPrevLanguage = subtypeSwitcher.getPreviousInputLanguageName();
                 }
                 // Draw language text with shadow
                 final float baseline = mHeight * SPACEBAR_LANGUAGE_BASELINE - paint.descent();
diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
index a295957..11b4dc7 100644
--- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
+++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java
@@ -31,7 +31,9 @@
 import java.util.Locale;
 
 public class SubtypeSwitcher {
-    private static final boolean USE_LEGACY_LANGUAGE_SWITCHER = true;
+    // This flag indicates if we support language switching by swipe on space bar.
+    // We may or may not draw the current language on space bar regardless of this flag.
+    public static final boolean USE_SPACEBAR_LANGUAGE_SWITCHER = true;
     private static final String TAG = "SubtypeSwitcher";
     private static final SubtypeSwitcher sInstance = new SubtypeSwitcher();
     private InputMethodService mService;
@@ -46,7 +48,7 @@
         sInstance.mService = service;
         sInstance.mResources = service.getResources();
         sInstance.mSystemLocale = sInstance.mResources.getConfiguration().locale;
-        if (USE_LEGACY_LANGUAGE_SWITCHER) {
+        if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
             sInstance.initLanguageSwitcher(service);
         }
     }
@@ -61,22 +63,23 @@
     // Language Switching functions //
     //////////////////////////////////
 
-    private int getEnabledKeyboardLocaleCount() {
-        if (USE_LEGACY_LANGUAGE_SWITCHER) {
+    public int getEnabledKeyboardLocaleCount() {
+        if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
             return mLanguageSwitcher.getLocaleCount();
         }
         // TODO: Implement for no legacy mode
         return 0;
     }
 
-    public boolean isLanguageSwitchEnabled() {
+    // TODO: Cache the value
+    public boolean needsToDisplayLanguage() {
      // TODO: Takes care of two-char locale such as "en" in addition to "en_US"
         return !(getEnabledKeyboardLocaleCount() == 1 && getSystemLocale().getLanguage(
                 ).equalsIgnoreCase(getInputLocale().getLanguage()));
     }
 
     public Locale getInputLocale() {
-        if (USE_LEGACY_LANGUAGE_SWITCHER) {
+        if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
             return mLanguageSwitcher.getInputLocale();
         }
         // TODO: Implement for no legacy mode
@@ -85,7 +88,7 @@
 
     public String getInputLanguage() {
         String inputLanguage = null;
-        if (USE_LEGACY_LANGUAGE_SWITCHER) {
+        if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
             inputLanguage = mLanguageSwitcher.getInputLanguage();
         }
         // Should return system locale if there is no Language available.
@@ -96,7 +99,7 @@
     }
 
     public String[] getEnabledLanguages() {
-        if (USE_LEGACY_LANGUAGE_SWITCHER) {
+        if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
             return mLanguageSwitcher.getEnabledLanguages();
         }
         // TODO: Implement for no legacy mode
@@ -107,6 +110,7 @@
         return mSystemLocale;
     }
 
+    // TODO: Cache the value for faster processing.
     public boolean isSystemLocaleSameAsInputLocale() {
         // TODO: Takes care of two-char locale such as "en" in addition to "en_US"
         return getSystemLocale().getLanguage().equalsIgnoreCase(
@@ -114,7 +118,7 @@
     }
 
     public void onConfigurationChanged(Configuration conf) {
-        if (USE_LEGACY_LANGUAGE_SWITCHER) {
+        if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
             // If the system locale changes and is different from the saved
             // locale (mSystemLocale), then reload the input locale list from the
             // latin ime settings (shared prefs) and reset the input locale
@@ -132,7 +136,7 @@
     }
 
     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
-        if (USE_LEGACY_LANGUAGE_SWITCHER) {
+        if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
             mLanguageSwitcher.loadLocales(sharedPreferences);
             return;
         }
@@ -151,6 +155,38 @@
     ////////////////////////////////////////////
     private LanguageSwitcher mLanguageSwitcher;
 
+    public static String getLanguageName(Locale locale) {
+        return toTitleCase(locale.getDisplayLanguage(locale));
+    }
+
+    public static String getShortLanguageName(Locale locale) {
+        return toTitleCase(locale.getLanguage());
+    }
+
+    private static String toTitleCase(String s) {
+        if (s.length() == 0) {
+            return s;
+        }
+        return Character.toUpperCase(s.charAt(0)) + s.substring(1);
+    }
+
+    public String getInputLanguageName() {
+        return getLanguageName(getInputLocale());
+    }
+
+    public String getNextInputLanguageName() {
+        if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
+            return getLanguageName(mLanguageSwitcher.getNextInputLocale());
+        }
+        return "";
+    }
+
+    public String getPreviousInputLanguageName() {
+        if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
+            return getLanguageName(mLanguageSwitcher.getPrevInputLocale());
+        }
+        return "";
+    }
 
     // TODO: This can be an array of String
     // A list of locales which are supported by default for voice input, unless we get a
@@ -183,7 +219,7 @@
     }
 
     public void loadSettings(SharedPreferences prefs) {
-        if (USE_LEGACY_LANGUAGE_SWITCHER) {
+        if (USE_SPACEBAR_LANGUAGE_SWITCHER) {
             mLanguageSwitcher.loadLocales(prefs);
         }
     }
@@ -208,9 +244,4 @@
         mLanguageSwitcher.loadLocales(prefs);
         mLanguageSwitcher.setSystemLocale(conf.locale);
     }
-
-    // TODO: remove this function when the refactor for LanguageSwitcher will be finished
-    public LanguageSwitcher getLanguageSwitcher() {
-        return mLanguageSwitcher;
-    }
 }
\ No newline at end of file