From 91200e610d9506e2eb69ea5766c62dfc111d721a Mon Sep 17 00:00:00 2001 From: Arc Wang Date: Mon, 12 Aug 2019 20:11:13 +0800 Subject: [PATCH] [Wi-Fi] Fix multi SIM devices switch to mobile data automatically settings missing problem Context#getResources loading is done based on the last SIM to come up, we may get wrong customization settings. This fix check the setting of active subscription and shows the preference. Bug: 138956509 Test: CellularFallbackPreferenceControllerTest manual test: 1. Insert feature supported SIM and check UI. 2. Remove feature supported SIM and check UI. 3. Turn airplane mode on and check 1. and 2.. Change-Id: I72b6db415429181395a02f163889bb1b9c0f070f Merged-In: I72b6db415429181395a02f163889bb1b9c0f070f --- .../CellularFallbackPreferenceController.java | 27 +++++++++++-- ...lularFallbackPreferenceControllerTest.java | 39 +++++++++++++++---- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/src/com/android/settings/wifi/CellularFallbackPreferenceController.java b/src/com/android/settings/wifi/CellularFallbackPreferenceController.java index cbb8fb8e26a..01677891bb1 100644 --- a/src/com/android/settings/wifi/CellularFallbackPreferenceController.java +++ b/src/com/android/settings/wifi/CellularFallbackPreferenceController.java @@ -17,8 +17,11 @@ package com.android.settings.wifi; import android.content.Context; +import android.content.res.Resources; import android.provider.Settings; +import android.telephony.SubscriptionManager; +import com.android.internal.annotations.VisibleForTesting; import com.android.settings.core.TogglePreferenceController; /** @@ -33,7 +36,7 @@ public class CellularFallbackPreferenceController extends TogglePreferenceContro @Override public int getAvailabilityStatus() { - return !avoidBadWifiConfig() ? AVAILABLE : UNSUPPORTED_ON_DEVICE; + return avoidBadWifiConfig() ? UNSUPPORTED_ON_DEVICE : AVAILABLE; } @Override @@ -49,12 +52,28 @@ public class CellularFallbackPreferenceController extends TogglePreferenceContro } private boolean avoidBadWifiConfig() { - return mContext.getResources().getInteger( - com.android.internal.R.integer.config_networkAvoidBadWifi) == 1; + final int activeDataSubscriptionId = getActiveDataSubscriptionId(); + if (activeDataSubscriptionId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { + return true; + } + + final Resources resources = getResourcesForSubId(activeDataSubscriptionId); + return resources.getInteger(com.android.internal.R.integer.config_networkAvoidBadWifi) == 1; + } + + @VisibleForTesting + int getActiveDataSubscriptionId() { + return SubscriptionManager.getActiveDataSubscriptionId(); + } + + @VisibleForTesting + Resources getResourcesForSubId(int subscriptionId) { + return SubscriptionManager.getResourcesForSubId(mContext, subscriptionId, + false /* useRootLocale */); } private boolean avoidBadWifiCurrentSettings() { return "1".equals(Settings.Global.getString(mContext.getContentResolver(), Settings.Global.NETWORK_AVOID_BAD_WIFI)); } -} \ No newline at end of file +} diff --git a/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java index c5bd555fa9a..6f3230cff4f 100644 --- a/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java @@ -18,36 +18,54 @@ package com.android.settings.wifi; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import android.content.Context; +import android.content.res.Resources; +import android.telephony.SubscriptionManager; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Answers; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; @RunWith(RobolectricTestRunner.class) public class CellularFallbackPreferenceControllerTest { private static final String KEY_CELLULAR_FALLBACK = "wifi_cellular_data_fallback"; - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private Context mContext; - private CellularFallbackPreferenceController mController; @Before public void setUp() { MockitoAnnotations.initMocks(this); - mController = new CellularFallbackPreferenceController(mContext, KEY_CELLULAR_FALLBACK); + + mController = spy(new CellularFallbackPreferenceController(RuntimeEnvironment.application, + KEY_CELLULAR_FALLBACK)); + } + + @Test + public void isAvailable_invalidActiveSubscriptionId_shouldReturnFalse() { + doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID) + .when(mController).getActiveDataSubscriptionId(); + + assertThat(mController.isAvailable()).isFalse(); } @Test public void isAvailable_avoidBadWifiConfigIsFalse_shouldReturnTrue() { - when(mContext.getResources().getInteger( + final Resources resources = mock(Resources.class); + + doReturn(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) + .when(mController).getActiveDataSubscriptionId(); + doReturn(resources).when(mController).getResourcesForSubId(anyInt()); + when(resources.getInteger( com.android.internal.R.integer.config_networkAvoidBadWifi)) .thenReturn(0); @@ -56,10 +74,15 @@ public class CellularFallbackPreferenceControllerTest { @Test public void isAvailable_avoidBadWifiConfigIsTrue_shouldReturnFalse() { - when(mContext.getResources().getInteger( + final Resources resources = mock(Resources.class); + + doReturn(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID) + .when(mController).getActiveDataSubscriptionId(); + doReturn(resources).when(mController).getResourcesForSubId(anyInt()); + when(resources.getInteger( com.android.internal.R.integer.config_networkAvoidBadWifi)) .thenReturn(1); assertThat(mController.isAvailable()).isFalse(); } -} +} \ No newline at end of file