/* * Copyright (C) 2022 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.app.admin; import android.annotation.NonNull; import android.annotation.Nullable; import android.annotation.RequiresPermission; import android.annotation.SuppressLint; import android.annotation.SystemApi; import android.content.Context; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.graphics.drawable.Icon; import android.os.RemoteException; import android.provider.DeviceConfig; import android.util.DisplayMetrics; import android.util.Log; import java.util.ArrayList; import java.util.Objects; import java.util.Set; import java.util.function.Supplier; /** * Class containing required APIs to set, reset, and get device policy related resources. */ public class DevicePolicyResourcesManager { private static String TAG = "DevicePolicyResourcesManager"; private static String DISABLE_RESOURCES_UPDATABILITY_FLAG = "disable_resources_updatability"; private static boolean DEFAULT_DISABLE_RESOURCES_UPDATABILITY = false; private final Context mContext; private final IDevicePolicyManager mService; /** * @hide */ protected DevicePolicyResourcesManager(Context context, IDevicePolicyManager service) { mContext = context; mService = service; } /** * For each {@link DevicePolicyDrawableResource} item in {@code drawables}, if * {@link DevicePolicyDrawableResource#getDrawableSource()} is not set, it updates the drawable * resource for the combination of {@link DevicePolicyDrawableResource#getDrawableId()} and * {@link DevicePolicyDrawableResource#getDrawableStyle()} to the drawable with resource ID * {@link DevicePolicyDrawableResource#getResourceIdInCallingPackage()}, * meaning any system UI surface calling {@link #getDrawable} with {@code drawableId} and * {@code drawableStyle} will get the new resource after this API is called. * *
Otherwise, if {@link DevicePolicyDrawableResource#getDrawableSource()} is set, it * overrides any drawables that was set for the same {@code drawableId} and * {@code drawableStyle} for the provided source. * *
Sends a broadcast with action * {@link DevicePolicyManager#ACTION_DEVICE_POLICY_RESOURCE_UPDATED} to registered receivers * when a resource has been updated successfully. * *
Important notes to consider when using this API: *
Sends a broadcast with action
* {@link DevicePolicyManager#ACTION_DEVICE_POLICY_RESOURCE_UPDATED} to registered receivers
* when a resource has been reset successfully.
*
* @param drawableIds The list of IDs to remove.
*
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_MANAGEMENT_RESOURCES)
public void resetDrawables(@NonNull Set Also returns the drawable from {@code defaultDrawableLoader} if {@code drawableId}
* is {@link DevicePolicyResources#UNDEFINED}.
*
* Calls to this API will not return {@code null} unless no updated drawable was found
* and the call to {@code defaultDrawableLoader} returned {@code null}.
*
* This API uses the screen density returned from {@link Resources#getConfiguration()}, to
* set a different value use
* {@link #getDrawableForDensity(String, String, int, Supplier)}.
*
* Callers should register for
* {@link DevicePolicyManager#ACTION_DEVICE_POLICY_RESOURCE_UPDATED} to get notified when a
* resource has been updated.
*
* Note that each call to this API loads the resource from the package that called
* {@code setDrawables} to set the updated resource.
*
* @param drawableId The drawable ID to get the updated resource for.
* @param drawableStyle The drawable style to use.
* @param defaultDrawableLoader To get the default drawable if no updated drawable was set for
* the provided params.
*/
@Nullable
public Drawable getDrawable(
@NonNull String drawableId,
@NonNull String drawableStyle,
@NonNull Supplier If {@code drawableSource} is {@link DevicePolicyResources#UNDEFINED}, it returns the
* appropriate string for {@code drawableId} and {@code drawableStyle} similar to
* {@link #getDrawable(String, String, Supplier)}.
*
* Calls to this API will not return {@code null} unless no updated drawable was found
* and the call to {@code defaultDrawableLoader} returned {@code null}.
*
* Callers should register for
* {@link DevicePolicyManager#ACTION_DEVICE_POLICY_RESOURCE_UPDATED} to get notified when a
* resource has been updated.
*
* @param drawableId The drawable ID to get the updated resource for.
* @param drawableStyle The drawable style to use.
* @param drawableSource The source for the caller.
* @param defaultDrawableLoader To get the default drawable if no updated drawable was set for
* the provided params.
*/
@Nullable
public Drawable getDrawable(
@NonNull String drawableId,
@NonNull String drawableStyle,
@NonNull String drawableSource,
@NonNull Supplier Calls to this API will not return {@code null} unless no updated drawable was found
* and the call to {@code defaultDrawableLoader} returned {@code null}.
*
* Callers should register for
* {@link DevicePolicyManager#ACTION_DEVICE_POLICY_RESOURCE_UPDATED} to get notified when a
* resource has been updated.
*
* @param drawableId The drawable ID to get the updated resource for.
* @param drawableStyle The drawable style to use.
* @param density The desired screen density indicated by the resource as
* found in {@link DisplayMetrics}. A value of 0 means to use the
* density returned from {@link Resources#getConfiguration()}.
* @param defaultDrawableLoader To get the default drawable if no updated drawable was set for
* the provided params.
*/
@Nullable
public Drawable getDrawableForDensity(
@NonNull String drawableId,
@NonNull String drawableStyle,
int density,
@NonNull Supplier Calls to this API will not return {@code null} unless no updated drawable was found
* and the call to {@code defaultDrawableLoader} returned {@code null}.
*
* Callers should register for
* {@link DevicePolicyManager#ACTION_DEVICE_POLICY_RESOURCE_UPDATED} to get notified when a
* resource has been updated.
*
* @param drawableId The drawable ID to get the updated resource for.
* @param drawableStyle The drawable style to use.
* @param drawableSource The source for the caller.
* @param density The desired screen density indicated by the resource as
* found in {@link DisplayMetrics}. A value of 0 means to use the
* density returned from {@link Resources#getConfiguration()}.
* @param defaultDrawableLoader To get the default drawable if no updated drawable was set for
* the provided params.
*/
@Nullable
public Drawable getDrawableForDensity(
@NonNull String drawableId,
@NonNull String drawableStyle,
@NonNull String drawableSource,
int density,
@NonNull Supplier Sends a broadcast with action
* {@link DevicePolicyManager#ACTION_DEVICE_POLICY_RESOURCE_UPDATED} to registered receivers
* when a resource has been updated successfully.
*
* Important notes to consider when using this API:
* Sends a broadcast with action
* {@link DevicePolicyManager#ACTION_DEVICE_POLICY_RESOURCE_UPDATED} to registered receivers
* when a resource has been reset successfully.
*
* @param stringIds The list of IDs to remove the updated resources for.
*
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_MANAGEMENT_RESOURCES)
public void resetStrings(@NonNull Set Also returns the string from {@code defaultStringLoader} if {@code stringId} is
* {@link DevicePolicyResources#UNDEFINED}.
*
* Calls to this API will not return {@code null} unless no updated drawable was found
* and the call to {@code defaultStringLoader} returned {@code null}.
*
* Callers should register for
* {@link DevicePolicyManager#ACTION_DEVICE_POLICY_RESOURCE_UPDATED} to get notified when a
* resource has been updated.
*
* Note that each call to this API loads the resource from the package that called
* {@code setStrings} to set the updated resource.
*
* @param stringId The IDs to get the updated resource for.
* @param defaultStringLoader To get the default string if no updated string was set for
* {@code stringId}.
*/
@Nullable
public String getString(
@NonNull String stringId,
@NonNull Supplier Calls to this API will not return {@code null} unless no updated drawable was found
* and the call to {@code defaultStringLoader} returned {@code null}.
*
* @param stringId The IDs to get the updated resource for.
* @param defaultStringLoader To get the default string if no updated string was set for
* {@code stringId}.
* @param formatArgs The format arguments that will be used for substitution.
*/
@Nullable
@SuppressLint("SamShouldBeLast")
public String getString(
@NonNull String stringId,
@NonNull Supplier
*
*
* @param strings The list of {@link DevicePolicyStringResource} to update.
*
* @hide
*/
@SystemApi
@RequiresPermission(android.Manifest.permission.UPDATE_DEVICE_MANAGEMENT_RESOURCES)
public void setStrings(@NonNull Set