Christine Chen | 03741c6 | 2013-10-23 16:17:03 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 16 | package com.android.contacts.util; |
Christine Chen | 03741c6 | 2013-10-23 16:17:03 -0700 | [diff] [blame] | 17 | |
Christine Chen | 03741c6 | 2013-10-23 16:17:03 -0700 | [diff] [blame] | 18 | import android.telephony.PhoneNumberUtils; |
Christine Chen | 03741c6 | 2013-10-23 16:17:03 -0700 | [diff] [blame] | 19 | import android.util.Log; |
| 20 | |
Christine Chen | 03741c6 | 2013-10-23 16:17:03 -0700 | [diff] [blame] | 21 | /** |
| 22 | * This class wraps several PhoneNumberUtil calls and TelephonyManager calls. Some of them are |
| 23 | * the same as the ones in the framework's code base. We can remove those once they are part of |
| 24 | * the public API. |
| 25 | */ |
| 26 | public class PhoneNumberHelper { |
| 27 | |
| 28 | private static final String LOG_TAG = PhoneNumberHelper.class.getSimpleName(); |
| 29 | |
Roshan Pius | 0810e9c | 2015-08-24 12:35:44 -0700 | [diff] [blame] | 30 | private static final String KOREA_ISO_COUNTRY_CODE = "KR"; |
Christine Chen | 03741c6 | 2013-10-23 16:17:03 -0700 | [diff] [blame] | 31 | /** |
Christine Chen | 03741c6 | 2013-10-23 16:17:03 -0700 | [diff] [blame] | 32 | * Determines if the specified number is actually a URI (i.e. a SIP address) rather than a |
| 33 | * regular PSTN phone number, based on whether or not the number contains an "@" character. |
| 34 | * |
| 35 | * @param number Phone number |
| 36 | * @return true if number contains @ |
| 37 | * |
| 38 | * TODO: Remove if PhoneNumberUtils.isUriNumber(String number) is made public. |
| 39 | */ |
| 40 | public static boolean isUriNumber(String number) { |
| 41 | // Note we allow either "@" or "%40" to indicate a URI, in case |
| 42 | // the passed-in string is URI-escaped. (Neither "@" nor "%40" |
| 43 | // will ever be found in a legal PSTN number.) |
| 44 | return number != null && (number.contains("@") || number.contains("%40")); |
| 45 | } |
| 46 | |
| 47 | /** |
Christine Chen | 03741c6 | 2013-10-23 16:17:03 -0700 | [diff] [blame] | 48 | * Normalize a phone number by removing the characters other than digits. If |
| 49 | * the given number has keypad letters, the letters will be converted to |
| 50 | * digits first. |
| 51 | * |
| 52 | * @param phoneNumber The number to be normalized. |
| 53 | * @return The normalized number. |
| 54 | * |
| 55 | * TODO: Remove if PhoneNumberUtils.normalizeNumber(String phoneNumber) is made public. |
| 56 | */ |
| 57 | public static String normalizeNumber(String phoneNumber) { |
| 58 | StringBuilder sb = new StringBuilder(); |
| 59 | int len = phoneNumber.length(); |
| 60 | for (int i = 0; i < len; i++) { |
| 61 | char c = phoneNumber.charAt(i); |
| 62 | // Character.digit() supports ASCII and Unicode digits (fullwidth, Arabic-Indic, etc.) |
| 63 | int digit = Character.digit(c, 10); |
| 64 | if (digit != -1) { |
| 65 | sb.append(digit); |
| 66 | } else if (i == 0 && c == '+') { |
| 67 | sb.append(c); |
| 68 | } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) { |
| 69 | return normalizeNumber(PhoneNumberUtils.convertKeypadLettersToDigits(phoneNumber)); |
| 70 | } |
| 71 | } |
| 72 | return sb.toString(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * @return the "username" part of the specified SIP address, i.e. the part before the "@" |
| 77 | * character (or "%40"). |
| 78 | * |
| 79 | * @param number SIP address of the form "username@domainname" (or the URI-escaped equivalent |
| 80 | * "username%40domainname") |
| 81 | * |
| 82 | * TODO: Remove if PhoneNumberUtils.getUsernameFromUriNumber(String number) is made public. |
| 83 | */ |
| 84 | public static String getUsernameFromUriNumber(String number) { |
| 85 | // The delimiter between username and domain name can be |
| 86 | // either "@" or "%40" (the URI-escaped equivalent.) |
| 87 | int delimiterIndex = number.indexOf('@'); |
| 88 | if (delimiterIndex < 0) { |
| 89 | delimiterIndex = number.indexOf("%40"); |
| 90 | } |
| 91 | if (delimiterIndex < 0) { |
| 92 | Log.w(LOG_TAG, |
| 93 | "getUsernameFromUriNumber: no delimiter found in SIP addr '" + number + "'"); |
| 94 | return number; |
| 95 | } |
| 96 | return number.substring(0, delimiterIndex); |
| 97 | } |
| 98 | } |