Send metric intent back to SUW

Add metric intent if ChooseLockGeneric is triggered in SUW.
1. Save the result bundle data from ChooseLockGeneric to cache inside
   BiometricEnrollActivity and pass back to SUW after all finished.
2. Since fingerprint enrollment trigger points in SUW for
   fingerprint-only or fingerprint + faceAuth devices are all moved
   from SetupFingerprintEnrollIntroduction to BiometricEnrollActivity,
   remove deprecated code from SetupFingerprintEnrollIntroduction.

Bug: 235458700
Test: Manually test fingerprint enroll with SUW or w/o SUW
Test: Manually test fingerprint enroll with unicorn SUW
Test: atest BiometricEnrollActivityTest
Test: robo tests FingerprintEnrollIntroductionTest
Change-Id: I34b2884ab4c2c65d464d91eaef0f58c72dad438b
diff --git a/src/com/android/settings/biometrics/BiometricEnrollActivity.java b/src/com/android/settings/biometrics/BiometricEnrollActivity.java
index 79e2ea3..6b830c9 100644
--- a/src/com/android/settings/biometrics/BiometricEnrollActivity.java
+++ b/src/com/android/settings/biometrics/BiometricEnrollActivity.java
@@ -95,6 +95,8 @@
     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_PASS_THROUGH_EXTRAS_FROM_CHOSEN_LOCK_IN_SUW =
+            "pass_through_extras_from_chosen_lock_in_suw";
     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";
@@ -104,6 +106,7 @@
     private int mUserId = UserHandle.myUserId();
     private boolean mConfirmingCredentials;
     private boolean mFingerprintOnlyEnrolling;
+    private Bundle mPassThroughExtrasFromChosenLockInSuw = null;
     private boolean mIsEnrollActionLogged;
     private boolean mHasFeatureFace = false;
     private boolean mHasFeatureFingerprint = false;
@@ -134,6 +137,8 @@
                     SAVED_STATE_CONFIRMING_CREDENTIALS, false);
             mFingerprintOnlyEnrolling = savedInstanceState.getBoolean(
                     SAVED_STATE_FINGERPRINT_ONLY_ENROLLING, false);
+            mPassThroughExtrasFromChosenLockInSuw = savedInstanceState.getBundle(
+                    SAVED_STATE_PASS_THROUGH_EXTRAS_FROM_CHOSEN_LOCK_IN_SUW);
             mIsEnrollActionLogged = savedInstanceState.getBoolean(
                     SAVED_STATE_ENROLL_ACTION_LOGGED, false);
             mParentalOptions = savedInstanceState.getBundle(SAVED_STATE_PARENTAL_OPTIONS);
@@ -330,6 +335,8 @@
         super.onSaveInstanceState(outState);
         outState.putBoolean(SAVED_STATE_CONFIRMING_CREDENTIALS, mConfirmingCredentials);
         outState.putBoolean(SAVED_STATE_FINGERPRINT_ONLY_ENROLLING, mFingerprintOnlyEnrolling);
+        outState.putBundle(SAVED_STATE_PASS_THROUGH_EXTRAS_FROM_CHOSEN_LOCK_IN_SUW,
+                mPassThroughExtrasFromChosenLockInSuw);
         outState.putBoolean(SAVED_STATE_ENROLL_ACTION_LOGGED, mIsEnrollActionLogged);
         if (mParentalOptions != null) {
             outState.putBundle(SAVED_STATE_PARENTAL_OPTIONS, mParentalOptions);
@@ -343,6 +350,12 @@
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
 
+        if (isSuccessfulChooseCredential(requestCode, resultCode)
+                && data != null && data.getExtras() != null && data.getExtras().size() > 0
+                && WizardManagerHelper.isAnySetupWizard(getIntent())) {
+            mPassThroughExtrasFromChosenLockInSuw = data.getExtras();
+        }
+
         Log.d(TAG,
                 "onActivityResult(requestCode=" + requestCode + ", resultCode=" + resultCode + ")");
         // single enrollment is handled entirely by the launched activity
@@ -416,7 +429,7 @@
                     }
                 } else {
                     Log.d(TAG, "Unknown or cancelled parental consent");
-                    setResult(RESULT_CANCELED);
+                    setResult(RESULT_CANCELED, newResultIntent());
                     finish();
                 }
                 break;
@@ -452,7 +465,7 @@
                         launchFingerprintOnlyEnroll();
                     } else {
                         Log.d(TAG, "Unknown result for set/choose lock: " + resultCode);
-                        setResult(resultCode);
+                        setResult(resultCode, newResultIntent());
                         finish();
                     }
                     break;
