Renamed DefaultImageMediaDecoder to DefaultMediaDecoder

This commit is contained in:
Dimitry Ivanov 2019-06-17 23:01:02 +03:00
parent 8944f39592
commit a3ebae3b87
9 changed files with 96 additions and 16 deletions

View File

@ -1,6 +1,7 @@
package io.noties.markwon.app; package io.noties.markwon.app;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.SystemClock; import android.os.SystemClock;
@ -9,6 +10,8 @@ import android.text.Spanned;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import java.io.InputStream;
import java.util.Collection;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future; import java.util.concurrent.Future;
@ -23,7 +26,10 @@ import io.noties.markwon.ext.strikethrough.StrikethroughPlugin;
import io.noties.markwon.ext.tables.TablePlugin; import io.noties.markwon.ext.tables.TablePlugin;
import io.noties.markwon.ext.tasklist.TaskListPlugin; import io.noties.markwon.ext.tasklist.TaskListPlugin;
import io.noties.markwon.html.HtmlPlugin; import io.noties.markwon.html.HtmlPlugin;
import io.noties.markwon.image.ImageItem;
import io.noties.markwon.image.ImagesPlugin; import io.noties.markwon.image.ImagesPlugin;
import io.noties.markwon.image.MediaDecoder;
import io.noties.markwon.image.SchemeHandler;
import io.noties.markwon.image.file.FileSchemeHandler; import io.noties.markwon.image.file.FileSchemeHandler;
import io.noties.markwon.image.network.OkHttpNetworkSchemeHandler; import io.noties.markwon.image.network.OkHttpNetworkSchemeHandler;
import io.noties.markwon.syntax.Prism4jTheme; import io.noties.markwon.syntax.Prism4jTheme;
@ -120,6 +126,29 @@ public class MarkdownRenderer {
builder.urlProcessor(urlProcessor); builder.urlProcessor(urlProcessor);
} }
}) })
.usePlugin(new AbstractMarkwonPlugin() {
@Override
public void configure(@NonNull Registry registry) {
registry.require(ImagesPlugin.class, new Action<ImagesPlugin>() {
@Override
public void apply(@NonNull ImagesPlugin imagesPlugin) {
imagesPlugin.addMediaDecoder(new MediaDecoder() {
@NonNull
@Override
public Drawable decode(@Nullable String contentType, @NonNull InputStream inputStream) {
return null;
}
@NonNull
@Override
public Collection<String> supportedTypes() {
return null;
}
});
}
});
}
})
.build(); .build();
final long start = SystemClock.uptimeMillis(); final long start = SystemClock.uptimeMillis();

View File

@ -62,6 +62,7 @@ module.exports = {
'/docs/v4/image/', '/docs/v4/image/',
'/docs/v4/image-glide/', '/docs/v4/image-glide/',
'/docs/v4/image-picasso/', '/docs/v4/image-picasso/',
'/docs/v4/linkify/',
'/docs/v4/recycler/', '/docs/v4/recycler/',
'/docs/v4/recycler-table/', '/docs/v4/recycler-table/',
'/docs/v4/syntax-highlight/', '/docs/v4/syntax-highlight/',

View File

@ -108,7 +108,7 @@ which accepts a MovementMethod as an argument.
## OnTextAddedListener <Badge text="4.0.0"/> ## OnTextAddedListener <Badge text="4.0.0"/>
Since `4.0.0` `CorePlugin` provides ability to receive text-added event. This can Since `4.0.0` `CorePlugin` provides ability to receive text-added event. This can
be useful in order to process raw text (for example to [linkify](/docs/v4/linkify.md) it): be useful in order to process raw text (for example to [linkify](/docs/v4/linkify/) it):
```java ```java
final Markwon markwon = Markwon.builder(context) final Markwon markwon = Markwon.builder(context)

View File

@ -66,7 +66,7 @@ FileSchemeHandler.createWithAssets(context);
``` ```
:::warning :::warning
Assets loading will work when your URL will include `android_asset` the the path, Assets loading will work when your URL will include `android_asset` in the path,
for example: `file:///android_asset/image.png` (mind the 3 slashes `///`). If you wish for example: `file:///android_asset/image.png` (mind the 3 slashes `///`). If you wish
to _assume_ all images without proper scheme to point to assets folder, then you can use to _assume_ all images without proper scheme to point to assets folder, then you can use
[UrlProcessorAndroidAssets](/docs/v4/core/configuration.html#urlprocessorandroidassets) [UrlProcessorAndroidAssets](/docs/v4/core/configuration.html#urlprocessorandroidassets)
@ -136,8 +136,9 @@ final Markwon markwon = Markwon.builder(context)
@Override @Override
public ImageItem handle(@NonNull String raw, @NonNull Uri uri) { public ImageItem handle(@NonNull String raw, @NonNull Uri uri) {
// will handle URLs like `drawable://ic_account_24dp_white`
final int resourceId = context.getResources().getIdentifier( final int resourceId = context.getResources().getIdentifier(
raw.substring("resources://".length()), raw.substring("drawable://".length()),
"drawable", "drawable",
context.getPackageName()); context.getPackageName());
@ -153,13 +154,59 @@ final Markwon markwon = Markwon.builder(context)
@NonNull @NonNull
@Override @Override
public Collection<String> supportedSchemes() { public Collection<String> supportedSchemes() {
return Collections.singleton("resources"); return Collections.singleton("drawable");
} }
}); });
} }
})) }))
``` ```
Otherwise `SchemeHandler` must return an `InputStream` with proper `content-type` information
for further processing by a `MediaDecoder`:
```java
imagesPlugin.addSchemeHandler(new SchemeHandler() {
@NonNull
@Override
public ImageItem handle(@NonNull String raw, @NonNull Uri uri) {
return ImageItem.withDecodingNeeded("image/png", load(raw));
}
@NonNull
private InputStream load(@NonNull String raw) {...}
});
```
## MediaDecoder
`ImagesPlugin` comes with predefined media-decoders:
* `GifMediaDecoder` adds support for GIF
* `SvgMediaDecoder` adds support for SVG
* `DefaultMediaDecoder`
:::warning
If you wish to add support for **SVG** or **GIF** you must explicitly add these dependencies
to your project:
* for `SVG`: `com.caverock:androidsvg:1.4`
* for `GIF`: `pl.droidsonroids.gif:android-gif-drawable:1.2.14`
You can try more recent versions of these libraries, but make sure that they doesn't
introduce any unexpected behavior.
:::
### GifMediaDecoder
### SvgMediaDecoder
### DefaultMediaDecoder
## AsyncDrawableScheduler
## ErrorHandler
## Placeholder
:::tip :::tip
If you are using [html](/docs/v4/html/) you do not have to additionally setup If you are using [html](/docs/v4/html/) you do not have to additionally setup
images displayed via `<img>` tag, as `HtmlPlugin` automatically uses configured images displayed via `<img>` tag, as `HtmlPlugin` automatically uses configured

