Merge "[Settings] Change the way in InternetPreferenceController for getting the subscription info from room db part4"
diff --git a/src/com/android/settings/network/InternetPreferenceController.java b/src/com/android/settings/network/InternetPreferenceController.java
index 48cc884..e5a8690 100644
--- a/src/com/android/settings/network/InternetPreferenceController.java
+++ b/src/com/android/settings/network/InternetPreferenceController.java
@@ -29,11 +29,13 @@
 import android.graphics.drawable.Drawable;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
+import android.util.Log;
 
 import androidx.annotation.IdRes;
 import androidx.annotation.VisibleForTesting;
 import androidx.lifecycle.Lifecycle;
 import androidx.lifecycle.LifecycleObserver;
+import androidx.lifecycle.LifecycleOwner;
 import androidx.lifecycle.OnLifecycleEvent;
 import androidx.preference.Preference;
 import androidx.preference.PreferenceScreen;
@@ -43,9 +45,14 @@
 import com.android.settings.wifi.WifiSummaryUpdater;
 import com.android.settingslib.Utils;
 import com.android.settingslib.core.AbstractPreferenceController;
+import com.android.settingslib.mobile.dataservice.MobileNetworkInfoEntity;
+import com.android.settingslib.mobile.dataservice.SubscriptionInfoEntity;
+import com.android.settingslib.mobile.dataservice.UiccInfoEntity;
 import com.android.settingslib.utils.ThreadUtils;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -53,7 +60,7 @@
  */
 public class InternetPreferenceController extends AbstractPreferenceController implements
         LifecycleObserver, SummaryUpdater.OnSummaryChangeListener,
