SVG and GIF dependencies check

This commit is contained in:
Dimitry Ivanov 2019-06-11 18:53:31 +03:00
parent 9aade9d6ca
commit 512814ac4c
5 changed files with 73 additions and 63 deletions

View File

@ -10,8 +10,10 @@ import java.util.concurrent.Executors;
import io.noties.markwon.image.data.DataUriSchemeHandler; import io.noties.markwon.image.data.DataUriSchemeHandler;
import io.noties.markwon.image.gif.GifMediaDecoder; 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.network.NetworkSchemeHandler;
import io.noties.markwon.image.svg.SvgMediaDecoder; import io.noties.markwon.image.svg.SvgMediaDecoder;
import io.noties.markwon.image.svg.SvgSupport;
class AsyncDrawableLoaderBuilder { class AsyncDrawableLoaderBuilder {
@ -33,11 +35,11 @@ class AsyncDrawableLoaderBuilder {
addSchemeHandler(NetworkSchemeHandler.create()); addSchemeHandler(NetworkSchemeHandler.create());
// add SVG and GIF, but only if they are present in the class-path // add SVG and GIF, but only if they are present in the class-path
if (SvgMediaDecoder.available()) { if (SvgSupport.hasSvgSupport()) {
addMediaDecoder(SvgMediaDecoder.create()); addMediaDecoder(SvgMediaDecoder.create());
} }
if (GifMediaDecoder.available()) { if (GifSupport.hasGifSupport()) {
addMediaDecoder(GifMediaDecoder.create()); addMediaDecoder(GifMediaDecoder.create());
} }

View File

@ -1,6 +1,7 @@
package io.noties.markwon.image.gif; package io.noties.markwon.image.gif;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -37,21 +38,13 @@ public class GifMediaDecoder extends MediaDecoder {
return new GifMediaDecoder(autoPlayGif); 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; private final boolean autoPlayGif;
protected GifMediaDecoder(boolean autoPlayGif) { protected GifMediaDecoder(boolean autoPlayGif) {
this.autoPlayGif = autoPlayGif; this.autoPlayGif = autoPlayGif;
// @since 4.0.0-SNAPSHOT // @since 4.0.0-SNAPSHOT
Holder.validate(); validate();
} }
@NonNull @NonNull
@ -104,28 +97,11 @@ public class GifMediaDecoder extends MediaDecoder {
return outputStream.toByteArray(); return outputStream.toByteArray();
} }
// @since 4.0.0-SNAPSHOT private static void validate() {
private static class Holder { if (!GifSupport.hasGifSupport()) {
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) {
throw new IllegalStateException("`pl.droidsonroids.gif:android-gif-drawable:*` " + throw new IllegalStateException("`pl.droidsonroids.gif:android-gif-drawable:*` " +
"dependency is missing, please add to your project explicitly if you " + "dependency is missing, please add to your project explicitly if you " +
"wish to use GIF media decoder"); "wish to use GIF media decoder");
} }
} }
} }
}

View File

@ -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() {
}
}

View File

@ -5,6 +5,7 @@ import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -39,14 +40,6 @@ public class SvgMediaDecoder extends MediaDecoder {
return new SvgMediaDecoder(resources); 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; private final Resources resources;
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
@ -54,7 +47,7 @@ public class SvgMediaDecoder extends MediaDecoder {
this.resources = resources; this.resources = resources;
// @since 4.0.0-SNAPSHOT // @since 4.0.0-SNAPSHOT
Holder.validate(); validate();
} }
@NonNull @NonNull
@ -91,27 +84,10 @@ public class SvgMediaDecoder extends MediaDecoder {
return Collections.singleton(CONTENT_TYPE); return Collections.singleton(CONTENT_TYPE);
} }
// @since 4.0.0-SNAPSHOT private static void validate() {
private static class Holder { if (!SvgSupport.hasSvgSupport()) {
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) {
throw new IllegalStateException("`com.caverock:androidsvg:*` dependency is missing, " + throw new IllegalStateException("`com.caverock:androidsvg:*` dependency is missing, " +
"please add to your project explicitly if you wish to use SVG media decoder"); "please add to your project explicitly if you wish to use SVG media decoder");
} }
} }
} }
}

View File

@ -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() {
}
}