Ensure explicit dependencies for SVG and GIF

This commit is contained in:
Dimitry Ivanov 2019-05-30 15:28:39 +03:00
parent 0b0d3c4753
commit 2e35ef53bb
8 changed files with 67 additions and 14 deletions

View File

@ -19,7 +19,6 @@ import ru.noties.markwon.ext.tables.TablePlugin;
import ru.noties.markwon.ext.tasklist.TaskListPlugin; import ru.noties.markwon.ext.tasklist.TaskListPlugin;
import ru.noties.markwon.gif.GifAwarePlugin; import ru.noties.markwon.gif.GifAwarePlugin;
import ru.noties.markwon.html.HtmlPlugin; import ru.noties.markwon.html.HtmlPlugin;
import ru.noties.markwon.image.DefaultImageMediaDecoder;
import ru.noties.markwon.image.ImagesPlugin; import ru.noties.markwon.image.ImagesPlugin;
import ru.noties.markwon.image.data.DataUriSchemeHandler; import ru.noties.markwon.image.data.DataUriSchemeHandler;
import ru.noties.markwon.image.file.FileSchemeHandler; import ru.noties.markwon.image.file.FileSchemeHandler;
@ -105,8 +104,7 @@ public class MarkdownRenderer {
.addSchemeHandler(OkHttpNetworkSchemeHandler.create()) .addSchemeHandler(OkHttpNetworkSchemeHandler.create())
.addSchemeHandler(FileSchemeHandler.createWithAssets(context.getAssets())) .addSchemeHandler(FileSchemeHandler.createWithAssets(context.getAssets()))
.addMediaDecoder(GifMediaDecoder.create(false)) .addMediaDecoder(GifMediaDecoder.create(false))
.addMediaDecoder(SvgMediaDecoder.create()) .addMediaDecoder(SvgMediaDecoder.create());
.defaultMediaDecoder(DefaultImageMediaDecoder.create());
} }
})) }))
.usePlugin(SyntaxHighlightPlugin.create(prism4j, prism4jTheme)) .usePlugin(SyntaxHighlightPlugin.create(prism4j, prism4jTheme))

View File

@ -101,7 +101,7 @@ public class TaskListPlugin extends AbstractMarkwonPlugin {
private static int resolve(@NonNull Context context, @AttrRes int attr) { private static int resolve(@NonNull Context context, @AttrRes int attr) {
final TypedValue typedValue = new TypedValue(); final TypedValue typedValue = new TypedValue();
final int attrs[] = new int[]{attr}; final int[] attrs = new int[]{attr};
final TypedArray typedArray = context.obtainStyledAttributes(typedValue.data, attrs); final TypedArray typedArray = context.obtainStyledAttributes(typedValue.data, attrs);
try { try {
return typedArray.getColor(0, 0); return typedArray.getColor(0, 0);

View File

@ -3,7 +3,6 @@ package ru.noties.markwon.image;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
@ -31,16 +30,10 @@ class AsyncDrawableLoaderBuilder {
} }
void addMediaDecoder(@NonNull MediaDecoder mediaDecoder) { void addMediaDecoder(@NonNull MediaDecoder mediaDecoder) {
final Collection<String> supportedTypes = mediaDecoder.supportedTypes(); for (String type : mediaDecoder.supportedTypes()) {
if (supportedTypes.isEmpty()) {
// todo: we should think about this little _side-effect_... does it worth it?
defaultMediaDecoder = mediaDecoder;
} else {
for (String type : supportedTypes) {
mediaDecoders.put(type, mediaDecoder); mediaDecoders.put(type, mediaDecoder);
} }
} }
}
void defaultMediaDecoder(@Nullable MediaDecoder mediaDecoder) { void defaultMediaDecoder(@Nullable MediaDecoder mediaDecoder) {
this.defaultMediaDecoder = mediaDecoder; this.defaultMediaDecoder = mediaDecoder;

View File

@ -47,9 +47,11 @@ public class ImagesPlugin extends AbstractMarkwonPlugin {
* Factory method to create an empty {@link ImagesPlugin} instance with no {@link SchemeHandler}s * Factory method to create an empty {@link ImagesPlugin} instance with no {@link SchemeHandler}s
* nor {@link MediaDecoder}s registered. Can be used to further configure via instance methods or * nor {@link MediaDecoder}s registered. Can be used to further configure via instance methods or
* via {@link ru.noties.markwon.MarkwonPlugin#configure(Registry)} * via {@link ru.noties.markwon.MarkwonPlugin#configure(Registry)}
*
* @see #create(ImagesConfigure)
*/ */
@NonNull @NonNull
public static ImagesPlugin createEmpty() { public static ImagesPlugin create() {
return new ImagesPlugin(); return new ImagesPlugin();
} }

View File

@ -31,6 +31,9 @@ public class GifMediaDecoder extends MediaDecoder {
protected GifMediaDecoder(boolean autoPlayGif) { protected GifMediaDecoder(boolean autoPlayGif) {
this.autoPlayGif = autoPlayGif; this.autoPlayGif = autoPlayGif;
// @since 4.0.0-SNAPSHOT
Holder.validate();
} }
@NonNull @NonNull
@ -82,4 +85,28 @@ public class GifMediaDecoder extends MediaDecoder {
} }
return outputStream.toByteArray(); return outputStream.toByteArray();
} }
// @since 4.0.0-SNAPSHOT
private static class Holder {
private static final boolean HAS_GIF;
static {
boolean result = true;
try {
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:*` " +
"dependency is missing, please add to your project explicitly");
}
}
}
} }

View File

@ -31,6 +31,11 @@ public class NetworkSchemeHandler extends SchemeHandler {
return new NetworkSchemeHandler(); return new NetworkSchemeHandler();
} }
@SuppressWarnings("WeakerAccess")
NetworkSchemeHandler() {
}
@NonNull @NonNull
@Override @Override
public ImageItem handle(@NonNull String raw, @NonNull Uri uri) { public ImageItem handle(@NonNull String raw, @NonNull Uri uri) {

View File

@ -36,6 +36,7 @@ public class OkHttpNetworkSchemeHandler extends SchemeHandler {
private final OkHttpClient client; private final OkHttpClient client;
@SuppressWarnings("WeakerAccess")
OkHttpNetworkSchemeHandler(@NonNull OkHttpClient client) { OkHttpNetworkSchemeHandler(@NonNull OkHttpClient client) {
this.client = client; this.client = client;
} }

View File

@ -44,6 +44,9 @@ public class SvgMediaDecoder extends MediaDecoder {
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
SvgMediaDecoder(Resources resources) { SvgMediaDecoder(Resources resources) {
this.resources = resources; this.resources = resources;
// @since 4.0.0-SNAPSHOT
Holder.validate();
} }
@NonNull @NonNull
@ -79,4 +82,28 @@ public class SvgMediaDecoder extends MediaDecoder {
public Collection<String> supportedTypes() { public Collection<String> supportedTypes() {
return Collections.singleton(CONTENT_TYPE); 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 {
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, " +
"please add to your project explicitly");
}
}
}
} }