SVG and GIF dependencies check
This commit is contained in:
parent
9aade9d6ca
commit
512814ac4c
@ -10,8 +10,10 @@ import java.util.concurrent.Executors;
|
||||
|
||||
import io.noties.markwon.image.data.DataUriSchemeHandler;
|
||||
import io.noties.markwon.image.gif.GifMediaDecoder;
|
||||
import io.noties.markwon.image.gif.GifSupport;
|
||||
import io.noties.markwon.image.network.NetworkSchemeHandler;
|
||||
import io.noties.markwon.image.svg.SvgMediaDecoder;
|
||||
import io.noties.markwon.image.svg.SvgSupport;
|
||||
|
||||
class AsyncDrawableLoaderBuilder {
|
||||
|
||||
@ -33,11 +35,11 @@ class AsyncDrawableLoaderBuilder {
|
||||
addSchemeHandler(NetworkSchemeHandler.create());
|
||||
|
||||
// add SVG and GIF, but only if they are present in the class-path
|
||||
if (SvgMediaDecoder.available()) {
|
||||
if (SvgSupport.hasSvgSupport()) {
|
||||
addMediaDecoder(SvgMediaDecoder.create());
|
||||
}
|
||||
|
||||
if (GifMediaDecoder.available()) {
|
||||
if (GifSupport.hasGifSupport()) {
|
||||
addMediaDecoder(GifMediaDecoder.create());
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package io.noties.markwon.image.gif;
|
||||
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
@ -37,21 +38,13 @@ public class GifMediaDecoder extends MediaDecoder {
|
||||
return new GifMediaDecoder(autoPlayGif);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean indicating if GIF dependency is satisfied
|
||||
* @since 4.0.0-SNAPSHOT
|
||||
*/
|
||||
public static boolean available() {
|
||||
return Holder.HAS_GIF;
|
||||
}
|
||||
|
||||
private final boolean autoPlayGif;
|
||||
|
||||
protected GifMediaDecoder(boolean autoPlayGif) {
|
||||
this.autoPlayGif = autoPlayGif;
|
||||
|
||||
// @since 4.0.0-SNAPSHOT
|
||||
Holder.validate();
|
||||
validate();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@ -104,28 +97,11 @@ public class GifMediaDecoder extends MediaDecoder {
|
||||
return outputStream.toByteArray();
|
||||
}
|
||||
|
||||
// @since 4.0.0-SNAPSHOT
|
||||
private static class Holder {
|
||||
|
||||
private static final boolean HAS_GIF;
|
||||
|
||||
static {
|
||||
boolean result = true;
|
||||
try {
|
||||
pl.droidsonroids.gif.GifDrawable.class.getName();
|
||||
} catch (Throwable t) {
|
||||
result = false;
|
||||
t.printStackTrace();
|
||||
}
|
||||
HAS_GIF = result;
|
||||
}
|
||||
|
||||
static void validate() {
|
||||
if (!HAS_GIF) {
|
||||
private static void validate() {
|
||||
if (!GifSupport.hasGifSupport()) {
|
||||
throw new IllegalStateException("`pl.droidsonroids.gif:android-gif-drawable:*` " +
|
||||
"dependency is missing, please add to your project explicitly if you " +
|
||||
"wish to use GIF media decoder");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
package io.noties.markwon.image.gif;
|
||||
|
||||
/**
|
||||
* @since 4.0.0-SNAPSHOT
|
||||
*/
|
||||
public abstract class GifSupport {
|
||||
|
||||
private static boolean HAS_GIF;
|
||||
|
||||
static {
|
||||
boolean result;
|
||||
try {
|
||||
pl.droidsonroids.gif.GifDrawable.class.getName();
|
||||
result = true;
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
result = false;
|
||||
}
|
||||
HAS_GIF = result;
|
||||
}
|
||||
|
||||
public static boolean hasGifSupport() {
|
||||
return HAS_GIF;
|
||||
}
|
||||
|
||||
private GifSupport() {
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@ import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
@ -39,14 +40,6 @@ public class SvgMediaDecoder extends MediaDecoder {
|
||||
return new SvgMediaDecoder(resources);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean indicating if SVG dependency is satisfied
|
||||
* @since 4.0.0-SNAPSHOT
|
||||
*/
|
||||
public static boolean available() {
|
||||
return Holder.HAS_SVG;
|
||||
}
|
||||
|
||||
private final Resources resources;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
@ -54,7 +47,7 @@ public class SvgMediaDecoder extends MediaDecoder {
|
||||
this.resources = resources;
|
||||
|
||||
// @since 4.0.0-SNAPSHOT
|
||||
Holder.validate();
|
||||
validate();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@ -91,27 +84,10 @@ public class SvgMediaDecoder extends MediaDecoder {
|
||||
return Collections.singleton(CONTENT_TYPE);
|
||||
}
|
||||
|
||||
// @since 4.0.0-SNAPSHOT
|
||||
private static class Holder {
|
||||
|
||||
private static final boolean HAS_SVG;
|
||||
|
||||
static {
|
||||
boolean result = true;
|
||||
try {
|
||||
com.caverock.androidsvg.SVG.class.getName();
|
||||
} catch (Throwable t) {
|
||||
result = false;
|
||||
t.printStackTrace();
|
||||
}
|
||||
HAS_SVG = result;
|
||||
}
|
||||
|
||||
static void validate() {
|
||||
if (!HAS_SVG) {
|
||||
private static void validate() {
|
||||
if (!SvgSupport.hasSvgSupport()) {
|
||||
throw new IllegalStateException("`com.caverock:androidsvg:*` dependency is missing, " +
|
||||
"please add to your project explicitly if you wish to use SVG media decoder");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
package io.noties.markwon.image.svg;
|
||||
|
||||
/**
|
||||
* @since 4.0.0-SNAPSHOT
|
||||
*/
|
||||
public abstract class SvgSupport {
|
||||
|
||||
private static final boolean HAS_SVG;
|
||||
|
||||
static {
|
||||
boolean result;
|
||||
try {
|
||||
com.caverock.androidsvg.SVG.class.getName();
|
||||
result = true;
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
result = false;
|
||||
}
|
||||
HAS_SVG = result;
|
||||
}
|
||||
|
||||
public static boolean hasSvgSupport() {
|
||||
return HAS_SVG;
|
||||
}
|
||||
|
||||
private SvgSupport() {
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user