AppDataUsage: Handle apps without INTERNET permission

* Disable network restriction toggles for apps without INTERNET permission

Change-Id: I1481b6f27912bf6559920e32b0072868d4f83a4c
This commit is contained in:
Oliver Scott
2022-01-14 16:06:54 +01:00
committed by Michael Bestas
parent 0017a2f638
commit f07da21855

View File

@@ -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());
}
}
}