1 /* 2 * Copyright (C) 2023 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.settingslib.testutils.shadow; 18 19 import android.content.AttributionSource; 20 import android.content.Context; 21 import android.content.PermissionChecker; 22 23 import androidx.annotation.NonNull; 24 import androidx.annotation.Nullable; 25 26 import org.robolectric.annotation.Implementation; 27 import org.robolectric.annotation.Implements; 28 29 import java.util.HashMap; 30 import java.util.Map; 31 /** Shadow class of {@link PermissionChecker}. */ 32 @Implements(PermissionChecker.class) 33 public class ShadowPermissionChecker { 34 private static final Map<String, Map<String, Integer>> RESULTS = new HashMap<>(); 35 /** Set the result of permission check for a specific permission. */ setResult(String packageName, String permission, int result)36 public static void setResult(String packageName, String permission, int result) { 37 if (!RESULTS.containsKey(packageName)) { 38 RESULTS.put(packageName, new HashMap<>()); 39 } 40 RESULTS.get(packageName).put(permission, result); 41 } 42 /** Check the permission of calling package. */ 43 @Implementation checkCallingPermissionForDataDelivery( Context context, String permission, String packageName, String attributionTag, String message)44 public static int checkCallingPermissionForDataDelivery( 45 Context context, 46 String permission, 47 String packageName, 48 String attributionTag, 49 String message) { 50 return RESULTS.containsKey(packageName) && RESULTS.get(packageName).containsKey(permission) 51 ? RESULTS.get(packageName).get(permission) 52 : PermissionChecker.checkCallingPermissionForDataDelivery( 53 context, permission, packageName, attributionTag, message); 54 } 55 /** Check general permission. */ 56 @Implementation checkPermissionForDataDelivery( Context context, String permission, int pid, int uid, String packageName, String attributionTag, String message)57 public static int checkPermissionForDataDelivery( 58 Context context, 59 String permission, 60 int pid, 61 int uid, 62 String packageName, 63 String attributionTag, 64 String message) { 65 return RESULTS.containsKey(packageName) && RESULTS.get(packageName).containsKey(permission) 66 ? RESULTS.get(packageName).get(permission) 67 : PermissionChecker.checkPermissionForDataDelivery( 68 context, permission, pid, uid, packageName, attributionTag, message); 69 } 70 /** Check general permission. */ 71 @Implementation checkPermissionForPreflight(@onNull Context context, @NonNull String permission, int pid, int uid, @Nullable String packageName)72 public static int checkPermissionForPreflight(@NonNull Context context, 73 @NonNull String permission, int pid, int uid, @Nullable String packageName) { 74 return checkPermissionForPreflight(context, permission, new AttributionSource( 75 uid, packageName, null /*attributionTag*/)); 76 } 77 /** Check general permission. */ 78 @Implementation checkPermissionForPreflight(@onNull Context context, @NonNull String permission, @NonNull AttributionSource attributionSource)79 public static int checkPermissionForPreflight(@NonNull Context context, 80 @NonNull String permission, @NonNull AttributionSource attributionSource) { 81 final String packageName = attributionSource.getPackageName(); 82 return RESULTS.containsKey(packageName) && RESULTS.get(packageName).containsKey(permission) 83 ? RESULTS.get(packageName).get(permission) 84 : PermissionChecker.checkPermissionForPreflight( 85 context, permission, attributionSource); 86 } 87 } 88