Merge "Fix an NPE"
diff --git a/java/res/xml-sw600dp/kbd_rows_symbols_shift.xml b/java/res/xml-sw600dp/kbd_rows_symbols_shift.xml
index 3d62eff..73fdf2d 100644
--- a/java/res/xml-sw600dp/kbd_rows_symbols_shift.xml
+++ b/java/res/xml-sw600dp/kbd_rows_symbols_shift.xml
@@ -23,6 +23,8 @@
 >
     <include
         latin:keyboardLayout="@xml/kbd_key_styles" />
+    <include
+        latin:keyboardLayout="@xml/kbd_currency_key_styles" />
     <Row
         latin:keyWidth="9.0%p"
     >
@@ -61,11 +63,11 @@
             latin:keyLabel="£"
             latin:keyXPos="4.5%p" />
         <Key
-            latin:keyLabel="¢" />
+            latin:keyStyle="moreCurrency1KeyStyle" />
         <Key
-            latin:keyLabel="€" />
+            latin:keyStyle="moreCurrency2KeyStyle" />
         <Key
-            latin:keyLabel="¥" />
+            latin:keyStyle="moreCurrency3KeyStyle" />
         <Key
             latin:keyLabel="^"
             latin:moreKeys="↑,↓,←,→" />
diff --git a/java/res/xml-sw768dp/kbd_rows_symbols_shift.xml b/java/res/xml-sw768dp/kbd_rows_symbols_shift.xml
index 30f72ac..ec78d57 100644
--- a/java/res/xml-sw768dp/kbd_rows_symbols_shift.xml
+++ b/java/res/xml-sw768dp/kbd_rows_symbols_shift.xml
@@ -23,6 +23,8 @@
 >
     <include
         latin:keyboardLayout="@xml/kbd_key_styles" />
+    <include
+        latin:keyboardLayout="@xml/kbd_currency_key_styles" />
     <Row
         latin:keyWidth="8.282%p"
     >
@@ -66,13 +68,13 @@
             latin:keyLabelOption="alignLeft"
             latin:keyWidth="11.172%p" />
         <Key
-            latin:keyLabel="£" />
+            latin:keyStyle="moreCurrency1KeyStyle" />
         <Key
-            latin:keyLabel="¢" />
+            latin:keyStyle="moreCurrency2KeyStyle" />
         <Key
-            latin:keyLabel="€" />
+            latin:keyStyle="moreCurrency3KeyStyle" />
         <Key
-            latin:keyLabel="¥" />
+            latin:keyStyle="moreCurrency4KeyStyle" />
         <Key
             latin:keyLabel="^"
             latin:moreKeys="↑,↓,←,→" />
diff --git a/java/res/xml/kbd_currency_dollar_key_styles.xml b/java/res/xml/kbd_currency_dollar_key_styles.xml
new file mode 100644
index 0000000..d5dca2a
--- /dev/null
+++ b/java/res/xml/kbd_currency_dollar_key_styles.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 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.
+*/
+-->
+
+<merge xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin">
+    <key-style
+        latin:styleName="currencyKeyStyle"
+        latin:keyLabel="$"
+        latin:moreKeys="@string/more_keys_for_currency_dollar" />
+    <key-style
+        latin:styleName="moreCurrency1KeyStyle"
+        latin:keyLabel="£" />
+    <key-style
+        latin:styleName="moreCurrency2KeyStyle"
+        latin:keyLabel="¢" />
+    <key-style
+        latin:styleName="moreCurrency3KeyStyle"
+        latin:keyLabel="€" />
+    <key-style
+        latin:styleName="moreCurrency4KeyStyle"
+        latin:keyLabel="¥" />
+</merge>
diff --git a/java/res/xml/kbd_currency_euro_key_styles.xml b/java/res/xml/kbd_currency_euro_key_styles.xml
new file mode 100644
index 0000000..6edddf0
--- /dev/null
+++ b/java/res/xml/kbd_currency_euro_key_styles.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+/*
+**
+** Copyright 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.
+*/
+-->
+
+<merge xmlns:latin="http://schemas.android.com/apk/res/com.android.inputmethod.latin">
+    <key-style
+        latin:styleName="currencyKeyStyle"
+        latin:keyLabel="€"
+        latin:moreKeys="@string/more_keys_for_currency_euro" />
+    <key-style
+        latin:styleName="moreCurrency1KeyStyle"
+        latin:keyLabel="£" />
+    <key-style
+        latin:styleName="moreCurrency2KeyStyle"
+        latin:keyLabel="¥" />
+    <key-style
+        latin:styleName="moreCurrency3KeyStyle"
+        latin:keyLabel="$"
+        latin:moreKeys="¢" />
+    <key-style
+        latin:styleName="moreCurrency4KeyStyle"
+        latin:keyLabel="¢" />
+</merge>
diff --git a/java/res/xml/kbd_currency_key_styles.xml b/java/res/xml/kbd_currency_key_styles.xml
index bcdfe56..2258883 100644
--- a/java/res/xml/kbd_currency_key_styles.xml
+++ b/java/res/xml/kbd_currency_key_styles.xml
@@ -25,9 +25,8 @@
         <case
             latin:passwordInput="true"
         >
