AR/FR: Keyboard challenge takes precedence.

Tested manually by:
(1) Setting up a gmail account.
(2) Setting up a screen pattern.
(3) Going to Settings > System > Reset Options > Factory Wipe
(4) Hit the Erase button.
(5) Enter pattern into the Screenlock prompt.
(6) Notice the Google credential confirmation screen.

Prior to this change we would skip (5) if there was a Google account on
the device.

Test: make RunSettingsRoboTests ROBOTEST_FILTER=andorid.settings.MasterClear
Bug: 72694056

Change-Id: Ie07c678ecbc8361e8e1792f82efdfb1261db49fb
diff --git a/src/com/android/settings/MasterClear.java b/src/com/android/settings/MasterClear.java
index 687e645..47aa8a6 100644
--- a/src/com/android/settings/MasterClear.java
+++ b/src/com/android/settings/MasterClear.java
@@ -123,24 +123,34 @@
         return !((requestCode != KEYGUARD_REQUEST) && (requestCode != CREDENTIAL_CONFIRM_REQUEST));
     }
 
-    @VisibleForTesting
-    boolean isShowFinalConfirmation(int requestCode, int resultCode) {
-        return (resultCode == Activity.RESULT_OK) || (requestCode == CREDENTIAL_CONFIRM_REQUEST);
-    }
-
     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
+        onActivityResultInternal(requestCode, resultCode, data);
+    }
+
+    /*
+     * Internal method that allows easy testing without dealing with super references.
+     */
+    @VisibleForTesting
+    void onActivityResultInternal(int requestCode, int resultCode, Intent data) {
         if (!isValidRequestCode(requestCode)) {
             return;
         }
 
-        // If the user entered a valid keyguard trace, present the final
-        // confirmation prompt; otherwise, go back to the initial state.
-        if (isShowFinalConfirmation(requestCode, resultCode)) {
-            showFinalConfirmation();
-        } else {
+        if (resultCode != Activity.RESULT_OK) {
             establishInitialState();
+            return;
+        }
+
+        Intent intent = null;
+        // If returning from a Keyguard request, try to show an account confirmation request if
+        // applciable.
+        if (CREDENTIAL_CONFIRM_REQUEST != requestCode
+                && (intent = getAccountConfirmationIntent()) != null) {
+            showAccountCredentialConfirmation(intent);
+        } else {
+            showFinalConfirmation();
         }
     }
 
@@ -155,7 +165,12 @@
     }
 
     @VisibleForTesting
-    boolean tryShowAccountConfirmation() {
+    void showAccountCredentialConfirmation(Intent intent) {
+        startActivityForResult(intent, CREDENTIAL_CONFIRM_REQUEST);
+    }
+
+    @VisibleForTesting
+    Intent getAccountConfirmationIntent() {
         final Context context = getActivity();
         final String accountType = context.getString(R.string.account_type);
         final String packageName = context.getString(R.string.account_confirmation_package);
@@ -163,7 +178,8 @@
         if (TextUtils.isEmpty(accountType)
                 || TextUtils.isEmpty(packageName)
                 || TextUtils.isEmpty(className)) {
-            return false;
+            Log.i(TAG, "Resources not set for account confirmation.");
+            return null;
         }
         final AccountManager am = AccountManager.get(context);
         Account[] accounts = am.getAccountsByType(accountType);
@@ -179,12 +195,14 @@
                     && packageName.equals(resolution.activityInfo.packageName)) {
                 // Note that we need to check the packagename to make sure that an Activity resolver
                 // wasn't returned.
-                startActivityForResult(
-                    requestAccountConfirmation, CREDENTIAL_CONFIRM_REQUEST);
-                return true;
+                return requestAccountConfirmation;
+            } else {
+                Log.i(TAG, "Unable to resolve Activity: " + packageName + "/" + className);
             }
+        } else {
+            Log.d(TAG, "No " + accountType + " accounts installed!");
         }
-        return false;
+        return null;
     }
 
     /**
@@ -210,7 +228,14 @@
                 return;
             }
 
-            if (!tryShowAccountConfirmation() && !runKeyguardConfirmation(KEYGUARD_REQUEST)) {
+            if (runKeyguardConfirmation(KEYGUARD_REQUEST)) {
+                return;
+            }
+
+            Intent intent = getAccountConfirmationIntent();
+            if (intent != null) {
+                showAccountCredentialConfirmation(intent);
+            } else {
                 showFinalConfirmation();
             }
         }
@@ -228,7 +253,8 @@
      * time, then simply reuse the inflated views directly whenever we need
      * to change contents.
      */
-    private void establishInitialState() {
+    @VisibleForTesting
+    void establishInitialState() {
         mInitiateButton = (Button) mContentView.findViewById(R.id.initiate_master_clear);
         mInitiateButton.setOnClickListener(mInitiateListener);
         mExternalStorageContainer = mContentView.findViewById(R.id.erase_external_container);