summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/dialpadview/DialpadCharMappings.java
diff options
context:
space:
mode:
authorlinyuh <linyuh@google.com>2017-11-02 18:05:33 -0700
committerzachh <zachh@google.com>2017-11-11 06:38:23 +0000
commit82670fee34bfc922905f31d3904406eb0677b162 (patch)
tree69c66cb89539c970896a3d16fcf347ad1ee47c7f /java/com/android/dialer/dialpadview/DialpadCharMappings.java
parent83d8a62ee2b2262941590ff5ef35986dc1df1fa1 (diff)
downloadandroid_packages_apps_Dialer-82670fee34bfc922905f31d3904406eb0677b162.tar.gz
android_packages_apps_Dialer-82670fee34bfc922905f31d3904406eb0677b162.tar.bz2
android_packages_apps_Dialer-82670fee34bfc922905f31d3904406eb0677b162.zip
Support dual alphabets in smart search when a secondary alphabet is available.
Bug: 30215380 Test: QueryBoldingUtilTest, QueryFilteringUtilTest, ContactFilterCursorTest PiperOrigin-RevId: 174408771 Change-Id: I4c601b16dd90db6b7b2a05c9daa6804749ea2a43
Diffstat (limited to 'java/com/android/dialer/dialpadview/DialpadCharMappings.java')
-rw-r--r--java/com/android/dialer/dialpadview/DialpadCharMappings.java181
1 files changed, 181 insertions, 0 deletions
diff --git a/java/com/android/dialer/dialpadview/DialpadCharMappings.java b/java/com/android/dialer/dialpadview/DialpadCharMappings.java
new file mode 100644
index 000000000..12994bc78
--- /dev/null
+++ b/java/com/android/dialer/dialpadview/DialpadCharMappings.java
@@ -0,0 +1,181 @@
+/*
+ * Copyright (C) 2017 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.dialer.dialpadview;
+
+import android.content.Context;
+import android.support.annotation.NonNull;
+import android.support.annotation.VisibleForTesting;
+import android.support.v4.util.SimpleArrayMap;
+import com.android.dialer.common.Assert;
+import com.android.dialer.compat.CompatUtils;
+import com.android.dialer.configprovider.ConfigProviderBindings;
+
+/** A class containing character mappings for the dialpad. */
+public class DialpadCharMappings {
+ @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
+ public static final String FLAG_ENABLE_DUAL_ALPHABETS = "enable_dual_alphabets_on_t9";
+
+ /** The character mapping for the Latin alphabet (the default mapping) */
+ private static class Latin {
+ private static final String[] KEY_TO_CHARS = {
+ "+" /* 0 */,
+ "" /* 1 */,
+ "ABC" /* 2 */,
+ "DEF" /* 3 */,
+ "GHI" /* 4 */,
+ "JKL" /* 5 */,
+ "MNO" /* 6 */,
+ "PQRS" /* 7 */,
+ "TUV" /* 8 */,
+ "WXYZ" /* 9 */,
+ "" /* * */,
+ "" /* # */,
+ };
+
+ private static final SimpleArrayMap<Character, Character> CHAR_TO_KEY =
+ getCharToKeyMap(KEY_TO_CHARS);
+ }
+
+ /** The character mapping for the Russian alphabet */
+ private static class Rus {
+ private static final String[] KEY_TO_CHARS = {
+ "" /* 0 */,
+ "" /* 1 */,
+ "АБВГ" /* 2 */,
+ "ДЕЖЗ" /* 3 */,
+ "ИЙКЛ" /* 4 */,
+ "МНОП" /* 5 */,
+ "РСТУ" /* 6 */,
+ "ФХЦЧ" /* 7 */,
+ "ШЩЪЫ" /* 8 */,
+ "ЬЭЮЯ" /* 9 */,
+ "" /* * */,
+ "" /* # */,
+ };
+
+ private static final SimpleArrayMap<Character, Character> CHAR_TO_KEY =
+ getCharToKeyMap(KEY_TO_CHARS);
+ }
+
+ // A map in which each key is an ISO 639-2 language code and the corresponding value is a
+ // character-key map.
+ private static final SimpleArrayMap<String, SimpleArrayMap<Character, Character>>
+ CHAR_TO_KEY_MAPS = new SimpleArrayMap<>();
+
+ // A map in which each key is an ISO 639-2 language code and the corresponding value is an array
+ // defining a key-characters map.
+ private static final SimpleArrayMap<String, String[]> KEY_TO_CHAR_MAPS = new SimpleArrayMap<>();
+
+ static {
+ CHAR_TO_KEY_MAPS.put("rus", Rus.CHAR_TO_KEY);
+ KEY_TO_CHAR_MAPS.put("rus", Rus.KEY_TO_CHARS);
+ }
+
+ /**
+ * Returns the character-key map of the ISO 639-2 language code of the 1st language preference or
+ * null if
+ *
+ * <ul>
+ * <li>no character-key map for the language code is defined, or
+ * <li>the support for dual alphabets is disabled.
+ * </ul>
+ */
+ public static SimpleArrayMap<Character, Character> getCharToKeyMap(@NonNull Context context) {
+ return isDualAlphabetsEnabled(context)
+ ? CHAR_TO_KEY_MAPS.get(CompatUtils.getLocale(context).getISO3Language())
+ : null;
+ }
+
+ /** Returns the default character-key map (the one that uses the Latin alphabet). */
+ public static SimpleArrayMap<Character, Character> getDefaultCharToKeyMap() {
+ return Latin.CHAR_TO_KEY;
+ }
+
+ /**
+ * Returns the key-characters map of the given ISO 639-2 language code of the 1st language
+ * preference or null if
+ *
+ * <ul>
+ * <li>no key-characters map for the language code is defined, or
+ * <li>the support for dual alphabets is disabled.
+ * </ul>
+ */
+ public static String[] getKeyToCharsMap(@NonNull Context context) {
+ return isDualAlphabetsEnabled(context)
+ ? KEY_TO_CHAR_MAPS.get(CompatUtils.getLocale(context).getISO3Language())
+ : null;
+ }
+
+ /** Returns the default key-characters map (the one that uses the Latin alphabet). */
+ public static String[] getDefaultKeyToCharsMap() {
+ return Latin.KEY_TO_CHARS;
+ }
+
+ private static boolean isDualAlphabetsEnabled(Context context) {
+ return ConfigProviderBindings.get(context).getBoolean(FLAG_ENABLE_DUAL_ALPHABETS, false);
+ }
+
+ /**
+ * Given a array representing a key-characters map, return its reverse map.
+ *
+ * <p>It is the caller's responsibility to ensure that
+ *
+ * <ul>
+ * <li>the array contains only 12 elements,
+ * <li>the 0th element ~ the 9th element are the mappings for keys "0" ~ "9",
+ * <li>the 10th element is for key "*", and
+ * <li>the 11th element is for key "#".
+ * </ul>
+ *
+ * @param keyToChars An array representing a key-characters map. It must satisfy the conditions
+ * above.
+ * @return A character-key map.
+ */
+ private static SimpleArrayMap<Character, Character> getCharToKeyMap(
+ @NonNull String[] keyToChars) {
+ Assert.checkArgument(keyToChars.length == 12);
+
+ SimpleArrayMap<Character, Character> charToKeyMap = new SimpleArrayMap<>();
+
+ for (int keyIndex = 0; keyIndex < keyToChars.length; keyIndex++) {
+ String chars = keyToChars[keyIndex];
+
+ for (int j = 0; j < chars.length(); j++) {
+ char c = chars.charAt(j);
+ if (Character.isAlphabetic(c)) {
+ charToKeyMap.put(Character.toLowerCase(c), getKeyChar(keyIndex));
+ }
+ }
+ }
+
+ return charToKeyMap;
+ }
+
+ /** Given a key index of the dialpad, returns the corresponding character. */
+ private static char getKeyChar(int keyIndex) {
+ Assert.checkArgument(0 <= keyIndex && keyIndex <= 11);
+
+ switch (keyIndex) {
+ case 10:
+ return '*';
+ case 11:
+ return '#';
+ default:
+ return (char) ('0' + keyIndex);
+ }
+ }
+}