• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Activity;
20 import android.app.KeyguardManager;
21 import android.app.admin.DevicePolicyManager;
22 import android.app.settings.SettingsEnums;
23 import android.content.Intent;
24 import android.hardware.fingerprint.FingerprintManager;
25 import android.os.Bundle;
26 import android.os.UserHandle;
27 import android.os.storage.StorageManager;
28 import android.view.View;
29 import android.widget.TextView;
30 
31 import com.android.internal.widget.LockPatternUtils;
32 import com.android.settings.R;
33 import com.android.settings.SetupWizardUtils;
34 import com.android.settings.Utils;
35 import com.android.settings.password.ChooseLockGeneric.ChooseLockGenericFragment;
36 import com.android.settings.password.SetupChooseLockGeneric;
37 import com.android.settings.password.SetupSkipDialog;
38 
39 import com.google.android.setupcompat.template.FooterButton;
40 
41 public class SetupFingerprintEnrollIntroduction extends FingerprintEnrollIntroduction {
42     /**
43      * Returns the number of fingerprint enrolled.
44      */
45     private static final String EXTRA_FINGERPRINT_ENROLLED_COUNT = "fingerprint_enrolled_count";
46 
47     private static final String KEY_LOCK_SCREEN_PRESENT = "wasLockScreenPresent";
48     private boolean mAlreadyHadLockScreenSetup = false;
49 
50     @Override
onCreate(Bundle savedInstanceState)51     protected void onCreate(Bundle savedInstanceState) {
52         super.onCreate(savedInstanceState);
53         if (savedInstanceState == null) {
54             mAlreadyHadLockScreenSetup = isKeyguardSecure();
55         } else {
56             mAlreadyHadLockScreenSetup = savedInstanceState.getBoolean(
57                     KEY_LOCK_SCREEN_PRESENT, false);
58         }
59     }
60 
61     @Override
onSaveInstanceState(Bundle outState)62     protected void onSaveInstanceState(Bundle outState) {
63         super.onSaveInstanceState(outState);
64         outState.putBoolean(KEY_LOCK_SCREEN_PRESENT, mAlreadyHadLockScreenSetup);
65     }
66 
67     @Override
getChooseLockIntent()68     protected Intent getChooseLockIntent() {
69         Intent intent = new Intent(this, SetupChooseLockGeneric.class);
70 
71         if (StorageManager.isFileEncryptedNativeOrEmulated()) {
72             intent.putExtra(
73                     LockPatternUtils.PASSWORD_TYPE_KEY,
74                     DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
75             intent.putExtra(ChooseLockGenericFragment.EXTRA_SHOW_OPTIONS_BUTTON, true);
76         }
77         SetupWizardUtils.copySetupExtras(getIntent(), intent);
78         return intent;
79     }
80 
81     @Override
getEnrollingIntent()82     protected Intent getEnrollingIntent() {
83         final Intent intent = new Intent(this, SetupFingerprintEnrollFindSensor.class);
84         SetupWizardUtils.copySetupExtras(getIntent(), intent);
85         return intent;
86     }
87 
88     @Override
initViews()89     protected void initViews() {
90         super.initViews();
91 
92         TextView description = (TextView) findViewById(R.id.sud_layout_description);
93         description.setText(
94                 R.string.security_settings_fingerprint_enroll_introduction_message_setup);
95 
96         FooterButton nextButton = getNextButton();
97         nextButton.setText(
98                 this, R.string.security_settings_fingerprint_enroll_introduction_continue_setup);
99 
100         final FooterButton cancelButton = getCancelButton();
101         cancelButton.setText(
102                 this, R.string.security_settings_fingerprint_enroll_introduction_cancel_setup);
103     }
104 
105     @Override
onActivityResult(int requestCode, int resultCode, Intent data)106     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
107         // if lock was already present, do not return intent data since it must have been
108         // reported in previous attempts
109         if (requestCode == BIOMETRIC_FIND_SENSOR_REQUEST && isKeyguardSecure()) {
110             if(!mAlreadyHadLockScreenSetup) {
111                 data = getMetricIntent(data);
112             }
113 
114             // Report fingerprint count if user adding a new fingerprint
115             if(resultCode == RESULT_FINISHED) {
116                 data = setFingerprintCount(data);
117             }
118         }
119 
120         super.onActivityResult(requestCode, resultCode, data);
121     }
122 
getMetricIntent(Intent data)123     private Intent getMetricIntent(Intent data) {
124         if (data == null) {
125             data = new Intent();
126         }
127         LockPatternUtils lockPatternUtils = new LockPatternUtils(this);
128         data.putExtra(SetupChooseLockGeneric.
129                 SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY,
130                 lockPatternUtils.getKeyguardStoredPasswordQuality(UserHandle.myUserId()));
131 
132         return data;
133     }
134 
setFingerprintCount(Intent data)135     private Intent setFingerprintCount(Intent data) {
136         if (data == null) {
137             data = new Intent();
138         }
139         final FingerprintManager fpm = Utils.getFingerprintManagerOrNull(this);
140         if (fpm != null) {
141             int enrolled = fpm.getEnrolledFingerprints(mUserId).size();
142             data.putExtra(EXTRA_FINGERPRINT_ENROLLED_COUNT, enrolled);
143         }
144         return data;
145     }
146 
147     @Override
onCancelButtonClick(View view)148     protected void onCancelButtonClick(View view) {
149         if (isKeyguardSecure()) {
150             // If the keyguard is already set up securely (maybe the user added a backup screen
151             // lock and skipped fingerprint), return RESULT_SKIP directly.
152             setResult(RESULT_SKIP, mAlreadyHadLockScreenSetup ? null : getMetricIntent(null));
153             finish();
154         } else {
155             setResult(SetupSkipDialog.RESULT_SKIP);
156             finish();
157         }
158     }
159 
160     /**
161      * Propagate lock screen metrics if the user goes back from the fingerprint setup screen
162      * after having added lock screen to his device.
163      */
164     @Override
onBackPressed()165     public void onBackPressed() {
166         if (!mAlreadyHadLockScreenSetup && isKeyguardSecure()) {
167             setResult(Activity.RESULT_CANCELED, getMetricIntent(null));
168         }
169         super.onBackPressed();
170     }
171 
isKeyguardSecure()172     private boolean isKeyguardSecure() {
173         return getSystemService(KeyguardManager.class).isKeyguardSecure();
174     }
175 
176     @Override
getMetricsCategory()177     public int getMetricsCategory() {
178         return SettingsEnums.FINGERPRINT_ENROLL_INTRO_SETUP;
179     }
180 }
181