Merge "[CB25] Refactor some array copying"
diff --git a/java/res/xml/key_styles_currency.xml b/java/res/xml/key_styles_currency.xml
index 29e2eae..0bb2bb4 100644
--- a/java/res/xml/key_styles_currency.xml
+++ b/java/res/xml/key_styles_currency.xml
@@ -129,6 +129,32 @@
                 latin:styleName="moreCurrency4KeyStyle"
                 latin:keySpec="¢" />
         </case>
+        <!-- IN: India (Rupee) -->
+        <case
+            latin:countryCode="IN"
+        >
+            <!-- U+20B9: "₹" INDIAN RUPEE SIGN
+                 U+00A3: "£" POUND SIGN
+                 U+20AC: "€" EURO SIGN
+                 U+00A2: "¢" CENT SIGN -->
+            <key-style
+                latin:styleName="currencyKeyStyle"
+                latin:keySpec="&#x20B9;"
+                latin:moreKeys="!text/morekeys_currency" />
+            <key-style
+                latin:styleName="moreCurrency1KeyStyle"
+                latin:keySpec="&#x00A3;" />
+            <key-style
+                latin:styleName="moreCurrency2KeyStyle"
+                latin:keySpec="&#x20AC;" />
+            <key-style
+                latin:styleName="moreCurrency3KeyStyle"
+                latin:keySpec="$"
+                latin:moreKeys="&#x00A2;" />
+            <key-style
+                latin:styleName="moreCurrency4KeyStyle"
+                latin:keySpec="&#x00A2;" />
+        </case>
         <!-- GB: United Kingdom (Pound) -->
         <case
             latin:countryCode="GB"
diff --git a/java/res/xml/method.xml b/java/res/xml/method.xml
index 6db80b8..1bef3c2 100644
--- a/java/res/xml/method.xml
+++ b/java/res/xml/method.xml
@@ -34,6 +34,7 @@
     de: German/qwertz
     de_CH: German (Switzerland)/swiss
     el: Greek/greek
+    en_IN: English (India)/qwerty
     en_US: English (United States)/qwerty
     en_GB: English (Great Britain)/qwerty
     eo: Esperanto/spanish
@@ -217,6 +218,14 @@
     />
     <subtype android:icon="@drawable/ic_ime_switcher_dark"
             android:label="@string/subtype_generic"
+            android:subtypeId="0x8d58fc2d"
+            android:imeSubtypeLocale="en_IN"
+            android:imeSubtypeMode="keyboard"
+            android:imeSubtypeExtraValue="KeyboardLayoutSet=qwerty,AsciiCapable,EmojiCapable"
+            android:isAsciiCapable="true"
+    />
+    <subtype android:icon="@drawable/ic_ime_switcher_dark"
+            android:label="@string/subtype_generic"
             android:subtypeId="0x4090554a"
             android:imeSubtypeLocale="eo"
             android:imeSubtypeMode="keyboard"
diff --git a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.java b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.java
index 1bd9833..f0356df 100644
--- a/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.java
+++ b/java/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.java
@@ -3656,7 +3656,7 @@
 
     private static final Object[] LOCALES_AND_TEXTS = {
     // "locale", TEXT_ARRAY,  /* numberOfNonNullText/lengthOf_TEXT_ARRAY localeName */
-        "DEFAULT", TEXTS_DEFAULT, /* 168/168 default */
+        "DEFAULT", TEXTS_DEFAULT, /* 168/168 DEFAULT */
         "af"     , TEXTS_af,    /*   7/ 12 Afrikaans */
         "ar"     , TEXTS_ar,    /*  55/107 Arabic */
         "az_AZ"  , TEXTS_az_AZ, /*   8/ 17 Azerbaijani (Azerbaijan) */
diff --git a/native/jni/NativeFileList.mk b/native/jni/NativeFileList.mk
index 1031903..70a6638 100644
--- a/native/jni/NativeFileList.mk
+++ b/native/jni/NativeFileList.mk
@@ -27,7 +27,6 @@
         dic_nodes_cache.cpp) \
     $(addprefix suggest/core/dictionary/, \
         bigram_dictionary.cpp \
-        bloom_filter.cpp \
         dictionary.cpp \
         digraph_utils.cpp \
         error_type_utils.cpp \
@@ -101,4 +100,7 @@
         time_keeper.cpp)
 
 LATIN_IME_CORE_TEST_FILES := \
+    defines_test.cpp \
+    suggest/core/layout/normal_distribution_2d_test.cpp \
+    suggest/core/dictionary/bloom_filter_test.cpp \
     utils/autocorrection_threshold_utils_test.cpp
diff --git a/native/jni/src/defines.h b/native/jni/src/defines.h
index 1719b1c..3becc79 100644
--- a/native/jni/src/defines.h
+++ b/native/jni/src/defines.h
@@ -35,7 +35,13 @@
 // Must be equal to ProximityInfo.MAX_PROXIMITY_CHARS_SIZE in Java
 #define MAX_PROXIMITY_CHARS_SIZE 16
 #define ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE 2
