1 /*
2  * Copyright (C) 2023 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.activeunlock;
18 
19 import static android.hardware.biometrics.BiometricManager.Authenticators.BIOMETRIC_STRONG;
20 import static android.provider.Settings.ACTION_BIOMETRIC_ENROLL;
21 import static android.provider.Settings.EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED;
22 
23 import static com.android.settings.password.ChooseLockSettingsHelper.EXTRA_KEY_GK_PW_HANDLE;
24 
25 import android.app.settings.SettingsEnums;
26 import android.content.Intent;
27 import android.content.pm.PackageManager;
28 import android.os.Bundle;
29 import android.os.UserHandle;
30 import android.util.Log;
31 import android.view.View;
32 
33 import androidx.annotation.VisibleForTesting;
34 
35 import com.android.settings.R;
36 import com.android.settings.biometrics.BiometricEnrollActivity;
37 import com.android.settings.biometrics.BiometricEnrollBase;
38 import com.android.settings.biometrics.combination.CombinedBiometricStatusUtils;
39 
40 import com.google.android.setupcompat.template.FooterBarMixin;
41 import com.google.android.setupcompat.template.FooterButton;
42 
43 /**
44  * Activity which instructs the user to set up face or fingerprint unlock before setting the watch
45  * unlock.
46  */
47 public class ActiveUnlockRequireBiometricSetup extends BiometricEnrollBase {
48     private static final String TAG = "ActiveUnlockRequireBiometricSetup";
49 
50     @VisibleForTesting
51     static final int BIOMETRIC_ENROLL_REQUEST = 1001;
52     private static final int ACTIVE_UNLOCK_REQUEST = 1002;
53     private long mGkPwHandle;
54     private boolean mNextClicked;
55     private ActiveUnlockStatusUtils mActiveUnlockStatusUtils;
56 
57     @Override
onCreate(Bundle savedInstanceState)58     protected void onCreate(Bundle savedInstanceState) {
59         super.onCreate(savedInstanceState);
60         setContentView(R.layout.activeunlock_require_biometric_setup);
61 
62         mActiveUnlockStatusUtils = new ActiveUnlockStatusUtils(this);
63         mUserId = getIntent().getIntExtra(Intent.EXTRA_USER_ID, UserHandle.myUserId());
64         Log.i(TAG, "mUserId = " + mUserId);
65         mGkPwHandle = getIntent().getLongExtra(EXTRA_KEY_GK_PW_HANDLE, 0L);
66 
67         final PackageManager pm = getApplicationContext().getPackageManager();
68         boolean hasFeatureFace = pm.hasSystemFeature(PackageManager.FEATURE_FACE);
69         boolean hasFeatureFingerprint = pm.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT);
70         if (hasFeatureFace && hasFeatureFingerprint) {
71             setHeaderText(
72                     R.string.security_settings_activeunlock_require_face_fingerprint_setup_title);
73             setDescriptionText(
74                     R.string.security_settings_activeunlock_require_face_fingerprint_setup_message);
75         } else if (hasFeatureFingerprint) {
76             setHeaderText(R.string.security_settings_activeunlock_require_fingerprint_setup_title);
77             setDescriptionText(
78                     R.string.security_settings_activeunlock_require_fingerprint_setup_message);
79         } else if (hasFeatureFace) {
80             setHeaderText(R.string.security_settings_activeunlock_require_face_setup_title);
81             setDescriptionText(
82                     R.string.security_settings_activeunlock_require_face_setup_message);
83         }
84 
85         mFooterBarMixin = getLayout().getMixin(FooterBarMixin.class);
86         mFooterBarMixin.setSecondaryButton(
87                 new FooterButton.Builder(this)
88                         .setText(R.string.cancel)
89                         .setListener(this::onCancelClick)
90                         .setButtonType(FooterButton.ButtonType.CANCEL)
91                         .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Secondary)
92                         .build()
93         );
94 
95         mFooterBarMixin.setPrimaryButton(
96                 new FooterButton.Builder(this)
97                         .setText(R.string.security_settings_activeunlock_biometric_setup)
98                         .setListener(this::onNextButtonClick)
99                         .setButtonType(FooterButton.ButtonType.NEXT)
100                         .setTheme(com.google.android.setupdesign.R.style.SudGlifButton_Primary)
101                         .build()
102         );
103     }
104 
105     @Override
onBackPressed()106     public void onBackPressed() {
107         finish();
108     }
109 
onCancelClick(View view)110     private void onCancelClick(View view) {
111         finish();
112     }
113 
114     @Override
shouldFinishWhenBackgrounded()115     protected boolean shouldFinishWhenBackgrounded() {
116         return super.shouldFinishWhenBackgrounded() && !mNextClicked;
117     }
118 
119     @Override
onNextButtonClick(View view)120     protected void onNextButtonClick(View view) {
121         mNextClicked = true;
122         Intent intent = new Intent(this, BiometricEnrollActivity.InternalActivity.class);
123         intent.setAction(ACTION_BIOMETRIC_ENROLL);
124         intent.putExtra(EXTRA_BIOMETRIC_AUTHENTICATORS_ALLOWED, BIOMETRIC_STRONG);
125         intent.putExtra(Intent.EXTRA_USER_ID, mUserId);
126         intent.putExtra(EXTRA_KEY_GK_PW_HANDLE, mGkPwHandle);
127         startActivityForResult(intent, BIOMETRIC_ENROLL_REQUEST);
128     }
129 
130     @Override
onActivityResult(int requestCode, int resultCode, Intent data)131     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
132         super.onActivityResult(requestCode, resultCode, data);
133 
134         if (requestCode == BIOMETRIC_ENROLL_REQUEST && resultCode != RESULT_CANCELED) {
135             CombinedBiometricStatusUtils combinedBiometricStatusUtils =
136                     new CombinedBiometricStatusUtils(this, mUserId);
137             if (combinedBiometricStatusUtils.hasEnrolled()) {
138                 Intent activeUnlockIntent = mActiveUnlockStatusUtils.getIntent();
139                 if (activeUnlockIntent != null) {
140                     startActivityForResult(activeUnlockIntent, ACTIVE_UNLOCK_REQUEST);
141                 }
142             }
143         }
144         mNextClicked = false;
145         finish();
146     }
147 
148     @Override
getMetricsCategory()149     public int getMetricsCategory() {
150         return SettingsEnums.ACTIVE_UNLOCK_REQUIRE_BIOMETRIC_SETUP;
151     }
152 }
153