@@ -480,25 +493,37 @@
                 finish();
             }
         } else {
-            setResult(resultCode);
+            setResult(resultCode, newResultIntent());
             finish();
         }
     }
 
+    @NonNull
     private Intent newResultIntent() {
         final Intent intent = new Intent();
-        final Bundle consentStatus = mParentalOptions.deepCopy();
-        intent.putExtra(EXTRA_PARENTAL_CONSENT_STATUS, consentStatus);
-        Log.v(TAG, "Result consent status: " + consentStatus);
+        if (mParentalOptionsRequired && mParentalOptions != null) {
+            final Bundle consentStatus = mParentalOptions.deepCopy();
+            intent.putExtra(EXTRA_PARENTAL_CONSENT_STATUS, consentStatus);
+            Log.v(TAG, "Result consent status: " + consentStatus);
+        }
+        if (mPassThroughExtrasFromChosenLockInSuw != null) {
+            intent.putExtras(mPassThroughExtrasFromChosenLockInSuw);
+        }
         return intent;
     }
 
     private static boolean isSuccessfulConfirmOrChooseCredential(int requestCode, int resultCode) {
-        final boolean okChoose = requestCode == REQUEST_CHOOSE_LOCK
+        return isSuccessfulChooseCredential(requestCode, resultCode)
+                || isSuccessfulConfirmCredential(requestCode, resultCode);
+    }
+
+    private static boolean isSuccessfulChooseCredential(int requestCode, int resultCode) {
+        return requestCode == REQUEST_CHOOSE_LOCK
                 && resultCode == ChooseLockPattern.RESULT_FINISHED;
-        final boolean okConfirm = requestCode == REQUEST_CONFIRM_LOCK
-                && resultCode == RESULT_OK;
-        return okChoose || okConfirm;
+    }
+
+    private static boolean isSuccessfulConfirmCredential(int requestCode, int resultCode) {
+        return requestCode == REQUEST_CONFIRM_LOCK && resultCode == RESULT_OK;
     }
 
     @Override
diff --git a/src/com/android/settings/biometrics/fingerprint/SetupFingerprintEnrollIntroduction.java b/src/com/android/settings/biometrics/fingerprint/SetupFingerprintEnrollIntroduction.java
index af25ecd..b313961 100644
--- a/src/com/android/settings/biometrics/fingerprint/SetupFingerprintEnrollIntroduction.java
+++ b/src/com/android/settings/biometrics/fingerprint/SetupFingerprintEnrollIntroduction.java
@@ -16,21 +16,16 @@
 
 package com.android.settings.biometrics.fingerprint;
 
-import android.app.Activity;
 import android.app.KeyguardManager;
 import android.app.settings.SettingsEnums;
 import android.content.Intent;
 import android.hardware.fingerprint.FingerprintManager;
-import android.os.Bundle;
-import android.os.UserHandle;
 import android.view.View;
 
-import com.android.internal.widget.LockPatternUtils;
 import com.android.settings.SetupWizardUtils;
 import com.android.settings.Utils;
 import com.android.settings.biometrics.BiometricUtils;
 import com.android.settings.password.ChooseLockSettingsHelper;
