Merge "[Provider Model] Carrie Wi-Fi offload toggle request" into sc-dev am: d4a4fead67

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/14236566

Change-Id: I369883434d2f25d0e253fcee30c63cf2c036512d
diff --git a/res/values/strings.xml b/res/values/strings.xml
index ebe641e..0c74a20 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -13047,6 +13047,12 @@
     <string name="to_switch_networks_disconnect_ethernet">To switch networks, disconnect ethernet</string>
     <!-- Panel subtitle for Wi-Fi turned on. [CHAR LIMIT=60] -->
     <string name="wifi_is_turned_on_subtitle">Wi\u2011Fi is turned on</string>
+    <!-- Title for merged carrier Wi-Fi offload toggle. [CHAR LIMIT=NONE] -->
+    <string name="carrier_wifi_offload_title">W+ connections</string>
+    <!-- Summary for merged carrier Wi-Fi offload toggle. [CHAR LIMIT=NONE] -->
+    <string name="carrier_wifi_offload_summary">Allow Google Fi to use W+ networks to improve speed and coverage</string>
+    <!-- Title for merged carrier Wi-Fi network information. [CHAR LIMIT=NONE] -->
+    <string name="carrier_wifi_network_title">W+ network</string>
 
     <!-- Summary text separator for preferences including a short description
          (eg. "Connected / 5G"). [CHAR LIMIT=50] -->
diff --git a/res/xml/mobile_network_settings.xml b/res/xml/mobile_network_settings.xml
index 7ab9893..f50268a 100644
--- a/res/xml/mobile_network_settings.xml
+++ b/res/xml/mobile_network_settings.xml
@@ -16,8 +16,7 @@
 <PreferenceScreen
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:settings="http://schemas.android.com/apk/res-auto"
-    android:key="mobile_network_pref_screen"
-    settings:initialExpandedChildrenCount="8">
+    android:key="mobile_network_pref_screen">
 
     <com.android.settings.widget.SettingsMainSwitchPreference
         android:key="use_sim_switch"
@@ -79,6 +78,16 @@
             settings:controller="com.android.settings.datausage.BillingCyclePreferenceController"/>
 
         <SwitchPreference
+            android:key="carrier_wifi_toggle"
+            android:title="@string/carrier_wifi_offload_title"
+            android:summary="@string/carrier_wifi_offload_summary"
+            settings:controller="com.android.settings.network.CarrierWifiTogglePreferenceController"/>
+
+        <Preference
+            android:key="carrier_wifi_network"
+            android:title="@string/carrier_wifi_network_title"/>
+
+        <SwitchPreference
             android:key="mms_message"
             android:title="@string/mms_message_title"
             android:summary="@string/mms_message_summary"
