|
18 | 18 |
|
19 | 19 | import com.google.android.material.R;
|
20 | 20 |
|
| 21 | +import static android.view.accessibility.AccessibilityManager.FLAG_CONTENT_CONTROLS; |
| 22 | +import static android.view.accessibility.AccessibilityManager.FLAG_CONTENT_TEXT; |
21 | 23 | import static androidx.core.math.MathUtils.clamp;
|
22 | 24 | import static androidx.core.view.accessibility.AccessibilityNodeInfoCompat.RangeInfoCompat.RANGE_TYPE_FLOAT;
|
23 | 25 | import static com.google.android.material.shape.CornerFamily.ROUNDED;
|
@@ -245,6 +247,8 @@ abstract class BaseSlider<
|
245 | 247 | "Error parsing value(%s), valueFrom(%s), and valueTo(%s) into a float.";
|
246 | 248 |
|
247 | 249 | 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; |
248 | 252 | private static final int HALO_ALPHA = 63;
|
249 | 253 | private static final double THRESHOLD = .0001;
|
250 | 254 | private static final float THUMB_WIDTH_PRESSED_RATIO = .5f;
|
@@ -350,12 +354,21 @@ abstract class BaseSlider<
|
350 | 354 | private float touchPosition;
|
351 | 355 | @SeparationUnit private int separationUnit = UNIT_PX;
|
352 | 356 |
|
| 357 | + private final int tooltipTimeoutMillis; |
| 358 | + |
353 | 359 | @NonNull
|
354 | 360 | private final ViewTreeObserver.OnScrollChangedListener onScrollChangedListener =
|
355 | 361 | this::updateLabels;
|
| 362 | + |
356 | 363 | @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 | + }; |
359 | 372 |
|
360 | 373 | /**
|
361 | 374 | * Determines the behavior of the label which can be any of the following.
|
@@ -432,6 +445,13 @@ public BaseSlider(
|
432 | 445 |
|
433 | 446 | accessibilityManager =
|
434 | 447 | (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 | + } |
435 | 455 | }
|
436 | 456 |
|
437 | 457 | private void loadResources(@NonNull Resources resources) {
|
@@ -685,6 +705,11 @@ private void validateConfigurationIfDirty() {
|
685 | 705 | }
|
686 | 706 | }
|
687 | 707 |
|
| 708 | + public void scheduleTooltipTimeout() { |
| 709 | + removeCallbacks(resetActiveThumbIndex); |
| 710 | + postDelayed(resetActiveThumbIndex, tooltipTimeoutMillis); |
| 711 | + } |
| 712 | + |
688 | 713 | /**
|
689 | 714 | * Returns the slider's {@code valueFrom} value.
|
690 | 715 | *
|
@@ -2238,11 +2263,7 @@ private void maybeDrawTicks(@NonNull Canvas canvas) {
|
2238 | 2263 |
|
2239 | 2264 | // Draw ticks on the left inactive track (if any).
|
2240 | 2265 | if (leftActiveTickIndex > 0) {
|
2241 |
| - canvas.drawPoints( |
2242 |
| - ticksCoordinates, |
2243 |
| - 0, |
2244 |
| - leftActiveTickIndex * 2, |
2245 |
| - inactiveTicksPaint); |
| 2266 | + canvas.drawPoints(ticksCoordinates, 0, leftActiveTickIndex * 2, inactiveTicksPaint); |
2246 | 2267 | }
|
2247 | 2268 |
|
2248 | 2269 | // Draw ticks on the active track (if any).
|
@@ -2937,7 +2958,7 @@ private Boolean onKeyDownNoActiveThumb(int keyCode, @NonNull KeyEvent event) {
|
2937 | 2958 | moveFocusInAbsoluteDirection(1);
|
2938 | 2959 | return true;
|
2939 | 2960 | 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. |
2941 | 2962 | case KeyEvent.KEYCODE_PLUS:
|
2942 | 2963 | moveFocus(1);
|
2943 | 2964 | return true;
|
@@ -3016,7 +3037,7 @@ private Float calculateIncrementForKey(int keyCode) {
|
3016 | 3037 | case KeyEvent.KEYCODE_MINUS:
|
3017 | 3038 | return -increment;
|
3018 | 3039 | 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. |
3020 | 3041 | case KeyEvent.KEYCODE_PLUS:
|
3021 | 3042 | return increment;
|
3022 | 3043 | default:
|
@@ -3357,6 +3378,8 @@ protected boolean onPerformActionForVirtualView(
|
3357 | 3378 | slider.getValueFrom(),
|
3358 | 3379 | slider.getValueTo());
|
3359 | 3380 | if (slider.snapThumbToValue(virtualViewId, clamped)) {
|
| 3381 | + slider.setActiveThumbIndex(virtualViewId); |
| 3382 | + slider.scheduleTooltipTimeout(); |
3360 | 3383 | slider.updateHaloHotspot();
|
3361 | 3384 | slider.postInvalidate();
|
3362 | 3385 | invalidateVirtualView(virtualViewId);
|
|
0 commit comments