Merge "Add an on/off switch to the top of mobile network details page"
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 8c6b9db..ae0b8f0 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -10473,6 +10473,12 @@
     <!-- Label on the confirmation button of a dialog that lets a user set the display name of a
          mobile network subscription [CHAR LIMIT=20] -->
     <string name="mobile_network_sim_name_rename">Rename</string>
+    <!-- Label for the on position of a switch on the mobile network details page which allows
+         disabling/enabling a SIM. The SIM is enabled in this state. [CHAR LIMIT=40] -->
+    <string name="mobile_network_use_sim_on">Use SIM</string>
+    <!-- Label for the off position of a switch on the mobile network details page which allows
+         disabling/enabling a SIM. The SIM is disabled in this state. [CHAR LIMIT=40] -->
+    <string name="mobile_network_use_sim_off">Off</string>
 
     <!-- Title for preferred network type [CHAR LIMIT=NONE] -->
     <string name="preferred_network_mode_title">Preferred network type</string>
diff --git a/res/xml/mobile_network_settings_v2.xml b/res/xml/mobile_network_settings_v2.xml
index 6273ad9..f269fad 100644
--- a/res/xml/mobile_network_settings_v2.xml
+++ b/res/xml/mobile_network_settings_v2.xml
@@ -17,7 +17,12 @@
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:settings="http://schemas.android.com/apk/res-auto"
     android:key="mobile_network_pref_screen"
-    settings:initialExpandedChildrenCount="7">
+    settings:initialExpandedChildrenCount="8">
+
+    <com.android.settingslib.widget.LayoutPreference
+        android:key="use_sim_switch"
+        android:layout="@layout/styled_switch_bar"
+        settings:controller="com.android.settings.network.telephony.MobileNetworkSwitchController"/>
 
     <com.android.settings.datausage.DataUsageSummaryPreference
         android:key="status_header"
diff --git a/src/com/android/settings/network/telephony/MobileNetworkSettings.java b/src/com/android/settings/network/telephony/MobileNetworkSettings.java
index a4156f5..e05f185 100644
--- a/src/com/android/settings/network/telephony/MobileNetworkSettings.java
+++ b/src/com/android/settings/network/telephony/MobileNetworkSettings.java
@@ -132,6 +132,7 @@
         if (FeatureFlagPersistent.isEnabled(getContext(), FeatureFlags.NETWORK_INTERNET_V2)) {
           use(CallsDefaultSubscriptionController.class).init(getLifecycle());
           use(SmsDefaultSubscriptionController.class).init(getLifecycle());
+          use(MobileNetworkSwitchController.class).init(getLifecycle(), mSubId);
         }
         use(MobileDataPreferenceController.class).init(getFragmentManager(), mSubId);
         use(RoamingPreferenceController.class).init(getFragmentManager(), mSubId);
