1 /* 2 * Copyright (C) 2022 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 17 package com.android.safetycenter; 18 19 import static android.os.Build.VERSION_CODES.TIRAMISU; 20 21 import static java.util.Objects.requireNonNull; 22 23 import android.annotation.StringRes; 24 import android.app.admin.DevicePolicyManager; 25 import android.app.admin.DevicePolicyResourcesManager; 26 import android.content.Context; 27 import android.os.Binder; 28 29 import androidx.annotation.RequiresApi; 30 31 import com.android.safetycenter.resources.SafetyCenterResourcesContext; 32 33 import java.util.function.Supplier; 34 35 /** A class that handles dynamically updating enterprise-related resources. */ 36 @RequiresApi(TIRAMISU) 37 final class DevicePolicyResources { 38 39 private static final String SAFETY_CENTER_PREFIX = "SafetyCenter."; 40 private static final String WORK_PROFILE_PAUSED_TITLE = "WORK_PROFILE_PAUSED"; 41 DevicePolicyResources()42 private DevicePolicyResources() {} 43 44 /** 45 * Returns the updated string for the given {@code safetySourceId} by calling {@link 46 * DevicePolicyResourcesManager#getString}. 47 */ getSafetySourceWorkString( SafetyCenterResourcesContext safetyCenterResourcesContext, String safetySourceId, @StringRes int workResId)48 static String getSafetySourceWorkString( 49 SafetyCenterResourcesContext safetyCenterResourcesContext, 50 String safetySourceId, 51 @StringRes int workResId) { 52 return getEnterpriseString( 53 safetyCenterResourcesContext, 54 safetySourceId, 55 () -> safetyCenterResourcesContext.getString(workResId)); 56 } 57 58 /** 59 * Returns the updated string for the {@code work_profile_paused} string by calling {@link 60 * DevicePolicyResourcesManager#getString}. 61 */ getWorkProfilePausedString( SafetyCenterResourcesContext safetyCenterResourcesContext)62 static String getWorkProfilePausedString( 63 SafetyCenterResourcesContext safetyCenterResourcesContext) { 64 return getEnterpriseString( 65 safetyCenterResourcesContext, 66 WORK_PROFILE_PAUSED_TITLE, 67 () -> safetyCenterResourcesContext.getStringByName("work_profile_paused")); 68 } 69 getEnterpriseString( Context context, String devicePolicyIdentifier, Supplier<String> defaultValueLoader)70 private static String getEnterpriseString( 71 Context context, String devicePolicyIdentifier, Supplier<String> defaultValueLoader) { 72 // This call requires the caller’s identity to match the package name of the given context. 73 // However, the SafetyCenterResourcesContext’s has package name "android", which does not 74 // necessarily match the caller’s package when making Binder calls, so the calling identity 75 // has to be cleared. 76 final long callingId = Binder.clearCallingIdentity(); 77 try { 78 return requireNonNull(context.getSystemService(DevicePolicyManager.class)) 79 .getResources() 80 .getString(SAFETY_CENTER_PREFIX + devicePolicyIdentifier, defaultValueLoader); 81 } finally { 82 Binder.restoreCallingIdentity(callingId); 83 } 84 } 85 } 86