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 android.app.KeyguardManager;
20 import android.app.settings.SettingsEnums;
21 import android.content.Intent;
22 import android.hardware.fingerprint.FingerprintManager;
23 import android.view.View;
24 
25 import com.android.settings.SetupWizardUtils;
26 import com.android.settings.Utils;
27 import com.android.settings.biometrics.BiometricUtils;
28 import com.android.settings.flags.Flags;
29 import com.android.settings.password.ChooseLockSettingsHelper;
30 import com.android.settings.password.SetupSkipDialog;
31 
32 public class SetupFingerprintEnrollIntroduction extends FingerprintEnrollIntroduction {
33     /**
34      * Returns the number of fingerprint enrolled.
35      */
36     public static final String EXTRA_FINGERPRINT_ENROLLED_COUNT = "fingerprint_enrolled_count";
37 
38     private static final String KEY_LOCK_SCREEN_PRESENT = "wasLockScreenPresent";
39 
40     @Override
getEnrollingIntent()41     protected Intent getEnrollingIntent() {
42         final Intent intent = new Intent(this, SetupFingerprintEnrollFindSensor.class);
43         BiometricUtils.copyMultiBiometricExtras(getIntent(), intent);
44         if (BiometricUtils.containsGatekeeperPasswordHandle(getIntent())) {
45             intent.putExtra(
46                     ChooseLockSettingsHelper.EXTRA_KEY_GK_PW_HANDLE,
47                     BiometricUtils.getGatekeeperPasswordHandle(getIntent()));
48         }
49         SetupWizardUtils.copySetupExtras(getIntent(), intent);
50         if (Flags.udfpsEnrollCalibration()) {
51             if (mCalibrator != null) {
52                 intent.putExtras(mCalibrator.getExtrasForNextIntent());
53             }
54         }
55         return intent;
56     }
57 
58     @Override
onActivityResult(int requestCode, int resultCode, Intent data)59     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
60         boolean hasEnrolledFace = false;
61         boolean hasEnrolledFingerprint = false;
62         if (data != null) {
63             hasEnrolledFace = data.getBooleanExtra(EXTRA_FINISHED_ENROLL_FACE, false);
64             hasEnrolledFingerprint = data.getBooleanExtra(EXTRA_FINISHED_ENROLL_FINGERPRINT, false);
65             // If we've enrolled a face, we can remove the pending intent to launch FaceEnrollIntro.
66             if (hasEnrolledFace) {
67                 removeEnrollNextBiometric();
68             }
69         }
70         if (requestCode == BIOMETRIC_FIND_SENSOR_REQUEST && isKeyguardSecure()) {
71             // Report fingerprint count if user adding a new fingerprint
72             if (resultCode == RESULT_FINISHED) {
73                 data = setFingerprintCount(data);
74             }
75 
76             if (resultCode == RESULT_CANCELED && hasEnrolledFingerprint) {
77                 // If we are coming from a back press from an already enrolled fingerprint,
78                 // we can finish this activity.
79                 setResult(resultCode, data);
80                 finish();
81                 return;
82             }
83         } else if (requestCode == ENROLL_NEXT_BIOMETRIC_REQUEST) {
84             // See if we can still enroll a fingerprint
85             boolean canEnrollFinger = checkMaxEnrolled() == 0;
86             // If we came from the next biometric flow and a user has either
87             // finished or skipped, we will also finish.
88             if (resultCode == RESULT_SKIP || resultCode == RESULT_FINISHED) {
89                 // If user skips the enroll next biometric, we will also finish
90                 setResult(RESULT_FINISHED, data);
91                 finish();
92             } else if (resultCode == RESULT_CANCELED) {
93                 // Note that result_canceled comes from onBackPressed.
94                 // If we can enroll a finger, Stay on this page, else we cannot,
95                 // and finish entirely.
96                 if (!canEnrollFinger) {
97                     finish();
98                 }
99             } else {
100                 super.onActivityResult(requestCode, resultCode, data);
101             }
102             return;
103         }
104         super.onActivityResult(requestCode, resultCode, data);
105     }
106 
setFingerprintCount(Intent data)107     private Intent setFingerprintCount(Intent data) {
108         if (data == null) {
109             data = new Intent();
110         }
111         final FingerprintManager fpm = Utils.getFingerprintManagerOrNull(this);
112         if (fpm != null) {
113             int enrolled = fpm.getEnrolledFingerprints(mUserId).size();
114             data.putExtra(EXTRA_FINGERPRINT_ENROLLED_COUNT, enrolled);
115         }
116         return data;
117     }
118 
119     @Override
onCancelButtonClick(View view)120     protected void onCancelButtonClick(View view) {
121         final int resultCode;
122         Intent data;
123         if (isKeyguardSecure()) {
124             // If the keyguard is already set up securely (maybe the user added a backup screen
125             // lock and skipped fingerprint), return RESULT_SKIP directly.
126             if (!BiometricUtils.tryStartingNextBiometricEnroll(
127                     this, ENROLL_NEXT_BIOMETRIC_REQUEST, "cancel")) {
128                 resultCode = RESULT_SKIP;
129                 setResult(resultCode);
130                 finish();
131                 return;
132             }
133         } else {
134             resultCode = SetupSkipDialog.RESULT_SKIP;
135             data = setSkipPendingEnroll(null);
136             setResult(resultCode, data);
137             finish();
138         }
139 
140         // User has explicitly canceled enroll. Don't restart it automatically.
141     }
142 
isKeyguardSecure()143     private boolean isKeyguardSecure() {
144         return getSystemService(KeyguardManager.class).isKeyguardSecure();
145     }
146 
147     @Override
getMetricsCategory()148     public int getMetricsCategory() {
149         return SettingsEnums.FINGERPRINT_ENROLL_INTRO_SETUP;
150     }
151 }
152