diff --git a/src/com/android/settings/network/telephony/MobileNetworkSwitchController.java b/src/com/android/settings/network/telephony/MobileNetworkSwitchController.java
new file mode 100644
index 0000000..60170d3
--- /dev/null
+++ b/src/com/android/settings/network/telephony/MobileNetworkSwitchController.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.network.telephony;
+
+import static androidx.lifecycle.Lifecycle.Event.ON_PAUSE;
+import static androidx.lifecycle.Lifecycle.Event.ON_RESUME;
+
+import android.content.Context;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+
+import com.android.settings.R;
+import com.android.settings.core.BasePreferenceController;
+import com.android.settings.core.FeatureFlags;
+import com.android.settings.development.featureflags.FeatureFlagPersistent;
+import com.android.settings.network.SubscriptionUtil;
+import com.android.settings.network.SubscriptionsChangeListener;
+import com.android.settings.widget.SwitchBar;
+import com.android.settingslib.widget.LayoutPreference;
+
+import java.util.List;
+
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleObserver;
+import androidx.lifecycle.OnLifecycleEvent;
+import androidx.preference.PreferenceScreen;
+
+/** This controls a switch to allow enabling/disabling a mobile network */
+public class MobileNetworkSwitchController extends BasePreferenceController implements
+        SubscriptionsChangeListener.SubscriptionsChangeListenerClient, LifecycleObserver {
+    private static final String TAG = "MobileNetworkSwitchCtrl";
+    private SwitchBar mSwitchBar;
+    private int mSubId;
+    private SubscriptionsChangeListener mChangeListener;
+    private SubscriptionManager mSubscriptionManager;
+
+    public MobileNetworkSwitchController(Context context, String preferenceKey) {
+        super(context, preferenceKey);
+        mSubId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+        mSubscriptionManager = mContext.getSystemService(SubscriptionManager.class);
+        mChangeListener = new SubscriptionsChangeListener(context, this);
+    }
+
+    public void init(Lifecycle lifecycle, int subId) {
+        lifecycle.addObserver(this);
+        mSubId = subId;
+    }
+
+    @OnLifecycleEvent(ON_RESUME)
+    public void onResume() {
+        mChangeListener.start();
+        update();
+    }
+
+    @OnLifecycleEvent(ON_PAUSE)
+    public void onPause() {
+        mChangeListener.stop();
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        final LayoutPreference pref = screen.findPreference(mPreferenceKey);
+        mSwitchBar = pref.findViewById(R.id.switch_bar);
+        mSwitchBar.setSwitchBarText(R.string.mobile_network_use_sim_on,
+                R.string.mobile_network_use_sim_off);
+
+        mSwitchBar.addOnSwitchChangeListener((switchView, isChecked) -> {
+            if (mSubscriptionManager.isSubscriptionEnabled(mSubId) != isChecked) {
+                mSubscriptionManager.setSubscriptionEnabled(mSubId, isChecked);
+            }
+        });
+        update();
+    }
+
+    private void update() {
+        if (mSwitchBar == null) {
+            return;
+        }
+        final List<SubscriptionInfo> subs = SubscriptionUtil.getAvailableSubscriptions(
+                mSubscriptionManager);
+        if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID || subs.size() < 2) {
+            mSwitchBar.hide();
+            return;
+        }
+
+        for (SubscriptionInfo info : subs) {
+            if (info.getSubscriptionId() == mSubId) {
+                mSwitchBar.show();
+                mSwitchBar.setChecked(mSubscriptionManager.isSubscriptionEnabled(mSubId));
+                return;
+            }
+        }
+        // This subscription was not found in the available list.
+        mSwitchBar.hide();
+    }
+
+    @Override
+    public int getAvailabilityStatus() {
+        if (FeatureFlagPersistent.isEnabled(mContext, FeatureFlags.NETWORK_INTERNET_V2)) {
+            return AVAILABLE_UNSEARCHABLE;
+        } else {
+            return CONDITIONALLY_UNAVAILABLE;
+        }
+    }
+
+    @Override
+    public void onAirplaneModeChanged(boolean airplaneModeEnabled) {}
+
+    @Override
+    public void onSubscriptionsChanged() {
+        update();
+    }
+}
diff --git a/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkSwitchControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkSwitchControllerTest.java
new file mode 100644
index 0000000..2a596d9
--- /dev/null
+++ b/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkSwitchControllerTest.java
@@ -0,0 +1,140 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings.network.telephony;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import android.content.Context;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+
+import com.android.settings.R;
+import com.android.settings.network.SubscriptionUtil;
+import com.android.settings.widget.SwitchBar;
+import com.android.settingslib.core.lifecycle.Lifecycle;
+import com.android.settingslib.widget.LayoutPreference;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+import java.util.Arrays;
+
+import androidx.lifecycle.LifecycleOwner;
+import androidx.preference.PreferenceScreen;
+
+@RunWith(RobolectricTestRunner.class)
+public class MobileNetworkSwitchControllerTest {
+    @Mock
+    private SubscriptionManager mSubscriptionManager;
+    @Mock
+    private PreferenceScreen mScreen;
+    @Mock
+    private LayoutPreference mLayoutPreference;
+    @Mock
+    private SubscriptionInfo mSubscription;
+
+    private Context mContext;
+    private LifecycleOwner mLifecycleOwner;
+    private Lifecycle mLifecycle;
+    private MobileNetworkSwitchController mController;
+    private SwitchBar mSwitchBar;
+    private int mSubId = 123;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        mContext = spy(RuntimeEnvironment.application);
+        when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
+
+        mLifecycleOwner = () -> mLifecycle;
+        mLifecycle = new Lifecycle(mLifecycleOwner);
+
+        when(mSubscription.getSubscriptionId()).thenReturn(mSubId);
+        // Most tests want to have 2 available subscriptions so that the switch bar will show.
+        SubscriptionInfo sub2 = mock(SubscriptionInfo.class);
+        when(sub2.getSubscriptionId()).thenReturn(456);
+        SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mSubscription, sub2));
+
+        String key = "prefKey";
+        mController = new MobileNetworkSwitchController(mContext, key);
+        mController.init(mLifecycle, mSubscription.getSubscriptionId());
+
+        mSwitchBar = new SwitchBar(mContext);
+        when(mScreen.findPreference(key)).thenReturn(mLayoutPreference);
+        when(mLayoutPreference.findViewById(R.id.switch_bar)).thenReturn(mSwitchBar);
+    }
+
+    @After
+    public void cleanUp() {
+        SubscriptionUtil.setAvailableSubscriptionsForTesting(null);
+    }
+
+    @Test
+    public void displayPreference_onlyOneSubscription_switchBarHidden() {
+        SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mSubscription));
+        mController.displayPreference(mScreen);
+        assertThat(mSwitchBar.isShowing()).isFalse();
+    }
+
+    @Test
+    public void displayPreference_subscriptionEnabled_switchIsOn() {
+        when(mSubscriptionManager.isSubscriptionEnabled(mSubId)).thenReturn(true);
+        mController.displayPreference(mScreen);
+        assertThat(mSwitchBar.isShowing()).isTrue();
+        assertThat(mSwitchBar.isChecked()).isTrue();
+    }
+
+    @Test
+    public void displayPreference_subscriptionDisabled_switchIsOff() {
+        when(mSubscriptionManager.isSubscriptionEnabled(mSubId)).thenReturn(false);
+        mController.displayPreference(mScreen);
+        assertThat(mSwitchBar.isShowing()).isTrue();
+        assertThat(mSwitchBar.isChecked()).isFalse();
+    }
+
+    @Test
+    public void switchChangeListener_fromEnabledToDisabled_setSubscriptionEnabledCalledCorrectly() {
+        when(mSubscriptionManager.isSubscriptionEnabled(mSubId)).thenReturn(true);
+        mController.displayPreference(mScreen);
+        assertThat(mSwitchBar.isShowing()).isTrue();
+        assertThat(mSwitchBar.isChecked()).isTrue();
+        mSwitchBar.setChecked(false);
+        verify(mSubscriptionManager).setSubscriptionEnabled(eq(mSubId), eq(false));
+    }
+
+    @Test
+    public void switchChangeListener_fromDisabledToEnabled_setSubscriptionEnabledCalledCorrectly() {
+        when(mSubscriptionManager.isSubscriptionEnabled(mSubId)).thenReturn(false);
+        mController.displayPreference(mScreen);
+        assertThat(mSwitchBar.isShowing()).isTrue();
+        assertThat(mSwitchBar.isChecked()).isFalse();
+        mSwitchBar.setChecked(true);
+        verify(mSubscriptionManager).setSubscriptionEnabled(eq(mSubId), eq(true));
+    }
+}