diff --git a/src/com/android/settings/network/CarrierWifiTogglePreferenceController.java b/src/com/android/settings/network/CarrierWifiTogglePreferenceController.java
new file mode 100644
index 0000000..cf65034
--- /dev/null
+++ b/src/com/android/settings/network/CarrierWifiTogglePreferenceController.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.network;
+
+import android.content.Context;
+
+import androidx.lifecycle.Lifecycle;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
+
+import com.android.settings.Utils;
+import com.android.settings.core.TogglePreferenceController;
+import com.android.settings.wifi.WifiPickerTrackerHelper;
+import com.android.wifitrackerlib.WifiPickerTracker;
+
+/**
+ * Preference controller for "Carrier Wi-Fi network"
+ */
+public class CarrierWifiTogglePreferenceController extends TogglePreferenceController implements
+        WifiPickerTracker.WifiPickerTrackerCallback {
+
+    private static final String TAG = "CarrierWifiTogglePreferenceController";
+    protected static final String CARRIER_WIFI_TOGGLE_PREF_KEY = "carrier_wifi_toggle";
+    protected static final String CARRIER_WIFI_NETWORK_PREF_KEY = "carrier_wifi_network";
+
+    protected final Context mContext;
+    protected boolean mIsProviderModelEnabled;
+    protected int mSubId;
+    protected WifiPickerTrackerHelper mWifiPickerTrackerHelper;
+    protected boolean mIsCarrierProvisionWifiEnabled;
+    protected Preference mCarrierNetworkPreference;
+
+    public CarrierWifiTogglePreferenceController(Context context,
+            String preferenceKey) {
+        super(context, preferenceKey);
+        mContext = context;
+        mIsProviderModelEnabled = Utils.isProviderModelEnabled(context);
+    }
+
+    /** Initialize related properties */
+    public void init(Lifecycle lifecycle, int subId) {
+        mSubId = subId;
+        mWifiPickerTrackerHelper = new WifiPickerTrackerHelper(lifecycle, mContext, this);
+        mIsCarrierProvisionWifiEnabled =
+                mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(mSubId);
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (!mIsProviderModelEnabled) {
+            return CONDITIONALLY_UNAVAILABLE;
+        }
+        return mIsCarrierProvisionWifiEnabled ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
+    }
+
+    @Override
+    public boolean isChecked() {
+        return mWifiPickerTrackerHelper.isCarrierNetworkEnabled(mSubId);
+    }
+
+    @Override
+    public boolean setChecked(boolean isChecked) {
+        if (mWifiPickerTrackerHelper == null) {
+            return false;
+        }
+        mWifiPickerTrackerHelper.setCarrierNetworkEnabled(isChecked);
+        updateCarrierNetworkPreference(isChecked);
+        return true;
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        mCarrierNetworkPreference = screen.findPreference(CARRIER_WIFI_NETWORK_PREF_KEY);
+        updateCarrierNetworkPreference(isChecked());
+    }
+
+    @Override
+    public void onWifiStateChanged() {
+        if (mCarrierNetworkPreference != null && mCarrierNetworkPreference.isVisible()) {
+            mCarrierNetworkPreference.setSummary(getCarrierNetworkSsid());
+        }
+    }
+
+    @Override
+    public void onWifiEntriesChanged() {
+        if (mCarrierNetworkPreference != null && mCarrierNetworkPreference.isVisible()) {
+            mCarrierNetworkPreference.setSummary(getCarrierNetworkSsid());
+        }
+    }
+
+    @Override
+    public void onNumSavedNetworksChanged() {
+        // Do nothing
+    }
+
+    @Override
+    public void onNumSavedSubscriptionsChanged() {
+        // Do nothing
+    }
+
+    protected void updateCarrierNetworkPreference(boolean isCarrierNetworkEnabled) {
+        if (mCarrierNetworkPreference == null) {
+            return;
+        }
+        if (!isCarrierNetworkEnabled || getAvailabilityStatus() != AVAILABLE) {
+            mCarrierNetworkPreference.setVisible(false);
+            return;
+        }
+        mCarrierNetworkPreference.setVisible(true);
+        mCarrierNetworkPreference.setSummary(getCarrierNetworkSsid());
+    }
+
+    protected String getCarrierNetworkSsid() {
+        if (mWifiPickerTrackerHelper == null) {
+            return null;
+        }
+        return mWifiPickerTrackerHelper.getCarrierNetworkSsid();
+    }
+}
diff --git a/src/com/android/settings/network/telephony/MobileDataDialogFragment.java b/src/com/android/settings/network/telephony/MobileDataDialogFragment.java
index 0292c6b..aa772ad 100644
--- a/src/com/android/settings/network/telephony/MobileDataDialogFragment.java
+++ b/src/com/android/settings/network/telephony/MobileDataDialogFragment.java
@@ -131,7 +131,8 @@
             case TYPE_DISABLE_DIALOG:
                 MobileNetworkUtils.setMobileDataEnabled(getContext(), mSubId, false /* enabled */,
                         false /* disableOtherSubscriptions */);
