Merge "Cleanup keyboard theme switching code"
diff --git a/java/res/layout/more_keys_keyboard.xml b/java/res/layout/more_keys_keyboard.xml
index 89161c6..6b2464b 100644
--- a/java/res/layout/more_keys_keyboard.xml
+++ b/java/res/layout/more_keys_keyboard.xml
@@ -27,7 +27,6 @@
     <com.android.inputmethod.keyboard.MoreKeysKeyboardView
             xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
             android:id="@+id/more_keys_keyboard_view"
-            android:layout_alignParentBottom="true"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             />
diff --git a/java/res/layout/more_suggestions.xml b/java/res/layout/more_suggestions.xml
index 34f54f9..49a00c6 100644
--- a/java/res/layout/more_suggestions.xml
+++ b/java/res/layout/more_suggestions.xml
@@ -27,7 +27,6 @@
     <com.android.inputmethod.latin.suggestions.MoreSuggestionsView
             xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin"
             android:id="@+id/more_suggestions_view"
-            android:layout_alignParentBottom="true"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             latin:keyLabelSize="@dimen/suggestion_text_size"
diff --git a/java/src/com/android/inputmethod/compat/AbstractCompatWrapper.java b/java/src/com/android/inputmethod/compat/AbstractCompatWrapper.java
index 6594935..2c31c55 100644
--- a/java/src/com/android/inputmethod/compat/AbstractCompatWrapper.java
+++ b/java/src/com/android/inputmethod/compat/AbstractCompatWrapper.java
@@ -24,7 +24,7 @@
 
     public AbstractCompatWrapper(Object obj) {
         if (obj == null) {
-            Log.e(TAG, "Invalid input to AbstructCompatWrapper");
+            Log.e(TAG, "Invalid input to AbstractCompatWrapper");
         }
         mObj = obj;
     }
diff --git a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java
index a6bb83a..5741588 100644
--- a/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java
+++ b/java/src/com/android/inputmethod/compat/InputMethodSubtypeCompatWrapper.java
@@ -29,7 +29,7 @@
 
 // TODO: Override this class with the concrete implementation if we need to take care of the
 // performance.
-public final class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper {
+public class InputMethodSubtypeCompatWrapper extends AbstractCompatWrapper {
     private static final boolean DBG = LatinImeLogger.sDBG;
     private static final String TAG = InputMethodSubtypeCompatWrapper.class.getSimpleName();
     private static final String DEFAULT_LOCALE = "en_US";
@@ -65,7 +65,7 @@
 
     public InputMethodSubtypeCompatWrapper(Object subtype) {
         super((CLASS_InputMethodSubtype != null && CLASS_InputMethodSubtype.isInstance(subtype))
-                ? subtype : null);
+                ? subtype : new Object());
         mDummyNameResId = 0;
         mDummyIconResId = 0;
         mDummyLocale = DEFAULT_LOCALE;
@@ -76,7 +76,7 @@
     // Constructor for creating a dummy subtype.
     public InputMethodSubtypeCompatWrapper(int nameResId, int iconResId, String locale,
             String mode, String extraValues) {
-        super(null);
+        super(new Object());
         if (DBG) {
             Log.d(TAG, "CreateInputMethodSubtypeCompatWrapper");
         }
diff --git a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java
index 9970d1d..2495b54 100644
--- a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java
@@ -136,34 +136,23 @@
         mController = controller;
         mListener = listener;
         final View container = (View)getParent();
-        final MoreKeysKeyboard moreKeysKeyboard = (MoreKeysKeyboard)getKeyboard();
-
-        parentView.getLocationInWindow(mCoordinates);
-        final int moreKeysKeyboardLeft = pointX - moreKeysKeyboard.getDefaultCoordX()
+        final MoreKeysKeyboard pane = (MoreKeysKeyboard)getKeyboard();
+        final int defaultCoordX = pane.getDefaultCoordX();
+        // The coordinates of panel's left-top corner in parentView's coordinate system.
+        final int x = pointX - defaultCoordX - container.getPaddingLeft()
                 + parentView.getPaddingLeft();
-        final int x = wrapUp(Math.max(0, Math.min(moreKeysKeyboardLeft,
-                parentView.getWidth() - moreKeysKeyboard.mOccupiedWidth))
-                - container.getPaddingLeft() + mCoordinates[0],
-                container.getMeasuredWidth(), 0, parentView.getWidth());
-        final int y = pointY
-                - (container.getMeasuredHeight() - container.getPaddingBottom())
-                + parentView.getPaddingTop() + mCoordinates[1];
+        final int y = pointY - container.getMeasuredHeight() + container.getPaddingBottom()
+                + parentView.getPaddingTop();
 
         window.setContentView(container);
         window.setWidth(container.getMeasuredWidth());
         window.setHeight(container.getMeasuredHeight());
-        window.showAtLocation(parentView, Gravity.NO_GRAVITY, x, y);
+        parentView.getLocationInWindow(mCoordinates);
+        window.showAtLocation(parentView, Gravity.NO_GRAVITY,
+                x + mCoordinates[0], y + mCoordinates[1]);
 
-        mOriginX = x + container.getPaddingLeft() - mCoordinates[0];
-        mOriginY = y + container.getPaddingTop() - mCoordinates[1];
-    }
-
-    private static int wrapUp(int x, int width, int left, int right) {
-        if (x < left)
-            return left;
-        if (x + width > right)
-            return right - width;
-        return x;
+        mOriginX = x + container.getPaddingLeft();
+        mOriginY = y + container.getPaddingTop();
     }
 
     private boolean mIsDismissing;
diff --git a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java
index f96f71e..721ea13 100644
--- a/java/src/com/android/inputmethod/keyboard/ProximityInfo.java
+++ b/java/src/com/android/inputmethod/keyboard/ProximityInfo.java
@@ -44,10 +44,11 @@
     // TODO: Find a proper name for mKeyboardMinWidth
     private final int mKeyboardMinWidth;
     private final int mKeyboardHeight;
+    private final int mMostCommonKeyWidth;
     private final Key[][] mGridNeighbors;
 
-    ProximityInfo(int gridWidth, int gridHeight, int minWidth, int height, int keyWidth,
-            int keyHeight, Set<Key> keys, TouchPositionCorrection touchPositionCorrection,
+    ProximityInfo(int gridWidth, int gridHeight, int minWidth, int height, int mostCommonKeyWidth,
+            int mostCommonKeyHeight, Set<Key> keys, TouchPositionCorrection touchPositionCorrection,
             Map<Integer, List<Integer>> additionalProximityChars) {
         mGridWidth = gridWidth;
         mGridHeight = gridHeight;
@@ -56,13 +57,15 @@
         mCellHeight = (height + mGridHeight - 1) / mGridHeight;
         mKeyboardMinWidth = minWidth;
         mKeyboardHeight = height;
-        mKeyHeight = keyHeight;
+        mKeyHeight = mostCommonKeyHeight;
+        mMostCommonKeyWidth = mostCommonKeyWidth;
         mGridNeighbors = new Key[mGridSize][];
         if (minWidth == 0 || height == 0) {
             // No proximity required. Keyboard might be more keys keyboard.
             return;
         }
-        computeNearestNeighbors(keyWidth, keys, touchPositionCorrection, additionalProximityChars);
+        computeNearestNeighbors(
+                mostCommonKeyWidth, keys, touchPositionCorrection, additionalProximityChars);
     }
 
     public static ProximityInfo createDummyProximityInfo() {
@@ -74,8 +77,8 @@
         final ProximityInfo spellCheckerProximityInfo = createDummyProximityInfo();
         spellCheckerProximityInfo.mNativeProximityInfo =
                 spellCheckerProximityInfo.setProximityInfoNative(
-                        SpellCheckerProximityInfo.ROW_SIZE, 480, 300, 11, 3, proximity, 0,
-                        null, null, null, null, null, null, null, null);
+                        SpellCheckerProximityInfo.ROW_SIZE, 480, 300, 11, 3, (480 / 10), proximity,
+                        0, null, null, null, null, null, null, null, null);
         return spellCheckerProximityInfo;
     }
 
@@ -85,7 +88,8 @@
     }
 
     private native long setProximityInfoNative(int maxProximityCharsSize, int displayWidth,
-            int displayHeight, int gridWidth, int gridHeight, int[] proximityCharsArray,
+            int displayHeight, int gridWidth, int gridHeight,
+            int mostCommonKeyWidth, int[] proximityCharsArray,
             int keyCount, int[] keyXCoordinates, int[] keyYCoordinates,
             int[] keyWidths, int[] keyHeights, int[] keyCharCodes,
             float[] sweetSpotCenterX, float[] sweetSpotCenterY, float[] sweetSpotRadii);
@@ -151,7 +155,8 @@
         }
 
         mNativeProximityInfo = setProximityInfoNative(MAX_PROXIMITY_CHARS_SIZE,
-                keyboardWidth, keyboardHeight, mGridWidth, mGridHeight, proximityCharsArray,
+                keyboardWidth, keyboardHeight, mGridWidth, mGridHeight, mMostCommonKeyWidth,
+                proximityCharsArray,
                 keyCount, keyXCoordinates, keyYCoordinates, keyWidths, keyHeights, keyCharCodes,
                 sweetSpotCenterXs, sweetSpotCenterYs, sweetSpotRadii);
     }
