blob: 0825cfb3e1b18f619f23702ee74e6e1fe0dff2fa [file] [log] [blame]
Etan Cohen0ca1c802014-07-07 15:35:48 -07001/*
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
17package com.android.phone;
18
Andrew Leea6bc3122015-01-29 14:47:34 -080019import android.content.Context;
Tyler Gunn0bb4d302016-07-07 10:28:39 -070020import android.net.ConnectivityManager;
21import android.net.NetworkInfo;
22import android.telephony.CarrierConfigManager;
Malcolm Chen995721f2017-12-12 18:19:27 -080023import android.telephony.SubscriptionManager;
Andrew Leea6bc3122015-01-29 14:47:34 -080024import android.util.Log;
25
26import com.android.ims.ImsConfig;
27import com.android.ims.ImsManager;
Meng Wang377b6ab2020-11-10 20:45:28 +000028import com.android.internal.telephony.PhoneFactory;
29import com.android.internal.telephony.imsphone.ImsPhone;
Etan Cohen0ca1c802014-07-07 15:35:48 -070030
31public class ImsUtil {
Andrew Leea6bc3122015-01-29 14:47:34 -080032 private static final String LOG_TAG = ImsUtil.class.getSimpleName();
33 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -070034
Andrew Lee77527ac2014-10-21 16:57:39 -070035 private static boolean sImsPhoneSupported = false;
36
Etan Cohen0ca1c802014-07-07 15:35:48 -070037 private ImsUtil() {
38 }
39
Etan Cohen0ca1c802014-07-07 15:35:48 -070040 static {
41 PhoneGlobals app = PhoneGlobals.getInstance();
42 sImsPhoneSupported = true;
43 }
44
45 /**
Andrew Leea6bc3122015-01-29 14:47:34 -080046 * @return {@code true} if this device supports voice calls using the built-in SIP stack.
Etan Cohen0ca1c802014-07-07 15:35:48 -070047 */
48 static boolean isImsPhoneSupported() {
49 return sImsPhoneSupported;
Andrew Lee77527ac2014-10-21 16:57:39 -070050
51 }
Andrew Leea6bc3122015-01-29 14:47:34 -080052
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 Sood71ae4902017-10-06 14:02:39 -070057 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 Chen995721f2017-12-12 18:19:27 -080065 boolean isEnabledByPlatform = imsManager.isWfcEnabledByPlatform();
66 boolean isEnabledByUser = imsManager.isWfcEnabledByUser();
Ashit Sood71ae4902017-10-06 14:02:39 -070067 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 Leea6bc3122015-01-29 14:47:34 -080071 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 Sood71ae4902017-10-06 14:02:39 -070079 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 Leea6bc3122015-01-29 14:47:34 -080093 }
Tyler Gunn0bb4d302016-07-07 10:28:39 -070094
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 Wange57bf022017-02-14 17:51:18 -080098 * network, WFC is disabled but provisioned, and the carrier config indicates that the
99 * features should be promoted.
Tyler Gunn0bb4d302016-07-07 10:28:39 -0700100 *
101 * @return {@code true} if use of WFC should be promoted, {@code false} otherwise.
102 */
103 public static boolean shouldPromoteWfc(Context context) {
Ashit Sood71ae4902017-10-06 14:02:39 -0700104 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 Gunn0bb4d302016-07-07 10:28:39 -0700116 CarrierConfigManager cfgManager = (CarrierConfigManager) context
117 .getSystemService(Context.CARRIER_CONFIG_SERVICE);
Grace Jia54070252020-01-13 16:28:59 -0800118
119 ImsManager imsManager = ImsManager.getInstance(context, phoneId);
120 if (!imsManager.isWfcEnabledByPlatform()) {
Tyler Gunn0bb4d302016-07-07 10:28:39 -0700121 return false;
122 }
123
Grace Jia54070252020-01-13 16:28:59 -0800124 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 Wange57bf022017-02-14 17:51:18 -0800130 return false;
131 }
132
Meng Wang377b6ab2020-11-10 20:45:28 +0000133 // 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 Gunn0bb4d302016-07-07 10:28:39 -0700140 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 Sood71ae4902017-10-06 14:02:39 -0700145 return ni.getType() == ConnectivityManager.TYPE_WIFI && !isWfcEnabled(context,
146 phoneId);
Tyler Gunn0bb4d302016-07-07 10:28:39 -0700147 }
148 }
149 return false;
150 }
Malcolm Chen995721f2017-12-12 18:19:27 -0800151
152 private static ImsManager getDefaultImsManagerInstance(Context context) {
153 return ImsManager.getInstance(context, SubscriptionManager.getDefaultVoicePhoneId());
154 }
Ashit Sood71ae4902017-10-06 14:02:39 -0700155
156 private static int getSubId(int phoneId) {
157 final int[] subIds = SubscriptionManager.getSubId(phoneId);
158 int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
159 if (subIds != null && subIds.length >= 1) {
160 subId = subIds[0];
161 }
162 return subId;
163 }
Meng Wang377b6ab2020-11-10 20:45:28 +0000164
165 private static boolean getLastKnownRoamingState(int phoneId) {
166 try {
167 ImsPhone imsPhone = (ImsPhone) (PhoneFactory.getPhone(phoneId).getImsPhone());
168 return imsPhone.getRoamingState();
169 } catch (NullPointerException | ClassCastException e) {
170 return false;
171 }
172 }
Etan Cohen0ca1c802014-07-07 15:35:48 -0700173}