Merge "[Settings] Do not creat multiple database"
diff --git a/src/com/android/settings/network/InternetPreferenceController.java b/src/com/android/settings/network/InternetPreferenceController.java
index e5a8690..8589807 100644
--- a/src/com/android/settings/network/InternetPreferenceController.java
+++ b/src/com/android/settings/network/InternetPreferenceController.java
@@ -101,7 +101,7 @@
         mInternetUpdater = new InternetUpdater(context, lifecycle, this);
         mInternetType = mInternetUpdater.getInternetType();
         mLifecycleOwner = lifecycleOwner;
-        mMobileNetworkRepository = new MobileNetworkRepository(context, this);
+        mMobileNetworkRepository = MobileNetworkRepository.create(context, this);
         lifecycle.addObserver(this);
     }
 
@@ -163,7 +163,6 @@
     /** @OnLifecycleEvent(ON_PAUSE) */
     @OnLifecycleEvent(ON_PAUSE)
     public void onPause() {
-        mMobileNetworkRepository.removeRegister();
         mSummaryHelper.register(false);
     }
 
@@ -203,22 +202,38 @@
     @VisibleForTesting
     void updateCellularSummary() {
         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);
+        SubscriptionInfoEntity activeSubInfo = null;
+        SubscriptionInfoEntity defaultSubInfo = null;
+
+        for (SubscriptionInfoEntity subInfo : getSubscriptionInfoList()) {
+            if (subInfo.isActiveDataSubscriptionId) {
+                activeSubInfo = subInfo;
+            }
+            if (subInfo.isDefaultDataSubscription) {
+                defaultSubInfo = subInfo;
             }
         }
-
-        if (summary == null) {
+        if (activeSubInfo == null) {
             return;
         }
+        activeSubInfo = activeSubInfo.isSubscriptionVisible ? activeSubInfo : defaultSubInfo;
+
+        if (activeSubInfo.equals(defaultSubInfo)) {
+            // DDS is active
+            summary = activeSubInfo.uniqueName;
+        } else {
+            summary = mContext.getString(
+                    R.string.mobile_data_temp_using, activeSubInfo.uniqueName);
+        }
+
         mPreference.setSummary(summary);
     }
 
