From a8d677fce88d2add418cdcc868f2a00b2e2d702e Mon Sep 17 00:00:00 2001 From: linyuh Date: Wed, 7 Mar 2018 17:16:08 -0800 Subject: Support local emergency phone number check when there are multiple SIMs. Bug: 73994020 Test: PhoneNumberHelperTest PiperOrigin-RevId: 188260007 Change-Id: I23025e9b9454f487117c35e7a3b09307371ae825 --- .../dialer/assisteddialing/Constraints.java | 3 +- .../dialer/oem/PhoneNumberUtilsAccessor.java | 47 ++++++++++++++++++++++ .../dialer/phonenumberutil/PhoneNumberHelper.java | 37 +++++++++++++++++ .../com/android/incallui/CallerInfoAsyncQuery.java | 4 +- 4 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 java/com/android/dialer/oem/PhoneNumberUtilsAccessor.java diff --git a/java/com/android/dialer/assisteddialing/Constraints.java b/java/com/android/dialer/assisteddialing/Constraints.java index 788b875ff..cae3734be 100644 --- a/java/com/android/dialer/assisteddialing/Constraints.java +++ b/java/com/android/dialer/assisteddialing/Constraints.java @@ -25,6 +25,7 @@ import android.text.TextUtils; import com.android.dialer.common.LogUtil; import com.android.dialer.logging.DialerImpression; import com.android.dialer.logging.Logger; +import com.android.dialer.phonenumberutil.PhoneNumberHelper; import com.android.dialer.strictmode.StrictModeUtils; import com.google.i18n.phonenumbers.NumberParseException; import com.google.i18n.phonenumbers.PhoneNumberUtil; @@ -209,7 +210,7 @@ final class Constraints { // roaming and out of service. boolean result = !PhoneNumberUtils.isEmergencyNumber(numberToCheck) - && !PhoneNumberUtils.isLocalEmergencyNumber(context, numberToCheck); + && !PhoneNumberHelper.isLocalEmergencyNumber(context, numberToCheck); LogUtil.i("Constraints.isNotEmergencyNumber", String.valueOf(result)); return result; } diff --git a/java/com/android/dialer/oem/PhoneNumberUtilsAccessor.java b/java/com/android/dialer/oem/PhoneNumberUtilsAccessor.java new file mode 100644 index 000000000..178a7b21d --- /dev/null +++ b/java/com/android/dialer/oem/PhoneNumberUtilsAccessor.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.dialer.oem; + +import android.content.Context; +import android.telephony.PhoneNumberUtils; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** Provides access to hidden APIs in {@link android.telephony.PhoneNumberUtils}. */ +public final class PhoneNumberUtilsAccessor { + + /** + * Checks if a given number is an emergency number for the country that the user is in. + * + * @param subId the subscription ID of the SIM + * @param number the number to check + * @param context the specific context which the number should be checked against + * @return true if the specified number is an emergency number for the country the user is + * currently in. + */ + public static boolean isLocalEmergencyNumber(Context context, int subId, String number) { + try { + Method method = + PhoneNumberUtils.class.getMethod( + "isLocalEmergencyNumber", Context.class, int.class, String.class); + return (boolean) method.invoke(null, context, subId, number); + + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } +} diff --git a/java/com/android/dialer/phonenumberutil/PhoneNumberHelper.java b/java/com/android/dialer/phonenumberutil/PhoneNumberHelper.java index e1051951e..33a94ea74 100644 --- a/java/com/android/dialer/phonenumberutil/PhoneNumberHelper.java +++ b/java/com/android/dialer/phonenumberutil/PhoneNumberHelper.java @@ -25,6 +25,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.telecom.PhoneAccountHandle; import android.telephony.PhoneNumberUtils; +import android.telephony.SubscriptionInfo; import android.telephony.TelephonyManager; import android.text.BidiFormatter; import android.text.TextDirectionHeuristics; @@ -34,10 +35,13 @@ import com.android.dialer.common.LogUtil; import com.android.dialer.compat.CompatUtils; import com.android.dialer.compat.telephony.TelephonyManagerCompat; import com.android.dialer.oem.MotorolaUtils; +import com.android.dialer.oem.PhoneNumberUtilsAccessor; import com.android.dialer.phonenumbergeoutil.PhoneNumberGeoUtilComponent; import com.android.dialer.telecom.TelecomUtil; +import com.google.common.base.Optional; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; public class PhoneNumberHelper { @@ -137,6 +141,39 @@ public class PhoneNumberHelper { return rawNumber1.equals(rawNumber2); } + /** + * An enhanced version of {@link PhoneNumberUtils#isLocalEmergencyNumber(Context, String)}. + * + *

This methods supports checking the number for all SIMs. + * + * @param context the context which the number should be checked against + * @param number the number to tbe checked + * @return true if the specified number is an emergency number for any SIM in the device. + */ + @SuppressWarnings("Guava") + public static boolean isLocalEmergencyNumber(Context context, String number) { + List phoneAccountHandles = + TelecomUtil.getSubscriptionPhoneAccounts(context); + + // If the number of phone accounts with a subscription is no greater than 1, only one SIM is + // installed in the device. We hand over the job to PhoneNumberUtils#isLocalEmergencyNumber. + if (phoneAccountHandles.size() <= 1) { + return PhoneNumberUtils.isLocalEmergencyNumber(context, number); + } + + for (PhoneAccountHandle phoneAccountHandle : phoneAccountHandles) { + Optional subscriptionInfo = + TelecomUtil.getSubscriptionInfo(context, phoneAccountHandle); + if (subscriptionInfo.isPresent() + && PhoneNumberUtilsAccessor.isLocalEmergencyNumber( + context, subscriptionInfo.get().getSubscriptionId(), number)) { + return true; + } + } + + return false; + } + /** * Returns true if the given number is the number of the configured voicemail. To be able to * mock-out this, it is not a static method. diff --git a/java/com/android/incallui/CallerInfoAsyncQuery.java b/java/com/android/incallui/CallerInfoAsyncQuery.java index 335895220..87dcc4f40 100644 --- a/java/com/android/incallui/CallerInfoAsyncQuery.java +++ b/java/com/android/incallui/CallerInfoAsyncQuery.java @@ -35,13 +35,13 @@ import android.provider.ContactsContract.Directory; import android.support.annotation.MainThread; import android.support.annotation.RequiresPermission; import android.support.annotation.WorkerThread; -import android.telephony.PhoneNumberUtils; import android.text.TextUtils; import com.android.dialer.common.cp2.DirectoryCompat; import com.android.dialer.phonenumbercache.CachedNumberLookupService; import com.android.dialer.phonenumbercache.CachedNumberLookupService.CachedContactInfo; import com.android.dialer.phonenumbercache.ContactInfoHelper; import com.android.dialer.phonenumbercache.PhoneNumberCache; +import com.android.dialer.phonenumberutil.PhoneNumberHelper; import com.android.dialer.strictmode.StrictModeUtils; import java.io.IOException; import java.io.InputStream; @@ -166,7 +166,7 @@ public class CallerInfoAsyncQuery { cw.countryIso = info.countryIso; // check to see if these are recognized numbers, and use shortcuts if we can. - if (PhoneNumberUtils.isLocalEmergencyNumber(context, info.phoneNumber)) { + if (PhoneNumberHelper.isLocalEmergencyNumber(context, info.phoneNumber)) { cw.event = EVENT_EMERGENCY_NUMBER; } else if (info.isVoiceMailNumber()) { cw.event = EVENT_VOICEMAIL_NUMBER; -- cgit v1.2.3