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 17package com.android.dialer.calllog; 18 19import android.content.Context; 20import android.provider.CallLog; 21import android.telecom.PhoneAccountHandle; 22import android.telecom.TelecomManager; 23import android.telephony.PhoneNumberUtils; 24import android.text.TextUtils; 25 26import com.android.contacts.common.util.PhoneNumberHelper; 27 28import com.google.common.collect.Sets; 29 30import java.util.Set; 31 32/** 33 * 34 */ 35public class PhoneNumberUtilsWrapper { 36 private static final Set<String> LEGACY_UNKNOWN_NUMBERS = Sets.newHashSet("-1", "-2", "-3"); 37 private final Context mContext; 38 39 public PhoneNumberUtilsWrapper(Context context) { 40 mContext = context; 41 } 42 43 /** Returns true if it is possible to place a call to the given number. */ 44 public static boolean canPlaceCallsTo(CharSequence number, int presentation) { 45 return presentation == CallLog.Calls.PRESENTATION_ALLOWED 46 && !TextUtils.isEmpty(number) && !isLegacyUnknownNumbers(number); 47 } 48 49 /** 50 * Returns true if the given number is the number of the configured voicemail. To be able to 51 * mock-out this, it is not a static method. 52 */ 53 public boolean isVoicemailNumber(PhoneAccountHandle accountHandle, 54 CharSequence number) { 55 final TelecomManager telecomManager = 56 (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); 57 return number!= null && telecomManager.isVoiceMailNumber(accountHandle, number.toString()); 58 } 59 60 /** 61 * Returns true if the given number is a SIP address. To be able to mock-out this, it is not a 62 * static method. 63 */ 64 public static boolean isSipNumber(CharSequence number) { 65 return number != null && PhoneNumberHelper.isUriNumber(number.toString()); 66 } 67 68 public boolean isUnknownNumberThatCanBeLookedUp(PhoneAccountHandle accountHandle, 69 CharSequence number, int presentation) { 70 if (presentation == CallLog.Calls.PRESENTATION_UNKNOWN) { 71 return false; 72 } 73 if (presentation == CallLog.Calls.PRESENTATION_RESTRICTED) { 74 return false; 75 } 76 if (presentation == CallLog.Calls.PRESENTATION_PAYPHONE) { 77 return false; 78 } 79 if (TextUtils.isEmpty(number)) { 80 return false; 81 } 82 if (isVoicemailNumber(accountHandle, number)) { 83 return false; 84 } 85 if (isLegacyUnknownNumbers(number)) { 86 return false; 87 } 88 return true; 89 } 90 91 public static boolean isLegacyUnknownNumbers(CharSequence number) { 92 return number != null && LEGACY_UNKNOWN_NUMBERS.contains(number.toString()); 93 } 94} 95