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; |
Etan Cohen | 0ca1c80 | 2014-07-07 15:35:48 -0700 | [diff] [blame] | 28 | import com.android.phone.PhoneGlobals; |
| 29 | |
| 30 | public class ImsUtil { |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 31 | private static final String LOG_TAG = ImsUtil.class.getSimpleName(); |
| 32 | private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2); |
Andrew Lee | 77527ac | 2014-10-21 16:57:39 -0700 | [diff] [blame] | 33 | |
Andrew Lee | 77527ac | 2014-10-21 16:57:39 -0700 | [diff] [blame] | 34 | private static boolean sImsPhoneSupported = false; |
| 35 | |
Etan Cohen | 0ca1c80 | 2014-07-07 15:35:48 -0700 | [diff] [blame] | 36 | private ImsUtil() { |
| 37 | } |
| 38 | |
Etan Cohen | 0ca1c80 | 2014-07-07 15:35:48 -0700 | [diff] [blame] | 39 | static { |
| 40 | PhoneGlobals app = PhoneGlobals.getInstance(); |
| 41 | sImsPhoneSupported = true; |
| 42 | } |
| 43 | |
| 44 | /** |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 45 | * @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] | 46 | */ |
| 47 | static boolean isImsPhoneSupported() { |
| 48 | return sImsPhoneSupported; |
Andrew Lee | 77527ac | 2014-10-21 16:57:39 -0700 | [diff] [blame] | 49 | |
| 50 | } |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * @return {@code true} if WFC is supported by the platform and has been enabled by the user. |
| 54 | */ |
| 55 | public static boolean isWfcEnabled(Context context) { |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame^] | 56 | return isWfcEnabled(context, SubscriptionManager.getDefaultVoicePhoneId()); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @return {@code true} if WFC is supported per Slot and has been enabled by the user. |
| 61 | */ |
| 62 | public static boolean isWfcEnabled(Context context, int phoneId) { |
| 63 | ImsManager imsManager = ImsManager.getInstance(context, phoneId); |
Malcolm Chen | 995721f | 2017-12-12 18:19:27 -0800 | [diff] [blame] | 64 | boolean isEnabledByPlatform = imsManager.isWfcEnabledByPlatform(); |
| 65 | boolean isEnabledByUser = imsManager.isWfcEnabledByUser(); |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame^] | 66 | if (DBG) Log.d(LOG_TAG, "isWfcEnabled :: isEnabledByPlatform=" + isEnabledByPlatform |
| 67 | + " phoneId=" + phoneId); |
| 68 | if (DBG) Log.d(LOG_TAG, "isWfcEnabled :: isEnabledByUser=" + isEnabledByUser |
| 69 | + " phoneId=" + phoneId); |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 70 | return isEnabledByPlatform && isEnabledByUser; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @return {@code true} if the device is configured to use "Wi-Fi only" mode. If WFC is not |
| 75 | * enabled, this will return {@code false}. |
| 76 | */ |
| 77 | public static boolean isWfcModeWifiOnly(Context context) { |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame^] | 78 | return isWfcModeWifiOnly(context, SubscriptionManager.getDefaultVoicePhoneId()); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @return {@code true} if the Slot is configured to use "Wi-Fi only" mode. If WFC is not |
| 83 | * enabled, this will return {@code false}. |
| 84 | */ |
| 85 | public static boolean isWfcModeWifiOnly(Context context, int phoneId) { |
| 86 | ImsManager imsManager = ImsManager.getInstance(context, phoneId); |
| 87 | boolean isWifiOnlyMode = |
| 88 | imsManager.getWfcMode() == ImsConfig.WfcModeFeatureValueConstants.WIFI_ONLY; |
| 89 | if (DBG) Log.d(LOG_TAG, "isWfcModeWifiOnly :: isWifiOnlyMode" + isWifiOnlyMode |
| 90 | + " phoneId=" + phoneId); |
| 91 | return isWfcEnabled(context, phoneId) && isWifiOnlyMode; |
Andrew Lee | a6bc312 | 2015-01-29 14:47:34 -0800 | [diff] [blame] | 92 | } |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * When a call cannot be placed, determines if the use of WFC should be promoted, per the |
| 96 | * 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] | 97 | * network, WFC is disabled but provisioned, and the carrier config indicates that the |
| 98 | * features should be promoted. |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 99 | * |
| 100 | * @return {@code true} if use of WFC should be promoted, {@code false} otherwise. |
| 101 | */ |
| 102 | public static boolean shouldPromoteWfc(Context context) { |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame^] | 103 | return shouldPromoteWfc(context, SubscriptionManager.getDefaultVoicePhoneId()); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * When a call cannot be placed, determines if the use of WFC should be promoted, per the |
| 108 | * carrier config of the slot. Use of WFC is promoted to the user if the device is |
| 109 | * connected to a WIFI network, WFC is disabled but provisioned, and the carrier config |
| 110 | * indicates that the features should be promoted. |
| 111 | * |
| 112 | * @return {@code true} if use of WFC should be promoted, {@code false} otherwise. |
| 113 | */ |
| 114 | public static boolean shouldPromoteWfc(Context context, int phoneId) { |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 115 | CarrierConfigManager cfgManager = (CarrierConfigManager) context |
| 116 | .getSystemService(Context.CARRIER_CONFIG_SERVICE); |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame^] | 117 | if (cfgManager == null || cfgManager.getConfigForSubId(getSubId(phoneId)) |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 118 | .getBoolean(CarrierConfigManager.KEY_CARRIER_PROMOTE_WFC_ON_CALL_FAIL_BOOL)) { |
| 119 | return false; |
| 120 | } |
| 121 | |
Malcolm Chen | 995721f | 2017-12-12 18:19:27 -0800 | [diff] [blame] | 122 | if (!getDefaultImsManagerInstance(context).isWfcProvisionedOnDevice()) { |
Meng Wang | e57bf02 | 2017-02-14 17:51:18 -0800 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 126 | ConnectivityManager cm = |
| 127 | (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 128 | if (cm != null) { |
| 129 | NetworkInfo ni = cm.getActiveNetworkInfo(); |
| 130 | if (ni != null && ni.isConnected()) { |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame^] | 131 | return ni.getType() == ConnectivityManager.TYPE_WIFI && !isWfcEnabled(context, |
| 132 | phoneId); |
Tyler Gunn | 0bb4d30 | 2016-07-07 10:28:39 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | return false; |
| 136 | } |
Malcolm Chen | 995721f | 2017-12-12 18:19:27 -0800 | [diff] [blame] | 137 | |
| 138 | private static ImsManager getDefaultImsManagerInstance(Context context) { |
| 139 | return ImsManager.getInstance(context, SubscriptionManager.getDefaultVoicePhoneId()); |
| 140 | } |
Ashit Sood | 71ae490 | 2017-10-06 14:02:39 -0700 | [diff] [blame^] | 141 | |
| 142 | private static int getSubId(int phoneId) { |
| 143 | final int[] subIds = SubscriptionManager.getSubId(phoneId); |
| 144 | int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
| 145 | if (subIds != null && subIds.length >= 1) { |
| 146 | subId = subIds[0]; |
| 147 | } |
| 148 | return subId; |
| 149 | } |
Etan Cohen | 0ca1c80 | 2014-07-07 15:35:48 -0700 | [diff] [blame] | 150 | } |