Skip to content

Commit

Permalink
[MaterialTimePicker][a11y] Give more context on hour/minute selection
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 435119125
  • Loading branch information
paulfthomas authored and veganafro committed Mar 16, 2022
1 parent ccfc0ee commit 380778f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
Expand Up @@ -30,6 +30,7 @@
import android.os.Build.VERSION_CODES;
import android.view.View;
import android.view.accessibility.AccessibilityManager;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import com.google.android.material.timepicker.ClockHandView.OnActionUpListener;
import com.google.android.material.timepicker.ClockHandView.OnRotateListener;
import com.google.android.material.timepicker.TimePickerControls.ActiveSelection;
Expand All @@ -56,8 +57,8 @@ class TimePickerClockPresenter
private static final int DEGREES_PER_HOUR = 30;
private static final int DEGREES_PER_MINUTE = 6;

private TimePickerView timePickerView;
private TimeModel time;
private final TimePickerView timePickerView;
private final TimeModel time;
private float minuteRotation;
private float hourRotation;

Expand Down Expand Up @@ -164,9 +165,28 @@ void setSelection(@ActiveSelection int selection, boolean animate) {
timePickerView.setHandRotation(isMinute ? minuteRotation : hourRotation, animate);
timePickerView.setActiveSelection(selection);
timePickerView.setMinuteHourDelegate(
new ClickActionDelegate(timePickerView.getContext(), R.string.material_hour_selection));
new ClickActionDelegate(timePickerView.getContext(), R.string.material_hour_selection) {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setContentDescription(
host.getResources()
.getString(
R.string.material_hour_suffix, String.valueOf(time.getHourForDisplay())));
}
});
timePickerView.setHourClickDelegate(
new ClickActionDelegate(timePickerView.getContext(), R.string.material_minute_selection));
new ClickActionDelegate(timePickerView.getContext(), R.string.material_minute_selection) {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setContentDescription(
host.getResources()
.getString(R.string.material_minute_suffix, String.valueOf(time.minute)));
}
});
}

@Override
Expand All @@ -182,7 +202,7 @@ public void onActionUp(float rotation, boolean moveInEventStream) {

AccessibilityManager am =
getSystemService(timePickerView.getContext(), AccessibilityManager.class);
boolean isExploreByTouchEnabled = am.isTouchExplorationEnabled();
boolean isExploreByTouchEnabled = am != null && am.isTouchExplorationEnabled();
if (!isExploreByTouchEnabled) {
setSelection(MINUTE, /* animate= */ true);
}
Expand Down
Expand Up @@ -43,6 +43,7 @@
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import com.google.android.material.button.MaterialButtonToggleGroup;
import com.google.android.material.button.MaterialButtonToggleGroup.OnButtonCheckedListener;
import com.google.android.material.color.MaterialColors;
Expand Down Expand Up @@ -95,7 +96,7 @@ public void afterTextChanged(Editable s) {
private final EditText minuteEditText;
private MaterialButtonToggleGroup toggle;

public TimePickerTextInputPresenter(LinearLayout timePickerView, TimeModel time) {
public TimePickerTextInputPresenter(final LinearLayout timePickerView, final TimeModel time) {
this.timePickerView = timePickerView;
this.time = time;
Resources res = timePickerView.getResources();
Expand Down Expand Up @@ -137,9 +138,28 @@ public void onClick(View v) {

controller = new TimePickerTextInputKeyController(hourTextInput, minuteTextInput, time);
hourTextInput.setChipDelegate(
new ClickActionDelegate(timePickerView.getContext(), R.string.material_hour_selection));
new ClickActionDelegate(timePickerView.getContext(), R.string.material_hour_selection) {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setContentDescription(
host.getResources()
.getString(
R.string.material_hour_suffix, String.valueOf(time.getHourForDisplay())));
}
});
minuteTextInput.setChipDelegate(
new ClickActionDelegate(timePickerView.getContext(), R.string.material_minute_selection));
new ClickActionDelegate(timePickerView.getContext(), R.string.material_minute_selection) {
@Override
public void onInitializeAccessibilityNodeInfo(
View host, AccessibilityNodeInfoCompat info) {
super.onInitializeAccessibilityNodeInfo(host, info);
info.setContentDescription(
host.getResources()
.getString(R.string.material_minute_suffix, String.valueOf(time.minute)));
}
});

initialize();
}
Expand Down

0 comments on commit 380778f

Please sign in to comment.