From 97291ac0ad1f39439a270874e0ddc205da0d98e7 Mon Sep 17 00:00:00 2001 From: Zoey Chen Date: Tue, 25 Oct 2022 03:21:30 +0000 Subject: [PATCH] [Settings] Do not creat multiple database Bug: 254405469 Test: manual Change-Id: I96ea77899270439a87569bd1676d38b52dd36650 --- .../network/InternetPreferenceController.java | 37 ++++-- .../network/MobileNetworkRepository.java | 15 ++- .../MobileNetworkSummaryController.java | 3 +- .../NetworkProviderCallsSmsController.java | 3 +- ...rkProviderDownloadedSimListController.java | 2 +- .../NetworkProviderSimListController.java | 2 +- .../MobileNetworkSummaryControllerTest.java | 2 +- .../InternetPreferenceControllerTest.java | 108 ++++++++++++++---- 8 files changed, 128 insertions(+), 44 deletions(-) diff --git a/src/com/android/settings/network/InternetPreferenceController.java b/src/com/android/settings/network/InternetPreferenceController.java index e5a86906eaa..8589807b383 100644 --- a/src/com/android/settings/network/InternetPreferenceController.java +++ b/src/com/android/settings/network/InternetPreferenceController.java @@ -101,7 +101,7 @@ public class InternetPreferenceController extends AbstractPreferenceController i 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 @@ public class InternetPreferenceController extends AbstractPreferenceController i /** @OnLifecycleEvent(ON_PAUSE) */ @OnLifecycleEvent(ON_PAUSE) public void onPause() { - mMobileNetworkRepository.removeRegister(); mSummaryHelper.register(false); } @@ -203,22 +202,38 @@ public class InternetPreferenceController extends AbstractPreferenceController i @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 getSubscriptionInfoList() { + return mSubInfoEntityList; + } + @Override public void onAvailableSubInfoChanged(List subInfoEntityList) { if ((mSubInfoEntityList != null && diff --git a/src/com/android/settings/network/MobileNetworkRepository.java b/src/com/android/settings/network/MobileNetworkRepository.java index 3b7a7ba9d69..61ad25db94d 100644 --- a/src/com/android/settings/network/MobileNetworkRepository.java +++ b/src/com/android/settings/network/MobileNetworkRepository.java @@ -93,10 +93,15 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions 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 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions return mMobileNetworkInfoEntityList; } + public SubscriptionInfoEntity getSubInfoById(String subId) { + return mSubscriptionInfoDao.querySubInfoById(subId); + } + public int getSubInfosCount() { return mSubscriptionInfoDao.count(); } @@ -439,7 +448,7 @@ public class MobileNetworkRepository extends SubscriptionManager.OnSubscriptions * 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 subInfoEntityList); void onActiveSubInfoChanged(List subInfoEntityList); diff --git a/src/com/android/settings/network/MobileNetworkSummaryController.java b/src/com/android/settings/network/MobileNetworkSummaryController.java index 361a200d15d..ab7498897aa 100644 --- a/src/com/android/settings/network/MobileNetworkSummaryController.java +++ b/src/com/android/settings/network/MobileNetworkSummaryController.java @@ -87,7 +87,7 @@ public class MobileNetworkSummaryController extends AbstractPreferenceController 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 @@ public class MobileNetworkSummaryController extends AbstractPreferenceController @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 cbc7f62ce04..4abd2a271df 100644 --- a/src/com/android/settings/network/NetworkProviderCallsSmsController.java +++ b/src/com/android/settings/network/NetworkProviderCallsSmsController.java @@ -71,7 +71,7 @@ public class NetworkProviderCallsSmsController extends AbstractPreferenceControl 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 @@ public class NetworkProviderCallsSmsController extends AbstractPreferenceControl @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 92a1e7ca737..fb861d896c6 100644 --- a/src/com/android/settings/network/NetworkProviderDownloadedSimListController.java +++ b/src/com/android/settings/network/NetworkProviderDownloadedSimListController.java @@ -72,7 +72,7 @@ public class NetworkProviderDownloadedSimListController extends 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 9bf21012eb3..e4ea3928086 100644 --- a/src/com/android/settings/network/NetworkProviderSimListController.java +++ b/src/com/android/settings/network/NetworkProviderSimListController.java @@ -68,7 +68,7 @@ public class NetworkProviderSimListController extends AbstractPreferenceControll 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 a6c4f679986..b5735efc2f5 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 @@ public class MobileNetworkSummaryControllerTest { 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 65edb3e66c3..8beeffb96e8 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.Handler; 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.Mock; 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 mSubscriptionInfoEntityList = new ArrayList<>(); @Before public void setUp() { @@ -85,13 +111,15 @@ public class InternetPreferenceControllerTest { 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 @@ public class InternetPreferenceControllerTest { 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 mSubscriptionInfoEntity; + + @Override + protected List getSubscriptionInfoList() { + return mSubscriptionInfoEntity; + } + + public void setSubscriptionInfoList(List 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 @@ public class InternetPreferenceControllerTest { } @Test + @UiThreadTest public void onPause_shouldUnregisterCallback() { mController.onResume(); mController.onPause(); @@ -149,33 +211,33 @@ public class InternetPreferenceControllerTest { @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); }