1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.BlendMode;
import android.graphics.Canvas;
import android.graphics.Insets;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.Region.Op;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.view.accessibility.AccessibilityNodeInfo;
import android.view.inspector.InspectableProperty;
import com.android.internal.R;
import com.android.internal.util.Preconditions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* AbsSeekBar extends the capabilities of ProgressBar by adding a draggable thumb.
*/
public abstract class AbsSeekBar extends ProgressBar {
private final Rect mTempRect = new Rect();
@UnsupportedAppUsage
private Drawable mThumb;
private ColorStateList mThumbTintList = null;
private BlendMode mThumbBlendMode = null;
private boolean mHasThumbTint = false;
private boolean mHasThumbBlendMode = false;
private Drawable mTickMark;
private ColorStateList mTickMarkTintList = null;
private BlendMode mTickMarkBlendMode = null;
private boolean mHasTickMarkTint = false;
private boolean mHasTickMarkBlendMode = false;
private int mThumbOffset;
@UnsupportedAppUsage
private boolean mSplitTrack;
/**
* On touch, this offset plus the scaled value from the position of the
* touch will form the progress value. Usually 0.
*/
@UnsupportedAppUsage
float mTouchProgressOffset;
/**
* Whether this is user seekable.
*/
@UnsupportedAppUsage
boolean mIsUserSeekable = true;
/**
* On key presses (right or left), the amount to increment/decrement the
* progress.
*/
private int mKeyProgressIncrement = 1;
private static final int NO_ALPHA = 0xFF;
@UnsupportedAppUsage
private float mDisabledAlpha;
private int mThumbExclusionMaxSize;
private int mScaledTouchSlop;
private float mTouchDownX;
@UnsupportedAppUsage
private boolean mIsDragging;
private List<Rect> mUserGestureExclusionRects = Collections.emptyList();
private final List<Rect> mGestureExclusionRects = new ArrayList<>();
private final Rect mThumbRect = new Rect();
public AbsSeekBar(Context context) {
super(context);
}
public AbsSeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AbsSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public AbsSeekBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.SeekBar, defStyleAttr, defStyleRes);
saveAttributeDataForStyleable(context, R.styleable.SeekBar, attrs, a, defStyleAttr,
defStyleRes);
final Drawable thumb = a.getDrawable(R.styleable.SeekBar_thumb);
setThumb(thumb);
if (a.hasValue(R.styleable.SeekBar_thumbTintMode)) {
mThumbBlendMode = Drawable.parseBlendMode(a.getInt(
R.styleable.SeekBar_thumbTintMode, -1), mThumbBlendMode);
mHasThumbBlendMode = true;
}
if (a.hasValue(R.styleable.SeekBar_thumbTint)) {
mThumbTintList = a.getColorStateList(R.styleable.SeekBar_thumbTint);
mHasThumbTint = true;
}
final Drawable tickMark = a.getDrawable(R.styleable.SeekBar_tickMark);
setTickMark(tickMark);
if (a.hasValue(R.styleable.SeekBar_tickMarkTintMode)) {
mTickMarkBlendMode = Drawable.parseBlendMode(a.getInt(
R.styleable.SeekBar_tickMarkTintMode, -1), mTickMarkBlendMode);
mHasTickMarkBlendMode = true;
}
if (a.hasValue(R.styleable.SeekBar_tickMarkTint)) {
mTickMarkTintList = a.getColorStateList(R.styleable.SeekBar_tickMarkTint);
mHasTickMarkTint = true;
}
mSplitTrack = a.getBoolean(R.styleable.SeekBar_splitTrack, false);
// Guess thumb offset if thumb != null, but allow layout to override.
final int thumbOffset = a.getDimensionPixelOffset(
R.styleable.SeekBar_thumbOffset, getThumbOffset());
setThumbOffset(thumbOffset);
final boolean useDisabledAlpha = a.getBoolean(R.styleable.SeekBar_useDisabledAlpha, true);
a.recycle();
if (useDisabledAlpha) {
final TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Theme, 0, 0);
mDisabledAlpha = ta.getFloat(R.styleable.Theme_disabledAlpha, 0.5f);
ta.recycle();
} else {
mDisabledAlpha = 1.0f;
}
applyThumbTint();
applyTickMarkTint();
mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mThumbExclusionMaxSize = getResources().getDimensionPixelSize(
com.android.internal.R.dimen.seekbar_thumb_exclusion_max_size);
}
/**
* Sets the thumb that will be drawn at the end of the progress meter within the SeekBar.
* <p>
* If the thumb is a valid drawable (i.e. not null), half its width will be
* used as the new thumb offset (@see #setThumbOffset(int)).
*
* @param thumb Drawable representing the thumb
*/
public void setThumb(Drawable thumb) {
final boolean needUpdate;
// This way, calling setThumb again with the same bitmap will result in
// it recalcuating mThumbOffset (if for example it the bounds of the
// drawable changed)
if (mThumb != null && thumb != mThumb) {
mThumb.setCallback(null);
needUpdate = true;
} else {
needUpdate = false;
}
if (thumb != null) {
thumb.setCallback(this);
if (canResolveLayoutDirection()) {
thumb.setLayoutDirection(getLayoutDirection());
}
// Assuming the thumb drawable is symmetric, set the thumb offset
// such that the thumb will hang halfway off either edge of the
// progress bar.
mThumbOffset = thumb.getIntrinsicWidth() / 2;
// If we're updating get the new states
if (needUpdate &&
(thumb.getIntrinsicWidth() != mThumb.getIntrinsicWidth()
|| thumb.getIntrinsicHeight() != mThumb.getIntrinsicHeight())) {
requestLayout();
}
}
mThumb = thumb;
applyThumbTint();
invalidate();
if (needUpdate) {
updateThumbAndTrackPos(getWidth(), getHeight());
if (thumb != null && thumb.isStateful()) {
// Note that if the states are different this won't work.
// For now, let's consider that an app bug.
int[] state = getDrawableState();
thumb.setState(state);
}
}
}
/**
* Return the drawable used to represent the scroll thumb - the component that
* the user can drag back and forth indicating the current value by its position.
*
* @return The current thumb drawable
*/
public Drawable getThumb() {
return mThumb;
}
/**
* Applies a tint to the thumb drawable. Does not modify the current tint
* mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
* <p>
* Subsequent calls to {@link #setThumb(Drawable)} will automatically
* mutate the drawable and apply the specified tint and tint mode using
* {@link Drawable#setTintList(ColorStateList)}.
*
* @param tint the tint to apply, may be {@code null} to clear tint
*
* @attr ref android.R.styleable#SeekBar_thumbTint
* @see #getThumbTintList()
* @see Drawable#setTintList(ColorStateList)
*/
public void setThumbTintList(@Nullable ColorStateList tint) {
mThumbTintList = tint;
mHasThumbTint = true;
applyThumbTint();
}
/**
* Returns the tint applied to the thumb drawable, if specified.
*
* @return the tint applied to the thumb drawable
* @attr ref android.R.styleable#SeekBar_thumbTint
* @see #setThumbTintList(ColorStateList)
*/
@InspectableProperty(name = "thumbTint")
@Nullable
public ColorStateList getThumbTintList() {
return mThumbTintList;
}
/**
* Specifies the blending mode used to apply the tint specified by
* {@link #setThumbTintList(ColorStateList)}} to the thumb drawable. The
* default mode is {@link PorterDuff.Mode#SRC_IN}.
*
* @param tintMode the blending mode used to apply the tint, may be
* {@code null} to clear tint
*
* @attr ref android.R.styleable#SeekBar_thumbTintMode
* @see #getThumbTintMode()
* @see Drawable#setTintMode(PorterDuff.Mode)
*/
public void setThumbTintMode(@Nullable PorterDuff.Mode tintMode) {
setThumbTintBlendMode(tintMode != null ? BlendMode.fromValue(tintMode.nativeInt) :
null);
}
/**
* Specifies the blending mode used to apply the tint specified by
* {@link #setThumbTintList(ColorStateList)}} to the thumb drawable. The
* default mode is {@link BlendMode#SRC_IN}.
*
* @param blendMode the blending mode used to apply the tint, may be
* {@code null} to clear tint
*
* @attr ref android.R.styleable#SeekBar_thumbTintMode
* @see #getThumbTintMode()
* @see Drawable#setTintBlendMode(BlendMode)
*/
public void setThumbTintBlendMode(@Nullable BlendMode blendMode) {
mThumbBlendMode = blendMode;
mHasThumbBlendMode = true;
applyThumbTint();
}
/**
* Returns the blending mode used to apply the tint to the thumb drawable,
* if specified.
*
* @return the blending mode used to apply the tint to the thumb drawable
* @attr ref android.R.styleable#SeekBar_thumbTintMode
* @see #setThumbTintMode(PorterDuff.Mode)
*/
@InspectableProperty
@Nullable
public PorterDuff.Mode getThumbTintMode() {
return mThumbBlendMode != null
? BlendMode.blendModeToPorterDuffMode(mThumbBlendMode) : null;
}
/**
* Returns the blending mode used to apply the tint to the thumb drawable,
* if specified.
*
* @return the blending mode used to apply the tint to the thumb drawable
* @attr ref android.R.styleable#SeekBar_thumbTintMode
* @see #setThumbTintBlendMode(BlendMode)
*/
@Nullable
public BlendMode getThumbTintBlendMode() {
return mThumbBlendMode;
}
private void applyThumbTint() {
if (mThumb != null && (mHasThumbTint || mHasThumbBlendMode)) {
mThumb = mThumb.mutate();
if (mHasThumbTint) {
mThumb.setTintList(mThumbTintList);
}
if (mHasThumbBlendMode) {
mThumb.setTintBlendMode(mThumbBlendMode);
}
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (mThumb.isStateful()) {
mThumb.setState(getDrawableState());
}
}
}
/**
* @see #setThumbOffset(int)
*/
public int getThumbOffset() {
return mThumbOffset;
}
/**
* Sets the thumb offset that allows the thumb to extend out of the range of
* the track.
*
* @param thumbOffset The offset amount in pixels.
*/
public void setThumbOffset(int thumbOffset) {
mThumbOffset = thumbOffset;
invalidate();
}
/**
* Specifies whether the track should be split by the thumb. When true,
* the thumb's optical bounds will be clipped out of the track drawable,
* then the thumb will be drawn into the resulting gap.
*
* @param splitTrack Whether the track should be split by the thumb
*/
public void setSplitTrack(boolean splitTrack) {
mSplitTrack = splitTrack;
invalidate();
}
/**
* Returns whether the track should be split by the thumb.
*/
public boolean getSplitTrack() {
return mSplitTrack;
}
/**
* Sets the drawable displayed at each progress position, e.g. at each
* possible thumb position.
*
* @param tickMark the drawable to display at each progress position
*/
public void setTickMark(Drawable tickMark) {
if (mTickMark != null) {
mTickMark.setCallback(null);
}
mTickMark = tickMark;
if (tickMark != null) {
tickMark.setCallback(this);
tickMark.setLayoutDirection(getLayoutDirection());
if (tickMark.isStateful()) {
tickMark.setState(getDrawableState());
}
applyTickMarkTint();
}
invalidate();
}
/**
* @return the drawable displayed at each progress position
*/
public Drawable getTickMark() {
return mTickMark;
}
/**
* Applies a tint to the tick mark drawable. Does not modify the current tint
* mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
* <p>
* Subsequent calls to {@link #setTickMark(Drawable)} will automatically
* mutate the drawable and apply the specified tint and tint mode using
* {@link Drawable#setTintList(ColorStateList)}.
*
* @param tint the tint to apply, may be {@code null} to clear tint
*
* @attr ref android.R.styleable#SeekBar_tickMarkTint
* @see #getTickMarkTintList()
* @see Drawable#setTintList(ColorStateList)
*/
public void setTickMarkTintList(@Nullable ColorStateList tint) {
mTickMarkTintList = tint;
mHasTickMarkTint = true;
applyTickMarkTint();
}
/**
* Returns the tint applied to the tick mark drawable, if specified.
*
* @return the tint applied to the tick mark drawable
* @attr ref android.R.styleable#SeekBar_tickMarkTint
* @see #setTickMarkTintList(ColorStateList)
*/
@InspectableProperty(name = "tickMarkTint")
@Nullable
public ColorStateList getTickMarkTintList() {
return mTickMarkTintList;
}
/**
* Specifies the blending mode used to apply the tint specified by
* {@link #setTickMarkTintList(ColorStateList)}} to the tick mark drawable. The
* default mode is {@link PorterDuff.Mode#SRC_IN}.
*
* @param tintMode the blending mode used to apply the tint, may be
* {@code null} to clear tint
*
* @attr ref android.R.styleable#SeekBar_tickMarkTintMode
* @see #getTickMarkTintMode()
* @see Drawable#setTintMode(PorterDuff.Mode)
*/
public void setTickMarkTintMode(@Nullable PorterDuff.Mode tintMode) {
setTickMarkTintBlendMode(tintMode != null ? BlendMode.fromValue(tintMode.nativeInt) : null);
}
/**
* Specifies the blending mode used to apply the tint specified by
* {@link #setTickMarkTintList(ColorStateList)}} to the tick mark drawable. The
* default mode is {@link BlendMode#SRC_IN}.
*
* @param blendMode the blending mode used to apply the tint, may be
* {@code null} to clear tint
*
* @attr ref android.R.styleable#SeekBar_tickMarkTintMode
* @see #getTickMarkTintMode()
* @see Drawable#setTintBlendMode(BlendMode)
*/
public void setTickMarkTintBlendMode(@Nullable BlendMode blendMode) {
mTickMarkBlendMode = blendMode;
mHasTickMarkBlendMode = true;
applyTickMarkTint();
}
/**
* Returns the blending mode used to apply the tint to the tick mark drawable,
* if specified.
*
* @return the blending mode used to apply the tint to the tick mark drawable
* @attr ref android.R.styleable#SeekBar_tickMarkTintMode
* @see #setTickMarkTintMode(PorterDuff.Mode)
*/
@InspectableProperty
@Nullable
public PorterDuff.Mode getTickMarkTintMode() {
return mTickMarkBlendMode != null
? BlendMode.blendModeToPorterDuffMode(mTickMarkBlendMode) : null;
}
/**
* Returns the blending mode used to apply the tint to the tick mark drawable,
* if specified.
*
* @return the blending mode used to apply the tint to the tick mark drawable
* @attr ref android.R.styleable#SeekBar_tickMarkTintMode
* @see #setTickMarkTintMode(PorterDuff.Mode)
*/
@InspectableProperty(attributeId = android.R.styleable.SeekBar_tickMarkTintMode)
@Nullable
public BlendMode getTickMarkTintBlendMode() {
return mTickMarkBlendMode;
}
private void applyTickMarkTint() {
if (mTickMark != null && (mHasTickMarkTint || mHasTickMarkBlendMode)) {
mTickMark = mTickMark.mutate();
if (mHasTickMarkTint) {
mTickMark.setTintList(mTickMarkTintList);
}
if (mHasTickMarkBlendMode) {
mTickMark.setTintBlendMode(mTickMarkBlendMode);
}
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (mTickMark.isStateful()) {
mTickMark.setState(getDrawableState());
}
}
}
/**
* Sets the amount of progress changed via the arrow keys.
*
* @param increment The amount to increment or decrement when the user
* presses the arrow keys.
*/
public void setKeyProgressIncrement(int increment) {
mKeyProgressIncrement = increment < 0 ? -increment : increment;
}
/**
* Returns the amount of progress changed via the arrow keys.
* <p>
* By default, this will be a value that is derived from the progress range.
*
* @return The amount to increment or decrement when the user presses the
* arrow keys. This will be positive.
*/
public int getKeyProgressIncrement() {
return mKeyProgressIncrement;
}
@Override
public synchronized void setMin(int min) {
super.setMin(min);
int range = getMax() - getMin();
if ((mKeyProgressIncrement == 0) || (range / mKeyProgressIncrement > 20)) {
// It will take the user too long to change this via keys, change it
// to something more reasonable
setKeyProgressIncrement(Math.max(1, Math.round((float) range / 20)));
}
}
@Override
public synchronized void setMax(int max) {
super.setMax(max);
int range = getMax() - getMin();
if ((mKeyProgressIncrement == 0) || (range / mKeyProgressIncrement > 20)) {
// It will take the user too long to change this via keys, change it
// to something more reasonable
setKeyProgressIncrement(Math.max(1, Math.round((float) range / 20)));
}
}
@Override
protected boolean verifyDrawable(@NonNull Drawable who) {
return who == mThumb || who == mTickMark || super.verifyDrawable(who);
}
@Override
public void jumpDrawablesToCurrentState() {
super.jumpDrawablesToCurrentState();
if (mThumb != null) {
mThumb.jumpToCurrentState();
}
if (mTickMark != null) {
mTickMark.jumpToCurrentState();
}
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
final Drawable progressDrawable = getProgressDrawable();
if (progressDrawable != null && mDisabledAlpha < 1.0f) {
progressDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
}
final Drawable thumb = mThumb;
if (thumb != null && thumb.isStateful()
&& thumb.setState(getDrawableState())) {
invalidateDrawable(thumb);
}
final Drawable tickMark = mTickMark;
if (tickMark != null && tickMark.isStateful()
&& tickMark.setState(getDrawableState())) {
invalidateDrawable(tickMark);
}
}
@Override
public void drawableHotspotChanged(float x, float y) {
super.drawableHotspotChanged(x, y);
if (mThumb != null) {
mThumb.setHotspot(x, y);
}
}
@Override
void onVisualProgressChanged(int id, float scale) {
super.onVisualProgressChanged(id, scale);
if (id == R.id.progress) {
final Drawable thumb = mThumb;
if (thumb != null) {
setThumbPos(getWidth(), thumb, scale, Integer.MIN_VALUE);
// Since we draw translated, the drawable's bounds that it signals
// for invalidation won't be the actual bounds we want invalidated,
// so just invalidate this whole view.
invalidate();
}
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
updateThumbAndTrackPos(w, h);
}
private void updateThumbAndTrackPos(int w, int h) {
final int paddedHeight = h - mPaddingTop - mPaddingBottom;
final Drawable track = getCurrentDrawable();
final Drawable thumb = mThumb;
// The max height does not incorporate padding, whereas the height
// parameter does.
final int trackHeight = Math.min(mMaxHeight, paddedHeight);
final int thumbHeight = thumb == null ? 0 : thumb.getIntrinsicHeight();
// Apply offset to whichever item is taller.
final int trackOffset;
final int thumbOffset;
if (thumbHeight > trackHeight) {
final int offsetHeight = (paddedHeight - thumbHeight) / 2;
trackOffset = offsetHeight + (thumbHeight - trackHeight) / 2;
thumbOffset = offsetHeight;
} else {
final int offsetHeight = (paddedHeight - trackHeight) / 2;
trackOffset = offsetHeight;
thumbOffset = offsetHeight + (trackHeight - thumbHeight) / 2;
}
if (track != null) {
final int trackWidth = w - mPaddingRight - mPaddingLeft;
track.setBounds(0, trackOffset, trackWidth, trackOffset + trackHeight);
}
if (thumb != null) {
setThumbPos(w, thumb, getScale(), thumbOffset);
}
}
private float getScale() {
int min = getMin();
int max = getMax();
int range = max - min;
return range > 0 ? (getProgress() - min) / (float) range : 0;
}
/**
* Updates the thumb drawable bounds.
*
* @param w Width of the view, including padding
* @param thumb Drawable used for the thumb
* @param scale Current progress between 0 and 1
* @param offset Vertical offset for centering. If set to
* {@link Integer#MIN_VALUE}, the current offset will be used.
*/
private void setThumbPos(int w, Drawable thumb, float scale, int offset) {
int available = w - mPaddingLeft - mPaddingRight;
final int thumbWidth = thumb.getIntrinsicWidth();
final int thumbHeight = thumb.getIntrinsicHeight();
available -= thumbWidth;
// The extra space for the thumb to move on the track
available += mThumbOffset * 2;
final int thumbPos = (int) (scale * available + 0.5f);
final int top, bottom;
if (offset == Integer.MIN_VALUE) {
final Rect oldBounds = thumb.getBounds();
top = oldBounds.top;
bottom = oldBounds.bottom;
} else {
top = offset;
bottom = offset + thumbHeight;
}
final int left = (isLayoutRtl() && mMirrorForRtl) ? available - thumbPos : thumbPos;
final int right = left + thumbWidth;
final Drawable background = getBackground();
if (background != null) {
final int offsetX = mPaddingLeft - mThumbOffset;
final int offsetY = mPaddingTop;
background.setHotspotBounds(left + offsetX, top + offsetY,
right + offsetX, bottom + offsetY);
}
// Canvas will be translated, so 0,0 is where we start drawing
thumb.setBounds(left, top, right, bottom);
updateGestureExclusionRects();
}
@Override
public void setSystemGestureExclusionRects(@NonNull List<Rect> rects) {
Preconditions.checkNotNull(rects, "rects must not be null");
mUserGestureExclusionRects = rects;
updateGestureExclusionRects();
}
private void updateGestureExclusionRects() {
final Drawable thumb = mThumb;
if (thumb == null) {
super.setSystemGestureExclusionRects(mUserGestureExclusionRects);
return;
}
mGestureExclusionRects.clear();
thumb.copyBounds(mThumbRect);
mThumbRect.offset(mPaddingLeft - mThumbOffset, mPaddingTop);
growRectTo(mThumbRect, Math.min(getHeight(), mThumbExclusionMaxSize));
mGestureExclusionRects.add(mThumbRect);
mGestureExclusionRects.addAll(mUserGestureExclusionRects);
super.setSystemGestureExclusionRects(mGestureExclusionRects);
}
/**
* Grows {@code r} from its center such that each dimension is at least {@code minimumSize}.
*/
private void growRectTo(Rect r, int minimumSize) {
int dy = (minimumSize - r.height()) / 2;
if (dy > 0) {
r.top -= dy;
r.bottom += dy;
}
int dx = (minimumSize - r.width()) / 2;
if (dx > 0) {
r.left -= dx;
r.right += dx;
}
}
/**
* @hide
*/
@Override
public void onResolveDrawables(int layoutDirection) {
super.onResolveDrawables(layoutDirection);
if (mThumb != null) {
mThumb.setLayoutDirection(layoutDirection);
}
}
@Override
protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawThumb(canvas);
}
@Override
void drawTrack(Canvas canvas) {
final Drawable thumbDrawable = mThumb;
if (thumbDrawable != null && mSplitTrack) {
final Insets insets = thumbDrawable.getOpticalInsets();
final Rect tempRect = mTempRect;
thumbDrawable.copyBounds(tempRect);
tempRect.offset(mPaddingLeft - mThumbOffset, mPaddingTop);
tempRect.left += insets.left;
tempRect.right -= insets.right;
final int saveCount = canvas.save();
canvas.clipRect(tempRect, Op.DIFFERENCE);
super.drawTrack(canvas);
drawTickMarks(canvas);
canvas.restoreToCount(saveCount);
} else {
super.drawTrack(canvas);
drawTickMarks(canvas);
}
}
/**
* @hide
*/
protected void drawTickMarks(Canvas canvas) {
if (mTickMark != null) {
final int count = getMax() - getMin();
if (count > 1) {
final int w = mTickMark.getIntrinsicWidth();
final int h = mTickMark.getIntrinsicHeight();
final int halfW = w >= 0 ? w / 2 : 1;
final int halfH = h >= 0 ? h / 2 : 1;
mTickMark.setBounds(-halfW, -halfH, halfW, halfH);
final float spacing = (getWidth() - mPaddingLeft - mPaddingRight) / (float) count;
final int saveCount = canvas.save();
canvas.translate(mPaddingLeft, getHeight() / 2);
for (int i = 0; i <= count; i++) {
mTickMark.draw(canvas);
canvas.translate(spacing, 0);
}
canvas.restoreToCount(saveCount);
}
}
}
/**
* Draw the thumb.
*/
@UnsupportedAppUsage
void drawThumb(Canvas canvas) {
if (mThumb != null) {
final int saveCount = canvas.save();
// Translate the padding. For the x, we need to allow the thumb to
// draw in its extra space
canvas.translate(mPaddingLeft - mThumbOffset, mPaddingTop);
mThumb.draw(canvas);
canvas.restoreToCount(saveCount);
}
}
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable d = getCurrentDrawable();
int thumbHeight = mThumb == null ? 0 : mThumb.getIntrinsicHeight();
int dw = 0;
int dh = 0;
if (d != null) {
dw = Math.max(mMinWidth, Math.min(mMaxWidth, d.getIntrinsicWidth()));
dh = Math.max(mMinHeight, Math.min(mMaxHeight, d.getIntrinsicHeight()));
dh = Math.max(thumbHeight, dh);
}
dw += mPaddingLeft + mPaddingRight;
dh += mPaddingTop + mPaddingBottom;
setMeasuredDimension(resolveSizeAndState(dw, widthMeasureSpec, 0),
resolveSizeAndState(dh, heightMeasureSpec, 0));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!mIsUserSeekable || !isEnabled()) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (isInScrollingContainer()) {
mTouchDownX = event.getX();
} else {
startDrag(event);
}
break;
case MotionEvent.ACTION_MOVE:
if (mIsDragging) {
trackTouchEvent(event);
} else {
final float x = event.getX();
if (Math.abs(x - mTouchDownX) > mScaledTouchSlop) {
startDrag(event);
}
}
break;
case MotionEvent.ACTION_UP:
if (mIsDragging) {
trackTouchEvent(event);
onStopTrackingTouch();
setPressed(false);
} else {
// Touch up when we never crossed the touch slop threshold should
// be interpreted as a tap-seek to that location.
onStartTrackingTouch();
trackTouchEvent(event);
onStopTrackingTouch();
}
// ProgressBar doesn't know to repaint the thumb drawable
// in its inactive state when the touch stops (because the
// value has not apparently changed)
invalidate();
break;
case MotionEvent.ACTION_CANCEL:
if (mIsDragging) {
onStopTrackingTouch();
setPressed(false);
}
invalidate(); // see above explanation
break;
}
return true;
}
private void startDrag(MotionEvent event) {
setPressed(true);
if (mThumb != null) {
// This may be within the padding region.
invalidate(mThumb.getBounds());
}
onStartTrackingTouch();
trackTouchEvent(event);
attemptClaimDrag();
}
private void setHotspot(float x, float y) {
final Drawable bg = getBackground();
if (bg != null) {
bg.setHotspot(x, y);
}
}
@UnsupportedAppUsage
private void trackTouchEvent(MotionEvent event) {
final int x = Math.round(event.getX());
final int y = Math.round(event.getY());
final int width = getWidth();
final int availableWidth = width - mPaddingLeft - mPaddingRight;
final float scale;
float progress = 0.0f;
if (isLayoutRtl() && mMirrorForRtl) {
if (x > width - mPaddingRight) {
scale = 0.0f;
} else if (x < mPaddingLeft) {
scale = 1.0f;
} else {
scale = (availableWidth - x + mPaddingLeft) / (float) availableWidth;
progress = mTouchProgressOffset;
}
} else {
if (x < mPaddingLeft) {
scale = 0.0f;
} else if (x > width - mPaddingRight) {
scale = 1.0f;
} else {
scale = (x - mPaddingLeft) / (float) availableWidth;
progress = mTouchProgressOffset;
}
}
final int range = getMax() - getMin();
progress += scale * range + getMin();
setHotspot(x, y);
setProgressInternal(Math.round(progress), true, false);
}
/**
* Tries to claim the user's drag motion, and requests disallowing any
* ancestors from stealing events in the drag.
*/
private void attemptClaimDrag() {
if (mParent != null) {
mParent.requestDisallowInterceptTouchEvent(true);
}
}
/**
* This is called when the user has started touching this widget.
*/
void onStartTrackingTouch() {
mIsDragging = true;
}
/**
* This is called when the user either releases his touch or the touch is
* canceled.
*/
void onStopTrackingTouch() {
mIsDragging = false;
}
/**
* Called when the user changes the seekbar's progress by using a key event.
*/
void onKeyChange() {
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (isEnabled()) {
int increment = mKeyProgressIncrement;
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_MINUS:
increment = -increment;
// fallthrough
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_PLUS:
case KeyEvent.KEYCODE_EQUALS:
increment = isLayoutRtl() ? -increment : increment;
if (setProgressInternal(getProgress() + increment, true, true)) {
onKeyChange();
return true;
}
break;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public CharSequence getAccessibilityClassName() {
return AbsSeekBar.class.getName();
}
/** @hide */
@Override
public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfoInternal(info);
if (isEnabled()) {
final int progress = getProgress();
if (progress > getMin()) {
info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_BACKWARD);
}
if (progress < getMax()) {
info.addAction(AccessibilityNodeInfo.AccessibilityAction.ACTION_SCROLL_FORWARD);
}
}
}
/** @hide */
@Override
public boolean performAccessibilityActionInternal(int action, Bundle arguments) {
if (super.performAccessibilityActionInternal(action, arguments)) {
return true;
}
if (!isEnabled()) {
return false;
}
switch (action) {
case R.id.accessibilityActionSetProgress: {
if (!canUserSetProgress()) {
return false;
}
if (arguments == null || !arguments.containsKey(
AccessibilityNodeInfo.ACTION_ARGUMENT_PROGRESS_VALUE)) {
return false;
}
float value = arguments.getFloat(
AccessibilityNodeInfo.ACTION_ARGUMENT_PROGRESS_VALUE);
return setProgressInternal((int) value, true, true);
}
case AccessibilityNodeInfo.ACTION_SCROLL_FORWARD:
case AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD: {
if (!canUserSetProgress()) {
return false;
}
int range = getMax() - getMin();
int increment = Math.max(1, Math.round((float) range / 20));
if (action == AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD) {
increment = -increment;
}
// Let progress bar handle clamping values.
if (setProgressInternal(getProgress() + increment, true, true)) {
onKeyChange();
return true;
}
return false;
}
}
return false;
}
/**
* @return whether user can change progress on the view
*/
boolean canUserSetProgress() {
return !isIndeterminate() && isEnabled();
}
@Override
public void onRtlPropertiesChanged(int layoutDirection) {
super.onRtlPropertiesChanged(layoutDirection);
final Drawable thumb = mThumb;
if (thumb != null) {
setThumbPos(getWidth(), thumb, getScale(), Integer.MIN_VALUE);
// Since we draw translated, the drawable's bounds that it signals
// for invalidation won't be the actual bounds we want invalidated,
// so just invalidate this whole view.
invalidate();
}
}
}
|