Merge "Cancel more suggestions when its outside is touched"
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 08af5c5..00cd74b 100644
--- a/java/src/com/android/inputmethod/latin/LatinIME.java
+++ b/java/src/com/android/inputmethod/latin/LatinIME.java
@@ -35,7 +35,6 @@
 import android.preference.PreferenceManager;
 import android.text.InputType;
 import android.text.TextUtils;
-import android.util.DisplayMetrics;
 import android.util.Log;
 import android.util.PrintWriterPrinter;
 import android.util.Printer;
@@ -1011,8 +1010,12 @@
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         switch (keyCode) {
         case KeyEvent.KEYCODE_BACK:
-            if (event.getRepeatCount() == 0 && mKeyboardSwitcher.getKeyboardView() != null) {
-                if (mKeyboardSwitcher.getKeyboardView().handleBack()) {
+            if (event.getRepeatCount() == 0) {
+                if (mSuggestionsView != null && mSuggestionsView.handleBack()) {
+                    return true;
+                }
+                final LatinKeyboardView keyboardView = mKeyboardSwitcher.getKeyboardView();
+                if (keyboardView != null && keyboardView.handleBack()) {
                     return true;
                 }
             }
diff --git a/java/src/com/android/inputmethod/latin/MoreSuggestionsView.java b/java/src/com/android/inputmethod/latin/MoreSuggestionsView.java
index f54a485..695e60f 100644
--- a/java/src/com/android/inputmethod/latin/MoreSuggestionsView.java
+++ b/java/src/com/android/inputmethod/latin/MoreSuggestionsView.java
@@ -67,7 +67,10 @@
 
         @Override
         public void onCodeInput(int primaryCode, int[] keyCodes, int x, int y) {
-            mListener.onCustomRequest(primaryCode - MoreSuggestions.SUGGESTION_CODE_BASE);
+            final int index = primaryCode - MoreSuggestions.SUGGESTION_CODE_BASE;
+            if (index >= 0 && index < SuggestionsView.MAX_SUGGESTIONS) {
+                mListener.onCustomRequest(index);
+            }
         }
 
         @Override
diff --git a/java/src/com/android/inputmethod/latin/SuggestionsView.java b/java/src/com/android/inputmethod/latin/SuggestionsView.java
index 6879f76..380e73b 100644
--- a/java/src/com/android/inputmethod/latin/SuggestionsView.java
+++ b/java/src/com/android/inputmethod/latin/SuggestionsView.java
@@ -728,6 +728,10 @@
         return false;
     }
 
+    public boolean handleBack() {
+        return dismissMoreSuggestions();
+    }
+
     @Override
     public boolean onLongClick(View view) {
         final SuggestionsViewParams params = mParams;