1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.settings.security; 17 18 import static android.view.contentprotection.flags.Flags.manageDevicePolicyEnabled; 19 20 import static com.android.internal.R.string.config_defaultContentProtectionService; 21 22 import android.app.admin.DevicePolicyManager; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.pm.PackageManager; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 import android.provider.DeviceConfig; 29 import android.view.contentcapture.ContentCaptureManager; 30 31 import androidx.annotation.NonNull; 32 import androidx.annotation.Nullable; 33 34 import com.android.settings.Utils; 35 36 /** Util class for content protection preference. */ 37 public class ContentProtectionPreferenceUtils { 38 39 /** 40 * Whether or not the content protection setting page is available. 41 */ isAvailable(@onNull Context context)42 public static boolean isAvailable(@NonNull Context context) { 43 if (!settingUiEnabled() || getContentProtectionServiceComponentName(context) == null) { 44 return false; 45 } 46 return true; 47 } 48 getContentProtectionServiceFlatComponentName(@onNull Context context)49 private static String getContentProtectionServiceFlatComponentName(@NonNull Context context) { 50 return context.getString(config_defaultContentProtectionService); 51 } 52 53 @Nullable getContentProtectionServiceComponentName(@onNull Context context)54 private static ComponentName getContentProtectionServiceComponentName(@NonNull Context context) { 55 String flatComponentName = getContentProtectionServiceFlatComponentName(context); 56 if (flatComponentName == null) { 57 return null; 58 } 59 return ComponentName.unflattenFromString(flatComponentName); 60 } 61 62 /** 63 * Whether or not the content protection UI is enabled. 64 */ settingUiEnabled()65 private static boolean settingUiEnabled() { 66 return DeviceConfig.getBoolean( 67 DeviceConfig.NAMESPACE_CONTENT_CAPTURE, 68 ContentCaptureManager.DEVICE_CONFIG_PROPERTY_ENABLE_CONTENT_PROTECTION_RECEIVER, 69 ContentCaptureManager.DEFAULT_ENABLE_CONTENT_PROTECTION_RECEIVER); 70 } 71 72 /** Returns the managed profile or null if none exists. */ 73 @Nullable getManagedProfile(@onNull Context context)74 public static UserHandle getManagedProfile(@NonNull Context context) { 75 UserManager userManager = context.getSystemService(UserManager.class); 76 if (userManager == null) { 77 return null; 78 } 79 return Utils.getManagedProfile(userManager); 80 } 81 82 /** Returns the current content protection policy. */ 83 @DevicePolicyManager.ContentProtectionPolicy getContentProtectionPolicy( @onNull Context context, @Nullable UserHandle managedProfile)84 public static int getContentProtectionPolicy( 85 @NonNull Context context, @Nullable UserHandle managedProfile) { 86 if (!manageDevicePolicyEnabled()) { 87 return DevicePolicyManager.CONTENT_PROTECTION_DISABLED; 88 } 89 Context policyContext = createContentProtectionPolicyContext(context, managedProfile); 90 return getContentProtectionPolicyWithGivenContext(policyContext); 91 } 92 93 @NonNull createContentProtectionPolicyContext( @onNull Context context, @Nullable UserHandle managedProfile)94 private static Context createContentProtectionPolicyContext( 95 @NonNull Context context, @Nullable UserHandle managedProfile) { 96 if (managedProfile == null) { 97 return context; 98 } 99 try { 100 return context.createPackageContextAsUser( 101 context.getPackageName(), /* flags= */ 0, managedProfile); 102 } catch (PackageManager.NameNotFoundException ex) { 103 throw new IllegalStateException(ex); 104 } 105 } 106 107 @DevicePolicyManager.ContentProtectionPolicy getContentProtectionPolicyWithGivenContext(@onNull Context context)108 private static int getContentProtectionPolicyWithGivenContext(@NonNull Context context) { 109 DevicePolicyManager devicePolicyManager = 110 context.getSystemService(DevicePolicyManager.class); 111 if (devicePolicyManager == null) { 112 return DevicePolicyManager.CONTENT_PROTECTION_DISABLED; 113 } 114 return devicePolicyManager.getContentProtectionPolicy(/* admin= */ null); 115 } 116 } 117