-import com.android.settings.password.SetupChooseLockGeneric;
 import com.android.settings.password.SetupSkipDialog;
 
 public class SetupFingerprintEnrollIntroduction extends FingerprintEnrollIntroduction {
@@ -40,24 +35,6 @@
     private static final String EXTRA_FINGERPRINT_ENROLLED_COUNT = "fingerprint_enrolled_count";
 
     private static final String KEY_LOCK_SCREEN_PRESENT = "wasLockScreenPresent";
-    private boolean mAlreadyHadLockScreenSetup = false;
-
-    @Override
-    protected void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        if (savedInstanceState == null) {
-            mAlreadyHadLockScreenSetup = isKeyguardSecure();
-        } else {
-            mAlreadyHadLockScreenSetup = savedInstanceState.getBoolean(
-                    KEY_LOCK_SCREEN_PRESENT, false);
-        }
-    }
-
-    @Override
-    protected void onSaveInstanceState(Bundle outState) {
-        super.onSaveInstanceState(outState);
-        outState.putBoolean(KEY_LOCK_SCREEN_PRESENT, mAlreadyHadLockScreenSetup);
-    }
 
     @Override
     protected Intent getEnrollingIntent() {
@@ -85,12 +62,6 @@
             }
         }
         if (requestCode == BIOMETRIC_FIND_SENSOR_REQUEST && isKeyguardSecure()) {
-            // if lock was already present, do not return intent data since it must have been
-            // reported in previous attempts
-            if (!mAlreadyHadLockScreenSetup) {
-                data = getMetricIntent(data);
-            }
-
             // Report fingerprint count if user adding a new fingerprint
             if (resultCode == RESULT_FINISHED) {
                 data = setFingerprintCount(data);
@@ -127,18 +98,6 @@
         super.onActivityResult(requestCode, resultCode, data);
     }
 
-    private Intent getMetricIntent(Intent data) {
-        if (data == null) {
-            data = new Intent();
-        }
-        LockPatternUtils lockPatternUtils = new LockPatternUtils(this);
-        data.putExtra(SetupChooseLockGeneric.
-                SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY,
-                lockPatternUtils.getKeyguardStoredPasswordQuality(UserHandle.myUserId()));
-
-        return data;
-    }
-
     private Intent setFingerprintCount(Intent data) {
         if (data == null) {
             data = new Intent();
@@ -161,8 +120,7 @@
             if (!BiometricUtils.tryStartingNextBiometricEnroll(
                     this, ENROLL_NEXT_BIOMETRIC_REQUEST, "cancel")) {
                 resultCode = RESULT_SKIP;
-                data = mAlreadyHadLockScreenSetup ? null : getMetricIntent(null);
-                setResult(resultCode, data);
+                setResult(resultCode);
                 finish();
                 return;
             }
@@ -176,18 +134,6 @@
         // User has explicitly canceled enroll. Don't restart it automatically.
     }
 
-    /**
-     * Propagate lock screen metrics if the user goes back from the fingerprint setup screen
-     * after having added lock screen to his device.
-     */
-    @Override
-    public void onBackPressed() {
-        if (!mAlreadyHadLockScreenSetup && isKeyguardSecure()) {
-            setResult(Activity.RESULT_CANCELED, getMetricIntent(null));
-        }
-        super.onBackPressed();
-    }
-
     private boolean isKeyguardSecure() {
         return getSystemService(KeyguardManager.class).isKeyguardSecure();
     }
diff --git a/tests/robotests/src/com/android/settings/biometrics/fingerprint/SetupFingerprintEnrollIntroductionTest.java b/tests/robotests/src/com/android/settings/biometrics/fingerprint/SetupFingerprintEnrollIntroductionTest.java
index e3b23ac..2aeda71 100644
--- a/tests/robotests/src/com/android/settings/biometrics/fingerprint/SetupFingerprintEnrollIntroductionTest.java
+++ b/tests/robotests/src/com/android/settings/biometrics/fingerprint/SetupFingerprintEnrollIntroductionTest.java
@@ -34,7 +34,6 @@
 import com.android.settings.R;
 import com.android.settings.biometrics.BiometricEnrollBase;
 import com.android.settings.biometrics.BiometricEnrollIntroduction;
-import com.android.settings.password.SetupChooseLockGeneric.SetupChooseLockGenericFragment;
 import com.android.settings.password.SetupSkipDialog;
 import com.android.settings.testutils.FakeFeatureFactory;
 import com.android.settings.testutils.shadow.ShadowFingerprintManager;
@@ -160,8 +159,6 @@
 
         ShadowActivity shadowActivity = Shadows.shadowOf(activity);
         assertThat(shadowActivity.getResultIntent()).isNotNull();
-        assertThat(shadowActivity.getResultIntent().hasExtra(
-            SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY)).isTrue();
     }
 
     @Test
@@ -191,8 +188,6 @@
 
         ShadowActivity shadowActivity = Shadows.shadowOf(activity);
         assertThat(shadowActivity.getResultIntent()).isNotNull();
-        assertThat(shadowActivity.getResultIntent().hasExtra(
-            SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY)).isTrue();
     }
 
     @Test
@@ -218,8 +213,6 @@
             BiometricEnrollBase.RESULT_FINISHED, null);
         ShadowActivity shadowActivity = Shadows.shadowOf(activity);
         assertThat(shadowActivity.getResultIntent()).isNotNull();
-        assertThat(shadowActivity.getResultIntent().hasExtra(
-                SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY)).isFalse();
     }
 
     @Test
@@ -260,8 +253,6 @@
         ShadowActivity shadowActivity = Shadows.shadowOf(activity);
         IntentForResult startedActivity = shadowActivity.getNextStartedActivityForResult();
         assertThat(startedActivity).isNotNull();
-        assertThat(startedActivity.intent.hasExtra(
-            SetupChooseLockGenericFragment.EXTRA_PASSWORD_QUALITY)).isFalse();
     }
 
     private ShadowKeyguardManager getShadowKeyguardManager() {