Merge "Using new API setDataEnabledForReason for setDataEnabled" into main

This commit is contained in:
SongFerng Wang
2024-01-24 06:46:07 +00:00
committed by Android (Google) Code Review
2 changed files with 18 additions and 9 deletions

View File

@@ -358,7 +358,8 @@ public class MobileNetworkUtils {
.createForSubscriptionId(subId); .createForSubscriptionId(subId);
final SubscriptionManager subscriptionManager = context.getSystemService( final SubscriptionManager subscriptionManager = context.getSystemService(
SubscriptionManager.class).createForAllUserProfiles(); SubscriptionManager.class).createForAllUserProfiles();
telephonyManager.setDataEnabled(enabled); telephonyManager.setDataEnabledForReason(TelephonyManager.DATA_ENABLED_REASON_USER,
enabled);
if (disableOtherSubscriptions) { if (disableOtherSubscriptions) {
final List<SubscriptionInfo> subInfoList = final List<SubscriptionInfo> subInfoList =
@@ -367,8 +368,10 @@ public class MobileNetworkUtils {
for (SubscriptionInfo subInfo : subInfoList) { for (SubscriptionInfo subInfo : subInfoList) {
// We never disable mobile data for opportunistic subscriptions. // We never disable mobile data for opportunistic subscriptions.
if (subInfo.getSubscriptionId() != subId && !subInfo.isOpportunistic()) { if (subInfo.getSubscriptionId() != subId && !subInfo.isOpportunistic()) {
context.getSystemService(TelephonyManager.class).createForSubscriptionId( context.getSystemService(TelephonyManager.class)
subInfo.getSubscriptionId()).setDataEnabled(false); .createForSubscriptionId(subInfo.getSubscriptionId())
.setDataEnabledForReason(TelephonyManager.DATA_ENABLED_REASON_USER,
false);
} }
} }
} }

View File

@@ -148,24 +148,30 @@ public class MobileNetworkUtilsTest {
public void setMobileDataEnabled_setEnabled_enabled() { public void setMobileDataEnabled_setEnabled_enabled() {
MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_1, true, false); MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_1, true, false);
verify(mTelephonyManager).setDataEnabled(true); verify(mTelephonyManager)
verify(mTelephonyManager2, never()).setDataEnabled(anyBoolean()); .setDataEnabledForReason(TelephonyManager.DATA_ENABLED_REASON_USER, true);
verify(mTelephonyManager2, never())
.setDataEnabledForReason(anyInt(), anyBoolean());
} }
@Test @Test
public void setMobileDataEnabled_setDisabled_disabled() { public void setMobileDataEnabled_setDisabled_disabled() {
MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_2, true, false); MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_2, true, false);
verify(mTelephonyManager2).setDataEnabled(true); verify(mTelephonyManager2)
verify(mTelephonyManager, never()).setDataEnabled(anyBoolean()); .setDataEnabledForReason(TelephonyManager.DATA_ENABLED_REASON_USER, true);
verify(mTelephonyManager, never())
.setDataEnabledForReason(anyInt(), anyBoolean());
} }
@Test @Test
public void setMobileDataEnabled_disableOtherSubscriptions() { public void setMobileDataEnabled_disableOtherSubscriptions() {
MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_1, true, true); MobileNetworkUtils.setMobileDataEnabled(mContext, SUB_ID_1, true, true);
verify(mTelephonyManager).setDataEnabled(true); verify(mTelephonyManager)
verify(mTelephonyManager2).setDataEnabled(false); .setDataEnabledForReason(TelephonyManager.DATA_ENABLED_REASON_USER, true);
verify(mTelephonyManager2)
.setDataEnabledForReason(TelephonyManager.DATA_ENABLED_REASON_USER, false);
} }
@Test @Test