-        InternetUpdater.InternetChangeListener {
+        InternetUpdater.InternetChangeListener, MobileNetworkRepository.MobileNetworkCallback  {
 
     public static final String KEY = "internet_settings";
 
@@ -61,6 +68,9 @@
     private final WifiSummaryUpdater mSummaryHelper;
     private InternetUpdater mInternetUpdater;
     private @InternetUpdater.InternetType int mInternetType;
+    private LifecycleOwner mLifecycleOwner;
+    private MobileNetworkRepository mMobileNetworkRepository;
+    private List<SubscriptionInfoEntity> mSubInfoEntityList = new ArrayList<>();
 
     @VisibleForTesting
     static Map<Integer, Integer> sIconMap = new HashMap<>();
@@ -81,7 +91,8 @@
         sSummaryMap.put(INTERNET_ETHERNET, R.string.to_switch_networks_disconnect_ethernet);
     }
 
-    public InternetPreferenceController(Context context, Lifecycle lifecycle) {
+    public InternetPreferenceController(Context context, Lifecycle lifecycle,
+            LifecycleOwner lifecycleOwner) {
         super(context);
         if (lifecycle == null) {
             throw new IllegalArgumentException("Lifecycle must be set");
@@ -89,6 +100,8 @@
         mSummaryHelper = new WifiSummaryUpdater(mContext, this);
         mInternetUpdater = new InternetUpdater(context, lifecycle, this);
         mInternetType = mInternetUpdater.getInternetType();
+        mLifecycleOwner = lifecycleOwner;
+        mMobileNetworkRepository = new MobileNetworkRepository(context, this);
         lifecycle.addObserver(this);
     }
 
@@ -143,12 +156,14 @@
     /** @OnLifecycleEvent(ON_RESUME) */
     @OnLifecycleEvent(ON_RESUME)
     public void onResume() {
+        mMobileNetworkRepository.addRegister(mLifecycleOwner);
         mSummaryHelper.register(true);
     }
 
     /** @OnLifecycleEvent(ON_PAUSE) */
     @OnLifecycleEvent(ON_PAUSE)
     public void onPause() {
+        mMobileNetworkRepository.removeRegister();
         mSummaryHelper.register(false);
     }
 
@@ -187,27 +202,43 @@
 
     @VisibleForTesting
     void updateCellularSummary() {
-        final SubscriptionManager subscriptionManager =
-                mContext.getSystemService(SubscriptionManager.class);
-        if (subscriptionManager == null) {
-            return;
+        CharSequence summary = null;
+        for (SubscriptionInfoEntity subInfo : mSubInfoEntityList) {
+            if (subInfo.isSubscriptionVisible && subInfo.isActiveDataSubscriptionId) {
+                summary = subInfo.uniqueName;
+                break;
+            } else if (subInfo.isDefaultDataSubscription) {
+                summary = mContext.getString(
+                        R.string.mobile_data_temp_using, subInfo.uniqueName);
+            }
         }
-        SubscriptionInfo subInfo = subscriptionManager.getActiveSubscriptionInfo(
-                SubscriptionManager.getActiveDataSubscriptionId());
-        SubscriptionInfo defaultSubInfo = subscriptionManager.getDefaultDataSubscriptionInfo();
-        subInfo = subscriptionManager.isSubscriptionVisible(subInfo) ? subInfo : defaultSubInfo;
-        if (subInfo == null) {
+
+        if (summary == null) {
             return;
         }
-        CharSequence summary;
-        if (subInfo.equals(defaultSubInfo)) {
-            // DDS is active
-            summary = SubscriptionUtil.getUniqueSubscriptionDisplayName(subInfo, mContext);
-        } else {
-            summary = mContext.getString(
-                    R.string.mobile_data_temp_using,
-                    SubscriptionUtil.getUniqueSubscriptionDisplayName(subInfo, mContext));
-        }
         mPreference.setSummary(summary);
     }
+
+    @Override
+    public void onAvailableSubInfoChanged(List<SubscriptionInfoEntity> subInfoEntityList) {
+        if ((mSubInfoEntityList != null &&
+                (subInfoEntityList.isEmpty() || !subInfoEntityList.equals(mSubInfoEntityList)))
+                || (!subInfoEntityList.isEmpty() && mSubInfoEntityList == null)) {
+            mSubInfoEntityList = subInfoEntityList;
+            updateState(mPreference);
+        }
+    }
+
+    @Override
+    public void onActiveSubInfoChanged(List<SubscriptionInfoEntity> activeSubInfoList) {
+    }
+
+    @Override
+    public void onAllUiccInfoChanged(List<UiccInfoEntity> uiccInfoEntityList) {
+    }
+
+    @Override
+    public void onAllMobileNetworkInfoChanged(
+            List<MobileNetworkInfoEntity> mobileNetworkInfoEntityList) {
+    }
 }
diff --git a/src/com/android/settings/network/MobileNetworkRepository.java b/src/com/android/settings/network/MobileNetworkRepository.java
index 2e8ef0e..3b7a7ba 100644
--- a/src/com/android/settings/network/MobileNetworkRepository.java
+++ b/src/com/android/settings/network/MobileNetworkRepository.java
@@ -348,7 +348,8 @@
                     mSubscriptionManager.getDefaultVoiceSubscriptionId() == mSubId,
                     mSubscriptionManager.getDefaultSmsSubscriptionId() == mSubId,
                     mSubscriptionManager.getDefaultDataSubscriptionId() == mSubId,
-                    mSubscriptionManager.getDefaultSubscriptionId() == mSubId);
+                    mSubscriptionManager.getDefaultSubscriptionId() == mSubId,
+                    mSubscriptionManager.getActiveDataSubscriptionId() == mSubId);
         }
     }
 
diff --git a/src/com/android/settings/network/NetworkDashboardFragment.java b/src/com/android/settings/network/NetworkDashboardFragment.java
index a94a1bf..39cf136 100644
--- a/src/com/android/settings/network/NetworkDashboardFragment.java
+++ b/src/com/android/settings/network/NetworkDashboardFragment.java
@@ -97,7 +97,7 @@
         final MobilePlanPreferenceController mobilePlanPreferenceController =
                 new MobilePlanPreferenceController(context, mobilePlanHost);
         final InternetPreferenceController internetPreferenceController =
-                new InternetPreferenceController(context, lifecycle);
+                new InternetPreferenceController(context, lifecycle, lifecycleOwner);
 
         final VpnPreferenceController vpnPreferenceController =
                 new VpnPreferenceController(context);
