15
15
*/
16
16
package com .google .android .material .progressindicator ;
17
17
18
+ import com .google .android .material .R ;
19
+
18
20
import static com .google .android .material .progressindicator .LinearProgressIndicator .INDETERMINATE_ANIMATION_TYPE_CONTIGUOUS ;
19
21
20
22
import android .animation .ObjectAnimator ;
21
23
import android .content .Context ;
22
24
import android .graphics .Canvas ;
23
25
import android .graphics .Color ;
24
26
import android .graphics .Rect ;
27
+ import android .graphics .drawable .Drawable ;
25
28
import android .os .Build .VERSION ;
26
29
import android .os .Build .VERSION_CODES ;
30
+ import android .util .Log ;
27
31
import androidx .annotation .NonNull ;
32
+ import androidx .annotation .Nullable ;
33
+ import androidx .annotation .RestrictTo ;
34
+ import androidx .annotation .RestrictTo .Scope ;
35
+ import androidx .annotation .VisibleForTesting ;
36
+ import androidx .core .graphics .drawable .DrawableCompat ;
37
+ import androidx .vectordrawable .graphics .drawable .VectorDrawableCompat ;
28
38
import com .google .android .material .color .MaterialColors ;
29
39
30
40
/** This class draws the graphics for indeterminate mode. */
@@ -36,6 +46,8 @@ public final class IndeterminateDrawable<S extends BaseProgressIndicatorSpec>
36
46
// Animator delegate object.
37
47
private IndeterminateAnimatorDelegate <ObjectAnimator > animatorDelegate ;
38
48
49
+ private Drawable staticDummyDrawable ;
50
+
39
51
IndeterminateDrawable (
40
52
@ NonNull Context context ,
41
53
@ NonNull BaseProgressIndicatorSpec baseSpec ,
@@ -76,11 +88,15 @@ public static IndeterminateDrawable<LinearProgressIndicatorSpec> createLinearDra
76
88
@ NonNull
77
89
public static IndeterminateDrawable <CircularProgressIndicatorSpec > createCircularDrawable (
78
90
@ NonNull Context context , @ NonNull CircularProgressIndicatorSpec spec ) {
79
- return new IndeterminateDrawable <>(
80
- context ,
81
- /* baseSpec= */ spec ,
82
- new CircularDrawingDelegate (spec ),
83
- new CircularIndeterminateAnimatorDelegate (spec ));
91
+ IndeterminateDrawable <CircularProgressIndicatorSpec > indeterminateDrawable =
92
+ new IndeterminateDrawable <>(
93
+ context ,
94
+ /* baseSpec= */ spec ,
95
+ new CircularDrawingDelegate (spec ),
96
+ new CircularIndeterminateAnimatorDelegate (spec ));
97
+ indeterminateDrawable .setStaticDummyDrawable (
98
+ VectorDrawableCompat .create (context .getResources (), R .drawable .indeterminate_static , null ));
99
+ return indeterminateDrawable ;
84
100
}
85
101
86
102
// ******************* Overridden methods *******************
@@ -101,17 +117,18 @@ public static IndeterminateDrawable<CircularProgressIndicatorSpec> createCircula
101
117
boolean setVisibleInternal (boolean visible , boolean restart , boolean animate ) {
102
118
boolean changed = super .setVisibleInternal (visible , restart , animate );
103
119
120
+ if (isSystemAnimatorDisabled () && staticDummyDrawable != null ) {
121
+ return staticDummyDrawable .setVisible (visible , restart );
122
+ }
123
+
104
124
// Unless it's showing or hiding, cancels the main animator.
105
125
if (!isRunning ()) {
106
126
animatorDelegate .cancelAnimatorImmediately ();
107
127
}
108
128
// Restarts the main animator if it's visible and needs to be animated.
109
- float systemAnimatorDurationScale =
110
- animatorDurationScaleProvider .getSystemAnimatorDurationScale (context .getContentResolver ());
111
129
if (visible
112
130
&& (animate
113
- || (VERSION .SDK_INT <= VERSION_CODES .LOLLIPOP_MR1
114
- && systemAnimatorDurationScale > 0 ))) {
131
+ || (VERSION .SDK_INT <= VERSION_CODES .LOLLIPOP_MR1 && !isSystemAnimatorDisabled ()))) {
115
132
animatorDelegate .startAnimator ();
116
133
}
117
134
@@ -140,6 +157,14 @@ public void draw(@NonNull Canvas canvas) {
140
157
return ;
141
158
}
142
159
160
+ if (isSystemAnimatorDisabled () && staticDummyDrawable != null ) {
161
+ staticDummyDrawable .setBounds (getBounds ());
162
+ Log .d ("ProgressIndicator" , "bounds: " + staticDummyDrawable .getBounds ());
163
+ DrawableCompat .setTint (staticDummyDrawable , baseSpec .indicatorColors [0 ]);
164
+ staticDummyDrawable .draw (canvas );
165
+ return ;
166
+ }
167
+
143
168
canvas .save ();
144
169
drawingDelegate .validateSpecAndAdjustCanvas (canvas , getBounds (), getGrowFraction ());
145
170
@@ -205,8 +230,34 @@ private void drawTrackIndicators(@NonNull Canvas canvas, int segmentIndex) {
205
230
}
206
231
}
207
232
233
+ // ******************* Utility functions *******************
234
+
235
+ private boolean isSystemAnimatorDisabled () {
236
+ if (animatorDurationScaleProvider != null ) {
237
+ float systemAnimatorDurationScale =
238
+ animatorDurationScaleProvider .getSystemAnimatorDurationScale (
239
+ context .getContentResolver ());
240
+ return systemAnimatorDurationScale == 0 ;
241
+ }
242
+ return false ;
243
+ }
244
+
208
245
// ******************* Setter and getter *******************
209
246
247
+ /** @hide */
248
+ @ RestrictTo (Scope .LIBRARY_GROUP )
249
+ @ Nullable
250
+ public Drawable getStaticDummyDrawable () {
251
+ return staticDummyDrawable ;
252
+ }
253
+
254
+ /** @hide */
255
+ @ RestrictTo (Scope .LIBRARY_GROUP )
256
+ @ VisibleForTesting
257
+ public void setStaticDummyDrawable (@ Nullable Drawable staticDummyDrawable ) {
258
+ this .staticDummyDrawable = staticDummyDrawable ;
259
+ }
260
+
210
261
@ NonNull
211
262
IndeterminateAnimatorDelegate <ObjectAnimator > getAnimatorDelegate () {
212
263
return animatorDelegate ;
0 commit comments