Skip to content

Commit

Permalink
[ProgressIndicator] Added static drawable for Circular type when syst…
Browse files Browse the repository at this point in the history
…em animator is disabled.

PiperOrigin-RevId: 582068647
  • Loading branch information
pekingme authored and dsn5ft committed Nov 14, 2023
1 parent 9b9449c commit 22e054b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,26 @@
*/
package com.google.android.material.progressindicator;

import com.google.android.material.R;

import static com.google.android.material.progressindicator.LinearProgressIndicator.INDETERMINATE_ANIMATION_TYPE_CONTIGUOUS;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.annotation.VisibleForTesting;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
import com.google.android.material.color.MaterialColors;

/** This class draws the graphics for indeterminate mode. */
Expand All @@ -36,6 +46,8 @@ public final class IndeterminateDrawable<S extends BaseProgressIndicatorSpec>
// Animator delegate object.
private IndeterminateAnimatorDelegate<ObjectAnimator> animatorDelegate;

private Drawable staticDummyDrawable;

IndeterminateDrawable(
@NonNull Context context,
@NonNull BaseProgressIndicatorSpec baseSpec,
Expand Down Expand Up @@ -76,11 +88,15 @@ public static IndeterminateDrawable<LinearProgressIndicatorSpec> createLinearDra
@NonNull
public static IndeterminateDrawable<CircularProgressIndicatorSpec> createCircularDrawable(
@NonNull Context context, @NonNull CircularProgressIndicatorSpec spec) {
return new IndeterminateDrawable<>(
context,
/* baseSpec= */ spec,
new CircularDrawingDelegate(spec),
new CircularIndeterminateAnimatorDelegate(spec));
IndeterminateDrawable<CircularProgressIndicatorSpec> indeterminateDrawable =
new IndeterminateDrawable<>(
context,
/* baseSpec= */ spec,
new CircularDrawingDelegate(spec),
new CircularIndeterminateAnimatorDelegate(spec));
indeterminateDrawable.setStaticDummyDrawable(
VectorDrawableCompat.create(context.getResources(), R.drawable.indeterminate_static, null));
return indeterminateDrawable;
}

// ******************* Overridden methods *******************
Expand All @@ -101,17 +117,18 @@ public static IndeterminateDrawable<CircularProgressIndicatorSpec> createCircula
boolean setVisibleInternal(boolean visible, boolean restart, boolean animate) {
boolean changed = super.setVisibleInternal(visible, restart, animate);

if (isSystemAnimatorDisabled() && staticDummyDrawable != null) {
return staticDummyDrawable.setVisible(visible, restart);
}

// Unless it's showing or hiding, cancels the main animator.
if (!isRunning()) {
animatorDelegate.cancelAnimatorImmediately();
}
// Restarts the main animator if it's visible and needs to be animated.
float systemAnimatorDurationScale =
animatorDurationScaleProvider.getSystemAnimatorDurationScale(context.getContentResolver());
if (visible
&& (animate
|| (VERSION.SDK_INT <= VERSION_CODES.LOLLIPOP_MR1
&& systemAnimatorDurationScale > 0))) {
|| (VERSION.SDK_INT <= VERSION_CODES.LOLLIPOP_MR1 && !isSystemAnimatorDisabled()))) {
animatorDelegate.startAnimator();
}

Expand Down Expand Up @@ -140,6 +157,14 @@ public void draw(@NonNull Canvas canvas) {
return;
}

if (isSystemAnimatorDisabled() && staticDummyDrawable != null) {
staticDummyDrawable.setBounds(getBounds());
Log.d("ProgressIndicator", "bounds: " + staticDummyDrawable.getBounds());
DrawableCompat.setTint(staticDummyDrawable, baseSpec.indicatorColors[0]);
staticDummyDrawable.draw(canvas);
return;
}

canvas.save();
drawingDelegate.validateSpecAndAdjustCanvas(canvas, getBounds(), getGrowFraction());

Expand Down Expand Up @@ -205,8 +230,34 @@ private void drawTrackIndicators(@NonNull Canvas canvas, int segmentIndex) {
}
}

// ******************* Utility functions *******************

private boolean isSystemAnimatorDisabled() {
if (animatorDurationScaleProvider != null) {
float systemAnimatorDurationScale =
animatorDurationScaleProvider.getSystemAnimatorDurationScale(
context.getContentResolver());
return systemAnimatorDurationScale == 0;
}
return false;
}

// ******************* Setter and getter *******************

/** @hide */
@RestrictTo(Scope.LIBRARY_GROUP)
@Nullable
public Drawable getStaticDummyDrawable() {
return staticDummyDrawable;
}

/** @hide */
@RestrictTo(Scope.LIBRARY_GROUP)
@VisibleForTesting
public void setStaticDummyDrawable(@Nullable Drawable staticDummyDrawable) {
this.staticDummyDrawable = staticDummyDrawable;
}

@NonNull
IndeterminateAnimatorDelegate<ObjectAnimator> getAnimatorDelegate() {
return animatorDelegate;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2023 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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="NewApi"
android:width="24.0dp"
android:height="24.0dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@android:color/white"
android:pathData="M17.65,6.35C16.2,4.9 14.21,4.0 12.0,4.0c-4.42,0.0 -7.99,3.58 -7.99,8.0s3.57,8.0 7.99,8.0c3.73,0.0 6.84,-2.55 7.73,-6.0l-2.08,0.0c-0.82,2.33 -3.04,4.0 -5.65,4.0 -3.31,0.0 -6.0,-2.69 -6.0,-6.0s2.69,-6.0 6.0,-6.0c1.66,0.0 3.1,0.69 4.22,1.78L13.0,11.0l7.0,0.0L20.0,4.0l-2.35,2.35z"/>
</vector>

0 comments on commit 22e054b

Please sign in to comment.