diff --git a/tests/unit/src/com/android/settings/network/InternetPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/InternetPreferenceControllerTest.java
index 6d9e49d..65edb3e 100644
--- a/tests/unit/src/com/android/settings/network/InternetPreferenceControllerTest.java
+++ b/tests/unit/src/com/android/settings/network/InternetPreferenceControllerTest.java
@@ -42,6 +42,7 @@
 import android.telephony.SubscriptionManager;
 
 import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleOwner;
 import androidx.preference.Preference;
 import androidx.preference.PreferenceManager;
 import androidx.preference.PreferenceScreen;
@@ -73,6 +74,7 @@
     private InternetPreferenceController mController;
     private PreferenceScreen mScreen;
     private Preference mPreference;
+    private LifecycleOwner mLifecycleOwner;
 
     @Before
     public void setUp() {
@@ -84,7 +86,8 @@
         when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(wifiManager);
         when(wifiManager.getWifiState()).thenReturn(WifiManager.WIFI_STATE_DISABLED);
 
-        mController = new InternetPreferenceController(mContext, mock(Lifecycle.class));
+        mController = new InternetPreferenceController(mContext, mock(Lifecycle.class),
+                mLifecycleOwner);
         mController.sIconMap.put(INTERNET_WIFI, 0);
         if (Looper.myLooper() == null) {
             Looper.prepare();
diff --git a/tests/unit/src/com/android/settings/network/NetworkProviderCallsSmsControllerTest.java b/tests/unit/src/com/android/settings/network/NetworkProviderCallsSmsControllerTest.java
index 88cf775..007251a 100644
--- a/tests/unit/src/com/android/settings/network/NetworkProviderCallsSmsControllerTest.java
+++ b/tests/unit/src/com/android/settings/network/NetworkProviderCallsSmsControllerTest.java
@@ -170,8 +170,8 @@
                 TelephonyManager.DEFAULT_PORT_INDEX, false, null,
                 SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM, displayName, false,
                 "1234567890", true, "default", false, isValid,
-                true, isActive, isAvailable, isDefaultCall,
-                isDefaultSms, false, false);
+                true, isActive, isAvailable, isDefaultCall, isDefaultSms, false, false,
+                false);
     }
 
     @Test
diff --git a/tests/unit/src/com/android/settings/network/NetworkProviderDownloadedSimListControllerTest.java b/tests/unit/src/com/android/settings/network/NetworkProviderDownloadedSimListControllerTest.java
index dd61b56..7dbef05 100644
--- a/tests/unit/src/com/android/settings/network/NetworkProviderDownloadedSimListControllerTest.java
+++ b/tests/unit/src/com/android/settings/network/NetworkProviderDownloadedSimListControllerTest.java
@@ -150,7 +150,7 @@
                 mcc, mnc, countryIso, true, cardId, TelephonyManager.DEFAULT_PORT_INDEX, false,
                 null, SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM, displayName, false,
                 "1234567890", true, defaultSimConfig.toString(), false, isValid, true, isActive,
-                isAvailable, isDefaultCall, isDefaultSms, isDefaultData, false);
+                isAvailable, isDefaultCall, isDefaultSms, isDefaultData, false, false);
     }
 
     private String setSummaryResId(String resName) {
diff --git a/tests/unit/src/com/android/settings/network/NetworkProviderSimListControllerTest.java b/tests/unit/src/com/android/settings/network/NetworkProviderSimListControllerTest.java
index 16f8afd..16995d5 100644
--- a/tests/unit/src/com/android/settings/network/NetworkProviderSimListControllerTest.java
+++ b/tests/unit/src/com/android/settings/network/NetworkProviderSimListControllerTest.java
@@ -156,8 +156,7 @@
                 mcc, mnc, countryIso, false, cardId, TelephonyManager.DEFAULT_PORT_INDEX, false,
                 null, SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM, displayName, false,
                 "1234567890", true, defaultSimConfig.toString(), false, isValid, true, isActive,
-                isAvailable,
-                isDefaultCall, isDefaultSms, false, false);
+                isAvailable, isDefaultCall, isDefaultSms, false, false, false);
     }
 
     private String setSummaryResId(String resName, String value) {