android: Update JSC and compat new api

This commit is contained in:
pengfei.zhou
2022-03-09 17:35:13 +08:00
committed by osborn
parent 774d3160a9
commit 8aebbc7a36
19 changed files with 1906 additions and 3122 deletions

View File

@@ -31,7 +31,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.1'
api 'com.github.penfeizhou:jsc4a:0.3.5'
api 'com.github.penfeizhou:jsc4a:0.3.6'
//api "pub.doric:jse:${rootProject.ext.Version}"
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
implementation('com.github.penfeizhou.android.animation:glide-plugin:2.17.0') {

View File

@@ -15,6 +15,8 @@
*/
package pub.doric.engine;
import android.util.Base64;
import com.github.pengfeizhou.jscore.ArchiveException;
import com.github.pengfeizhou.jscore.JSArray;
import com.github.pengfeizhou.jscore.JSArrayBuffer;
@@ -98,6 +100,11 @@ public class DoricJSDecoder extends JSDecoder {
return new JSBoolean((Boolean) this.value);
}
if (isString()) {
if (((String) this.value).startsWith("__buffer_")) {
String base64String = ((String) this.value).substring("__buffer_".length());
byte[] data = Base64.decode(base64String, 0);
return new DoricJSArrayBuffer(data);
}
return new JSString((String) this.value);
}
if (isNumber()) {
@@ -129,8 +136,22 @@ public class DoricJSDecoder extends JSDecoder {
return jsArray;
}
if (this.value instanceof byte[]) {
return new JSArrayBuffer((byte[]) this.value);
return new DoricJSArrayBuffer((byte[]) this.value);
}
return JS_NULL;
}
private static class DoricJSArrayBuffer extends JSArrayBuffer {
private final byte[] data;
public DoricJSArrayBuffer(byte[] data) {
super(0, data.length);
this.data = data;
}
@Override
public byte[] value() {
return this.data;
}
}
}

View File

@@ -4,20 +4,21 @@ import com.github.pengfeizhou.jscore.JavaValue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import pub.doric.DoricContext;
public class RetainedJavaValue extends JavaValue {
private final WeakReference<DoricContext> contextRef;
public RetainedJavaValue(DoricContext doricContext, byte[] data) {
super(data);
public RetainedJavaValue(DoricContext doricContext, ByteBuffer data) {
super(data, null);
contextRef = new WeakReference<>(doricContext);
final SoftReference<RetainedJavaValue> softRef = new SoftReference<>(this);
doricContext.retainJavaValue(softRef);
this.memoryReleaser = new MemoryReleaser() {
@Override
public void deallocate(byte[] data) {
public void deallocate(ByteBuffer data) {
if (contextRef.get() != null) {
contextRef.get().releaseJavaValue(softRef);
}

View File

@@ -32,6 +32,7 @@ import pub.doric.extension.bridge.DoricPlugin;
import pub.doric.extension.bridge.DoricPromise;
import pub.doric.resource.DoricResource;
import pub.doric.utils.DoricLog;
import pub.doric.utils.ThreadMode;
/**
* @Description: This is for loading resource into js as ArrayBuffer
@@ -45,7 +46,7 @@ public class ImageDecoderPlugin extends DoricJavaPlugin {
super(doricContext);
}
@DoricMethod
@DoricMethod(thread = ThreadMode.UI)
public void getImageInfo(final JSObject resource, final DoricPromise promise) {
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(
getDoricContext(),
@@ -82,7 +83,8 @@ public class ImageDecoderPlugin extends DoricJavaPlugin {
}
}
@DoricMethod
@DoricMethod(thread = ThreadMode.UI)
public void decodeToPixels(final JSObject resource, final DoricPromise promise) {
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(
getDoricContext(),
@@ -90,13 +92,13 @@ public class ImageDecoderPlugin extends DoricJavaPlugin {
if (doricResource != null) {
doricResource.fetch().setCallback(new AsyncResult.Callback<byte[]>() {
@Override
public void onResult(byte[] rawData) {
public void onResult(final byte[] rawData) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeByteArray(rawData, 0, rawData.length);
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
Bitmap bitmap = BitmapFactory.decodeByteArray(rawData, 0, rawData.length, options);
ByteBuffer buffer = ByteBuffer.allocateDirect(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(buffer);
RetainedJavaValue retainedJavaValue = new RetainedJavaValue(getDoricContext(), buffer.array());
final RetainedJavaValue retainedJavaValue = new RetainedJavaValue(getDoricContext(), buffer);
promise.resolve(retainedJavaValue);
}

View File

@@ -3,6 +3,10 @@ package com.github.pengfeizhou.jscore;
public class JSArrayBuffer extends JSValue {
private final byte[] mVal;
public JSArrayBuffer(long ptr, int length) {
this.mVal = new byte[0];
}
public JSArrayBuffer(byte[] val) {
this.mVal = val;
}

View File

@@ -18,6 +18,8 @@ package com.github.pengfeizhou.jscore;
import org.json.JSONArray;
import org.json.JSONObject;
import java.nio.ByteBuffer;
public class JavaValue {
protected static final int TYPE_NULL = 0;
protected static final int TYPE_NUMBER = 1;
@@ -31,7 +33,7 @@ public class JavaValue {
protected String[] functionNames = null;
protected int type;
protected String value = "";
protected byte[] data = null;
protected ByteBuffer data = null;
protected MemoryReleaser memoryReleaser = null;
public JavaValue() {
@@ -79,17 +81,24 @@ public class JavaValue {
this.value = jsonArray.toString();
}
public JavaValue(byte[] data) {
public JavaValue(byte[] data, MemoryReleaser memoryReleaser) {
this.type = TYPE_ARRAYBUFFER;
this.data = data;
this.data = ByteBuffer.allocateDirect(data.length);
this.data.put(data);
this.memoryReleaser = memoryReleaser;
}
public JavaValue(byte[] data, MemoryReleaser memoryReleaser) {
public JavaValue(ByteBuffer data, MemoryReleaser memoryReleaser) {
this.type = TYPE_ARRAYBUFFER;
this.data = data;
this.memoryReleaser = memoryReleaser;
}
public JavaValue(byte[] data) {
this(data, null);
}
public int getType() {
return type;
}
@@ -99,14 +108,15 @@ public class JavaValue {
}
public byte[] getByteData() {
return data;
return data.array();
}
public interface MemoryReleaser {
/**
* Called when JS deallocated the arraybuffer
*/
void deallocate(byte[] data);
void deallocate(ByteBuffer data);
}
/**