• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.systemui.biometrics;
18 
19 import static android.Manifest.permission.USE_BIOMETRIC_INTERNAL;
20 import static android.hardware.biometrics.BiometricManager.Authenticators;
21 import static android.view.accessibility.AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE;
22 
23 import android.annotation.IntDef;
24 import android.annotation.NonNull;
25 import android.annotation.Nullable;
26 import android.app.admin.DevicePolicyManager;
27 import android.content.Context;
28 import android.content.pm.PackageManager;
29 import android.hardware.biometrics.PromptInfo;
30 import android.hardware.biometrics.SensorPropertiesInternal;
31 import android.os.UserManager;
32 import android.util.DisplayMetrics;
33 import android.view.ViewGroup;
34 import android.view.accessibility.AccessibilityEvent;
35 import android.view.accessibility.AccessibilityManager;
36 
37 import com.android.internal.widget.LockPatternUtils;
38 
39 import java.lang.annotation.Retention;
40 import java.lang.annotation.RetentionPolicy;
41 import java.util.List;
42 
43 public class Utils {
44 
45     public static final int CREDENTIAL_PIN = 1;
46     public static final int CREDENTIAL_PATTERN = 2;
47     public static final int CREDENTIAL_PASSWORD = 3;
48 
49     @Retention(RetentionPolicy.SOURCE)
50     @IntDef({CREDENTIAL_PIN, CREDENTIAL_PATTERN, CREDENTIAL_PASSWORD})
51     @interface CredentialType {}
52 
dpToPixels(Context context, float dp)53     static float dpToPixels(Context context, float dp) {
54         return dp * ((float) context.getResources().getDisplayMetrics().densityDpi
55                 / DisplayMetrics.DENSITY_DEFAULT);
56     }
57 
pixelsToDp(Context context, float pixels)58     static float pixelsToDp(Context context, float pixels) {
59         return pixels / ((float) context.getResources().getDisplayMetrics().densityDpi
60                 / DisplayMetrics.DENSITY_DEFAULT);
61     }
62 
notifyAccessibilityContentChanged(AccessibilityManager am, ViewGroup view)63     static void notifyAccessibilityContentChanged(AccessibilityManager am, ViewGroup view) {
64         if (!am.isEnabled()) {
65             return;
66         }
67         AccessibilityEvent event = AccessibilityEvent.obtain();
68         event.setEventType(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
69         event.setContentChangeTypes(CONTENT_CHANGE_TYPE_SUBTREE);
70         view.sendAccessibilityEventUnchecked(event);
71         view.notifySubtreeAccessibilityStateChanged(view, view, CONTENT_CHANGE_TYPE_SUBTREE);
72     }
73 
isDeviceCredentialAllowed(PromptInfo promptInfo)74     static boolean isDeviceCredentialAllowed(PromptInfo promptInfo) {
75         @Authenticators.Types final int authenticators = promptInfo.getAuthenticators();
76         return (authenticators & Authenticators.DEVICE_CREDENTIAL) != 0;
77     }
78 
isBiometricAllowed(PromptInfo promptInfo)79     static boolean isBiometricAllowed(PromptInfo promptInfo) {
80         @Authenticators.Types final int authenticators = promptInfo.getAuthenticators();
81         return (authenticators & Authenticators.BIOMETRIC_WEAK) != 0;
82     }
83 
getCredentialType(Context context, int userId)84     static @CredentialType int getCredentialType(Context context, int userId) {
85         final LockPatternUtils lpu = new LockPatternUtils(context);
86         switch (lpu.getKeyguardStoredPasswordQuality(userId)) {
87             case DevicePolicyManager.PASSWORD_QUALITY_SOMETHING:
88                 return CREDENTIAL_PATTERN;
89             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
90             case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX:
91                 return CREDENTIAL_PIN;
92             case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
93             case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
94             case DevicePolicyManager.PASSWORD_QUALITY_COMPLEX:
95             case DevicePolicyManager.PASSWORD_QUALITY_MANAGED:
96                 return CREDENTIAL_PASSWORD;
97             default:
98                 return CREDENTIAL_PASSWORD;
99         }
100     }
101 
isManagedProfile(Context context, int userId)102     static boolean isManagedProfile(Context context, int userId) {
103         final UserManager userManager = context.getSystemService(UserManager.class);
104         return userManager.isManagedProfile(userId);
105     }
106 
containsSensorId(@ullable List<? extends SensorPropertiesInternal> properties, int sensorId)107     static boolean containsSensorId(@Nullable List<? extends SensorPropertiesInternal> properties,
108             int sensorId) {
109         if (properties == null) {
110             return false;
111         }
112 
113         for (SensorPropertiesInternal prop : properties) {
114             if (prop.sensorId == sensorId) {
115                 return true;
116             }
117         }
118 
119         return false;
120     }
121 
isSystem(@onNull Context context, @Nullable String clientPackage)122     static boolean isSystem(@NonNull Context context, @Nullable String clientPackage) {
123         final boolean hasPermission = context.checkCallingOrSelfPermission(USE_BIOMETRIC_INTERNAL)
124                 == PackageManager.PERMISSION_GRANTED;
125         return hasPermission && "android".equals(clientPackage);
126     }
127 }
128