|
52 | 52 | import androidx.core.view.accessibility.AccessibilityNodeInfoCompat.RangeInfoCompat;
|
53 | 53 | import androidx.appcompat.content.res.AppCompatResources;
|
54 | 54 | import android.util.AttributeSet;
|
| 55 | +import android.util.Log; |
55 | 56 | import android.view.KeyEvent;
|
56 | 57 | import android.view.MotionEvent;
|
57 | 58 | import android.view.View;
|
@@ -193,6 +194,10 @@ abstract class BaseSlider<
|
193 | 194 | "valueTo(%s) must be greater than valueFrom(%s)";
|
194 | 195 | private static final String EXCEPTION_ILLEGAL_STEP_SIZE =
|
195 | 196 | "The stepSize(%s) must be 0, or a factor of the valueFrom(%s)-valueTo(%s) range";
|
| 197 | + private static final String WARNING_FLOATING_POINT_ERRROR = |
| 198 | + "Floating point value used for %s(%s). Using floats can have rounding errors which may" |
| 199 | + + " result in incorrect values. Instead, consider using integers with a custom" |
| 200 | + + " LabelFormatter to display the value correctly."; |
196 | 201 |
|
197 | 202 | private static final int TIMEOUT_SEND_ACCESSIBILITY_EVENT = 200;
|
198 | 203 | private static final int HALO_ALPHA = 63;
|
@@ -504,12 +509,32 @@ private void validateValues() {
|
504 | 509 | }
|
505 | 510 | }
|
506 | 511 |
|
| 512 | + private void warnAboutFloatingPointError() { |
| 513 | + if (stepSize == 0) { |
| 514 | + // Only warn if slider uses a step value. |
| 515 | + return; |
| 516 | + } |
| 517 | + |
| 518 | + if ((int) stepSize != stepSize) { |
| 519 | + Log.w(TAG, String.format(WARNING_FLOATING_POINT_ERRROR, "stepSize", stepSize)); |
| 520 | + } |
| 521 | + |
| 522 | + if ((int) valueFrom != valueFrom) { |
| 523 | + Log.w(TAG, String.format(WARNING_FLOATING_POINT_ERRROR, "valueFrom", valueFrom)); |
| 524 | + } |
| 525 | + |
| 526 | + if ((int) valueTo != valueTo) { |
| 527 | + Log.w(TAG, String.format(WARNING_FLOATING_POINT_ERRROR, "valueTo", valueTo)); |
| 528 | + } |
| 529 | + } |
| 530 | + |
507 | 531 | private void validateConfigurationIfDirty() {
|
508 | 532 | if (dirtyConfig) {
|
509 | 533 | validateValueFrom();
|
510 | 534 | validateValueTo();
|
511 | 535 | validateStepSize();
|
512 | 536 | validateValues();
|
| 537 | + warnAboutFloatingPointError(); |
513 | 538 | dirtyConfig = false;
|
514 | 539 | }
|
515 | 540 | }
|
|
0 commit comments