Merge "Fix mobile data on/off switch for multi-sim case"
diff --git a/src/com/android/settings/network/telephony/MobileDataPreferenceController.java b/src/com/android/settings/network/telephony/MobileDataPreferenceController.java
index 9ebeda0..47822b6 100644
--- a/src/com/android/settings/network/telephony/MobileDataPreferenceController.java
+++ b/src/com/android/settings/network/telephony/MobileDataPreferenceController.java
@@ -118,7 +118,8 @@
 
     @Override
     public boolean isChecked() {
-        return mTelephonyManager.isDataEnabled();
+        return mTelephonyManager.isDataEnabled()
+                && mSubId == SubscriptionManager.getDefaultDataSubscriptionId();
     }
 
     public void init(FragmentManager fragmentManager, int subId) {
@@ -129,7 +130,7 @@
 
     @VisibleForTesting
     boolean isDialogNeeded() {
-        final boolean enableData = !mTelephonyManager.isDataEnabled();
+        final boolean enableData = !isChecked();
         final boolean isMultiSim = (mTelephonyManager.getSimCount() > 1);
         final int defaultSubId = mSubscriptionManager.getDefaultDataSubscriptionId();
         final boolean needToDisableOthers = mSubscriptionManager
diff --git a/tests/robotests/src/com/android/settings/network/telephony/MobileDataPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/MobileDataPreferenceControllerTest.java
index e0bfce4..4c242c1 100644
--- a/tests/robotests/src/com/android/settings/network/telephony/MobileDataPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/network/telephony/MobileDataPreferenceControllerTest.java
@@ -20,7 +20,6 @@
 
 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.never;
 import static org.mockito.Mockito.spy;
@@ -31,10 +30,6 @@
 import android.telephony.SubscriptionManager;
 import android.telephony.TelephonyManager;
 
-import androidx.fragment.app.FragmentManager;
-import androidx.fragment.app.FragmentTransaction;
-import androidx.preference.SwitchPreference;
-
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -42,10 +37,18 @@
 import org.mockito.MockitoAnnotations;
 import org.robolectric.RobolectricTestRunner;
 import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowSubscriptionManager;
+
+import androidx.fragment.app.FragmentManager;
+import androidx.fragment.app.FragmentTransaction;
+import androidx.preference.SwitchPreference;
 
 @RunWith(RobolectricTestRunner.class)
+@Config(shadows = ShadowSubscriptionManager.class)
 public class MobileDataPreferenceControllerTest {
     private static final int SUB_ID = 2;
+    private static final int SUB_ID_OTHER = 3;
 
     @Mock
     private FragmentManager mFragmentManager;
@@ -78,6 +81,7 @@
 
         mPreference = new SwitchPreference(mContext);
         mController = new MobileDataPreferenceController(mContext, "mobile_data");
+        ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID);
         mController.init(mFragmentManager, SUB_ID);
         mPreference.setKey(mController.getPreferenceKey());
     }
@@ -104,7 +108,8 @@
     public void isDialogNeeded_enableNonDefaultSimInMultiSimMode_returnTrue() {
         doReturn(false).when(mTelephonyManager).isDataEnabled();
         doReturn(mSubscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(SUB_ID);
-        doReturn(true).when(mSubscriptionManager).isActiveSubscriptionId(anyInt());
+        doReturn(true).when(mSubscriptionManager).isActiveSubscriptionId(SUB_ID_OTHER);
+        ShadowSubscriptionManager.setDefaultDataSubscriptionId(SUB_ID_OTHER);
         doReturn(2).when(mTelephonyManager).getSimCount();
 
         assertThat(mController.isDialogNeeded()).isTrue();