Integrate admin controls for content protection

Bug: 323038631
Test: End-to-end with TestDPC, new units
Change-Id: I98d52fdd95fca023b8eb576c97d563a1f3cab464
This commit is contained in:
Nino Jagar
2024-03-06 01:17:52 +00:00
parent 5cbc60dcf3
commit 65eb00f0f3
6 changed files with 740 additions and 153 deletions

View File

@@ -15,9 +15,11 @@
*/
package com.android.settings.security;
import static android.view.contentprotection.flags.Flags.manageDevicePolicyEnabled;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.os.UserHandle;
import android.os.UserManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -25,7 +27,6 @@ import androidx.annotation.VisibleForTesting;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.TogglePreferenceController;
import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedSwitchPreference;
@@ -33,25 +34,49 @@ import com.android.settingslib.RestrictedSwitchPreference;
/** Preference controller for content protection work profile switch bar. */
public class ContentProtectionWorkSwitchController extends TogglePreferenceController {
@Nullable private UserHandle mManagedProfile;
@DevicePolicyManager.ContentProtectionPolicy
private int mContentProtectionPolicy = DevicePolicyManager.CONTENT_PROTECTION_DISABLED;
public ContentProtectionWorkSwitchController(
@NonNull Context context, @NonNull String preferenceKey) {
super(context, preferenceKey);
if (manageDevicePolicyEnabled()) {
mManagedProfile = getManagedProfile();
if (mManagedProfile != null) {
mContentProtectionPolicy = getContentProtectionPolicy(mManagedProfile);
}
}
}
@Override
public int getAvailabilityStatus() {
return getManagedProfile() != null ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
if (!manageDevicePolicyEnabled()) {
return getManagedProfile() != null ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
}
if (mManagedProfile == null) {
return CONDITIONALLY_UNAVAILABLE;
}
if (mContentProtectionPolicy
== DevicePolicyManager.CONTENT_PROTECTION_NOT_CONTROLLED_BY_POLICY) {
return CONDITIONALLY_UNAVAILABLE;
}
return AVAILABLE;
}
// The switch is always set to unchecked until Android V by design
@Override
public boolean isChecked() {
return false;
if (!manageDevicePolicyEnabled()) {
return false;
}
return mContentProtectionPolicy == DevicePolicyManager.CONTENT_PROTECTION_ENABLED;
}
// The switch is disabled until Android V by design
@Override
public boolean setChecked(boolean isChecked) {
// Controlled by the admin API
return false;
}
@@ -59,10 +84,13 @@ public class ContentProtectionWorkSwitchController extends TogglePreferenceContr
public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen);
RestrictedSwitchPreference switchPreference = screen.findPreference(getPreferenceKey());
UserHandle managedProfile = getManagedProfile();
UserHandle managedProfile =
manageDevicePolicyEnabled() ? mManagedProfile : getManagedProfile();
if (managedProfile != null) {
switchPreference.setDisabledByAdmin(getEnforcedAdmin(managedProfile));
RestrictedSwitchPreference switchPreference = screen.findPreference(getPreferenceKey());
if (switchPreference != null) {
switchPreference.setDisabledByAdmin(getEnforcedAdmin(managedProfile));
}
}
}
@@ -74,13 +102,18 @@ public class ContentProtectionWorkSwitchController extends TogglePreferenceContr
@VisibleForTesting
@Nullable
protected UserHandle getManagedProfile() {
return Utils.getManagedProfile(mContext.getSystemService(UserManager.class));
return ContentProtectionPreferenceUtils.getManagedProfile(mContext);
}
@VisibleForTesting
@Nullable
protected RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin(
@NonNull UserHandle managedProfile) {
return RestrictedLockUtils.getProfileOrDeviceOwner(mContext, managedProfile);
protected RestrictedLockUtils.EnforcedAdmin getEnforcedAdmin(@NonNull UserHandle userHandle) {
return RestrictedLockUtils.getProfileOrDeviceOwner(mContext, userHandle);
}
@VisibleForTesting
@DevicePolicyManager.ContentProtectionPolicy
protected int getContentProtectionPolicy(@Nullable UserHandle userHandle) {
return ContentProtectionPreferenceUtils.getContentProtectionPolicy(mContext, userHandle);
}
}