-#define NELEMS(x) (sizeof(x) / sizeof((x)[0]))
+
+// TODO: Use size_t instead of int.
+// Disclaimer: You will see a compile error if you use this macro against a variable-length array.
+// Sorry for the inconvenience. It isn't supported.
+template <typename T, int N>
+char (&ArraySizeHelper(T (&array)[N]))[N];
+#define NELEMS(x) (sizeof(ArraySizeHelper(x)))
 
 AK_FORCE_INLINE static int intArrayToCharArray(const int *const source, const int sourceSize,
         char *dest, const int destSize) {
diff --git a/native/jni/src/suggest/core/dictionary/bloom_filter.cpp b/native/jni/src/suggest/core/dictionary/bloom_filter.cpp
deleted file mode 100644
index 4ae474e..0000000
--- a/native/jni/src/suggest/core/dictionary/bloom_filter.cpp
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (C) 2013, 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.
- */
-
-#include "suggest/core/dictionary/bloom_filter.h"
-
-namespace latinime {
-
-// Must be smaller than BIGRAM_FILTER_BYTE_SIZE * 8, and preferably prime. 1021 is the largest
-// prime under 128 * 8.
-const int BloomFilter::BIGRAM_FILTER_MODULO = 1021;
-
-} // namespace latinime
diff --git a/native/jni/src/suggest/core/dictionary/bloom_filter.h b/native/jni/src/suggest/core/dictionary/bloom_filter.h
index 85b8fc3..1e60f49 100644
--- a/native/jni/src/suggest/core/dictionary/bloom_filter.h
+++ b/native/jni/src/suggest/core/dictionary/bloom_filter.h
@@ -17,8 +17,7 @@
 #ifndef LATINIME_BLOOM_FILTER_H
 #define LATINIME_BLOOM_FILTER_H
 
-#include <cstdint>
-#include <cstring>
+#include <bitset>
 
 #include "defines.h"
 
@@ -34,41 +33,37 @@
 //   Total 148603.14 (sum of others 148579.90)
 class BloomFilter {
  public:
-    BloomFilter() {
-        ASSERT(BIGRAM_FILTER_BYTE_SIZE * 8 >= BIGRAM_FILTER_MODULO);
-        memset(mFilter, 0, sizeof(mFilter));
+    BloomFilter() : mFilter() {}
+
+    AK_FORCE_INLINE void setInFilter(const int position) {
+        mFilter.set(getIndex(position));
     }
 
-    // TODO: uint32_t position
-    AK_FORCE_INLINE void setInFilter(const int32_t position) {
-        const uint32_t bucket = static_cast<uint32_t>(position % BIGRAM_FILTER_MODULO);
-        mFilter[bucket >> 3] |= static_cast<uint8_t>(1 << (bucket & 0x7));
-    }
-
-    // TODO: uint32_t position
-    AK_FORCE_INLINE bool isInFilter(const int32_t position) const {
-        const uint32_t bucket = static_cast<uint32_t>(position % BIGRAM_FILTER_MODULO);
-        return (mFilter[bucket >> 3] & static_cast<uint8_t>(1 << (bucket & 0x7))) != 0;
+    AK_FORCE_INLINE bool isInFilter(const int position) const {
+        return mFilter.test(getIndex(position));
     }
 
  private:
     DISALLOW_ASSIGNMENT_OPERATOR(BloomFilter);
 
-    // Size, in bytes, of the bloom filter index for bigrams
-    // 128 gives us 1024 buckets. The probability of false positive is (1 - e ** (-kn/m))**k,
+    AK_FORCE_INLINE size_t getIndex(const int position) const {
+        return static_cast<size_t>(position) % BIGRAM_FILTER_MODULO;
+    }
+
+    // Size, in bits, of the bloom filter index for bigrams
+    // The probability of false positive is (1 - e ** (-kn/m))**k,
     // where k is the number of hash functions, n the number of bigrams, and m the number of
     // bits we can test.
-    // At the moment 100 is the maximum number of bigrams for a word with the current
+    // At the moment 100 is the maximum number of bigrams for a word with the current main
     // dictionaries, so n = 100. 1024 buckets give us m = 1024.
     // With 1 hash function, our false positive rate is about 9.3%, which should be enough for
     // our uses since we are only using this to increase average performance. For the record,
     // k = 2 gives 3.1% and k = 3 gives 1.6%. With k = 1, making m = 2048 gives 4.8%,
     // and m = 4096 gives 2.4%.
-    // This is assigned here because it is used for array size.
-    static const int BIGRAM_FILTER_BYTE_SIZE = 128;
-    static const int BIGRAM_FILTER_MODULO;
-
-    uint8_t mFilter[BIGRAM_FILTER_BYTE_SIZE];
+    // This is assigned here because it is used for bitset size.
+    // 1021 is the largest prime under 1024.
+    static const size_t BIGRAM_FILTER_MODULO = 1021;
+    std::bitset<BIGRAM_FILTER_MODULO> mFilter;
 };
 } // namespace latinime
 #endif // LATINIME_BLOOM_FILTER_H
diff --git a/native/jni/src/suggest/core/layout/normal_distribution_2d.h b/native/jni/src/suggest/core/layout/normal_distribution_2d.h
new file mode 100644
index 0000000..3bc0a01
--- /dev/null
+++ b/native/jni/src/suggest/core/layout/normal_distribution_2d.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+#ifndef LATINIME_NORMAL_DISTRIBUTION_2D_H
+#define LATINIME_NORMAL_DISTRIBUTION_2D_H
+
+#include <cmath>
+
+#include "defines.h"
+#include "suggest/core/layout/geometry_utils.h"
+#include "suggest/core/layout/normal_distribution.h"
+
+namespace latinime {
+
+// Normal distribution on a 2D plane. The covariance is always zero, but the distribution can be
+// rotated.
+class NormalDistribution2D {
+ public:
+    NormalDistribution2D(const float uX, const float sigmaX, const float uY, const float sigmaY,
+            const float theta)
+            : mXDistribution(0.0f, sigmaX), mYDistribution(0.0f, sigmaY), mUX(uX), mUY(uY),
+              mSinTheta(sinf(theta)), mCosTheta(cosf(theta)) {}
+
+    float getProbabilityDensity(const float x, const float y) const {
+        // Shift
+        const float shiftedX = x - mUX;
+        const float shiftedY = y - mUY;
+        // Rotate
+        const float rotatedShiftedX = mCosTheta * shiftedX + mSinTheta * shiftedY;
+        const float rotatedShiftedY = -mSinTheta * shiftedX + mCosTheta * shiftedY;
+        return mXDistribution.getProbabilityDensity(rotatedShiftedX)
+                * mYDistribution.getProbabilityDensity(rotatedShiftedY);
+    }
+
+ private:
+    DISALLOW_IMPLICIT_CONSTRUCTORS(NormalDistribution2D);
+
+    const NormalDistribution mXDistribution;
+    const NormalDistribution mYDistribution;
+    const float mUX;
+    const float mUY;
+    const float mSinTheta;
+    const float mCosTheta;
+};
+} // namespace latinime
+#endif // LATINIME_NORMAL_DISTRIBUTION_2D_H
diff --git a/native/jni/src/suggest/core/layout/proximity_info_params.cpp b/native/jni/src/suggest/core/layout/proximity_info_params.cpp
index 597518a..a70dd7e 100644
--- a/native/jni/src/suggest/core/layout/proximity_info_params.cpp
+++ b/native/jni/src/suggest/core/layout/proximity_info_params.cpp
@@ -76,8 +76,12 @@
 const float ProximityInfoParams::SPEEDxNEAREST_WEIGHT_FOR_STANDARD_DEVIATION = 0.5f;
 const float ProximityInfoParams::MAX_SPEEDxNEAREST_RATE_FOR_STANDARD_DEVIATION = 0.15f;
 const float ProximityInfoParams::MIN_STANDARD_DEVIATION = 0.37f;
-const float ProximityInfoParams::PREV_DISTANCE_WEIGHT = 0.5f;
-const float ProximityInfoParams::NEXT_DISTANCE_WEIGHT = 0.6f;
+const float ProximityInfoParams::STANDARD_DEVIATION_X_WEIGHT_FOR_FIRST = 1.25f;
+const float ProximityInfoParams::STANDARD_DEVIATION_Y_WEIGHT_FOR_FIRST = 0.85f;
+const float ProximityInfoParams::STANDARD_DEVIATION_X_WEIGHT_FOR_LAST = 1.4f;
+const float ProximityInfoParams::STANDARD_DEVIATION_Y_WEIGHT_FOR_LAST = 0.95f;
+const float ProximityInfoParams::STANDARD_DEVIATION_X_WEIGHT = 1.1f;
+const float ProximityInfoParams::STANDARD_DEVIATION_Y_WEIGHT = 0.95f;
 
 // Used by ProximityInfoStateUtils::suppressCharProbabilities()
 const float ProximityInfoParams::SUPPRESSION_LENGTH_WEIGHT = 1.5f;
diff --git a/native/jni/src/suggest/core/layout/proximity_info_params.h b/native/jni/src/suggest/core/layout/proximity_info_params.h
index ae1f82c..b8e9f5d 100644
--- a/native/jni/src/suggest/core/layout/proximity_info_params.h
+++ b/native/jni/src/suggest/core/layout/proximity_info_params.h
@@ -78,8 +78,13 @@
     static const float SPEEDxNEAREST_WEIGHT_FOR_STANDARD_DEVIATION;
     static const float MAX_SPEEDxNEAREST_RATE_FOR_STANDARD_DEVIATION;
     static const float MIN_STANDARD_DEVIATION;
-    static const float PREV_DISTANCE_WEIGHT;
-    static const float NEXT_DISTANCE_WEIGHT;
+    // X means gesture's direction. Y means gesture's orthogonal direction.
+    static const float STANDARD_DEVIATION_X_WEIGHT_FOR_FIRST;
+    static const float STANDARD_DEVIATION_Y_WEIGHT_FOR_FIRST;
+    static const float STANDARD_DEVIATION_X_WEIGHT_FOR_LAST;
+    static const float STANDARD_DEVIATION_Y_WEIGHT_FOR_LAST;
+    static const float STANDARD_DEVIATION_X_WEIGHT;
+    static const float STANDARD_DEVIATION_Y_WEIGHT;
 
     // Used by ProximityInfoStateUtils::suppressCharProbabilities()
     static const float SUPPRESSION_LENGTH_WEIGHT;
diff --git a/native/jni/src/suggest/core/layout/proximity_info_state.cpp b/native/jni/src/suggest/core/layout/proximity_info_state.cpp
index 2919904..b75c2ef 100644
--- a/native/jni/src/suggest/core/layout/proximity_info_state.cpp
+++ b/native/jni/src/suggest/core/layout/proximity_info_state.cpp
@@ -134,7 +134,7 @@
                     mProximityInfo->getKeyCount(), lastSavedInputSize, mSampledInputSize,
                     &mSampledInputXs, &mSampledInputYs, &mSpeedRates, &mSampledLengthCache,
                     &mSampledNormalizedSquaredLengthCache, &mSampledNearKeySets,
-                    &mCharProbabilities);
+                    mProximityInfo, &mCharProbabilities);
             ProximityInfoStateUtils::updateSampledSearchKeySets(mProximityInfo,
                     mSampledInputSize, lastSavedInputSize, &mSampledLengthCache,
                     &mSampledNearKeySets, &mSampledSearchKeySets,
diff --git a/native/jni/src/suggest/core/layout/proximity_info_state_utils.cpp b/native/jni/src/suggest/core/layout/proximity_info_state_utils.cpp
index 5a3ff73..638297e 100644
--- a/native/jni/src/suggest/core/layout/proximity_info_state_utils.cpp
+++ b/native/jni/src/suggest/core/layout/proximity_info_state_utils.cpp
@@ -24,7 +24,7 @@
 
 #include "defines.h"
 #include "suggest/core/layout/geometry_utils.h"
-#include "suggest/core/layout/normal_distribution.h"
+#include "suggest/core/layout/normal_distribution_2d.h"
 #include "suggest/core/layout/proximity_info.h"
 #include "suggest/core/layout/proximity_info_params.h"
 
@@ -627,6 +627,7 @@
         const std::vector<int> *const sampledLengthCache,
         const std::vector<float> *const sampledNormalizedSquaredLengthCache,
         std::vector<NearKeycodesSet> *sampledNearKeySets,
+        const ProximityInfo *const proximityInfo,
         std::vector<hash_map_compat<int, float> > *charProbabilities) {
     charProbabilities->resize(sampledInputSize);
     // Calculates probabilities of using a point as a correlated point with the character
@@ -709,89 +710,57 @@
         // (1.0f - skipProbability).
         const float inputCharProbability = 1.0f - skipProbability;
 
-        const float speedxAngleRate = std::min(speedRate * currentAngle / M_PI_F
+        const float speedMultipliedByAngleRate = std::min(speedRate * currentAngle / M_PI_F
                 * ProximityInfoParams::SPEEDxANGLE_WEIGHT_FOR_STANDARD_DEVIATION,
                         ProximityInfoParams::MAX_SPEEDxANGLE_RATE_FOR_STANDARD_DEVIATION);
-        const float speedxNearestKeyDistanceRate = std::min(speedRate * nearestKeyDistance
-                * ProximityInfoParams::SPEEDxNEAREST_WEIGHT_FOR_STANDARD_DEVIATION,
-                        ProximityInfoParams::MAX_SPEEDxNEAREST_RATE_FOR_STANDARD_DEVIATION);
-        const float sigma = speedxAngleRate + speedxNearestKeyDistanceRate
-                + ProximityInfoParams::MIN_STANDARD_DEVIATION;
-
-        NormalDistribution distribution(
-                ProximityInfoParams::CENTER_VALUE_OF_NORMALIZED_DISTRIBUTION, sigma);
+        const float speedMultipliedByNearestKeyDistanceRate = std::min(
+                speedRate * nearestKeyDistance
+                        * ProximityInfoParams::SPEEDxNEAREST_WEIGHT_FOR_STANDARD_DEVIATION,
+                                ProximityInfoParams::MAX_SPEEDxNEAREST_RATE_FOR_STANDARD_DEVIATION);
+        const float sigma = (speedMultipliedByAngleRate + speedMultipliedByNearestKeyDistanceRate
+                + ProximityInfoParams::MIN_STANDARD_DEVIATION) * mostCommonKeyWidth;
+        float theta = 0.0f;
+        // TODO: Use different metrics to compute sigmas.
+        float sigmaX = sigma;
+        float sigmaY = sigma;
+        if (i == 0 && i != sampledInputSize - 1) {
+            // First point
+            theta = getDirection(sampledInputXs, sampledInputYs, i + 1, i);
+            sigmaX *= ProximityInfoParams::STANDARD_DEVIATION_X_WEIGHT_FOR_FIRST;
+            sigmaY *= ProximityInfoParams::STANDARD_DEVIATION_Y_WEIGHT_FOR_FIRST;
+        } else {
+            if (i == sampledInputSize - 1) {
+                // Last point
+                sigmaX *= ProximityInfoParams::STANDARD_DEVIATION_X_WEIGHT_FOR_LAST;
+                sigmaY *= ProximityInfoParams::STANDARD_DEVIATION_Y_WEIGHT_FOR_LAST;
+            } else {
+                sigmaX *= ProximityInfoParams::STANDARD_DEVIATION_X_WEIGHT;
+                sigmaY *= ProximityInfoParams::STANDARD_DEVIATION_Y_WEIGHT;
+            }
+            theta = getDirection(sampledInputXs, sampledInputYs, i, i - 1);
+        }
+        NormalDistribution2D distribution((*sampledInputXs)[i], sigmaX, (*sampledInputYs)[i],
+                sigmaY, theta);
         // Summing up probability densities of all near keys.
         float sumOfProbabilityDensities = 0.0f;
         for (int j = 0; j < keyCount; ++j) {
             if ((*sampledNearKeySets)[i].test(j)) {
-                float distance = sqrtf(getPointToKeyByIdLength(
-                        maxPointToKeyLength, sampledNormalizedSquaredLengthCache, keyCount, i, j));
-                if (i == 0 && i != sampledInputSize - 1) {
-                    // For the first point, weighted average of distances from first point and the
-                    // next point to the key is used as a point to key distance.
-                    const float nextDistance = sqrtf(getPointToKeyByIdLength(
-                            maxPointToKeyLength, sampledNormalizedSquaredLengthCache, keyCount,
-                            i + 1, j));
-                    if (nextDistance < distance) {
-                        // The distance of the first point tends to bigger than continuing
-                        // points because the first touch by the user can be sloppy.
-                        // So we promote the first point if the distance of that point is larger
-                        // than the distance of the next point.
-                        distance = (distance
-                                + nextDistance * ProximityInfoParams::NEXT_DISTANCE_WEIGHT)
-                                        / (1.0f + ProximityInfoParams::NEXT_DISTANCE_WEIGHT);
-                    }
-                } else if (i != 0 && i == sampledInputSize - 1) {
-                    // For the first point, weighted average of distances from last point and
-                    // the previous point to the key is used as a point to key distance.
-                    const float previousDistance = sqrtf(getPointToKeyByIdLength(
-                            maxPointToKeyLength, sampledNormalizedSquaredLengthCache, keyCount,
-                            i - 1, j));
-                    if (previousDistance < distance) {
-                        // The distance of the last point tends to bigger than continuing points
-                        // because the last touch by the user can be sloppy. So we promote the
-                        // last point if the distance of that point is larger than the distance of
-                        // the previous point.
-                        distance = (distance
-                                + previousDistance * ProximityInfoParams::PREV_DISTANCE_WEIGHT)
-                                        / (1.0f + ProximityInfoParams::PREV_DISTANCE_WEIGHT);
-                    }
-                }
-                // TODO: Promote the first point when the extended line from the next input is near
-                // from a key. Also, promote the last point as well.
-                sumOfProbabilityDensities += distribution.getProbabilityDensity(distance);
+                sumOfProbabilityDensities += distribution.getProbabilityDensity(
+                        proximityInfo->getKeyCenterXOfKeyIdG(j,
+                                NOT_A_COORDINATE /* referencePointX */, true /* isGeometric */),
+                        proximityInfo->getKeyCenterYOfKeyIdG(j,
+                                NOT_A_COORDINATE /* referencePointY */, true /* isGeometric */));
             }
         }
 
         // Split the probability of an input point to keys that are close to the input point.
         for (int j = 0; j < keyCount; ++j) {
             if ((*sampledNearKeySets)[i].test(j)) {
-                float distance = sqrtf(getPointToKeyByIdLength(
-                        maxPointToKeyLength, sampledNormalizedSquaredLengthCache, keyCount, i, j));
-                if (i == 0 && i != sampledInputSize - 1) {
-                    // For the first point, weighted average of distances from the first point and
-                    // the next point to the key is used as a point to key distance.
-                    const float prevDistance = sqrtf(getPointToKeyByIdLength(
-                            maxPointToKeyLength, sampledNormalizedSquaredLengthCache, keyCount,
-                            i + 1, j));
-                    if (prevDistance < distance) {
-                        distance = (distance
-                                + prevDistance * ProximityInfoParams::NEXT_DISTANCE_WEIGHT)
-                                        / (1.0f + ProximityInfoParams::NEXT_DISTANCE_WEIGHT);
-                    }
-                } else if (i != 0 && i == sampledInputSize - 1) {
-                    // For the first point, weighted average of distances from last point and
-                    // the previous point to the key is used as a point to key distance.
-                    const float prevDistance = sqrtf(getPointToKeyByIdLength(
-                            maxPointToKeyLength, sampledNormalizedSquaredLengthCache, keyCount,
-                            i - 1, j));
-                    if (prevDistance < distance) {
-                        distance = (distance
-                                + prevDistance * ProximityInfoParams::PREV_DISTANCE_WEIGHT)
-                                        / (1.0f + ProximityInfoParams::PREV_DISTANCE_WEIGHT);
-                    }
-                }
-                const float probabilityDensity = distribution.getProbabilityDensity(distance);
+                const float probabilityDensity = distribution.getProbabilityDensity(
+                        proximityInfo->getKeyCenterXOfKeyIdG(j,
+                                NOT_A_COORDINATE /* referencePointX */, true /* isGeometric */),
+                        proximityInfo->getKeyCenterYOfKeyIdG(j,
+                                NOT_A_COORDINATE /* referencePointY */, true /* isGeometric */));
                 const float probability = inputCharProbability * probabilityDensity
                         / sumOfProbabilityDensities;
                 (*charProbabilities)[i][j] = probability;
diff --git a/native/jni/src/suggest/core/layout/proximity_info_state_utils.h b/native/jni/src/suggest/core/layout/proximity_info_state_utils.h
index ea0cd11..5d7a9c5 100644
--- a/native/jni/src/suggest/core/layout/proximity_info_state_utils.h
+++ b/native/jni/src/suggest/core/layout/proximity_info_state_utils.h
@@ -72,6 +72,7 @@
             const std::vector<int> *const sampledLengthCache,
             const std::vector<float> *const sampledNormalizedSquaredLengthCache,
             std::vector<NearKeycodesSet> *sampledNearKeySets,
+            const ProximityInfo *const proximityInfo,
             std::vector<hash_map_compat<int, float> > *charProbabilities);
     static void updateSampledSearchKeySets(const ProximityInfo *const proximityInfo,
             const int sampledInputSize, const int lastSavedInputSize,
diff --git a/native/jni/tests/defines_test.cpp b/native/jni/tests/defines_test.cpp
new file mode 100644
index 0000000..f7b80b2
--- /dev/null
+++ b/native/jni/tests/defines_test.cpp
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+#include "defines.h"
+
+#include <gtest/gtest.h>
+
+namespace latinime {
+namespace {
+
+TEST(DefinesTest, NELEMSForFixedLengthArray) {
+    const size_t SMALL_ARRAY_SIZE = 1;
+    const size_t LARGE_ARRAY_SIZE = 100;
+    int smallArray[SMALL_ARRAY_SIZE];
+    int largeArray[LARGE_ARRAY_SIZE];
+    EXPECT_EQ(SMALL_ARRAY_SIZE, NELEMS(smallArray));
+    EXPECT_EQ(LARGE_ARRAY_SIZE, NELEMS(largeArray));
+}
+
+}  // namespace
+}  // namespace latinime
diff --git a/native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp b/native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp
new file mode 100644
index 0000000..b620217
--- /dev/null
+++ b/native/jni/tests/suggest/core/dictionary/bloom_filter_test.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+#include "suggest/core/dictionary/bloom_filter.h"
+
+#include <gtest/gtest.h>
+
+#include <algorithm>
+#include <cstdlib>
+#include <functional>
+#include <random>
+#include <unordered_set>
+#include <vector>
+
+namespace latinime {
+namespace {
+
+TEST(BloomFilterTest, TestFilter) {
+    static const int TEST_RANDOM_DATA_MAX = 65536;
+    static const int ELEMENT_COUNT = 1000;
+    std::vector<int> elements;
+
+    // Initialize data set with random integers.
+    {
+        // Use the uniform integer distribution [0, TEST_RANDOM_DATA_MAX].
+        std::uniform_int_distribution<int> distribution(0, TEST_RANDOM_DATA_MAX);
+        auto randomNumberGenerator = std::bind(distribution, std::mt19937());
+        for (int i = 0; i < ELEMENT_COUNT; ++i) {
+            elements.push_back(randomNumberGenerator());
+        }
+    }
+
+    // Make sure BloomFilter contains nothing by default.
+    BloomFilter bloomFilter;
+    for (const int elem : elements) {
+        ASSERT_FALSE(bloomFilter.isInFilter(elem));
+    }
+
+    // Copy some of the test vector into bloom filter.
+    std::unordered_set<int> elementsThatHaveBeenSetInFilter;
+    {
+        // Use the uniform integer distribution [0, 1].
+        std::uniform_int_distribution<int> distribution(0, 1);
+        auto randomBitGenerator = std::bind(distribution, std::mt19937());
+        for (const int elem : elements) {
+            if (randomBitGenerator() == 0) {
+                bloomFilter.setInFilter(elem);
+                elementsThatHaveBeenSetInFilter.insert(elem);
+            }
+        }
+    }
+
+    for (const int elem : elements) {
+        const bool existsInFilter = bloomFilter.isInFilter(elem);
+        const bool hasBeenSetInFilter =
+                elementsThatHaveBeenSetInFilter.find(elem) != elementsThatHaveBeenSetInFilter.end();
+        if (hasBeenSetInFilter) {
+            EXPECT_TRUE(existsInFilter) << "elem: " << elem;
+        }
+        if (!existsInFilter) {
+            EXPECT_FALSE(hasBeenSetInFilter) << "elem: " << elem;
+        }
+    }
+}
+
+}  // namespace
+}  // namespace latinime
diff --git a/native/jni/tests/suggest/core/layout/normal_distribution_2d_test.cpp b/native/jni/tests/suggest/core/layout/normal_distribution_2d_test.cpp
new file mode 100644
index 0000000..1d6a27c
--- /dev/null
+++ b/native/jni/tests/suggest/core/layout/normal_distribution_2d_test.cpp
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2014 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.
+ */
+
+#include "suggest/core/layout/normal_distribution_2d.h"
+
+#include <gtest/gtest.h>
+
+#include <vector>
+
+namespace latinime {
+namespace {
+
+static const float ORIGIN_X = 0.0f;
+static const float ORIGIN_Y = 0.0f;
+static const float LARGE_STANDARD_DEVIATION = 100.0f;
+static const float SMALL_STANDARD_DEVIATION = 10.0f;
+static const float ZERO_RADIAN = 0.0f;
+
+TEST(NormalDistribution2DTest, ProbabilityDensity) {
+    const NormalDistribution2D distribution(ORIGIN_X, LARGE_STANDARD_DEVIATION, ORIGIN_Y,
+            SMALL_STANDARD_DEVIATION, ZERO_RADIAN);
+
+    static const float SMALL_COORDINATE = 10.0f;
+    static const float LARGE_COORDINATE = 20.0f;
+    // The probability density of the point near the distribution center is larger than the
+    // probability density of the point that is far from distribution center.
+    EXPECT_GE(distribution.getProbabilityDensity(SMALL_COORDINATE, SMALL_COORDINATE),
+            distribution.getProbabilityDensity(LARGE_COORDINATE, LARGE_COORDINATE));
+    // The probability density of the point shifted toward the direction that has larger standard
+    // deviation is larger than the probability density of the point shifted towards another
+    // direction.
+    EXPECT_GE(distribution.getProbabilityDensity(LARGE_COORDINATE, SMALL_COORDINATE),
+            distribution.getProbabilityDensity(SMALL_COORDINATE, LARGE_COORDINATE));
+}
+
+TEST(NormalDistribution2DTest, Rotate) {
+    static const float COORDINATES[] = {0.0f, 10.0f, 100.0f, -20.0f};
+    static const float EPSILON = 0.01f;
+    const NormalDistribution2D distribution(ORIGIN_X, LARGE_STANDARD_DEVIATION, ORIGIN_Y,
+            SMALL_STANDARD_DEVIATION, ZERO_RADIAN);
+    const NormalDistribution2D rotatedDistribution(ORIGIN_X, LARGE_STANDARD_DEVIATION, ORIGIN_Y,
+            SMALL_STANDARD_DEVIATION, M_PI_4);
+    for (const float x : COORDINATES) {
+        for (const float y : COORDINATES) {
+            // The probability density of the rotated distribution at the point and the probability
+            // density of the original distribution at the rotated point are the same.
+            const float probabilityDensity0 = distribution.getProbabilityDensity(x, y);
+            const float probabilityDensity1 = rotatedDistribution.getProbabilityDensity(-y, x);
+            EXPECT_NEAR(probabilityDensity0, probabilityDensity1, EPSILON);
+        }
+    }
+}
+
+}  // namespace
+}  // namespace latinime
diff --git a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetSubtypesCountTests.java b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetSubtypesCountTests.java
index d6d12ea..57d2950 100644
--- a/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetSubtypesCountTests.java
+++ b/tests/src/com/android/inputmethod/keyboard/KeyboardLayoutSetSubtypesCountTests.java
@@ -25,8 +25,8 @@
 
 @SmallTest
 public class KeyboardLayoutSetSubtypesCountTests extends KeyboardLayoutSetTestsBase {
-    private static final int NUMBER_OF_SUBTYPES = 67;
-    private static final int NUMBER_OF_ASCII_CAPABLE_SUBTYPES = 42;
+    private static final int NUMBER_OF_SUBTYPES = 68;
+    private static final int NUMBER_OF_ASCII_CAPABLE_SUBTYPES = 43;
     private static final int NUMBER_OF_PREDEFINED_ADDITIONAL_SUBTYPES = 2;
 
     private static String toString(final ArrayList<InputMethodSubtype> subtypeList) {
diff --git a/tests/src/com/android/inputmethod/keyboard/layout/Hindi.java b/tests/src/com/android/inputmethod/keyboard/layout/Hindi.java
index c3f4531..b12b8be 100644
--- a/tests/src/com/android/inputmethod/keyboard/layout/Hindi.java
+++ b/tests/src/com/android/inputmethod/keyboard/layout/Hindi.java
@@ -51,7 +51,7 @@
         public ExpectedKey getBackToSymbolsKey() { return HINDI_BACK_TO_SYMBOLS_KEY; }
 
         @Override
-        public ExpectedKey getCurrencyKey() { return CURRENCY_HINDI; }
+        public ExpectedKey getCurrencyKey() { return CURRENCY_RUPEE; }
 
         @Override
         public ExpectedKey[] getOtherCurrencyKeys() {
@@ -78,7 +78,7 @@
                 Constants.CODE_SHIFT);
 
         // U+20B9: "₹" INDIAN RUPEE SIGN
-        private static final ExpectedKey CURRENCY_HINDI = key("\u20B9",
+        private static final ExpectedKey CURRENCY_RUPEE = key("\u20B9",
                 Symbols.CURRENCY_GENERIC_MORE_KEYS);
     }
 
diff --git a/tests/src/com/android/inputmethod/keyboard/layout/tests/TestsEnglishIN.java b/tests/src/com/android/inputmethod/keyboard/layout/tests/TestsEnglishIN.java
new file mode 100644
index 0000000..c80b250
--- /dev/null
+++ b/tests/src/com/android/inputmethod/keyboard/layout/tests/TestsEnglishIN.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2014 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.keyboard.layout.tests;
+
+import android.test.suitebuilder.annotation.SmallTest;
+
+import com.android.inputmethod.keyboard.layout.LayoutBase;
+import com.android.inputmethod.keyboard.layout.Qwerty;
+import com.android.inputmethod.keyboard.layout.Symbols;
+import com.android.inputmethod.keyboard.layout.SymbolsShifted;
+import com.android.inputmethod.keyboard.layout.expected.ExpectedKey;
+
+import java.util.Locale;
+
+/*
+ * en_IN: English (India)/qwerty
+ */
+@SmallTest
+public final class TestsEnglishIN extends TestsEnglishUS {
+    private static final Locale LOCALE = new Locale("en", "IN");
+    private static final LayoutBase LAYOUT = new Qwerty(new EnglishINCustomizer(LOCALE));
+
+    @Override
+    LayoutBase getLayout() { return LAYOUT; }
+
+    private static class EnglishINCustomizer extends EnglishCustomizer {
+        public EnglishINCustomizer(final Locale locale) { super(locale); }
+
+        @Override
+        public ExpectedKey getCurrencyKey() { return CURRENCY_RUPEE; }
+
+        @Override
+        public ExpectedKey[] getOtherCurrencyKeys() {
+            return SymbolsShifted.CURRENCIES_OTHER_GENERIC;
+        }
+
+        // U+20B9: "₹" INDIAN RUPEE SIGN
+        private static final ExpectedKey CURRENCY_RUPEE = key("\u20B9",
+            Symbols.CURRENCY_GENERIC_MORE_KEYS);
+    }
+}
diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java
index b892df2..c947a63 100644
--- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java
+++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java
@@ -24,6 +24,7 @@
 import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.Locale;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
@@ -86,23 +87,23 @@
     }
 
     // The locale is taken from string resource jar entry name (values-<locale>/)
-    // or {@link LocaleUtils#DEFAULT_LOCALE_KEY} for the default string resource
+    // or {@link LocaleUtils#DEFAULT_LOCALE} for the default string resource
     // directory (values/).
-    public static String getLocaleFromEntryName(final String jarEntryName) {
+    public static Locale getLocaleFromEntryName(final String jarEntryName) {
         final String dirName = jarEntryName.substring(0, jarEntryName.lastIndexOf('/'));
         final int pos = dirName.lastIndexOf('/');
         final String parentName = (pos >= 0) ? dirName.substring(pos + 1) : dirName;
         final int localePos = parentName.indexOf('-');
         if (localePos < 0) {
             // Default resource name.
-            return LocaleUtils.DEFAULT_LOCALE_KEY;
+            return LocaleUtils.DEFAULT_LOCALE;
         }
-        final String locale = parentName.substring(localePos + 1);
-        final int regionPos = locale.indexOf("-r");
+        final String localeStr = parentName.substring(localePos + 1);
+        final int regionPos = localeStr.indexOf("-r");
         if (regionPos < 0) {
-            return locale;
+            return LocaleUtils.constructLocaleFromString(localeStr);
         }
-        return locale.replace("-r", "_");
+        return LocaleUtils.constructLocaleFromString(localeStr.replace("-r", "_"));
     }
 
     public static void close(final Closeable stream) {
diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java
index d0f8b42..0dfa376 100644
--- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java
+++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java
@@ -26,7 +26,8 @@
  * for the make-keyboard-text tool.
  */
 public final class LocaleUtils {
-    public static final String DEFAULT_LOCALE_KEY = "DEFAULT";
+    public static final Locale DEFAULT_LOCALE = Locale.ROOT;
+    private static final String DEFAULT_LOCALE_CODE = "DEFAULT";
     public static final String NO_LANGUAGE_LOCALE_CODE = "zz";
     public static final String NO_LANGUAGE_LOCALE_DISPLAY_NAME = "Alphabet";
 
@@ -36,39 +37,131 @@
 
     private static final HashMap<String, Locale> sLocaleCache = new HashMap<String, Locale>();
 
+    private static final int INDEX_LANGUAGE = 0;
+    private static final int INDEX_SCRIPT = 1;
+    private static final int INDEX_REGION = 2;
+    private static final int ELEMENT_LIMIT = INDEX_REGION + 1;
+
     /**
      * Creates a locale from a string specification.
+     *
+     * Locale string is: language(_script)?(_region)?
+     * where: language := [a-zA-Z]{2,3}
+     *        script := [a-zA-Z]{4}
+     *        region := [a-zA-Z]{2,3}|[0-9]{3}
      */
     public static Locale constructLocaleFromString(final String localeStr) {
         if (localeStr == null) {
             return null;
         }
         synchronized (sLocaleCache) {
-            Locale retval = sLocaleCache.get(localeStr);
-            if (retval != null) {
-                return retval;
+            if (sLocaleCache.containsKey(localeStr)) {
+                return sLocaleCache.get(localeStr);
             }
-            final String[] localeParams = localeStr.split("_", 3);
-            // TODO: Use JDK 7 Locale.Builder to handle a script name.
-            if (localeParams.length == 1) {
-                retval = new Locale(localeParams[0]);
-            } else if (localeParams.length == 2) {
-                retval = new Locale(localeParams[0], localeParams[1]);
-            } else if (localeParams.length == 3) {
-                retval = new Locale(localeParams[0], localeParams[1], localeParams[2]);
+            boolean hasRegion = false;
+            final Locale.Builder builder = new Locale.Builder();
+            final String[] localeElements = localeStr.split("_", ELEMENT_LIMIT);
+            if (localeElements.length > INDEX_LANGUAGE) {
+                final String text = localeElements[INDEX_LANGUAGE];
+                if (isValidLanguage(text)) {
+                    builder.setLanguage(text);
+                } else {
+                    throw new RuntimeException("Unknown locale format: " + localeStr);
+                }
             }
-            if (retval != null) {
-                sLocaleCache.put(localeStr, retval);
+            if (localeElements.length > INDEX_SCRIPT) {
+                final String text = localeElements[INDEX_SCRIPT];
+                if (isValidScript(text)) {
+                    builder.setScript(text);
+                } else if (isValidRegion(text)) {
+                    builder.setRegion(text);
+                    hasRegion = true;
+                } else {
+                    throw new RuntimeException("Unknown locale format: " + localeStr);
+                }
             }
-            return retval;
+            if (localeElements.length > INDEX_REGION) {
+                final String text = localeElements[INDEX_REGION];
+                if (!hasRegion && isValidRegion(text)) {
+                    builder.setRegion(text);
+                } else {
+                    throw new RuntimeException("Unknown locale format: " + localeStr);
+                }
+            }
+            final Locale locale = builder.build();
+            sLocaleCache.put(localeStr, locale);
+            return locale;
         }
     }
 
-    public static String getLocaleDisplayName(final String localeString) {
-        if (localeString.equals(NO_LANGUAGE_LOCALE_CODE)) {
+    private static final int MIN_LENGTH_OF_LANGUAGE = 2;
+    private static final int MAX_LENGTH_OF_LANGUAGE = 2;
+    private static final int LENGTH_OF_SCRIPT = 4;
+    private static final int MIN_LENGTH_OF_REGION = 2;
+    private static final int MAX_LENGTH_OF_REGION = 2;
+    private static final int LENGTH_OF_AREA_CODE = 3;
+
+    private static boolean isValidLanguage(final String text) {
+        return isAlphabetSequence(text, MIN_LENGTH_OF_LANGUAGE, MAX_LENGTH_OF_LANGUAGE);
+    }
+
+    private static boolean isValidScript(final String text) {
+        return isAlphabetSequence(text, LENGTH_OF_SCRIPT, LENGTH_OF_SCRIPT);
+    }
+
+    private static boolean isValidRegion(final String text) {
+        return isAlphabetSequence(text, MIN_LENGTH_OF_REGION, MAX_LENGTH_OF_REGION)
+                || isDigitSequence(text, LENGTH_OF_AREA_CODE, LENGTH_OF_AREA_CODE);
+    }
+
+    private static boolean isAlphabetSequence(final String text, final int lower, final int upper) {
+        final int length = text.length();
+        if (length < lower || length > upper) {
+            return false;
+        }
+        for (int index = 0; index < length; index++) {
+            if (!isAsciiAlphabet(text.charAt(index))) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private static boolean isDigitSequence(final String text, final int lower, final int upper) {
+        final int length = text.length();
+        if (length < lower || length > upper) {
+            return false;
+        }
+        for (int index = 0; index < length; ++index) {
+            if (!isAsciiDigit(text.charAt(index))) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private static boolean isAsciiAlphabet(char c) {
+        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
+    }
+
+    private static boolean isAsciiDigit(char c) {
+        return c >= '0' && c <= '9';
+    }
+
+    public static String getLocaleCode(final Locale locale) {
+        if (locale == DEFAULT_LOCALE) {
+            return DEFAULT_LOCALE_CODE;
+        }
+        return locale.toString();
+    }
+
+    public static String getLocaleDisplayName(final Locale locale) {
+        if (locale == DEFAULT_LOCALE) {
+            return DEFAULT_LOCALE_CODE;
+        }
+        if (locale.getLanguage().equals(NO_LANGUAGE_LOCALE_CODE)) {
             return NO_LANGUAGE_LOCALE_DISPLAY_NAME;
         }
-        final Locale locale = constructLocaleFromString(localeString);
         return locale.getDisplayName(Locale.ENGLISH);
     }
 }
diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java
index c1a9753..c8cb4ac 100644
--- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java
+++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java
@@ -25,6 +25,7 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.TreeMap;
 import java.util.jar.JarFile;
 
@@ -60,9 +61,10 @@
                 jar, TEXT_RESOURCE_NAME);
         for (final String entryName : resourceEntryNames) {
             final StringResourceMap resMap = new StringResourceMap(entryName);
-            mResourcesMap.put(resMap.mLocale, resMap);
+            mResourcesMap.put(LocaleUtils.getLocaleCode(resMap.mLocale), resMap);
         }
-        mDefaultResourceMap = mResourcesMap.get(LocaleUtils.DEFAULT_LOCALE_KEY);
+        mDefaultResourceMap = mResourcesMap.get(
+                LocaleUtils.getLocaleCode(LocaleUtils.DEFAULT_LOCALE));
 
         // Initialize name histogram and names list.
         final HashMap<String, Integer> nameHistogram = mNameHistogram;
@@ -165,13 +167,13 @@
         mDefaultResourceMap.setOutputArraySize(outputArraySize);
     }
 
-    private static String getArrayNameForLocale(final String locale) {
-        return TEXTS_ARRAY_NAME_PREFIX + locale;
+    private static String getArrayNameForLocale(final Locale locale) {
+        return TEXTS_ARRAY_NAME_PREFIX + LocaleUtils.getLocaleCode(locale);
     }
 
     private void dumpTexts(final PrintStream out) {
         for (final StringResourceMap resMap : mResourcesMap.values()) {
-            final String locale = resMap.mLocale;
+            final Locale locale = resMap.mLocale;
             if (resMap == mDefaultResourceMap) continue;
             out.format("    /* Locale %s: %s */\n",
                     locale, LocaleUtils.getLocaleDisplayName(locale));
@@ -185,10 +187,11 @@
 
     private void dumpLocalesMap(final PrintStream out) {
         for (final StringResourceMap resMap : mResourcesMap.values()) {
-            final String locale = resMap.mLocale;
-            final String localeToDump = locale.equals(LocaleUtils.DEFAULT_LOCALE_KEY)
-                    ? String.format("\"%s\"", locale)
-                    : String.format("\"%s\"%s", locale, "       ".substring(locale.length()));
+            final Locale locale = resMap.mLocale;
+            final String localeStr = LocaleUtils.getLocaleCode(locale);
+            final String localeToDump = (locale == LocaleUtils.DEFAULT_LOCALE)
+                    ? String.format("\"%s\"", localeStr)
+                    : String.format("\"%s\"%s", localeStr, "       ".substring(localeStr.length()));
             out.format("        %s, %-12s /* %3d/%3d %s */\n",
                     localeToDump, getArrayNameForLocale(locale) + ",",
                     resMap.getResources().size(), resMap.getOutputArraySize(),
diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java
index d7e76ad..6a79268 100644
--- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java
+++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java
@@ -27,6 +27,7 @@
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
 import javax.xml.parsers.ParserConfigurationException;
@@ -34,8 +35,8 @@
 import javax.xml.parsers.SAXParserFactory;
 
 public class StringResourceMap {
-    // Locale name.
-    public final String mLocale;
+    // Locale of this string resource map.
+    public final Locale mLocale;
     // String resource list.
     private final List<StringResource> mResources;
     // Name to string resource map.