From e5dcd02f4cb96cf7ea818f7d46f9e25ce62d0b1a Mon Sep 17 00:00:00 2001 From: Malcolm Chen Date: Wed, 4 Dec 2019 16:44:27 -0800 Subject: [PATCH] Add disable pSIM option in Settings As we start disabling pSIM option in R, we check that if the feature is supported, add the "Use SIM" toggle back to pSIM page. Bug: 141018421 Test: manual test Change-Id: I1af497ce5ede93f823825718b8136ff8ae0ab49c --- .../network/MobileNetworkListController.java | 5 +++- .../MobileNetworkSummaryController.java | 6 ++-- .../settings/network/SubscriptionUtil.java | 7 +++++ .../DisableSimFooterPreferenceController.java | 5 ++-- .../MobileNetworkSwitchController.java | 9 +++--- .../MobileNetworkListControllerTest.java | 16 ++++++++--- .../MobileNetworkSummaryControllerTest.java | 28 +++++++++++++++++-- ...ableSimFooterPreferenceControllerTest.java | 18 ++++++++++-- .../MobileNetworkSwitchControllerTest.java | 4 +++ 9 files changed, 79 insertions(+), 19 deletions(-) diff --git a/src/com/android/settings/network/MobileNetworkListController.java b/src/com/android/settings/network/MobileNetworkListController.java index ab41fad6236..f1980b26ac9 100644 --- a/src/com/android/settings/network/MobileNetworkListController.java +++ b/src/com/android/settings/network/MobileNetworkListController.java @@ -117,6 +117,8 @@ public class MobileNetworkListController extends AbstractPreferenceController im } else { if (mSubscriptionManager.isActiveSubscriptionId(subId)) { pref.setSummary(R.string.mobile_network_active_sim); + } else if (SubscriptionUtil.showToggleForPhysicalSim(mSubscriptionManager)) { + pref.setSummary(mContext.getString(R.string.mobile_network_inactive_sim)); } else { pref.setSummary(mContext.getString(R.string.mobile_network_tap_to_activate, SubscriptionUtil.getDisplayName(info))); @@ -124,7 +126,8 @@ public class MobileNetworkListController extends AbstractPreferenceController im } pref.setOnPreferenceClickListener(clickedPref -> { - if (!info.isEmbedded() && !mSubscriptionManager.isActiveSubscriptionId(subId)) { + if (!info.isEmbedded() && !mSubscriptionManager.isActiveSubscriptionId(subId) + && !SubscriptionUtil.showToggleForPhysicalSim(mSubscriptionManager)) { mSubscriptionManager.setSubscriptionEnabled(subId, true); } else { final Intent intent = new Intent(mContext, MobileNetworkActivity.class); diff --git a/src/com/android/settings/network/MobileNetworkSummaryController.java b/src/com/android/settings/network/MobileNetworkSummaryController.java index bd3e2efbb6d..861adf17943 100644 --- a/src/com/android/settings/network/MobileNetworkSummaryController.java +++ b/src/com/android/settings/network/MobileNetworkSummaryController.java @@ -110,7 +110,8 @@ public class MobileNetworkSummaryController extends AbstractPreferenceController } else if (subs.size() == 1) { final SubscriptionInfo info = subs.get(0); final int subId = info.getSubscriptionId(); - if (!info.isEmbedded() && !mSubscriptionManager.isActiveSubscriptionId(subId)) { + if (!info.isEmbedded() && !mSubscriptionManager.isActiveSubscriptionId(subId) + && !SubscriptionUtil.showToggleForPhysicalSim(mSubscriptionManager)) { return mContext.getString(R.string.mobile_network_tap_to_activate, SubscriptionUtil.getDisplayName(info)); } else { @@ -163,7 +164,8 @@ public class MobileNetworkSummaryController extends AbstractPreferenceController mPreference.setOnPreferenceClickListener((Preference pref) -> { final SubscriptionInfo info = subs.get(0); final int subId = info.getSubscriptionId(); - if (!info.isEmbedded() && !mSubscriptionManager.isActiveSubscriptionId(subId)) { + if (!info.isEmbedded() && !mSubscriptionManager.isActiveSubscriptionId(subId) + && !SubscriptionUtil.showToggleForPhysicalSim(mSubscriptionManager)) { mSubscriptionManager.setSubscriptionEnabled(subId, true); } else { final Intent intent = new Intent(mContext, MobileNetworkActivity.class); diff --git a/src/com/android/settings/network/SubscriptionUtil.java b/src/com/android/settings/network/SubscriptionUtil.java index f7235754f6c..4e8ab89e84a 100644 --- a/src/com/android/settings/network/SubscriptionUtil.java +++ b/src/com/android/settings/network/SubscriptionUtil.java @@ -113,4 +113,11 @@ public class SubscriptionUtil { } return ""; } + + /** + * Whether Settings should show a "Use SIM" toggle in pSIM detailed page. + */ + public static boolean showToggleForPhysicalSim(SubscriptionManager subMgr) { + return subMgr.canDisablePhysicalSubscription(); + } } diff --git a/src/com/android/settings/network/telephony/DisableSimFooterPreferenceController.java b/src/com/android/settings/network/telephony/DisableSimFooterPreferenceController.java index ab01b9d5a86..f5bcce79f42 100644 --- a/src/com/android/settings/network/telephony/DisableSimFooterPreferenceController.java +++ b/src/com/android/settings/network/telephony/DisableSimFooterPreferenceController.java @@ -19,7 +19,6 @@ package com.android.settings.network.telephony; import android.content.Context; import android.telephony.SubscriptionInfo; import android.telephony.SubscriptionManager; -import android.telephony.TelephonyManager; import com.android.settings.core.BasePreferenceController; import com.android.settings.network.SubscriptionUtil; @@ -41,9 +40,11 @@ public class DisableSimFooterPreferenceController extends BasePreferenceControll if (mSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) { return CONDITIONALLY_UNAVAILABLE; } + + SubscriptionManager subManager = mContext.getSystemService(SubscriptionManager.class); for (SubscriptionInfo info : SubscriptionUtil.getAvailableSubscriptions(mContext)) { if (info.getSubscriptionId() == mSubId) { - if (info.isEmbedded()) { + if (info.isEmbedded() || SubscriptionUtil.showToggleForPhysicalSim(subManager)) { return CONDITIONALLY_UNAVAILABLE; } break; diff --git a/src/com/android/settings/network/telephony/MobileNetworkSwitchController.java b/src/com/android/settings/network/telephony/MobileNetworkSwitchController.java index 94f86f490c4..9d5c7cba833 100644 --- a/src/com/android/settings/network/telephony/MobileNetworkSwitchController.java +++ b/src/com/android/settings/network/telephony/MobileNetworkSwitchController.java @@ -99,14 +99,13 @@ public class MobileNetworkSwitchController extends BasePreferenceController impl } } - // For eSIM, we always want the toggle. The telephony stack doesn't currently support - // disabling a pSIM directly (b/133379187), so we for now we don't include this on pSIM. - if (subInfo == null || !subInfo.isEmbedded()) { + // For eSIM, we always want the toggle. If telephony stack support disabling a pSIM + // directly, we show the toggle. + if (subInfo == null || (!subInfo.isEmbedded() && !SubscriptionUtil.showToggleForPhysicalSim( + mSubscriptionManager))) { mSwitchBar.hide(); } else { mSwitchBar.show(); - // TODO b/135222940: re-evaluate whether to use - // mSubscriptionManager#isSubscriptionEnabled mSwitchBar.setChecked(mSubscriptionManager.isActiveSubId(mSubId)); } } diff --git a/tests/robotests/src/com/android/settings/network/MobileNetworkListControllerTest.java b/tests/robotests/src/com/android/settings/network/MobileNetworkListControllerTest.java index 1c71b962e3e..bbb1bdda5cd 100644 --- a/tests/robotests/src/com/android/settings/network/MobileNetworkListControllerTest.java +++ b/tests/robotests/src/com/android/settings/network/MobileNetworkListControllerTest.java @@ -22,6 +22,7 @@ import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.clearInvocations; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -30,7 +31,6 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import android.app.Activity; import android.content.Context; import android.content.Intent; import android.provider.Settings; @@ -46,7 +46,6 @@ import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; @@ -155,6 +154,7 @@ public class MobileNetworkListControllerTest { when(sub1.isEmbedded()).thenReturn(true); doReturn(true).when(mSubscriptionManager).isActiveSubscriptionId(eq(1)); doReturn(false).when(mSubscriptionManager).isActiveSubscriptionId(eq(2)); + doReturn(false).when(mSubscriptionManager).canDisablePhysicalSubscription(); when(sub2.isEmbedded()).thenReturn(false); SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1, sub2)); @@ -166,13 +166,21 @@ public class MobileNetworkListControllerTest { final ArgumentCaptor preferenceCaptor = ArgumentCaptor.forClass( Preference.class); verify(mPreferenceScreen, times(2)).addPreference(preferenceCaptor.capture()); - final Preference pref1 = preferenceCaptor.getAllValues().get(0); - final Preference pref2 = preferenceCaptor.getAllValues().get(1); + Preference pref1 = preferenceCaptor.getAllValues().get(0); + Preference pref2 = preferenceCaptor.getAllValues().get(1); assertThat(pref1.getSummary()).isEqualTo("Active / Downloaded SIM"); assertThat(pref2.getSummary()).isEqualTo("Tap to activate sub2"); pref2.getOnPreferenceClickListener().onPreferenceClick(pref2); verify(mSubscriptionManager).setSubscriptionEnabled(eq(2), eq(true)); + + // If disabling pSIM is allowed, summary of inactive pSIM should be different. + clearInvocations(mPreferenceScreen); + clearInvocations(mSubscriptionManager); + doReturn(true).when(mSubscriptionManager).canDisablePhysicalSubscription(); + mController.onResume(); + pref2 = preferenceCaptor.getAllValues().get(1); + assertThat(pref2.getSummary()).isEqualTo("Inactive / SIM"); } @Test diff --git a/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java b/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java index 4f4a549b1f2..5913f7bf81e 100644 --- a/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java +++ b/tests/robotests/src/com/android/settings/network/MobileNetworkSummaryControllerTest.java @@ -29,7 +29,6 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; @@ -52,7 +51,6 @@ import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.robolectric.Robolectric; import org.robolectric.RobolectricTestRunner; import org.robolectric.RuntimeEnvironment; @@ -168,7 +166,7 @@ public class MobileNetworkSummaryControllerTest { } @Test - public void getSummary_oneInactivePSim_correctSummaryAndClickHandler() { + public void getSummary_oneInactivePSim_cannotDisablePsim_correctSummaryAndClickHandler() { final SubscriptionInfo sub1 = mock(SubscriptionInfo.class); when(sub1.getSubscriptionId()).thenReturn(1); when(sub1.getDisplayName()).thenReturn("sub1"); @@ -185,6 +183,30 @@ public class MobileNetworkSummaryControllerTest { verify(mSubscriptionManager).setSubscriptionEnabled(eq(sub1.getSubscriptionId()), eq(true)); } + @Test + public void getSummary_oneInactivePSim_canDisablePsim_correctSummaryAndClickHandler() { + final SubscriptionInfo sub1 = mock(SubscriptionInfo.class); + when(sub1.getSubscriptionId()).thenReturn(1); + when(sub1.getDisplayName()).thenReturn("sub1"); + SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(sub1)); + when(mSubscriptionManager.isActiveSubscriptionId(eq(1))).thenReturn(false); + when(mSubscriptionManager.canDisablePhysicalSubscription()).thenReturn(true); + + mController.displayPreference(mPreferenceScreen); + mController.onResume(); + + assertThat(mController.getSummary()).isEqualTo("sub1"); + + final ArgumentCaptor intentCaptor = ArgumentCaptor.forClass(Intent.class); + doNothing().when(mContext).startActivity(intentCaptor.capture()); + mPreference.getOnPreferenceClickListener().onPreferenceClick(mPreference); + Intent intent = intentCaptor.getValue(); + assertThat(intent.getComponent().getClassName()).isEqualTo( + MobileNetworkActivity.class.getName()); + assertThat(intent.getIntExtra(Settings.EXTRA_SUB_ID, + SubscriptionManager.INVALID_SUBSCRIPTION_ID)).isEqualTo(sub1.getSubscriptionId()); + } + @Test public void getSummary_twoSubscriptions_correctSummaryAndFragment() { final SubscriptionInfo sub1 = mock(SubscriptionInfo.class); diff --git a/tests/robotests/src/com/android/settings/network/telephony/DisableSimFooterPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/DisableSimFooterPreferenceControllerTest.java index a0a4f009832..fa48ba5e257 100644 --- a/tests/robotests/src/com/android/settings/network/telephony/DisableSimFooterPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/network/telephony/DisableSimFooterPreferenceControllerTest.java @@ -18,11 +18,13 @@ package com.android.settings.network.telephony; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import android.content.Context; import android.telephony.SubscriptionInfo; +import android.telephony.SubscriptionManager; import com.android.settings.network.SubscriptionUtil; @@ -45,12 +47,15 @@ public class DisableSimFooterPreferenceControllerTest { private SubscriptionInfo mInfo; private Context mContext; + @Mock + private SubscriptionManager mSubscriptionManager; private DisableSimFooterPreferenceController mController; @Before public void setUp() { MockitoAnnotations.initMocks(this); - mContext = RuntimeEnvironment.application; + mContext = spy(RuntimeEnvironment.application); + doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class); when(mInfo.getSubscriptionId()).thenReturn(SUB_ID); SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mInfo)); mController = new DisableSimFooterPreferenceController(mContext, PREF_KEY); @@ -69,9 +74,18 @@ public class DisableSimFooterPreferenceControllerTest { } @Test - public void isAvailable_pSIM_available() { + public void isAvailable_pSIM_available_cannot_disable_pSIM() { when(mInfo.isEmbedded()).thenReturn(false); mController.init(SUB_ID); + doReturn(false).when(mSubscriptionManager).canDisablePhysicalSubscription(); assertThat(mController.isAvailable()).isTrue(); } + + @Test + public void isAvailable_pSIM_available_can_disable_pSIM() { + when(mInfo.isEmbedded()).thenReturn(false); + mController.init(SUB_ID); + doReturn(true).when(mSubscriptionManager).canDisablePhysicalSubscription(); + assertThat(mController.isAvailable()).isFalse(); + } } diff --git a/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkSwitchControllerTest.java b/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkSwitchControllerTest.java index ccf345f8bdd..04e3df83a73 100644 --- a/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkSwitchControllerTest.java +++ b/tests/robotests/src/com/android/settings/network/telephony/MobileNetworkSwitchControllerTest.java @@ -105,6 +105,10 @@ public class MobileNetworkSwitchControllerTest { when(mSubscription.isEmbedded()).thenReturn(false); mController.displayPreference(mScreen); assertThat(mSwitchBar.isShowing()).isFalse(); + + when(mSubscriptionManager.canDisablePhysicalSubscription()).thenReturn(true); + mController.displayPreference(mScreen); + assertThat(mSwitchBar.isShowing()).isTrue(); } @Test