diff --git a/java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java b/java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java
new file mode 100644
index 0000000..1cbdbd6
--- /dev/null
+++ b/java/src/com/android/inputmethod/latin/AudioAndHapticFeedbackManager.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.android.inputmethod.latin;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.media.AudioManager;
+import android.view.HapticFeedbackConstants;
+import android.view.View;
+
+import com.android.inputmethod.compat.VibratorCompatWrapper;
+import com.android.inputmethod.keyboard.Keyboard;
+
+/**
+ * This class gathers audio feedback and haptic feedback functions.
+ *
+ * It offers a consistent and simple interface that allows LatinIME to forget about the
+ * complexity of settings and the like.
+ */
+public class AudioAndHapticFeedbackManager extends BroadcastReceiver {
+    final private SettingsValues mSettingsValues;
+    final private AudioManager mAudioManager;
+    final private VibratorCompatWrapper mVibrator;
+    private boolean mSoundOn;
+
+    public AudioAndHapticFeedbackManager(final LatinIME latinIme,
+            final SettingsValues settingsValues) {
+        mSettingsValues = settingsValues;
+        mVibrator = VibratorCompatWrapper.getInstance(latinIme);
+        mAudioManager = (AudioManager) latinIme.getSystemService(Context.AUDIO_SERVICE);
+        mSoundOn = reevaluateIfSoundIsOn();
+    }
+
+    public void hapticAndAudioFeedback(final int primaryCode,
+            final View viewToPerformHapticFeedbackOn) {
+        vibrate(viewToPerformHapticFeedbackOn);
+        playKeyClick(primaryCode);
+    }
+
+    private boolean reevaluateIfSoundIsOn() {
+        if (!mSettingsValues.mSoundOn || mAudioManager == null) {
+            return false;
+        } else {
+            return mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL;
+        }
+    }
+
+    private void playKeyClick(int primaryCode) {
+        // if mAudioManager is null, we can't play a sound anyway, so return
+        if (mAudioManager == null) return;
+        if (mSoundOn) {
+            final int sound;
+            switch (primaryCode) {
+            case Keyboard.CODE_DELETE:
+                sound = AudioManager.FX_KEYPRESS_DELETE;
+                break;
+            case Keyboard.CODE_ENTER:
+                sound = AudioManager.FX_KEYPRESS_RETURN;
+                break;
+            case Keyboard.CODE_SPACE:
+                sound = AudioManager.FX_KEYPRESS_SPACEBAR;
+                break;
+            default:
+                sound = AudioManager.FX_KEYPRESS_STANDARD;
+                break;
+            }
+            mAudioManager.playSoundEffect(sound, mSettingsValues.mFxVolume);
+        }
+    }
+
+    // TODO: make this private when LatinIME does not call it any more
+    public void vibrate(final View viewToPerformHapticFeedbackOn) {
+        if (!mSettingsValues.mVibrateOn) {
+            return;
+        }
+        if (mSettingsValues.mKeypressVibrationDuration < 0) {
+            // Go ahead with the system default
+            if (viewToPerformHapticFeedbackOn != null) {
+                viewToPerformHapticFeedbackOn.performHapticFeedback(
+                        HapticFeedbackConstants.KEYBOARD_TAP,
+                        HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
+            }
+        } else if (mVibrator != null) {
+            mVibrator.vibrate(mSettingsValues.mKeypressVibrationDuration);
+        }
+    }
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        final String action = intent.getAction();
+        // The following test is supposedly useless since we only listen for the ringer event.
+        // Still, it's a good safety measure.
+        if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
+            mSoundOn = reevaluateIfSoundIsOn();
+        }
+    }
+}
diff --git a/java/src/com/android/inputmethod/latin/AutoCorrection.java b/java/src/com/android/inputmethod/latin/AutoCorrection.java
index bcb7891..a9489da 100644
--- a/java/src/com/android/inputmethod/latin/AutoCorrection.java
+++ b/java/src/com/android/inputmethod/latin/AutoCorrection.java
@@ -25,18 +25,16 @@
 public class AutoCorrection {
     private static final boolean DBG = LatinImeLogger.sDBG;
     private static final String TAG = AutoCorrection.class.getSimpleName();
-    private boolean mHasAutoCorrection;
     private CharSequence mAutoCorrectionWord;
     private double mNormalizedScore;
 
     public void init() {
-        mHasAutoCorrection = false;
         mAutoCorrectionWord = null;
         mNormalizedScore = Integer.MIN_VALUE;
     }
 
     public boolean hasAutoCorrection() {
-        return mHasAutoCorrection;
+        return null != mAutoCorrectionWord;
     }
 
     public CharSequence getAutoCorrectionWord() {
@@ -47,22 +45,20 @@
         return mNormalizedScore;
     }
 
-    public void updateAutoCorrectionStatus(Map<String, Dictionary> dictionaries,
+    public CharSequence updateAutoCorrectionStatus(Map<String, Dictionary> dictionaries,
             WordComposer wordComposer, ArrayList<CharSequence> suggestions, int[] sortedScores,
             CharSequence typedWord, double autoCorrectionThreshold, int correctionMode,
             CharSequence whitelistedWord) {
         if (hasAutoCorrectionForWhitelistedWord(whitelistedWord)) {
-            mHasAutoCorrection = true;
             mAutoCorrectionWord = whitelistedWord;
         } else if (hasAutoCorrectionForTypedWord(
                 dictionaries, wordComposer, suggestions, typedWord, correctionMode)) {
-            mHasAutoCorrection = true;
             mAutoCorrectionWord = typedWord;
         } else if (hasAutoCorrectionForBinaryDictionary(wordComposer, suggestions, correctionMode,
                 sortedScores, typedWord, autoCorrectionThreshold)) {
-            mHasAutoCorrection = true;
             mAutoCorrectionWord = suggestions.get(0);
         }
+        return mAutoCorrectionWord;
     }
 
     public static boolean isValidWord(
diff --git a/java/src/com/android/inputmethod/latin/ComposingStateManager.java b/java/src/com/android/inputmethod/latin/ComposingStateManager.java
deleted file mode 100644
index 8811f20..0000000
--- a/java/src/com/android/inputmethod/latin/ComposingStateManager.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/**
- * Copyright (C) 2011 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
-
-package com.android.inputmethod.latin;
-
-import android.util.Log;
-
-public class ComposingStateManager {
-    private static final String TAG = ComposingStateManager.class.getSimpleName();
-    private static final ComposingStateManager sInstance = new ComposingStateManager();
-    private boolean mAutoCorrectionIndicatorOn;
-    private boolean mIsComposing;
-
-    public static ComposingStateManager getInstance() {
-        return sInstance;
-    }
-
-    private ComposingStateManager() {
-        mAutoCorrectionIndicatorOn = false;
-        mIsComposing = false;
-    }
-
-    public synchronized void onStartComposingText() {
-        if (!mIsComposing) {
-            if (LatinImeLogger.sDBG) {
-                Log.i(TAG, "Start composing text.");
-            }
-            mAutoCorrectionIndicatorOn = false;
-            mIsComposing = true;
-        }
-    }
-
-    public synchronized void onFinishComposingText() {
-        if (mIsComposing) {
-            if (LatinImeLogger.sDBG) {
-                Log.i(TAG, "Finish composing text.");
-            }
-            mAutoCorrectionIndicatorOn = false;
-            mIsComposing = false;
-        }
-    }
-
-    public synchronized boolean isAutoCorrectionIndicatorOn() {
-        return mAutoCorrectionIndicatorOn;
-    }
-
-    public synchronized void setAutoCorrectionIndicatorOn(boolean on) {
-        // Auto-correction indicator should be specified only when the current state is "composing".
-        if (!mIsComposing) return;
-        if (LatinImeLogger.sDBG) {
-            Log.i(TAG, "Set auto correction Indicator: " + on);
-        }
-        mAutoCorrectionIndicatorOn = on;
-    }
-}
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 1858db9..7a081c0 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -40,7 +40,6 @@
 import android.util.Log;
 import android.util.PrintWriterPrinter;
 import android.util.Printer;
-import android.view.HapticFeedbackConstants;
 import android.view.KeyEvent;
 import android.view.View;
 import android.view.ViewGroup;
@@ -61,7 +60,6 @@
 import com.android.inputmethod.compat.InputMethodSubtypeCompatWrapper;
 import com.android.inputmethod.compat.InputTypeCompatUtils;
 import com.android.inputmethod.compat.SuggestionSpanUtils;
-import com.android.inputmethod.compat.VibratorCompatWrapper;
 import com.android.inputmethod.deprecated.LanguageSwitcherProxy;
 import com.android.inputmethod.deprecated.VoiceProxy;
 import com.android.inputmethod.keyboard.Keyboard;
@@ -223,10 +221,7 @@
     private int mDeleteCount;
     private long mLastKeyTime;
 
-    private AudioManager mAudioManager;
-    private boolean mSilentModeOn; // System-wide current configuration
-
-    private VibratorCompatWrapper mVibrator;
+    private AudioAndHapticFeedbackManager mFeedbackManager;
 
     // Member variables for remembering the current device orientation.
     private int mDisplayOrientation;
@@ -238,8 +233,7 @@
     // Keeps track of most recently inserted text (multi-character key) for reverting
     private CharSequence mEnteredText;
 
-    private final ComposingStateManager mComposingStateManager =
-            ComposingStateManager.getInstance();
+    private boolean mIsAutoCorrectionIndicatorOn;
 
     public final UIHandler mHandler = new UIHandler(this);
 
@@ -511,7 +505,6 @@
         super.onCreate();
 
         mImm = InputMethodManagerCompatWrapper.getInstance();
-        mVibrator = VibratorCompatWrapper.getInstance(this);
         mHandler.onCreate();
         DEBUG = LatinImeLogger.sDBG;
 
@@ -538,11 +531,14 @@
         // Register to receive ringer mode change and network state change.
         // Also receive installation and removal of a dictionary pack.
         final IntentFilter filter = new IntentFilter();
-        filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
         filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
         registerReceiver(mReceiver, filter);
         mVoiceProxy = VoiceProxy.init(this, prefs, mHandler);
 
+        final IntentFilter ringerModeFilter = new IntentFilter();
+        ringerModeFilter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
+        registerReceiver(mFeedbackManager, ringerModeFilter);
+
         final IntentFilter packageFilter = new IntentFilter();
         packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
         packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
@@ -559,6 +555,7 @@
     /* package */ void loadSettings() {
         if (null == mPrefs) mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
         mSettingsValues = new SettingsValues(mPrefs, this, mSubtypeSwitcher.getInputLocaleStr());
+        mFeedbackManager = new AudioAndHapticFeedbackManager(this, mSettingsValues);
         resetContactsDictionary(null == mSuggest ? null : mSuggest.getContactsDictionary());
     }
 
@@ -647,6 +644,7 @@
             mSuggest = null;
         }
         unregisterReceiver(mReceiver);
+        unregisterReceiver(mFeedbackManager);
         unregisterReceiver(mDictionaryPackInstallReceiver);
         mVoiceProxy.destroy();
         LatinImeLogger.commit();
@@ -657,7 +655,6 @@
     @Override
     public void onConfigurationChanged(Configuration conf) {
         mSubtypeSwitcher.onConfigurationChanged(conf);
-        mComposingStateManager.onFinishComposingText();
         // If orientation changed while predicting, commit the change
         if (mDisplayOrientation != conf.orientation) {
             mDisplayOrientation = conf.orientation;
@@ -1148,7 +1145,6 @@
     // and the composingStateManager about it.
     private void resetEntireInputState() {
         resetComposingState(true /* alsoResetLastComposedWord */);
-        mComposingStateManager.onFinishComposingText();
         updateSuggestions();
         final InputConnection ic = getCurrentInputConnection();
         if (ic != null) {
@@ -1339,6 +1335,7 @@
         // all inputs that do not result in a special state. Each character handling is then
         // free to override the state as they see fit.
         final int spaceState = mSpaceState;
+        if (!mWordComposer.isComposingWord()) mIsAutoCorrectionIndicatorOn = false;
 
         // TODO: Consolidate the double space timer, mLastKeyTime, and the space state.
         if (primaryCode != Keyboard.CODE_SPACE) {
@@ -1588,7 +1585,6 @@
                 // it entirely and resume suggestions on the previous word, we'd like to still
                 // have touch coordinates for it.
                 resetComposingState(false /* alsoResetLastComposedWord */);
-                mComposingStateManager.onFinishComposingText();
                 clearSuggestions();
             }
         }
@@ -1599,7 +1595,6 @@
                 // If it's the first letter, make note of auto-caps state
                 if (mWordComposer.size() == 1) {
                     mWordComposer.setAutoCapitalized(getCurrentAutoCapsState());
-                    mComposingStateManager.onStartComposingText();
                 }
                 ic.setComposingText(getTextWithUnderline(mWordComposer.getTypedWord()), 1);
             }
@@ -1631,7 +1626,6 @@
     private boolean handleSeparator(final int primaryCode, final int x, final int y,
             final int spaceState) {
         mVoiceProxy.handleSeparator();
-        mComposingStateManager.onFinishComposingText();
 
         // Should dismiss the "Touch again to save" message when handling separator
         if (mSuggestionsView != null && mSuggestionsView.dismissAddToDictionaryHint()) {
@@ -1711,7 +1705,7 @@
     }
 
     private CharSequence getTextWithUnderline(final CharSequence text) {
-        return mComposingStateManager.isAutoCorrectionIndicatorOn()
+        return mIsAutoCorrectionIndicatorOn
                 ? SuggestionSpanUtils.getTextWithAutoCorrectionIndicatorUnderline(this, text)
                 : text;
     }
@@ -1787,15 +1781,9 @@
         // Put a blue underline to a word in TextView which will be auto-corrected.
         final InputConnection ic = getCurrentInputConnection();
         if (ic != null) {
-            final boolean oldAutoCorrectionIndicator =
-                    mComposingStateManager.isAutoCorrectionIndicatorOn();
-            if (oldAutoCorrectionIndicator != newAutoCorrectionIndicator) {
-                mComposingStateManager.setAutoCorrectionIndicatorOn(newAutoCorrectionIndicator);
-                if (DEBUG) {
-                    Log.d(TAG, "Flip the indicator. " + oldAutoCorrectionIndicator
-                            + " -> " + newAutoCorrectionIndicator);
-                }
+            if (mIsAutoCorrectionIndicatorOn != newAutoCorrectionIndicator) {
                 if (mWordComposer.isComposingWord()) {
+                    mIsAutoCorrectionIndicatorOn = newAutoCorrectionIndicator;
                     final CharSequence textWithUnderline =
                             getTextWithUnderline(mWordComposer.getTypedWord());
                     ic.setComposingText(textWithUnderline, 1);
@@ -1941,7 +1929,6 @@
 
     @Override
     public void pickSuggestionManually(final int index, final CharSequence suggestion) {
-        mComposingStateManager.onFinishComposingText();
         final SuggestedWords suggestedWords = mSuggestionsView.getSuggestions();
         mVoiceProxy.flushAndLogAllTextModificationCounters(index, suggestion,
                 mSettingsValues.mWordSeparators);
@@ -2144,14 +2131,12 @@
     public boolean isCursorTouchingWord() {
         final InputConnection ic = getCurrentInputConnection();
         if (ic == null) return false;
-        CharSequence toLeft = ic.getTextBeforeCursor(1, 0);
-        CharSequence toRight = ic.getTextAfterCursor(1, 0);
-        if (!TextUtils.isEmpty(toLeft)
-                && !mSettingsValues.isWordSeparator(toLeft.charAt(0))) {
+        CharSequence before = ic.getTextBeforeCursor(1, 0);
+        CharSequence after = ic.getTextAfterCursor(1, 0);
+        if (!TextUtils.isEmpty(before) && !mSettingsValues.isWordSeparator(before.charAt(0))) {
             return true;
         }
-        if (!TextUtils.isEmpty(toRight)
-                && !mSettingsValues.isWordSeparator(toRight.charAt(0))) {
+        if (!TextUtils.isEmpty(after) && !mSettingsValues.isWordSeparator(after.charAt(0))) {
             return true;
         }
         return false;
@@ -2212,7 +2197,6 @@
     private void restartSuggestionsOnWordBeforeCursor(final InputConnection ic,
             final CharSequence word) {
         mWordComposer.setComposingWord(word, mKeyboardSwitcher.getKeyboard());
-        mComposingStateManager.onStartComposingText();
         ic.deleteSurroundingText(word.length(), 0);
         ic.setComposingText(word, 1);
         mHandler.postUpdateSuggestions();
@@ -2244,7 +2228,6 @@
             // This is the case when we cancel a manual pick.
             // We should restart suggestion on the word right away.
             mWordComposer.resumeSuggestionOnLastComposedWord(mLastComposedWord);
-            mComposingStateManager.onStartComposingText();
             ic.setComposingText(originallyTypedWord, 1);
         } else {
             ic.commitText(originallyTypedWord, 1);
@@ -2335,9 +2318,8 @@
         }
     }
 
-    public void hapticAndAudioFeedback(int primaryCode) {
-        vibrate();
-        playKeyClick(primaryCode);
+    public void hapticAndAudioFeedback(final int primaryCode) {
+        mFeedbackManager.hapticAndAudioFeedback(primaryCode, mKeyboardSwitcher.getKeyboardView());
     }
 
     @Override
@@ -2367,76 +2349,21 @@
         @Override
         public void onReceive(Context context, Intent intent) {
             final String action = intent.getAction();
-            if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
-                updateRingerMode();
-            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
+            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                 mSubtypeSwitcher.onNetworkStateChanged(intent);
             }
         }
     };
 
-    // update flags for silent mode
-    private void updateRingerMode() {
-        if (mAudioManager == null) {
-            mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
-            if (mAudioManager == null) return;
-        }
-        mSilentModeOn = (mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL);
-    }
-
-    private void playKeyClick(int primaryCode) {
-        // if mAudioManager is null, we don't have the ringer state yet
-        // mAudioManager will be set by updateRingerMode
-        if (mAudioManager == null) {
-            if (mKeyboardSwitcher.getKeyboardView() != null) {
-                updateRingerMode();
-            }
-        }
-        if (isSoundOn()) {
-            final int sound;
-            switch (primaryCode) {
-            case Keyboard.CODE_DELETE:
-                sound = AudioManager.FX_KEYPRESS_DELETE;
-                break;
-            case Keyboard.CODE_ENTER:
-                sound = AudioManager.FX_KEYPRESS_RETURN;
-                break;
-            case Keyboard.CODE_SPACE:
-                sound = AudioManager.FX_KEYPRESS_SPACEBAR;
-                break;
-            default:
-                sound = AudioManager.FX_KEYPRESS_STANDARD;
-                break;
-            }
-            mAudioManager.playSoundEffect(sound, mSettingsValues.mFxVolume);
-        }
-    }
-
+    // TODO: remove this method when VoiceProxy has been removed
     public void vibrate() {
-        if (!mSettingsValues.mVibrateOn) {
-            return;
-        }
-        if (mSettingsValues.mKeypressVibrationDuration < 0) {
-            // Go ahead with the system default
-            LatinKeyboardView inputView = mKeyboardSwitcher.getKeyboardView();
-            if (inputView != null) {
-                inputView.performHapticFeedback(
-                        HapticFeedbackConstants.KEYBOARD_TAP,
-                        HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING);
-            }
-        } else if (mVibrator != null) {
-            mVibrator.vibrate(mSettingsValues.mKeypressVibrationDuration);
-        }
+        mFeedbackManager.vibrate(mKeyboardSwitcher.getKeyboardView());
     }
 
     public boolean isAutoCapitalized() {
         return mWordComposer.isAutoCapitalized();
     }
 
-    boolean isSoundOn() {
-        return mSettingsValues.mSoundOn && !mSilentModeOn;
-    }
-
     private void updateCorrectionMode() {
         // TODO: cleanup messy flags
         final boolean shouldAutoCorrect = mSettingsValues.mAutoCorrectEnabled
diff --git a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
index 1f72147..f11b1a4 100644
--- a/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
+++ b/java/src/com/android/inputmethod/latin/suggestions/MoreSuggestionsView.java
@@ -147,32 +147,22 @@
         mListener = listener;
         final View container = (View)getParent();
         final MoreSuggestions pane = (MoreSuggestions)getKeyboard();
-
-        parentView.getLocationInWindow(mCoordinates);
-        final int paneLeft = pointX - (pane.mOccupiedWidth / 2) + parentView.getPaddingLeft();
-        final int x = wrapUp(Math.max(0, Math.min(paneLeft,
-                parentView.getWidth() - pane.mOccupiedWidth))
-                - container.getPaddingLeft() + mCoordinates[0],
-                container.getMeasuredWidth(), 0, parentView.getWidth());
-        final int y = pointY
-                - (container.getMeasuredHeight() - container.getPaddingBottom())
-                + parentView.getPaddingTop() + mCoordinates[1];
+        final int defaultCoordX = pane.mOccupiedWidth / 2;
+        // The coordinates of panel's left-top corner in parentView's coordinate system.
+        final int x = pointX - defaultCoordX - container.getPaddingLeft()
+                + parentView.getPaddingLeft();
+        final int y = pointY - container.getMeasuredHeight() + container.getPaddingBottom()
+                + parentView.getPaddingTop();
 
         window.setContentView(container);
         window.setWidth(container.getMeasuredWidth());
         window.setHeight(container.getMeasuredHeight());
-        window.showAtLocation(parentView, Gravity.NO_GRAVITY, x, y);
+        parentView.getLocationInWindow(mCoordinates);
+        window.showAtLocation(parentView, Gravity.NO_GRAVITY,
+                x + mCoordinates[0], y + mCoordinates[1]);
 
-        mOriginX = x + container.getPaddingLeft() - mCoordinates[0];
-        mOriginY = y + container.getPaddingTop() - mCoordinates[1];
-    }
-
-    private static int wrapUp(int x, int width, int left, int right) {
-        if (x < left)
-            return left;
-        if (x + width > right)
-            return right - width;
-        return x;
+        mOriginX = x + container.getPaddingLeft();
+        mOriginY = y + container.getPaddingTop();
     }
 
     private boolean mIsDismissing;
diff --git a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
index 6e4fefd..844b230 100644
--- a/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
+++ b/native/jni/com_android_inputmethod_keyboard_ProximityInfo.cpp
@@ -30,7 +30,7 @@
 
 static jlong latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object,
         jint maxProximityCharsSize, jint displayWidth, jint displayHeight, jint gridWidth,
-        jint gridHeight, jintArray proximityCharsArray, jint keyCount,
+        jint gridHeight, jint mostCommonkeyWidth, jintArray proximityCharsArray, jint keyCount,
         jintArray keyXCoordinateArray, jintArray keyYCoordinateArray, jintArray keyWidthArray,
         jintArray keyHeightArray, jintArray keyCharCodeArray,
         jfloatArray sweetSpotCenterXArray, jfloatArray sweetSpotCenterYArray,
@@ -45,7 +45,8 @@
     jfloat *sweetSpotCenterYs = safeGetFloatArrayElements(env, sweetSpotCenterYArray);
     jfloat *sweetSpotRadii = safeGetFloatArrayElements(env, sweetSpotRadiusArray);
     ProximityInfo *proximityInfo = new ProximityInfo(maxProximityCharsSize, displayWidth,
-            displayHeight, gridWidth, gridHeight, (const uint32_t*)proximityChars,
+            displayHeight, gridWidth, gridHeight, mostCommonkeyWidth,
+            (const uint32_t*)proximityChars,
             keyCount, (const int32_t*)keyXCoordinates, (const int32_t*)keyYCoordinates,
             (const int32_t*)keyWidths, (const int32_t*)keyHeights, (const int32_t*)keyCharCodes,
             (const float*)sweetSpotCenterXs, (const float*)sweetSpotCenterYs,
@@ -69,7 +70,7 @@
 }
 
 static JNINativeMethod sKeyboardMethods[] = {
-    {"setProximityInfoNative", "(IIIII[II[I[I[I[I[I[F[F[F)J",
+    {"setProximityInfoNative", "(IIIIII[II[I[I[I[I[I[F[F[F)J",
             (void*)latinime_Keyboard_setProximityInfo},
     {"releaseProximityInfoNative", "(J)V", (void*)latinime_Keyboard_release}
 };
diff --git a/native/src/proximity_info.cpp b/native/src/proximity_info.cpp
index b6bab22..ad19f58 100644
--- a/native/src/proximity_info.cpp
+++ b/native/src/proximity_info.cpp
@@ -35,12 +35,14 @@
 
 ProximityInfo::ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
         const int keyboardHeight, const int gridWidth, const int gridHeight,
+        const int mostCommonKeyWidth,
         const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
         const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
         const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs,
         const float *sweetSpotRadii)
         : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
           KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
+          MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth),
           CELL_WIDTH((keyboardWidth + gridWidth - 1) / gridWidth),
           CELL_HEIGHT((keyboardHeight + gridHeight - 1) / gridHeight),
           KEY_COUNT(min(keyCount, MAX_KEY_COUNT_IN_A_KEYBOARD)),
@@ -123,6 +125,47 @@
     return false;
 }
 
+bool ProximityInfo::isOnKey(const int keyId, const int x, const int y) {
+    const int left = mKeyXCoordinates[keyId];
+    const int top = mKeyYCoordinates[keyId];
+    const int right = left + mKeyWidths[keyId] + 1;
+    const int bottom = top + mKeyHeights[keyId];
+    return left < right && top < bottom && x >= left && x < right && y >= top && y < bottom;
+}
+
+int ProximityInfo::squaredDistanceToEdge(const int keyId, const int x, const int y) {
+    const int left = mKeyXCoordinates[keyId];
+    const int top = mKeyYCoordinates[keyId];
+    const int right = left + mKeyWidths[keyId] + 1;
+    const int bottom = top + mKeyHeights[keyId];
+    const int edgeX = x < left ? left : (x > right ? right : x);
+    const int edgeY = y < top ? top : (y > bottom ? bottom : y);
+    const int dx = x - edgeX;
+    const int dy = y - edgeY;
+    return dx * dx + dy * dy;
+}
+
+void ProximityInfo::calculateNearbyKeyCodes(
+        const int x, const int y, const uint32_t primaryKey, int *inputCodes) {
+    int insertPos = 0;
+    inputCodes[insertPos++] = primaryKey;
+    const int startIndex = getStartIndexFromCoordinates(x, y);
+    for (int i = 0; i < MAX_PROXIMITY_CHARS_SIZE; ++i) {
+        const uint32_t c = mProximityCharsArray[startIndex + i];
+        if (c < KEYCODE_SPACE || c == primaryKey) {
+            continue;
+        }
+        for (int j = 0; j < KEY_COUNT; ++j) {
+            const bool onKey = isOnKey(j, x, y);
+            const int distance = squaredDistanceToEdge(j, x, y);
+            if (onKey || distance < MOST_COMMON_KEY_WIDTH_SQUARE) {
+                inputCodes[insertPos++] = c;
+            }
+        }
+    }
+    // TODO: calculate additional chars
+}
+
 // TODO: Calculate nearby codes here.
 void ProximityInfo::setInputParams(const int* inputCodes, const int inputLength,
         const int* xCoordinates, const int* yCoordinates) {
diff --git a/native/src/proximity_info.h b/native/src/proximity_info.h
index b77c1bb..caabadf 100644
--- a/native/src/proximity_info.h
+++ b/native/src/proximity_info.h
@@ -45,13 +45,14 @@
 
     ProximityInfo(const int maxProximityCharsSize, const int keyboardWidth,
             const int keybaordHeight, const int gridWidth, const int gridHeight,
+            const int mostCommonkeyWidth,
             const uint32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates,
             const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights,
             const int32_t *keyCharCodes, const float *sweetSpotCenterXs,
             const float *sweetSpotCenterYs, const float *sweetSpotRadii);
     ~ProximityInfo();
     bool hasSpaceProximity(const int x, const int y) const;
-    void setInputParams(const int* inputCodes, const int inputLength,
+    void setInputParams(const int *inputCodes, const int inputLength,
             const int *xCoordinates, const int *yCoordinates);
     const int* getProximityCharsAt(const int index) const;
     unsigned short getPrimaryCharAt(const int index) const;
@@ -87,12 +88,17 @@
         // the radius of the key is assigned to zero.
         return mSweetSpotRadii[keyIndex] > 0.0;
     }
+    bool isOnKey(const int keyId, const int x, const int y);
+    int squaredDistanceToEdge(const int keyId, const int x, const int y);
+    void calculateNearbyKeyCodes(
+            const int x, const int y, const uint32_t primaryKey, int *inputCodes);
 
     const int MAX_PROXIMITY_CHARS_SIZE;
     const int KEYBOARD_WIDTH;
     const int KEYBOARD_HEIGHT;
     const int GRID_WIDTH;
     const int GRID_HEIGHT;
+    const int MOST_COMMON_KEY_WIDTH_SQUARE;
     const int CELL_WIDTH;
     const int CELL_HEIGHT;
     const int KEY_COUNT;
diff --git a/native/src/unigram_dictionary.cpp b/native/src/unigram_dictionary.cpp
index 0b646d1..10aa7e2 100644
--- a/native/src/unigram_dictionary.cpp
+++ b/native/src/unigram_dictionary.cpp
@@ -91,12 +91,16 @@
 // codesRemain is the remaining size in codesSrc.
 void UnigramDictionary::getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo,
         const int *xcoordinates, const int *ycoordinates, const int *codesBuffer,
+        int *xCoordinatesBuffer, int *yCoordinatesBuffer,
         const int codesBufferSize, const int flags, const int *codesSrc,
         const int codesRemain, const int currentDepth, int *codesDest, Correction *correction,
         WordsPriorityQueuePool *queuePool) {
 
+    const int startIndex = (codesDest - codesBuffer) / MAX_PROXIMITY_CHARS;
     if (currentDepth < MAX_UMLAUT_SEARCH_DEPTH) {
         for (int i = 0; i < codesRemain; ++i) {
+            xCoordinatesBuffer[startIndex + i] = xcoordinates[codesBufferSize - codesRemain + i];
+            yCoordinatesBuffer[startIndex + i] = ycoordinates[codesBufferSize - codesRemain + i];
             if (isDigraph(codesSrc, i, codesRemain)) {
                 // Found a digraph. We will try both spellings. eg. the word is "pruefen"
 
@@ -108,7 +112,7 @@
                 ++i;
                 memcpy(codesDest, codesSrc, i * BYTES_IN_ONE_CHAR);
                 getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates,
-                        codesBuffer, codesBufferSize, flags,
+                        codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize, flags,
                         codesSrc + (i + 1) * MAX_PROXIMITY_CHARS, codesRemain - i - 1,
                         currentDepth + 1, codesDest + i * MAX_PROXIMITY_CHARS, correction,
                         queuePool);
@@ -119,7 +123,7 @@
                 memcpy(codesDest + i * MAX_PROXIMITY_CHARS, codesSrc + i * MAX_PROXIMITY_CHARS,
                         BYTES_IN_ONE_CHAR);
                 getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates,
-                        codesBuffer, codesBufferSize, flags,
+                        codesBuffer, xCoordinatesBuffer, yCoordinatesBuffer, codesBufferSize, flags,
                         codesSrc + i * MAX_PROXIMITY_CHARS, codesRemain - i, currentDepth + 1,
                         codesDest + i * MAX_PROXIMITY_CHARS, correction, queuePool);
                 return;
@@ -133,11 +137,16 @@
     // eg. if the word is "ueberpruefen" we'll test, in order, against
     // "uberprufen", "uberpruefen", "ueberprufen", "ueberpruefen".
     const unsigned int remainingBytes = BYTES_IN_ONE_CHAR * codesRemain;
-    if (0 != remainingBytes)
+    if (0 != remainingBytes) {
         memcpy(codesDest, codesSrc, remainingBytes);
+        memcpy(&xCoordinatesBuffer[startIndex], &xcoordinates[codesBufferSize - codesRemain],
+                sizeof(int) * codesRemain);
+        memcpy(&yCoordinatesBuffer[startIndex], &ycoordinates[codesBufferSize - codesRemain],
+                sizeof(int) * codesRemain);
+    }
 
-    getWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codesBuffer,
-            (codesDest - codesBuffer) / MAX_PROXIMITY_CHARS + codesRemain, flags, correction,
+    getWordSuggestions(proximityInfo, xCoordinatesBuffer, yCoordinatesBuffer, codesBuffer,
+            startIndex + codesRemain, flags, correction,
             queuePool);
 }
 
@@ -146,11 +155,15 @@
         const int *ycoordinates, const int *codes, const int codesSize, const int flags,
         unsigned short *outWords, int *frequencies) {
 
+    queuePool->clearAll();
     Correction* masterCorrection = correction;
     if (REQUIRES_GERMAN_UMLAUT_PROCESSING & flags)
     { // Incrementally tune the word and try all possibilities
         int codesBuffer[getCodesBufferSize(codes, codesSize, MAX_PROXIMITY_CHARS)];
+        int xCoordinatesBuffer[codesSize];
+        int yCoordinatesBuffer[codesSize];
         getWordWithDigraphSuggestionsRec(proximityInfo, xcoordinates, ycoordinates, codesBuffer,
+                xCoordinatesBuffer, yCoordinatesBuffer,
                 codesSize, flags, codes, codesSize, 0, codesBuffer, masterCorrection, queuePool);
     } else { // Normal processing
         getWordSuggestions(proximityInfo, xcoordinates, ycoordinates, codes, codesSize, flags,
@@ -192,7 +205,6 @@
 
     PROF_OPEN;
     PROF_START(0);
-    queuePool->clearAll();
     PROF_END(0);
 
     PROF_START(1);
diff --git a/native/src/unigram_dictionary.h b/native/src/unigram_dictionary.h
index 396a811..12b68d7 100644
--- a/native/src/unigram_dictionary.h
+++ b/native/src/unigram_dictionary.h
@@ -89,6 +89,7 @@
     bool isDigraph(const int *codes, const int i, const int codesSize) const;
     void getWordWithDigraphSuggestionsRec(ProximityInfo *proximityInfo,
         const int *xcoordinates, const int* ycoordinates, const int *codesBuffer,
+        int *xCoordinatesBuffer, int *yCoordinatesBuffer,
         const int codesBufferSize, const int flags, const int* codesSrc,
         const int codesRemain, const int currentDepth, int* codesDest, Correction *correction,
         WordsPriorityQueuePool* queuePool);
diff --git a/tests/src/com/android/inputmethod/latin/ArbitrarySubtype.java b/tests/src/com/android/inputmethod/latin/ArbitrarySubtype.java
new file mode 100644
index 0000000..1e3482c
--- /dev/null
+++ b/tests/src/com/android/inputmethod/latin/ArbitrarySubtype.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+
+package com.android.inputmethod.latin;
+
+import com.android.inputmethod.compat.InputMethodSubtypeCompatWrapper;
+
+public class ArbitrarySubtype extends InputMethodSubtypeCompatWrapper {
+    final String mLocale;
+    final String mExtraValue;
+
+    public ArbitrarySubtype(final String locale, final String extraValue) {
+        super(locale);
+        mLocale = locale;
+        mExtraValue = extraValue;
+    }
+
+    public String getLocale() {
+        return mLocale;
+    }
+
+    public String getExtraValue() {
+        return mExtraValue;
+    }
+
+    public String getMode() {
+        return "keyboard";
+    }
+
+    public String getExtraValueOf(final String key) {
+        if (LatinIME.SUBTYPE_EXTRA_VALUE_ASCII_CAPABLE.equals(key)) {
+            return "";
+        } else {
+            return null;
+        }
+    }
+
+    public boolean containsExtraValueKey(final String key) {
+        return LatinIME.SUBTYPE_EXTRA_VALUE_ASCII_CAPABLE.equals(key);
+    }
+}
diff --git a/tests/src/com/android/inputmethod/latin/InputLogicTests.java b/tests/src/com/android/inputmethod/latin/InputLogicTests.java
index 50aba7b..a6a4d93 100644
--- a/tests/src/com/android/inputmethod/latin/InputLogicTests.java
+++ b/tests/src/com/android/inputmethod/latin/InputLogicTests.java
@@ -95,20 +95,7 @@
         mLatinIME.onStartInputView(ei, false);
         mLatinIME.onCreateInputMethodInterface().startInput(ic, ei);
         mInputConnection = ic;
-        // Wait for the main dictionary to be loaded (we need it for auto-correction tests)
-        int remainingAttempts = 10;
-        while (remainingAttempts > 0 && !mLatinIME.mSuggest.hasMainDictionary()) {
-            try {
-                Thread.sleep(100);
-            } catch (InterruptedException e) {
-                // Don't do much
-            } finally {
-                --remainingAttempts;
-            }
-        }
-        if (!mLatinIME.mSuggest.hasMainDictionary()) {
-            throw new RuntimeException("Can't initialize the main dictionary");
-        }
+        changeLanguage("en_US");
     }
 
     // We need to run the messages added to the handler from LatinIME. The only way to do
@@ -177,6 +164,29 @@
         }
     }
 
+    private void waitForDictionaryToBeLoaded() {
+        int remainingAttempts = 10;
+        while (remainingAttempts > 0 && !mLatinIME.mSuggest.hasMainDictionary()) {
+            try {
+                Thread.sleep(200);
+            } catch (InterruptedException e) {
+                // Don't do much
+            } finally {
+                --remainingAttempts;
+            }
+        }
+        if (!mLatinIME.mSuggest.hasMainDictionary()) {
+            throw new RuntimeException("Can't initialize the main dictionary");
+        }
+    }
+
+    private void changeLanguage(final String locale) {
+        SubtypeSwitcher.getInstance().updateSubtype(
+                new ArbitrarySubtype(locale, LatinIME.SUBTYPE_EXTRA_VALUE_ASCII_CAPABLE));
+        waitForDictionaryToBeLoaded();
+    }
+
+
     // Helper to avoid writing the try{}catch block each time
     private static void sleep(final int milliseconds) {
         try {
@@ -273,6 +283,15 @@
         assertEquals("simple auto-correct", EXPECTED_RESULT, mTextView.getText().toString());
     }
 
+    public void testAutoCorrectForFrench() {
+        final String STRING_TO_TYPE = "irq ";
+        final String EXPECTED_RESULT = "ira ";
+        changeLanguage("fr");
+        type(STRING_TO_TYPE);
+        assertEquals("simple auto-correct for French", EXPECTED_RESULT,
+                mTextView.getText().toString());
+    }
+
     public void testAutoCorrectWithPeriod() {
         final String STRING_TO_TYPE = "tgis.";
         final String EXPECTED_RESULT = "this.";
@@ -375,6 +394,34 @@
                 mTextView.getText().toString());
     }
 
+    public void testManualPickThenSeparatorForFrench() {
+        final String WORD1_TO_TYPE = "test";
+        final String WORD2_TO_TYPE = "!";
+        final String EXPECTED_RESULT = "test !";
+        changeLanguage("fr");
+        type(WORD1_TO_TYPE);
+        mLatinIME.pickSuggestionManually(0, WORD1_TO_TYPE);
+        type(WORD2_TO_TYPE);
+        assertEquals("manual pick then separator for French", EXPECTED_RESULT,
+                mTextView.getText().toString());
+    }
+
+    public void testWordThenSpaceThenPunctuationFromStripTwiceForFrench() {
+        final String WORD_TO_TYPE = "test ";
+        final String PUNCTUATION_FROM_STRIP = "!";
+        final String EXPECTED_RESULT = "test !!";
+        changeLanguage("fr");
+        type(WORD_TO_TYPE);
+        sleep(DELAY_TO_WAIT_FOR_UNDERLINE);
+        runMessages();
+        assertTrue("type word then type space should display punctuation strip",
+                mLatinIME.isShowingPunctuationList());
+        mLatinIME.pickSuggestionManually(0, PUNCTUATION_FROM_STRIP);
+        mLatinIME.pickSuggestionManually(0, PUNCTUATION_FROM_STRIP);
+        assertEquals("type word then type space then punctuation from strip twice for French",
+                EXPECTED_RESULT, mTextView.getText().toString());
+    }
+
     public void testWordThenSpaceThenPunctuationFromKeyboardTwice() {
         final String WORD_TO_TYPE = "this !!";
         final String EXPECTED_RESULT = "this !!";