From f4342f1eb8aeae9932967529b2df9eea1a7b3f20 Mon Sep 17 00:00:00 2001 From: hunterstich Date: Mon, 13 Jan 2025 16:51:57 +0000 Subject: [PATCH] [Motion] Added default style parameter for MotionUtil when resolving theme spring attributes PiperOrigin-RevId: 714984914 --- .../material/button/MaterialButton.java | 3 ++- .../android/material/motion/MotionUtils.java | 16 ++++++++++++---- .../material/motion/MotionUtilsTest.java | 19 ++++++++++++++++--- .../material/motion/res/values/themes.xml | 8 ++++++-- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/lib/java/com/google/android/material/button/MaterialButton.java b/lib/java/com/google/android/material/button/MaterialButton.java index 4ffa360e891..fa51b58af3e 100644 --- a/lib/java/com/google/android/material/button/MaterialButton.java +++ b/lib/java/com/google/android/material/button/MaterialButton.java @@ -314,7 +314,8 @@ private void initializeSizeAnimation() { } private SpringForce createSpringForce() { - return MotionUtils.resolveThemeSpringForce(getContext(), R.attr.motionSpringFastSpatial); + return MotionUtils.resolveThemeSpringForce(getContext(), R.attr.motionSpringFastSpatial, + R.style.Motion_Material3_Spring_Standard_Fast_Spatial); } @NonNull diff --git a/lib/java/com/google/android/material/motion/MotionUtils.java b/lib/java/com/google/android/material/motion/MotionUtils.java index fc6fbd12a74..5f3d3c3652a 100644 --- a/lib/java/com/google/android/material/motion/MotionUtils.java +++ b/lib/java/com/google/android/material/motion/MotionUtils.java @@ -24,6 +24,7 @@ import android.view.animation.AnimationUtils; import androidx.annotation.AttrRes; import androidx.annotation.NonNull; +import androidx.annotation.StyleRes; import androidx.core.graphics.PathParser; import androidx.core.view.animation.PathInterpolatorCompat; import androidx.dynamicanimation.animation.SpringForce; @@ -46,15 +47,22 @@ private MotionUtils() {} * @param context the context from where the theme attribute will be resolved * @param attrResId the {@code motionSpring*} theme attribute to resolve * into a {@link SpringForce} object + * @param defStyleRes a {@code MaterialSpring} style to load if attrResId cannot be resolved * @return a {@link SpringForce} object configured using the stiffness and damping from the * resolved Material spring attribute */ @NonNull public static SpringForce resolveThemeSpringForce( - @NonNull Context context, @AttrRes int attrResId) { - TypedValue tv = MaterialAttributes.resolveTypedValueOrThrow( - context, attrResId, "MaterialSpring"); - TypedArray a = context.obtainStyledAttributes(tv.resourceId, R.styleable.MaterialSpring); + @NonNull Context context, @AttrRes int attrResId, @StyleRes int defStyleRes) { + + TypedValue tv = MaterialAttributes.resolve(context, attrResId); + TypedArray a; + if (tv == null) { + a = context.obtainStyledAttributes(null, R.styleable.MaterialSpring, 0, defStyleRes); + } else { + a = context.obtainStyledAttributes(tv.resourceId, R.styleable.MaterialSpring); + } + SpringForce springForce = new SpringForce(); try { float stiffness = a.getFloat(R.styleable.MaterialSpring_stiffness, Float.MIN_VALUE); diff --git a/lib/javatests/com/google/android/material/motion/MotionUtilsTest.java b/lib/javatests/com/google/android/material/motion/MotionUtilsTest.java index 43d54337669..7bf03bd1284 100644 --- a/lib/javatests/com/google/android/material/motion/MotionUtilsTest.java +++ b/lib/javatests/com/google/android/material/motion/MotionUtilsTest.java @@ -63,13 +63,25 @@ public void testResolvesThemeSpring() { context.getResources(), R.dimen.m3_sys_motion_standard_spring_fast_spatial_stiffness); float expectedDampingRatio = ResourcesCompat.getFloat( context.getResources(), R.dimen.m3_sys_motion_standard_spring_fast_spatial_damping); - SpringForce spring = - MotionUtils.resolveThemeSpringForce(context, R.attr.motionSpringFastSpatial); + SpringForce spring = MotionUtils.resolveThemeSpringForce(context, + R.attr.motionSpringFastSpatial, R.style.Motion_Material3_Spring_Standard_Fast_Spatial); assertThat(spring.getStiffness()).isEqualTo(expectedStiffness); assertThat(spring.getDampingRatio()).isEqualTo(expectedDampingRatio); } + @Test + public void testAbsentThemeSpring_shouldResolveDefault() { + createActivityAndSetTheme(R.style.Theme_AppCompat_DayNight); + Context context = activityController.get().getApplicationContext(); + + SpringForce spring = MotionUtils.resolveThemeSpringForce(context, + R.attr.motionSpringFastSpatial, R.style.Motion_MyApp_Spring_Custom_Default); + + assertThat(spring.getStiffness()).isEqualTo(1450f); + assertThat(spring.getDampingRatio()).isEqualTo(0.5f); + } + @Test public void testPartialSpring_shouldThrow() { createActivityAndSetTheme(R.style.Theme_Material3_DayNight_PartialSpring); @@ -77,7 +89,8 @@ public void testPartialSpring_shouldThrow() { IllegalArgumentException thrown = assertThrows( IllegalArgumentException.class, - () -> MotionUtils.resolveThemeSpringForce(context, R.attr.motionSpringFastSpatial) + () -> MotionUtils.resolveThemeSpringForce(context, R.attr.motionSpringFastSpatial, + R.style.Motion_Material3_Spring_Standard_Fast_Spatial) ); assertThat(thrown).hasMessageThat().contains("must have a damping"); } diff --git a/lib/javatests/com/google/android/material/motion/res/values/themes.xml b/lib/javatests/com/google/android/material/motion/res/values/themes.xml index c753eae4032..f117b195b84 100644 --- a/lib/javatests/com/google/android/material/motion/res/values/themes.xml +++ b/lib/javatests/com/google/android/material/motion/res/values/themes.xml @@ -17,11 +17,15 @@ - +