Prevent disconnecting admin-configured VPN

First, if the VPN is configured by an admin, the preference is
disabled and tapping on it will results in a policy disclouser
dialog.
In addition restriction checks in the dialog also check if the
VPN is admin-configured.

Bug: 179975048
Test: Manual, setting VPN in profile and primary user and via DPM API.
Test: make RunSettingsRoboTests -j ROBOTEST_FILTER=com.android.settings.vpn2
Change-Id: Id59d2ac2782e83601bc3093d3a092faea36ff5d9
This commit is contained in:
Pavel Grafov
2021-03-18 22:46:08 +00:00
parent 7d9233302f
commit 16d588c11a
2 changed files with 33 additions and 2 deletions

View File

@@ -16,6 +16,7 @@
package com.android.settings.vpn2;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
@@ -26,6 +27,8 @@ import androidx.preference.Preference;
import com.android.internal.net.LegacyVpnInfo;
import com.android.internal.net.VpnConfig;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/**
* {@link androidx.preference.Preference} containing information about a VPN
@@ -43,6 +46,7 @@ public class AppPreference extends ManageablePreference {
super.setUserId(userId);
mPackageName = packageName;
disableIfConfiguredByAdmin();
// Fetch icon and VPN label
String label = packageName;
@@ -74,6 +78,25 @@ public class AppPreference extends ManageablePreference {
setIcon(icon);
}
/**
* Disable this preference if VPN is set as always on by a profile or device owner.
* NB: it should be called after super.setUserId() otherwise admin information can be lost.
*/
private void disableIfConfiguredByAdmin() {
if (isDisabledByAdmin()) {
// Already disabled due to user restriction.
return;
}
final DevicePolicyManager dpm = getContext()
.createContextAsUser(UserHandle.of(getUserId()), /* flags= */ 0)
.getSystemService(DevicePolicyManager.class);
if (mPackageName.equals(dpm.getAlwaysOnVpnPackage())) {
final EnforcedAdmin admin = RestrictedLockUtils.getProfileOrDeviceOwner(
getContext(), UserHandle.of(mUserId));
setDisabledByAdmin(admin);
}
}
public PackageInfo getPackageInfo() {
try {
PackageManager pm = getUserContext().getPackageManager();