[Wi-Fi] Check default data SIM instead of all active subscriptions for
'Switch to mobile data automatically' setting
To align with framework design.
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: If10616c9b47fae490521479c5abc99c5df87f409
diff --git a/src/com/android/settings/wifi/CellularFallbackPreferenceController.java b/src/com/android/settings/wifi/CellularFallbackPreferenceController.java
index c411aba..659ddbe 100644
--- a/src/com/android/settings/wifi/CellularFallbackPreferenceController.java
+++ b/src/com/android/settings/wifi/CellularFallbackPreferenceController.java
@@ -19,7 +19,6 @@
import android.content.Context;
import android.content.res.Resources;
import android.provider.Settings;
-import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import com.android.internal.annotations.VisibleForTesting;
@@ -37,7 +36,7 @@
@Override
public int getAvailabilityStatus() {
- return !avoidBadWifiConfig() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+ return avoidBadWifiConfig() ? UNSUPPORTED_ON_DEVICE : AVAILABLE;
}
@Override
@@ -53,20 +52,18 @@
}
private boolean avoidBadWifiConfig() {
- boolean avoidBadWifiConfig = true;
- final SubscriptionManager subscriptionManager =
- mContext.getSystemService(SubscriptionManager.class);
-
- for (SubscriptionInfo subscriptionInfo :
- subscriptionManager.getActiveSubscriptionInfoList()) {
- final Resources resources = getResourcesForSubId(subscriptionInfo.getSubscriptionId());
- if (resources.getInteger(
- com.android.internal.R.integer.config_networkAvoidBadWifi) == 0) {
- avoidBadWifiConfig = false;
- break;
- }
+ final int activeDataSubscriptionId = getActiveDataSubscriptionId();
+ if (activeDataSubscriptionId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
+ return true;
}
- return avoidBadWifiConfig;
+
+ final Resources resources = getResourcesForSubId(activeDataSubscriptionId);
+ return resources.getInteger(com.android.internal.R.integer.config_networkAvoidBadWifi) == 1;
+ }
+
+ @VisibleForTesting
+ int getActiveDataSubscriptionId() {
+ return SubscriptionManager.getActiveDataSubscriptionId();
}
@VisibleForTesting
diff --git a/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java
index e5d5d8f..6f3230c 100644
--- a/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/wifi/CellularFallbackPreferenceControllerTest.java
@@ -26,12 +26,8 @@
import android.content.Context;
import android.content.res.Resources;
-import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
-import java.util.ArrayList;
-import java.util.List;
-
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -39,32 +35,35 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
-import org.robolectric.shadows.ShadowApplication;
@RunWith(RobolectricTestRunner.class)
public class CellularFallbackPreferenceControllerTest {
private static final String KEY_CELLULAR_FALLBACK = "wifi_cellular_data_fallback";
- @Mock
- private SubscriptionManager mSubscriptionManager;
-
private CellularFallbackPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
- ShadowApplication shadowApplication = ShadowApplication.getInstance();
- shadowApplication.setSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE,
- mSubscriptionManager);
+
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() {
- setupMockIcc();
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))
@@ -75,9 +74,10 @@
@Test
public void isAvailable_avoidBadWifiConfigIsTrue_shouldReturnFalse() {
- setupMockIcc();
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))
@@ -85,14 +85,4 @@
assertThat(mController.isAvailable()).isFalse();
}
-
- private void setupMockIcc() {
- final List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>();
- final SubscriptionInfo info1 = mock(SubscriptionInfo.class);
- final SubscriptionInfo info2 = mock(SubscriptionInfo.class);
- subscriptionInfoList.add(info1);
- subscriptionInfoList.add(info2);
- when(mSubscriptionManager.getActiveSubscriptionInfoList())
- .thenReturn(subscriptionInfoList);
- }
-}
+}
\ No newline at end of file