Added changes for a11y for the left and right shortcut buttons

This CL aims at making changes for making sure we get the right content
description for the left and right shortcut buttons on the wallpaper
picker screen.

Bug: b/280551132
Test: Tested by building and installing picker on local, and making sure
we get the right text announced. Video attached in bug.

Change-Id: Ic2c845fc75ad94ef187dc1ed484f2635ee00b9f4
diff --git a/src/com/android/customization/picker/quickaffordance/ui/binder/KeyguardQuickAffordancePickerBinder.kt b/src/com/android/customization/picker/quickaffordance/ui/binder/KeyguardQuickAffordancePickerBinder.kt
index 091f484..3ac52ad 100644
--- a/src/com/android/customization/picker/quickaffordance/ui/binder/KeyguardQuickAffordancePickerBinder.kt
+++ b/src/com/android/customization/picker/quickaffordance/ui/binder/KeyguardQuickAffordancePickerBinder.kt
@@ -20,7 +20,11 @@
 import android.app.Dialog
 import android.content.Context
 import android.view.View
+import android.view.ViewGroup
+import android.view.accessibility.AccessibilityEvent
 import android.widget.ImageView
+import androidx.core.view.AccessibilityDelegateCompat
+import androidx.core.view.ViewCompat
 import androidx.lifecycle.Lifecycle
 import androidx.lifecycle.LifecycleOwner
 import androidx.lifecycle.lifecycleScope
@@ -62,6 +66,26 @@
         slotTabView.layoutManager =
             LinearLayoutManager(view.context, RecyclerView.HORIZONTAL, false)
         slotTabView.addItemDecoration(ItemSpacing(ItemSpacing.TAB_ITEM_SPACING_DP))
+
+        // Setting a custom accessibility delegate so that the default content descriptions
+        // for items in a list aren't announced (for left & right shortcuts). We populate
+        // the content description for these shortcuts later on with the right (expected)
+        // values.
+        val slotTabViewDelegate: AccessibilityDelegateCompat =
+            object : AccessibilityDelegateCompat() {
+                override fun onRequestSendAccessibilityEvent(
+                    host: ViewGroup,
+                    child: View,
+                    event: AccessibilityEvent
+                ): Boolean {
+                    if (event.eventType != AccessibilityEvent.TYPE_VIEW_FOCUSED) {
+                        child.contentDescription = null
+                    }
+                    return super.onRequestSendAccessibilityEvent(host, child, event)
+                }
+            }
+
+        ViewCompat.setAccessibilityDelegate(slotTabView, slotTabViewDelegate)
         val affordancesAdapter =
             OptionItemAdapter(
                 layoutResourceId = R.layout.keyguard_quick_affordance,
diff --git a/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModel.kt b/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModel.kt
index 809d20e..d67ad1d 100644
--- a/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModel.kt
+++ b/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModel.kt
@@ -158,7 +158,8 @@
                                         Icon.Loaded(
                                             drawable =
                                                 getAffordanceIcon(affordanceModel.iconResourceId),
-                                            contentDescription = null,
+                                            contentDescription =
+                                                Text.Loaded(getSlotContentDescription(slot.id)),
                                         ),
                                     text = Text.Loaded(affordanceModel.name),
                                     isSelected = MutableStateFlow(true) as StateFlow<Boolean>,
@@ -423,6 +424,18 @@
         )
     }
 
+    private fun getSlotContentDescription(slotId: String): String {
+        return applicationContext.getString(
+            when (slotId) {
+                KeyguardQuickAffordanceSlots.SLOT_ID_BOTTOM_START ->
+                    R.string.keyguard_slot_name_bottom_start
+                KeyguardQuickAffordanceSlots.SLOT_ID_BOTTOM_END ->
+                    R.string.keyguard_slot_name_bottom_end
+                else -> error("No accessibility label for slot with ID \"$slotId\"!")
+            }
+        )
+    }
+
     private suspend fun getAffordanceIcon(@DrawableRes iconResourceId: Int): Drawable {
         return quickAffordanceInteractor.getAffordanceIcon(iconResourceId)
     }