Merge "Improve KQS accessibility" into main

This commit is contained in:
Treehugger Robot authored and Android (Google) Code Review committed 2025-03-10 12:49:00 -07:00
commit a6533ae04b
4 files changed
+73 -14

No files matched your search

@@ -66,6 +66,11 @@ public class KeyboardQuickSwitchTaskView extends ConstraintLayout {
@Nullable private ImageView mIcon2;
@Nullable private View mContent;
// Describe the task position in the parent container. Used to add information about the task's
// position in a task list to the task view's content description.
private int mIndexInParent = -1;
private int mTotalTasksInParent = -1;
public KeyboardQuickSwitchTaskView(@NonNull Context context) {
this(context, null);
}
@@ -155,36 +160,51 @@ public class KeyboardQuickSwitchTaskView extends ConstraintLayout {
applyThumbnail(mThumbnailView1, task1, thumbnailUpdateFunction);
applyThumbnail(mThumbnailView2, task2, thumbnailUpdateFunction);
// Update content description, even in cases task icons, and content descriptions need to be
// loaded asynchronously to ensure that the task has non empty description (assuming task
// position information was set), as KeyboardQuickSwitch view may request accessibility
// focus to be moved to the task when the quick switch UI gets shown. The description will
// be updated once the task metadata has been loaded - the delay should be very short, and
// the content description when task titles are not available still gives some useful
// information to the user (the task's position in the list).
updateContentDesctiptionForTasks(task1, task2);
if (iconUpdateFunction == null) {
applyIcon(mIcon1, task1);
applyIcon(mIcon2, task2);
setContentDescription(task2 == null
? task1.titleDescription
: getContext().getString(
R.string.quick_switch_split_task,
task1.titleDescription,
task2.titleDescription));
return;
}
iconUpdateFunction.updateIconInBackground(task1, t -> {
applyIcon(mIcon1, task1);
if (task2 != null) {
return;
}
setContentDescription(task1.titleDescription);
updateContentDesctiptionForTasks(task1, null);
});
if (task2 == null) {
return;
}
iconUpdateFunction.updateIconInBackground(task2, t -> {
applyIcon(mIcon2, task2);
setContentDescription(getContext().getString(
R.string.quick_switch_split_task,
task1.titleDescription,
task2.titleDescription));
updateContentDesctiptionForTasks(task1, task2);
});
}
/**
* Initializes information about the task's position within the parent container context - used
* to add position information to the view's content description.
* Should be called before associating the view with tasks.
*
* @param index The view's 0-based index within the parent task container.
* @param totalTasks The total number of tasks in the parent task container.
*/
protected void setPositionInformation(int index, int totalTasks) {
mIndexInParent = index;
mTotalTasksInParent = totalTasks;
}
protected void setThumbnailsForSplitTasks(
@NonNull Task task1,
@Nullable Task task2,
@@ -283,6 +303,28 @@ public class KeyboardQuickSwitchTaskView extends ConstraintLayout {
constantState.newDrawable(getResources(), getContext().getTheme()));
}
/**
* Updates the task view's content description to reflect tasks represented by the view.
*/
private void updateContentDesctiptionForTasks(@NonNull Task task1, @Nullable Task task2) {
String tasksDescription = task1.titleDescription == null || task2 == null
? task1.titleDescription
: getContext().getString(
R.string.quick_switch_split_task,
task1.titleDescription,
task2.titleDescription);
if (mIndexInParent < 0) {
setContentDescription(tasksDescription);
return;
}
setContentDescription(
getContext().getString(R.string.quick_switch_task_with_position_in_parent,
tasksDescription != null ? tasksDescription : "",
mIndexInParent + 1,
mTotalTasksInParent));
}
protected interface ThumbnailUpdateFunction {
void updateThumbnailInBackground(Task task, Consumer<ThumbnailData> callback);