Check biometrics availability when relevant

Check the availability of biometrics when we need to know, waiting until
then to decide the presence of ScreenLockActivity, rather than checking
once during initialization and permanently disabling pages of the setup
wizard. Otherwise, a race condition may cause the system to report that
the hardware is unavailable, even though it will be ready when needed.

Reorder pages such that biometrics comes first, allowing screen lock
to act as the fallback.

Issue: calyxos#2544
Change-Id: I654ce25569b983035f1df453623c7a4acd54931d
diff --git a/res/raw/lineage_wizard_script.xml b/res/raw/lineage_wizard_script.xml
index ac4f5a8..7c87abd 100644
--- a/res/raw/lineage_wizard_script.xml
+++ b/res/raw/lineage_wizard_script.xml
@@ -71,18 +71,18 @@
     <WizardAction
         wizard:uri="intent:#Intent;action=org.lineageos.setupwizard.LINEAGE_SETTINGS;end"
         id="lineage_settings">
-        <result wizard:action="lockscreen_settings" />
-    </WizardAction>
-
-    <WizardAction
-        wizard:uri="intent:#Intent;action=org.lineageos.setupwizard.LINEAGE_LOCKSCREEN_SETTINGS;end"
-        id="lockscreen_settings">
         <result wizard:action="biometric_settings" />
     </WizardAction>
 
     <WizardAction
         wizard:uri="intent:#Intent;action=org.lineageos.setupwizard.LINEAGE_BIOMETRIC_SETTINGS;end"
         id="biometric_settings">
+        <result wizard:action="lockscreen_settings" />
+    </WizardAction>
+
+    <WizardAction
+        wizard:uri="intent:#Intent;action=org.lineageos.setupwizard.LINEAGE_LOCKSCREEN_SETTINGS;end"
+        id="lockscreen_settings">
         <result wizard:action="theme_settings" />
     </WizardAction>
 
diff --git a/res/raw/lineage_wizard_script_user.xml b/res/raw/lineage_wizard_script_user.xml
index f0ff14e..15325c6 100644
--- a/res/raw/lineage_wizard_script_user.xml
+++ b/res/raw/lineage_wizard_script_user.xml
@@ -23,18 +23,18 @@
     <WizardAction
         wizard:uri="intent:#Intent;action=org.lineageos.setupwizard.LINEAGE_LOCATION_SETTINGS;end"
         id="location_settings">
-        <result wizard:action="lockscreen_settings" />
-    </WizardAction>
-
-    <WizardAction
-        wizard:uri="intent:#Intent;action=org.lineageos.setupwizard.LINEAGE_LOCKSCREEN_SETTINGS;end"
-        id="lockscreen_settings">
         <result wizard:action="biometric_settings" />
     </WizardAction>
 
     <WizardAction
         wizard:uri="intent:#Intent;action=org.lineageos.setupwizard.LINEAGE_BIOMETRIC_SETTINGS;end"
         id="biometric_settings">
+        <result wizard:action="lockscreen_settings" />
+    </WizardAction>
+
+    <WizardAction
+        wizard:uri="intent:#Intent;action=org.lineageos.setupwizard.LINEAGE_LOCKSCREEN_SETTINGS;end"
+        id="lockscreen_settings">
         <result wizard:action="theme_settings" />
     </WizardAction>
 
diff --git a/src/org/lineageos/setupwizard/BiometricActivity.java b/src/org/lineageos/setupwizard/BiometricActivity.java
index dcfc45f..e72c449 100644
--- a/src/org/lineageos/setupwizard/BiometricActivity.java
+++ b/src/org/lineageos/setupwizard/BiometricActivity.java
@@ -6,13 +6,24 @@
 
 package org.lineageos.setupwizard;
 
+import static com.google.android.setupcompat.util.ResultCodes.RESULT_SKIP;
+
 import android.content.Intent;
 import android.provider.Settings;
 
+import org.lineageos.setupwizard.util.SetupWizardUtils;
+
 public class BiometricActivity extends SubBaseActivity {
 
     @Override
     protected void onStartSubactivity() {
+        if (!SetupWizardUtils.hasBiometric(this)) {
+            SetupWizardUtils.enableComponent(this, ScreenLockActivity.class);
+            finishAction(RESULT_SKIP);
+            return;
+        } else {
+            SetupWizardUtils.disableComponent(this, ScreenLockActivity.class);
+        }
         Intent intent = new Intent(Settings.ACTION_BIOMETRIC_ENROLL);
         startSubactivity(intent);
     }
diff --git a/src/org/lineageos/setupwizard/util/SetupWizardUtils.java b/src/org/lineageos/setupwizard/util/SetupWizardUtils.java
index 7b10a1f..e114269 100644
--- a/src/org/lineageos/setupwizard/util/SetupWizardUtils.java
+++ b/src/org/lineageos/setupwizard/util/SetupWizardUtils.java
@@ -51,10 +51,8 @@
 import android.util.Log;
 
 import org.lineageos.setupwizard.BaseSetupWizardActivity;
-import org.lineageos.setupwizard.BiometricActivity;
 import org.lineageos.setupwizard.BluetoothSetupActivity;
 import org.lineageos.setupwizard.NetworkSetupActivity;
-import org.lineageos.setupwizard.ScreenLockActivity;
 import org.lineageos.setupwizard.SetupWizardApp;
 import org.lineageos.setupwizard.SimMissingActivity;
 
@@ -240,11 +238,6 @@
         if (!hasLeanback(context) || isBluetoothDisabled()) {
             disableComponent(context, BluetoothSetupActivity.class);
         }
-        if (!hasBiometric(context)) {
-            disableComponent(context, BiometricActivity.class);
-        } else {
-            disableComponent(context, ScreenLockActivity.class);
-        }
         if (!hasTelephony(context)) {
             disableComponent(context, SimMissingActivity.class);
         }