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