diff --git a/src/com/android/settings/datausage/AppDataUsage.java b/src/com/android/settings/datausage/AppDataUsage.java index a272043a8d0..c727d06cc77 100644 --- a/src/com/android/settings/datausage/AppDataUsage.java +++ b/src/com/android/settings/datausage/AppDataUsage.java @@ -23,6 +23,7 @@ import static com.android.settings.datausage.lib.AppDataUsageRepository.getAppUi import static com.android.settings.datausage.lib.AppDataUsageRepository.getAppUidList; import static com.android.settings.spa.app.appinfo.AppInfoSettingsProvider.startAppInfoSettings; +import android.Manifest; import android.app.Activity; import android.app.settings.SettingsEnums; import android.content.Context; @@ -292,7 +293,8 @@ public class AppDataUsage extends DataUsageBaseFragment implements OnPreferenceC @VisibleForTesting void updatePrefs() { updatePrefs(getAppRestrictBackground(), getUnrestrictData(), getAppRestrictAll(), - getAppRestrictCellular(), getAppRestrictVpn(), getAppRestrictWifi()); + getAppRestrictCellular(), getAppRestrictVpn(), getAppRestrictWifi(), + hasInternetPermission()); } @VisibleForTesting @@ -335,7 +337,7 @@ public class AppDataUsage extends DataUsageBaseFragment implements OnPreferenceC private void updatePrefs(boolean restrictBackground, boolean unrestrictData, boolean restrictAll, boolean restrictCellular, boolean restrictVpn, - boolean restrictWifi) { + boolean restrictWifi, boolean hasInternetPermission) { if (!isSimHardwareVisible(mContext)) { return; } @@ -344,33 +346,36 @@ public class AppDataUsage extends DataUsageBaseFragment implements OnPreferenceC .checkIfMeteredDataUsageUserControlDisabled(mContext, mPackageName, UserHandle.getUserId(mAppItem.key)); if (mRestrictAll != null) { + mRestrictAll.setEnabled(hasInternetPermission); mRestrictAll.setChecked(!restrictAll); } if (mRestrictBackground != null) { mRestrictBackground.setDisabledByAdmin(admin); - mRestrictBackground.setEnabled(!mRestrictBackground.isDisabledByAdmin() && !restrictAll - && !restrictCellular); + mRestrictBackground.setEnabled(hasInternetPermission && + !mRestrictBackground.isDisabledByAdmin() && !restrictAll && + !restrictCellular); mRestrictBackground.setChecked(!restrictBackground && !restrictAll && !restrictCellular); } if (mRestrictCellular != null) { - mRestrictCellular.setEnabled(!restrictAll); + mRestrictCellular.setEnabled(hasInternetPermission && !restrictAll); mRestrictCellular.setChecked(!restrictAll && !restrictCellular); } if (mRestrictVpn != null) { - mRestrictVpn.setEnabled(!restrictAll); + mRestrictVpn.setEnabled(hasInternetPermission && !restrictAll); mRestrictVpn.setChecked(!restrictAll && !restrictVpn); } if (mRestrictWifi != null) { - mRestrictWifi.setEnabled(!restrictAll); + mRestrictWifi.setEnabled(hasInternetPermission && !restrictAll); mRestrictWifi.setChecked(!restrictAll && !restrictWifi); } if (mUnrestrictedData != null) { mUnrestrictedData.setDisabledByAdmin(admin); - mUnrestrictedData.setEnabled(!mUnrestrictedData.isDisabledByAdmin() && - !restrictBackground && !restrictAll && !restrictCellular); - mUnrestrictedData.setChecked(unrestrictData && !restrictBackground && !restrictAll - && !restrictCellular); + mUnrestrictedData.setEnabled(hasInternetPermission && + !mUnrestrictedData.isDisabledByAdmin() && !restrictBackground && !restrictAll && + !restrictCellular); + mUnrestrictedData.setChecked(unrestrictData && !restrictBackground && !restrictAll && + !restrictCellular); } } @@ -415,6 +420,11 @@ public class AppDataUsage extends DataUsageBaseFragment implements OnPreferenceC return (uidPolicy & policy) != 0; } + private boolean hasInternetPermission() { + return mPackageManager.checkPermission(Manifest.permission.INTERNET, mPackageName) + == PackageManager.PERMISSION_GRANTED; + } + private void setAppRestrictCellular(boolean restrict) { setAppRestriction(POLICY_REJECT_CELLULAR, restrict); } @@ -479,7 +489,8 @@ public class AppDataUsage extends DataUsageBaseFragment implements OnPreferenceC public void onAllowlistStatusChanged(int uid, boolean isAllowlisted) { if (mAppItem.uids.get(uid, false)) { updatePrefs(getAppRestrictBackground(), isAllowlisted, getAppRestrictAll(), - getAppRestrictCellular(), getAppRestrictVpn(), getAppRestrictWifi()); + getAppRestrictCellular(), getAppRestrictVpn(), getAppRestrictWifi(), + hasInternetPermission()); } } @@ -487,7 +498,8 @@ public class AppDataUsage extends DataUsageBaseFragment implements OnPreferenceC public void onDenylistStatusChanged(int uid, boolean isDenylisted) { if (mAppItem.uids.get(uid, false)) { updatePrefs(isDenylisted, getUnrestrictData(), getAppRestrictAll(), - getAppRestrictCellular(), getAppRestrictVpn(), getAppRestrictWifi()); + getAppRestrictCellular(), getAppRestrictVpn(), getAppRestrictWifi(), + hasInternetPermission()); } } }