Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ab146531 authored by linyuh's avatar linyuh Committed by Copybara-Service
Browse files

Support dual alphabets in smart dial.

Bug: 30215380,70633239
Test: CompositeSmartDialMapTest, LatinSmartDialMapTest, RussianSmartDialMapTest, SmartDialNameMatcherTest
PiperOrigin-RevId: 179580982
Change-Id: I5e4c3e61f0dfdc6ca1e80a93bb985ffec08dd8b0
parent 0f76636b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -1400,7 +1400,7 @@ public class DialtactsActivity extends TransactionSafeActivity
      mNewSearchFragment.setRawNumber(query);
    }
    final String normalizedQuery =
        SmartDialNameMatcher.normalizeNumber(query, SmartDialNameMatcher.LATIN_SMART_DIAL_MAP);
        SmartDialNameMatcher.normalizeNumber(/* context = */ this, query);

    if (!TextUtils.equals(mSearchView.getText(), normalizedQuery)) {
      if (DEBUG) {
+5 −4
Original line number Diff line number Diff line
@@ -25,7 +25,6 @@ import com.android.dialer.common.LogUtil;
import com.android.dialer.dialpadview.SmartDialCursorLoader;
import com.android.dialer.smartdial.SmartDialMatchPosition;
import com.android.dialer.smartdial.SmartDialNameMatcher;
import com.android.dialer.smartdial.SmartDialPrefix;
import com.android.dialer.util.CallUtil;
import java.util.ArrayList;

@@ -35,11 +34,13 @@ public class SmartDialNumberListAdapter extends DialerPhoneNumberListAdapter {
  private static final String TAG = SmartDialNumberListAdapter.class.getSimpleName();
  private static final boolean DEBUG = false;

  private final Context mContext;
  @NonNull private final SmartDialNameMatcher mNameMatcher;

  public SmartDialNumberListAdapter(Context context) {
    super(context);
    mNameMatcher = new SmartDialNameMatcher("", SmartDialPrefix.getMap());
    mContext = context;
    mNameMatcher = new SmartDialNameMatcher("");
    setShortcutEnabled(SmartDialNumberListAdapter.SHORTCUT_DIRECT_CALL, false);

    if (DEBUG) {
@@ -72,7 +73,7 @@ public class SmartDialNumberListAdapter extends DialerPhoneNumberListAdapter {
  protected void setHighlight(ContactListItemView view, Cursor cursor) {
    view.clearHighlightSequences();

    if (mNameMatcher.matches(cursor.getString(PhoneQuery.DISPLAY_NAME))) {
    if (mNameMatcher.matches(mContext, cursor.getString(PhoneQuery.DISPLAY_NAME))) {
      final ArrayList<SmartDialMatchPosition> nameMatches = mNameMatcher.getMatchPositions();
      for (SmartDialMatchPosition match : nameMatches) {
        view.addNameHighlightSequence(match.start, match.end);
@@ -89,7 +90,7 @@ public class SmartDialNumberListAdapter extends DialerPhoneNumberListAdapter {
    }

    final SmartDialMatchPosition numberMatch =
        mNameMatcher.matchesNumber(cursor.getString(PhoneQuery.PHONE_NUMBER));
        mNameMatcher.matchesNumber(mContext, cursor.getString(PhoneQuery.PHONE_NUMBER));
    if (numberMatch != null) {
      view.addNumberHighlightSequence(numberMatch.start, numberMatch.end);
    }
+5 −4
Original line number Diff line number Diff line
@@ -535,7 +535,7 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper {
        insert.executeInsert();
        final String contactPhoneNumber = updatedContactCursor.getString(PhoneQuery.PHONE_NUMBER);
        final ArrayList<String> numberPrefixes =
            SmartDialPrefix.parseToNumberTokens(contactPhoneNumber);
            SmartDialPrefix.parseToNumberTokens(mContext, contactPhoneNumber);

        for (String numberPrefix : numberPrefixes) {
          numberInsert.bindLong(1, updatedContactCursor.getLong(PhoneQuery.PHONE_CONTACT_ID));
@@ -578,7 +578,7 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper {
      while (nameCursor.moveToNext()) {
        /** Computes a list of prefixes of a given contact name. */
        final ArrayList<String> namePrefixes =
            SmartDialPrefix.generateNamePrefixes(nameCursor.getString(columnIndexName));
            SmartDialPrefix.generateNamePrefixes(mContext, nameCursor.getString(columnIndexName));

        for (String namePrefix : namePrefixes) {
          insert.bindLong(1, nameCursor.getLong(columnIndexContactId));
@@ -912,8 +912,9 @@ public class DialerDatabaseHelper extends SQLiteOpenHelper {
        /**
         * If the contact has either the name or number that matches the query, add to the result.
         */
        final boolean nameMatches = nameMatcher.matches(displayName);
        final boolean numberMatches = (nameMatcher.matchesNumber(phoneNumber, query) != null);
        final boolean nameMatches = nameMatcher.matches(mContext, displayName);
        final boolean numberMatches =
            (nameMatcher.matchesNumber(mContext, phoneNumber, query) != null);
        if (nameMatches || numberMatches) {
          /** If a contact has not been added, add it to the result and the hash set. */
          duplicates.add(contactMatch);
+2 −3
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ import com.android.dialer.database.Database;
import com.android.dialer.database.DialerDatabaseHelper;
import com.android.dialer.database.DialerDatabaseHelper.ContactNumber;
import com.android.dialer.smartdial.SmartDialNameMatcher;
import com.android.dialer.smartdial.SmartDialPrefix;
import com.android.dialer.util.PermissionsUtil;
import java.util.ArrayList;

@@ -59,10 +58,10 @@ public class SmartDialCursorLoader extends AsyncTaskLoader<Cursor> {
    if (DEBUG) {
      LogUtil.v(TAG, "Configure new query to be " + query);
    }
    mQuery = SmartDialNameMatcher.normalizeNumber(query, SmartDialPrefix.getMap());
    mQuery = SmartDialNameMatcher.normalizeNumber(mContext, query);

    /** Constructs a name matcher object for matching names. */
    mNameMatcher = new SmartDialNameMatcher(mQuery, SmartDialPrefix.getMap());
    mNameMatcher = new SmartDialNameMatcher(mQuery);
    mNameMatcher.setShouldMatchEmptyQuery(!mShowEmptyListForNullQuery);
  }

+170 −0
Original line number Diff line number Diff line
/*
 * 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.smartdial;

import android.content.Context;
import android.support.annotation.VisibleForTesting;
import android.support.v4.util.SimpleArrayMap;
import com.android.dialer.compat.CompatUtils;
import com.android.dialer.configprovider.ConfigProviderBindings;
import com.google.common.base.Optional;

/**
 * A utility class that combines the functionality of two implementations of {@link SmartDialMap} so
 * that we support smart dial for dual alphabets.
 *
 * <p>Of the two implementations of {@link SmartDialMap}, the default one always takes precedence.
 * The second one is consulted only when the default one is unable to provide a valid result.
 *
 * <p>Note that the second implementation can be absent if it is not defined for the system's 1st
 * language preference.
 */
@SuppressWarnings("Guava")
public class CompositeSmartDialMap {
  @VisibleForTesting
  public static final String FLAG_ENABLE_DUAL_ALPHABETS = "enable_dual_alphabets_on_t9";

  private static final SmartDialMap DEFAULT_MAP = LatinSmartDialMap.getInstance();

  // A map in which each key is an ISO 639-2 language code and the corresponding value is a
  // SmartDialMap
  private static final SimpleArrayMap<String, SmartDialMap> EXTRA_MAPS = new SimpleArrayMap<>();

  static {
    EXTRA_MAPS.put("rus", RussianSmartDialMap.getInstance());
  }

  private CompositeSmartDialMap() {}

  /**
   * Returns true if the provided character can be mapped to a key on the dialpad.
   *
   * <p>The provided character is expected to be a normalized character. See {@link
   * SmartDialMap#normalizeCharacter(char)} for details.
   */
  static boolean isValidDialpadCharacter(Context context, char ch) {
    if (DEFAULT_MAP.isValidDialpadCharacter(ch)) {
      return true;
    }

    Optional<SmartDialMap> extraMap = getExtraMap(context);
    return extraMap.isPresent() && extraMap.get().isValidDialpadCharacter(ch);
  }

  /**
   * Returns true if the provided character is a letter, and can be mapped to a key on the dialpad.
   *
   * <p>The provided character is expected to be a normalized character. See {@link
   * SmartDialMap#normalizeCharacter(char)} for details.
   */
  static boolean isValidDialpadAlphabeticChar(Context context, char ch) {
    if (DEFAULT_MAP.isValidDialpadAlphabeticChar(ch)) {
      return true;
    }

    Optional<SmartDialMap> extraMap = getExtraMap(context);
    return extraMap.isPresent() && extraMap.get().isValidDialpadAlphabeticChar(ch);
  }

  /**
   * Returns true if the provided character is a digit, and can be mapped to a key on the dialpad.
   */
  static boolean isValidDialpadNumericChar(Context context, char ch) {
    if (DEFAULT_MAP.isValidDialpadNumericChar(ch)) {
      return true;
    }

    Optional<SmartDialMap> extraMap = getExtraMap(context);
    return extraMap.isPresent() && extraMap.get().isValidDialpadNumericChar(ch);
  }

  /**
   * Get the index of the key on the dialpad which the character corresponds to.
   *
   * <p>The provided character is expected to be a normalized character. See {@link
   * SmartDialMap#normalizeCharacter(char)} for details.
   *
   * <p>If the provided character can't be mapped to a key on the dialpad, return -1.
   */
  static byte getDialpadIndex(Context context, char ch) {
    Optional<Byte> dialpadIndex = DEFAULT_MAP.getDialpadIndex(ch);
    if (dialpadIndex.isPresent()) {
      return dialpadIndex.get();
    }

    Optional<SmartDialMap> extraMap = getExtraMap(context);
    if (extraMap.isPresent()) {
      dialpadIndex = extraMap.get().getDialpadIndex(ch);
    }

    return dialpadIndex.isPresent() ? dialpadIndex.get() : -1;
  }

  /**
   * Get the actual numeric character on the dialpad which the character corresponds to.
   *
   * <p>The provided character is expected to be a normalized character. See {@link
   * SmartDialMap#normalizeCharacter(char)} for details.
   *
   * <p>If the provided character can't be mapped to a key on the dialpad, return the character.
   */
  static char getDialpadNumericCharacter(Context context, char ch) {
    Optional<Character> dialpadNumericChar = DEFAULT_MAP.getDialpadNumericCharacter(ch);
    if (dialpadNumericChar.isPresent()) {
      return dialpadNumericChar.get();
    }

    Optional<SmartDialMap> extraMap = getExtraMap(context);
    if (extraMap.isPresent()) {
      dialpadNumericChar = extraMap.get().getDialpadNumericCharacter(ch);
    }

    return dialpadNumericChar.isPresent() ? dialpadNumericChar.get() : ch;
  }

  /**
   * Converts uppercase characters to lower case ones, and on a best effort basis, strips accents
   * from accented characters.
   *
   * <p>If the provided character can't be mapped to a key on the dialpad, return the character.
   */
  static char normalizeCharacter(Context context, char ch) {
    Optional<Character> normalizedChar = DEFAULT_MAP.normalizeCharacter(ch);
    if (normalizedChar.isPresent()) {
      return normalizedChar.get();
    }

    Optional<SmartDialMap> extraMap = getExtraMap(context);
    if (extraMap.isPresent()) {
      normalizedChar = extraMap.get().normalizeCharacter(ch);
    }

    return normalizedChar.isPresent() ? normalizedChar.get() : ch;
  }

  @VisibleForTesting
  static Optional<SmartDialMap> getExtraMap(Context context) {
    if (!ConfigProviderBindings.get(context).getBoolean(FLAG_ENABLE_DUAL_ALPHABETS, false)) {
      return Optional.absent();
    }

    String languageCode = CompatUtils.getLocale(context).getISO3Language();
    return EXTRA_MAPS.containsKey(languageCode)
        ? Optional.of(EXTRA_MAPS.get(languageCode))
        : Optional.absent();
  }
}
Loading