Merge "Show W+ when connected to Carrier Wi-Fi Network" into sc-dev
diff --git a/src/com/android/settings/network/SubscriptionsPreferenceController.java b/src/com/android/settings/network/SubscriptionsPreferenceController.java
index aea30f2..2d23f1f 100644
--- a/src/com/android/settings/network/SubscriptionsPreferenceController.java
+++ b/src/com/android/settings/network/SubscriptionsPreferenceController.java
@@ -28,6 +28,7 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.graphics.drawable.Drawable;
+import android.net.wifi.WifiManager;
 import android.provider.Settings;
 import android.telephony.ServiceState;
 import android.telephony.SignalStrength;
@@ -56,9 +57,11 @@
 import com.android.settings.network.telephony.TelephonyDisplayInfoListener;
 import com.android.settings.widget.GearPreference;
 import com.android.settings.wifi.WifiPickerTrackerHelper;
+import com.android.settingslib.SignalIcon.MobileIconGroup;
 import com.android.settingslib.core.AbstractPreferenceController;
 import com.android.settingslib.mobile.MobileMappings;
 import com.android.settingslib.mobile.MobileMappings.Config;
+import com.android.settingslib.mobile.TelephonyIcons;
 import com.android.settingslib.net.SignalStrengthUtil;
 
 import java.util.Collections;
@@ -96,13 +99,15 @@
     private WifiPickerTrackerHelper mWifiPickerTrackerHelper;
 
     @VisibleForTesting
-    final BroadcastReceiver mDataSubscriptionChangedReceiver = new BroadcastReceiver() {
+    final BroadcastReceiver mConnectionChangeReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
             final String action = intent.getAction();
             if (action.equals(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED)) {
                 mConfig = mSubsPrefCtrlInjector.getConfig(mContext);
                 update();
+            } else if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
+                update();
             }
         }
     };
@@ -159,17 +164,17 @@
         mConfig = mSubsPrefCtrlInjector.getConfig(mContext);
     }
 
