1 /*
2  * Copyright (C) 2015 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.biometrics.fingerprint;
18 
19 import static android.hardware.fingerprint.FingerprintManager.ENROLL_ENROLL;
20 
21 import android.app.Activity;
22 import android.app.settings.SettingsEnums;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.hardware.fingerprint.FingerprintEnrollOptions;
26 import android.hardware.fingerprint.FingerprintManager;
27 import android.os.SystemClock;
28 import android.util.Log;
29 
30 import com.android.internal.annotations.VisibleForTesting;
31 import com.android.settings.R;
32 import com.android.settings.biometrics.BiometricEnrollSidecar;
33 import com.android.settings.biometrics.BiometricUtils;
34 
35 import com.google.android.setupcompat.util.WizardManagerHelper;
36 
37 /**
38  * Sidecar fragment to handle the state around fingerprint enrollment.
39  */
40 public class FingerprintEnrollSidecar extends BiometricEnrollSidecar {
41     private static final String TAG = "FingerprintEnrollSidecar";
42 
43     private FingerprintUpdater mFingerprintUpdater;
44     private @FingerprintManager.EnrollReason int mEnrollReason;
45     private final MessageDisplayController mMessageDisplayController;
46     private final boolean mMessageDisplayControllerFlag;
47     private final Intent mIntent;
48 
49     /**
50      * Create a new FingerprintEnrollSidecar object.
51      *
52      * @param context      associated context
53      * @param enrollReason reason for enrollment
54      */
FingerprintEnrollSidecar(Context context, @FingerprintManager.EnrollReason int enrollReason, Intent intent)55     public FingerprintEnrollSidecar(Context context,
56             @FingerprintManager.EnrollReason int enrollReason, Intent intent) {
57         mEnrollReason = enrollReason;
58         mIntent = intent;
59 
60         int helpMinimumDisplayTime = context.getResources().getInteger(
61                 R.integer.enrollment_help_minimum_time_display);
62         int progressMinimumDisplayTime = context.getResources().getInteger(
63                 R.integer.enrollment_progress_minimum_time_display);
64         boolean progressPriorityOverHelp = context.getResources().getBoolean(
65                 R.bool.enrollment_progress_priority_over_help);
66         boolean prioritizeAcquireMessages = context.getResources().getBoolean(
67                 R.bool.enrollment_prioritize_acquire_messages);
68         int collectTime = context.getResources().getInteger(
69                 R.integer.enrollment_collect_time);
70         mMessageDisplayControllerFlag = context.getResources().getBoolean(
71                 R.bool.enrollment_message_display_controller_flag);
72 
73         mMessageDisplayController = new MessageDisplayController(context.getMainThreadHandler(),
74                 mEnrollmentCallback, SystemClock.elapsedRealtimeClock(), helpMinimumDisplayTime,
75                 progressMinimumDisplayTime, progressPriorityOverHelp, prioritizeAcquireMessages,
76                 collectTime);
77     }
78 
79     @Override
onAttach(Activity activity)80     public void onAttach(Activity activity) {
81         super.onAttach(activity);
82         mFingerprintUpdater = new FingerprintUpdater(activity);
83     }
84 
85     @Override
startEnrollment()86     protected void startEnrollment() {
87         super.startEnrollment();
88 
89         if (mToken == null) {
90             Log.e(TAG, "Null hardware auth token for enroll");
91             onEnrollmentError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE,
92                     getString(R.string.fingerprint_intro_error_unknown));
93             return;
94         }
95 
96         if (mIntent.getIntExtra(BiometricUtils.EXTRA_ENROLL_REASON, -1) == -1) {
97             final boolean isSuw = WizardManagerHelper.isAnySetupWizard(mIntent);
98             mIntent.putExtra(BiometricUtils.EXTRA_ENROLL_REASON,
99                     isSuw ? FingerprintEnrollOptions.ENROLL_REASON_SUW :
100                             FingerprintEnrollOptions.ENROLL_REASON_SETTINGS);
101         }
102 
103         if (mEnrollReason == ENROLL_ENROLL && mMessageDisplayControllerFlag) {
104             //API calls need to be processed for {@link FingerprintEnrollEnrolling}
105             mFingerprintUpdater.enroll(mToken, mEnrollmentCancel, mUserId,
106                     mMessageDisplayController, mEnrollReason, mIntent);
107         } else {
108             //No processing required for {@link FingerprintEnrollFindSensor}
109             mFingerprintUpdater.enroll(mToken, mEnrollmentCancel, mUserId, mEnrollmentCallback,
110                     mEnrollReason, mIntent);
111         }
112     }
113 
setEnrollReason(@ingerprintManager.EnrollReason int enrollReason)114     public void setEnrollReason(@FingerprintManager.EnrollReason int enrollReason) {
115         mEnrollReason = enrollReason;
116     }
117 
118     @VisibleForTesting
119     FingerprintManager.EnrollmentCallback mEnrollmentCallback
120             = new FingerprintManager.EnrollmentCallback() {
121 
122         @Override
123         public void onEnrollmentProgress(int remaining) {
124             FingerprintEnrollSidecar.super.onEnrollmentProgress(remaining);
125         }
126 
127         @Override
128         public void onAcquired(boolean isAcquiredGood) {
129             FingerprintEnrollSidecar.super.onAcquired(isAcquiredGood);
130         }
131 
132         @Override
133         public void onEnrollmentHelp(int helpMsgId, CharSequence helpString) {
134             FingerprintEnrollSidecar.super.onEnrollmentHelp(helpMsgId, helpString);
135         }
136 
137         @Override
138         public void onEnrollmentError(int errMsgId, CharSequence errString) {
139             FingerprintEnrollSidecar.super.onEnrollmentError(errMsgId, errString);
140         }
141 
142         @Override
143         public void onUdfpsPointerDown(int sensorId) {
144             FingerprintEnrollSidecar.super.onUdfpsPointerDown(sensorId);
145         }
146 
147         @Override
148         public void onUdfpsPointerUp(int sensorId) {
149             FingerprintEnrollSidecar.super.onUdfpsPointerUp(sensorId);
150         }
151 
152         @Override
153         public void onUdfpsOverlayShown() {
154             FingerprintEnrollSidecar.super.onUdfpsOverlayShown();
155         }
156     };
157 
158     @Override
getMetricsCategory()159     public int getMetricsCategory() {
160         return SettingsEnums.FINGERPRINT_ENROLL_SIDECAR;
161     }
162 }
163