| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.animation.Animator; |
| 20 |
import android.animation.AnimatorListenerAdapter; |
| 21 |
import android.animation.ObjectAnimator; |
| 22 |
import android.annotation.InterpolatorRes; |
| 23 |
import android.annotation.NonNull; |
| 24 |
import android.annotation.Nullable; |
| 25 |
import android.annotation.Px; |
| 26 |
import android.compat.annotation.UnsupportedAppUsage; |
| 27 |
import android.content.Context; |
| 28 |
import android.content.res.ColorStateList; |
| 29 |
import android.content.res.TypedArray; |
| 30 |
import android.graphics.BlendMode; |
| 31 |
import android.graphics.Canvas; |
| 32 |
import android.graphics.PorterDuff; |
| 33 |
import android.graphics.Rect; |
| 34 |
import android.graphics.Shader; |
| 35 |
import android.graphics.drawable.Animatable; |
| 36 |
import android.graphics.drawable.AnimationDrawable; |
| 37 |
import android.graphics.drawable.BitmapDrawable; |
| 38 |
import android.graphics.drawable.ClipDrawable; |
| 39 |
import android.graphics.drawable.Drawable; |
| 40 |
import android.graphics.drawable.LayerDrawable; |
| 41 |
import android.graphics.drawable.StateListDrawable; |
| 42 |
import android.graphics.drawable.shapes.RoundRectShape; |
| 43 |
import android.graphics.drawable.shapes.Shape; |
| 44 |
import android.os.Build; |
| 45 |
import android.os.Parcel; |
| 46 |
import android.os.Parcelable; |
| 47 |
import android.util.AttributeSet; |
| 48 |
import android.util.FloatProperty; |
| 49 |
import android.util.MathUtils; |
| 50 |
import android.util.Pools.SynchronizedPool; |
| 51 |
import android.view.Gravity; |
| 52 |
import android.view.RemotableViewMethod; |
| 53 |
import android.view.View; |
| 54 |
import android.view.ViewDebug; |
| 55 |
import android.view.ViewHierarchyEncoder; |
| 56 |
import android.view.accessibility.AccessibilityEvent; |
| 57 |
import android.view.accessibility.AccessibilityManager; |
| 58 |
import android.view.accessibility.AccessibilityNodeInfo; |
| 59 |
import android.view.animation.AlphaAnimation; |
| 60 |
import android.view.animation.Animation; |
| 61 |
import android.view.animation.AnimationUtils; |
| 62 |
import android.view.animation.DecelerateInterpolator; |
| 63 |
import android.view.animation.Interpolator; |
| 64 |
import android.view.animation.LinearInterpolator; |
| 65 |
import android.view.animation.Transformation; |
| 66 |
import android.view.inspector.InspectableProperty; |
| 67 |
import android.widget.RemoteViews.RemoteView; |
| 68 |
|
| 69 |
import com.android.internal.R; |
| 70 |
|
| 71 |
import java.text.NumberFormat; |
| 72 |
import java.util.ArrayList; |
| 73 |
import java.util.Locale; |
| 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 |
@RemoteView |
| 167 |
public class ProgressBar extends View { |
| 168 |
|
| 169 |
private static final int MAX_LEVEL = 10000; |
| 170 |
|
| 171 |
|
| 172 |
private static final DecelerateInterpolator PROGRESS_ANIM_INTERPOLATOR = |
| 173 |
new DecelerateInterpolator(); |
| 174 |
|
| 175 |
|
| 176 |
private static final int PROGRESS_ANIM_DURATION = 80; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 183 |
int mMinWidth; |
| 184 |
int mMaxWidth; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 190 |
int mMinHeight; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 196 |
int mMaxHeight; |
| 197 |
|
| 198 |
private int mProgress; |
| 199 |
private int mSecondaryProgress; |
| 200 |
private int mMin; |
| 201 |
private boolean mMinInitialized; |
| 202 |
private int mMax; |
| 203 |
private boolean mMaxInitialized; |
| 204 |
|
| 205 |
private int mBehavior; |
| 206 |
|
| 207 |
|
| 208 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 124052713) |
| 209 |
private int mDuration; |
| 210 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 211 |
private boolean mIndeterminate; |
| 212 |
@UnsupportedAppUsage(trackingBug = 124049927) |
| 213 |
private boolean mOnlyIndeterminate; |
| 214 |
private Transformation mTransformation; |
| 215 |
private AlphaAnimation mAnimation; |
| 216 |
private boolean mHasAnimation; |
| 217 |
|
| 218 |
private Drawable mIndeterminateDrawable; |
| 219 |
private Drawable mProgressDrawable; |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P) |
| 226 |
private Drawable mCurrentDrawable; |
| 227 |
private ProgressTintInfo mProgressTintInfo; |
| 228 |
|
| 229 |
int mSampleWidth = 0; |
| 230 |
private boolean mNoInvalidate; |
| 231 |
private Interpolator mInterpolator; |
| 232 |
private RefreshProgressRunnable mRefreshProgressRunnable; |
| 233 |
private long mUiThreadId; |
| 234 |
private boolean mShouldStartAnimationDrawable; |
| 235 |
|
| 236 |
private boolean mInDrawing; |
| 237 |
private boolean mAttached; |
| 238 |
private boolean mRefreshIsPosted; |
| 239 |
|
| 240 |
|
| 241 |
private float mVisualProgress; |
| 242 |
|
| 243 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 244 |
boolean mMirrorForRtl = false; |
| 245 |
|
| 246 |
private boolean mAggregatedIsVisible; |
| 247 |
|
| 248 |
private final ArrayList<RefreshData> mRefreshData = new ArrayList<RefreshData>(); |
| 249 |
|
| 250 |
private ObjectAnimator mLastProgressAnimator; |
| 251 |
|
| 252 |
private NumberFormat mPercentFormat; |
| 253 |
private Locale mCachedLocale; |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public ProgressBar(Context context) { |
| 260 |
this(context, null); |
| 261 |
} |
| 262 |
|
| 263 |
public ProgressBar(Context context, AttributeSet attrs) { |
| 264 |
this(context, attrs, com.android.internal.R.attr.progressBarStyle); |
| 265 |
} |
| 266 |
|
| 267 |
public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { |
| 268 |
this(context, attrs, defStyleAttr, 0); |
| 269 |
} |
| 270 |
|
| 271 |
public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 272 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 273 |
|
| 274 |
mUiThreadId = Thread.currentThread().getId(); |
| 275 |
initProgressBar(); |
| 276 |
|
| 277 |
final TypedArray a = context.obtainStyledAttributes( |
| 278 |
attrs, R.styleable.ProgressBar, defStyleAttr, defStyleRes); |
| 279 |
saveAttributeDataForStyleable(context, R.styleable.ProgressBar, |
| 280 |
attrs, a, defStyleAttr, defStyleRes); |
| 281 |
|
| 282 |
mNoInvalidate = true; |
| 283 |
|
| 284 |
final Drawable progressDrawable = a.getDrawable(R.styleable.ProgressBar_progressDrawable); |
| 285 |
if (progressDrawable != null) { |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
if (needsTileify(progressDrawable)) { |
| 290 |
setProgressDrawableTiled(progressDrawable); |
| 291 |
} else { |
| 292 |
setProgressDrawable(progressDrawable); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
mDuration = a.getInt(R.styleable.ProgressBar_indeterminateDuration, mDuration); |
| 297 |
|
| 298 |
mMinWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_minWidth, mMinWidth); |
| 299 |
mMaxWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_maxWidth, mMaxWidth); |
| 300 |
mMinHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_minHeight, mMinHeight); |
| 301 |
mMaxHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_maxHeight, mMaxHeight); |
| 302 |
|
| 303 |
mBehavior = a.getInt(R.styleable.ProgressBar_indeterminateBehavior, mBehavior); |
| 304 |
|
| 305 |
final int resID = a.getResourceId( |
| 306 |
com.android.internal.R.styleable.ProgressBar_interpolator, |
| 307 |
android.R.anim.linear_interpolator); |
| 308 |
if (resID > 0) { |
| 309 |
setInterpolator(context, resID); |
| 310 |
} |
| 311 |
|
| 312 |
setMin(a.getInt(R.styleable.ProgressBar_min, mMin)); |
| 313 |
setMax(a.getInt(R.styleable.ProgressBar_max, mMax)); |
| 314 |
|
| 315 |
setProgress(a.getInt(R.styleable.ProgressBar_progress, mProgress)); |
| 316 |
|
| 317 |
setSecondaryProgress(a.getInt( |
| 318 |
R.styleable.ProgressBar_secondaryProgress, mSecondaryProgress)); |
| 319 |
|
| 320 |
final Drawable indeterminateDrawable = a.getDrawable( |
| 321 |
R.styleable.ProgressBar_indeterminateDrawable); |
| 322 |
if (indeterminateDrawable != null) { |
| 323 |
if (needsTileify(indeterminateDrawable)) { |
| 324 |
setIndeterminateDrawableTiled(indeterminateDrawable); |
| 325 |
} else { |
| 326 |
setIndeterminateDrawable(indeterminateDrawable); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
mOnlyIndeterminate = a.getBoolean( |
| 331 |
R.styleable.ProgressBar_indeterminateOnly, mOnlyIndeterminate); |
| 332 |
|
| 333 |
mNoInvalidate = false; |
| 334 |
|
| 335 |
setIndeterminate(mOnlyIndeterminate || a.getBoolean( |
| 336 |
R.styleable.ProgressBar_indeterminate, mIndeterminate)); |
| 337 |
|
| 338 |
mMirrorForRtl = a.getBoolean(R.styleable.ProgressBar_mirrorForRtl, mMirrorForRtl); |
| 339 |
|
| 340 |
if (a.hasValue(R.styleable.ProgressBar_progressTintMode)) { |
| 341 |
if (mProgressTintInfo == null) { |
| 342 |
mProgressTintInfo = new ProgressTintInfo(); |
| 343 |
} |
| 344 |
mProgressTintInfo.mProgressBlendMode = Drawable.parseBlendMode(a.getInt( |
| 345 |
R.styleable.ProgressBar_progressTintMode, -1), null); |
| 346 |
mProgressTintInfo.mHasProgressTintMode = true; |
| 347 |
} |
| 348 |
|
| 349 |
if (a.hasValue(R.styleable.ProgressBar_progressTint)) { |
| 350 |
if (mProgressTintInfo == null) { |
| 351 |
mProgressTintInfo = new ProgressTintInfo(); |
| 352 |
} |
| 353 |
mProgressTintInfo.mProgressTintList = a.getColorStateList( |
| 354 |
R.styleable.ProgressBar_progressTint); |
| 355 |
mProgressTintInfo.mHasProgressTint = true; |
| 356 |
} |
| 357 |
|
| 358 |
if (a.hasValue(R.styleable.ProgressBar_progressBackgroundTintMode)) { |
| 359 |
if (mProgressTintInfo == null) { |
| 360 |
mProgressTintInfo = new ProgressTintInfo(); |
| 361 |
} |
| 362 |
mProgressTintInfo.mProgressBackgroundBlendMode = Drawable.parseBlendMode(a.getInt( |
| 363 |
R.styleable.ProgressBar_progressBackgroundTintMode, -1), null); |
| 364 |
mProgressTintInfo.mHasProgressBackgroundTintMode = true; |
| 365 |
} |
| 366 |
|
| 367 |
if (a.hasValue(R.styleable.ProgressBar_progressBackgroundTint)) { |
| 368 |
if (mProgressTintInfo == null) { |
| 369 |
mProgressTintInfo = new ProgressTintInfo(); |
| 370 |
} |
| 371 |
mProgressTintInfo.mProgressBackgroundTintList = a.getColorStateList( |
| 372 |
R.styleable.ProgressBar_progressBackgroundTint); |
| 373 |
mProgressTintInfo.mHasProgressBackgroundTint = true; |
| 374 |
} |
| 375 |
|
| 376 |
if (a.hasValue(R.styleable.ProgressBar_secondaryProgressTintMode)) { |
| 377 |
if (mProgressTintInfo == null) { |
| 378 |
mProgressTintInfo = new ProgressTintInfo(); |
| 379 |
} |
| 380 |
mProgressTintInfo.mSecondaryProgressBlendMode = Drawable.parseBlendMode( |
| 381 |
a.getInt(R.styleable.ProgressBar_secondaryProgressTintMode, -1), null); |
| 382 |
mProgressTintInfo.mHasSecondaryProgressTintMode = true; |
| 383 |
} |
| 384 |
|
| 385 |
if (a.hasValue(R.styleable.ProgressBar_secondaryProgressTint)) { |
| 386 |
if (mProgressTintInfo == null) { |
| 387 |
mProgressTintInfo = new ProgressTintInfo(); |
| 388 |
} |
| 389 |
mProgressTintInfo.mSecondaryProgressTintList = a.getColorStateList( |
| 390 |
R.styleable.ProgressBar_secondaryProgressTint); |
| 391 |
mProgressTintInfo.mHasSecondaryProgressTint = true; |
| 392 |
} |
| 393 |
|
| 394 |
if (a.hasValue(R.styleable.ProgressBar_indeterminateTintMode)) { |
| 395 |
if (mProgressTintInfo == null) { |
| 396 |
mProgressTintInfo = new ProgressTintInfo(); |
| 397 |
} |
| 398 |
mProgressTintInfo.mIndeterminateBlendMode = Drawable.parseBlendMode(a.getInt( |
| 399 |
R.styleable.ProgressBar_indeterminateTintMode, -1), null); |
| 400 |
mProgressTintInfo.mHasIndeterminateTintMode = true; |
| 401 |
} |
| 402 |
|
| 403 |
if (a.hasValue(R.styleable.ProgressBar_indeterminateTint)) { |
| 404 |
if (mProgressTintInfo == null) { |
| 405 |
mProgressTintInfo = new ProgressTintInfo(); |
| 406 |
} |
| 407 |
mProgressTintInfo.mIndeterminateTintList = a.getColorStateList( |
| 408 |
R.styleable.ProgressBar_indeterminateTint); |
| 409 |
mProgressTintInfo.mHasIndeterminateTint = true; |
| 410 |
} |
| 411 |
|
| 412 |
a.recycle(); |
| 413 |
|
| 414 |
applyProgressTints(); |
| 415 |
applyIndeterminateTint(); |
| 416 |
|
| 417 |
|
| 418 |
if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) { |
| 419 |
setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
public void setMinWidth(@Px int minWidth) { |
| 429 |
mMinWidth = minWidth; |
| 430 |
requestLayout(); |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
@Px public int getMinWidth() { |
| 437 |
return mMinWidth; |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
public void setMaxWidth(@Px int maxWidth) { |
| 446 |
mMaxWidth = maxWidth; |
| 447 |
requestLayout(); |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
@Px public int getMaxWidth() { |
| 454 |
return mMaxWidth; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
public void setMinHeight(@Px int minHeight) { |
| 463 |
mMinHeight = minHeight; |
| 464 |
requestLayout(); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
@Px public int getMinHeight() { |
| 471 |
return mMinHeight; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
public void setMaxHeight(@Px int maxHeight) { |
| 480 |
mMaxHeight = maxHeight; |
| 481 |
requestLayout(); |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
@Px public int getMaxHeight() { |
| 488 |
return mMaxHeight; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
private static boolean needsTileify(Drawable dr) { |
| 499 |
if (dr instanceof LayerDrawable) { |
| 500 |
final LayerDrawable orig = (LayerDrawable) dr; |
| 501 |
final int N = orig.getNumberOfLayers(); |
| 502 |
for (int i = 0; i < N; i++) { |
| 503 |
if (needsTileify(orig.getDrawable(i))) { |
| 504 |
return true; |
| 505 |
} |
| 506 |
} |
| 507 |
return false; |
| 508 |
} |
| 509 |
|
| 510 |
if (dr instanceof StateListDrawable) { |
| 511 |
final StateListDrawable in = (StateListDrawable) dr; |
| 512 |
final int N = in.getStateCount(); |
| 513 |
for (int i = 0; i < N; i++) { |
| 514 |
if (needsTileify(in.getStateDrawable(i))) { |
| 515 |
return true; |
| 516 |
} |
| 517 |
} |
| 518 |
return false; |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
if (dr instanceof BitmapDrawable) { |
| 524 |
return true; |
| 525 |
} |
| 526 |
|
| 527 |
return false; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
@UnsupportedAppUsage |
| 535 |
private Drawable tileify(Drawable drawable, boolean clip) { |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
if (drawable instanceof LayerDrawable) { |
| 540 |
final LayerDrawable orig = (LayerDrawable) drawable; |
| 541 |
final int N = orig.getNumberOfLayers(); |
| 542 |
final Drawable[] outDrawables = new Drawable[N]; |
| 543 |
|
| 544 |
for (int i = 0; i < N; i++) { |
| 545 |
final int id = orig.getId(i); |
| 546 |
outDrawables[i] = tileify(orig.getDrawable(i), |
| 547 |
(id == R.id.progress || id == R.id.secondaryProgress)); |
| 548 |
} |
| 549 |
|
| 550 |
final LayerDrawable clone = new LayerDrawable(outDrawables); |
| 551 |
for (int i = 0; i < N; i++) { |
| 552 |
clone.setId(i, orig.getId(i)); |
| 553 |
clone.setLayerGravity(i, orig.getLayerGravity(i)); |
| 554 |
clone.setLayerWidth(i, orig.getLayerWidth(i)); |
| 555 |
clone.setLayerHeight(i, orig.getLayerHeight(i)); |
| 556 |
clone.setLayerInsetLeft(i, orig.getLayerInsetLeft(i)); |
| 557 |
clone.setLayerInsetRight(i, orig.getLayerInsetRight(i)); |
| 558 |
clone.setLayerInsetTop(i, orig.getLayerInsetTop(i)); |
| 559 |
clone.setLayerInsetBottom(i, orig.getLayerInsetBottom(i)); |
| 560 |
clone.setLayerInsetStart(i, orig.getLayerInsetStart(i)); |
| 561 |
clone.setLayerInsetEnd(i, orig.getLayerInsetEnd(i)); |
| 562 |
} |
| 563 |
|
| 564 |
return clone; |
| 565 |
} |
| 566 |
|
| 567 |
if (drawable instanceof StateListDrawable) { |
| 568 |
final StateListDrawable in = (StateListDrawable) drawable; |
| 569 |
final StateListDrawable out = new StateListDrawable(); |
| 570 |
final int N = in.getStateCount(); |
| 571 |
for (int i = 0; i < N; i++) { |
| 572 |
out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip)); |
| 573 |
} |
| 574 |
|
| 575 |
return out; |
| 576 |
} |
| 577 |
|
| 578 |
if (drawable instanceof BitmapDrawable) { |
| 579 |
final Drawable.ConstantState cs = drawable.getConstantState(); |
| 580 |
final BitmapDrawable clone = (BitmapDrawable) cs.newDrawable(getResources()); |
| 581 |
clone.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); |
| 582 |
|
| 583 |
if (mSampleWidth <= 0) { |
| 584 |
mSampleWidth = clone.getIntrinsicWidth(); |
| 585 |
} |
| 586 |
|
| 587 |
if (clip) { |
| 588 |
return new ClipDrawable(clone, Gravity.LEFT, ClipDrawable.HORIZONTAL); |
| 589 |
} else { |
| 590 |
return clone; |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
return drawable; |
| 595 |
} |
| 596 |
|
| 597 |
Shape getDrawableShape() { |
| 598 |
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 }; |
| 599 |
return new RoundRectShape(roundedCorners, null, null); |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
private Drawable tileifyIndeterminate(Drawable drawable) { |
| 608 |
if (drawable instanceof AnimationDrawable) { |
| 609 |
AnimationDrawable background = (AnimationDrawable) drawable; |
| 610 |
final int N = background.getNumberOfFrames(); |
| 611 |
AnimationDrawable newBg = new AnimationDrawable(); |
| 612 |
newBg.setOneShot(background.isOneShot()); |
| 613 |
|
| 614 |
for (int i = 0; i < N; i++) { |
| 615 |
Drawable frame = tileify(background.getFrame(i), true); |
| 616 |
frame.setLevel(10000); |
| 617 |
newBg.addFrame(frame, background.getDuration(i)); |
| 618 |
} |
| 619 |
newBg.setLevel(10000); |
| 620 |
drawable = newBg; |
| 621 |
} |
| 622 |
return drawable; |
| 623 |
} |
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
private void initProgressBar() { |
| 638 |
mMin = 0; |
| 639 |
mMax = 100; |
| 640 |
mProgress = 0; |
| 641 |
mSecondaryProgress = 0; |
| 642 |
mIndeterminate = false; |
| 643 |
mOnlyIndeterminate = false; |
| 644 |
mDuration = 4000; |
| 645 |
mBehavior = AlphaAnimation.RESTART; |
| 646 |
mMinWidth = 24; |
| 647 |
mMaxWidth = 48; |
| 648 |
mMinHeight = 24; |
| 649 |
mMaxHeight = 48; |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
@InspectableProperty |
| 658 |
@ViewDebug.ExportedProperty(category = "progress") |
| 659 |
public synchronized boolean isIndeterminate() { |
| 660 |
return mIndeterminate; |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
@android.view.RemotableViewMethod |
| 674 |
public synchronized void setIndeterminate(boolean indeterminate) { |
| 675 |
if ((!mOnlyIndeterminate || !mIndeterminate) && indeterminate != mIndeterminate) { |
| 676 |
mIndeterminate = indeterminate; |
| 677 |
|
| 678 |
if (indeterminate) { |
| 679 |
|
| 680 |
swapCurrentDrawable(mIndeterminateDrawable); |
| 681 |
startAnimation(); |
| 682 |
} else { |
| 683 |
swapCurrentDrawable(mProgressDrawable); |
| 684 |
stopAnimation(); |
| 685 |
} |
| 686 |
|
| 687 |
notifyViewAccessibilityStateChangedIfNeeded( |
| 688 |
AccessibilityEvent.CONTENT_CHANGE_TYPE_UNDEFINED); |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
private void swapCurrentDrawable(Drawable newDrawable) { |
| 693 |
final Drawable oldDrawable = mCurrentDrawable; |
| 694 |
mCurrentDrawable = newDrawable; |
| 695 |
|
| 696 |
if (oldDrawable != mCurrentDrawable) { |
| 697 |
if (oldDrawable != null) { |
| 698 |
oldDrawable.setVisible(false, false); |
| 699 |
} |
| 700 |
if (mCurrentDrawable != null) { |
| 701 |
mCurrentDrawable.setVisible(getWindowVisibility() == VISIBLE && isShown(), false); |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
@InspectableProperty |
| 716 |
public Drawable getIndeterminateDrawable() { |
| 717 |
return mIndeterminateDrawable; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
public void setIndeterminateDrawable(Drawable d) { |
| 738 |
if (mIndeterminateDrawable != d) { |
| 739 |
if (mIndeterminateDrawable != null) { |
| 740 |
mIndeterminateDrawable.setCallback(null); |
| 741 |
unscheduleDrawable(mIndeterminateDrawable); |
| 742 |
} |
| 743 |
|
| 744 |
mIndeterminateDrawable = d; |
| 745 |
|
| 746 |
if (d != null) { |
| 747 |
d.setCallback(this); |
| 748 |
d.setLayoutDirection(getLayoutDirection()); |
| 749 |
if (d.isStateful()) { |
| 750 |
d.setState(getDrawableState()); |
| 751 |
} |
| 752 |
applyIndeterminateTint(); |
| 753 |
} |
| 754 |
|
| 755 |
if (mIndeterminate) { |
| 756 |
swapCurrentDrawable(d); |
| 757 |
postInvalidate(); |
| 758 |
} |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
@RemotableViewMethod |
| 778 |
public void setIndeterminateTintList(@Nullable ColorStateList tint) { |
| 779 |
if (mProgressTintInfo == null) { |
| 780 |
mProgressTintInfo = new ProgressTintInfo(); |
| 781 |
} |
| 782 |
mProgressTintInfo.mIndeterminateTintList = tint; |
| 783 |
mProgressTintInfo.mHasIndeterminateTint = true; |
| 784 |
|
| 785 |
applyIndeterminateTint(); |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
@InspectableProperty(name = "indeterminateTint") |
| 794 |
@Nullable |
| 795 |
public ColorStateList getIndeterminateTintList() { |
| 796 |
return mProgressTintInfo != null ? mProgressTintInfo.mIndeterminateTintList : null; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
public void setIndeterminateTintMode(@Nullable PorterDuff.Mode tintMode) { |
| 812 |
setIndeterminateTintBlendMode(tintMode != null |
| 813 |
? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
@RemotableViewMethod |
| 828 |
public void setIndeterminateTintBlendMode(@Nullable BlendMode blendMode) { |
| 829 |
if (mProgressTintInfo == null) { |
| 830 |
mProgressTintInfo = new ProgressTintInfo(); |
| 831 |
} |
| 832 |
mProgressTintInfo.mIndeterminateBlendMode = blendMode; |
| 833 |
mProgressTintInfo.mHasIndeterminateTintMode = true; |
| 834 |
|
| 835 |
applyIndeterminateTint(); |
| 836 |
} |
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
@InspectableProperty |
| 848 |
@Nullable |
| 849 |
public PorterDuff.Mode getIndeterminateTintMode() { |
| 850 |
BlendMode mode = getIndeterminateTintBlendMode(); |
| 851 |
return mode != null ? BlendMode.blendModeToPorterDuffMode(mode) : null; |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
@InspectableProperty(attributeId = R.styleable.ProgressBar_indeterminateTintMode) |
| 864 |
@Nullable |
| 865 |
public BlendMode getIndeterminateTintBlendMode() { |
| 866 |
return mProgressTintInfo != null ? mProgressTintInfo.mIndeterminateBlendMode : null; |
| 867 |
} |
| 868 |
|
| 869 |
private void applyIndeterminateTint() { |
| 870 |
if (mIndeterminateDrawable != null && mProgressTintInfo != null) { |
| 871 |
final ProgressTintInfo tintInfo = mProgressTintInfo; |
| 872 |
if (tintInfo.mHasIndeterminateTint || tintInfo.mHasIndeterminateTintMode) { |
| 873 |
mIndeterminateDrawable = mIndeterminateDrawable.mutate(); |
| 874 |
|
| 875 |
if (tintInfo.mHasIndeterminateTint) { |
| 876 |
mIndeterminateDrawable.setTintList(tintInfo.mIndeterminateTintList); |
| 877 |
} |
| 878 |
|
| 879 |
if (tintInfo.mHasIndeterminateTintMode) { |
| 880 |
mIndeterminateDrawable.setTintBlendMode(tintInfo.mIndeterminateBlendMode); |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
if (mIndeterminateDrawable.isStateful()) { |
| 886 |
mIndeterminateDrawable.setState(getDrawableState()); |
| 887 |
} |
| 888 |
} |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
public void setIndeterminateDrawableTiled(Drawable d) { |
| 904 |
if (d != null) { |
| 905 |
d = tileifyIndeterminate(d); |
| 906 |
} |
| 907 |
|
| 908 |
setIndeterminateDrawable(d); |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
@InspectableProperty |
| 921 |
public Drawable getProgressDrawable() { |
| 922 |
return mProgressDrawable; |
| 923 |
} |
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
public void setProgressDrawable(Drawable d) { |
| 933 |
if (mProgressDrawable != d) { |
| 934 |
if (mProgressDrawable != null) { |
| 935 |
mProgressDrawable.setCallback(null); |
| 936 |
unscheduleDrawable(mProgressDrawable); |
| 937 |
} |
| 938 |
|
| 939 |
mProgressDrawable = d; |
| 940 |
|
| 941 |
if (d != null) { |
| 942 |
d.setCallback(this); |
| 943 |
d.setLayoutDirection(getLayoutDirection()); |
| 944 |
if (d.isStateful()) { |
| 945 |
d.setState(getDrawableState()); |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
int drawableHeight = d.getMinimumHeight(); |
| 950 |
if (mMaxHeight < drawableHeight) { |
| 951 |
mMaxHeight = drawableHeight; |
| 952 |
requestLayout(); |
| 953 |
} |
| 954 |
|
| 955 |
applyProgressTints(); |
| 956 |
} |
| 957 |
|
| 958 |
if (!mIndeterminate) { |
| 959 |
swapCurrentDrawable(d); |
| 960 |
postInvalidate(); |
| 961 |
} |
| 962 |
|
| 963 |
updateDrawableBounds(getWidth(), getHeight()); |
| 964 |
updateDrawableState(); |
| 965 |
|
| 966 |
doRefreshProgress(R.id.progress, mProgress, false, false, false); |
| 967 |
doRefreshProgress(R.id.secondaryProgress, mSecondaryProgress, false, false, false); |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
@InspectableProperty |
| 975 |
public boolean getMirrorForRtl() { |
| 976 |
return mMirrorForRtl; |
| 977 |
} |
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
private void applyProgressTints() { |
| 983 |
if (mProgressDrawable != null && mProgressTintInfo != null) { |
| 984 |
applyPrimaryProgressTint(); |
| 985 |
applyProgressBackgroundTint(); |
| 986 |
applySecondaryProgressTint(); |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
private void applyPrimaryProgressTint() { |
| 995 |
if (mProgressTintInfo.mHasProgressTint |
| 996 |
|| mProgressTintInfo.mHasProgressTintMode) { |
| 997 |
final Drawable target = getTintTarget(R.id.progress, true); |
| 998 |
if (target != null) { |
| 999 |
if (mProgressTintInfo.mHasProgressTint) { |
| 1000 |
target.setTintList(mProgressTintInfo.mProgressTintList); |
| 1001 |
} |
| 1002 |
if (mProgressTintInfo.mHasProgressTintMode) { |
| 1003 |
target.setTintBlendMode(mProgressTintInfo.mProgressBlendMode); |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
if (target.isStateful()) { |
| 1009 |
target.setState(getDrawableState()); |
| 1010 |
} |
| 1011 |
} |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
private void applyProgressBackgroundTint() { |
| 1020 |
if (mProgressTintInfo.mHasProgressBackgroundTint |
| 1021 |
|| mProgressTintInfo.mHasProgressBackgroundTintMode) { |
| 1022 |
final Drawable target = getTintTarget(R.id.background, false); |
| 1023 |
if (target != null) { |
| 1024 |
if (mProgressTintInfo.mHasProgressBackgroundTint) { |
| 1025 |
target.setTintList(mProgressTintInfo.mProgressBackgroundTintList); |
| 1026 |
} |
| 1027 |
if (mProgressTintInfo.mHasProgressBackgroundTintMode) { |
| 1028 |
target.setTintBlendMode(mProgressTintInfo.mProgressBackgroundBlendMode); |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
if (target.isStateful()) { |
| 1034 |
target.setState(getDrawableState()); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
} |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
private void applySecondaryProgressTint() { |
| 1045 |
if (mProgressTintInfo.mHasSecondaryProgressTint |
| 1046 |
|| mProgressTintInfo.mHasSecondaryProgressTintMode) { |
| 1047 |
final Drawable target = getTintTarget(R.id.secondaryProgress, false); |
| 1048 |
if (target != null) { |
| 1049 |
if (mProgressTintInfo.mHasSecondaryProgressTint) { |
| 1050 |
target.setTintList(mProgressTintInfo.mSecondaryProgressTintList); |
| 1051 |
} |
| 1052 |
if (mProgressTintInfo.mHasSecondaryProgressTintMode) { |
| 1053 |
target.setTintBlendMode(mProgressTintInfo.mSecondaryProgressBlendMode); |
| 1054 |
} |
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
if (target.isStateful()) { |
| 1059 |
target.setState(getDrawableState()); |
| 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 |
@RemotableViewMethod |
| 1086 |
public void setProgressTintList(@Nullable ColorStateList tint) { |
| 1087 |
if (mProgressTintInfo == null) { |
| 1088 |
mProgressTintInfo = new ProgressTintInfo(); |
| 1089 |
} |
| 1090 |
mProgressTintInfo.mProgressTintList = tint; |
| 1091 |
mProgressTintInfo.mHasProgressTint = true; |
| 1092 |
|
| 1093 |
if (mProgressDrawable != null) { |
| 1094 |
applyPrimaryProgressTint(); |
| 1095 |
} |
| 1096 |
} |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
@InspectableProperty(name = "progressTint") |
| 1106 |
@Nullable |
| 1107 |
public ColorStateList getProgressTintList() { |
| 1108 |
return mProgressTintInfo != null ? mProgressTintInfo.mProgressTintList : null; |
| 1109 |
} |
| 1110 |
|
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
public void setProgressTintMode(@Nullable PorterDuff.Mode tintMode) { |
| 1123 |
setProgressTintBlendMode(tintMode != null ? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 1124 |
} |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
@RemotableViewMethod |
| 1138 |
public void setProgressTintBlendMode(@Nullable BlendMode blendMode) { |
| 1139 |
if (mProgressTintInfo == null) { |
| 1140 |
mProgressTintInfo = new ProgressTintInfo(); |
| 1141 |
} |
| 1142 |
mProgressTintInfo.mProgressBlendMode = blendMode; |
| 1143 |
mProgressTintInfo.mHasProgressTintMode = true; |
| 1144 |
|
| 1145 |
if (mProgressDrawable != null) { |
| 1146 |
applyPrimaryProgressTint(); |
| 1147 |
} |
| 1148 |
} |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
@InspectableProperty |
| 1160 |
@Nullable |
| 1161 |
public PorterDuff.Mode getProgressTintMode() { |
| 1162 |
BlendMode mode = getProgressTintBlendMode(); |
| 1163 |
return mode != null ? BlendMode.blendModeToPorterDuffMode(mode) : null; |
| 1164 |
} |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
@InspectableProperty(attributeId = android.R.styleable.ProgressBar_progressTintMode) |
| 1176 |
@Nullable |
| 1177 |
public BlendMode getProgressTintBlendMode() { |
| 1178 |
return mProgressTintInfo != null ? mProgressTintInfo.mProgressBlendMode : null; |
| 1179 |
} |
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
@RemotableViewMethod |
| 1202 |
public void setProgressBackgroundTintList(@Nullable ColorStateList tint) { |
| 1203 |
if (mProgressTintInfo == null) { |
| 1204 |
mProgressTintInfo = new ProgressTintInfo(); |
| 1205 |
} |
| 1206 |
mProgressTintInfo.mProgressBackgroundTintList = tint; |
| 1207 |
mProgressTintInfo.mHasProgressBackgroundTint = true; |
| 1208 |
|
| 1209 |
if (mProgressDrawable != null) { |
| 1210 |
applyProgressBackgroundTint(); |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
@InspectableProperty(name = "progressBackgroundTint") |
| 1222 |
@Nullable |
| 1223 |
public ColorStateList getProgressBackgroundTintList() { |
| 1224 |
return mProgressTintInfo != null ? mProgressTintInfo.mProgressBackgroundTintList : null; |
| 1225 |
} |
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
public void setProgressBackgroundTintMode(@Nullable PorterDuff.Mode tintMode) { |
| 1239 |
setProgressBackgroundTintBlendMode(tintMode != null |
| 1240 |
? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 1241 |
} |
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
@RemotableViewMethod |
| 1255 |
public void setProgressBackgroundTintBlendMode(@Nullable BlendMode blendMode) { |
| 1256 |
if (mProgressTintInfo == null) { |
| 1257 |
mProgressTintInfo = new ProgressTintInfo(); |
| 1258 |
} |
| 1259 |
mProgressTintInfo.mProgressBackgroundBlendMode = blendMode; |
| 1260 |
mProgressTintInfo.mHasProgressBackgroundTintMode = true; |
| 1261 |
|
| 1262 |
if (mProgressDrawable != null) { |
| 1263 |
applyProgressBackgroundTint(); |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
@InspectableProperty |
| 1274 |
@Nullable |
| 1275 |
public PorterDuff.Mode getProgressBackgroundTintMode() { |
| 1276 |
BlendMode mode = getProgressBackgroundTintBlendMode(); |
| 1277 |
return mode != null ? BlendMode.blendModeToPorterDuffMode(mode) : null; |
| 1278 |
} |
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
@InspectableProperty(attributeId = R.styleable.ProgressBar_progressBackgroundTintMode) |
| 1287 |
@Nullable |
| 1288 |
public BlendMode getProgressBackgroundTintBlendMode() { |
| 1289 |
return mProgressTintInfo != null ? mProgressTintInfo.mProgressBackgroundBlendMode : null; |
| 1290 |
} |
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
@RemotableViewMethod |
| 1313 |
public void setSecondaryProgressTintList(@Nullable ColorStateList tint) { |
| 1314 |
if (mProgressTintInfo == null) { |
| 1315 |
mProgressTintInfo = new ProgressTintInfo(); |
| 1316 |
} |
| 1317 |
mProgressTintInfo.mSecondaryProgressTintList = tint; |
| 1318 |
mProgressTintInfo.mHasSecondaryProgressTint = true; |
| 1319 |
|
| 1320 |
if (mProgressDrawable != null) { |
| 1321 |
applySecondaryProgressTint(); |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
@InspectableProperty(name = "secondaryProgressTint") |
| 1334 |
@Nullable |
| 1335 |
public ColorStateList getSecondaryProgressTintList() { |
| 1336 |
return mProgressTintInfo != null ? mProgressTintInfo.mSecondaryProgressTintList : null; |
| 1337 |
} |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
|
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
|
| 1351 |
public void setSecondaryProgressTintMode(@Nullable PorterDuff.Mode tintMode) { |
| 1352 |
setSecondaryProgressTintBlendMode(tintMode != null |
| 1353 |
? BlendMode.fromValue(tintMode.nativeInt) : null); |
| 1354 |
} |
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
|
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
|
| 1368 |
@RemotableViewMethod |
| 1369 |
public void setSecondaryProgressTintBlendMode(@Nullable BlendMode blendMode) { |
| 1370 |
if (mProgressTintInfo == null) { |
| 1371 |
mProgressTintInfo = new ProgressTintInfo(); |
| 1372 |
} |
| 1373 |
mProgressTintInfo.mSecondaryProgressBlendMode = blendMode; |
| 1374 |
mProgressTintInfo.mHasSecondaryProgressTintMode = true; |
| 1375 |
|
| 1376 |
if (mProgressDrawable != null) { |
| 1377 |
applySecondaryProgressTint(); |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
@InspectableProperty |
| 1391 |
@Nullable |
| 1392 |
public PorterDuff.Mode getSecondaryProgressTintMode() { |
| 1393 |
BlendMode mode = getSecondaryProgressTintBlendMode(); |
| 1394 |
return mode != null ? BlendMode.blendModeToPorterDuffMode(mode) : null; |
| 1395 |
} |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
@InspectableProperty(attributeId = android.R.styleable.ProgressBar_secondaryProgressTintMode) |
| 1407 |
@Nullable |
| 1408 |
public BlendMode getSecondaryProgressTintBlendMode() { |
| 1409 |
return mProgressTintInfo != null ? mProgressTintInfo.mSecondaryProgressBlendMode : null; |
| 1410 |
} |
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
@Nullable |
| 1421 |
private Drawable getTintTarget(int layerId, boolean shouldFallback) { |
| 1422 |
Drawable layer = null; |
| 1423 |
|
| 1424 |
final Drawable d = mProgressDrawable; |
| 1425 |
if (d != null) { |
| 1426 |
mProgressDrawable = d.mutate(); |
| 1427 |
|
| 1428 |
if (d instanceof LayerDrawable) { |
| 1429 |
layer = ((LayerDrawable) d).findDrawableByLayerId(layerId); |
| 1430 |
} |
| 1431 |
|
| 1432 |
if (shouldFallback && layer == null) { |
| 1433 |
layer = d; |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
return layer; |
| 1438 |
} |
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
public void setProgressDrawableTiled(Drawable d) { |
| 1452 |
if (d != null) { |
| 1453 |
d = tileify(d, false); |
| 1454 |
} |
| 1455 |
|
| 1456 |
setProgressDrawable(d); |
| 1457 |
} |
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
@Nullable |
| 1467 |
public Drawable getCurrentDrawable() { |
| 1468 |
return mCurrentDrawable; |
| 1469 |
} |
| 1470 |
|
| 1471 |
@Override |
| 1472 |
protected boolean verifyDrawable(@NonNull Drawable who) { |
| 1473 |
return who == mProgressDrawable || who == mIndeterminateDrawable |
| 1474 |
|| super.verifyDrawable(who); |
| 1475 |
} |
| 1476 |
|
| 1477 |
@Override |
| 1478 |
public void jumpDrawablesToCurrentState() { |
| 1479 |
super.jumpDrawablesToCurrentState(); |
| 1480 |
if (mProgressDrawable != null) mProgressDrawable.jumpToCurrentState(); |
| 1481 |
if (mIndeterminateDrawable != null) mIndeterminateDrawable.jumpToCurrentState(); |
| 1482 |
} |
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
@Override |
| 1488 |
public void onResolveDrawables(int layoutDirection) { |
| 1489 |
final Drawable d = mCurrentDrawable; |
| 1490 |
if (d != null) { |
| 1491 |
d.setLayoutDirection(layoutDirection); |
| 1492 |
} |
| 1493 |
if (mIndeterminateDrawable != null) { |
| 1494 |
mIndeterminateDrawable.setLayoutDirection(layoutDirection); |
| 1495 |
} |
| 1496 |
if (mProgressDrawable != null) { |
| 1497 |
mProgressDrawable.setLayoutDirection(layoutDirection); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
@Override |
| 1502 |
public void postInvalidate() { |
| 1503 |
if (!mNoInvalidate) { |
| 1504 |
super.postInvalidate(); |
| 1505 |
} |
| 1506 |
} |
| 1507 |
|
| 1508 |
private class RefreshProgressRunnable implements Runnable { |
| 1509 |
public void run() { |
| 1510 |
synchronized (ProgressBar.this) { |
| 1511 |
final int count = mRefreshData.size(); |
| 1512 |
for (int i = 0; i < count; i++) { |
| 1513 |
final RefreshData rd = mRefreshData.get(i); |
| 1514 |
doRefreshProgress(rd.id, rd.progress, rd.fromUser, true, rd.animate); |
| 1515 |
rd.recycle(); |
| 1516 |
} |
| 1517 |
mRefreshData.clear(); |
| 1518 |
mRefreshIsPosted = false; |
| 1519 |
} |
| 1520 |
} |
| 1521 |
} |
| 1522 |
|
| 1523 |
private static class RefreshData { |
| 1524 |
private static final int POOL_MAX = 24; |
| 1525 |
private static final SynchronizedPool<RefreshData> sPool = |
| 1526 |
new SynchronizedPool<RefreshData>(POOL_MAX); |
| 1527 |
|
| 1528 |
public int id; |
| 1529 |
public int progress; |
| 1530 |
public boolean fromUser; |
| 1531 |
public boolean animate; |
| 1532 |
|
| 1533 |
public static RefreshData obtain(int id, int progress, boolean fromUser, boolean animate) { |
| 1534 |
RefreshData rd = sPool.acquire(); |
| 1535 |
if (rd == null) { |
| 1536 |
rd = new RefreshData(); |
| 1537 |
} |
| 1538 |
rd.id = id; |
| 1539 |
rd.progress = progress; |
| 1540 |
rd.fromUser = fromUser; |
| 1541 |
rd.animate = animate; |
| 1542 |
return rd; |
| 1543 |
} |
| 1544 |
|
| 1545 |
public void recycle() { |
| 1546 |
sPool.release(this); |
| 1547 |
} |
| 1548 |
} |
| 1549 |
|
| 1550 |
private synchronized void doRefreshProgress(int id, int progress, boolean fromUser, |
| 1551 |
boolean callBackToApp, boolean animate) { |
| 1552 |
int range = mMax - mMin; |
| 1553 |
final float scale = range > 0 ? (progress - mMin) / (float) range : 0; |
| 1554 |
final boolean isPrimary = id == R.id.progress; |
| 1555 |
|
| 1556 |
if (isPrimary && animate) { |
| 1557 |
final ObjectAnimator animator = ObjectAnimator.ofFloat(this, VISUAL_PROGRESS, scale); |
| 1558 |
animator.setAutoCancel(true); |
| 1559 |
animator.setDuration(PROGRESS_ANIM_DURATION); |
| 1560 |
animator.setInterpolator(PROGRESS_ANIM_INTERPOLATOR); |
| 1561 |
animator.addListener(new AnimatorListenerAdapter() { |
| 1562 |
@Override |
| 1563 |
public void onAnimationEnd(Animator animation) { |
| 1564 |
mLastProgressAnimator = null; |
| 1565 |
} |
| 1566 |
}); |
| 1567 |
animator.start(); |
| 1568 |
mLastProgressAnimator = animator; |
| 1569 |
} else { |
| 1570 |
if (isPrimary && mLastProgressAnimator != null) { |
| 1571 |
mLastProgressAnimator.cancel(); |
| 1572 |
mLastProgressAnimator = null; |
| 1573 |
} |
| 1574 |
setVisualProgress(id, scale); |
| 1575 |
} |
| 1576 |
|
| 1577 |
if (isPrimary && callBackToApp) { |
| 1578 |
onProgressRefresh(scale, fromUser, progress); |
| 1579 |
} |
| 1580 |
} |
| 1581 |
|
| 1582 |
private float getPercent(int progress) { |
| 1583 |
final float maxProgress = getMax(); |
| 1584 |
final float minProgress = getMin(); |
| 1585 |
final float currentProgress = progress; |
| 1586 |
final float diffProgress = maxProgress - minProgress; |
| 1587 |
if (diffProgress <= 0.0f) { |
| 1588 |
return 0.0f; |
| 1589 |
} |
| 1590 |
final float percent = (currentProgress - minProgress) / diffProgress; |
| 1591 |
return Math.max(0.0f, Math.min(1.0f, percent)); |
| 1592 |
} |
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
private CharSequence formatStateDescription(int progress) { |
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
final Locale curLocale = mContext.getResources().getConfiguration().getLocales().get(0); |
| 1606 |
if (!curLocale.equals(mCachedLocale)) { |
| 1607 |
mCachedLocale = curLocale; |
| 1608 |
mPercentFormat = NumberFormat.getPercentInstance(curLocale); |
| 1609 |
} |
| 1610 |
return mPercentFormat.format(getPercent(progress)); |
| 1611 |
} |
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
|
| 1621 |
|
| 1622 |
|
| 1623 |
@Override |
| 1624 |
@RemotableViewMethod |
| 1625 |
public void setStateDescription(@Nullable CharSequence stateDescription) { |
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
super.setStateDescription(stateDescription); |
| 1632 |
} |
| 1633 |
|
| 1634 |
void onProgressRefresh(float scale, boolean fromUser, int progress) { |
| 1635 |
if (AccessibilityManager.getInstance(mContext).isEnabled() |
| 1636 |
&& getStateDescription() == null && !isIndeterminate()) { |
| 1637 |
AccessibilityEvent event = AccessibilityEvent.obtain(); |
| 1638 |
event.setEventType(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); |
| 1639 |
event.setContentChangeTypes(AccessibilityEvent.CONTENT_CHANGE_TYPE_STATE_DESCRIPTION); |
| 1640 |
sendAccessibilityEventUnchecked(event); |
| 1641 |
} |
| 1642 |
} |
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
private void setVisualProgress(int id, float progress) { |
| 1651 |
mVisualProgress = progress; |
| 1652 |
|
| 1653 |
Drawable d = mCurrentDrawable; |
| 1654 |
|
| 1655 |
if (d instanceof LayerDrawable) { |
| 1656 |
d = ((LayerDrawable) d).findDrawableByLayerId(id); |
| 1657 |
if (d == null) { |
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
d = mCurrentDrawable; |
| 1663 |
} |
| 1664 |
} |
| 1665 |
|
| 1666 |
if (d != null) { |
| 1667 |
final int level = (int) (progress * MAX_LEVEL); |
| 1668 |
d.setLevel(level); |
| 1669 |
} else { |
| 1670 |
invalidate(); |
| 1671 |
} |
| 1672 |
|
| 1673 |
onVisualProgressChanged(id, progress); |
| 1674 |
} |
| 1675 |
|
| 1676 |
|
| 1677 |
|
| 1678 |
|
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
void onVisualProgressChanged(int id, float progress) { |
| 1683 |
|
| 1684 |
} |
| 1685 |
|
| 1686 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1687 |
private synchronized void refreshProgress(int id, int progress, boolean fromUser, |
| 1688 |
boolean animate) { |
| 1689 |
if (mUiThreadId == Thread.currentThread().getId()) { |
| 1690 |
doRefreshProgress(id, progress, fromUser, true, animate); |
| 1691 |
} else { |
| 1692 |
if (mRefreshProgressRunnable == null) { |
| 1693 |
mRefreshProgressRunnable = new RefreshProgressRunnable(); |
| 1694 |
} |
| 1695 |
|
| 1696 |
final RefreshData rd = RefreshData.obtain(id, progress, fromUser, animate); |
| 1697 |
mRefreshData.add(rd); |
| 1698 |
if (mAttached && !mRefreshIsPosted) { |
| 1699 |
post(mRefreshProgressRunnable); |
| 1700 |
mRefreshIsPosted = true; |
| 1701 |
} |
| 1702 |
} |
| 1703 |
} |
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
|
| 1717 |
|
| 1718 |
|
| 1719 |
|
| 1720 |
@android.view.RemotableViewMethod |
| 1721 |
public synchronized void setProgress(int progress) { |
| 1722 |
setProgressInternal(progress, false, false); |
| 1723 |
} |
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
public void setProgress(int progress, boolean animate) { |
| 1737 |
setProgressInternal(progress, false, animate); |
| 1738 |
} |
| 1739 |
|
| 1740 |
@android.view.RemotableViewMethod |
| 1741 |
@UnsupportedAppUsage |
| 1742 |
synchronized boolean setProgressInternal(int progress, boolean fromUser, boolean animate) { |
| 1743 |
if (mIndeterminate) { |
| 1744 |
|
| 1745 |
return false; |
| 1746 |
} |
| 1747 |
|
| 1748 |
progress = MathUtils.constrain(progress, mMin, mMax); |
| 1749 |
|
| 1750 |
if (progress == mProgress) { |
| 1751 |
|
| 1752 |
return false; |
| 1753 |
} |
| 1754 |
|
| 1755 |
mProgress = progress; |
| 1756 |
refreshProgress(R.id.progress, mProgress, fromUser, animate); |
| 1757 |
return true; |
| 1758 |
} |
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
@android.view.RemotableViewMethod |
| 1774 |
public synchronized void setSecondaryProgress(int secondaryProgress) { |
| 1775 |
if (mIndeterminate) { |
| 1776 |
return; |
| 1777 |
} |
| 1778 |
|
| 1779 |
if (secondaryProgress < mMin) { |
| 1780 |
secondaryProgress = mMin; |
| 1781 |
} |
| 1782 |
|
| 1783 |
if (secondaryProgress > mMax) { |
| 1784 |
secondaryProgress = mMax; |
| 1785 |
} |
| 1786 |
|
| 1787 |
if (secondaryProgress != mSecondaryProgress) { |
| 1788 |
mSecondaryProgress = secondaryProgress; |
| 1789 |
refreshProgress(R.id.secondaryProgress, mSecondaryProgress, false, false); |
| 1790 |
} |
| 1791 |
} |
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
|
| 1805 |
@ViewDebug.ExportedProperty(category = "progress") |
| 1806 |
@InspectableProperty |
| 1807 |
public synchronized int getProgress() { |
| 1808 |
return mIndeterminate ? 0 : mProgress; |
| 1809 |
} |
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
@ViewDebug.ExportedProperty(category = "progress") |
| 1824 |
@InspectableProperty |
| 1825 |
public synchronized int getSecondaryProgress() { |
| 1826 |
return mIndeterminate ? 0 : mSecondaryProgress; |
| 1827 |
} |
| 1828 |
|
| 1829 |
|
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
|
| 1838 |
@ViewDebug.ExportedProperty(category = "progress") |
| 1839 |
@InspectableProperty |
| 1840 |
public synchronized int getMin() { |
| 1841 |
return mMin; |
| 1842 |
} |
| 1843 |
|
| 1844 |
|
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
|
| 1850 |
|
| 1851 |
|
| 1852 |
|
| 1853 |
@ViewDebug.ExportedProperty(category = "progress") |
| 1854 |
@InspectableProperty |
| 1855 |
public synchronized int getMax() { |
| 1856 |
return mMax; |
| 1857 |
} |
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
|
| 1862 |
|
| 1863 |
|
| 1864 |
|
| 1865 |
|
| 1866 |
|
| 1867 |
|
| 1868 |
@android.view.RemotableViewMethod |
| 1869 |
public synchronized void setMin(int min) { |
| 1870 |
if (mMaxInitialized) { |
| 1871 |
if (min > mMax) { |
| 1872 |
min = mMax; |
| 1873 |
} |
| 1874 |
} |
| 1875 |
mMinInitialized = true; |
| 1876 |
if (mMaxInitialized && min != mMin) { |
| 1877 |
mMin = min; |
| 1878 |
postInvalidate(); |
| 1879 |
|
| 1880 |
if (mProgress < min) { |
| 1881 |
mProgress = min; |
| 1882 |
} |
| 1883 |
refreshProgress(R.id.progress, mProgress, false, false); |
| 1884 |
} else { |
| 1885 |
mMin = min; |
| 1886 |
} |
| 1887 |
} |
| 1888 |
|
| 1889 |
|
| 1890 |
|
| 1891 |
|
| 1892 |
|
| 1893 |
|
| 1894 |
|
| 1895 |
|
| 1896 |
|
| 1897 |
|
| 1898 |
@android.view.RemotableViewMethod |
| 1899 |
public synchronized void setMax(int max) { |
| 1900 |
if (mMinInitialized) { |
| 1901 |
if (max < mMin) { |
| 1902 |
max = mMin; |
| 1903 |
} |
| 1904 |
} |
| 1905 |
mMaxInitialized = true; |
| 1906 |
if (mMinInitialized && max != mMax) { |
| 1907 |
mMax = max; |
| 1908 |
postInvalidate(); |
| 1909 |
|
| 1910 |
if (mProgress > max) { |
| 1911 |
mProgress = max; |
| 1912 |
} |
| 1913 |
refreshProgress(R.id.progress, mProgress, false, false); |
| 1914 |
} else { |
| 1915 |
mMax = max; |
| 1916 |
} |
| 1917 |
} |
| 1918 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
public synchronized final void incrementProgressBy(int diff) { |
| 1927 |
setProgress(mProgress + diff); |
| 1928 |
} |
| 1929 |
|
| 1930 |
|
| 1931 |
|
| 1932 |
|
| 1933 |
|
| 1934 |
|
| 1935 |
|
| 1936 |
|
| 1937 |
public synchronized final void incrementSecondaryProgressBy(int diff) { |
| 1938 |
setSecondaryProgress(mSecondaryProgress + diff); |
| 1939 |
} |
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
@UnsupportedAppUsage |
| 1945 |
void startAnimation() { |
| 1946 |
if (getVisibility() != VISIBLE || getWindowVisibility() != VISIBLE) { |
| 1947 |
return; |
| 1948 |
} |
| 1949 |
|
| 1950 |
if (mIndeterminateDrawable instanceof Animatable) { |
| 1951 |
mShouldStartAnimationDrawable = true; |
| 1952 |
mHasAnimation = false; |
| 1953 |
} else { |
| 1954 |
mHasAnimation = true; |
| 1955 |
|
| 1956 |
if (mInterpolator == null) { |
| 1957 |
mInterpolator = new LinearInterpolator(); |
| 1958 |
} |
| 1959 |
|
| 1960 |
if (mTransformation == null) { |
| 1961 |
mTransformation = new Transformation(); |
| 1962 |
} else { |
| 1963 |
mTransformation.clear(); |
| 1964 |
} |
| 1965 |
|
| 1966 |
if (mAnimation == null) { |
| 1967 |
mAnimation = new AlphaAnimation(0.0f, 1.0f); |
| 1968 |
} else { |
| 1969 |
mAnimation.reset(); |
| 1970 |
} |
| 1971 |
|
| 1972 |
mAnimation.setRepeatMode(mBehavior); |
| 1973 |
mAnimation.setRepeatCount(Animation.INFINITE); |
| 1974 |
mAnimation.setDuration(mDuration); |
| 1975 |
mAnimation.setInterpolator(mInterpolator); |
| 1976 |
mAnimation.setStartTime(Animation.START_ON_FIRST_FRAME); |
| 1977 |
} |
| 1978 |
postInvalidate(); |
| 1979 |
} |
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
@UnsupportedAppUsage |
| 1985 |
void stopAnimation() { |
| 1986 |
mHasAnimation = false; |
| 1987 |
if (mIndeterminateDrawable instanceof Animatable) { |
| 1988 |
((Animatable) mIndeterminateDrawable).stop(); |
| 1989 |
mShouldStartAnimationDrawable = false; |
| 1990 |
} |
| 1991 |
postInvalidate(); |
| 1992 |
} |
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
|
| 2004 |
|
| 2005 |
|
| 2006 |
|
| 2007 |
|
| 2008 |
|
| 2009 |
|
| 2010 |
|
| 2011 |
|
| 2012 |
public void setInterpolator(Context context, @InterpolatorRes int resID) { |
| 2013 |
setInterpolator(AnimationUtils.loadInterpolator(context, resID)); |
| 2014 |
} |
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
|
| 2024 |
|
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
|
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
public void setInterpolator(Interpolator interpolator) { |
| 2033 |
mInterpolator = interpolator; |
| 2034 |
} |
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
@InspectableProperty |
| 2045 |
public Interpolator getInterpolator() { |
| 2046 |
return mInterpolator; |
| 2047 |
} |
| 2048 |
|
| 2049 |
@Override |
| 2050 |
public void onVisibilityAggregated(boolean isVisible) { |
| 2051 |
super.onVisibilityAggregated(isVisible); |
| 2052 |
|
| 2053 |
if (isVisible != mAggregatedIsVisible) { |
| 2054 |
mAggregatedIsVisible = isVisible; |
| 2055 |
|
| 2056 |
if (mIndeterminate) { |
| 2057 |
|
| 2058 |
if (isVisible) { |
| 2059 |
startAnimation(); |
| 2060 |
} else { |
| 2061 |
stopAnimation(); |
| 2062 |
} |
| 2063 |
} |
| 2064 |
|
| 2065 |
if (mCurrentDrawable != null) { |
| 2066 |
mCurrentDrawable.setVisible(isVisible, false); |
| 2067 |
} |
| 2068 |
} |
| 2069 |
} |
| 2070 |
|
| 2071 |
@Override |
| 2072 |
public void invalidateDrawable(@NonNull Drawable dr) { |
| 2073 |
if (!mInDrawing) { |
| 2074 |
if (verifyDrawable(dr)) { |
| 2075 |
final Rect dirty = dr.getBounds(); |
| 2076 |
final int scrollX = mScrollX + mPaddingLeft; |
| 2077 |
final int scrollY = mScrollY + mPaddingTop; |
| 2078 |
|
| 2079 |
invalidate(dirty.left + scrollX, dirty.top + scrollY, |
| 2080 |
dirty.right + scrollX, dirty.bottom + scrollY); |
| 2081 |
} else { |
| 2082 |
super.invalidateDrawable(dr); |
| 2083 |
} |
| 2084 |
} |
| 2085 |
} |
| 2086 |
|
| 2087 |
@Override |
| 2088 |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 2089 |
updateDrawableBounds(w, h); |
| 2090 |
} |
| 2091 |
|
| 2092 |
private void updateDrawableBounds(int w, int h) { |
| 2093 |
|
| 2094 |
|
| 2095 |
w -= mPaddingRight + mPaddingLeft; |
| 2096 |
h -= mPaddingTop + mPaddingBottom; |
| 2097 |
|
| 2098 |
int right = w; |
| 2099 |
int bottom = h; |
| 2100 |
int top = 0; |
| 2101 |
int left = 0; |
| 2102 |
|
| 2103 |
if (mIndeterminateDrawable != null) { |
| 2104 |
|
| 2105 |
if (mOnlyIndeterminate && !(mIndeterminateDrawable instanceof AnimationDrawable)) { |
| 2106 |
|
| 2107 |
|
| 2108 |
final int intrinsicWidth = mIndeterminateDrawable.getIntrinsicWidth(); |
| 2109 |
final int intrinsicHeight = mIndeterminateDrawable.getIntrinsicHeight(); |
| 2110 |
final float intrinsicAspect = (float) intrinsicWidth / intrinsicHeight; |
| 2111 |
final float boundAspect = (float) w / h; |
| 2112 |
if (intrinsicAspect != boundAspect) { |
| 2113 |
if (boundAspect > intrinsicAspect) { |
| 2114 |
|
| 2115 |
final int width = (int) (h * intrinsicAspect); |
| 2116 |
left = (w - width) / 2; |
| 2117 |
right = left + width; |
| 2118 |
} else { |
| 2119 |
|
| 2120 |
final int height = (int) (w * (1 / intrinsicAspect)); |
| 2121 |
top = (h - height) / 2; |
| 2122 |
bottom = top + height; |
| 2123 |
} |
| 2124 |
} |
| 2125 |
} |
| 2126 |
if (isLayoutRtl() && mMirrorForRtl) { |
| 2127 |
int tempLeft = left; |
| 2128 |
left = w - right; |
| 2129 |
right = w - tempLeft; |
| 2130 |
} |
| 2131 |
mIndeterminateDrawable.setBounds(left, top, right, bottom); |
| 2132 |
} |
| 2133 |
|
| 2134 |
if (mProgressDrawable != null) { |
| 2135 |
mProgressDrawable.setBounds(0, 0, right, bottom); |
| 2136 |
} |
| 2137 |
} |
| 2138 |
|
| 2139 |
@Override |
| 2140 |
protected synchronized void onDraw(Canvas canvas) { |
| 2141 |
super.onDraw(canvas); |
| 2142 |
|
| 2143 |
drawTrack(canvas); |
| 2144 |
} |
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
void drawTrack(Canvas canvas) { |
| 2150 |
final Drawable d = mCurrentDrawable; |
| 2151 |
if (d != null) { |
| 2152 |
|
| 2153 |
|
| 2154 |
final int saveCount = canvas.save(); |
| 2155 |
|
| 2156 |
if (isLayoutRtl() && mMirrorForRtl) { |
| 2157 |
canvas.translate(getWidth() - mPaddingRight, mPaddingTop); |
| 2158 |
canvas.scale(-1.0f, 1.0f); |
| 2159 |
} else { |
| 2160 |
canvas.translate(mPaddingLeft, mPaddingTop); |
| 2161 |
} |
| 2162 |
|
| 2163 |
final long time = getDrawingTime(); |
| 2164 |
if (mHasAnimation) { |
| 2165 |
mAnimation.getTransformation(time, mTransformation); |
| 2166 |
final float scale = mTransformation.getAlpha(); |
| 2167 |
try { |
| 2168 |
mInDrawing = true; |
| 2169 |
d.setLevel((int) (scale * MAX_LEVEL)); |
| 2170 |
} finally { |
| 2171 |
mInDrawing = false; |
| 2172 |
} |
| 2173 |
postInvalidateOnAnimation(); |
| 2174 |
} |
| 2175 |
|
| 2176 |
d.draw(canvas); |
| 2177 |
canvas.restoreToCount(saveCount); |
| 2178 |
|
| 2179 |
if (mShouldStartAnimationDrawable && d instanceof Animatable) { |
| 2180 |
((Animatable) d).start(); |
| 2181 |
mShouldStartAnimationDrawable = false; |
| 2182 |
} |
| 2183 |
} |
| 2184 |
} |
| 2185 |
|
| 2186 |
@Override |
| 2187 |
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 2188 |
int dw = 0; |
| 2189 |
int dh = 0; |
| 2190 |
|
| 2191 |
final Drawable d = mCurrentDrawable; |
| 2192 |
if (d != null) { |
| 2193 |
dw = Math.max(mMinWidth, Math.min(mMaxWidth, d.getIntrinsicWidth())); |
| 2194 |
dh = Math.max(mMinHeight, Math.min(mMaxHeight, d.getIntrinsicHeight())); |
| 2195 |
} |
| 2196 |
|
| 2197 |
updateDrawableState(); |
| 2198 |
|
| 2199 |
dw += mPaddingLeft + mPaddingRight; |
| 2200 |
dh += mPaddingTop + mPaddingBottom; |
| 2201 |
|
| 2202 |
final int measuredWidth = resolveSizeAndState(dw, widthMeasureSpec, 0); |
| 2203 |
final int measuredHeight = resolveSizeAndState(dh, heightMeasureSpec, 0); |
| 2204 |
setMeasuredDimension(measuredWidth, measuredHeight); |
| 2205 |
} |
| 2206 |
|
| 2207 |
@Override |
| 2208 |
protected void drawableStateChanged() { |
| 2209 |
super.drawableStateChanged(); |
| 2210 |
updateDrawableState(); |
| 2211 |
} |
| 2212 |
|
| 2213 |
private void updateDrawableState() { |
| 2214 |
final int[] state = getDrawableState(); |
| 2215 |
boolean changed = false; |
| 2216 |
|
| 2217 |
final Drawable progressDrawable = mProgressDrawable; |
| 2218 |
if (progressDrawable != null && progressDrawable.isStateful()) { |
| 2219 |
changed |= progressDrawable.setState(state); |
| 2220 |
} |
| 2221 |
|
| 2222 |
final Drawable indeterminateDrawable = mIndeterminateDrawable; |
| 2223 |
if (indeterminateDrawable != null && indeterminateDrawable.isStateful()) { |
| 2224 |
changed |= indeterminateDrawable.setState(state); |
| 2225 |
} |
| 2226 |
|
| 2227 |
if (changed) { |
| 2228 |
invalidate(); |
| 2229 |
} |
| 2230 |
} |
| 2231 |
|
| 2232 |
@Override |
| 2233 |
public void drawableHotspotChanged(float x, float y) { |
| 2234 |
super.drawableHotspotChanged(x, y); |
| 2235 |
|
| 2236 |
if (mProgressDrawable != null) { |
| 2237 |
mProgressDrawable.setHotspot(x, y); |
| 2238 |
} |
| 2239 |
|
| 2240 |
if (mIndeterminateDrawable != null) { |
| 2241 |
mIndeterminateDrawable.setHotspot(x, y); |
| 2242 |
} |
| 2243 |
} |
| 2244 |
|
| 2245 |
static class SavedState extends BaseSavedState { |
| 2246 |
int progress; |
| 2247 |
int secondaryProgress; |
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 2252 |
SavedState(Parcelable superState) { |
| 2253 |
super(superState); |
| 2254 |
} |
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
private SavedState(Parcel in) { |
| 2260 |
super(in); |
| 2261 |
progress = in.readInt(); |
| 2262 |
secondaryProgress = in.readInt(); |
| 2263 |
} |
| 2264 |
|
| 2265 |
@Override |
| 2266 |
public void writeToParcel(Parcel out, int flags) { |
| 2267 |
super.writeToParcel(out, flags); |
| 2268 |
out.writeInt(progress); |
| 2269 |
out.writeInt(secondaryProgress); |
| 2270 |
} |
| 2271 |
|
| 2272 |
public static final @android.annotation.NonNull Parcelable.Creator<SavedState> CREATOR |
| 2273 |
= new Parcelable.Creator<SavedState>() { |
| 2274 |
public SavedState createFromParcel(Parcel in) { |
| 2275 |
return new SavedState(in); |
| 2276 |
} |
| 2277 |
|
| 2278 |
public SavedState[] newArray(int size) { |
| 2279 |
return new SavedState[size]; |
| 2280 |
} |
| 2281 |
}; |
| 2282 |
} |
| 2283 |
|
| 2284 |
@Override |
| 2285 |
public Parcelable onSaveInstanceState() { |
| 2286 |
|
| 2287 |
Parcelable superState = super.onSaveInstanceState(); |
| 2288 |
SavedState ss = new SavedState(superState); |
| 2289 |
|
| 2290 |
ss.progress = mProgress; |
| 2291 |
ss.secondaryProgress = mSecondaryProgress; |
| 2292 |
|
| 2293 |
return ss; |
| 2294 |
} |
| 2295 |
|
| 2296 |
@Override |
| 2297 |
public void onRestoreInstanceState(Parcelable state) { |
| 2298 |
SavedState ss = (SavedState) state; |
| 2299 |
super.onRestoreInstanceState(ss.getSuperState()); |
| 2300 |
|
| 2301 |
setProgress(ss.progress); |
| 2302 |
setSecondaryProgress(ss.secondaryProgress); |
| 2303 |
} |
| 2304 |
|
| 2305 |
@Override |
| 2306 |
protected void onAttachedToWindow() { |
| 2307 |
super.onAttachedToWindow(); |
| 2308 |
if (mIndeterminate) { |
| 2309 |
startAnimation(); |
| 2310 |
} |
| 2311 |
if (mRefreshData != null) { |
| 2312 |
synchronized (this) { |
| 2313 |
final int count = mRefreshData.size(); |
| 2314 |
for (int i = 0; i < count; i++) { |
| 2315 |
final RefreshData rd = mRefreshData.get(i); |
| 2316 |
doRefreshProgress(rd.id, rd.progress, rd.fromUser, true, rd.animate); |
| 2317 |
rd.recycle(); |
| 2318 |
} |
| 2319 |
mRefreshData.clear(); |
| 2320 |
} |
| 2321 |
} |
| 2322 |
mAttached = true; |
| 2323 |
} |
| 2324 |
|
| 2325 |
@Override |
| 2326 |
protected void onDetachedFromWindow() { |
| 2327 |
if (mIndeterminate) { |
| 2328 |
stopAnimation(); |
| 2329 |
} |
| 2330 |
if (mRefreshProgressRunnable != null) { |
| 2331 |
removeCallbacks(mRefreshProgressRunnable); |
| 2332 |
mRefreshIsPosted = false; |
| 2333 |
} |
| 2334 |
|
| 2335 |
|
| 2336 |
super.onDetachedFromWindow(); |
| 2337 |
mAttached = false; |
| 2338 |
} |
| 2339 |
|
| 2340 |
@Override |
| 2341 |
public CharSequence getAccessibilityClassName() { |
| 2342 |
return ProgressBar.class.getName(); |
| 2343 |
} |
| 2344 |
|
| 2345 |
|
| 2346 |
@Override |
| 2347 |
public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 2348 |
super.onInitializeAccessibilityEventInternal(event); |
| 2349 |
event.setItemCount(mMax - mMin); |
| 2350 |
event.setCurrentItemIndex(mProgress); |
| 2351 |
} |
| 2352 |
|
| 2353 |
|
| 2354 |
@Override |
| 2355 |
public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 2356 |
super.onInitializeAccessibilityNodeInfoInternal(info); |
| 2357 |
|
| 2358 |
if (!isIndeterminate()) { |
| 2359 |
AccessibilityNodeInfo.RangeInfo rangeInfo = AccessibilityNodeInfo.RangeInfo.obtain( |
| 2360 |
AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_INT, getMin(), getMax(), |
| 2361 |
getProgress()); |
| 2362 |
info.setRangeInfo(rangeInfo); |
| 2363 |
} |
| 2364 |
|
| 2365 |
|
| 2366 |
if (getStateDescription() == null) { |
| 2367 |
if (isIndeterminate()) { |
| 2368 |
info.setStateDescription(getResources().getString(R.string.in_progress)); |
| 2369 |
} else { |
| 2370 |
info.setStateDescription(formatStateDescription(mProgress)); |
| 2371 |
} |
| 2372 |
} |
| 2373 |
} |
| 2374 |
|
| 2375 |
|
| 2376 |
@Override |
| 2377 |
protected void encodeProperties(@NonNull ViewHierarchyEncoder stream) { |
| 2378 |
super.encodeProperties(stream); |
| 2379 |
|
| 2380 |
stream.addProperty("progress:max", getMax()); |
| 2381 |
stream.addProperty("progress:progress", getProgress()); |
| 2382 |
stream.addProperty("progress:secondaryProgress", getSecondaryProgress()); |
| 2383 |
stream.addProperty("progress:indeterminate", isIndeterminate()); |
| 2384 |
} |
| 2385 |
|
| 2386 |
|
| 2387 |
|
| 2388 |
|
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
public boolean isAnimating() { |
| 2395 |
return isIndeterminate() && getWindowVisibility() == VISIBLE && isShown(); |
| 2396 |
} |
| 2397 |
|
| 2398 |
private static class ProgressTintInfo { |
| 2399 |
ColorStateList mIndeterminateTintList; |
| 2400 |
BlendMode mIndeterminateBlendMode; |
| 2401 |
boolean mHasIndeterminateTint; |
| 2402 |
boolean mHasIndeterminateTintMode; |
| 2403 |
|
| 2404 |
ColorStateList mProgressTintList; |
| 2405 |
BlendMode mProgressBlendMode; |
| 2406 |
boolean mHasProgressTint; |
| 2407 |
boolean mHasProgressTintMode; |
| 2408 |
|
| 2409 |
ColorStateList mProgressBackgroundTintList; |
| 2410 |
BlendMode mProgressBackgroundBlendMode; |
| 2411 |
boolean mHasProgressBackgroundTint; |
| 2412 |
boolean mHasProgressBackgroundTintMode; |
| 2413 |
|
| 2414 |
ColorStateList mSecondaryProgressTintList; |
| 2415 |
BlendMode mSecondaryProgressBlendMode; |
| 2416 |
boolean mHasSecondaryProgressTint; |
| 2417 |
boolean mHasSecondaryProgressTintMode; |
| 2418 |
} |
| 2419 |
|
| 2420 |
|
| 2421 |
|
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
private final FloatProperty<ProgressBar> VISUAL_PROGRESS = |
| 2426 |
new FloatProperty<ProgressBar>("visual_progress") { |
| 2427 |
@Override |
| 2428 |
public void setValue(ProgressBar object, float value) { |
| 2429 |
object.setVisualProgress(R.id.progress, value); |
| 2430 |
object.mVisualProgress = value; |
| 2431 |
} |
| 2432 |
|
| 2433 |
@Override |
| 2434 |
public Float get(ProgressBar object) { |
| 2435 |
return object.mVisualProgress; |
| 2436 |
} |
| 2437 |
}; |
| 2438 |
} |