-    private void registerDataSubscriptionChangedReceiver() {
+    private void registerReceiver() {
         IntentFilter filter = new IntentFilter();
         filter.addAction(TelephonyManager.ACTION_DEFAULT_DATA_SUBSCRIPTION_CHANGED);
-        mContext.registerReceiver(mDataSubscriptionChangedReceiver, filter);
+        filter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
+        mContext.registerReceiver(mConnectionChangeReceiver, filter);
     }
 
-    private void unRegisterDataSubscriptionChangedReceiver() {
-        if (mDataSubscriptionChangedReceiver != null) {
-            mContext.unregisterReceiver(mDataSubscriptionChangedReceiver);
+    private void unRegisterReceiver() {
+        if (mConnectionChangeReceiver != null) {
+            mContext.unregisterReceiver(mConnectionChangeReceiver);
         }
-
     }
 
     @OnLifecycleEvent(ON_RESUME)
@@ -179,7 +184,7 @@
         mConnectivityListener.start();
         mSignalStrengthListener.resume();
         mTelephonyDisplayInfoListener.resume();
-        registerDataSubscriptionChangedReceiver();
+        registerReceiver();
         update();
     }
 
@@ -190,7 +195,7 @@
         mConnectivityListener.stop();
         mSignalStrengthListener.pause();
         mTelephonyDisplayInfoListener.pause();
-        unRegisterDataSubscriptionChangedReceiver();
+        unRegisterReceiver();
     }
 
     @Override
@@ -264,9 +269,12 @@
 
         final boolean isDataInService = tmForSubId.getDataState()
                 == TelephonyManager.DATA_CONNECTED;
+        final boolean isActiveCarrierNetwork =
+                (mWifiPickerTrackerHelper != null)
+                        && mWifiPickerTrackerHelper.isActiveCarrierNetwork();
         String result = mSubsPrefCtrlInjector.getNetworkType(
-                mContext, mConfig, mTelephonyDisplayInfo, subId);
-        if (mSubsPrefCtrlInjector.isActiveCellularNetwork(mContext)) {
+                mContext, mConfig, mTelephonyDisplayInfo, subId, isActiveCarrierNetwork);
+        if (mSubsPrefCtrlInjector.isActiveCellularNetwork(mContext) || isActiveCarrierNetwork) {
             result = mContext.getString(R.string.preference_summary_default_combination,
                     mContext.getString(R.string.mobile_data_connection_active), result);
         } else if (!isDataInService) {
@@ -288,7 +296,8 @@
         Drawable icon = mSubsPrefCtrlInjector.getIcon(mContext, level, numLevels, false);
         final boolean isActiveCellularNetwork =
                 mSubsPrefCtrlInjector.isActiveCellularNetwork(mContext);
-        if (isActiveCellularNetwork) {
+        if (isActiveCellularNetwork || (mWifiPickerTrackerHelper != null)
+                        && mWifiPickerTrackerHelper.isActiveCarrierNetwork()) {
             icon.setTint(Utils.getColorAccentDefaultColor(mContext));
             return icon;
         }
@@ -523,7 +532,7 @@
     @VisibleForTesting
     public static class SubsPrefCtrlInjector {
         /**
-         * Use to inject function and value for class and test class.
+         * Uses to inject function and value for class and test class.
          */
         public boolean canSubscriptionBeDisplayed(Context context, int subId) {
             return (SubscriptionUtil.getAvailableSubscription(context,
@@ -538,42 +547,42 @@
         }
 
         /**
-         * Get default voice subscription ID.
+         * Gets default voice subscription ID.
          */
         public int getDefaultVoiceSubscriptionId() {
             return SubscriptionManager.getDefaultVoiceSubscriptionId();
         }
 
         /**
-         * Get default data subscription ID.
+         * Gets default data subscription ID.
          */
         public int getDefaultDataSubscriptionId() {
             return SubscriptionManager.getDefaultDataSubscriptionId();
         }
 
         /**
-         * Confirm the current network is cellular and active.
+         * Confirms the current network is cellular and active.
          */
         public boolean isActiveCellularNetwork(Context context) {
             return MobileNetworkUtils.activeNetworkIsCellular(context);
         }
 
         /**
-         * Confirm the flag of Provider Model switch is turned on or not.
+         * Confirms the flag of Provider Model switch is turned on or not.
          */
         public boolean isProviderModelEnabled(Context context) {
             return Utils.isProviderModelEnabled(context);
         }
 
         /**
-         * Get config for carrier customization.
+         * Gets config for carrier customization.
          */
         public Config getConfig(Context context) {
             return MobileMappings.Config.readConfig(context);
         }
 
         /**
-         * Get current mobile network type.
+         * Gets current mobile network type.
          */
         public String getNetworkType(Context context, Config config,
                 TelephonyDisplayInfo telephonyDisplayInfo, int subId) {
@@ -585,7 +594,24 @@
         }
 
         /**
-         * Get signal icon with different signal level.
+         * Gets current network type of Carrier Wi-Fi Network or Cellular.
+         */
+        public String getNetworkType(Context context, Config config,
+                TelephonyDisplayInfo telephonyDisplayInfo, int subId,
+                boolean isCarrierWifiNetwork) {
+            if (isCarrierWifiNetwork) {
+                MobileIconGroup carrierMergedWifiIconGroup = TelephonyIcons.CARRIER_MERGED_WIFI;
+                int resId = carrierMergedWifiIconGroup.dataContentDescription;
+                return resId != 0
+                        ? SubscriptionManager.getResourcesForSubId(context, subId)
+                        .getString(resId) : "";
+            } else {
+                return getNetworkType(context, config, telephonyDisplayInfo, subId);
+            }
+        }
+
+        /**
+         * Gets signal icon with different signal level.
          */
         public Drawable getIcon(Context context, int level, int numLevels, boolean cutOut) {
             return MobileNetworkUtils.getSignalStrengthIcon(context, level, numLevels,
diff --git a/src/com/android/settings/network/telephony/NetworkProviderWorker.java b/src/com/android/settings/network/telephony/NetworkProviderWorker.java
index 698e779..464ee75 100644
--- a/src/com/android/settings/network/telephony/NetworkProviderWorker.java
+++ b/src/com/android/settings/network/telephony/NetworkProviderWorker.java
@@ -39,8 +39,10 @@
 import com.android.settings.network.MobileDataEnabledListener;
 import com.android.settings.network.SubscriptionsChangeListener;
 import com.android.settings.wifi.slice.WifiScanWorker;
+import com.android.settingslib.SignalIcon.MobileIconGroup;
 import com.android.settingslib.mobile.MobileMappings;
 import com.android.settingslib.mobile.MobileMappings.Config;
+import com.android.settingslib.mobile.TelephonyIcons;
 
 import java.util.Collections;
 
@@ -247,14 +249,21 @@
         return SubscriptionManager.getDefaultDataSubscriptionId();
     }
 
-
     private String updateNetworkTypeName(Context context, Config config,
             TelephonyDisplayInfo telephonyDisplayInfo, int subId) {
         String iconKey = getIconKey(telephonyDisplayInfo);
         int resId = mapIconSets(config).get(iconKey).dataContentDescription;
+        if (mWifiPickerTrackerHelper != null
+                && mWifiPickerTrackerHelper.isActiveCarrierNetwork()) {
+            MobileIconGroup carrierMergedWifiIconGroup = TelephonyIcons.CARRIER_MERGED_WIFI;
+            resId = carrierMergedWifiIconGroup.dataContentDescription;
+            return resId != 0
+                    ? SubscriptionManager.getResourcesForSubId(context, subId)
+                    .getString(resId) : "";
+        }
+
         return resId != 0
                 ? SubscriptionManager.getResourcesForSubId(context, subId).getString(resId) : "";
-
     }
 
     @VisibleForTesting
diff --git a/src/com/android/settings/wifi/WifiPickerTrackerHelper.java b/src/com/android/settings/wifi/WifiPickerTrackerHelper.java
index fcae6ea..aaab368 100644
--- a/src/com/android/settings/wifi/WifiPickerTrackerHelper.java
+++ b/src/com/android/settings/wifi/WifiPickerTrackerHelper.java
@@ -139,6 +139,16 @@
         return true;
     }
 
+    /** Confirms connection of the carrier network */
+    public boolean isActiveCarrierNetwork() {
+        final MergedCarrierEntry mergedCarrierEntry = mWifiPickerTracker.getMergedCarrierEntry();
+        if (mergedCarrierEntry != null) {
+            return mergedCarrierEntry.getConnectedState() == WifiEntry.CONNECTED_STATE_CONNECTED
+                    && mergedCarrierEntry.hasInternetAccess();
+        }
+        return false;
+    }
+
     /** Return the carrier network ssid */
     public String getCarrierNetworkSsid() {
         final MergedCarrierEntry mergedCarrierEntry = mWifiPickerTracker.getMergedCarrierEntry();
diff --git a/tests/unit/src/com/android/settings/network/SubscriptionsPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/SubscriptionsPreferenceControllerTest.java
index 916d4e6..0ef44ee 100644
--- a/tests/unit/src/com/android/settings/network/SubscriptionsPreferenceControllerTest.java
+++ b/tests/unit/src/com/android/settings/network/SubscriptionsPreferenceControllerTest.java
@@ -428,7 +428,30 @@
                 TelephonyManager.DATA_CONNECTED, ServiceState.STATE_IN_SERVICE);
         doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext);
         doReturn(networkType)
-                .when(sInjector).getNetworkType(any(), any(), any(), anyInt());
+                .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false));
+
+        mController.onResume();
+        mController.displayPreference(mPreferenceScreen);
+
+        assertThat(mPreferenceCategory.getPreference(0).getSummary()).isEqualTo(expectedSummary);
+    }
+
+    @Test
+    @UiThreadTest
+    public void displayPreference_providerAndHasMultiSimAndActiveCarrierWifi_connectedAndWPlus() {
+        final CharSequence expectedSummary =
+                Html.fromHtml("Connected / W+", Html.FROM_HTML_MODE_LEGACY);
+        final String networkType = "W+";
+        final List<SubscriptionInfo> sub = setupMockSubscriptions(2);
+        doReturn(true).when(sInjector).isProviderModelEnabled(mContext);
+        doReturn(sub.get(0)).when(mSubscriptionManager).getDefaultDataSubscriptionInfo();
+        setupGetIconConditions(sub.get(0).getSubscriptionId(), false, true,
+                TelephonyManager.DATA_CONNECTED, ServiceState.STATE_IN_SERVICE);
+        doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext);
+        doReturn(networkType)
+                .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(true));
+        doReturn(true).when(mWifiPickerTrackerHelper).isActiveCarrierNetwork();
+        mController.setWifiPickerTrackerHelper(mWifiPickerTrackerHelper);
 
         mController.onResume();
         mController.displayPreference(mPreferenceScreen);
@@ -450,7 +473,7 @@
         setupGetIconConditions(sub.get(0).getSubscriptionId(), false, false,
                 TelephonyManager.DATA_CONNECTED, ServiceState.STATE_IN_SERVICE);
         doReturn(networkType)
-                .when(sInjector).getNetworkType(any(), any(), any(), anyInt());
+                .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false));
 
         mController.onResume();
         mController.displayPreference(mPreferenceScreen);