-                if (mWifiPickerTrackerHelper != null) {
+                if (mWifiPickerTrackerHelper != null
+                        && !mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(mSubId)) {
                     mWifiPickerTrackerHelper.setCarrierNetworkEnabled(false);
                 }
                 break;
@@ -139,7 +140,8 @@
                 mSubscriptionManager.setDefaultDataSubId(mSubId);
                 MobileNetworkUtils.setMobileDataEnabled(getContext(), mSubId, true /* enabled */,
                         true /* disableOtherSubscriptions */);
-                if (mWifiPickerTrackerHelper != null) {
+                if (mWifiPickerTrackerHelper != null
+                        && !mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(mSubId)) {
                     mWifiPickerTrackerHelper.setCarrierNetworkEnabled(true);
                 }
                 break;
diff --git a/src/com/android/settings/network/telephony/MobileDataPreferenceController.java b/src/com/android/settings/network/telephony/MobileDataPreferenceController.java
index 78bc0a0..1ac121e 100644
--- a/src/com/android/settings/network/telephony/MobileDataPreferenceController.java
+++ b/src/com/android/settings/network/telephony/MobileDataPreferenceController.java
@@ -110,7 +110,8 @@
         if (!mNeedDialog) {
             // Update data directly if we don't need dialog
             MobileNetworkUtils.setMobileDataEnabled(mContext, mSubId, isChecked, false);
-            if (mWifiPickerTrackerHelper != null) {
+            if (mWifiPickerTrackerHelper != null
+                    && !mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(mSubId)) {
                 mWifiPickerTrackerHelper.setCarrierNetworkEnabled(isChecked);
             }
             return true;
diff --git a/src/com/android/settings/network/telephony/MobileNetworkSettings.java b/src/com/android/settings/network/telephony/MobileNetworkSettings.java
index 6e89c8a..f54c18b 100644
--- a/src/com/android/settings/network/telephony/MobileNetworkSettings.java
+++ b/src/com/android/settings/network/telephony/MobileNetworkSettings.java
@@ -38,6 +38,7 @@
 import com.android.settings.datausage.BillingCyclePreferenceController;
 import com.android.settings.datausage.DataUsageSummaryPreferenceController;
 import com.android.settings.network.ActiveSubsciptionsListener;
+import com.android.settings.network.CarrierWifiTogglePreferenceController;
 import com.android.settings.network.telephony.cdma.CdmaSubscriptionPreferenceController;
 import com.android.settings.network.telephony.cdma.CdmaSystemSelectPreferenceController;
 import com.android.settings.network.telephony.gsm.AutoSelectPreferenceController;
@@ -159,6 +160,7 @@
         use(EnabledNetworkModePreferenceController.class).init(getLifecycle(), mSubId);
         use(DataServiceSetupPreferenceController.class).init(mSubId);
         use(Enable2gPreferenceController.class).init(mSubId);
+        use(CarrierWifiTogglePreferenceController.class).init(getLifecycle(), mSubId);
 
         final WifiCallingPreferenceController wifiCallingPreferenceController =
                 use(WifiCallingPreferenceController.class).init(mSubId);
diff --git a/src/com/android/settings/wifi/WifiPickerTrackerHelper.java b/src/com/android/settings/wifi/WifiPickerTrackerHelper.java
index c20f355..fcae6ea 100644
--- a/src/com/android/settings/wifi/WifiPickerTrackerHelper.java
+++ b/src/com/android/settings/wifi/WifiPickerTrackerHelper.java
@@ -17,12 +17,16 @@
 package com.android.settings.wifi;
 
 import android.content.Context;
+import android.net.wifi.WifiManager;
 import android.os.Handler;
 import android.os.HandlerThread;
 import android.os.Looper;
+import android.os.PersistableBundle;
 import android.os.Process;
 import android.os.SimpleClock;
 import android.os.SystemClock;
+import android.telephony.CarrierConfigManager;
+import android.util.Log;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
@@ -55,9 +59,12 @@
         }
     };
 
-    private WifiPickerTracker mWifiPickerTracker;
+    protected WifiPickerTracker mWifiPickerTracker;
     // Worker thread used for WifiPickerTracker work
-    private HandlerThread mWorkerThread;
+    protected HandlerThread mWorkerThread;
+
+    protected final WifiManager mWifiManager;
+    protected final CarrierConfigManager mCarrierConfigManager;
 
     public WifiPickerTrackerHelper(@NonNull Lifecycle lifecycle, @NonNull Context context,
             @Nullable WifiPickerTracker.WifiPickerTrackerCallback listener) {
@@ -80,26 +87,49 @@
                 SCAN_INTERVAL_MILLIS,
                 listener);
 
+        mWifiManager = context.getSystemService(WifiManager.class);
+        mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
     }
 
+    /** @OnLifecycleEvent(ON_DESTROY) */
     @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
     public void onDestroy() {
         mWorkerThread.quit();
     }
 
+    /** Return the WifiPickerTracker class */
     public @NonNull WifiPickerTracker getWifiPickerTracker() {
         return mWifiPickerTracker;
     }
 
-    public boolean setCarrierNetworkEnabled(boolean enable) {
-        final MergedCarrierEntry mergedCarrierEntry = mWifiPickerTracker.getMergedCarrierEntry();
-        if (mergedCarrierEntry == null) {
+    /** Return the enabled/disabled state of the carrier network provision */
+    public boolean isCarrierNetworkProvisionEnabled(int subId) {
+        final PersistableBundle config = mCarrierConfigManager.getConfigForSubId(subId);
+        if (config == null) {
+            Log.e(TAG, "Could not get carrier config, subId:" + subId);
             return false;
         }
-        mergedCarrierEntry.setEnabled(enable);
-        return true;
+        final boolean enabled = config.getBoolean(
+                CarrierConfigManager.KEY_CARRIER_PROVISIONS_WIFI_MERGED_NETWORKS_BOOL);
+        Log.i(TAG, "isCarrierNetworkProvisionEnabled:" + enabled);
+        return enabled;
     }
 
+    /** Return the enabled/disabled state of the carrier network */
+    public boolean isCarrierNetworkEnabled(int subId) {
+        return mWifiManager.isCarrierNetworkOffloadEnabled(subId, true /* merged */);
+    }
+
+    /** Enables/disables the carrier network */
+    public void setCarrierNetworkEnabled(boolean enabled) {
+        final MergedCarrierEntry mergedCarrierEntry = mWifiPickerTracker.getMergedCarrierEntry();
+        if (mergedCarrierEntry == null) {
+            return;
+        }
+        mergedCarrierEntry.setEnabled(enabled);
+    }
+
+    /** Connect to the carrier network */
     public boolean connectCarrierNetwork(@Nullable WifiEntry.ConnectCallback callback) {
         final MergedCarrierEntry mergedCarrierEntry = mWifiPickerTracker.getMergedCarrierEntry();
         if (mergedCarrierEntry == null || !mergedCarrierEntry.canConnect()) {
@@ -109,6 +139,15 @@
         return true;
     }
 
+    /** Return the carrier network ssid */
+    public String getCarrierNetworkSsid() {
+        final MergedCarrierEntry mergedCarrierEntry = mWifiPickerTracker.getMergedCarrierEntry();
+        if (mergedCarrierEntry == null) {
+            return null;
+        }
+        return mergedCarrierEntry.getSsid();
+    }
+
     @VisibleForTesting
     void setWifiPickerTracker(@NonNull WifiPickerTracker wifiPickerTracker) {
         mWifiPickerTracker = wifiPickerTracker;
diff --git a/tests/unit/src/com/android/settings/network/CarrierWifiTogglePreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/CarrierWifiTogglePreferenceControllerTest.java
new file mode 100644
index 0000000..4deea3f
--- /dev/null
+++ b/tests/unit/src/com/android/settings/network/CarrierWifiTogglePreferenceControllerTest.java
@@ -0,0 +1,183 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.network;
+
+import static com.android.settings.core.BasePreferenceController.AVAILABLE;
+import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+import android.content.Context;
+import android.os.Looper;
+
+import androidx.lifecycle.Lifecycle;
+import androidx.preference.Preference;
+import androidx.preference.PreferenceManager;
+import androidx.preference.PreferenceScreen;
+import androidx.test.core.app.ApplicationProvider;
+import androidx.test.ext.junit.runners.AndroidJUnit4;
+
+import com.android.settings.wifi.WifiPickerTrackerHelper;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+
+@RunWith(AndroidJUnit4.class)
+public class CarrierWifiTogglePreferenceControllerTest {
+
+    private static final int SUB_ID = 2;
+    private static final String SSID = "ssid";
+
+    @Rule
+    public final MockitoRule mMockitoRule = MockitoJUnit.rule();
+    @Mock
+    public WifiPickerTrackerHelper mWifiPickerTrackerHelper;
+
+    private Context mContext;
+    private CarrierWifiTogglePreferenceController mController;
+    private PreferenceScreen mScreen;
+    private Preference mTogglePreference;
+    private Preference mNetworkPreference;
+
+    @Before
+    public void setUp() {
+        mContext = spy(ApplicationProvider.getApplicationContext());
+
+        mController = new CarrierWifiTogglePreferenceController(mContext,
+                CarrierWifiTogglePreferenceController.CARRIER_WIFI_TOGGLE_PREF_KEY);
+        mController.init(mock(Lifecycle.class), SUB_ID);
+        mController.mIsProviderModelEnabled = true;
+        mController.mIsCarrierProvisionWifiEnabled = true;
+        doReturn(SSID).when(mWifiPickerTrackerHelper).getCarrierNetworkSsid();
+        mController.mWifiPickerTrackerHelper = mWifiPickerTrackerHelper;
+        if (Looper.myLooper() == null) {
+            Looper.prepare();
+        }
+        final PreferenceManager preferenceManager = new PreferenceManager(mContext);
+        mScreen = preferenceManager.createPreferenceScreen(mContext);
+        mTogglePreference = new Preference(mContext);
+        mTogglePreference.setKey(
+                CarrierWifiTogglePreferenceController.CARRIER_WIFI_TOGGLE_PREF_KEY);
+        mScreen.addPreference(mTogglePreference);
+        mNetworkPreference = new Preference(mContext);
+        mNetworkPreference.setKey(
+                CarrierWifiTogglePreferenceController.CARRIER_WIFI_NETWORK_PREF_KEY);
+        mScreen.addPreference(mNetworkPreference);
+    }
+
+    @Test
+    public void getAvailabilityStatus_providerModelDisable_returnUnavailable() {
+        mController.mIsProviderModelEnabled = false;
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_carrierProvisionWifiEnabled_returnAvailable() {
+        mController.mIsCarrierProvisionWifiEnabled = true;
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
+    }
+
+    @Test
+    public void getAvailabilityStatus_carrierProvisionWifiDisabled_returnUnavailable() {
+        mController.mIsCarrierProvisionWifiEnabled = false;
+
+        assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
+    }
+
+    @Test
+    public void isChecked_carrierNetworkEnabled_returnTrue() {
+        doReturn(true).when(mWifiPickerTrackerHelper).isCarrierNetworkEnabled(SUB_ID);
+
+        assertThat(mController.isChecked()).isEqualTo(true);
+    }
+
+    @Test
+    public void isChecked_carrierNetworkDisabled_returnFalse() {
+        doReturn(false).when(mWifiPickerTrackerHelper).isCarrierNetworkEnabled(SUB_ID);
+
+        assertThat(mController.isChecked()).isEqualTo(false);
+    }
+
+    @Test
+    public void setChecked_checked_shouldSetCarrierNetworkEnabled() {
+        mController.setChecked(true);
+
+        verify(mWifiPickerTrackerHelper).setCarrierNetworkEnabled(true);
+    }
+
+    @Test
+    public void setChecked_unchecked_shouldSetCarrierNetworkDisabled() {
+        mController.setChecked(false);
+
+        verify(mWifiPickerTrackerHelper).setCarrierNetworkEnabled(false);
+    }
+
+    @Test
+    public void displayPreference_carrierNetworkEnabled_showCarrierNetwork() {
+        doReturn(true).when(mWifiPickerTrackerHelper).isCarrierNetworkEnabled(SUB_ID);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mController.mCarrierNetworkPreference).isEqualTo(mNetworkPreference);
+        assertThat(mNetworkPreference.isVisible()).isTrue();
+        assertThat(mNetworkPreference.getSummary()).isEqualTo(SSID);
+    }
+
+    @Test
+    public void displayPreference_carrierNetworkDisabled_hideCarrierNetwork() {
+        doReturn(false).when(mWifiPickerTrackerHelper).isCarrierNetworkEnabled(SUB_ID);
+
+        mController.displayPreference(mScreen);
+
+        assertThat(mController.mCarrierNetworkPreference).isEqualTo(mNetworkPreference);
+        assertThat(mNetworkPreference.isVisible()).isFalse();
+    }
+
+    @Test
+    public void onWifiStateChanged_carrierNetworkVisible_shouldSetSummary() {
+        mController.mCarrierNetworkPreference = mNetworkPreference;
+        mNetworkPreference.setVisible(true);
+        mNetworkPreference.setSummary(null);
+
+        mController.onWifiEntriesChanged();
+
+        assertThat(mNetworkPreference.getSummary()).isEqualTo(SSID);
+    }
+
+    @Test
+    public void onWifiEntriesChanged_carrierNetworkVisible_shouldSetSummary() {
+        mController.mCarrierNetworkPreference = mNetworkPreference;
+        mNetworkPreference.setVisible(true);
+        mNetworkPreference.setSummary(null);
+
+        mController.onWifiEntriesChanged();
+
+        assertThat(mNetworkPreference.getSummary()).isEqualTo(SSID);
+    }
+}
diff --git a/tests/unit/src/com/android/settings/wifi/WifiPickerTrackerHelperTest.java b/tests/unit/src/com/android/settings/wifi/WifiPickerTrackerHelperTest.java
index 2991550..70ad0f0 100644
--- a/tests/unit/src/com/android/settings/wifi/WifiPickerTrackerHelperTest.java
+++ b/tests/unit/src/com/android/settings/wifi/WifiPickerTrackerHelperTest.java
@@ -20,12 +20,18 @@
 
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
+import android.content.Context;
+import android.net.wifi.WifiManager;
 import android.os.HandlerThread;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
 
 import androidx.lifecycle.Lifecycle;
 import androidx.test.core.app.ApplicationProvider;
@@ -47,9 +53,15 @@
 @RunWith(AndroidJUnit4.class)
 public class WifiPickerTrackerHelperTest {
 
+    private static final int SUB_ID = 2;
+
     @Rule
     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
     @Mock
+    public WifiManager mWifiManager;
+    @Mock
+    public CarrierConfigManager mCarrierConfigManager;
+    @Mock
     public WifiPickerTracker mWifiPickerTracker;
     @Mock
     public MergedCarrierEntry mMergedCarrierEntry;
@@ -59,16 +71,24 @@
     private WifiPickerTrackerHelper mWifiPickerTrackerHelper;
 
     private FakeFeatureFactory mFeatureFactory;
+    private PersistableBundle mCarrierConfig;
 
     @Before
     public void setUp() {
+        final Context context = spy(ApplicationProvider.getApplicationContext());
+        when(context.getSystemService(WifiManager.class)).thenReturn(mWifiManager);
+        when(context.getSystemService(CarrierConfigManager.class))
+                .thenReturn(mCarrierConfigManager);
+        mCarrierConfig = new PersistableBundle();
+        doReturn(mCarrierConfig).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
+
         mFeatureFactory = FakeFeatureFactory.setupForTest();
         when(mFeatureFactory.wifiTrackerLibProvider
                 .createWifiPickerTracker(
                         any(), any(), any(), any(), any(), anyLong(), anyLong(), any()))
                 .thenReturn(mWifiPickerTracker);
         mWifiPickerTrackerHelper = new WifiPickerTrackerHelper(mock(Lifecycle.class),
-                ApplicationProvider.getApplicationContext(), null);
+                context, null);
     }
 
     @Test
@@ -87,24 +107,63 @@
     }
 
     @Test
-    public void setCarrierNetworkEnabled_returnTrueAndSetEnabled() {
+    public void isCarrierNetworkProvisionEnabled_getNullConfig_returnFalse() {
+        doReturn(null).when(mCarrierConfigManager).getConfigForSubId(SUB_ID);
+
+        assertThat(mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(SUB_ID)).isFalse();
+    }
+
+    @Test
+    public void isCarrierNetworkProvisionEnabled_returnCorrect() {
+        mCarrierConfig.putBoolean(
+                CarrierConfigManager.KEY_CARRIER_PROVISIONS_WIFI_MERGED_NETWORKS_BOOL, true);
+
+        assertThat(mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(SUB_ID)).isTrue();
+
+        mCarrierConfig.putBoolean(
+                CarrierConfigManager.KEY_CARRIER_PROVISIONS_WIFI_MERGED_NETWORKS_BOOL, false);
+
+        assertThat(mWifiPickerTrackerHelper.isCarrierNetworkProvisionEnabled(SUB_ID)).isFalse();
+    }
+
+    @Test
+    public void isCarrierNetworkEnabled_returnCorrect() {
+        doReturn(true).when(mWifiManager).isCarrierNetworkOffloadEnabled(SUB_ID, true /* merged */);
+
+        assertThat(mWifiPickerTrackerHelper.isCarrierNetworkEnabled(SUB_ID)).isTrue();
+
+        doReturn(false).when(mWifiManager)
+                .isCarrierNetworkOffloadEnabled(SUB_ID, true /* merged */);
+
+        assertThat(mWifiPickerTrackerHelper.isCarrierNetworkEnabled(SUB_ID)).isFalse();
+    }
+
+    @Test
+    public void setCarrierNetworkEnabled_shouldSetCorrect() {
         mWifiPickerTrackerHelper.setWifiPickerTracker(mWifiPickerTracker);
         when(mWifiPickerTracker.getMergedCarrierEntry()).thenReturn(mMergedCarrierEntry);
 
-        assertThat(mWifiPickerTrackerHelper.setCarrierNetworkEnabled(true)).isTrue();
+        mWifiPickerTrackerHelper.setCarrierNetworkEnabled(true);
+
         verify(mMergedCarrierEntry).setEnabled(true);
 
-        assertThat(mWifiPickerTrackerHelper.setCarrierNetworkEnabled(false)).isTrue();
+        mWifiPickerTrackerHelper.setCarrierNetworkEnabled(false);
+
         verify(mMergedCarrierEntry).setEnabled(false);
     }
 
     @Test
-    public void setCarrierNetworkEnabled_mergedCarrierEntryIsNull_returnFalse() {
+    public void setCarrierNetworkEnabled_mergedCarrierEntryIsNull_shouldNotSet() {
         mWifiPickerTrackerHelper.setWifiPickerTracker(mWifiPickerTracker);
         when(mWifiPickerTracker.getMergedCarrierEntry()).thenReturn(null);
 
-        assertThat(mWifiPickerTrackerHelper.setCarrierNetworkEnabled(true)).isFalse();
-        assertThat(mWifiPickerTrackerHelper.setCarrierNetworkEnabled(false)).isFalse();
+        mWifiPickerTrackerHelper.setCarrierNetworkEnabled(true);
+
+        verify(mMergedCarrierEntry, never()).setEnabled(true);
+
+        mWifiPickerTrackerHelper.setCarrierNetworkEnabled(false);
+
+        verify(mMergedCarrierEntry, never()).setEnabled(true);
     }
 
     @Test