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 package android.annotation; 17 18 import static java.lang.annotation.ElementType.CONSTRUCTOR; 19 import static java.lang.annotation.ElementType.METHOD; 20 import static java.lang.annotation.ElementType.PACKAGE; 21 import static java.lang.annotation.ElementType.TYPE; 22 import static java.lang.annotation.RetentionPolicy.SOURCE; 23 24 import java.lang.annotation.Retention; 25 import java.lang.annotation.Target; 26 27 /** 28 * Indicates an API that uses {@code context.getUser} or {@code context.getUserId} 29 * to operate across users (as the user associated with the context) 30 * <p> 31 * To create a {@link android.content.Context} associated with a different user, 32 * use {@link android.content.Context#createContextAsUser} or 33 * {@link android.content.Context#createPackageContextAsUser} 34 * <p> 35 * Example: 36 * <pre>{@code 37 * {@literal @}UserHandleAware 38 * public abstract PackageInfo getPackageInfo({@literal @}NonNull String packageName, 39 * {@literal @}PackageInfoFlags int flags) throws NameNotFoundException; 40 * }</pre> 41 * 42 * @memberDoc This method uses {@linkplain android.content.Context#getUser} 43 * or {@linkplain android.content.Context#getUserId} to execute across users. 44 * @hide 45 */ 46 @Retention(SOURCE) 47 @Target({TYPE, METHOD, CONSTRUCTOR, PACKAGE}) 48 public @interface UserHandleAware { 49 50 /** 51 * Specifies the SDK version at which this method became {@literal @}UserHandleAware, 52 * if it was not always so. 53 * 54 * Prior to this level, the method is not considered {@literal @}UserHandleAware and therefore 55 * uses the {@link android.os#myUserHandle() calling user}, 56 * not the {@link android.content.Context#getUser context user}. 57 * 58 * Note that when an API marked with this parameter is run on a device whose OS predates the 59 * stated version, the calling user will be used, since on such a 60 * device, the API is not {@literal @}UserHandleAware yet. 61 */ enabledSinceTargetSdkVersion()62 int enabledSinceTargetSdkVersion() default 0; 63 64 /** 65 * Specifies the permission name required 66 * if the context user differs from the calling user. 67 * 68 * This requirement is in addition to any specified by 69 * {@link android.annotation.RequiresPermission}. 70 * 71 * @see android.annotation.RequiresPermission#value() 72 */ requiresPermissionIfNotCaller()73 String requiresPermissionIfNotCaller() default ""; 74 75 /** 76 * Specifies a list of permission names where at least one is required 77 * if the context user differs from the calling user. 78 * 79 * This requirement is in addition to any specified by 80 * {@link android.annotation.RequiresPermission}. 81 * 82 * @see android.annotation.RequiresPermission#anyOf() 83 */ requiresAnyOfPermissionsIfNotCaller()84 String[] requiresAnyOfPermissionsIfNotCaller() default {}; 85 86 /** 87 * Specifies a list of permission names where at least one is required if the context 88 * user is not in the same profile group as the calling user. 89 * 90 * This requirement is in addition to any specified by 91 * {@link android.annotation.RequiresPermission}. 92 * 93 * @see android.annotation.RequiresPermission#anyOf() 94 */ requiresAnyOfPermissionsIfNotCallerProfileGroup()95 String[] requiresAnyOfPermissionsIfNotCallerProfileGroup() default {}; 96 } 97