Skip to content

Commit 2803960

Browse files
pekingmehunterstich
authored andcommitted
[ProgressIndicator] Removed explicitly disabling animators.
PiperOrigin-RevId: 322170475
1 parent 1f2a26b commit 2803960

File tree

2 files changed

+70
-31
lines changed

2 files changed

+70
-31
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2020 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.android.material.progressindicator;
17+
18+
import android.content.ContentResolver;
19+
import android.os.Build.VERSION;
20+
import android.provider.Settings.Global;
21+
import android.provider.Settings.System;
22+
import androidx.annotation.NonNull;
23+
import androidx.annotation.VisibleForTesting;
24+
25+
/**
26+
* This is a utility class to get system animator duration scale from the system settings. It's used
27+
* as instances so that some requirements for testing can be met by mocking.
28+
*/
29+
public class AnimatorDurationScaleProvider {
30+
31+
/** The emulated system animator duration scale setting for SDK_INT < 16. */
32+
private static float defaultSystemAnimatorDurationScale = 1f;
33+
34+
/** Returns the animator duration scale from developer options setting. */
35+
public float getSystemAnimatorDurationScale(@NonNull ContentResolver contentResolver) {
36+
if (VERSION.SDK_INT >= 17) {
37+
return Global.getFloat(contentResolver, Global.ANIMATOR_DURATION_SCALE, 1f);
38+
}
39+
if (VERSION.SDK_INT == 16) {
40+
return System.getFloat(contentResolver, System.ANIMATOR_DURATION_SCALE, 1f);
41+
}
42+
return defaultSystemAnimatorDurationScale;
43+
}
44+
45+
/**
46+
* Sets the default system animator duration scale for SDK < 16.
47+
*
48+
* @param scale New system animator duration scale.
49+
* @see android.provider.Settings.Global#ANIMATOR_DURATION_SCALE
50+
* @see android.provider.Settings.System#ANIMATOR_DURATION_SCALE
51+
*/
52+
@VisibleForTesting
53+
public static void setDefaultSystemAnimatorDurationScale(float scale) {
54+
defaultSystemAnimatorDurationScale = scale;
55+
}
56+
}

lib/java/com/google/android/material/progressindicator/ProgressIndicator.java

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@
2727
import android.content.res.TypedArray;
2828
import android.graphics.Canvas;
2929
import android.graphics.drawable.Drawable;
30-
import android.os.Build.VERSION;
3130
import android.os.SystemClock;
32-
import android.provider.Settings.Global;
33-
import android.provider.Settings.System;
3431
import androidx.core.view.ViewCompat;
3532
import android.util.AttributeSet;
3633
import android.view.View;
@@ -124,11 +121,11 @@ public class ProgressIndicator extends ProgressBar {
124121

125122
private long lastShowStartTime = -1L;
126123

127-
private boolean animatorDisabled = false;
128-
129124
/** The scale of the animation speed combining system setting and debug parameters. */
130125
private float systemAnimationScale = 1f;
131126

127+
private AnimatorDurationScaleProvider animatorDurationScaleProvider;
128+
132129
// ******************** Interfaces **********************
133130

134131
/** The type of the progress indicator. */
@@ -159,6 +156,7 @@ public ProgressIndicator(
159156
public ProgressIndicator(
160157
@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
161158
super(wrap(context, attrs, defStyleAttr, DEF_STYLE_RES), attrs, defStyleAttr);
159+
animatorDurationScaleProvider = new AnimatorDurationScaleProvider();
162160
isParentDoneInitializing = true;
163161
// Ensure we are using the correctly themed context rather than the context was passed in.
164162
context = getContext();
@@ -346,7 +344,9 @@ private void unregisterAnimationCallbacks() {
346344
}
347345

348346
private void updateAnimationScale() {
349-
systemAnimationScale = getSystemAnimatorDurationScale();
347+
systemAnimationScale =
348+
animatorDurationScaleProvider.getSystemAnimatorDurationScale(
349+
getContext().getContentResolver());
350350
if (systemAnimationScale > 0) {
351351
if (getProgressDrawable() != null) {
352352
getProgressDrawable().invalidateAnimationScale(systemAnimationScale);
@@ -356,15 +356,6 @@ private void updateAnimationScale() {
356356

357357
// ******************** Visibility control **********************
358358

359-
/**
360-
* This method sets a flag to prevent using any animators. It should be called before being
361-
* visible.
362-
*/
363-
@VisibleForTesting
364-
public void disableAnimatorsForTesting() {
365-
animatorDisabled = true;
366-
}
367-
368359
/**
369360
* Sets the visibility to {@code VISIBLE}. If this changes the visibility it will invoke {@code
370361
* onVisibilityChanged} and handle the visibility with animation of the drawables.
@@ -653,23 +644,9 @@ private void updateColorsInDrawables() {
653644
getIndeterminateDrawable().getAnimatorDelegate().invalidateSpecValues();
654645
}
655646

656-
/** Returns the animator duration scale from developer options setting. */
657-
private float getSystemAnimatorDurationScale() {
658-
if (VERSION.SDK_INT >= 17) {
659-
return Global.getFloat(getContext().getContentResolver(), Global.ANIMATOR_DURATION_SCALE, 1f);
660-
}
661-
if (VERSION.SDK_INT == 16) {
662-
return System.getFloat(getContext().getContentResolver(), System.ANIMATOR_DURATION_SCALE, 1f);
663-
}
664-
return 1f;
665-
}
666-
667-
/**
668-
* Returns whether the animators are disabled passively (by system settings) or actively (for
669-
* testings).
670-
*/
647+
/** Returns whether the animators are disabled passively (by system settings). */
671648
private boolean isAnimatorDisabled() {
672-
return animatorDisabled || systemAnimationScale == 0;
649+
return systemAnimationScale == 0;
673650
}
674651

675652
// ******************** Getters and setters **********************
@@ -1037,6 +1014,12 @@ && getProgress() != progress
10371014
super.setProgress(progress);
10381015
}
10391016

1017+
@VisibleForTesting
1018+
public void setAnimatorDurationScaleProvider(
1019+
@NonNull AnimatorDurationScaleProvider animatorDurationScaleProvider) {
1020+
this.animatorDurationScaleProvider = animatorDurationScaleProvider;
1021+
}
1022+
10401023
// ************************ In-place defined parameters ****************************
10411024

10421025
/**

0 commit comments

Comments
 (0)