Merge "Fix an NPE in settings"
diff --git a/java/res/values/strings.xml b/java/res/values/strings.xml
index 2b91936..37cb23b 100644
--- a/java/res/values/strings.xml
+++ b/java/res/values/strings.xml
@@ -75,6 +75,11 @@
     <!-- Description for option enabling or disabling the use of names of people in Contacts for suggestion and correction [CHAR LIMIT=65] -->
     <string name="use_contacts_dict_summary">Use names from Contacts for suggestions and corrections</string>
 
+    <!-- Option name for enabling insertion of suggestion spans (advanced option) [CHAR LIMIT=25] -->
+    <string name="enable_span_insert">Enable recorrections</string>
+    <!-- Option summary for enabling insertion of suggestion spans (advanced option) [CHAR LIMIT=65] -->
+    <string name="enable_span_insert_summary">Set suggestions for recorrections</string>
+
     <!-- Option to enable auto capitalization of sentences -->
     <string name="auto_cap">Auto-capitalization</string>
 
diff --git a/java/res/xml/prefs.xml b/java/res/xml/prefs.xml
index 2f32181..d02e5d7 100644
--- a/java/res/xml/prefs.xml
+++ b/java/res/xml/prefs.xml
@@ -132,6 +132,12 @@
              android:summary="@string/bigram_suggestion_summary"
              android:persistent="true"
              android:defaultValue="true" />
+          <CheckBoxPreference
+             android:key="enable_span_insert"
+             android:title="@string/enable_span_insert"
+             android:summary="@string/enable_span_insert_summary"
+             android:persistent="true"
+             android:defaultValue="true" />
           <!-- TODO: evaluate results and revive this option. The code already supports it. -->
           <!-- <CheckBoxPreference -->
           <!--    android:key="bigram_prediction" -->
diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
index ceadc91..96eb694 100644
--- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java
@@ -103,6 +103,8 @@
     private ViewGroup mPreviewPlacer;
 
     // Drawing
+    /** True if the entire keyboard needs to be dimmed. */
+    private boolean mNeedsToDimBackground;
     /** Whether the keyboard bitmap buffer needs to be redrawn before it's blitted. **/
     private boolean mBufferNeedsUpdate;
     /** The dirty region in the keyboard bitmap */
@@ -481,8 +483,8 @@
             }
         }
 
-        // Overlay a dark rectangle to dim the keyboard
-        if (needsToDimKeyboard()) {
+        // Overlay a dark rectangle to dim the entire keyboard
+        if (mNeedsToDimBackground) {
             mPaint.setColor((int) (mBackgroundDimAmount * 0xFF) << 24);
             canvas.drawRect(0, 0, width, height, mPaint);
         }
@@ -491,8 +493,12 @@
         mDirtyRect.setEmpty();
     }
 
-    protected boolean needsToDimKeyboard() {
-        return false;
+    public void dimEntireKeyboard(boolean dimmed) {
+        final boolean needsRedrawing = mNeedsToDimBackground != dimmed;
+        mNeedsToDimBackground = dimmed;
+        if (needsRedrawing) {
+            invalidateAllKeys();
+        }
     }
 
     private static void onBufferDrawKey(final Key key, final Keyboard keyboard, final Canvas canvas,
diff --git a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
index 777bae3..d9089e1 100644
--- a/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
+++ b/java/src/com/android/inputmethod/keyboard/LatinKeyboardView.java
@@ -373,11 +373,6 @@
         return miniKeyboardView;
     }
 
-    @Override
-    protected boolean needsToDimKeyboard() {
-        return mMoreKeysPanel != null;
-    }
-
     public void setSpacebarTextFadeFactor(float fadeFactor, LatinKeyboard oldKeyboard) {
         final Keyboard keyboard = getKeyboard();
         // We should not set text fade factor to the keyboard which does not display the language on
@@ -460,8 +455,7 @@
         final int translatedY = moreKeysPanel.translateY(tracker.getLastY());
         tracker.onShowMoreKeysPanel(
                 translatedX, translatedY, SystemClock.uptimeMillis(), moreKeysPanel);
-
-        invalidateAllKeys();
+        dimEntireKeyboard(true);
         return true;
     }
 
@@ -620,7 +614,7 @@
             mMoreKeysWindow.dismiss();
             mMoreKeysPanel = null;
             mMoreKeysPanelPointerTrackerId = -1;
-            invalidateAllKeys();
+            dimEntireKeyboard(false);
             return true;
         }
         return false;
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 85363fd..cea59fe 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -1807,9 +1807,13 @@
         final InputConnection ic = getCurrentInputConnection();
         if (ic != null) {
             mVoiceProxy.rememberReplacedWord(bestWord, mSettingsValues.mWordSeparators);
-            SuggestedWords suggestedWords = mSuggestionsView.getSuggestions();
-            ic.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan(
-                    this, bestWord, suggestedWords), 1);
+            if (mSettingsValues.mEnableSuggestionSpanInsertion) {
+                final SuggestedWords suggestedWords = mSuggestionsView.getSuggestions();
+                ic.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan(
+                        this, bestWord, suggestedWords), 1);
+            } else {
+                ic.commitText(bestWord, 1);
+            }
         }
         mRecorrection.saveRecorrectionSuggestion(mWordComposer, bestWord);
         mHasUncommittedTypedChars = false;
diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java
index db8ca34..e99bb70 100644
--- a/java/src/com/android/inputmethod/latin/Settings.java
+++ b/java/src/com/android/inputmethod/latin/Settings.java
@@ -82,6 +82,8 @@
             "pref_key_preview_popup_dismiss_delay";
     public static final String PREF_KEY_USE_CONTACTS_DICT =
             "pref_key_use_contacts_dict";
+    public static final String PREF_KEY_ENABLE_SPAN_INSERT =
+            "enable_span_insert";
 
     public static final String PREF_USABILITY_STUDY_MODE = "usability_study_mode";
 
@@ -117,6 +119,7 @@
         // Prediction: use bigrams to predict the next word when there is no input for it yet
         public final boolean mBigramPredictionEnabled;
         public final boolean mUseContactsDict;
+        public final boolean mEnableSuggestionSpanInsertion;
 
         private final boolean mShowSettingsKey;
         private final boolean mVoiceKeyEnabled;
@@ -179,6 +182,8 @@
                     && isBigramPredictionEnabled(prefs, res);
             mAutoCorrectionThreshold = getAutoCorrectionThreshold(prefs, res);
             mUseContactsDict = prefs.getBoolean(Settings.PREF_KEY_USE_CONTACTS_DICT, true);
+            mEnableSuggestionSpanInsertion =
+                    prefs.getBoolean(Settings.PREF_KEY_ENABLE_SPAN_INSERT, true);
             final boolean defaultShowSettingsKey = res.getBoolean(
                     R.bool.config_default_show_settings_key);
             mShowSettingsKey = isShowSettingsKeyOption(res)
diff --git a/java/src/com/android/inputmethod/latin/SuggestionsView.java b/java/src/com/android/inputmethod/latin/SuggestionsView.java
index 617d7f1..07a44f7 100644
--- a/java/src/com/android/inputmethod/latin/SuggestionsView.java
+++ b/java/src/com/android/inputmethod/latin/SuggestionsView.java
@@ -50,6 +50,7 @@
 import com.android.inputmethod.compat.FrameLayoutCompatUtils;
 import com.android.inputmethod.compat.LinearLayoutCompatUtils;
 import com.android.inputmethod.keyboard.KeyboardActionListener;
+import com.android.inputmethod.keyboard.KeyboardView;
 import com.android.inputmethod.keyboard.MoreKeysPanel;
 import com.android.inputmethod.keyboard.PointerTracker;
 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
@@ -70,7 +71,7 @@
 
     private final ViewGroup mSuggestionsPlacer;
     private final ViewGroup mSuggestionsStrip;
-    private View mKeyboardView;
+    private KeyboardView mKeyboardView;
 
     private final View mMoreSuggestionsContainer;
     private final MoreSuggestionsView mMoreSuggestionsView;
@@ -515,7 +516,7 @@
      */
     public void setListener(Listener listener, View inputView) {
         mListener = listener;
-        mKeyboardView = inputView.findViewById(R.id.keyboard_view);
+        mKeyboardView = (KeyboardView)inputView.findViewById(R.id.keyboard_view);
     }
 
     public void setSuggestions(SuggestedWords suggestions) {
@@ -658,7 +659,7 @@
         mSuggestionsPlacer.removeAllViews();
         mSuggestionsPlacer.addView(mSuggestionsStrip);
         mSuggestionsStrip.removeAllViews();
-        mMoreSuggestionsWindow.dismiss();
+        dismissMoreSuggestions();
     }
 
     private void hidePreview() {
@@ -702,13 +703,13 @@
             final int index = requestCode;
             final CharSequence word = mSuggestions.getWord(index);
             mListener.pickSuggestionManually(index, word);
-            mMoreSuggestionsView.dismissMoreKeysPanel();
+            dismissMoreSuggestions();
             return true;
         }
 
         @Override
         public void onCancelInput() {
-            mMoreSuggestionsView.dismissMoreKeysPanel();
+            dismissMoreSuggestions();
         }
     };
 
@@ -716,14 +717,19 @@
             new MoreKeysPanel.Controller() {
         @Override
         public boolean dismissMoreKeysPanel() {
-            if (mMoreSuggestionsWindow.isShowing()) {
-                mMoreSuggestionsWindow.dismiss();
-                return true;
-            }
-            return false;
+            return dismissMoreSuggestions();
         }
     };
 
+    private boolean dismissMoreSuggestions() {
+        if (mMoreSuggestionsWindow.isShowing()) {
+            mMoreSuggestionsWindow.dismiss();
+            mKeyboardView.dimEntireKeyboard(false);
+            return true;
+        }
+        return false;
+    }
+
     @Override
     public boolean onLongClick(View view) {
         final SuggestionsStripParams params = mStripParams;
@@ -754,7 +760,7 @@
             tracker.onShowMoreKeysPanel(
                     translatedX, translatedY, SystemClock.uptimeMillis(), moreKeysPanel);
             view.setPressed(false);
-            // TODO: Should gray out the keyboard here as well?
+            mKeyboardView.dimEntireKeyboard(true);
             return true;
         }
         return false;