Merge "[Wi-Fi] Fix multi SIM devices switch to mobile data automatically settings missing problem" into qt-r1-dev am: 54dbed859d
am: 0e53b6293d

Change-Id: I574c161e5cdde8f879031ff403082dddefe0dbb9
diff --git a/src/com/android/settings/wifi/CellularFallbackPreferenceController.java b/src/com/android/settings/wifi/CellularFallbackPreferenceController.java
index cbb8fb8..0167789 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 @@
 
     @Override
     public int getAvailabilityStatus() {
-        return !avoidBadWifiConfig() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
+        return avoidBadWifiConfig() ? UNSUPPORTED_ON_DEVICE : AVAILABLE;
     }
 
     @Override
@@ -49,12 +52,28 @@
     }
 
     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 c5bd555..6f3230c 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 @@
 
 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 @@
 
     @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