Run lock before fingerprint enroll

Run setOrConfirmCredentialsNow() before enrolling fingerprint for
fingerprint-only device.

Bug: 237433373
Test: atest BiometricEnrollActivityTest
Test: Manually test SUW flows for fingerprint-only device
      1. check confirm pin case
      2. check choose pin case
      3. enable always_finish_activities, check confirm pin case
      4. enable always_finish_activities, check choose pin case
Change-Id: I68da429145835fc79f83f3292749088dc25fdeee
diff --git a/src/com/android/settings/biometrics/BiometricEnrollActivity.java b/src/com/android/settings/biometrics/BiometricEnrollActivity.java
index e682934..79e2ea3 100644
--- a/src/com/android/settings/biometrics/BiometricEnrollActivity.java
+++ b/src/com/android/settings/biometrics/BiometricEnrollActivity.java
@@ -93,6 +93,8 @@
     public static final String EXTRA_PARENTAL_CONSENT_STATUS = "consent_status";
 
     private static final String SAVED_STATE_CONFIRMING_CREDENTIALS = "confirming_credentials";
+    private static final String SAVED_STATE_FINGERPRINT_ONLY_ENROLLING =
+            "fingerprint_only_enrolling";
     private static final String SAVED_STATE_ENROLL_ACTION_LOGGED = "enroll_action_logged";
     private static final String SAVED_STATE_PARENTAL_OPTIONS = "enroll_preferences";
     private static final String SAVED_STATE_GK_PW_HANDLE = "gk_pw_handle";
@@ -101,6 +103,7 @@
 
     private int mUserId = UserHandle.myUserId();
     private boolean mConfirmingCredentials;
+    private boolean mFingerprintOnlyEnrolling;
     private boolean mIsEnrollActionLogged;
     private boolean mHasFeatureFace = false;
     private boolean mHasFeatureFingerprint = false;
@@ -129,6 +132,8 @@
         if (savedInstanceState != null) {
             mConfirmingCredentials = savedInstanceState.getBoolean(
                     SAVED_STATE_CONFIRMING_CREDENTIALS, false);
+            mFingerprintOnlyEnrolling = savedInstanceState.getBoolean(
+                    SAVED_STATE_FINGERPRINT_ONLY_ENROLLING, false);
             mIsEnrollActionLogged = savedInstanceState.getBoolean(
                     SAVED_STATE_ENROLL_ACTION_LOGGED, false);
             mParentalOptions = savedInstanceState.getBundle(SAVED_STATE_PARENTAL_OPTIONS);
@@ -302,7 +307,11 @@
                 setOrConfirmCredentialsNow();
             }
         } else if (canUseFingerprint) {
-            launchFingerprintOnlyEnroll();
+            if (mGkPwHandle != null) {
+                launchFingerprintOnlyEnroll();
+            } else {
+                setOrConfirmCredentialsNow();
+            }
         } else if (canUseFace) {
             launchFaceOnlyEnroll();
         } else { // no modalities available
@@ -320,6 +329,7 @@
     protected void onSaveInstanceState(@NonNull Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putBoolean(SAVED_STATE_CONFIRMING_CREDENTIALS, mConfirmingCredentials);
+        outState.putBoolean(SAVED_STATE_FINGERPRINT_ONLY_ENROLLING, mFingerprintOnlyEnrolling);
         outState.putBoolean(SAVED_STATE_ENROLL_ACTION_LOGGED, mIsEnrollActionLogged);
         if (mParentalOptions != null) {
             outState.putBundle(SAVED_STATE_PARENTAL_OPTIONS, mParentalOptions);
@@ -432,11 +442,14 @@
                     mConfirmingCredentials = false;
                     final boolean isOk =
                             isSuccessfulConfirmOrChooseCredential(requestCode, resultCode);
-                    // single modality enrollment requests confirmation directly
+                    // single face enrollment requests confirmation directly
                     // via BiometricEnrollBase#onCreate and should never get here
                     if (isOk && mHasFeatureFace && mHasFeatureFingerprint) {
                         updateGatekeeperPasswordHandle(data);
                         launchFaceAndFingerprintEnroll();
+                    } else if (isOk && mHasFeatureFingerprint) {
+                        updateGatekeeperPasswordHandle(data);
+                        launchFingerprintOnlyEnroll();
                     } else {
                         Log.d(TAG, "Unknown result for set/choose lock: " + resultCode);
                         setResult(resultCode);
@@ -444,6 +457,7 @@
                     }
                     break;
                 case REQUEST_SINGLE_ENROLL:
+                    mFingerprintOnlyEnrolling = false;
                     finishOrLaunchHandToParent(resultCode);
                     break;
                 default:
@@ -572,15 +586,18 @@
     }
 
     private void launchFingerprintOnlyEnroll() {
-        final Intent intent;
-        // ChooseLockGeneric can request to start fingerprint enroll bypassing the intro screen.
-        if (getIntent().getBooleanExtra(EXTRA_SKIP_INTRO, false)
-                && this instanceof InternalActivity) {
-            intent = BiometricUtils.getFingerprintFindSensorIntent(this, getIntent());
-        } else {
-            intent = BiometricUtils.getFingerprintIntroIntent(this, getIntent());
+        if (!mFingerprintOnlyEnrolling) {
+            mFingerprintOnlyEnrolling = true;
+            final Intent intent;
+            // ChooseLockGeneric can request to start fingerprint enroll bypassing the intro screen.
+            if (getIntent().getBooleanExtra(EXTRA_SKIP_INTRO, false)
+                    && this instanceof InternalActivity) {
+                intent = BiometricUtils.getFingerprintFindSensorIntent(this, getIntent());
+            } else {
+                intent = BiometricUtils.getFingerprintIntroIntent(this, getIntent());
+            }
+            launchSingleSensorEnrollActivity(intent, REQUEST_SINGLE_ENROLL);
         }
-        launchSingleSensorEnrollActivity(intent, REQUEST_SINGLE_ENROLL);
     }
 
     private void launchFaceOnlyEnroll() {