1/* 2 * Copyright 2018 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 17package androidx.core.content.pm; 18 19import android.annotation.SuppressLint; 20import android.content.pm.PermissionInfo; 21 22import androidx.annotation.IntDef; 23import androidx.annotation.NonNull; 24import androidx.annotation.RestrictTo; 25import androidx.core.os.BuildCompat; 26 27import java.lang.annotation.Retention; 28import java.lang.annotation.RetentionPolicy; 29 30/** 31 * Helper for accessing features in {@link PermissionInfo}. 32 */ 33public final class PermissionInfoCompat { 34 private PermissionInfoCompat() { 35 } 36 37 /** @hide */ 38 @IntDef(flag = false, value = { 39 PermissionInfo.PROTECTION_NORMAL, 40 PermissionInfo.PROTECTION_DANGEROUS, 41 PermissionInfo.PROTECTION_SIGNATURE, 42 PermissionInfo.PROTECTION_SIGNATURE_OR_SYSTEM, 43 }) 44 @RestrictTo(RestrictTo.Scope.LIBRARY) 45 @Retention(RetentionPolicy.SOURCE) 46 public @interface Protection {} 47 48 /** @hide */ 49 @SuppressLint("UniqueConstants") // because _SYSTEM and _PRIVILEGED are aliases. 50 @IntDef(flag = true, value = { 51 PermissionInfo.PROTECTION_FLAG_PRIVILEGED, 52 PermissionInfo.PROTECTION_FLAG_SYSTEM, 53 PermissionInfo.PROTECTION_FLAG_DEVELOPMENT, 54 PermissionInfo.PROTECTION_FLAG_APPOP, 55 PermissionInfo.PROTECTION_FLAG_PRE23, 56 PermissionInfo.PROTECTION_FLAG_INSTALLER, 57 PermissionInfo.PROTECTION_FLAG_VERIFIER, 58 PermissionInfo.PROTECTION_FLAG_PREINSTALLED, 59 PermissionInfo.PROTECTION_FLAG_SETUP, 60 PermissionInfo.PROTECTION_FLAG_INSTANT, 61 PermissionInfo.PROTECTION_FLAG_RUNTIME_ONLY, 62 }) 63 @RestrictTo(RestrictTo.Scope.LIBRARY) 64 @Retention(RetentionPolicy.SOURCE) 65 public @interface ProtectionFlags {} 66 67 /** 68 * Return the base permission type of a {@link PermissionInfo}. 69 */ 70 @SuppressLint("WrongConstant") // for "PermissionInfo.PROTECTION_MASK_BASE" 71 @Protection 72 public static int getProtection(@NonNull PermissionInfo permissionInfo) { 73 if (BuildCompat.isAtLeastP()) { 74 return permissionInfo.getProtection(); 75 } else { 76 return permissionInfo.protectionLevel & PermissionInfo.PROTECTION_MASK_BASE; 77 } 78 } 79 80 /** 81 * Return the additional protection flags of a {@link PermissionInfo}. 82 */ 83 @SuppressLint("WrongConstant") // for "~PermissionInfo.PROTECTION_MASK_BASE" 84 @ProtectionFlags 85 public static int getProtectionFlags(@NonNull PermissionInfo permissionInfo) { 86 if (BuildCompat.isAtLeastP()) { 87 return permissionInfo.getProtectionFlags(); 88 } else { 89 return permissionInfo.protectionLevel & ~PermissionInfo.PROTECTION_MASK_BASE; 90 } 91 } 92} 93