-            <key-style
-                latin:styleName="currencyKeyStyle"
-                latin:keyLabel="$" />
+            <include
+                latin:keyboardLayout="@xml/kbd_currency_dollar_key_styles" />
         </case>
         <!-- Countries using Euro currency, 23 countries as for January 2011. -->
               1. Andorra (ca_AD, ca_ES)
@@ -54,31 +53,28 @@
              22. Spain (es_ES, ca_ES)
              23. Vatican City (it_VA)
         -->
+        <!-- Though Denmark and Turkey don't using Euro as their currencies, but having Euro sign on
+             the symbol keyboard might be useful.  Especially Danish krone (kr) and Turkish lira
+             (TL) can be represented by usual alphabet letters. -->
         <!-- Note: Some locales may not have country code, and it it supposed to indicate the
              country where the language originally/mainly spoken. -->
         <case
-            latin:localeCode="de|es|el|fi|fr|it|nl|sk|sl|pt_PT"
+            latin:localeCode="da|de|es|el|fi|fr|it|nl|sk|sl|pt_PT|tr"
         >
-            <key-style
-                latin:styleName="currencyKeyStyle"
-                latin:keyLabel="€"
-                latin:moreKeys="@string/more_keys_for_currency_euro" />
+            <include
+                latin:keyboardLayout="@xml/kbd_currency_euro_key_styles" />
         </case>
         <case
             latin:languageCode="ca|et|lb|mt|sla"
         >
-            <key-style
-                latin:styleName="currencyKeyStyle"
-                latin:keyLabel="€"
-                latin:moreKeys="@string/more_keys_for_currency_euro" />
+            <include
+                latin:keyboardLayout="@xml/kbd_currency_euro_key_styles" />
         </case>
         <case
             latin:countryCode="AD|AT|BE|CY|EE|FI|FR|DE|GR|IE|IT|XK|LU|MT|MO|ME|NL|PT|SM|SK|SI|ES|VA"
         >
-            <key-style
-                latin:styleName="currencyKeyStyle"
-                latin:keyLabel="€"
-                latin:moreKeys="@string/more_keys_for_currency_euro" />
+            <include
+                latin:keyboardLayout="@xml/kbd_currency_euro_key_styles" />
         </case>
         <case
             latin:languageCode="iw"
@@ -87,6 +83,19 @@
                 latin:styleName="currencyKeyStyle"
                 latin:keyLabel="₪"
                 latin:moreKeys="@string/more_keys_for_currency_general" />
+            <key-style
+                latin:styleName="moreCurrency1KeyStyle"
+                latin:keyLabel="£" />
+            <key-style
+                latin:styleName="moreCurrency2KeyStyle"
+                latin:keyLabel="€" />
+            <key-style
+                latin:styleName="moreCurrency3KeyStyle"
+                latin:keyLabel="$"
+                latin:moreKeys="¢" />
+            <key-style
+                latin:styleName="moreCurrency4KeyStyle"
+                latin:keyLabel="¢" />
         </case>
         <!-- United Kingdom -->
         <case
