Fix biometric activities launched twice

Save launched state

Bug: 401461494
Test: After pressing Done in biometric confirmation page, intro page
      don not show again
Flag: EXEMPT bug fix
Change-Id: I7dfca8e2a6752a20de0429b61f8d9885fdd12e47
diff --git a/src/com/android/settings/biometrics/face/FaceEnroll.kt b/src/com/android/settings/biometrics/face/FaceEnroll.kt
index 2ed628d..74f1613 100644
--- a/src/com/android/settings/biometrics/face/FaceEnroll.kt
+++ b/src/com/android/settings/biometrics/face/FaceEnroll.kt
@@ -23,7 +23,6 @@
 import androidx.appcompat.app.AppCompatActivity
 import com.android.settings.biometrics.BiometricEnrollBase.RESULT_FINISHED
 import com.android.settings.biometrics.combination.CombinedBiometricStatusUtils
-
 import com.android.settings.overlay.FeatureFactory.Companion.featureFactory
 
 class FaceEnroll: AppCompatActivity() {
@@ -39,18 +38,33 @@
     private val enrollActivityProvider: FaceEnrollActivityClassProvider
         get() = featureFactory.faceFeatureProvider.enrollActivityClassProvider
 
+    private var isLaunched = false
+
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
 
-        /**
-         *  Logs the next activity to be launched, creates an intent for that activity,
-         *  adds flags to forward the result, includes any existing extras from the current intent,
-         *  starts the new activity and then finishes the current one
-         */
-        Log.d("FaceEnroll", "forward to $nextActivityClass")
-        val nextIntent = Intent(this, nextActivityClass)
-        nextIntent.putExtras(intent)
-        startActivityForResult(nextIntent, 0)
+        if (savedInstanceState != null) {
+            isLaunched = savedInstanceState.getBoolean(KEY_IS_LAUNCHED, isLaunched)
+        }
+
+        if (!isLaunched) {
+            /**
+             *  Logs the next activity to be launched, creates an intent for that activity,
+             *  adds flags to forward the result, includes any existing extras from the current intent,
+             *  starts the new activity and then finishes the current one
+             */
+            Log.d("FaceEnroll", "forward to $nextActivityClass")
+            val nextIntent = Intent(this, nextActivityClass)
+            nextIntent.putExtras(intent)
+            startActivityForResult(nextIntent, 0)
+
+            isLaunched = true
+        }
+    }
+
+    override fun onSaveInstanceState(outState: Bundle) {
+        outState.putBoolean(KEY_IS_LAUNCHED, isLaunched)
+        super.onSaveInstanceState(outState)
     }
 
     override fun onActivityResult(
@@ -60,6 +74,7 @@
         caller: ComponentCaller
     ) {
         super.onActivityResult(requestCode, resultCode, data, caller)
+        isLaunched = false
         if (intent.getBooleanExtra(
                 CombinedBiometricStatusUtils.EXTRA_LAUNCH_FROM_SAFETY_SOURCE_ISSUE, false)
             && resultCode != RESULT_FINISHED) {
@@ -68,4 +83,8 @@
         setResult(resultCode, data)
         finish()
     }
-}
\ No newline at end of file
+
+    private companion object {
+        const val KEY_IS_LAUNCHED = "isLaunched"
+    }
+}
diff --git a/src/com/android/settings/biometrics/fingerprint/FingerprintEnroll.kt b/src/com/android/settings/biometrics/fingerprint/FingerprintEnroll.kt
index 795be13..229f6c4 100644
--- a/src/com/android/settings/biometrics/fingerprint/FingerprintEnroll.kt
+++ b/src/com/android/settings/biometrics/fingerprint/FingerprintEnroll.kt
@@ -62,18 +62,33 @@
     protected val enrollActivityProvider: FingerprintEnrollActivityClassProvider
         get() = featureFactory.fingerprintFeatureProvider.getEnrollActivityClassProvider(this)
 
+    private var isLaunched = false
+
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
 
-        /**
-         *  Logs the next activity to be launched, creates an intent for that activity,
-         *  adds flags to forward the result, includes any existing extras from the current intent,
-         *  starts the new activity and then finishes the current one
-         */
-        Log.d("FingerprintEnroll", "forward to $nextActivityClass")
-        val nextIntent = Intent(this, nextActivityClass)
-        nextIntent.putExtras(intent)
-        startActivityForResult(nextIntent, 0)
+        if (savedInstanceState != null) {
+            isLaunched = savedInstanceState.getBoolean(KEY_IS_LAUNCHED, isLaunched)
+        }
+
+        if (!isLaunched) {
+            /**
+             *  Logs the next activity to be launched, creates an intent for that activity,
+             *  adds flags to forward the result, includes any existing extras from the current intent,
+             *  starts the new activity and then finishes the current one
+             */
+            Log.d("FingerprintEnroll", "forward to $nextActivityClass")
+            val nextIntent = Intent(this, nextActivityClass)
+            nextIntent.putExtras(intent)
+            startActivityForResult(nextIntent, 0)
+
+            isLaunched = true
+        }
+    }
+
+    override fun onSaveInstanceState(outState: Bundle) {
+        outState.putBoolean(KEY_IS_LAUNCHED, isLaunched)
+        super.onSaveInstanceState(outState)
     }
 
     override fun onActivityResult(
@@ -83,6 +98,7 @@
         caller: ComponentCaller
     ) {
         super.onActivityResult(requestCode, resultCode, data, caller)
+        isLaunched = false
         if (intent.getBooleanExtra(
                 CombinedBiometricStatusUtils.EXTRA_LAUNCH_FROM_SAFETY_SOURCE_ISSUE, false)
             && resultCode != BiometricEnrollBase.RESULT_FINISHED
@@ -92,4 +108,8 @@
         setResult(resultCode, data)
         finish()
     }
+
+    private companion object {
+        const val KEY_IS_LAUNCHED = "isLaunched"
+    }
 }
\ No newline at end of file