+    @VisibleForTesting
+    protected List<SubscriptionInfoEntity> getSubscriptionInfoList() {
+        return mSubInfoEntityList;
+    }
+
     @Override
     public void onAvailableSubInfoChanged(List<SubscriptionInfoEntity> subInfoEntityList) {
         if ((mSubInfoEntityList != null &&
diff --git a/src/com/android/settings/network/MobileNetworkRepository.java b/src/com/android/settings/network/MobileNetworkRepository.java
index 3b7a7ba..61ad25d 100644
--- a/src/com/android/settings/network/MobileNetworkRepository.java
+++ b/src/com/android/settings/network/MobileNetworkRepository.java
@@ -93,10 +93,15 @@
     private boolean mIsRemovable = false;
     private boolean mIsActive = false;
 
-    MobileNetworkRepository(Context context, MobileNetworkCallback mobileNetworkCallback) {
+    public static MobileNetworkRepository create(Context context,
+            MobileNetworkCallback mobileNetworkCallback) {
+        return new MobileNetworkRepository(context, mobileNetworkCallback);
+    }
+
+    private MobileNetworkRepository(Context context, MobileNetworkCallback mobileNetworkCallback) {
         mContext = context;
         mCallback = mobileNetworkCallback;
-        mMobileNetworkDatabase = MobileNetworkDatabase.createDatabase(context);
+        mMobileNetworkDatabase = MobileNetworkDatabase.getInstance(context);
         mSubscriptionInfoDao = mMobileNetworkDatabase.mSubscriptionInfoDao();
         mUiccInfoDao = mMobileNetworkDatabase.mUiccInfoDao();
         mMobileNetworkInfoDao = mMobileNetworkDatabase.mMobileNetworkInfoDao();
@@ -194,6 +199,10 @@
         return mMobileNetworkInfoEntityList;
     }
 
+    public SubscriptionInfoEntity getSubInfoById(String subId) {
+        return mSubscriptionInfoDao.querySubInfoById(subId);
+    }
+
     public int getSubInfosCount() {
         return mSubscriptionInfoDao.count();
     }
@@ -439,7 +448,7 @@
      * Callback for clients to get the latest info changes if the framework or content observers.
      * updates the relevant info.
      */
-    interface MobileNetworkCallback {
+    public interface MobileNetworkCallback {
         void onAvailableSubInfoChanged(List<SubscriptionInfoEntity> subInfoEntityList);
 
         void onActiveSubInfoChanged(List<SubscriptionInfoEntity> subInfoEntityList);
diff --git a/src/com/android/settings/network/MobileNetworkSummaryController.java b/src/com/android/settings/network/MobileNetworkSummaryController.java
index 361a200..ab74988 100644
--- a/src/com/android/settings/network/MobileNetworkSummaryController.java
+++ b/src/com/android/settings/network/MobileNetworkSummaryController.java
@@ -87,7 +87,7 @@
         mMetricsFeatureProvider = FeatureFactory.getFactory(mContext).getMetricsFeatureProvider();
         mUserManager = context.getSystemService(UserManager.class);
         mLifecycleOwner = lifecycleOwner;
-        mMobileNetworkRepository = new MobileNetworkRepository(context, this);
+        mMobileNetworkRepository = MobileNetworkRepository.create(context, this);
         if (lifecycle != null) {
             lifecycle.addObserver(this);
         }
@@ -101,7 +101,6 @@
 
     @OnLifecycleEvent(ON_PAUSE)
     public void onPause() {
-        mMobileNetworkRepository.removeRegister();
     }
 
     @Override
diff --git a/src/com/android/settings/network/NetworkProviderCallsSmsController.java b/src/com/android/settings/network/NetworkProviderCallsSmsController.java
index cbc7f62..4abd2a2 100644
--- a/src/com/android/settings/network/NetworkProviderCallsSmsController.java
+++ b/src/com/android/settings/network/NetworkProviderCallsSmsController.java
@@ -71,7 +71,7 @@
         mIsRtlMode = context.getResources().getConfiguration().getLayoutDirection()
                 == View.LAYOUT_DIRECTION_RTL;
         mLifecycleOwner = lifecycleOwner;
-        mMobileNetworkRepository = new MobileNetworkRepository(context, this);
+        mMobileNetworkRepository = MobileNetworkRepository.create(context, this);
         if (lifecycle != null) {
             lifecycle.addObserver(this);
         }
@@ -85,7 +85,6 @@
 
     @OnLifecycleEvent(Event.ON_PAUSE)
     public void onPause() {
-        mMobileNetworkRepository.removeRegister();
     }
 
     @Override
diff --git a/src/com/android/settings/network/NetworkProviderDownloadedSimListController.java b/src/com/android/settings/network/NetworkProviderDownloadedSimListController.java
index 92a1e7c..fb861d8 100644
--- a/src/com/android/settings/network/NetworkProviderDownloadedSimListController.java
+++ b/src/com/android/settings/network/NetworkProviderDownloadedSimListController.java
@@ -72,7 +72,7 @@
         mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
         mPreferences = new ArrayMap<>();
         mLifecycleOwner = lifecycleOwner;
-        mMobileNetworkRepository = new MobileNetworkRepository(context, this);
+        mMobileNetworkRepository = MobileNetworkRepository.create(context, this);
         lifecycle.addObserver(this);
     }
 
diff --git a/src/com/android/settings/network/NetworkProviderSimListController.java b/src/com/android/settings/network/NetworkProviderSimListController.java
index 9bf2101..e4ea392 100644
--- a/src/com/android/settings/network/NetworkProviderSimListController.java
+++ b/src/com/android/settings/network/NetworkProviderSimListController.java
@@ -68,7 +68,7 @@
         mSubscriptionManager = context.getSystemService(SubscriptionManager.class);
         mPreferences = new ArrayMap<>();
         mLifecycleOwner = lifecycleOwner;
-        mMobileNetworkRepository = new MobileNetworkRepository(context, this);
+        mMobileNetworkRepository = MobileNetworkRepository.create(context, this);
         lifecycle.addObserver(this);
     }
 
diff --git a/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java b/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java
index a6c4f67..b5735ef 100644
--- a/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java
@@ -95,7 +95,7 @@
         doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
         doReturn(mEuiccManager).when(mContext).getSystemService(EuiccManager.class);
         doReturn(mUserManager).when(mContext).getSystemService(UserManager.class);
-        mMobileNetworkRepository = new MobileNetworkRepository(mContext, mMobileNetworkCallback);
+        mMobileNetworkRepository = MobileNetworkRepository.create(mContext, mMobileNetworkCallback);
         mLifecycleOwner = () -> mLifecycle;
         mLifecycle = new Lifecycle(mLifecycleOwner);
         mMobileNetworkRepository.addRegister(mLifecycleOwner);
diff --git a/tests/unit/src/com/android/settings/network/InternetPreferenceControllerTest.java b/tests/unit/src/com/android/settings/network/InternetPreferenceControllerTest.java
index 65edb3e..8beeffb 100644
--- a/tests/unit/src/com/android/settings/network/InternetPreferenceControllerTest.java
+++ b/tests/unit/src/com/android/settings/network/InternetPreferenceControllerTest.java
@@ -40,16 +40,20 @@
 import android.os.Looper;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
 
 import androidx.lifecycle.Lifecycle;
 import androidx.lifecycle.LifecycleOwner;
+import androidx.lifecycle.LifecycleRegistry;
 import androidx.preference.Preference;
 import androidx.preference.PreferenceManager;
 import androidx.preference.PreferenceScreen;
+import androidx.test.annotation.UiThreadTest;
 import androidx.test.core.app.ApplicationProvider;
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 
 import com.android.settings.testutils.ResourcesUtils;
+import com.android.settingslib.mobile.dataservice.SubscriptionInfoEntity;
 
 import org.junit.Before;
 import org.junit.Rule;
@@ -59,22 +63,44 @@
 import org.mockito.junit.MockitoJUnit;
 import org.mockito.junit.MockitoRule;
 
+import java.util.ArrayList;
+import java.util.List;
+
 @RunWith(AndroidJUnit4.class)
 public class InternetPreferenceControllerTest {
 
     private static final String TEST_SUMMARY = "test summary";
     private static final String NOT_CONNECTED = "Not connected";
+    private static final String SUB_ID_1 = "1";
+    private static final String SUB_ID_2 = "2";
+    private static final String INVALID_SUB_ID = "-1";
+    private static final String DISPLAY_NAME_1 = "Sub 1";
+    private static final String DISPLAY_NAME_2 = "Sub 2";
+    private static final String SUB_MCC_1 = "123";
+    private static final String SUB_MNC_1 = "456";
+    private static final String SUB_MCC_2 = "223";
+    private static final String SUB_MNC_2 = "456";
+    private static final String SUB_COUNTRY_ISO_1 = "Sub 1";
+    private static final String SUB_COUNTRY_ISO_2 = "Sub 2";
 
     @Rule
     public final MockitoRule mMockitoRule = MockitoJUnit.rule();
     @Mock
+    private SubscriptionInfoEntity mActiveSubInfo;
+    @Mock
+    private SubscriptionInfoEntity mDefaultDataSubInfo;
+    @Mock
     private ConnectivityManager mConnectivityManager;
+    @Mock
+    private LifecycleOwner mLifecycleOwner;
+
+    private LifecycleRegistry mLifecycleRegistry;
 
     private Context mContext;
-    private InternetPreferenceController mController;
+    private MockInternetPreferenceController mController;
     private PreferenceScreen mScreen;
     private Preference mPreference;
-    private LifecycleOwner mLifecycleOwner;
+    private List<SubscriptionInfoEntity> mSubscriptionInfoEntityList = new ArrayList<>();
 
     @Before
     public void setUp() {
@@ -85,13 +111,15 @@
         final WifiManager wifiManager = mock(WifiManager.class);
         when(mContext.getSystemService(Context.WIFI_SERVICE)).thenReturn(wifiManager);
         when(wifiManager.getWifiState()).thenReturn(WifiManager.WIFI_STATE_DISABLED);
-
-        mController = new InternetPreferenceController(mContext, mock(Lifecycle.class),
-                mLifecycleOwner);
-        mController.sIconMap.put(INTERNET_WIFI, 0);
         if (Looper.myLooper() == null) {
             Looper.prepare();
         }
+        mLifecycleRegistry = new LifecycleRegistry(mLifecycleOwner);
+        when(mLifecycleOwner.getLifecycle()).thenReturn(mLifecycleRegistry);
+        mController = new MockInternetPreferenceController(mContext, mock(Lifecycle.class),
+                mLifecycleOwner);
+        mController.sIconMap.put(INTERNET_WIFI, 0);
+
         final PreferenceManager preferenceManager = new PreferenceManager(mContext);
         mScreen = preferenceManager.createPreferenceScreen(mContext);
         mPreference = new Preference(mContext);
@@ -99,12 +127,45 @@
         mScreen.addPreference(mPreference);
     }
 
+    private class MockInternetPreferenceController extends
+            com.android.settings.network.InternetPreferenceController {
+        public MockInternetPreferenceController(Context context, Lifecycle lifecycle,
+                LifecycleOwner lifecycleOwner) {
+            super(context, lifecycle, lifecycleOwner);
+        }
+
+        private List<SubscriptionInfoEntity> mSubscriptionInfoEntity;
+
+        @Override
+        protected List<SubscriptionInfoEntity> getSubscriptionInfoList() {
+            return mSubscriptionInfoEntity;
+        }
+
+        public void setSubscriptionInfoList(List<SubscriptionInfoEntity> list) {
+            mSubscriptionInfoEntity = list;
+        }
+
+    }
+
+    private SubscriptionInfoEntity setupSubscriptionInfoEntity(String subId, int slotId,
+            int carrierId, String displayName, String mcc, String mnc, String countryIso,
+            int cardId, boolean isVisible, boolean isValid, boolean isActive, boolean isAvailable,
+            boolean isDefaultData, boolean isActiveData) {
+        return new SubscriptionInfoEntity(subId, slotId, carrierId,
+                displayName, displayName, 0, mcc, mnc, countryIso, false, cardId,
+                TelephonyManager.DEFAULT_PORT_INDEX, false, null,
+                SubscriptionManager.SUBSCRIPTION_TYPE_LOCAL_SIM, displayName, isVisible,
+                "1234567890", true, "default", false, isValid, true, isActive, isAvailable, false,
+                false, isDefaultData, false, isActiveData);
+    }
+
     @Test
     public void isAvailable_shouldBeTrue() {
         assertThat(mController.isAvailable()).isTrue();
     }
 
     @Test
+    @UiThreadTest
     public void onResume_shouldRegisterCallback() {
         mController.onResume();
 
@@ -117,6 +178,7 @@
     }
 
     @Test
+    @UiThreadTest
     public void onPause_shouldUnregisterCallback() {
         mController.onResume();
         mController.onPause();
@@ -149,33 +211,33 @@
 
     @Test
     public void updateCellularSummary_getNullSubscriptionInfo_shouldNotCrash() {
-        final SubscriptionManager subscriptionManager = mock(SubscriptionManager.class);
-        when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(subscriptionManager);
-        when(subscriptionManager.getDefaultDataSubscriptionInfo()).thenReturn(null);
-        when(subscriptionManager.getActiveSubscriptionInfo(anyInt())).thenReturn(null);
+        mController.setSubscriptionInfoList(mSubscriptionInfoEntityList);
 
         mController.updateCellularSummary();
     }
 
     @Test
     public void updateCellularSummary_getActiveSubscriptionInfo_cbrs() {
+        mActiveSubInfo = setupSubscriptionInfoEntity(SUB_ID_1, 1, 1, DISPLAY_NAME_1, SUB_MCC_1,
+                SUB_MNC_1, SUB_COUNTRY_ISO_1, 1, false, true, true, true, false, true);
+        mDefaultDataSubInfo = setupSubscriptionInfoEntity(SUB_ID_2, 1, 1, DISPLAY_NAME_2, SUB_MCC_2,
+                SUB_MNC_2, SUB_COUNTRY_ISO_2, 1, false, true, true, true, true, false);
+        mSubscriptionInfoEntityList.add(mActiveSubInfo);
+        mSubscriptionInfoEntityList.add(mDefaultDataSubInfo);
+        mController.setSubscriptionInfoList(mSubscriptionInfoEntityList);
         mController.displayPreference(mScreen);
-        final SubscriptionManager subscriptionManager = mock(SubscriptionManager.class);
-        final SubscriptionInfo defaultSubInfo = mock(SubscriptionInfo.class);
-        final SubscriptionInfo activeSubInfo = mock(SubscriptionInfo.class);
-        final String expectedSummary =
-                ResourcesUtils.getResourcesString(mContext, "mobile_data_temp_using", "");
-
-        when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(subscriptionManager);
-        when(subscriptionManager.getDefaultDataSubscriptionInfo()).thenReturn(defaultSubInfo);
-
-        when(subscriptionManager.getActiveSubscriptionInfo(anyInt())).thenReturn(activeSubInfo);
-        when(subscriptionManager.isSubscriptionVisible(activeSubInfo)).thenReturn(false);
 
         mController.updateCellularSummary();
-        assertThat(mPreference.getSummary()).isEqualTo("");
+        assertThat(mPreference.getSummary()).isEqualTo(DISPLAY_NAME_2);
 
-        when(subscriptionManager.isSubscriptionVisible(activeSubInfo)).thenReturn(true);
+        mActiveSubInfo = setupSubscriptionInfoEntity(SUB_ID_1, 1, 1, DISPLAY_NAME_1, SUB_MCC_1,
+                SUB_MNC_1, SUB_COUNTRY_ISO_1, 1, true, true, true, true, false, true);
+        mSubscriptionInfoEntityList.add(mActiveSubInfo);
+        mController.setSubscriptionInfoList(mSubscriptionInfoEntityList);
+        mController.onAvailableSubInfoChanged(mSubscriptionInfoEntityList);
+        final String expectedSummary =
+                ResourcesUtils.getResourcesString(mContext, "mobile_data_temp_using",
+                        DISPLAY_NAME_1);
         mController.updateCellularSummary();
         assertThat(mPreference.getSummary()).isEqualTo(expectedSummary);
     }