@@ -96,12 +105,23 @@
                 latin:styleName="currencyKeyStyle"
                 latin:keyLabel="£"
                 latin:moreKeys="@string/more_keys_for_currency_pound" />
+            <key-style
+                latin:styleName="moreCurrency1KeyStyle"
+                latin:keyLabel="€" />
+            <key-style
+                latin:styleName="moreCurrency2KeyStyle"
+                latin:keyLabel="¥" />
+            <key-style
+                latin:styleName="moreCurrency3KeyStyle"
+                latin:keyLabel="$"
+                latin:moreKeys="¢" />
+            <key-style
+                latin:styleName="moreCurrency4KeyStyle"
+                latin:keyLabel="¢" />
         </case>
         <default>
-            <key-style
-                latin:styleName="currencyKeyStyle"
-                latin:keyLabel="$"
-                latin:moreKeys="@string/more_keys_for_currency_dollar" />
+            <include
+                latin:keyboardLayout="@xml/kbd_currency_dollar_key_styles" />
         </default>
     </switch>
 </merge>
diff --git a/java/res/xml/kbd_rows_symbols_shift.xml b/java/res/xml/kbd_rows_symbols_shift.xml
index 66c14fe..58ae72c 100644
--- a/java/res/xml/kbd_rows_symbols_shift.xml
+++ b/java/res/xml/kbd_rows_symbols_shift.xml
@@ -23,6 +23,8 @@
 >
     <include
         latin:keyboardLayout="@xml/kbd_key_styles" />
+    <include
+        latin:keyboardLayout="@xml/kbd_currency_key_styles" />
     <Row
         latin:keyWidth="10%p"
     >
@@ -56,11 +58,11 @@
         <Key
             latin:keyStyle="nonSpecialBackgroundTabKeyStyle" />
         <Key
-            latin:keyLabel="£" />
+            latin:keyStyle="moreCurrency1KeyStyle" />
         <Key
-            latin:keyLabel="¢" />
+            latin:keyStyle="moreCurrency2KeyStyle" />
         <Key
-            latin:keyLabel="€" />
+            latin:keyStyle="moreCurrency3KeyStyle" />
         <Key
             latin:keyLabel="°"
             latin:moreKeys="′,″" />
diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java
index 8431851..b467a32 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 f595510..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
@@ -142,6 +145,13 @@
         // Nothing to do with.
     }
 
+    private final View.OnTouchListener mMotionEventDelegate = new View.OnTouchListener() {
+        @Override
+        public boolean onTouch(View view, MotionEvent me) {
+            return MoreSuggestionsView.this.dispatchTouchEvent(me);
+        }
+    };
+
     @Override
     public void showMoreKeysPanel(View parentView, Controller controller, int pointX, int pointY,
             PopupWindow window, KeyboardActionListener listener) {
@@ -160,6 +170,10 @@
                 - (container.getMeasuredHeight() - container.getPaddingBottom())
                 + parentView.getPaddingTop() + mCoordinates[1];
 
+        container.setOnTouchListener(mMotionEventDelegate);
+        window.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
+        window.setFocusable(true);
+        window.setOutsideTouchable(true);
         window.setContentView(container);
         window.setWidth(container.getMeasuredWidth());
         window.setHeight(container.getMeasuredHeight());
@@ -215,6 +229,19 @@
     };
 
     @Override
+    public boolean dispatchTouchEvent(MotionEvent me) {
+        final int x = (int)me.getX();
+        final int y = (int)me.getY();
+        final boolean inside = (x >= 0 && x < getWidth() && y >= 0 && y < getHeight());
+        if (inside) {
+            return super.dispatchTouchEvent(me);
+        } else {
+            dismissMoreKeysPanel();
+            return true;
+        }
+    }
+
+    @Override
     public boolean onTouchEvent(MotionEvent me) {
         final int action = me.getAction();
         final long eventTime = me.getEventTime();
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;