• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
54     @Implementation
enforceSameOwner(Context context, int userId)55     protected static int enforceSameOwner(Context context, int userId) {
56         return userId;
57     }
58 
59     @Implementation
getFingerprintManagerOrNull(Context context)60     protected static FingerprintManager getFingerprintManagerOrNull(Context context) {
61         return sFingerprintManager;
62     }
63 
setFingerprintManager(FingerprintManager fingerprintManager)64     public static void setFingerprintManager(FingerprintManager fingerprintManager) {
65         sFingerprintManager = fingerprintManager;
66     }
67 
68     @Implementation
getFaceManagerOrNull(Context context)69     protected static FaceManager getFaceManagerOrNull(Context context) {
70         return sFaceManager;
71     }
72 
setFaceManager(FaceManager faceManager)73     public static void setFaceManager(FaceManager faceManager) {
74         sFaceManager = faceManager;
75     }
76 
reset()77     public static void reset() {
78         sFingerprintManager = null;
79         sIsUserAMonkey = false;
80         sIsDemoUser = false;
81         sIsVoiceCapable = false;
82         sResultLinks = new ArraySet<>();
83         sIsBatteryPresent = true;
84         sIsMultipleBiometricsSupported = false;
85     }
86 
setIsDemoUser(boolean isDemoUser)87     public static void setIsDemoUser(boolean isDemoUser) {
88         sIsDemoUser = isDemoUser;
89     }
90 
91     @Implementation
isDemoUser(Context context)92     public static boolean isDemoUser(Context context) {
93         return sIsDemoUser;
94     }
95 
setIsUserAMonkey(boolean isUserAMonkey)96     public static void setIsUserAMonkey(boolean isUserAMonkey) {
97         sIsUserAMonkey = isUserAMonkey;
98     }
99 
100     /**
101      * Returns true if Monkey is running.
102      */
103     @Implementation
isMonkeyRunning()104     protected static boolean isMonkeyRunning() {
105         return sIsUserAMonkey;
106     }
107 
setDeviceOwnerComponent(ComponentName componentName)108     public static void setDeviceOwnerComponent(ComponentName componentName) {
109         sDeviceOwnerComponentName = componentName;
110     }
111 
112     @Implementation
getDeviceOwnerComponent(Context context)113     protected static ComponentName getDeviceOwnerComponent(Context context) {
114         return sDeviceOwnerComponentName;
115     }
116 
117     @Implementation
getManagedProfileId(UserManager um, int parentUserId)118     protected static int getManagedProfileId(UserManager um, int parentUserId) {
119         return UserHandle.USER_NULL;
120     }
121 
122     @Implementation
getApplicationLabel(Context context, String packageName)123     protected static CharSequence getApplicationLabel(Context context, String packageName) {
124         if (sAppNameMap != null) {
125             return sAppNameMap.get(packageName);
126         }
127         return null;
128     }
129 
130     @Implementation
isPackageEnabled(Context context, String packageName)131     protected static boolean isPackageEnabled(Context context, String packageName) {
132         return true;
133     }
134 
setApplicationLabel(String packageName, String appLabel)135     public static void setApplicationLabel(String packageName, String appLabel) {
136         if (sAppNameMap == null) {
137             sAppNameMap = new HashMap<>();
138         }
139         sAppNameMap.put(packageName, appLabel);
140     }
141 
142     @Implementation
isSystemAlertWindowEnabled(Context context)143     protected static boolean isSystemAlertWindowEnabled(Context context) {
144         return sIsSystemAlertWindowEnabled;
145     }
146 
setIsSystemAlertWindowEnabled(boolean enabled)147     public static void setIsSystemAlertWindowEnabled(boolean enabled) {
148         sIsSystemAlertWindowEnabled = enabled;
149     }
150 
151     @Implementation
isVoiceCapable(Context context)152     protected static boolean isVoiceCapable(Context context) {
153         return sIsVoiceCapable;
154     }
155 
setIsVoiceCapable(boolean isVoiceCapable)156     public static void setIsVoiceCapable(boolean isVoiceCapable) {
157         sIsVoiceCapable = isVoiceCapable;
158     }
159 
160     @Implementation
getHandledDomains(PackageManager pm, String packageName)161     protected static ArraySet<String> getHandledDomains(PackageManager pm, String packageName) {
162         return sResultLinks;
163     }
164 
165     @Implementation
getBadgedIcon(Context context, ApplicationInfo appInfo)166     protected static Drawable getBadgedIcon(Context context, ApplicationInfo appInfo) {
167         return new ColorDrawable(0);
168     }
169 
setHandledDomains(ArraySet<String> links)170     public static void setHandledDomains(ArraySet<String> links) {
171         sResultLinks = links;
172     }
173 
174     @Implementation
isBatteryPresent(Context context)175     protected static boolean isBatteryPresent(Context context) {
176         return sIsBatteryPresent;
177     }
178 
setIsBatteryPresent(boolean isBatteryPresent)179     public static void setIsBatteryPresent(boolean isBatteryPresent) {
180         sIsBatteryPresent = isBatteryPresent;
181     }
182 
183     @Implementation
isMultipleBiometricsSupported(Context context)184     protected static boolean isMultipleBiometricsSupported(Context context) {
185         return sIsMultipleBiometricsSupported;
186     }
187 
setIsMultipleBiometricsSupported(boolean isMultipleBiometricsSupported)188     public static void setIsMultipleBiometricsSupported(boolean isMultipleBiometricsSupported) {
189         sIsMultipleBiometricsSupported = isMultipleBiometricsSupported;
190     }
191 }
192