Merge "Fix NPE when pressing ALT key" into lmp-dev
diff --git a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
index 6fce249..616828e 100644
--- a/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
+++ b/java/src/com/android/inputmethod/latin/inputlogic/InputLogic.java
@@ -233,6 +233,20 @@
     }
 
     /**
+     * Determines whether "Touch again to save" should be shown or not.
+     * @param suggestionInfo the suggested word chosen by the user.
+     * @return {@code true} if we should show the "Touch again to save" hint.
+     */
+    private boolean shouldShowAddToDictionaryHint(final SuggestedWordInfo suggestionInfo) {
+        // We should show the "Touch again to save" hint if the user pressed the first entry
+        // AND it's in none of our current dictionaries (main, user or otherwise).
+        return (suggestionInfo.isKindOf(SuggestedWordInfo.KIND_TYPED)
+                || suggestionInfo.isKindOf(SuggestedWordInfo.KIND_OOV_CORRECTION))
+                && !mDictionaryFacilitator.isValidWord(suggestionInfo.mWord, true /* ignoreCase */)
+                && mDictionaryFacilitator.isUserDictionaryEnabled();
+    }
+
+    /**
      * A suggestion was picked from the suggestion strip.
      * @param settingsValues the current values of the settings.
      * @param suggestionInfo the suggestion info.
@@ -298,14 +312,7 @@
         mSpaceState = SpaceState.PHANTOM;
         inputTransaction.requireShiftUpdate(InputTransaction.SHIFT_UPDATE_NOW);
 
-        // We should show the "Touch again to save" hint if the user pressed the first entry
-        // AND it's in none of our current dictionaries (main, user or otherwise).
-        final boolean showingAddToDictionaryHint =
-                (suggestionInfo.isKindOf(SuggestedWordInfo.KIND_TYPED)
-                        || suggestionInfo.isKindOf(SuggestedWordInfo.KIND_OOV_CORRECTION))
-                        && !mDictionaryFacilitator.isValidWord(suggestion, true /* ignoreCase */);
-
-        if (showingAddToDictionaryHint && mDictionaryFacilitator.isUserDictionaryEnabled()) {
+        if (shouldShowAddToDictionaryHint(suggestionInfo)) {
             mSuggestionStripViewAccessor.showAddToDictionaryHint(suggestion);
         } else {
             // If we're not showing the "Touch again to save", then update the suggestion strip.
@@ -744,6 +751,13 @@
             final InputTransaction inputTransaction,
             // TODO: remove this argument
             final LatinIME.UIHandler handler) {
+        // In case the "add to dictionary" hint was still displayed.
+        // TODO: Do we really need to check if we have composing text here?
+        if (!mWordComposer.isComposingWord() &&
+                mSuggestionStripViewAccessor.isShowingAddToDictionaryHint()) {
+            mSuggestionStripViewAccessor.dismissAddToDictionaryHint();
+        }
+
         final int codePoint = event.mCodePoint;
         mSpaceState = SpaceState.NONE;
         if (inputTransaction.mSettingsValues.isWordSeparator(codePoint)
@@ -839,8 +853,6 @@
             } else {
                 sendKeyCodePoint(settingsValues, codePoint);
             }
-            // In case the "add to dictionary" hint was still displayed.
-            mSuggestionStripViewAccessor.dismissAddToDictionaryHint();
         }
         inputTransaction.setRequiresUpdateSuggestions();
     }