Skip to content

Commit

Permalink
Adding null check for context before calling Utils.getAnimationScale (
Browse files Browse the repository at this point in the history
#2546)

When calling reduce motion check we need to pass context, we read context in LottieDrawable using `getContext` method. getContext method can return a nullable context, since the code is in java we don't get any compile time error when passing the null context around. 

This resulted in issue where we end up calling `getContentResolver` on a null object in case where context is null. #2536

This PR fixes it by adding a null check before calling `Utils.getAnimationScale(context)`

Co-authored-by: Pranay Airan <[email protected]>
  • Loading branch information
pranayairan and Pranay Airan authored Sep 4, 2024
1 parent 0cf178c commit 453b43c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ boolean isAnimatingOrWillAnimateOnVisible() {
}
}

public boolean animationsEnabled(Context context) {
public boolean animationsEnabled(@Nullable Context context) {
if (ignoreSystemAnimationsDisabled) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.airbnb.lottie.configurations.reducemotion;

import android.content.Context;
import androidx.annotation.Nullable;

public interface ReducedMotionOption {

/**
* Returns the current reduced motion mode.
*/
ReducedMotionMode getCurrentReducedMotionMode(Context context);
ReducedMotionMode getCurrentReducedMotionMode(@Nullable Context context);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.airbnb.lottie.configurations.reducemotion;

import android.content.Context;
import androidx.annotation.Nullable;
import com.airbnb.lottie.utils.Utils;

/**
Expand All @@ -18,8 +19,8 @@
public class SystemReducedMotionOption implements ReducedMotionOption {

@Override
public ReducedMotionMode getCurrentReducedMotionMode(Context context) {
if (Utils.getAnimationScale(context) != 0f) {
public ReducedMotionMode getCurrentReducedMotionMode(@Nullable Context context) {
if (context == null || Utils.getAnimationScale(context) != 0f) {
return ReducedMotionMode.STANDARD_MOTION;
} else {
return ReducedMotionMode.REDUCED_MOTION;
Expand Down
3 changes: 2 additions & 1 deletion lottie/src/main/java/com/airbnb/lottie/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.os.Build;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.airbnb.lottie.L;
Expand Down Expand Up @@ -262,7 +263,7 @@ public static float dpScale() {
return Resources.getSystem().getDisplayMetrics().density;
}

public static float getAnimationScale(Context context) {
public static float getAnimationScale(@NonNull Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return Settings.Global.getFloat(context.getContentResolver(),
Settings.Global.ANIMATOR_DURATION_SCALE, 1.0f);
Expand Down

0 comments on commit 453b43c

Please sign in to comment.