Fix enrolled face id can't be deleted when changing device rotation
then pressing back key

Handling the back key to enable remove-button state when face
deleting dialog appears.

Bug: 323277209

Test: manually check the reproducible steps:

Pre-Condition (Skip if not available):
1. make sure face unlock has been set
2. enable auto-rotate

STEPS TO REPRODUCE:
1. go to "Face unlock" page
2. click "Delete face model"
3. rotate to landscape mode
4. swipe back then observe

Change-Id: I5e3a06229390609deb42544f545ec9a21d374245
diff --git a/src/com/android/settings/biometrics/face/FaceSettingsRemoveButtonPreferenceController.java b/src/com/android/settings/biometrics/face/FaceSettingsRemoveButtonPreferenceController.java
index 797364b..ae5b62b 100644
--- a/src/com/android/settings/biometrics/face/FaceSettingsRemoveButtonPreferenceController.java
+++ b/src/com/android/settings/biometrics/face/FaceSettingsRemoveButtonPreferenceController.java
@@ -29,7 +29,9 @@
 import android.view.View;
 import android.widget.Button;
 import android.widget.Toast;
+import android.window.OnBackInvokedCallback;
 
+import androidx.annotation.Nullable;
 import androidx.annotation.VisibleForTesting;
 import androidx.preference.Preference;
 
@@ -57,9 +59,14 @@
     private static final String TAG = "FaceSettings/Remove";
     static final String KEY = "security_settings_face_delete_faces_container";
 
-    public static class ConfirmRemoveDialog extends InstrumentedDialogFragment {
+    public static class ConfirmRemoveDialog extends InstrumentedDialogFragment
+            implements OnBackInvokedCallback {
         private static final String KEY_IS_CONVENIENCE = "is_convenience";
         private DialogInterface.OnClickListener mOnClickListener;
+        @Nullable
+        private AlertDialog mDialog = null;
+        @Nullable
+        private Preference mFaceUnlockPreference = null;
 
         /** Returns the new instance of the class */
         public static ConfirmRemoveDialog newInstance(boolean isConvenience) {
@@ -99,14 +106,41 @@
                     .setMessage(dialogMessageRes)
                     .setPositiveButton(R.string.delete, mOnClickListener)
                     .setNegativeButton(R.string.cancel, mOnClickListener);
-            AlertDialog dialog = builder.create();
-            dialog.setCanceledOnTouchOutside(false);
-            return dialog;
+            mDialog = builder.create();
+            mDialog.setCanceledOnTouchOutside(false);
+            mDialog.getOnBackInvokedDispatcher().registerOnBackInvokedCallback(0, this);
+            return mDialog;
         }
 
         public void setOnClickListener(DialogInterface.OnClickListener listener) {
             mOnClickListener = listener;
         }
+
+        public void setPreference(@Nullable Preference preference) {
+            mFaceUnlockPreference = preference;
+        }
+
+        public void unregisterOnBackInvokedCallback() {
+            if (mDialog != null) {
+                mDialog.getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(this);
+            }
+        }
+
+        @Override
+        public void onBackInvoked() {
+            if (mDialog != null) {
+                mDialog.cancel();
+            }
+            unregisterOnBackInvokedCallback();
+
+            if (mFaceUnlockPreference != null) {
+                final Button removeButton = ((LayoutPreference) mFaceUnlockPreference)
+                        .findViewById(R.id.security_settings_face_settings_remove_button);
+                if (removeButton != null) {
+                    removeButton.setEnabled(true);
+                }
+            }
+        }
     }
 
     interface Listener {
@@ -171,6 +205,13 @@
                 mButton.setEnabled(true);
                 mRemoving = false;
             }
+
+            final ConfirmRemoveDialog removeDialog =
+                    (ConfirmRemoveDialog) mActivity.getSupportFragmentManager()
+                            .findFragmentByTag(ConfirmRemoveDialog.class.getName());
+            if (removeDialog != null) {
+                removeDialog.unregisterOnBackInvokedCallback();
+            }
         }
     };
 
@@ -210,6 +251,7 @@
                 (ConfirmRemoveDialog) mActivity.getSupportFragmentManager()
                         .findFragmentByTag(ConfirmRemoveDialog.class.getName());
         if (removeDialog != null) {
+            removeDialog.setPreference(mPreference);
             mRemoving = true;
             removeDialog.setOnClickListener(mOnConfirmDialogClickListener);
         }