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.packageinstaller.permission.utils; 18 19 import static android.Manifest.permission.READ_EXTERNAL_STORAGE; 20 import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; 21 import static android.content.pm.PackageManager.FLAG_PERMISSION_RESTRICTION_INSTALLER_EXEMPT; 22 import static android.content.pm.PackageManager.FLAG_PERMISSION_RESTRICTION_SYSTEM_EXEMPT; 23 import static android.content.pm.PackageManager.FLAG_PERMISSION_RESTRICTION_UPGRADE_EXEMPT; 24 25 import android.content.pm.PackageInfo; 26 import android.os.Build; 27 28 import androidx.annotation.NonNull; 29 30 import com.android.packageinstaller.permission.model.Permission; 31 32 /** 33 * The behavior of soft restricted permissions is different for each permission. This class collects 34 * the policies in one place. 35 * 36 * This is the twin of {@link com.android.server.policy.SoftRestrictedPermissionPolicy} 37 */ 38 public abstract class SoftRestrictedPermissionPolicy { 39 private static final int FLAGS_PERMISSION_RESTRICTION_ANY_EXEMPT = 40 FLAG_PERMISSION_RESTRICTION_SYSTEM_EXEMPT 41 | FLAG_PERMISSION_RESTRICTION_UPGRADE_EXEMPT 42 | FLAG_PERMISSION_RESTRICTION_INSTALLER_EXEMPT; 43 44 /** 45 * Check if the permission should be shown in the UI. 46 * 47 * @param pkg the package the permission belongs to 48 * @param permission the permission 49 * 50 * @return {@code true} iff the permission should be shown in the UI. 51 */ shouldShow(@onNull PackageInfo pkg, @NonNull Permission permission)52 public static boolean shouldShow(@NonNull PackageInfo pkg, @NonNull Permission permission) { 53 switch (permission.getName()) { 54 case READ_EXTERNAL_STORAGE: 55 case WRITE_EXTERNAL_STORAGE: { 56 boolean isWhiteListed = 57 (permission.getFlags() & FLAGS_PERMISSION_RESTRICTION_ANY_EXEMPT) != 0; 58 int targetSDK = pkg.applicationInfo.targetSdkVersion; 59 60 return isWhiteListed || targetSDK >= Build.VERSION_CODES.Q; 61 } 62 default: 63 return true; 64 } 65 } 66 } 67