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.hardware.biometrics.BiometricManager.Authenticators; 20 import static android.view.accessibility.AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE; 21 22 import android.annotation.IntDef; 23 import android.app.admin.DevicePolicyManager; 24 import android.content.Context; 25 import android.hardware.biometrics.BiometricPrompt; 26 import android.os.Bundle; 27 import android.os.UserManager; 28 import android.util.DisplayMetrics; 29 import android.view.ViewGroup; 30 import android.view.accessibility.AccessibilityEvent; 31 import android.view.accessibility.AccessibilityManager; 32 33 import com.android.internal.widget.LockPatternUtils; 34 35 import java.lang.annotation.Retention; 36 import java.lang.annotation.RetentionPolicy; 37 38 public class Utils { 39 40 public static final int CREDENTIAL_PIN = 1; 41 public static final int CREDENTIAL_PATTERN = 2; 42 public static final int CREDENTIAL_PASSWORD = 3; 43 44 @Retention(RetentionPolicy.SOURCE) 45 @IntDef({CREDENTIAL_PIN, CREDENTIAL_PATTERN, CREDENTIAL_PASSWORD}) 46 @interface CredentialType {} 47 48 dpToPixels(Context context, float dp)49 static float dpToPixels(Context context, float dp) { 50 return dp * ((float) context.getResources().getDisplayMetrics().densityDpi 51 / DisplayMetrics.DENSITY_DEFAULT); 52 } 53 pixelsToDp(Context context, float pixels)54 static float pixelsToDp(Context context, float pixels) { 55 return pixels / ((float) context.getResources().getDisplayMetrics().densityDpi 56 / DisplayMetrics.DENSITY_DEFAULT); 57 } 58 notifyAccessibilityContentChanged(AccessibilityManager am, ViewGroup view)59 static void notifyAccessibilityContentChanged(AccessibilityManager am, ViewGroup view) { 60 if (!am.isEnabled()) { 61 return; 62 } 63 AccessibilityEvent event = AccessibilityEvent.obtain(); 64 event.setEventType(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); 65 event.setContentChangeTypes(CONTENT_CHANGE_TYPE_SUBTREE); 66 view.sendAccessibilityEventUnchecked(event); 67 view.notifySubtreeAccessibilityStateChanged(view, view, CONTENT_CHANGE_TYPE_SUBTREE); 68 } 69 isDeviceCredentialAllowed(Bundle biometricPromptBundle)70 static boolean isDeviceCredentialAllowed(Bundle biometricPromptBundle) { 71 final int authenticators = getAuthenticators(biometricPromptBundle); 72 return (authenticators & Authenticators.DEVICE_CREDENTIAL) != 0; 73 } 74 isBiometricAllowed(Bundle biometricPromptBundle)75 static boolean isBiometricAllowed(Bundle biometricPromptBundle) { 76 final int authenticators = getAuthenticators(biometricPromptBundle); 77 return (authenticators & Authenticators.BIOMETRIC_WEAK) != 0; 78 } 79 getAuthenticators(Bundle biometricPromptBundle)80 static int getAuthenticators(Bundle biometricPromptBundle) { 81 return biometricPromptBundle.getInt(BiometricPrompt.KEY_AUTHENTICATORS_ALLOWED); 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 } 107