Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generalized Lottie Feature Flags API #2512

Merged
merged 13 commits into from
Jul 3, 2024
Merged
12 changes: 10 additions & 2 deletions lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,22 @@ public void setUseCompositionFrameRate(boolean useCompositionFrameRate) {
* instead of using merge paths.
*/
public void enableMergePathsForKitKatAndAbove(boolean enable) {
lottieDrawable.enableMergePathsForKitKatAndAbove(enable);
lottieDrawable.enableFeatureFlag(LottieFeatureFlags.FeatureFlag.MergePathsApi19, enable);
}

/**
* Returns whether merge paths are enabled for KitKat and above.
*/
public boolean isMergePathsEnabledForKitKatAndAbove() {
return lottieDrawable.isMergePathsEnabledForKitKatAndAbove();
return lottieDrawable.isFeatureFlagEnabled(LottieFeatureFlags.FeatureFlag.MergePathsApi19);
}

public void enableFeatureFlag(LottieFeatureFlags.FeatureFlag flag, boolean enable) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add docs here since this is a new public API?

I would make it extra explicit that people validate that opted in features look acceptable across all supported API levels.

lottieDrawable.enableFeatureFlag(flag, enable);
}

public boolean isFeatureFlagEnabled(LottieFeatureFlags.FeatureFlag flag) {
return lottieDrawable.isFeatureFlagEnabled(flag);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand Down
33 changes: 19 additions & 14 deletions lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private enum OnVisibleAction {
FontAssetDelegate fontAssetDelegate;
@Nullable
TextDelegate textDelegate;
private boolean enableMergePaths;
private final LottieFeatureFlags lottieFeatureFlags = new LottieFeatureFlags();
private boolean maintainOriginalImageBounds = false;
private boolean clipToCompositionBounds = true;
@Nullable
Expand Down Expand Up @@ -285,34 +285,39 @@ public boolean hasMatte() {
return compositionLayer != null && compositionLayer.hasMatte();
}

public boolean enableMergePathsForKitKatAndAbove() {
return enableMergePaths;
}

/**
* Enable this to get merge path support for devices running KitKat (19) and above.
* Deprecated: Use enableFeatureFlag(LottieFeatureFlags.FeatureFlag.MergePathsApi19, enable)
jbeta51 marked this conversation as resolved.
Show resolved Hide resolved
* <p>
* Merge paths currently don't work if the the operand shape is entirely contained within the
* first shape. If you need to cut out one shape from another shape, use an even-odd fill type
* instead of using merge paths.
*/
@Deprecated
public void enableMergePathsForKitKatAndAbove(boolean enable) {
if (enableMergePaths == enable) {
return;
lottieFeatureFlags.enableFlag(LottieFeatureFlags.FeatureFlag.MergePathsApi19, enable);
if (composition != null) {
buildCompositionLayer();
}
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
Logger.warning("Merge paths are not supported pre-Kit Kat.");
return;
}
enableMergePaths = enable;
/**
* Deprecated: Use isFeatureFlagEnabled(LottieFeatureFlags.FeatureFlag.MergePathsApi19)
*/
@Deprecated
public boolean isMergePathsEnabledForKitKatAndAbove() {
return lottieFeatureFlags.isFlagEnabled(LottieFeatureFlags.FeatureFlag.MergePathsApi19);
}

public void enableFeatureFlag(LottieFeatureFlags.FeatureFlag flag, boolean enable) {
lottieFeatureFlags.enableFlag(flag, enable);
if (composition != null) {
buildCompositionLayer();
}
}

public boolean isMergePathsEnabledForKitKatAndAbove() {
return enableMergePaths;
public boolean isFeatureFlagEnabled(LottieFeatureFlags.FeatureFlag flag) {
return lottieFeatureFlags.isFlagEnabled(flag);
}

/**
Expand Down
45 changes: 45 additions & 0 deletions lottie/src/main/java/com/airbnb/lottie/LottieFeatureFlags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.airbnb.lottie;

import android.annotation.SuppressLint;
import android.os.Build;

import com.airbnb.lottie.utils.Logger;

import java.util.HashSet;

public class LottieFeatureFlags {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move LottieFeatureFlags to a package that's package-private so we don't expose this class in the public API and make FeatureFlag a top-level public enum LottieFeatureFlag

public enum FeatureFlag {
/**
* Merge paths currently don't work if the the operand shape is entirely contained within the
* first shape. If you need to cut out one shape from another shape, use an even-odd fill type
* instead of using merge paths.
*/
MergePathsApi19(Build.VERSION_CODES.KITKAT);

public final int minRequiredSdkVersion;

FeatureFlag(int minRequiredSdkVersion) {
this.minRequiredSdkVersion = minRequiredSdkVersion;
}
}

private final HashSet<FeatureFlag> enabledFlags = new HashSet<>();

@SuppressLint("DefaultLocale")
public void enableFlag(FeatureFlag flag, boolean enable) {
if (enable) {
if (Build.VERSION.SDK_INT < flag.minRequiredSdkVersion) {
Logger.warning(String.format("%s is not supported pre SDK %d", flag.name(), flag.minRequiredSdkVersion));
return;
}
enabledFlags.add(flag);
} else {
enabledFlags.remove(flag);
}
}

public boolean isFlagEnabled(FeatureFlag flag) {
return enabledFlags.contains(flag);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.airbnb.lottie.LottieComposition;
import com.airbnb.lottie.LottieDrawable;
import com.airbnb.lottie.LottieFeatureFlags;
import com.airbnb.lottie.animation.content.Content;
import com.airbnb.lottie.animation.content.MergePathsContent;
import com.airbnb.lottie.model.layer.BaseLayer;
Expand Down Expand Up @@ -60,7 +61,7 @@ public boolean isHidden() {
}

@Override @Nullable public Content toContent(LottieDrawable drawable, LottieComposition composition, BaseLayer layer) {
if (!drawable.enableMergePathsForKitKatAndAbove()) {
if (!drawable.isFeatureFlagEnabled(LottieFeatureFlags.FeatureFlag.MergePathsApi19)) {
Logger.warning("Animation contains merge paths but they are disabled.");
return null;
}
Expand Down
Loading