1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.dialer.smartdial.map; 18 19 import android.support.v4.util.SimpleArrayMap; 20 import com.google.common.base.Optional; 21 22 /** Definition for utilities that supports smart dial in different languages. */ 23 @SuppressWarnings("Guava") 24 abstract class SmartDialMap { 25 26 /** 27 * Returns true if the provided character can be mapped to a key on the dialpad. 28 * 29 * <p>The provided character is expected to be a normalized character. See {@link 30 * SmartDialMap#normalizeCharacter(char)} for details. 31 */ isValidDialpadCharacter(char ch)32 protected boolean isValidDialpadCharacter(char ch) { 33 return isValidDialpadAlphabeticChar(ch) || isValidDialpadNumericChar(ch); 34 } 35 36 /** 37 * Returns true if the provided character is a letter and can be mapped to a key on the dialpad. 38 * 39 * <p>The provided character is expected to be a normalized character. See {@link 40 * SmartDialMap#normalizeCharacter(char)} for details. 41 */ isValidDialpadAlphabeticChar(char ch)42 protected boolean isValidDialpadAlphabeticChar(char ch) { 43 return getCharToKeyMap().containsKey(ch); 44 } 45 46 /** 47 * Returns true if the provided character is a digit, and can be mapped to a key on the dialpad. 48 */ isValidDialpadNumericChar(char ch)49 protected boolean isValidDialpadNumericChar(char ch) { 50 return '0' <= ch && ch <= '9'; 51 } 52 53 /** 54 * Get the index of the key on the dialpad which the character corresponds to. 55 * 56 * <p>The provided character is expected to be a normalized character. See {@link 57 * SmartDialMap#normalizeCharacter(char)} for details. 58 * 59 * <p>An {@link Optional#absent()} is returned if the provided character can't be mapped to a key 60 * on the dialpad. 61 */ getDialpadIndex(char ch)62 protected Optional<Byte> getDialpadIndex(char ch) { 63 if (isValidDialpadNumericChar(ch)) { 64 return Optional.of((byte) (ch - '0')); 65 } 66 67 if (isValidDialpadAlphabeticChar(ch)) { 68 return Optional.of((byte) (getCharToKeyMap().get(ch) - '0')); 69 } 70 71 return Optional.absent(); 72 } 73 74 /** 75 * Get the actual numeric character on the dialpad which the character corresponds to. 76 * 77 * <p>The provided character is expected to be a normalized character. See {@link 78 * SmartDialMap#normalizeCharacter(char)} for details. 79 * 80 * <p>An {@link Optional#absent()} is returned if the provided character can't be mapped to a key 81 * on the dialpad. 82 */ getDialpadNumericCharacter(char ch)83 protected Optional<Character> getDialpadNumericCharacter(char ch) { 84 return isValidDialpadAlphabeticChar(ch) 85 ? Optional.of(getCharToKeyMap().get(ch)) 86 : Optional.absent(); 87 } 88 89 /** 90 * Converts uppercase characters to lower case ones, and on a best effort basis, strips accents 91 * from accented characters. 92 * 93 * <p>An {@link Optional#absent()} is returned if the provided character can't be mapped to a key 94 * on the dialpad. 95 */ normalizeCharacter(char ch)96 abstract Optional<Character> normalizeCharacter(char ch); 97 98 /** 99 * Returns a map in which each key is a normalized character and the corresponding value is a 100 * dialpad key. 101 */ getCharToKeyMap()102 abstract SimpleArrayMap<Character, Character> getCharToKeyMap(); 103 } 104