@@ -470,7 +493,7 @@
         setupGetIconConditions(sub.get(0).getSubscriptionId(), false, true,
                 TelephonyManager.DATA_CONNECTED, ServiceState.STATE_IN_SERVICE);
         doReturn(networkType)
-                .when(sInjector).getNetworkType(any(), any(), any(), anyInt());
+                .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false));
         when(mTelephonyManager.isDataEnabled()).thenReturn(true);
 
         mController.onResume();
@@ -507,7 +530,7 @@
                 TelephonyManager.DATA_CONNECTED, ServiceState.STATE_IN_SERVICE);
         doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext);
         doReturn(networkType)
-                .when(sInjector).getNetworkType(any(), any(), any(), anyInt());
+                .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false));
         when(mTelephonyManager.isDataEnabled()).thenReturn(true);
 
         mController.onResume();
@@ -533,7 +556,7 @@
                 TelephonyManager.DATA_CONNECTED, ServiceState.STATE_IN_SERVICE);
         doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext);
         doReturn(networkType)
-                .when(sInjector).getNetworkType(any(), any(), any(), anyInt());
+                .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false));
 
         mController.onResume();
         mController.displayPreference(mPreferenceScreen);
@@ -560,7 +583,7 @@
                 TelephonyManager.DATA_DISCONNECTED, ServiceState.STATE_OUT_OF_SERVICE);
         doReturn(mock(MobileMappings.Config.class)).when(sInjector).getConfig(mContext);
         doReturn(networkType)
-                .when(sInjector).getNetworkType(any(), any(), any(), anyInt());
+                .when(sInjector).getNetworkType(any(), any(), any(), anyInt(), eq(false));
 
         mController.onResume();
         mController.displayPreference(mPreferenceScreen);
@@ -595,7 +618,7 @@
 
         mController.onResume();
         mController.displayPreference(mPreferenceScreen);
-        mController.mDataSubscriptionChangedReceiver.onReceive(mContext, intent);
+        mController.mConnectionChangeReceiver.onReceive(mContext, intent);
 
         assertThat(mController.isAvailable()).isTrue();
         assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(1);
@@ -621,7 +644,7 @@
 
         doReturn(sub.get(1)).when(mSubscriptionManager).getDefaultDataSubscriptionInfo();
 
-        mController.mDataSubscriptionChangedReceiver.onReceive(mContext, intent);
+        mController.mConnectionChangeReceiver.onReceive(mContext, intent);
 
         assertThat(mController.isAvailable()).isTrue();
         assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(1);