1 /* 2 * Copyright (C) 2017 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.ComponentName; 20 import android.content.Context; 21 import android.content.pm.ApplicationInfo; 22 import android.content.pm.PackageManager; 23 import android.graphics.drawable.ColorDrawable; 24 import android.graphics.drawable.Drawable; 25 import android.hardware.fingerprint.FingerprintManager; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 import android.util.ArraySet; 29 30 import com.android.settings.Utils; 31 32 import org.robolectric.annotation.Implementation; 33 import org.robolectric.annotation.Implements; 34 35 import java.util.HashMap; 36 import java.util.Map; 37 38 @Implements(Utils.class) 39 public class ShadowUtils { 40 41 private static FingerprintManager sFingerprintManager = null; 42 private static boolean sIsUserAMonkey; 43 private static boolean sIsDemoUser; 44 private static ComponentName sDeviceOwnerComponentName; 45 private static Map<String, String> sAppNameMap; 46 private static boolean sIsSystemAlertWindowEnabled; 47 private static boolean sIsVoiceCapable; 48 private static ArraySet<String> sResultLinks = new ArraySet<>(); 49 private static boolean sIsBatteryPresent; 50 private static boolean sIsMultipleBiometricsSupported; 51 52 @Implementation enforceSameOwner(Context context, int userId)53 protected static int enforceSameOwner(Context context, int userId) { 54 return userId; 55 } 56 57 @Implementation getFingerprintManagerOrNull(Context context)58 protected static FingerprintManager getFingerprintManagerOrNull(Context context) { 59 return sFingerprintManager; 60 } 61 setFingerprintManager(FingerprintManager fingerprintManager)62 public static void setFingerprintManager(FingerprintManager fingerprintManager) { 63 sFingerprintManager = fingerprintManager; 64 } 65 reset()66 public static void reset() { 67 sFingerprintManager = null; 68 sIsUserAMonkey = false; 69 sIsDemoUser = false; 70 sIsVoiceCapable = false; 71 sResultLinks = new ArraySet<>(); 72 sIsBatteryPresent = true; 73 sIsMultipleBiometricsSupported = false; 74 } 75 setIsDemoUser(boolean isDemoUser)76 public static void setIsDemoUser(boolean isDemoUser) { 77 sIsDemoUser = isDemoUser; 78 } 79 80 @Implementation isDemoUser(Context context)81 public static boolean isDemoUser(Context context) { 82 return sIsDemoUser; 83 } 84 setIsUserAMonkey(boolean isUserAMonkey)85 public static void setIsUserAMonkey(boolean isUserAMonkey) { 86 sIsUserAMonkey = isUserAMonkey; 87 } 88 89 /** 90 * Returns true if Monkey is running. 91 */ 92 @Implementation isMonkeyRunning()93 protected static boolean isMonkeyRunning() { 94 return sIsUserAMonkey; 95 } 96 setDeviceOwnerComponent(ComponentName componentName)97 public static void setDeviceOwnerComponent(ComponentName componentName) { 98 sDeviceOwnerComponentName = componentName; 99 } 100 101 @Implementation getDeviceOwnerComponent(Context context)102 protected static ComponentName getDeviceOwnerComponent(Context context) { 103 return sDeviceOwnerComponentName; 104 } 105 106 @Implementation getManagedProfileId(UserManager um, int parentUserId)107 protected static int getManagedProfileId(UserManager um, int parentUserId) { 108 return UserHandle.USER_NULL; 109 } 110 111 @Implementation getApplicationLabel(Context context, String packageName)112 protected static CharSequence getApplicationLabel(Context context, String packageName) { 113 if (sAppNameMap != null) { 114 return sAppNameMap.get(packageName); 115 } 116 return null; 117 } 118 119 @Implementation isPackageEnabled(Context context, String packageName)120 protected static boolean isPackageEnabled(Context context, String packageName) { 121 return true; 122 } 123 setApplicationLabel(String packageName, String appLabel)124 public static void setApplicationLabel(String packageName, String appLabel) { 125 if (sAppNameMap == null) { 126 sAppNameMap = new HashMap<>(); 127 } 128 sAppNameMap.put(packageName, appLabel); 129 } 130 131 @Implementation isSystemAlertWindowEnabled(Context context)132 protected static boolean isSystemAlertWindowEnabled(Context context) { 133 return sIsSystemAlertWindowEnabled; 134 } 135 setIsSystemAlertWindowEnabled(boolean enabled)136 public static void setIsSystemAlertWindowEnabled(boolean enabled) { 137 sIsSystemAlertWindowEnabled = enabled; 138 } 139 140 @Implementation isVoiceCapable(Context context)141 protected static boolean isVoiceCapable(Context context) { 142 return sIsVoiceCapable; 143 } 144 setIsVoiceCapable(boolean isVoiceCapable)145 public static void setIsVoiceCapable(boolean isVoiceCapable) { 146 sIsVoiceCapable = isVoiceCapable; 147 } 148 149 @Implementation getHandledDomains(PackageManager pm, String packageName)150 protected static ArraySet<String> getHandledDomains(PackageManager pm, String packageName) { 151 return sResultLinks; 152 } 153 154 @Implementation getBadgedIcon(Context context, ApplicationInfo appInfo)155 protected static Drawable getBadgedIcon(Context context, ApplicationInfo appInfo) { 156 return new ColorDrawable(0); 157 } 158 setHandledDomains(ArraySet<String> links)159 public static void setHandledDomains(ArraySet<String> links) { 160 sResultLinks = links; 161 } 162 163 @Implementation isBatteryPresent(Context context)164 protected static boolean isBatteryPresent(Context context) { 165 return sIsBatteryPresent; 166 } 167 setIsBatteryPresent(boolean isBatteryPresent)168 public static void setIsBatteryPresent(boolean isBatteryPresent) { 169 sIsBatteryPresent = isBatteryPresent; 170 } 171 172 @Implementation isMultipleBiometricsSupported(Context context)173 protected static boolean isMultipleBiometricsSupported(Context context) { 174 return sIsMultipleBiometricsSupported; 175 } 176 setIsMultipleBiometricsSupported(boolean isMultipleBiometricsSupported)177 public static void setIsMultipleBiometricsSupported(boolean isMultipleBiometricsSupported) { 178 sIsMultipleBiometricsSupported = isMultipleBiometricsSupported; 179 } 180 } 181