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