From 71a2a4d31a28e4a11a26fad91f9899cd7cea229d Mon Sep 17 00:00:00 2001
From: Benoit Marty <benoit@matrix.org>
Date: Wed, 10 May 2023 16:43:54 +0200
Subject: [PATCH] a11y: add custom action to be able to close the alert.

---
 .../app/features/popup/PopupAlertManager.kt   | 24 +++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/vector/src/main/java/im/vector/app/features/popup/PopupAlertManager.kt b/vector/src/main/java/im/vector/app/features/popup/PopupAlertManager.kt
index d3c6e83589..29aa623c9b 100644
--- a/vector/src/main/java/im/vector/app/features/popup/PopupAlertManager.kt
+++ b/vector/src/main/java/im/vector/app/features/popup/PopupAlertManager.kt
@@ -21,9 +21,11 @@ import android.os.Handler
 import android.os.Looper
 import android.view.View
 import android.view.WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
+import androidx.core.view.ViewCompat
 import com.tapadoo.alerter.Alerter
 import im.vector.app.R
 import im.vector.app.core.platform.VectorBaseActivity
+import im.vector.app.core.resources.StringProvider
 import im.vector.app.core.utils.isAnimationEnabled
 import im.vector.app.features.MainActivity
 import im.vector.app.features.analytics.ui.consent.AnalyticsOptInActivity
@@ -46,6 +48,7 @@ import javax.inject.Singleton
 @Singleton
 class PopupAlertManager @Inject constructor(
         private val clock: Clock,
+        private val stringProvider: StringProvider,
 ) {
 
     companion object {
@@ -282,6 +285,9 @@ class PopupAlertManager @Inject constructor(
                     }
                     currentIsDismissed()
                 }
+                .setOnShowListener {
+                    handleAccessibility(activity)
+                }
                 .enableSwipeToDismiss()
                 .enableInfiniteDuration(true)
                 .apply {
@@ -297,6 +303,24 @@ class PopupAlertManager @Inject constructor(
                 .show()
     }
 
+    /* a11y */
+    private fun handleAccessibility(activity: Activity) {
+        activity.window.decorView.findViewById<View>(R.id.llAlertBackground)?.let { alertView ->
+            alertView.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_YES
+
+            // Add close action for a11y (same action than swipe). User can select the action by swiping on the screen vertically,
+            // and double tap to perform the action
+            ViewCompat.addAccessibilityAction(
+                    alertView,
+                    stringProvider.getString(R.string.action_close)
+            ) { _, _ ->
+                currentIsDismissed()
+                Alerter.hide()
+                true
+            }
+        }
+    }
+
     private fun currentIsDismissed() {
         // current alert has been hidden
         if (currentAlerter?.isLight == false) {