From 82670fee34bfc922905f31d3904406eb0677b162 Mon Sep 17 00:00:00 2001 From: linyuh Date: Thu, 2 Nov 2017 18:05:33 -0700 Subject: Support dual alphabets in smart search when a secondary alphabet is available. Bug: 30215380 Test: QueryBoldingUtilTest, QueryFilteringUtilTest, ContactFilterCursorTest PiperOrigin-RevId: 174408771 Change-Id: I4c601b16dd90db6b7b2a05c9daa6804749ea2a43 --- .../dialer/dialpadview/DialpadAlphabets.java | 80 --------- .../dialer/dialpadview/DialpadCharMappings.java | 181 +++++++++++++++++++++ .../android/dialer/dialpadview/DialpadView.java | 5 +- 3 files changed, 183 insertions(+), 83 deletions(-) delete mode 100644 java/com/android/dialer/dialpadview/DialpadAlphabets.java create mode 100644 java/com/android/dialer/dialpadview/DialpadCharMappings.java (limited to 'java/com/android/dialer/dialpadview') diff --git a/java/com/android/dialer/dialpadview/DialpadAlphabets.java b/java/com/android/dialer/dialpadview/DialpadAlphabets.java deleted file mode 100644 index f02ca4395..000000000 --- a/java/com/android/dialer/dialpadview/DialpadAlphabets.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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.support.v4.util.SimpleArrayMap; - -/** A class containing key-letter mappings for the dialpad. */ -public class DialpadAlphabets { - - // The default mapping (the Latin alphabet) - private static final String[] def = { - "+" /* 0 */, - "" /* 1 */, - "ABC" /* 2 */, - "DEF" /* 3 */, - "GHI" /* 4 */, - "JKL" /* 5 */, - "MNO" /* 6 */, - "PQRS" /* 7 */, - "TUV" /* 8 */, - "WXYZ" /* 9 */, - "" /* * */, - "" /* # */, - }; - - // Russian - private static final String[] rus = { - "" /* 0 */, - "" /* 1 */, - "АБВГ" /* 2 */, - "ДЕЖЗ" /* 3 */, - "ИЙКЛ" /* 4 */, - "МНОП" /* 5 */, - "РСТУ" /* 6 */, - "ФХЦЧ" /* 7 */, - "ШЩЪЫ" /* 8 */, - "ЬЭЮЯ" /* 9 */, - "" /* * */, - "" /* # */, - }; - - // A map in which each key is an ISO 639-2 language code and the corresponding key is an array - // defining key-letter mappings - private static final SimpleArrayMap alphabets = new SimpleArrayMap<>(); - - static { - alphabets.put("rus", rus); - } - - /** - * Returns the alphabet (a key-letter mapping) of the given ISO 639-2 language code or null if - * - * - */ - public static String[] getAlphabetForLanguage(String languageCode) { - return alphabets.get(languageCode); - } - - /** Returns the default key-letter mapping (the one that uses the Latin alphabet). */ - public static String[] getDefaultAlphabet() { - return def; - } -} 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 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 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> + 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 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 + * + *
    + *
  • no character-key map for the language code is defined, or + *
  • the support for dual alphabets is disabled. + *
+ */ + public static SimpleArrayMap 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 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 + * + *
    + *
  • no key-characters map for the language code is defined, or + *
  • the support for dual alphabets is disabled. + *
+ */ + 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. + * + *

It is the caller's responsibility to ensure that + * + *

    + *
  • the array contains only 12 elements, + *
  • the 0th element ~ the 9th element are the mappings for keys "0" ~ "9", + *
  • the 10th element is for key "*", and + *
  • the 11th element is for key "#". + *
+ * + * @param keyToChars An array representing a key-characters map. It must satisfy the conditions + * above. + * @return A character-key map. + */ + private static SimpleArrayMap getCharToKeyMap( + @NonNull String[] keyToChars) { + Assert.checkArgument(keyToChars.length == 12); + + SimpleArrayMap 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); + } + } +} diff --git a/java/com/android/dialer/dialpadview/DialpadView.java b/java/com/android/dialer/dialpadview/DialpadView.java index 38ab383a8..5794038ce 100644 --- a/java/com/android/dialer/dialpadview/DialpadView.java +++ b/java/com/android/dialer/dialpadview/DialpadView.java @@ -114,9 +114,8 @@ public class DialpadView extends LinearLayout { mIsRtl = TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == View.LAYOUT_DIRECTION_RTL; - mPrimaryLettersMapping = DialpadAlphabets.getDefaultAlphabet(); - mSecondaryLettersMapping = - DialpadAlphabets.getAlphabetForLanguage(CompatUtils.getLocale(context).getISO3Language()); + mPrimaryLettersMapping = DialpadCharMappings.getDefaultKeyToCharsMap(); + mSecondaryLettersMapping = DialpadCharMappings.getKeyToCharsMap(context); } @Override -- cgit v1.2.3