Skip to content

Commit

Permalink
[Compose] Fix safe mode for Compose (#2557)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpeal authored Sep 29, 2024
1 parent b851583 commit 5eb81c0
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@
*/
@SuppressWarnings({"WeakerAccess"})
public class LottieDrawable extends Drawable implements Drawable.Callback, Animatable {

private interface LazyCompositionTask {

void run(LottieComposition composition);
}

Expand All @@ -96,10 +98,10 @@ private enum OnVisibleAction {
/**
* The marker to use if "reduced motion" is enabled.
* Supported marker names are case insensitive, and include:
* - reduced motion
* - reducedMotion
* - reduced_motion
* - reduced-motion
* - reduced motion
* - reducedMotion
* - reduced_motion
* - reduced-motion
*/
private static final List<String> ALLOWED_REDUCED_MOTION_MARKERS = Arrays.asList(
"reduced motion",
Expand Down Expand Up @@ -179,7 +181,9 @@ private enum OnVisibleAction {
*/
private boolean isDirty = false;

/** Use the getter so that it can fall back to {@link L#getDefaultAsyncUpdates()}. */
/**
* Use the getter so that it can fall back to {@link L#getDefaultAsyncUpdates()}.
*/
@Nullable private AsyncUpdates asyncUpdates;
private final ValueAnimator.AnimatorUpdateListener progressUpdateListener = animation -> {
if (getAsyncUpdatesEnabled()) {
Expand Down Expand Up @@ -250,6 +254,7 @@ private enum OnVisibleAction {
@IntDef({RESTART, REVERSE})
@Retention(RetentionPolicy.SOURCE)
public @interface RepeatMode {

}

/**
Expand Down Expand Up @@ -766,13 +771,14 @@ public void draw(Canvas canvas, Matrix matrix) {
}
}

if (useSoftwareRendering) {
canvas.save();
canvas.concat(matrix);
renderAndDrawAsBitmap(canvas, compositionLayer);
canvas.restore();
if (safeMode) {
try {
draw(canvas, matrix, compositionLayer, alpha);
} catch (Throwable e) {
Logger.error("Lottie crashed in draw!", e);
}
} else {
compositionLayer.draw(canvas, matrix, alpha);
draw(canvas, matrix, compositionLayer, alpha);
}
isDirty = false;
} catch (InterruptedException e) {
Expand All @@ -787,6 +793,17 @@ public void draw(Canvas canvas, Matrix matrix) {
}
}

private void draw(Canvas canvas, Matrix matrix, CompositionLayer compositionLayer, int alpha) {
if (useSoftwareRendering) {
canvas.save();
canvas.concat(matrix);
renderAndDrawAsBitmap(canvas, compositionLayer);
canvas.restore();
} else {
compositionLayer.draw(canvas, matrix, alpha);
}
}

// <editor-fold desc="animator">

@MainThread
Expand Down

0 comments on commit 5eb81c0

Please sign in to comment.