View File

@ -0,0 +1,3 @@
# Linkify
<MavenBadge4 :artifact="'linkify'" />

View File

@ -43,7 +43,7 @@ class AsyncDrawableLoaderBuilder {
addMediaDecoder(GifMediaDecoder.create()); addMediaDecoder(GifMediaDecoder.create());
} }
defaultMediaDecoder = DefaultImageMediaDecoder.create(); defaultMediaDecoder = DefaultMediaDecoder.create();
} }
void executorService(@NonNull ExecutorService executorService) { void executorService(@NonNull ExecutorService executorService) {

View File

@ -77,7 +77,7 @@ class AsyncDrawableLoaderImpl extends AsyncDrawableLoader {
@NonNull @NonNull
private Future<?> execute(@NonNull final AsyncDrawable asyncDrawable) { private Future<?> execute(@NonNull final AsyncDrawable asyncDrawable) {
// todo: more efficient DefaultImageMediaDecoder... BitmapFactory.decodeStream is a bit not optimal // todo: more efficient DefaultMediaDecoder... BitmapFactory.decodeStream is a bit not optimal
// for big images for sure. We _could_ introduce internal Drawable that will check for // for big images for sure. We _could_ introduce internal Drawable that will check for
// image bounds (but we will need to cache inputStream in order to inspect and optimize // image bounds (but we will need to cache inputStream in order to inspect and optimize
// input image...) // input image...)

View File

@ -19,22 +19,22 @@ import java.util.Collections;
* *
* @since 1.1.0 * @since 1.1.0
*/ */
public class DefaultImageMediaDecoder extends MediaDecoder { public class DefaultMediaDecoder extends MediaDecoder {
@NonNull @NonNull
public static DefaultImageMediaDecoder create() { public static DefaultMediaDecoder create() {
return new DefaultImageMediaDecoder(Resources.getSystem()); return new DefaultMediaDecoder(Resources.getSystem());
} }
@NonNull @NonNull
public static DefaultImageMediaDecoder create(@NonNull Resources resources) { public static DefaultMediaDecoder create(@NonNull Resources resources) {
return new DefaultImageMediaDecoder(resources); return new DefaultMediaDecoder(resources);
} }
private final Resources resources; private final Resources resources;
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
DefaultImageMediaDecoder(Resources resources) { DefaultMediaDecoder(Resources resources) {
this.resources = resources; this.resources = resources;
} }

View File

@ -111,7 +111,7 @@ public class ImagesPlugin extends AbstractMarkwonPlugin {
} }
/** /**
* @see DefaultImageMediaDecoder * @see DefaultMediaDecoder
* @see SvgMediaDecoder * @see SvgMediaDecoder
* @see GifMediaDecoder * @see GifMediaDecoder
* @since 4.0.0-SNAPSHOT * @since 4.0.0-SNAPSHOT
@ -123,10 +123,10 @@ public class ImagesPlugin extends AbstractMarkwonPlugin {
} }
/** /**
* Please note that if not specified a {@link DefaultImageMediaDecoder} will be used. So * Please note that if not specified a {@link DefaultMediaDecoder} will be used. So
* if you need to disable default-image-media-decoder specify here own no-op implementation or null. * if you need to disable default-image-media-decoder specify here own no-op implementation or null.
* *
* @see DefaultImageMediaDecoder * @see DefaultMediaDecoder
* @since 4.0.0-SNAPSHOT * @since 4.0.0-SNAPSHOT
*/ */
@NonNull @NonNull