From 829063e0ffa6fe1522a8301fe3a7f86c634a2637 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 31 Dec 2021 17:01:28 +0800 Subject: [PATCH] android: optimize code style --- .../src/main/java/pub/doric/DoricContext.java | 3 +-- .../src/main/java/pub/doric/RetainedJavaValue.java | 14 +++++++------- .../java/pub/doric/plugin/ImageDecoderPlugin.java | 4 +--- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/DoricContext.java b/doric-android/doric/src/main/java/pub/doric/DoricContext.java index 001ffaae..9c71c87c 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricContext.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricContext.java @@ -70,7 +70,7 @@ public class DoricContext { private final DoricPerformanceProfile performanceProfile; private final Map animators = new HashMap<>(); private final Map cachedResources = new WeakHashMap<>(); - private final ArrayList> retainedJavaValues = new ArrayList<>(); + private final Set> retainedJavaValues = new HashSet<>(); public Collection> allHeadNodes(String type) { Map> headNode = mHeadNodes.get(type); @@ -225,7 +225,6 @@ public class DoricContext { return null; } }, ThreadMode.UI); - retainedJavaValues.clear(); } }); diff --git a/doric-android/doric/src/main/java/pub/doric/RetainedJavaValue.java b/doric-android/doric/src/main/java/pub/doric/RetainedJavaValue.java index 6b847ba1..d3436d06 100644 --- a/doric-android/doric/src/main/java/pub/doric/RetainedJavaValue.java +++ b/doric-android/doric/src/main/java/pub/doric/RetainedJavaValue.java @@ -6,18 +6,18 @@ import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; public class RetainedJavaValue extends JavaValue { - private final WeakReference mDoricContext; + private final WeakReference contextRef; - public RetainedJavaValue(WeakReference doricContext, byte[] data) { + public RetainedJavaValue(DoricContext doricContext, byte[] data) { super(data); - - this.mDoricContext = doricContext; - this.mDoricContext.get().retainJavaValue(new SoftReference<>(this)); + contextRef = new WeakReference<>(doricContext); + final SoftReference softRef = new SoftReference<>(this); + doricContext.retainJavaValue(softRef); this.memoryReleaser = new MemoryReleaser() { @Override public void deallocate(byte[] data) { - if (doricContext.get() != null) { - doricContext.get().releaseJavaValue(new SoftReference<>(RetainedJavaValue.this)); + if (contextRef.get() != null) { + contextRef.get().releaseJavaValue(softRef); } } }; diff --git a/doric-android/doric/src/main/java/pub/doric/plugin/ImageDecoderPlugin.java b/doric-android/doric/src/main/java/pub/doric/plugin/ImageDecoderPlugin.java index 867f0c32..ee970158 100644 --- a/doric-android/doric/src/main/java/pub/doric/plugin/ImageDecoderPlugin.java +++ b/doric-android/doric/src/main/java/pub/doric/plugin/ImageDecoderPlugin.java @@ -22,7 +22,6 @@ 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; @@ -97,8 +96,7 @@ public class ImageDecoderPlugin extends DoricJavaPlugin { Bitmap bitmap = BitmapFactory.decodeByteArray(rawData, 0, rawData.length); ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount()); bitmap.copyPixelsToBuffer(buffer); - - RetainedJavaValue retainedJavaValue = new RetainedJavaValue(new WeakReference(getDoricContext()), buffer.array()); + RetainedJavaValue retainedJavaValue = new RetainedJavaValue(getDoricContext(), buffer.array()); promise.resolve(retainedJavaValue); }