• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.safetycenter;
18 
19 import android.app.PendingIntent;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.hardware.face.FaceManager;
23 import android.hardware.fingerprint.FingerprintManager;
24 import android.safetycenter.SafetyEvent;
25 import android.safetycenter.SafetySourceData;
26 import android.safetycenter.SafetySourceIssue;
27 import android.safetycenter.SafetySourceStatus;
28 
29 import com.android.settings.Utils;
30 
31 /** Static helpers for setting SafetyCenter data for biometric safety sources. */
32 public final class BiometricSourcesUtils {
33 
34     public static final int REQUEST_CODE_COMBINED_BIOMETRIC_SETTING = 10;
35     public static final int REQUEST_CODE_FACE_SETTING = 20;
36     public static final int REQUEST_CODE_FINGERPRINT_SETTING = 30;
37 
BiometricSourcesUtils()38     private BiometricSourcesUtils() {}
39 
40     /** Sets data for one of the biometrics sources */
setBiometricSafetySourceData( String safetySourceId, Context context, String title, String summary, PendingIntent pendingIntent, boolean enabled, boolean hasEnrolled, SafetyEvent safetyEvent)41     public static void setBiometricSafetySourceData(
42             String safetySourceId,
43             Context context,
44             String title,
45             String summary,
46             PendingIntent pendingIntent,
47             boolean enabled,
48             boolean hasEnrolled,
49             SafetyEvent safetyEvent) {
50         setBiometricSafetySourceData(
51                 safetySourceId,
52                 context,
53                 title,
54                 summary,
55                 pendingIntent,
56                 enabled,
57                 hasEnrolled,
58                 safetyEvent,
59                 null
60         );
61     }
62 
63     /** Sets data for one of the biometrics sources */
setBiometricSafetySourceData( String safetySourceId, Context context, String title, String summary, PendingIntent pendingIntent, boolean enabled, boolean hasEnrolled, SafetyEvent safetyEvent, SafetySourceIssue safetySourceIssue)64     public static void setBiometricSafetySourceData(
65             String safetySourceId,
66             Context context,
67             String title,
68             String summary,
69             PendingIntent pendingIntent,
70             boolean enabled,
71             boolean hasEnrolled,
72             SafetyEvent safetyEvent,
73             SafetySourceIssue safetySourceIssue) {
74         int severityLevel =
75                 enabled && hasEnrolled
76                         ? SafetySourceData.SEVERITY_LEVEL_INFORMATION
77                         : SafetySourceData.SEVERITY_LEVEL_UNSPECIFIED;
78 
79         SafetySourceStatus status =
80                 new SafetySourceStatus.Builder(title, summary, severityLevel)
81                         .setPendingIntent(pendingIntent)
82                         .setEnabled(enabled)
83                         .build();
84 
85         SafetySourceData.Builder builder = new SafetySourceData.Builder().setStatus(status);
86         if (safetySourceIssue != null) {
87             builder.addIssue(safetySourceIssue);
88         }
89         SafetySourceData safetySourceData = builder.build();
90 
91 
92         SafetyCenterManagerWrapper.get()
93                 .setSafetySourceData(context, safetySourceId, safetySourceData, safetyEvent);
94     }
95 
96     /** Check whether the multiple biometrics enrollment is needed. */
isMultipleBiometricsEnrollmentNeeded(Context context, int userId)97     public static boolean isMultipleBiometricsEnrollmentNeeded(Context context, int userId) {
98         FaceManager faceManager = Utils.getFaceManagerOrNull(context);
99         FingerprintManager fingerprintManager = Utils.getFingerprintManagerOrNull(context);
100         return Utils.isMultipleBiometricsSupported(context)
101                 && !faceManager.hasEnrolledTemplates(userId)
102                 && !fingerprintManager.hasEnrolledFingerprints(userId);
103     }
104 
105     /** Helper method for creating a pending intent. */
createPendingIntent( Context context, Intent intent, int requestCode)106     public static PendingIntent createPendingIntent(
107             Context context, Intent intent, int requestCode) {
108         return PendingIntent.getActivity(
109                 context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE);
110     }
111 }
112