Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 76c19652 authored by Hongyu Long's avatar Hongyu Long
Browse files

a11y: Add cancel listener to dialog delegate

This CL sets the cancel listener for the dialog, so that when clicking
outside, the negative button, or pressing ESC, the cancel listener will
call the dialog dismissal finally.

Bug: b/425722546
Flag: com.android.hardware.input.enable_magnify_magnification_key_gesture_dialog
Test: manual
Change-Id: Ica5f6e46c0196d722c2b67cabf6db91fbe82afe5
parent d679a5ee
Loading
Loading
Loading
Loading
+16 −12
Original line number Diff line number Diff line
@@ -81,6 +81,9 @@ constructor(
        /** Called when the positive button is clicked. */
        fun onPositiveButtonClick(info: KeyGestureConfirmInfo)

        /** Called when the dialog is canceled. */
        fun onDialogCanceled(info: KeyGestureConfirmInfo) {}

        /** Called when the dialog is dismissed for any reason. */
        fun onDialogDismissed(info: KeyGestureConfirmInfo) {}
    }
@@ -156,26 +159,20 @@ constructor(
            R.string.accessibility_key_gesture_magnification_dialog_negative_button_text
        override val positiveButtonTextId: Int =
            R.string.accessibility_key_gesture_magnification_dialog_positive_button_text
        private var magnificationShortcutConfirmed = false

        override fun onDialogCreated(info: KeyGestureConfirmInfo) {
            interactor.enableShortcutsForTargets(enable = true, info.targetName)
            magnificationShortcutConfirmed = false
            interactor.enableMagnificationAndZoomIn(info.displayId)
        }

        override fun onPositiveButtonClick(info: KeyGestureConfirmInfo) {
            magnificationShortcutConfirmed = true
        }
        override fun onPositiveButtonClick(info: KeyGestureConfirmInfo) {}

        override fun onDialogDismissed(info: KeyGestureConfirmInfo) {
            if (!magnificationShortcutConfirmed) {
        override fun onDialogCanceled(info: KeyGestureConfirmInfo) {
            // We need to remove the shortcut target if the user clicks the negative
            // button or clicks outside of the dialog.
            interactor.enableShortcutsForTargets(enable = false, info.targetName)
        }
    }
    }

    private fun getDialogDelegate(keyGestureType: Int): DialogBehaviorDelegate {
        return when (keyGestureType) {
@@ -237,7 +234,13 @@ constructor(
                            )
                        },
                        negativeButton = {
                            PlatformOutlinedButton(onClick = { dialog.dismiss() }) {
                            PlatformOutlinedButton(
                                onClick = {
                                    // We need explicitly call `cancel` here; otherwise, clicking
                                    // the negative button, the cancel listener won't be triggered.
                                    dialog.cancel()
                                }
                            ) {
                                Text(stringResource(id = delegate.negativeButtonTextId))
                            }
                        },
@@ -258,6 +261,7 @@ constructor(
        currentDialog?.let { dialog ->
            dialogType = keyGestureConfirmInfo.keyGestureType
            delegate.onDialogCreated(keyGestureConfirmInfo)
            dialog.setOnCancelListener { delegate.onDialogCanceled(keyGestureConfirmInfo) }
            dialog.setOnDismissListener {
                delegate.onDialogDismissed(keyGestureConfirmInfo)
                currentDialog = null