blob: 36f174b33c60de6a1bcb1ce8a9ce143764e09dea [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
2 * Copyright (C) 2013 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
17package com.android.dialer.smartdial;
18
19import android.content.Context;
20import android.content.SharedPreferences;
21import android.preference.PreferenceManager;
22import android.support.annotation.VisibleForTesting;
23import android.telephony.TelephonyManager;
24import android.text.TextUtils;
25import java.util.ArrayList;
26import java.util.HashSet;
27import java.util.Set;
28
29/**
30 * Smart Dial utility class to find prefixes of contacts. It contains both methods to find supported
31 * prefix combinations for contact names, and also methods to find supported prefix combinations for
32 * contacts' phone numbers. Each contact name is separated into several tokens, such as first name,
33 * middle name, family name etc. Each phone number is also separated into country code, NANP area
34 * code, and local number if such separation is possible.
35 */
36public class SmartDialPrefix {
37
38 /**
39 * The number of starting and ending tokens in a contact's name considered for initials. For
40 * example, if both constants are set to 2, and a contact's name is "Albert Ben Charles Daniel Ed
41 * Foster", the first two tokens "Albert" "Ben", and last two tokens "Ed" "Foster" can be replaced
42 * by their initials in contact name matching. Users can look up this contact by combinations of
43 * his initials such as "AF" "BF" "EF" "ABF" "BEF" "ABEF" etc, but can not use combinations such
44 * as "CF" "DF" "ACF" "ADF" etc.
45 */
46 private static final int LAST_TOKENS_FOR_INITIALS = 2;
47
48 private static final int FIRST_TOKENS_FOR_INITIALS = 2;
49
50 /** The country code of the user's sim card obtained by calling getSimCountryIso */
51 private static final String PREF_USER_SIM_COUNTRY_CODE =
52 "DialtactsActivity_user_sim_country_code";
53
54 private static final String PREF_USER_SIM_COUNTRY_CODE_DEFAULT = null;
55 /** Dialpad mapping. */
56 private static final SmartDialMap mMap = new LatinSmartDialMap();
57
58 private static String sUserSimCountryCode = PREF_USER_SIM_COUNTRY_CODE_DEFAULT;
59 /** Indicates whether user is in NANP regions. */
60 private static boolean sUserInNanpRegion = false;
61 /** Set of country names that use NANP code. */
62 private static Set<String> sNanpCountries = null;
63 /** Set of supported country codes in front of the phone number. */
64 private static Set<String> sCountryCodes = null;
65
66 private static boolean sNanpInitialized = false;
67
68 /** Initializes the Nanp settings, and finds out whether user is in a NANP region. */
69 public static void initializeNanpSettings(Context context) {
70 final TelephonyManager manager =
71 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
72 if (manager != null) {
73 sUserSimCountryCode = manager.getSimCountryIso();
74 }
75
Eric Erfanianfc0eb8c2017-08-31 06:57:16 -070076 final SharedPreferences prefs =
77 PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
Eric Erfanianccca3152017-02-22 16:32:36 -080078
79 if (sUserSimCountryCode != null) {
80 /** Updates shared preferences with the latest country obtained from getSimCountryIso. */
81 prefs.edit().putString(PREF_USER_SIM_COUNTRY_CODE, sUserSimCountryCode).apply();
82 } else {
83 /** Uses previously stored country code if loading fails. */
84 sUserSimCountryCode =
85 prefs.getString(PREF_USER_SIM_COUNTRY_CODE, PREF_USER_SIM_COUNTRY_CODE_DEFAULT);
86 }
87 /** Queries the NANP country list to find out whether user is in a NANP region. */
88 sUserInNanpRegion = isCountryNanp(sUserSimCountryCode);
89 sNanpInitialized = true;
90 }
91
92 /**
93 * Parses a contact's name into a list of separated tokens.
94 *
95 * @param contactName Contact's name stored in string.
96 * @return A list of name tokens, for example separated first names, last name, etc.
97 */
98 public static ArrayList<String> parseToIndexTokens(String contactName) {
99 final int length = contactName.length();
100 final ArrayList<String> result = new ArrayList<>();
101 char c;
102 final StringBuilder currentIndexToken = new StringBuilder();
103 /**
104 * Iterates through the whole name string. If the current character is a valid character, append
105 * it to the current token. If the current character is not a valid character, for example space
106 * " ", mark the current token as complete and add it to the list of tokens.
107 */
108 for (int i = 0; i < length; i++) {
109 c = mMap.normalizeCharacter(contactName.charAt(i));
110 if (mMap.isValidDialpadCharacter(c)) {
111 /** Converts a character into the number on dialpad that represents the character. */
112 currentIndexToken.append(mMap.getDialpadIndex(c));
113 } else {
114 if (currentIndexToken.length() != 0) {
115 result.add(currentIndexToken.toString());
116 }
117 currentIndexToken.delete(0, currentIndexToken.length());
118 }
119 }
120
121 /** Adds the last token in case it has not been added. */
122 if (currentIndexToken.length() != 0) {
123 result.add(currentIndexToken.toString());
124 }
125 return result;
126 }
127
128 /**
129 * Generates a list of strings that any prefix of any string in the list can be used to look up
130 * the contact's name.
131 *
132 * @param index The contact's name in string.
133 * @return A List of strings, whose prefix can be used to look up the contact.
134 */
135 public static ArrayList<String> generateNamePrefixes(String index) {
136 final ArrayList<String> result = new ArrayList<>();
137
138 /** Parses the name into a list of tokens. */
139 final ArrayList<String> indexTokens = parseToIndexTokens(index);
140
141 if (indexTokens.size() > 0) {
142 /**
143 * Adds the full token combinations to the list. For example, a contact with name "Albert Ben
144 * Ed Foster" can be looked up by any prefix of the following strings "Foster" "EdFoster"
145 * "BenEdFoster" and "AlbertBenEdFoster". This covers all cases of look up that contains only
146 * one token, and that spans multiple continuous tokens.
147 */
148 final StringBuilder fullNameToken = new StringBuilder();
149 for (int i = indexTokens.size() - 1; i >= 0; i--) {
150 fullNameToken.insert(0, indexTokens.get(i));
151 result.add(fullNameToken.toString());
152 }
153
154 /**
155 * Adds initial combinations to the list, with the number of initials restricted by {@link
156 * #LAST_TOKENS_FOR_INITIALS} and {@link #FIRST_TOKENS_FOR_INITIALS}. For example, a contact
157 * with name "Albert Ben Ed Foster" can be looked up by any prefix of the following strings
158 * "EFoster" "BFoster" "BEFoster" "AFoster" "ABFoster" "AEFoster" and "ABEFoster". This covers
159 * all cases of initial lookup.
160 */
161 ArrayList<String> fullNames = new ArrayList<>();
162 fullNames.add(indexTokens.get(indexTokens.size() - 1));
163 final int recursiveNameStart = result.size();
164 int recursiveNameEnd = result.size();
165 String initial = "";
166 for (int i = indexTokens.size() - 2; i >= 0; i--) {
167 if ((i >= indexTokens.size() - LAST_TOKENS_FOR_INITIALS)
168 || (i < FIRST_TOKENS_FOR_INITIALS)) {
169 initial = indexTokens.get(i).substring(0, 1);
170
171 /** Recursively adds initial combinations to the list. */
172 for (int j = 0; j < fullNames.size(); ++j) {
173 result.add(initial + fullNames.get(j));
174 }
175 for (int j = recursiveNameStart; j < recursiveNameEnd; ++j) {
176 result.add(initial + result.get(j));
177 }
178 recursiveNameEnd = result.size();
179 final String currentFullName = fullNames.get(fullNames.size() - 1);
180 fullNames.add(indexTokens.get(i) + currentFullName);
181 }
182 }
183 }
184
185 return result;
186 }
187
188 /**
189 * Computes a list of number strings based on tokens of a given phone number. Any prefix of any
190 * string in the list can be used to look up the phone number. The list include the full phone
191 * number, the national number if there is a country code in the phone number, and the local
192 * number if there is an area code in the phone number following the NANP format. For example, if
193 * a user has phone number +41 71 394 8392, the list will contain 41713948392 and 713948392. Any
194 * prefix to either of the strings can be used to look up the phone number. If a user has a phone
195 * number +1 555-302-3029 (NANP format), the list will contain 15553023029, 5553023029, and
196 * 3023029.
197 *
198 * @param number String of user's phone number.
199 * @return A list of strings where any prefix of any entry can be used to look up the number.
200 */
201 public static ArrayList<String> parseToNumberTokens(String number) {
202 final ArrayList<String> result = new ArrayList<>();
203 if (!TextUtils.isEmpty(number)) {
204 /** Adds the full number to the list. */
205 result.add(SmartDialNameMatcher.normalizeNumber(number, mMap));
206
207 final PhoneNumberTokens phoneNumberTokens = parsePhoneNumber(number);
208 if (phoneNumberTokens == null) {
209 return result;
210 }
211
212 if (phoneNumberTokens.countryCodeOffset != 0) {
213 result.add(
214 SmartDialNameMatcher.normalizeNumber(
215 number, phoneNumberTokens.countryCodeOffset, mMap));
216 }
217
218 if (phoneNumberTokens.nanpCodeOffset != 0) {
219 result.add(
220 SmartDialNameMatcher.normalizeNumber(number, phoneNumberTokens.nanpCodeOffset, mMap));
221 }
222 }
223 return result;
224 }
225
226 /**
227 * Parses a phone number to find out whether it has country code and NANP area code.
228 *
229 * @param number Raw phone number.
230 * @return a PhoneNumberToken instance with country code, NANP code information.
231 */
232 public static PhoneNumberTokens parsePhoneNumber(String number) {
233 String countryCode = "";
234 int countryCodeOffset = 0;
235 int nanpNumberOffset = 0;
236
237 if (!TextUtils.isEmpty(number)) {
238 String normalizedNumber = SmartDialNameMatcher.normalizeNumber(number, mMap);
239 if (number.charAt(0) == '+') {
240 /** If the number starts with '+', tries to find valid country code. */
241 for (int i = 1; i <= 1 + 3; i++) {
242 if (number.length() <= i) {
243 break;
244 }
245 countryCode = number.substring(1, i);
246 if (isValidCountryCode(countryCode)) {
247 countryCodeOffset = i;
248 break;
249 }
250 }
251 } else {
252 /**
253 * If the number does not start with '+', finds out whether it is in NANP format and has '1'
254 * preceding the number.
255 */
256 if ((normalizedNumber.length() == 11)
257 && (normalizedNumber.charAt(0) == '1')
258 && (sUserInNanpRegion)) {
259 countryCode = "1";
260 countryCodeOffset = number.indexOf(normalizedNumber.charAt(1));
261 if (countryCodeOffset == -1) {
262 countryCodeOffset = 0;
263 }
264 }
265 }
266
267 /** If user is in NANP region, finds out whether a number is in NANP format. */
268 if (sUserInNanpRegion) {
269 String areaCode = "";
270 if (countryCode.equals("") && normalizedNumber.length() == 10) {
271 /**
272 * if the number has no country code but fits the NANP format, extracts the NANP area
273 * code, and finds out offset of the local number.
274 */
275 areaCode = normalizedNumber.substring(0, 3);
276 } else if (countryCode.equals("1") && normalizedNumber.length() == 11) {
277 /**
278 * If the number has country code '1', finds out area code and offset of the local number.
279 */
280 areaCode = normalizedNumber.substring(1, 4);
281 }
282 if (!areaCode.equals("")) {
283 final int areaCodeIndex = number.indexOf(areaCode);
284 if (areaCodeIndex != -1) {
285 nanpNumberOffset = number.indexOf(areaCode) + 3;
286 }
287 }
288 }
289 }
290 return new PhoneNumberTokens(countryCode, countryCodeOffset, nanpNumberOffset);
291 }
292
293 /** Checkes whether a country code is valid. */
294 private static boolean isValidCountryCode(String countryCode) {
295 if (sCountryCodes == null) {
296 sCountryCodes = initCountryCodes();
297 }
298 return sCountryCodes.contains(countryCode);
299 }
300
301 private static Set<String> initCountryCodes() {
302 final HashSet<String> result = new HashSet<String>();
303 result.add("1");
304 result.add("7");
305 result.add("20");
306 result.add("27");
307 result.add("30");
308 result.add("31");
309 result.add("32");
310 result.add("33");
311 result.add("34");
312 result.add("36");
313 result.add("39");
314 result.add("40");
315 result.add("41");
316 result.add("43");
317 result.add("44");
318 result.add("45");
319 result.add("46");
320 result.add("47");
321 result.add("48");
322 result.add("49");
323 result.add("51");
324 result.add("52");
325 result.add("53");
326 result.add("54");
327 result.add("55");
328 result.add("56");
329 result.add("57");
330 result.add("58");
331 result.add("60");
332 result.add("61");
333 result.add("62");
334 result.add("63");
335 result.add("64");
336 result.add("65");
337 result.add("66");
338 result.add("81");
339 result.add("82");
340 result.add("84");
341 result.add("86");
342 result.add("90");
343 result.add("91");
344 result.add("92");
345 result.add("93");
346 result.add("94");
347 result.add("95");
348 result.add("98");
349 result.add("211");
350 result.add("212");
351 result.add("213");
352 result.add("216");
353 result.add("218");
354 result.add("220");
355 result.add("221");
356 result.add("222");
357 result.add("223");
358 result.add("224");
359 result.add("225");
360 result.add("226");
361 result.add("227");
362 result.add("228");
363 result.add("229");
364 result.add("230");
365 result.add("231");
366 result.add("232");
367 result.add("233");
368 result.add("234");
369 result.add("235");
370 result.add("236");
371 result.add("237");
372 result.add("238");
373 result.add("239");
374 result.add("240");
375 result.add("241");
376 result.add("242");
377 result.add("243");
378 result.add("244");
379 result.add("245");
380 result.add("246");
381 result.add("247");
382 result.add("248");
383 result.add("249");
384 result.add("250");
385 result.add("251");
386 result.add("252");
387 result.add("253");
388 result.add("254");
389 result.add("255");
390 result.add("256");
391 result.add("257");
392 result.add("258");
393 result.add("260");
394 result.add("261");
395 result.add("262");
396 result.add("263");
397 result.add("264");
398 result.add("265");
399 result.add("266");
400 result.add("267");
401 result.add("268");
402 result.add("269");
403 result.add("290");
404 result.add("291");
405 result.add("297");
406 result.add("298");
407 result.add("299");
408 result.add("350");
409 result.add("351");
410 result.add("352");
411 result.add("353");
412 result.add("354");
413 result.add("355");
414 result.add("356");
415 result.add("357");
416 result.add("358");
417 result.add("359");
418 result.add("370");
419 result.add("371");
420 result.add("372");
421 result.add("373");
422 result.add("374");
423 result.add("375");
424 result.add("376");
425 result.add("377");
426 result.add("378");
427 result.add("379");
428 result.add("380");
429 result.add("381");
430 result.add("382");
431 result.add("385");
432 result.add("386");
433 result.add("387");
434 result.add("389");
435 result.add("420");
436 result.add("421");
437 result.add("423");
438 result.add("500");
439 result.add("501");
440 result.add("502");
441 result.add("503");
442 result.add("504");
443 result.add("505");
444 result.add("506");
445 result.add("507");
446 result.add("508");
447 result.add("509");
448 result.add("590");
449 result.add("591");
450 result.add("592");
451 result.add("593");
452 result.add("594");
453 result.add("595");
454 result.add("596");
455 result.add("597");
456 result.add("598");
457 result.add("599");
458 result.add("670");
459 result.add("672");
460 result.add("673");
461 result.add("674");
462 result.add("675");
463 result.add("676");
464 result.add("677");
465 result.add("678");
466 result.add("679");
467 result.add("680");
468 result.add("681");
469 result.add("682");
470 result.add("683");
471 result.add("685");
472 result.add("686");
473 result.add("687");
474 result.add("688");
475 result.add("689");
476 result.add("690");
477 result.add("691");
478 result.add("692");
479 result.add("800");
480 result.add("808");
481 result.add("850");
482 result.add("852");
483 result.add("853");
484 result.add("855");
485 result.add("856");
486 result.add("870");
487 result.add("878");
488 result.add("880");
489 result.add("881");
490 result.add("882");
491 result.add("883");
492 result.add("886");
493 result.add("888");
494 result.add("960");
495 result.add("961");
496 result.add("962");
497 result.add("963");
498 result.add("964");
499 result.add("965");
500 result.add("966");
501 result.add("967");
502 result.add("968");
503 result.add("970");
504 result.add("971");
505 result.add("972");
506 result.add("973");
507 result.add("974");
508 result.add("975");
509 result.add("976");
510 result.add("977");
511 result.add("979");
512 result.add("992");
513 result.add("993");
514 result.add("994");
515 result.add("995");
516 result.add("996");
517 result.add("998");
518 return result;
519 }
520
521 public static SmartDialMap getMap() {
522 return mMap;
523 }
524
525 /**
526 * Indicates whether the given country uses NANP numbers
527 *
528 * @param country ISO 3166 country code (case doesn't matter)
529 * @return True if country uses NANP numbers (e.g. US, Canada), false otherwise
530 * @see <a href="https://en.wikipedia.org/wiki/North_American_Numbering_Plan">
531 * https://en.wikipedia.org/wiki/North_American_Numbering_Plan</a>
532 */
533 @VisibleForTesting
534 public static boolean isCountryNanp(String country) {
535 if (TextUtils.isEmpty(country)) {
536 return false;
537 }
538 if (sNanpCountries == null) {
539 sNanpCountries = initNanpCountries();
540 }
541 return sNanpCountries.contains(country.toUpperCase());
542 }
543
544 private static Set<String> initNanpCountries() {
545 final HashSet<String> result = new HashSet<String>();
546 result.add("US"); // United States
547 result.add("CA"); // Canada
548 result.add("AS"); // American Samoa
549 result.add("AI"); // Anguilla
550 result.add("AG"); // Antigua and Barbuda
551 result.add("BS"); // Bahamas
552 result.add("BB"); // Barbados
553 result.add("BM"); // Bermuda
554 result.add("VG"); // British Virgin Islands
555 result.add("KY"); // Cayman Islands
556 result.add("DM"); // Dominica
557 result.add("DO"); // Dominican Republic
558 result.add("GD"); // Grenada
559 result.add("GU"); // Guam
560 result.add("JM"); // Jamaica
561 result.add("PR"); // Puerto Rico
562 result.add("MS"); // Montserrat
563 result.add("MP"); // Northern Mariana Islands
564 result.add("KN"); // Saint Kitts and Nevis
565 result.add("LC"); // Saint Lucia
566 result.add("VC"); // Saint Vincent and the Grenadines
567 result.add("TT"); // Trinidad and Tobago
568 result.add("TC"); // Turks and Caicos Islands
569 result.add("VI"); // U.S. Virgin Islands
570 return result;
571 }
572
573 /**
574 * Returns whether the user is in a region that uses Nanp format based on the sim location.
575 *
576 * @return Whether user is in Nanp region.
577 */
578 public static boolean getUserInNanpRegion() {
579 return sUserInNanpRegion;
580 }
581
582 /** Explicitly setting the user Nanp to the given boolean */
583 @VisibleForTesting
584 public static void setUserInNanpRegion(boolean userInNanpRegion) {
585 sUserInNanpRegion = userInNanpRegion;
586 }
587
588 /** Class to record phone number parsing information. */
589 public static class PhoneNumberTokens {
590
591 /** Country code of the phone number. */
592 final String countryCode;
593
594 /** Offset of national number after the country code. */
595 final int countryCodeOffset;
596
597 /** Offset of local number after NANP area code. */
598 final int nanpCodeOffset;
599
600 public PhoneNumberTokens(String countryCode, int countryCodeOffset, int nanpCodeOffset) {
601 this.countryCode = countryCode;
602 this.countryCodeOffset = countryCodeOffset;
603 this.nanpCodeOffset = nanpCodeOffset;
604 }
605 }
606}