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.settings.testutils.shadow; 18 19 import android.content.Context; 20 import android.os.IBinder; 21 22 import com.android.settings.password.PasswordUtils; 23 24 import org.robolectric.annotation.Implementation; 25 import org.robolectric.annotation.Implements; 26 27 import java.util.Arrays; 28 import java.util.HashSet; 29 import java.util.Set; 30 31 @Implements(PasswordUtils.class) 32 public class ShadowPasswordUtils { 33 34 private static String sCallingAppLabel; 35 private static String sCallingAppPackageName; 36 private static Set<String> sGrantedPermissions; 37 reset()38 public static void reset() { 39 sCallingAppLabel = null; 40 sGrantedPermissions = null; 41 sCallingAppPackageName = null; 42 } 43 44 @Implementation isCallingAppPermitted(Context context, IBinder activityToken, String permission)45 protected static boolean isCallingAppPermitted(Context context, IBinder activityToken, 46 String permission) { 47 if (sGrantedPermissions == null) { 48 return false; 49 } 50 return sGrantedPermissions.contains(permission); 51 } 52 addGrantedPermission(String... permissions)53 public static void addGrantedPermission(String... permissions) { 54 if (sGrantedPermissions == null) { 55 sGrantedPermissions = new HashSet<>(); 56 } 57 sGrantedPermissions.addAll(Arrays.asList(permissions)); 58 } 59 60 @Implementation getCallingAppLabel(Context context, IBinder activityToken)61 protected static String getCallingAppLabel(Context context, IBinder activityToken) { 62 return sCallingAppLabel; 63 } 64 setCallingAppLabel(String label)65 public static void setCallingAppLabel(String label) { 66 sCallingAppLabel = label; 67 } 68 69 @Implementation getCallingAppPackageName(IBinder activityToken)70 protected static String getCallingAppPackageName(IBinder activityToken) { 71 return sCallingAppPackageName; 72 } 73 setCallingAppPackageName(String packageName)74 public static void setCallingAppPackageName(String packageName) { 75 sCallingAppPackageName = packageName; 76 } 77 } 78