blob: 38936ec138508272dd347ca4f6bd6354ae6e0c56 [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;
Etan Cohen0ca1c802014-07-07 15:35:48 -070028
29public class ImsUtil {
Andrew Leea6bc3122015-01-29 14:47:34 -080030 private static final String LOG_TAG = ImsUtil.class.getSimpleName();
31 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
Andrew Lee77527ac2014-10-21 16:57:39 -070032
Andrew Lee77527ac2014-10-21 16:57:39 -070033 private static boolean sImsPhoneSupported = false;
34
Etan Cohen0ca1c802014-07-07 15:35:48 -070035 private ImsUtil() {
36 }
37
Etan Cohen0ca1c802014-07-07 15:35:48 -070038 static {
39 PhoneGlobals app = PhoneGlobals.getInstance();
40 sImsPhoneSupported = true;
41 }
42
43 /**
Andrew Leea6bc3122015-01-29 14:47:34 -080044 * @return {@code true} if this device supports voice calls using the built-in SIP stack.
Etan Cohen0ca1c802014-07-07 15:35:48 -070045 */
46 static boolean isImsPhoneSupported() {
47 return sImsPhoneSupported;
Andrew Lee77527ac2014-10-21 16:57:39 -070048
49 }
Andrew Leea6bc3122015-01-29 14:47:34 -080050
51 /**
52 * @return {@code true} if WFC is supported by the platform and has been enabled by the user.
53 */
54 public static boolean isWfcEnabled(Context context) {
Ashit Sood71ae4902017-10-06 14:02:39 -070055 return isWfcEnabled(context, SubscriptionManager.getDefaultVoicePhoneId());
56 }
57
58 /**
59 * @return {@code true} if WFC is supported per Slot and has been enabled by the user.
60 */
61 public static boolean isWfcEnabled(Context context, int phoneId) {
62 ImsManager imsManager = ImsManager.getInstance(context, phoneId);
Malcolm Chen995721f2017-12-12 18:19:27 -080063 boolean isEnabledByPlatform = imsManager.isWfcEnabledByPlatform();
64 boolean isEnabledByUser = imsManager.isWfcEnabledByUser();
Ashit Sood71ae4902017-10-06 14:02:39 -070065 if (DBG) Log.d(LOG_TAG, "isWfcEnabled :: isEnabledByPlatform=" + isEnabledByPlatform
66 + " phoneId=" + phoneId);
67 if (DBG) Log.d(LOG_TAG, "isWfcEnabled :: isEnabledByUser=" + isEnabledByUser
68 + " phoneId=" + phoneId);
Andrew Leea6bc3122015-01-29 14:47:34 -080069 return isEnabledByPlatform && isEnabledByUser;
70 }
71
72 /**
73 * @return {@code true} if the device is configured to use "Wi-Fi only" mode. If WFC is not
74 * enabled, this will return {@code false}.
75 */
76 public static boolean isWfcModeWifiOnly(Context context) {
Ashit Sood71ae4902017-10-06 14:02:39 -070077 return isWfcModeWifiOnly(context, SubscriptionManager.getDefaultVoicePhoneId());
78 }
79
80 /**
81 * @return {@code true} if the Slot is configured to use "Wi-Fi only" mode. If WFC is not
82 * enabled, this will return {@code false}.
83 */
84 public static boolean isWfcModeWifiOnly(Context context, int phoneId) {
85 ImsManager imsManager = ImsManager.getInstance(context, phoneId);
86 boolean isWifiOnlyMode =
87 imsManager.getWfcMode() == ImsConfig.WfcModeFeatureValueConstants.WIFI_ONLY;
88 if (DBG) Log.d(LOG_TAG, "isWfcModeWifiOnly :: isWifiOnlyMode" + isWifiOnlyMode
89 + " phoneId=" + phoneId);
90 return isWfcEnabled(context, phoneId) && isWifiOnlyMode;
Andrew Leea6bc3122015-01-29 14:47:34 -080091 }
Tyler Gunn0bb4d302016-07-07 10:28:39 -070092
93 /**
94 * When a call cannot be placed, determines if the use of WFC should be promoted, per the
95 * 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 -080096 * network, WFC is disabled but provisioned, and the carrier config indicates that the
97 * features should be promoted.
Tyler Gunn0bb4d302016-07-07 10:28:39 -070098 *
99 * @return {@code true} if use of WFC should be promoted, {@code false} otherwise.
100 */
101 public static boolean shouldPromoteWfc(Context context) {
Ashit Sood71ae4902017-10-06 14:02:39 -0700102 return shouldPromoteWfc(context, SubscriptionManager.getDefaultVoicePhoneId());
103 }
104
105 /**
106 * When a call cannot be placed, determines if the use of WFC should be promoted, per the
107 * carrier config of the slot. Use of WFC is promoted to the user if the device is
108 * connected to a WIFI network, WFC is disabled but provisioned, and the carrier config
109 * indicates that the features should be promoted.
110 *
111 * @return {@code true} if use of WFC should be promoted, {@code false} otherwise.
112 */
113 public static boolean shouldPromoteWfc(Context context, int phoneId) {
Tyler Gunn0bb4d302016-07-07 10:28:39 -0700114 CarrierConfigManager cfgManager = (CarrierConfigManager) context
115 .getSystemService(Context.CARRIER_CONFIG_SERVICE);
Grace Jia54070252020-01-13 16:28:59 -0800116
117 ImsManager imsManager = ImsManager.getInstance(context, phoneId);
118 if (!imsManager.isWfcEnabledByPlatform()) {
Tyler Gunn0bb4d302016-07-07 10:28:39 -0700119 return false;
120 }
121
Grace Jia54070252020-01-13 16:28:59 -0800122 if (!imsManager.isWfcProvisionedOnDevice()) {
123 return false;
124 }
125
126 if (cfgManager == null || !cfgManager.getConfigForSubId(getSubId(phoneId))
127 .getBoolean(CarrierConfigManager.KEY_CARRIER_PROMOTE_WFC_ON_CALL_FAIL_BOOL)) {
Meng Wange57bf022017-02-14 17:51:18 -0800128 return false;
129 }
130
Tyler Gunn0bb4d302016-07-07 10:28:39 -0700131 ConnectivityManager cm =
132 (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
133 if (cm != null) {
134 NetworkInfo ni = cm.getActiveNetworkInfo();
135 if (ni != null && ni.isConnected()) {
Ashit Sood71ae4902017-10-06 14:02:39 -0700136 return ni.getType() == ConnectivityManager.TYPE_WIFI && !isWfcEnabled(context,
137 phoneId);
Tyler Gunn0bb4d302016-07-07 10:28:39 -0700138 }
139 }
140 return false;
141 }
Malcolm Chen995721f2017-12-12 18:19:27 -0800142
143 private static ImsManager getDefaultImsManagerInstance(Context context) {
144 return ImsManager.getInstance(context, SubscriptionManager.getDefaultVoicePhoneId());
145 }
Ashit Sood71ae4902017-10-06 14:02:39 -0700146
147 private static int getSubId(int phoneId) {
148 final int[] subIds = SubscriptionManager.getSubId(phoneId);
149 int subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
150 if (subIds != null && subIds.length >= 1) {
151 subId = subIds[0];
152 }
153 return subId;
154 }
Etan Cohen0ca1c802014-07-07 15:35:48 -0700155}