android: optimize code style

This commit is contained in:
pengfei.zhou 2021-12-31 17:01:28 +08:00 committed by osborn
parent 8c57e5e500
commit 829063e0ff
3 changed files with 9 additions and 12 deletions

View File

@ -70,7 +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<>();
private final Set<SoftReference<RetainedJavaValue>> retainedJavaValues = new HashSet<>();
public Collection<ViewNode<?>> allHeadNodes(String type) {
Map<String, ViewNode<?>> headNode = mHeadNodes.get(type);
@ -225,7 +225,6 @@ public class DoricContext {
return null;
}
}, ThreadMode.UI);
retainedJavaValues.clear();
}
});

View File

@ -6,18 +6,18 @@ import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
public class RetainedJavaValue extends JavaValue {
private final WeakReference<DoricContext> mDoricContext;
private final WeakReference<DoricContext> contextRef;
public RetainedJavaValue(WeakReference<DoricContext> 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<RetainedJavaValue> 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);
}
}
};

View File

@ -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<DoricContext>(getDoricContext()), buffer.array());
RetainedJavaValue retainedJavaValue = new RetainedJavaValue(getDoricContext(), buffer.array());
promise.resolve(retainedJavaValue);
}