-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressBar.java
More file actions
1939 lines (1716 loc) · 68.1 KB
/
Copy pathProgressBar.java
File metadata and controls
1939 lines (1716 loc) · 68.1 KB
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
/*
* Copyright (C) 2006 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.graphics.PorterDuff;
import com.android.internal.R;
import android.annotation.InterpolatorRes;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.shapes.Shape;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.MathUtils;
import android.util.Pools.SynchronizedPool;
import android.view.Gravity;
import android.view.RemotableViewMethod;
import android.view.View;
import android.view.ViewDebug;
import android.view.ViewHierarchyEncoder;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import android.widget.RemoteViews.RemoteView;
import java.util.ArrayList;
/**
* <p>
* Visual indicator of progress in some operation. Displays a bar to the user
* representing how far the operation has progressed; the application can
* change the amount of progress (modifying the length of the bar) as it moves
* forward. There is also a secondary progress displayable on a progress bar
* which is useful for displaying intermediate progress, such as the buffer
* level during a streaming playback progress bar.
* </p>
*
* <p>
* A progress bar can also be made indeterminate. In indeterminate mode, the
* progress bar shows a cyclic animation without an indication of progress. This mode is used by
* applications when the length of the task is unknown. The indeterminate progress bar can be either
* a spinning wheel or a horizontal bar.
* </p>
*
* <p>The following code example shows how a progress bar can be used from
* a worker thread to update the user interface to notify the user of progress:
* </p>
*
* <pre>
* public class MyActivity extends Activity {
* private static final int PROGRESS = 0x1;
*
* private ProgressBar mProgress;
* private int mProgressStatus = 0;
*
* private Handler mHandler = new Handler();
*
* protected void onCreate(Bundle icicle) {
* super.onCreate(icicle);
*
* setContentView(R.layout.progressbar_activity);
*
* mProgress = (ProgressBar) findViewById(R.id.progress_bar);
*
* // Start lengthy operation in a background thread
* new Thread(new Runnable() {
* public void run() {
* while (mProgressStatus < 100) {
* mProgressStatus = doWork();
*
* // Update the progress bar
* mHandler.post(new Runnable() {
* public void run() {
* mProgress.setProgress(mProgressStatus);
* }
* });
* }
* }
* }).start();
* }
* }</pre>
*
* <p>To add a progress bar to a layout file, you can use the {@code <ProgressBar>} element.
* By default, the progress bar is a spinning wheel (an indeterminate indicator). To change to a
* horizontal progress bar, apply the {@link android.R.style#Widget_ProgressBar_Horizontal
* Widget.ProgressBar.Horizontal} style, like so:</p>
*
* <pre>
* <ProgressBar
* style="@android:style/Widget.ProgressBar.Horizontal"
* ... /></pre>
*
* <p>If you will use the progress bar to show real progress, you must use the horizontal bar. You
* can then increment the progress with {@link #incrementProgressBy incrementProgressBy()} or
* {@link #setProgress setProgress()}. By default, the progress bar is full when it reaches 100. If
* necessary, you can adjust the maximum value (the value for a full bar) using the {@link
* android.R.styleable#ProgressBar_max android:max} attribute. Other attributes available are listed
* below.</p>
*
* <p>Another common style to apply to the progress bar is {@link
* android.R.style#Widget_ProgressBar_Small Widget.ProgressBar.Small}, which shows a smaller
* version of the spinning wheel—useful when waiting for content to load.
* For example, you can insert this kind of progress bar into your default layout for
* a view that will be populated by some content fetched from the Internet—the spinning wheel
* appears immediately and when your application receives the content, it replaces the progress bar
* with the loaded content. For example:</p>
*
* <pre>
* <LinearLayout
* android:orientation="horizontal"
* ... >
* <ProgressBar
* android:layout_width="wrap_content"
* android:layout_height="wrap_content"
* style="@android:style/Widget.ProgressBar.Small"
* android:layout_marginRight="5dp" />
* <TextView
* android:layout_width="wrap_content"
* android:layout_height="wrap_content"
* android:text="@string/loading" />
* </LinearLayout></pre>
*
* <p>Other progress bar styles provided by the system include:</p>
* <ul>
* <li>{@link android.R.style#Widget_ProgressBar_Horizontal Widget.ProgressBar.Horizontal}</li>
* <li>{@link android.R.style#Widget_ProgressBar_Small Widget.ProgressBar.Small}</li>
* <li>{@link android.R.style#Widget_ProgressBar_Large Widget.ProgressBar.Large}</li>
* <li>{@link android.R.style#Widget_ProgressBar_Inverse Widget.ProgressBar.Inverse}</li>
* <li>{@link android.R.style#Widget_ProgressBar_Small_Inverse
* Widget.ProgressBar.Small.Inverse}</li>
* <li>{@link android.R.style#Widget_ProgressBar_Large_Inverse
* Widget.ProgressBar.Large.Inverse}</li>
* </ul>
* <p>The "inverse" styles provide an inverse color scheme for the spinner, which may be necessary
* if your application uses a light colored theme (a white background).</p>
*
* <p><strong>XML attributes</b></strong>
* <p>
* See {@link android.R.styleable#ProgressBar ProgressBar Attributes},
* {@link android.R.styleable#View View Attributes}
* </p>
*
* @attr ref android.R.styleable#ProgressBar_animationResolution
* @attr ref android.R.styleable#ProgressBar_indeterminate
* @attr ref android.R.styleable#ProgressBar_indeterminateBehavior
* @attr ref android.R.styleable#ProgressBar_indeterminateDrawable
* @attr ref android.R.styleable#ProgressBar_indeterminateDuration
* @attr ref android.R.styleable#ProgressBar_indeterminateOnly
* @attr ref android.R.styleable#ProgressBar_interpolator
* @attr ref android.R.styleable#ProgressBar_max
* @attr ref android.R.styleable#ProgressBar_maxHeight
* @attr ref android.R.styleable#ProgressBar_maxWidth
* @attr ref android.R.styleable#ProgressBar_minHeight
* @attr ref android.R.styleable#ProgressBar_minWidth
* @attr ref android.R.styleable#ProgressBar_mirrorForRtl
* @attr ref android.R.styleable#ProgressBar_progress
* @attr ref android.R.styleable#ProgressBar_progressDrawable
* @attr ref android.R.styleable#ProgressBar_secondaryProgress
*/
@RemoteView
public class ProgressBar extends View {
private static final int MAX_LEVEL = 10000;
private static final int TIMEOUT_SEND_ACCESSIBILITY_EVENT = 200;
int mMinWidth;
int mMaxWidth;
int mMinHeight;
int mMaxHeight;
private int mProgress;
private int mSecondaryProgress;
private int mMax;
private int mBehavior;
private int mDuration;
private boolean mIndeterminate;
private boolean mOnlyIndeterminate;
private Transformation mTransformation;
private AlphaAnimation mAnimation;
private boolean mHasAnimation;
private Drawable mIndeterminateDrawable;
private Drawable mProgressDrawable;
private Drawable mCurrentDrawable;
private ProgressTintInfo mProgressTintInfo;
Bitmap mSampleTile;
private boolean mNoInvalidate;
private Interpolator mInterpolator;
private RefreshProgressRunnable mRefreshProgressRunnable;
private long mUiThreadId;
private boolean mShouldStartAnimationDrawable;
private boolean mInDrawing;
private boolean mAttached;
private boolean mRefreshIsPosted;
boolean mMirrorForRtl = false;
private final ArrayList<RefreshData> mRefreshData = new ArrayList<RefreshData>();
private AccessibilityEventSender mAccessibilityEventSender;
/**
* Create a new progress bar with range 0...100 and initial progress of 0.
* @param context the application environment
*/
public ProgressBar(Context context) {
this(context, null);
}
public ProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.progressBarStyle);
}
public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
mUiThreadId = Thread.currentThread().getId();
initProgressBar();
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.ProgressBar, defStyleAttr, defStyleRes);
mNoInvalidate = true;
final Drawable progressDrawable = a.getDrawable(R.styleable.ProgressBar_progressDrawable);
if (progressDrawable != null) {
// Calling setProgressDrawable can set mMaxHeight, so make sure the
// corresponding XML attribute for mMaxHeight is read after calling
// this method.
if (needsTileify(progressDrawable)) {
setProgressDrawableTiled(progressDrawable);
} else {
setProgressDrawable(progressDrawable);
}
}
mDuration = a.getInt(R.styleable.ProgressBar_indeterminateDuration, mDuration);
mMinWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_minWidth, mMinWidth);
mMaxWidth = a.getDimensionPixelSize(R.styleable.ProgressBar_maxWidth, mMaxWidth);
mMinHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_minHeight, mMinHeight);
mMaxHeight = a.getDimensionPixelSize(R.styleable.ProgressBar_maxHeight, mMaxHeight);
mBehavior = a.getInt(R.styleable.ProgressBar_indeterminateBehavior, mBehavior);
final int resID = a.getResourceId(
com.android.internal.R.styleable.ProgressBar_interpolator,
android.R.anim.linear_interpolator); // default to linear interpolator
if (resID > 0) {
setInterpolator(context, resID);
}
setMax(a.getInt(R.styleable.ProgressBar_max, mMax));
setProgress(a.getInt(R.styleable.ProgressBar_progress, mProgress));
setSecondaryProgress(a.getInt(
R.styleable.ProgressBar_secondaryProgress, mSecondaryProgress));
final Drawable indeterminateDrawable = a.getDrawable(
R.styleable.ProgressBar_indeterminateDrawable);
if (indeterminateDrawable != null) {
if (needsTileify(indeterminateDrawable)) {
setIndeterminateDrawableTiled(indeterminateDrawable);
} else {
setIndeterminateDrawable(indeterminateDrawable);
}
}
mOnlyIndeterminate = a.getBoolean(
R.styleable.ProgressBar_indeterminateOnly, mOnlyIndeterminate);
mNoInvalidate = false;
setIndeterminate(mOnlyIndeterminate || a.getBoolean(
R.styleable.ProgressBar_indeterminate, mIndeterminate));
mMirrorForRtl = a.getBoolean(R.styleable.ProgressBar_mirrorForRtl, mMirrorForRtl);
if (a.hasValue(R.styleable.ProgressBar_progressTintMode)) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mProgressTintMode = Drawable.parseTintMode(a.getInt(
R.styleable.ProgressBar_progressTintMode, -1), null);
mProgressTintInfo.mHasProgressTintMode = true;
}
if (a.hasValue(R.styleable.ProgressBar_progressTint)) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mProgressTintList = a.getColorStateList(
R.styleable.ProgressBar_progressTint);
mProgressTintInfo.mHasProgressTint = true;
}
if (a.hasValue(R.styleable.ProgressBar_progressBackgroundTintMode)) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mProgressBackgroundTintMode = Drawable.parseTintMode(a.getInt(
R.styleable.ProgressBar_progressBackgroundTintMode, -1), null);
mProgressTintInfo.mHasProgressBackgroundTintMode = true;
}
if (a.hasValue(R.styleable.ProgressBar_progressBackgroundTint)) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mProgressBackgroundTintList = a.getColorStateList(
R.styleable.ProgressBar_progressBackgroundTint);
mProgressTintInfo.mHasProgressBackgroundTint = true;
}
if (a.hasValue(R.styleable.ProgressBar_secondaryProgressTintMode)) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mSecondaryProgressTintMode = Drawable.parseTintMode(
a.getInt(R.styleable.ProgressBar_secondaryProgressTintMode, -1), null);
mProgressTintInfo.mHasSecondaryProgressTintMode = true;
}
if (a.hasValue(R.styleable.ProgressBar_secondaryProgressTint)) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mSecondaryProgressTintList = a.getColorStateList(
R.styleable.ProgressBar_secondaryProgressTint);
mProgressTintInfo.mHasSecondaryProgressTint = true;
}
if (a.hasValue(R.styleable.ProgressBar_indeterminateTintMode)) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mIndeterminateTintMode = Drawable.parseTintMode(a.getInt(
R.styleable.ProgressBar_indeterminateTintMode, -1), null);
mProgressTintInfo.mHasIndeterminateTintMode = true;
}
if (a.hasValue(R.styleable.ProgressBar_indeterminateTint)) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mIndeterminateTintList = a.getColorStateList(
R.styleable.ProgressBar_indeterminateTint);
mProgressTintInfo.mHasIndeterminateTint = true;
}
a.recycle();
applyProgressTints();
applyIndeterminateTint();
// If not explicitly specified this view is important for accessibility.
if (getImportantForAccessibility() == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
}
}
/**
* Returns {@code true} if the target drawable needs to be tileified.
*
* @param dr the drawable to check
* @return {@code true} if the target drawable needs to be tileified,
* {@code false} otherwise
*/
private static boolean needsTileify(Drawable dr) {
if (dr instanceof LayerDrawable) {
final LayerDrawable orig = (LayerDrawable) dr;
final int N = orig.getNumberOfLayers();
for (int i = 0; i < N; i++) {
if (needsTileify(orig.getDrawable(i))) {
return true;
}
}
return false;
}
if (dr instanceof StateListDrawable) {
final StateListDrawable in = (StateListDrawable) dr;
final int N = in.getStateCount();
for (int i = 0; i < N; i++) {
if (needsTileify(in.getStateDrawable(i))) {
return true;
}
}
return false;
}
// If there's a bitmap that's not wrapped with a ClipDrawable or
// ScaleDrawable, we'll need to wrap it and apply tiling.
if (dr instanceof BitmapDrawable) {
return true;
}
return false;
}
/**
* Converts a drawable to a tiled version of itself. It will recursively
* traverse layer and state list drawables.
*/
private Drawable tileify(Drawable drawable, boolean clip) {
// TODO: This is a terrible idea that potentially destroys any drawable
// that extends any of these classes. We *really* need to remove this.
if (drawable instanceof LayerDrawable) {
final LayerDrawable orig = (LayerDrawable) drawable;
final int N = orig.getNumberOfLayers();
final Drawable[] outDrawables = new Drawable[N];
for (int i = 0; i < N; i++) {
final int id = orig.getId(i);
outDrawables[i] = tileify(orig.getDrawable(i),
(id == R.id.progress || id == R.id.secondaryProgress));
}
final LayerDrawable clone = new LayerDrawable(outDrawables);
for (int i = 0; i < N; i++) {
clone.setId(i, orig.getId(i));
clone.setLayerGravity(i, orig.getLayerGravity(i));
clone.setLayerWidth(i, orig.getLayerWidth(i));
clone.setLayerHeight(i, orig.getLayerHeight(i));
clone.setLayerInsetLeft(i, orig.getLayerInsetLeft(i));
clone.setLayerInsetRight(i, orig.getLayerInsetRight(i));
clone.setLayerInsetTop(i, orig.getLayerInsetTop(i));
clone.setLayerInsetBottom(i, orig.getLayerInsetBottom(i));
clone.setLayerInsetStart(i, orig.getLayerInsetStart(i));
clone.setLayerInsetEnd(i, orig.getLayerInsetEnd(i));
}
return clone;
}
if (drawable instanceof StateListDrawable) {
final StateListDrawable in = (StateListDrawable) drawable;
final StateListDrawable out = new StateListDrawable();
final int N = in.getStateCount();
for (int i = 0; i < N; i++) {
out.addState(in.getStateSet(i), tileify(in.getStateDrawable(i), clip));
}
return out;
}
if (drawable instanceof BitmapDrawable) {
final BitmapDrawable bitmap = (BitmapDrawable) drawable;
final Bitmap tileBitmap = bitmap.getBitmap();
if (mSampleTile == null) {
mSampleTile = tileBitmap;
}
final BitmapDrawable clone = (BitmapDrawable) bitmap.getConstantState().newDrawable();
clone.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
if (clip) {
return new ClipDrawable(clone, Gravity.LEFT, ClipDrawable.HORIZONTAL);
} else {
return clone;
}
}
return drawable;
}
Shape getDrawableShape() {
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
return new RoundRectShape(roundedCorners, null, null);
}
/**
* Convert a AnimationDrawable for use as a barberpole animation.
* Each frame of the animation is wrapped in a ClipDrawable and
* given a tiling BitmapShader.
*/
private Drawable tileifyIndeterminate(Drawable drawable) {
if (drawable instanceof AnimationDrawable) {
AnimationDrawable background = (AnimationDrawable) drawable;
final int N = background.getNumberOfFrames();
AnimationDrawable newBg = new AnimationDrawable();
newBg.setOneShot(background.isOneShot());
for (int i = 0; i < N; i++) {
Drawable frame = tileify(background.getFrame(i), true);
frame.setLevel(10000);
newBg.addFrame(frame, background.getDuration(i));
}
newBg.setLevel(10000);
drawable = newBg;
}
return drawable;
}
/**
* <p>
* Initialize the progress bar's default values:
* </p>
* <ul>
* <li>progress = 0</li>
* <li>max = 100</li>
* <li>animation duration = 4000 ms</li>
* <li>indeterminate = false</li>
* <li>behavior = repeat</li>
* </ul>
*/
private void initProgressBar() {
mMax = 100;
mProgress = 0;
mSecondaryProgress = 0;
mIndeterminate = false;
mOnlyIndeterminate = false;
mDuration = 4000;
mBehavior = AlphaAnimation.RESTART;
mMinWidth = 24;
mMaxWidth = 48;
mMinHeight = 24;
mMaxHeight = 48;
}
/**
* <p>Indicate whether this progress bar is in indeterminate mode.</p>
*
* @return true if the progress bar is in indeterminate mode
*/
@ViewDebug.ExportedProperty(category = "progress")
public synchronized boolean isIndeterminate() {
return mIndeterminate;
}
/**
* <p>Change the indeterminate mode for this progress bar. In indeterminate
* mode, the progress is ignored and the progress bar shows an infinite
* animation instead.</p>
*
* If this progress bar's style only supports indeterminate mode (such as the circular
* progress bars), then this will be ignored.
*
* @param indeterminate true to enable the indeterminate mode
*/
@android.view.RemotableViewMethod
public synchronized void setIndeterminate(boolean indeterminate) {
if ((!mOnlyIndeterminate || !mIndeterminate) && indeterminate != mIndeterminate) {
mIndeterminate = indeterminate;
if (indeterminate) {
// swap between indeterminate and regular backgrounds
mCurrentDrawable = mIndeterminateDrawable;
startAnimation();
} else {
mCurrentDrawable = mProgressDrawable;
stopAnimation();
}
}
}
/**
* <p>Get the drawable used to draw the progress bar in
* indeterminate mode.</p>
*
* @return a {@link android.graphics.drawable.Drawable} instance
*
* @see #setIndeterminateDrawable(android.graphics.drawable.Drawable)
* @see #setIndeterminate(boolean)
*/
public Drawable getIndeterminateDrawable() {
return mIndeterminateDrawable;
}
/**
* Define the drawable used to draw the progress bar in indeterminate mode.
*
* @param d the new drawable
* @see #getIndeterminateDrawable()
* @see #setIndeterminate(boolean)
*/
public void setIndeterminateDrawable(Drawable d) {
if (mIndeterminateDrawable != d) {
if (mIndeterminateDrawable != null) {
mIndeterminateDrawable.setCallback(null);
unscheduleDrawable(mIndeterminateDrawable);
}
mIndeterminateDrawable = d;
if (d != null) {
d.setCallback(this);
d.setLayoutDirection(getLayoutDirection());
if (d.isStateful()) {
d.setState(getDrawableState());
}
applyIndeterminateTint();
}
if (mIndeterminate) {
mCurrentDrawable = d;
postInvalidate();
}
}
}
/**
* Applies a tint to the indeterminate drawable. Does not modify the
* current tint mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
* <p>
* Subsequent calls to {@link #setIndeterminateDrawable(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#ProgressBar_indeterminateTint
* @see #getIndeterminateTintList()
* @see Drawable#setTintList(ColorStateList)
*/
@RemotableViewMethod
public void setIndeterminateTintList(@Nullable ColorStateList tint) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mIndeterminateTintList = tint;
mProgressTintInfo.mHasIndeterminateTint = true;
applyIndeterminateTint();
}
/**
* @return the tint applied to the indeterminate drawable
* @attr ref android.R.styleable#ProgressBar_indeterminateTint
* @see #setIndeterminateTintList(ColorStateList)
*/
@Nullable
public ColorStateList getIndeterminateTintList() {
return mProgressTintInfo != null ? mProgressTintInfo.mIndeterminateTintList : null;
}
/**
* Specifies the blending mode used to apply the tint specified by
* {@link #setIndeterminateTintList(ColorStateList)} to the indeterminate
* 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#ProgressBar_indeterminateTintMode
* @see #setIndeterminateTintList(ColorStateList)
* @see Drawable#setTintMode(PorterDuff.Mode)
*/
public void setIndeterminateTintMode(@Nullable PorterDuff.Mode tintMode) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mIndeterminateTintMode = tintMode;
mProgressTintInfo.mHasIndeterminateTintMode = true;
applyIndeterminateTint();
}
/**
* Returns the blending mode used to apply the tint to the indeterminate
* drawable, if specified.
*
* @return the blending mode used to apply the tint to the indeterminate
* drawable
* @attr ref android.R.styleable#ProgressBar_indeterminateTintMode
* @see #setIndeterminateTintMode(PorterDuff.Mode)
*/
@Nullable
public PorterDuff.Mode getIndeterminateTintMode() {
return mProgressTintInfo != null ? mProgressTintInfo.mIndeterminateTintMode : null;
}
private void applyIndeterminateTint() {
if (mIndeterminateDrawable != null && mProgressTintInfo != null) {
final ProgressTintInfo tintInfo = mProgressTintInfo;
if (tintInfo.mHasIndeterminateTint || tintInfo.mHasIndeterminateTintMode) {
mIndeterminateDrawable = mIndeterminateDrawable.mutate();
if (tintInfo.mHasIndeterminateTint) {
mIndeterminateDrawable.setTintList(tintInfo.mIndeterminateTintList);
}
if (tintInfo.mHasIndeterminateTintMode) {
mIndeterminateDrawable.setTintMode(tintInfo.mIndeterminateTintMode);
}
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (mIndeterminateDrawable.isStateful()) {
mIndeterminateDrawable.setState(getDrawableState());
}
}
}
}
/**
* Define the tileable drawable used to draw the progress bar in
* indeterminate mode.
* <p>
* If the drawable is a BitmapDrawable or contains BitmapDrawables, a
* tiled copy will be generated for display as a progress bar.
*
* @param d the new drawable
* @see #getIndeterminateDrawable()
* @see #setIndeterminate(boolean)
*/
public void setIndeterminateDrawableTiled(Drawable d) {
if (d != null) {
d = tileifyIndeterminate(d);
}
setIndeterminateDrawable(d);
}
/**
* <p>Get the drawable used to draw the progress bar in
* progress mode.</p>
*
* @return a {@link android.graphics.drawable.Drawable} instance
*
* @see #setProgressDrawable(android.graphics.drawable.Drawable)
* @see #setIndeterminate(boolean)
*/
public Drawable getProgressDrawable() {
return mProgressDrawable;
}
/**
* Define the drawable used to draw the progress bar in progress mode.
*
* @param d the new drawable
* @see #getProgressDrawable()
* @see #setIndeterminate(boolean)
*/
public void setProgressDrawable(Drawable d) {
if (mProgressDrawable != d) {
if (mProgressDrawable != null) {
mProgressDrawable.setCallback(null);
unscheduleDrawable(mProgressDrawable);
}
mProgressDrawable = d;
if (d != null) {
d.setCallback(this);
d.setLayoutDirection(getLayoutDirection());
if (d.isStateful()) {
d.setState(getDrawableState());
}
// Make sure the ProgressBar is always tall enough
int drawableHeight = d.getMinimumHeight();
if (mMaxHeight < drawableHeight) {
mMaxHeight = drawableHeight;
requestLayout();
}
applyProgressTints();
}
if (!mIndeterminate) {
mCurrentDrawable = d;
postInvalidate();
}
updateDrawableBounds(getWidth(), getHeight());
updateDrawableState();
doRefreshProgress(R.id.progress, mProgress, false, false);
doRefreshProgress(R.id.secondaryProgress, mSecondaryProgress, false, false);
}
}
/**
* Applies the progress tints in order of increasing specificity.
*/
private void applyProgressTints() {
if (mProgressDrawable != null && mProgressTintInfo != null) {
applyPrimaryProgressTint();
applyProgressBackgroundTint();
applySecondaryProgressTint();
}
}
/**
* Should only be called if we've already verified that mProgressDrawable
* and mProgressTintInfo are non-null.
*/
private void applyPrimaryProgressTint() {
if (mProgressTintInfo.mHasProgressTint
|| mProgressTintInfo.mHasProgressTintMode) {
final Drawable target = getTintTarget(R.id.progress, true);
if (target != null) {
if (mProgressTintInfo.mHasProgressTint) {
target.setTintList(mProgressTintInfo.mProgressTintList);
}
if (mProgressTintInfo.mHasProgressTintMode) {
target.setTintMode(mProgressTintInfo.mProgressTintMode);
}
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (target.isStateful()) {
target.setState(getDrawableState());
}
}
}
}
/**
* Should only be called if we've already verified that mProgressDrawable
* and mProgressTintInfo are non-null.
*/
private void applyProgressBackgroundTint() {
if (mProgressTintInfo.mHasProgressBackgroundTint
|| mProgressTintInfo.mHasProgressBackgroundTintMode) {
final Drawable target = getTintTarget(R.id.background, false);
if (target != null) {
if (mProgressTintInfo.mHasProgressBackgroundTint) {
target.setTintList(mProgressTintInfo.mProgressBackgroundTintList);
}
if (mProgressTintInfo.mHasProgressBackgroundTintMode) {
target.setTintMode(mProgressTintInfo.mProgressBackgroundTintMode);
}
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (target.isStateful()) {
target.setState(getDrawableState());
}
}
}
}
/**
* Should only be called if we've already verified that mProgressDrawable
* and mProgressTintInfo are non-null.
*/
private void applySecondaryProgressTint() {
if (mProgressTintInfo.mHasSecondaryProgressTint
|| mProgressTintInfo.mHasSecondaryProgressTintMode) {
final Drawable target = getTintTarget(R.id.secondaryProgress, false);
if (target != null) {
if (mProgressTintInfo.mHasSecondaryProgressTint) {
target.setTintList(mProgressTintInfo.mSecondaryProgressTintList);
}
if (mProgressTintInfo.mHasSecondaryProgressTintMode) {
target.setTintMode(mProgressTintInfo.mSecondaryProgressTintMode);
}
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (target.isStateful()) {
target.setState(getDrawableState());
}
}
}
}
/**
* Applies a tint to the progress indicator, if one exists, or to the
* entire progress drawable otherwise. Does not modify the current tint
* mode, which is {@link PorterDuff.Mode#SRC_IN} by default.
* <p>
* The progress indicator should be specified as a layer with
* id {@link android.R.id#progress} in a {@link LayerDrawable}
* used as the progress drawable.
* <p>
* Subsequent calls to {@link #setProgressDrawable(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#ProgressBar_progressTint
* @see #getProgressTintList()
* @see Drawable#setTintList(ColorStateList)
*/
@RemotableViewMethod
public void setProgressTintList(@Nullable ColorStateList tint) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mProgressTintList = tint;
mProgressTintInfo.mHasProgressTint = true;
if (mProgressDrawable != null) {
applyPrimaryProgressTint();
}
}
/**
* Returns the tint applied to the progress drawable, if specified.
*
* @return the tint applied to the progress drawable
* @attr ref android.R.styleable#ProgressBar_progressTint
* @see #setProgressTintList(ColorStateList)
*/
@Nullable
public ColorStateList getProgressTintList() {
return mProgressTintInfo != null ? mProgressTintInfo.mProgressTintList : null;
}
/**
* Specifies the blending mode used to apply the tint specified by
* {@link #setProgressTintList(ColorStateList)}} to the progress
* indicator. 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#ProgressBar_progressTintMode
* @see #getProgressTintMode()
* @see Drawable#setTintMode(PorterDuff.Mode)
*/
public void setProgressTintMode(@Nullable PorterDuff.Mode tintMode) {
if (mProgressTintInfo == null) {
mProgressTintInfo = new ProgressTintInfo();
}
mProgressTintInfo.mProgressTintMode = tintMode;
mProgressTintInfo.mHasProgressTintMode = true;
if (mProgressDrawable != null) {
applyPrimaryProgressTint();
}
}
/**
* Returns the blending mode used to apply the tint to the progress
* drawable, if specified.
*
* @return the blending mode used to apply the tint to the progress
* drawable
* @attr ref android.R.styleable#ProgressBar_progressTintMode
* @see #setProgressTintMode(PorterDuff.Mode)
*/
@Nullable
public PorterDuff.Mode getProgressTintMode() {
return mProgressTintInfo != null ? mProgressTintInfo.mProgressTintMode : null;
}
/**
* Applies a tint to the progress background, if one exists. Does not
* modify the current tint mode, which is
* {@link PorterDuff.Mode#SRC_ATOP} by default.
* <p>
* The progress background must be specified as a layer with
* id {@link android.R.id#background} in a {@link LayerDrawable}
* used as the progress drawable.
* <p>
* Subsequent calls to {@link #setProgressDrawable(Drawable)} where the
* drawable contains a progress background will automatically mutate the
* drawable and apply the specified tint and tint mode using