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