Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.ComponentName; |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.net.Uri; |
Yorke Lee | c127211 | 2014-10-16 09:50:00 -0700 | [diff] [blame] | 22 | import android.telecom.PhoneAccount; |
| 23 | import android.telecom.PhoneAccountHandle; |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 24 | import android.telephony.PhoneNumberUtils; |
Brad Ebinger | e6c481a | 2016-05-12 14:13:26 -0700 | [diff] [blame] | 25 | import android.telephony.SubscriptionManager; |
| 26 | import android.telephony.TelephonyManager; |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 27 | |
Hall Liu | ada0301 | 2015-10-26 15:44:00 -0700 | [diff] [blame] | 28 | import com.android.internal.annotations.VisibleForTesting; |
| 29 | |
Brad Ebinger | e6c481a | 2016-05-12 14:13:26 -0700 | [diff] [blame] | 30 | import java.util.Collections; |
| 31 | import java.util.Comparator; |
| 32 | import java.util.List; |
| 33 | |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 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 { |
Sailesh Nepal | 4a51550 | 2014-06-26 12:58:17 -0700 | [diff] [blame] | 39 | private static final String TELEPHONY_PACKAGE_NAME = "com.android.phone"; |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 40 | |
Ihab Awad | 55a3428 | 2014-06-18 10:31:09 -0700 | [diff] [blame] | 41 | private static final String PSTN_CALL_SERVICE_CLASS_NAME = |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 42 | "com.android.services.telephony.TelephonyConnectionService"; |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 43 | |
Yorke Lee | c127211 | 2014-10-16 09:50:00 -0700 | [diff] [blame] | 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 | |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 48 | private TelephonyUtil() {} |
| 49 | |
Yorke Lee | c127211 | 2014-10-16 09:50:00 -0700 | [diff] [blame] | 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 | */ |
Hall Liu | ada0301 | 2015-10-26 15:44:00 -0700 | [diff] [blame] | 56 | @VisibleForTesting |
| 57 | public static PhoneAccount getDefaultEmergencyPhoneAccount() { |
Yorke Lee | c127211 | 2014-10-16 09:50:00 -0700 | [diff] [blame] | 58 | return PhoneAccount.builder(DEFAULT_EMERGENCY_PHONE_ACCOUNT_HANDLE, "E") |
| 59 | .setCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION | |
| 60 | PhoneAccount.CAPABILITY_CALL_PROVIDER | |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 61 | PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS) |
| 62 | .setIsEnabled(true) |
| 63 | .build(); |
Yorke Lee | c127211 | 2014-10-16 09:50:00 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 66 | static boolean isPstnComponentName(ComponentName componentName) { |
Sailesh Nepal | 4a51550 | 2014-06-26 12:58:17 -0700 | [diff] [blame] | 67 | final ComponentName pstnComponentName = new ComponentName( |
| 68 | TELEPHONY_PACKAGE_NAME, PSTN_CALL_SERVICE_CLASS_NAME); |
Sailesh Nepal | 905dfba | 2014-07-14 08:20:41 -0700 | [diff] [blame] | 69 | return pstnComponentName.equals(componentName); |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Ihab Awad | 8d5d9dd | 2015-03-12 11:11:06 -0700 | [diff] [blame] | 72 | public static boolean shouldProcessAsEmergency(Context context, Uri handle) { |
Santos Cordon | 971e329 | 2015-07-09 12:46:21 -0700 | [diff] [blame] | 73 | return handle != null && PhoneNumberUtils.isLocalEmergencyNumber( |
Ihab Awad | 69eb0f5 | 2014-07-18 11:20:37 -0700 | [diff] [blame] | 74 | context, handle.getSchemeSpecificPart()); |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 75 | } |
Brad Ebinger | e6c481a | 2016-05-12 14:13:26 -0700 | [diff] [blame] | 76 | |
| 77 | public static void sortSimPhoneAccounts(Context context, List<PhoneAccount> accounts) { |
| 78 | final TelephonyManager telephonyManager = TelephonyManager.from(context); |
| 79 | |
| 80 | // Sort the accounts according to how we want to display them. |
| 81 | Collections.sort(accounts, new Comparator<PhoneAccount>() { |
| 82 | @Override |
| 83 | public int compare(PhoneAccount account1, PhoneAccount account2) { |
| 84 | int retval = 0; |
| 85 | |
| 86 | // SIM accounts go first |
| 87 | boolean isSim1 = account1.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION); |
| 88 | boolean isSim2 = account2.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION); |
| 89 | if (isSim1 != isSim2) { |
| 90 | retval = isSim1 ? -1 : 1; |
| 91 | } |
| 92 | |
| 93 | int subId1 = telephonyManager.getSubIdForPhoneAccount(account1); |
| 94 | int subId2 = telephonyManager.getSubIdForPhoneAccount(account2); |
| 95 | if (subId1 != SubscriptionManager.INVALID_SUBSCRIPTION_ID && |
| 96 | subId2 != SubscriptionManager.INVALID_SUBSCRIPTION_ID) { |
Sanket Padawe | 05cb0f9 | 2017-03-20 15:05:53 -0700 | [diff] [blame] | 97 | retval = (SubscriptionManager.getSlotIndex(subId1) < |
| 98 | SubscriptionManager.getSlotIndex(subId2)) ? -1 : 1; |
Brad Ebinger | e6c481a | 2016-05-12 14:13:26 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Then order by package |
| 102 | if (retval == 0) { |
| 103 | String pkg1 = account1.getAccountHandle().getComponentName().getPackageName(); |
| 104 | String pkg2 = account2.getAccountHandle().getComponentName().getPackageName(); |
| 105 | retval = pkg1.compareTo(pkg2); |
| 106 | } |
| 107 | |
| 108 | // Finally, order by label |
| 109 | if (retval == 0) { |
| 110 | String label1 = nullToEmpty(account1.getLabel().toString()); |
| 111 | String label2 = nullToEmpty(account2.getLabel().toString()); |
| 112 | retval = label1.compareTo(label2); |
| 113 | } |
| 114 | |
| 115 | // Then by hashcode |
| 116 | if (retval == 0) { |
| 117 | retval = account1.hashCode() - account2.hashCode(); |
| 118 | } |
| 119 | return retval; |
| 120 | } |
| 121 | }); |
| 122 | } |
| 123 | |
| 124 | private static String nullToEmpty(String str) { |
| 125 | return str == null ? "" : str; |
| 126 | } |
Sailesh Nepal | 18386a8 | 2014-03-19 10:22:40 -0700 | [diff] [blame] | 127 | } |