1 /*
2  * Copyright (C) 2019 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.permissioncontroller.permission.utils;
18 
19 import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
20 import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
21 
22 import android.content.pm.PackageInfo;
23 import android.os.Build;
24 
25 import androidx.annotation.NonNull;
26 
27 import com.android.permissioncontroller.permission.model.Permission;
28 import com.android.permissioncontroller.permission.model.livedatatypes.LightPackageInfo;
29 
30 /**
31  * The behavior of soft restricted permissions is different for each permission. This class collects
32  * the policies in one place.
33  *
34  * This is the twin of {@link com.android.server.policy.SoftRestrictedPermissionPolicy}
35  */
36 public abstract class SoftRestrictedPermissionPolicy {
37 
38     /**
39      * Check if the permission should be shown in the UI.
40      *
41      * @param pkg the package the permission belongs to
42      * @param permission the permission
43      *
44      * @return {@code true} iff the permission should be shown in the UI.
45      */
shouldShow(@onNull PackageInfo pkg, @NonNull Permission permission)46     public static boolean shouldShow(@NonNull PackageInfo pkg, @NonNull Permission permission) {
47         switch (permission.getName()) {
48             case READ_EXTERNAL_STORAGE:
49             case WRITE_EXTERNAL_STORAGE: {
50                 boolean isWhiteListed =
51                         (permission.getFlags() & Utils.FLAGS_PERMISSION_RESTRICTION_ANY_EXEMPT)
52                                 != 0;
53                 int targetSDK = pkg.applicationInfo.targetSdkVersion;
54 
55                 return isWhiteListed || targetSDK >= Build.VERSION_CODES.Q;
56             }
57             default:
58                 return true;
59         }
60     }
61 
62     /**
63      * Check if the permission should be shown in the UI.
64      *
65      * @param pkg the LightPackageInfo the permission belongs to
66      * @param permissionName the name of the permission
67      * @param permissionFlags the PermissionController flags (not the PermissionInfo flags) for
68      * the permission
69      *
70      * @return {@code true} iff the permission should be shown in the UI.
71      */
shouldShow(@onNull LightPackageInfo pkg, @NonNull String permissionName, @NonNull int permissionFlags)72     public static boolean shouldShow(@NonNull LightPackageInfo pkg, @NonNull String permissionName,
73             @NonNull int permissionFlags) {
74         switch (permissionName) {
75             case READ_EXTERNAL_STORAGE:
76             case WRITE_EXTERNAL_STORAGE: {
77                 boolean isWhiteListed =
78                         (permissionFlags & Utils.FLAGS_PERMISSION_RESTRICTION_ANY_EXEMPT) != 0;
79                 return isWhiteListed || pkg.getTargetSdkVersion() >= Build.VERSION_CODES.Q;
80             }
81             default:
82                 return true;
83         }
84     }
85 }
86