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