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 package com.android.systemui.biometrics 17 18 import android.Manifest 19 import android.annotation.IntDef 20 import android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC 21 import android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC 22 import android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_COMPLEX 23 import android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_MANAGED 24 import android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC 25 import android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX 26 import android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_SOMETHING 27 import android.content.Context 28 import android.content.pm.PackageManager 29 import android.hardware.biometrics.BiometricManager.Authenticators 30 import android.hardware.biometrics.PromptInfo 31 import android.hardware.biometrics.SensorPropertiesInternal 32 import android.os.UserManager 33 import android.util.DisplayMetrics 34 import android.view.ViewGroup 35 import android.view.WindowManager 36 import android.view.accessibility.AccessibilityEvent 37 import android.view.accessibility.AccessibilityManager 38 import com.android.internal.widget.LockPatternUtils 39 import java.lang.annotation.Retention 40 import java.lang.annotation.RetentionPolicy 41 42 object Utils { 43 const val CREDENTIAL_PIN = 1 44 const val CREDENTIAL_PATTERN = 2 45 const val CREDENTIAL_PASSWORD = 3 46 47 /** Base set of layout flags for fingerprint overlay widgets. */ 48 const val FINGERPRINT_OVERLAY_LAYOUT_PARAM_FLAGS = 49 (WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN 50 or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL 51 or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE 52 or WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) 53 54 @JvmStatic dpToPixelsnull55 fun dpToPixels(context: Context, dp: Float): Float { 56 val density = context.resources.displayMetrics.densityDpi.toFloat() 57 return dp * (density / DisplayMetrics.DENSITY_DEFAULT) 58 } 59 60 @JvmStatic notifyAccessibilityContentChangednull61 fun notifyAccessibilityContentChanged(am: AccessibilityManager, view: ViewGroup) { 62 if (!am.isEnabled) { 63 return 64 } 65 val event = AccessibilityEvent.obtain() 66 event.eventType = AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED 67 event.contentChangeTypes = 68 AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE 69 view.sendAccessibilityEventUnchecked(event) 70 view.notifySubtreeAccessibilityStateChanged( 71 view, 72 view, 73 AccessibilityEvent.CONTENT_CHANGE_TYPE_SUBTREE 74 ) 75 } 76 77 @JvmStatic isDeviceCredentialAllowednull78 fun isDeviceCredentialAllowed(promptInfo: PromptInfo): Boolean = 79 (promptInfo.authenticators and Authenticators.DEVICE_CREDENTIAL) != 0 80 81 @JvmStatic 82 fun isBiometricAllowed(promptInfo: PromptInfo): Boolean = 83 (promptInfo.authenticators and Authenticators.BIOMETRIC_WEAK) != 0 84 85 @JvmStatic 86 @CredentialType 87 fun getCredentialType(utils: LockPatternUtils, userId: Int): Int = 88 when (utils.getKeyguardStoredPasswordQuality(userId)) { 89 PASSWORD_QUALITY_SOMETHING -> CREDENTIAL_PATTERN 90 PASSWORD_QUALITY_NUMERIC, 91 PASSWORD_QUALITY_NUMERIC_COMPLEX -> CREDENTIAL_PIN 92 PASSWORD_QUALITY_ALPHABETIC, 93 PASSWORD_QUALITY_ALPHANUMERIC, 94 PASSWORD_QUALITY_COMPLEX, 95 PASSWORD_QUALITY_MANAGED -> CREDENTIAL_PASSWORD 96 else -> CREDENTIAL_PASSWORD 97 } 98 99 @JvmStatic isManagedProfilenull100 fun isManagedProfile(context: Context, userId: Int): Boolean = 101 context.getSystemService(UserManager::class.java)?.isManagedProfile(userId) ?: false 102 103 @JvmStatic 104 fun <T : SensorPropertiesInternal> findFirstSensorProperties( 105 properties: List<T>?, 106 sensorIds: IntArray 107 ): T? = properties?.firstOrNull { sensorIds.contains(it.sensorId) } 108 109 @JvmStatic isSystemnull110 fun isSystem(context: Context, clientPackage: String?): Boolean { 111 val hasPermission = 112 (context.checkCallingOrSelfPermission(Manifest.permission.USE_BIOMETRIC_INTERNAL) 113 == PackageManager.PERMISSION_GRANTED) 114 return hasPermission && "android" == clientPackage 115 } 116 117 @Retention(RetentionPolicy.SOURCE) 118 @IntDef(CREDENTIAL_PIN, CREDENTIAL_PATTERN, CREDENTIAL_PASSWORD) 119 internal annotation class CredentialType 120 }