Give accessibility focus to the alerts.

This commit is contained in:
Benoit Marty 2023-05-10 17:00:07 +02:00
parent 71a2a4d31a
commit 6c942a7575
2 changed files with 19 additions and 2 deletions

View File

@ -20,6 +20,8 @@ import android.graphics.drawable.Drawable
import android.text.InputType
import android.view.View
import android.view.ViewGroup
import android.view.accessibility.AccessibilityEvent
import android.view.accessibility.AccessibilityNodeInfo
import android.widget.EditText
import android.widget.ImageView
import androidx.annotation.AttrRes
@ -28,6 +30,7 @@ import androidx.annotation.DrawableRes
import androidx.appcompat.widget.SearchView
import androidx.core.content.ContextCompat
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.view.ViewCompat
import androidx.core.view.isVisible
import androidx.transition.ChangeBounds
import androidx.transition.Fade
@ -97,6 +100,14 @@ fun View.setAttributeBackground(@AttrRes attributeId: Int) {
setBackgroundResource(attribute.resourceId)
}
/**
* Inspired from https://stackoverflow.com/a/64597532/1472514. Safer to call the 2 available API.
*/
fun View.giveAccessibilityFocus() {
ViewCompat.performAccessibilityAction(this, AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null)
sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED)
}
fun ViewGroup.animateLayoutChange(animationDuration: Long, transitionComplete: (() -> Unit)? = null) {
val transition = TransitionSet().apply {
ordering = TransitionSet.ORDERING_SEQUENTIAL

View File

@ -24,6 +24,7 @@ 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.extensions.giveAccessibilityFocus
import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.core.resources.StringProvider
import im.vector.app.core.utils.isAnimationEnabled
@ -286,7 +287,7 @@ class PopupAlertManager @Inject constructor(
currentIsDismissed()
}
.setOnShowListener {
handleAccessibility(activity)
handleAccessibility(activity, animate)
}
.enableSwipeToDismiss()
.enableInfiniteDuration(true)
@ -304,7 +305,7 @@ class PopupAlertManager @Inject constructor(
}
/* a11y */
private fun handleAccessibility(activity: Activity) {
private fun handleAccessibility(activity: Activity, giveFocus: Boolean) {
activity.window.decorView.findViewById<View>(R.id.llAlertBackground)?.let { alertView ->
alertView.importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_YES
@ -318,6 +319,11 @@ class PopupAlertManager @Inject constructor(
Alerter.hide()
true
}
// And give focus to the alert right now, only for first display, i.e. when there is an animation.
if (giveFocus) {
alertView.giveAccessibilityFocus()
}
}
}