PanelSlicesAdapter: load slice label safely

Load the slice action label string safely when action label is set, and
no longer at adapter instantiation time.

Bug: b/298571448

Test: atest PanelSlicesAdapterTest
Change-Id: I0d5b6e0b2db2fa78d3f58ae2cb96f14437923ffd
This commit is contained in:
Behnam Heydarshahi
2023-09-05 13:50:03 +00:00
parent c7757a00d3
commit 374b417b68
2 changed files with 67 additions and 10 deletions

View File

@@ -61,15 +61,12 @@ public class PanelSlicesAdapter
private final List<LiveData<Slice>> mSliceLiveData;
private final int mMetricsCategory;
private final PanelFragment mPanelFragment;
private final String mSliceClickActionLabel;
public PanelSlicesAdapter(
PanelFragment fragment, Map<Uri, LiveData<Slice>> sliceLiveData, int metricsCategory) {
mPanelFragment = fragment;
mSliceLiveData = new ArrayList<>(sliceLiveData.values());
mMetricsCategory = metricsCategory;
mSliceClickActionLabel = mPanelFragment.getContext().getString(
R.string.accessibility_action_label_panel_slice);
}
@NonNull
@@ -77,7 +74,7 @@ public class PanelSlicesAdapter
public SliceRowViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
final Context context = viewGroup.getContext();
final LayoutInflater inflater = LayoutInflater.from(context);
View view;
final View view;
if (viewType == PanelContent.VIEW_TYPE_SLIDER) {
view = inflater.inflate(R.layout.panel_slice_slider_row, viewGroup, false);
} else {
@@ -189,7 +186,6 @@ public class PanelSlicesAdapter
return;
}
sliceView.setTag(ROW_VIEW_TAG, new Object());
sliceView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
@@ -208,15 +204,17 @@ public class PanelSlicesAdapter
* Update the action label for TalkBack to be more specific
* @param view the RowView within the Slice
*/
private void setActionLabel(View view) {
@VisibleForTesting void setActionLabel(View view) {
view.setAccessibilityDelegate(new View.AccessibilityDelegate() {
@Override
public void onInitializeAccessibilityNodeInfo(View host,
AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(host, info);
AccessibilityNodeInfo.AccessibilityAction customClick =
new AccessibilityNodeInfo.AccessibilityAction(
ACTION_CLICK, mSliceClickActionLabel);
new AccessibilityNodeInfo.AccessibilityAction(ACTION_CLICK, host
.getResources()
.getString(R.string.accessibility_action_label_panel_slice));
info.addAction(customClick);
}
});