Restrict MobileDataSlice

- Hide MobileDataSlice if the user is not allowed to configure mobile networks.

Bug: 310630794
Flag: EXEMPT bugfix
Test: manual test
atest -c MobileDataSliceTest

Change-Id: I35814733a915f011e284b082ce7a94898ce8a6fb
This commit is contained in:
Weng Su
2024-08-02 19:44:50 +08:00
parent 09fb13fd1c
commit 75a070f3f0
2 changed files with 59 additions and 14 deletions

View File

@@ -29,6 +29,7 @@ import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.UserManager;
import android.provider.Settings;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
@@ -79,19 +80,24 @@ public class MobileDataSlice implements CustomSliceable {
@Override
public Slice getSlice() {
ListBuilder listBuilder = createListBuilder();
if (!isConfigMobileNetworksAllowed()) {
return listBuilder.build();
}
final IconCompat icon = IconCompat.createWithResource(mContext,
R.drawable.ic_network_cell);
final String title = mContext.getText(R.string.mobile_data_settings_title).toString();
@ColorInt final int color = Utils.getColorAccentDefaultColor(mContext);
// Return null until we can show a disabled-action Slice, blaming Airplane mode.
// Return empty slice until we can show a disabled-action Slice, blaming Airplane mode.
if (isAirplaneModeEnabled()) {
return null;
return listBuilder.build();
}
// Return null until we can show a disabled-action Slice.
// Return empty slice until we can show a disabled-action Slice.
if (!isMobileDataAvailable()) {
return null;
return listBuilder.build();
}
final CharSequence summary = getSummary();
@@ -109,11 +115,15 @@ public class MobileDataSlice implements CustomSliceable {
rowBuilder.setSubtitle(summary);
}
final ListBuilder listBuilder = new ListBuilder(mContext, getUri(),
ListBuilder.INFINITY)
return listBuilder
.setAccentColor(color)
.addRow(rowBuilder);
return listBuilder.build();
.addRow(rowBuilder)
.build();
}
@VisibleForTesting
ListBuilder createListBuilder() {
return new ListBuilder(mContext, getUri(), ListBuilder.INFINITY);
}
@Override
@@ -211,6 +221,19 @@ public class MobileDataSlice implements CustomSliceable {
return mTelephonyManager.isDataEnabled();
}
@VisibleForTesting
boolean isConfigMobileNetworksAllowed() {
if (mContext == null) return true;
UserManager userManager = mContext.getSystemService(UserManager.class);
if (userManager == null) return true;
boolean isAllowed = userManager.isAdminUser() && !userManager.hasUserRestriction(
UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS);
if (!isAllowed) {
Log.w(TAG, "The user is not allowed to configure Mobile Networks.");
}
return isAllowed;
}
/**
* Listener for mobile data state changes.
*