retain java value
This commit is contained in:
parent
7f39727cf4
commit
c67803b717
@ -29,6 +29,7 @@ import com.github.pengfeizhou.jscore.JSONBuilder;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
@ -69,6 +70,7 @@ public class DoricContext {
|
||||
private final DoricPerformanceProfile performanceProfile;
|
||||
private final Map<String, Animator> animators = new HashMap<>();
|
||||
private final Map<String, DoricResource> cachedResources = new WeakHashMap<>();
|
||||
private final ArrayList<SoftReference<RetainedJavaValue>> retainedJavaValues = new ArrayList<>();
|
||||
|
||||
public Collection<ViewNode<?>> allHeadNodes(String type) {
|
||||
Map<String, ViewNode<?>> headNode = mHeadNodes.get(type);
|
||||
@ -223,6 +225,8 @@ public class DoricContext {
|
||||
return null;
|
||||
}
|
||||
}, ThreadMode.UI);
|
||||
|
||||
retainedJavaValues.clear();
|
||||
}
|
||||
});
|
||||
DoricContextManager.getInstance().destroyContext(this);
|
||||
@ -367,4 +371,12 @@ public class DoricContext {
|
||||
public DoricResource getCachedResource(String resId) {
|
||||
return this.cachedResources.get(resId);
|
||||
}
|
||||
|
||||
public void retainJavaValue(SoftReference<RetainedJavaValue> retainedJavaValue) {
|
||||
retainedJavaValues.add(retainedJavaValue);
|
||||
}
|
||||
|
||||
public void releaseJavaValue(SoftReference<RetainedJavaValue> retainedJavaValue) {
|
||||
retainedJavaValues.remove(retainedJavaValue);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package pub.doric;
|
||||
|
||||
import com.github.pengfeizhou.jscore.JavaValue;
|
||||
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
public class RetainedJavaValue extends JavaValue {
|
||||
private final WeakReference<DoricContext> mDoricContext;
|
||||
|
||||
public RetainedJavaValue(WeakReference<DoricContext> doricContext, byte[] data) {
|
||||
super(data);
|
||||
|
||||
this.mDoricContext = doricContext;
|
||||
this.mDoricContext.get().retainJavaValue(new SoftReference<>(this));
|
||||
this.memoryReleaser = new MemoryReleaser() {
|
||||
@Override
|
||||
public void deallocate(byte[] data) {
|
||||
if (doricContext.get() != null) {
|
||||
doricContext.get().releaseJavaValue(new SoftReference<>(RetainedJavaValue.this));
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -22,9 +22,11 @@ import com.github.pengfeizhou.jscore.JSONBuilder;
|
||||
import com.github.pengfeizhou.jscore.JSObject;
|
||||
import com.github.pengfeizhou.jscore.JavaValue;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.RetainedJavaValue;
|
||||
import pub.doric.async.AsyncResult;
|
||||
import pub.doric.extension.bridge.DoricMethod;
|
||||
import pub.doric.extension.bridge.DoricPlugin;
|
||||
@ -95,7 +97,9 @@ public class ImageDecoderPlugin extends DoricJavaPlugin {
|
||||
Bitmap bitmap = BitmapFactory.decodeByteArray(rawData, 0, rawData.length);
|
||||
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
|
||||
bitmap.copyPixelsToBuffer(buffer);
|
||||
promise.resolve(new JavaValue(buffer.array()));
|
||||
|
||||
RetainedJavaValue retainedJavaValue = new RetainedJavaValue(new WeakReference<DoricContext>(getDoricContext()), buffer.array());
|
||||
promise.resolve(retainedJavaValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user