Fix 2146178: On-screen keyboard is wider than screen

Sometimes the keyboard is getting confused about it's width when switching
between hard keyboard open and close state and portrait-forced home. Force
creation of keyboard layouts after a configuration change to fix the
inconsistent state.

Don't force create when just switching between input fields, too expensive.

Also fixes the problem of keyboard not changing layout after a locale change,
unless there's an orientation change.
diff --git a/src/com/android/inputmethod/latin/KeyboardSwitcher.java b/src/com/android/inputmethod/latin/KeyboardSwitcher.java
index 2da7036..c82587b 100644
--- a/src/com/android/inputmethod/latin/KeyboardSwitcher.java
+++ b/src/com/android/inputmethod/latin/KeyboardSwitcher.java
@@ -70,14 +70,15 @@
         mInputView = inputView;
     }
     
-    void makeKeyboards() {
+    void makeKeyboards(boolean forceCreate) {
+        if (forceCreate) mKeyboards.clear();
         // Configuration change is coming after the keyboard gets recreated. So don't rely on that.
         // If keyboards have already been made, check if we have a screen width change and 
         // create the keyboard layouts again at the correct orientation
         int displayWidth = mContext.getMaxWidth();
         if (displayWidth == mLastDisplayWidth) return;
         mLastDisplayWidth = displayWidth;
-        mKeyboards.clear();
+        if (!forceCreate) mKeyboards.clear();
         mSymbolsId = new KeyboardId(R.xml.kbd_symbols);
         mSymbolsShiftedId = new KeyboardId(R.xml.kbd_symbols_shift);
     }
diff --git a/src/com/android/inputmethod/latin/LatinIME.java b/src/com/android/inputmethod/latin/LatinIME.java
index 6ee9bd1..8c9102d 100644
--- a/src/com/android/inputmethod/latin/LatinIME.java
+++ b/src/com/android/inputmethod/latin/LatinIME.java
@@ -210,16 +210,16 @@
     public void onConfigurationChanged(Configuration conf) {
         if (!TextUtils.equals(conf.locale.toString(), mLocale)) {
             initSuggest(conf.locale.toString());
-            if (mKeyboardSwitcher == null) {
-                mKeyboardSwitcher = new KeyboardSwitcher(this);
-            }
-            mKeyboardSwitcher.makeKeyboards();
         }
         // If orientation changed while predicting, commit the change
         if (conf.orientation != mOrientation) {
             commitTyped(getCurrentInputConnection());
             mOrientation = conf.orientation;
         }
+        if (mKeyboardSwitcher == null) {
+            mKeyboardSwitcher = new KeyboardSwitcher(this);
+        }
+        mKeyboardSwitcher.makeKeyboards(true);
         super.onConfigurationChanged(conf);
     }
 
@@ -228,7 +228,7 @@
         mInputView = (LatinKeyboardView) getLayoutInflater().inflate(
                 R.layout.input, null);
         mKeyboardSwitcher.setInputView(mInputView);
-        mKeyboardSwitcher.makeKeyboards();
+        mKeyboardSwitcher.makeKeyboards(true);
         mInputView.setOnKeyboardActionListener(this);
         mKeyboardSwitcher.setKeyboardMode(KeyboardSwitcher.MODE_TEXT, 0);
         return mInputView;
@@ -236,7 +236,7 @@
 
     @Override
     public View onCreateCandidatesView() {
-        mKeyboardSwitcher.makeKeyboards();
+        mKeyboardSwitcher.makeKeyboards(true);
         mCandidateViewContainer = (CandidateViewContainer) getLayoutInflater().inflate(
                 R.layout.candidates, null);
         mCandidateViewContainer.initViews();
@@ -253,7 +253,7 @@
             return;
         }
 
-        mKeyboardSwitcher.makeKeyboards();
+        mKeyboardSwitcher.makeKeyboards(false);
 
         TextEntryState.newSession(this);