Update Markwon to 3.0.0

This commit is contained in:
topjohnwu 2019-04-02 23:58:19 -04:00
parent 056dafc59f
commit 036b5acf42
2 changed files with 19 additions and 60 deletions

View File

@ -25,12 +25,15 @@ dependencies {
implementation project(':net')
implementation project(':shared')
implementation project(':signing')
implementation 'ru.noties:markwon:2.0.1'
implementation 'com.caverock:androidsvg-aar:1.3'
implementation 'com.github.topjohnwu:jtar:1.0.0'
implementation 'net.sourceforge.streamsupport:android-retrostreams:1.7.0'
implementation 'com.github.sevar83:indeterminate-checkbox:1.0.5'
def markwonVersion = '3.0.0'
implementation "ru.noties.markwon:core:${markwonVersion}"
implementation "ru.noties.markwon:html:${markwonVersion}"
implementation "ru.noties.markwon:image-svg:${markwonVersion}"
def androidXVersion = "1.0.0"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.0.2'

View File

@ -1,49 +1,34 @@
package com.topjohnwu.magisk.uicomponents;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import com.caverock.androidsvg.SVG;
import com.topjohnwu.magisk.App;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.net.Networking;
import com.topjohnwu.net.ResponseListener;
import com.topjohnwu.signing.ByteArrayStream;
import com.topjohnwu.superuser.ShellUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.Callable;
import java.util.Scanner;
import ru.noties.markwon.Markwon;
import ru.noties.markwon.SpannableConfiguration;
import ru.noties.markwon.spans.AsyncDrawable;
import ru.noties.markwon.html.HtmlPlugin;
import ru.noties.markwon.image.ImagesPlugin;
import ru.noties.markwon.image.svg.SvgPlugin;
public class MarkDownWindow {
private static final SpannableConfiguration config = SpannableConfiguration.builder(App.self)
.asyncDrawableLoader(new Loader()).build();
public static void show(Activity activity, String title, String url) {
Networking.get(url).getAsString(new Listener(activity, title));
}
public static void show (Activity activity, String title, InputStream is) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ShellUtils.pump(is, baos);
new Listener(activity, title).onResponse(baos.toString());
} catch (IOException ignored) {}
try (Scanner s = new Scanner(is, "UTF-8")) {
s.useDelimiter("\\A");
new Listener(activity, title).onResponse(s.next());
}
}
static class Listener implements ResponseListener<String> {
@ -58,47 +43,18 @@ public class MarkDownWindow {
@Override
public void onResponse(String md) {
Markwon markwon = Markwon.builder(activity)
.usePlugin(HtmlPlugin.create())
.usePlugin(ImagesPlugin.create(activity))
.usePlugin(SvgPlugin.create(activity.getResources()))
.build();
AlertDialog.Builder alert = new AlertDialog.Builder(activity);
alert.setTitle(title);
View mv = LayoutInflater.from(activity).inflate(R.layout.markdown_window, null);
Markwon.setMarkdown(mv.findViewById(R.id.md_txt), config, md);
markwon.setMarkdown(mv.findViewById(R.id.md_txt), md);
alert.setView(mv);
alert.setNegativeButton(R.string.close, (dialog, id) -> dialog.dismiss());
alert.show();
}
}
static class Loader implements AsyncDrawable.Loader {
@Override
public void load(@NonNull String url, @NonNull AsyncDrawable asyncDrawable) {
App.THREAD_POOL.submit((Callable<?>) () -> {
InputStream is = Networking.get(url).execForInputStream().getResult();
if (is == null)
return null;
ByteArrayStream buf = new ByteArrayStream();
buf.readFrom(is);
// First try default drawables
Drawable drawable = Drawable.createFromStream(buf.getInputStream(), "");
if (drawable == null) {
// SVG
SVG svg = SVG.getFromInputStream(buf.getInputStream());
int width = Utils.dpInPx((int) svg.getDocumentWidth());
int height = Utils.dpInPx((int) svg.getDocumentHeight());
final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);
final Canvas canvas = new Canvas(bitmap);
float density = App.self.getResources().getDisplayMetrics().density;
canvas.scale(density, density);
svg.renderToCanvas(canvas);
drawable = new BitmapDrawable(App.self.getResources(), bitmap);
}
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
asyncDrawable.setResult(drawable);
return null;
});
}
@Override
public void cancel(@NonNull String url) {}
}
}