1 /*
2  * Copyright 2014, 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 
17 package com.android.server.telecom;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.net.Uri;
22 import android.telecom.PhoneAccount;
23 import android.telecom.PhoneAccountHandle;
24 import android.telephony.PhoneNumberUtils;
25 import android.telephony.SubscriptionManager;
26 import android.telephony.TelephonyManager;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 
30 import java.util.Collections;
31 import java.util.Comparator;
32 import java.util.List;
33 
34 /**
35  * Utilities to deal with the system telephony services. The system telephony services are treated
36  * differently from 3rd party services in some situations (emergency calls, audio focus, etc...).
37  */
38 public final class TelephonyUtil {
39     private static final String TELEPHONY_PACKAGE_NAME = "com.android.phone";
40 
41     private static final String PSTN_CALL_SERVICE_CLASS_NAME =
42             "com.android.services.telephony.TelephonyConnectionService";
43 
44     private static final PhoneAccountHandle DEFAULT_EMERGENCY_PHONE_ACCOUNT_HANDLE =
45             new PhoneAccountHandle(
46                     new ComponentName(TELEPHONY_PACKAGE_NAME, PSTN_CALL_SERVICE_CLASS_NAME), "E");
47 
TelephonyUtil()48     private TelephonyUtil() {}
49 
50     /**
51      * @return fallback {@link PhoneAccount} to be used by Telecom for emergency calls in the
52      * rare case that Telephony has not registered any phone accounts yet. Details about this
53      * account are not expected to be displayed in the UI, so the description, etc are not
54      * populated.
55      */
56     @VisibleForTesting
getDefaultEmergencyPhoneAccount()57     public static PhoneAccount getDefaultEmergencyPhoneAccount() {
58         return PhoneAccount.builder(DEFAULT_EMERGENCY_PHONE_ACCOUNT_HANDLE, "E")
59                 .setCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION |
60                         PhoneAccount.CAPABILITY_CALL_PROVIDER |
61                         PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS)
62                 .setIsEnabled(true)
63                 .build();
64     }
65 
isPstnComponentName(ComponentName componentName)66     static boolean isPstnComponentName(ComponentName componentName) {
67         final ComponentName pstnComponentName = new ComponentName(
68                 TELEPHONY_PACKAGE_NAME, PSTN_CALL_SERVICE_CLASS_NAME);
69         return pstnComponentName.equals(componentName);
70     }
71 
shouldProcessAsEmergency(Context context, Uri handle)72     public static boolean shouldProcessAsEmergency(Context context, Uri handle) {
73         try {
74             TelephonyManager tm = (TelephonyManager) context.getSystemService(
75                     Context.TELEPHONY_SERVICE);
76             return handle != null && tm.isEmergencyNumber(handle.getSchemeSpecificPart());
77         } catch (IllegalStateException ise) {
78             return false;
79         }
80     }
81 }
82