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