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.providers.media.photopicker.util;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.os.Build;
22 import android.provider.DeviceConfig;
23 
24 import androidx.annotation.ChecksSdkIntAtLeast;
25 
26 import com.android.modules.utils.build.SdkLevel;
27 
28 /**
29  * Util class for whether we should show the safety protection resources.
30  */
31 public class SafetyProtectionUtils {
32     private static final String SAFETY_PROTECTION_RESOURCES_ENABLED = "safety_protection_enabled";
33 
34     /**
35      * Determines whether we should show the safety protection resources.
36      * We show the resources only if
37      * (1) the build version is T or after and
38      * (2) the feature flag safety_protection_enabled is enabled and
39      * (3) the config value config_safetyProtectionEnabled is enabled/true and
40      * (4) the resources exist (currently the resources only exist on GMS devices)
41      */
42     @ChecksSdkIntAtLeast(api = Build.VERSION_CODES.TIRAMISU)
shouldShowSafetyProtectionResources(Context context)43     public static boolean shouldShowSafetyProtectionResources(Context context) {
44         try {
45             return SdkLevel.isAtLeastT()
46                     && DeviceConfig.getBoolean(DeviceConfig.NAMESPACE_PRIVACY,
47                             SAFETY_PROTECTION_RESOURCES_ENABLED, false)
48                     && context.getResources().getBoolean(
49                             Resources.getSystem()
50                                     .getIdentifier("config_safetyProtectionEnabled",
51                                             "bool", "android"))
52                     && context.getDrawable(android.R.drawable.ic_safety_protection) != null
53                     && !context.getString(
54                             android.R.string.safety_protection_display_text).isEmpty();
55         } catch (Resources.NotFoundException e) {
56             // We should expect the resources to not exist for non-pixel devices
57             // (except for the OEMs that opt-in)
58             return false;
59         }
60     }
61 }
62