Etan Cohen | 0ca1c80 | 2014-07-07 15:35:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
| 17 | package com.android.phone; |
| 18 | |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 20 | import android.net.ConnectivityManager; |
| 21 | import android.net.NetworkInfo; |
| 22 | import android.telephony.CarrierConfigManager; |
Malcolm Chen | 995721f | 2017-12-12 18:19:27 -0800 | [diff] [blame] | 23 | import android.telephony.SubscriptionManager; |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | |
| 26 | import com.android.ims.ImsConfig; |
| 27 | import com.android.ims.ImsManager; |
Meng Wang | 377b6ab | 2020-11-10 20:45:28 +0000 | [diff] [blame] | 28 | import com.android.internal.telephony.PhoneFactory; |
| 29 | import com.android.internal.telephony.imsphone.ImsPhone; |
Etan Cohen | 0ca1c80 | 2014-07-07 15:35:48 -0700 | [diff] [blame] | 30 | |
| 31 | public class ImsUtil { |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 32 | private static final String LOG_TAG = ImsUtil.class.getSimpleName(); |
| 33 | private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2); |
Andrew Lee | 77527ac | 2014-10-21 16:57:39 -0700 | [diff] [blame] | 34 | |
Andrew Lee | 77527ac | 2014-10-21 16:57:39 -0700 | [diff] [blame] | 35 | private static boolean sImsPhoneSupported = false; |
| 36 | |
Etan Cohen | 0ca1c80 | 2014-07-07 15:35:48 -0700 | [diff] [blame] | 37 | private ImsUtil() { |
| 38 | } |
| 39 | |
Etan Cohen | 0ca1c80 | 2014-07-07 15:35:48 -0700 | [diff] [blame] | 40 | static { |
| 41 | PhoneGlobals app = PhoneGlobals.getInstance(); |
| 42 | sImsPhoneSupported = true; |
| 43 | } |
| 44 | |
| 45 | /** |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 46 | * @return {@code true} if this device supports voice calls using the built-in SIP stack. |
Etan Cohen | 0ca1c80 | 2014-07-07 15:35:48 -0700 | [diff] [blame] | 47 | */ |
| 48 | static boolean isImsPhoneSupported() { |
| 49 | return sImsPhoneSupported; |
Andrew Lee | 77527ac | 2014-10-21 16:57:39 -0700 | [diff] [blame] | 50 | |
| 51 | } |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * @return {@code true} if WFC is supported by the platform and has been enabled by the user. |
| 55 | */ |
| 56 | public static boolean isWfcEnabled(Context context) { |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame] | 57 | return isWfcEnabled(context, SubscriptionManager.getDefaultVoicePhoneId()); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @return {@code true} if WFC is supported per Slot and has been enabled by the user. |
| 62 | */ |
| 63 | public static boolean isWfcEnabled(Context context, int phoneId) { |
| 64 | ImsManager imsManager = ImsManager.getInstance(context, phoneId); |
Malcolm Chen | 995721f | 2017-12-12 18:19:27 -0800 | [diff] [blame] | 65 | boolean isEnabledByPlatform = imsManager.isWfcEnabledByPlatform(); |
| 66 | boolean isEnabledByUser = imsManager.isWfcEnabledByUser(); |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame] | 67 | if (DBG) Log.d(LOG_TAG, "isWfcEnabled :: isEnabledByPlatform=" + isEnabledByPlatform |
| 68 | + " phoneId=" + phoneId); |
| 69 | if (DBG) Log.d(LOG_TAG, "isWfcEnabled :: isEnabledByUser=" + isEnabledByUser |
| 70 | + " phoneId=" + phoneId); |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 71 | return isEnabledByPlatform && isEnabledByUser; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @return {@code true} if the device is configured to use "Wi-Fi only" mode. If WFC is not |
| 76 | * enabled, this will return {@code false}. |
| 77 | */ |
| 78 | public static boolean isWfcModeWifiOnly(Context context) { |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame] | 79 | return isWfcModeWifiOnly(context, SubscriptionManager.getDefaultVoicePhoneId()); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @return {@code true} if the Slot is configured to use "Wi-Fi only" mode. If WFC is not |
| 84 | * enabled, this will return {@code false}. |
| 85 | */ |
| 86 | public static boolean isWfcModeWifiOnly(Context context, int phoneId) { |
| 87 | ImsManager imsManager = ImsManager.getInstance(context, phoneId); |
| 88 | boolean isWifiOnlyMode = |
| 89 | imsManager.getWfcMode() == ImsConfig.WfcModeFeatureValueConstants.WIFI_ONLY; |
| 90 | if (DBG) Log.d(LOG_TAG, "isWfcModeWifiOnly :: isWifiOnlyMode" + isWifiOnlyMode |
| 91 | + " phoneId=" + phoneId); |
| 92 | return isWfcEnabled(context, phoneId) && isWifiOnlyMode; |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 93 | } |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * When a call cannot be placed, determines if the use of WFC should be promoted, per the |
| 97 | * carrier config. Use of WFC is promoted to the user if the device is connected to a WIFI |
Meng Wang | e57bf02 | 2017-02-14 17:51:18 -0800 | [diff] [blame] | 98 | * network, WFC is disabled but provisioned, and the carrier config indicates that the |
| 99 | * features should be promoted. |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 100 | * |
| 101 | * @return {@code true} if use of WFC should be promoted, {@code false} otherwise. |
| 102 | */ |
| 103 | public static boolean shouldPromoteWfc(Context context) { |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame] | 104 | return shouldPromoteWfc(context, SubscriptionManager.getDefaultVoicePhoneId()); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * When a call cannot be placed, determines if the use of WFC should be promoted, per the |
| 109 | * carrier config of the slot. Use of WFC is promoted to the user if the device is |
| 110 | * connected to a WIFI network, WFC is disabled but provisioned, and the carrier config |
| 111 | * indicates that the features should be promoted. |
| 112 | * |
| 113 | * @return {@code true} if use of WFC should be promoted, {@code false} otherwise. |
| 114 | */ |
| 115 | public static boolean shouldPromoteWfc(Context context, int phoneId) { |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 116 | CarrierConfigManager cfgManager = (CarrierConfigManager) context |
| 117 | .getSystemService(Context.CARRIER_CONFIG_SERVICE); |
Grace Jia | 5407025 | 2020-01-13 16:28:59 -0800 | [diff] [blame] | 118 | |
| 119 | ImsManager imsManager = ImsManager.getInstance(context, phoneId); |
| 120 | if (!imsManager.isWfcEnabledByPlatform()) { |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
Grace Jia | 5407025 | 2020-01-13 16:28:59 -0800 | [diff] [blame] | 124 | if (!imsManager.isWfcProvisionedOnDevice()) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | if (cfgManager == null || !cfgManager.getConfigForSubId(getSubId(phoneId)) |
| 129 | .getBoolean(CarrierConfigManager.KEY_CARRIER_PROMOTE_WFC_ON_CALL_FAIL_BOOL)) { |
Meng Wang | e57bf02 | 2017-02-14 17:51:18 -0800 | [diff] [blame] | 130 | return false; |
| 131 | } |
| 132 | |
Meng Wang | 377b6ab | 2020-11-10 20:45:28 +0000 | [diff] [blame] | 133 | // Do not promote WFC if in roaming and WFC roaming not allowed. |
| 134 | // WFC roaming setting is not modifiable, so its value is decided by the default value |
| 135 | // chosen by the carrier, hence it really means if the carrier supports WFC roaming. |
| 136 | if (getLastKnownRoamingState(phoneId) && !imsManager.isWfcRoamingEnabledByUser()) { |
| 137 | return false; |
| 138 | } |
| 139 | |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 140 | ConnectivityManager cm = |
| 141 | (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 142 | if (cm != null) { |
| 143 | NetworkInfo ni = cm.getActiveNetworkInfo(); |
| 144 | if (ni != null && ni.isConnected()) { |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame] | 145 | return ni.getType() == ConnectivityManager.TYPE_WIFI && !isWfcEnabled(context, |
| 146 | phoneId); |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | return false; |
| 150 | } |
Malcolm Chen | 995721f | 2017-12-12 18:19:27 -0800 | [diff] [blame] | 151 | |
| 152 | private static ImsManager getDefaultImsManagerInstance(Context context) { |
| 153 | return ImsManager.getInstance(context, SubscriptionManager.getDefaultVoicePhoneId()); |
| 154 | } |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame] | 155 | |
| 156 | private static int getSubId(int phoneId) { |
Jack Yu | de40a3f | 2022-11-19 22:29:12 -0800 | [diff] [blame] | 157 | return SubscriptionManager.getSubscriptionId(phoneId); |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame] | 158 | } |
Meng Wang | 377b6ab | 2020-11-10 20:45:28 +0000 | [diff] [blame] | 159 | |
| 160 | private static boolean getLastKnownRoamingState(int phoneId) { |
| 161 | try { |
| 162 | ImsPhone imsPhone = (ImsPhone) (PhoneFactory.getPhone(phoneId).getImsPhone()); |
Meng Wang | aea1176 | 2021-04-21 23:05:51 +0000 | [diff] [blame] | 163 | return imsPhone.getLastKnownRoamingState(); |
Meng Wang | 377b6ab | 2020-11-10 20:45:28 +0000 | [diff] [blame] | 164 | } catch (NullPointerException | ClassCastException e) { |
| 165 | return false; |
| 166 | } |
| 167 | } |
Etan Cohen | 0ca1c80 | 2014-07-07 15:35:48 -0700 | [diff] [blame] | 168 | } |