Skip to content

Commit 237f5ec

Browse files
committed
[Slider] Updated to show tooltips when focus by a11y.
PiperOrigin-RevId: 683257820
1 parent 8eae09d commit 237f5ec

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

lib/java/com/google/android/material/slider/BaseSlider.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import com.google.android.material.R;
2020

21+
import static android.view.accessibility.AccessibilityManager.FLAG_CONTENT_CONTROLS;
22+
import static android.view.accessibility.AccessibilityManager.FLAG_CONTENT_TEXT;
2123
import static androidx.core.math.MathUtils.clamp;
2224
import static androidx.core.view.accessibility.AccessibilityNodeInfoCompat.RangeInfoCompat.RANGE_TYPE_FLOAT;
2325
import static com.google.android.material.shape.CornerFamily.ROUNDED;
@@ -245,6 +247,8 @@ abstract class BaseSlider<
245247
"Error parsing value(%s), valueFrom(%s), and valueTo(%s) into a float.";
246248

247249
private static final int TIMEOUT_SEND_ACCESSIBILITY_EVENT = 200;
250+
private static final int MIN_TIMEOUT_TOOLTIP_WITH_ACCESSIBILITY = 10000;
251+
private static final int MAX_TIMEOUT_TOOLTIP_WITH_ACCESSIBILITY = 120000;
248252
private static final int HALO_ALPHA = 63;
249253
private static final double THRESHOLD = .0001;
250254
private static final float THUMB_WIDTH_PRESSED_RATIO = .5f;
@@ -350,12 +354,21 @@ abstract class BaseSlider<
350354
private float touchPosition;
351355
@SeparationUnit private int separationUnit = UNIT_PX;
352356

357+
private final int tooltipTimeoutMillis;
358+
353359
@NonNull
354360
private final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener =
355361
this::updateLabels;
362+
356363
@NonNull
357-
private final ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener =
358-
this::updateLabels;
364+
private final ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = this::updateLabels;
365+
366+
@NonNull
367+
private final Runnable resetActiveThumbIndex =
368+
() -> {
369+
setActiveThumbIndex(-1);
370+
invalidate();
371+
};
359372

360373
/**
361374
* Determines the behavior of the label which can be any of the following.
@@ -432,6 +445,13 @@ public BaseSlider(
432445

433446
accessibilityManager =
434447
(AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE);
448+
if (VERSION.SDK_INT >= VERSION_CODES.Q) {
449+
tooltipTimeoutMillis =
450+
accessibilityManager.getRecommendedTimeoutMillis(
451+
MIN_TIMEOUT_TOOLTIP_WITH_ACCESSIBILITY, FLAG_CONTENT_CONTROLS | FLAG_CONTENT_TEXT);
452+
} else {
453+
tooltipTimeoutMillis = MAX_TIMEOUT_TOOLTIP_WITH_ACCESSIBILITY;
454+
}
435455
}
436456

437457
private void loadResources(@NonNull Resources resources) {
@@ -685,6 +705,11 @@ private void validateConfigurationIfDirty() {
685705
}
686706
}
687707

708+
public void scheduleTooltipTimeout() {
709+
removeCallbacks(resetActiveThumbIndex);
710+
postDelayed(resetActiveThumbIndex, tooltipTimeoutMillis);
711+
}
712+
688713
/**
689714
* Returns the slider's {@code valueFrom} value.
690715
*
@@ -2238,11 +2263,7 @@ private void maybeDrawTicks(@NonNull Canvas canvas) {
22382263

22392264
// Draw ticks on the left inactive track (if any).
22402265
if (leftActiveTickIndex > 0) {
2241-
canvas.drawPoints(
2242-
ticksCoordinates,
2243-
0,
2244-
leftActiveTickIndex * 2,
2245-
inactiveTicksPaint);
2266+
canvas.drawPoints(ticksCoordinates, 0, leftActiveTickIndex * 2, inactiveTicksPaint);
22462267
}
22472268

22482269
// Draw ticks on the active track (if any).
@@ -2937,7 +2958,7 @@ private Boolean onKeyDownNoActiveThumb(int keyCode, @NonNull KeyEvent event) {
29372958
moveFocusInAbsoluteDirection(1);
29382959
return true;
29392960
case KeyEvent.KEYCODE_EQUALS:
2940-
// Numpad Plus == Shift + Equals, at least in AVD, so fall through.
2961+
// Numpad Plus == Shift + Equals, at least in AVD, so fall through.
29412962
case KeyEvent.KEYCODE_PLUS:
29422963
moveFocus(1);
29432964
return true;
@@ -3016,7 +3037,7 @@ private Float calculateIncrementForKey(int keyCode) {
30163037
case KeyEvent.KEYCODE_MINUS:
30173038
return -increment;
30183039
case KeyEvent.KEYCODE_EQUALS:
3019-
// Numpad Plus == Shift + Equals, at least in AVD, so fall through.
3040+
// Numpad Plus == Shift + Equals, at least in AVD, so fall through.
30203041
case KeyEvent.KEYCODE_PLUS:
30213042
return increment;
30223043
default:
@@ -3357,6 +3378,8 @@ protected boolean onPerformActionForVirtualView(
33573378
slider.getValueFrom(),
33583379
slider.getValueTo());
33593380
if (slider.snapThumbToValue(virtualViewId, clamped)) {
3381+
slider.setActiveThumbIndex(virtualViewId);
3382+
slider.scheduleTooltipTimeout();
33603383
slider.updateHaloHotspot();
33613384
slider.postInvalidate();
33623385
invalidateVirtualView(virtualViewId);

0 commit comments

Comments
 (0)