Skip to content

Commit

Permalink
Merge branch 'main' into ilikethese/fix/serializer_free_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
open-hippy authored Nov 22, 2023
2 parents 0a93210 + be23474 commit d9f8b3e
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.tencent.mtt.hippy.annotation.HippyNativeModule.Thread;
import com.tencent.mtt.hippy.common.HippyArray;
import com.tencent.mtt.hippy.common.Provider;
import com.tencent.mtt.hippy.modules.HippyModulePromise;
import com.tencent.mtt.hippy.modules.Promise;
import com.tencent.mtt.hippy.modules.PromiseImpl;
import com.tencent.mtt.hippy.runtime.builtins.array.JSDenseArray;
Expand Down Expand Up @@ -208,7 +209,11 @@ private Object[] prepareArguments(@NonNull Object args, PromiseImpl promise)
int size = mUseJSValueType ? ((JSDenseArray) args).size() : ((HippyArray) args).size();
for (int i = 0; i < mParamTypes.length; i++) {
Type paramCls = mParamTypes[i];
if (paramCls == Promise.class) {
// It is necessary to be compatible with the HippyModulePromise type here,
// as some host custom modules interface promise parameters are directly defined
// using the HippyModulePromise type, resulting in parameter mismatches when
// calling the interface.
if (paramCls == Promise.class || paramCls == HippyModulePromise.class) {
params[i] = promise;
promise.setNeedResolveBySelf(false);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public class ImageLoaderModule extends HippyNativeModuleBase {

private final VfsManager mVfsManager;
private static final String ERROR_KEY_MESSAGE = "message";

public ImageLoaderModule(HippyEngineContext context) {
super(context);
Expand All @@ -50,7 +51,9 @@ private void decodeImageData(@NonNull final String url, @NonNull byte[] data, fi
jsObject.set("height", options.outHeight);
promise.resolve(jsObject);
} catch (OutOfMemoryError | Exception e) {
promise.reject("Fetch image failed, url=" + url + ", msg=" + e.getMessage());
JSObject jsObject = new JSObject();
jsObject.set(ERROR_KEY_MESSAGE, "Fetch image failed, url=" + url + ", msg=" + e.getMessage());
promise.reject(jsObject);
}
}

Expand All @@ -64,7 +67,9 @@ private HashMap<String, String> generateRequestParams() {
@HippyMethod(name = "getSize")
public void getSize(final String url, final Promise promise) {
if (TextUtils.isEmpty(url)) {
promise.reject("Url parameter is empty!");
JSObject jsObject = new JSObject();
jsObject.set(ERROR_KEY_MESSAGE, "Url parameter is empty!");
promise.reject(jsObject);
return;
}
mVfsManager.fetchResourceAsync(url, null, generateRequestParams(),
Expand All @@ -77,7 +82,10 @@ public void onFetchCompleted(@NonNull final ResourceDataHolder dataHolder) {
|| bytes.length <= 0) {
String message =
dataHolder.errorMessage != null ? dataHolder.errorMessage : "";
promise.reject("Fetch image failed, url=" + url + ", msg=" + message);
String errorMsg = "Fetch image failed, url=" + url + ", msg=" + message;
JSObject jsObject = new JSObject();
jsObject.set(ERROR_KEY_MESSAGE, errorMsg);
promise.reject(jsObject);
} else {
decodeImageData(url, bytes, promise);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,9 @@ public void dispatchEvent(int rootId, int nodeId, @NonNull String eventName,
if (lowerCaseEventName.startsWith(EVENT_PREFIX)) {
lowerCaseEventName = lowerCaseEventName.substring(EVENT_PREFIX.length());
}
if (eventType != EventType.EVENT_TYPE_GESTURE && !mRenderManager.checkRegisteredEvent(
rootId, nodeId, lowerCaseEventName)) {
if (eventType != EventType.EVENT_TYPE_GESTURE
&& !mRenderManager.checkRegisteredEvent(rootId, nodeId, lowerCaseEventName)
&& !mVirtualNodeManager.checkRegisteredEvent(rootId, nodeId, lowerCaseEventName)) {
return;
}
LogUtils.d(TAG, "dispatchEvent: id " + nodeId + ", eventName " + eventName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ public void setTintColorBlendMode(int tintColorBlendMode) {
}

protected void onFetchImageStart() {
if (mHostRef.get() != null) {
final RenderNode host = mHostRef.get();
if (host != null) {
// send onLoadStart event
EventUtils.sendComponentEvent(mHostRef.get(), EVENT_IMAGE_LOAD_START, null);
EventUtils.sendComponentEvent(host, EVENT_IMAGE_LOAD_START, null);
}
}

Expand Down Expand Up @@ -217,17 +218,28 @@ private void onFetchImageSuccess(@NonNull String uri, ImageSourceType sourceType
mImageHolder = imageHolder;
mImageFetchState = ImageFetchState.LOADED;
setImageData(imageHolder);
if (mHostRef.get() != null && !loadFromCache) {
final RenderNode host = mHostRef.get();
if (host != null) {
final int width = imageHolder.getImageWidth();
final int height = imageHolder.getImageHeight();
// send onLoad event
EventUtils.sendComponentEvent(mHostRef.get(), EVENT_IMAGE_ON_LOAD, null);
HashMap<String, Object> params = new HashMap<>();
params.put("success", 1);
HashMap<String, Object> imageSize = new HashMap<>();
imageSize.put("width", imageHolder.getImageWidth());
imageSize.put("height", imageHolder.getImageHeight());
params.put("image", imageSize);
HashMap<String, Object> onLoad = new HashMap<>();
onLoad.put("width", width);
onLoad.put("height", height);
onLoad.put("url", uri);
EventUtils.sendComponentEvent(host, EVENT_IMAGE_ON_LOAD, onLoad);
// send onLoadEnd event
EventUtils.sendComponentEvent(mHostRef.get(), EVENT_IMAGE_LOAD_END, params);
HashMap<String, Object> onLoadEnd = new HashMap<>();
onLoadEnd.put("success", 1);
onLoadEnd.put("width", width);
onLoadEnd.put("height", height);
onLoadEnd.put("url", uri);
@Deprecated
HashMap<String, Object> imageSize = new HashMap<>();
imageSize.put("width", width);
imageSize.put("height", height);
onLoadEnd.put("image", imageSize);
EventUtils.sendComponentEvent(host, EVENT_IMAGE_LOAD_END, onLoadEnd);
}
} else if (sourceType == ImageSourceType.DEFAULT) {
if (!uri.equals(mDefaultUri)) {
Expand All @@ -247,35 +259,45 @@ private void onFetchImageSuccess(@NonNull String uri, ImageSourceType sourceType
postInvalidateDelayed(0);
}

private void onFetchImageFail() {
private void onFetchImageFail(String url, Throwable throwable) {
mImageFetchState = ImageFetchState.UNLOAD;
if (mHostRef.get() == null) {
final RenderNode host = mHostRef.get();
if (host == null) {
return;
}
// send onError event
EventUtils.sendComponentEvent(mHostRef.get(), EVENT_IMAGE_LOAD_ERROR, null);
HashMap<String, Object> params = new HashMap<>();
params.put("success", 0);
HashMap<String, Object> onError = new HashMap<>();
onError.put("error", String.valueOf(throwable));
onError.put("errorCode", -1);
onError.put("errorURL", url);
EventUtils.sendComponentEvent(host, EVENT_IMAGE_LOAD_ERROR, onError);
// send onLoadEnd event
EventUtils.sendComponentEvent(mHostRef.get(), EVENT_IMAGE_LOAD_END, params);
HashMap<String, Object> onLoadEnd = new HashMap<>();
onLoadEnd.put("success", 0);
onLoadEnd.put("error", String.valueOf(throwable));
onLoadEnd.put("errorCode", -1);
onLoadEnd.put("url", url);
EventUtils.sendComponentEvent(host, EVENT_IMAGE_LOAD_END, onLoadEnd);
}

protected void onFetchImageProgress(float total, float loaded) {
if (mHostRef.get() == null) {
final RenderNode host = mHostRef.get();
if (host == null) {
return;
}
HashMap<String, Object> params = new HashMap<>();
params.put("loaded", loaded);
params.put("total", total);
EventUtils.sendComponentEvent(mHostRef.get(), EVENT_IMAGE_LOAD_PROGRESS, params);
EventUtils.sendComponentEvent(host, EVENT_IMAGE_LOAD_PROGRESS, params);
}

private void doFetchImage(final String uri, final ImageSourceType sourceType) {
int width = (mHostRef.get() != null) ? mHostRef.get().getWidth() : 0;
int height = (mHostRef.get() != null) ? mHostRef.get().getHeight() : 0;
final RenderNode host = mHostRef.get();
int width = (host != null) ? host.getWidth() : 0;
int height = (host != null) ? host.getHeight() : 0;
Map<String, Object> params = new HashMap<>();
if (mHostRef.get() != null) {
params.put("props", mHostRef.get().getProps());
if (host != null) {
params.put("props", host.getProps());
}
assert mImageLoader != null;
mImageLoader.fetchImageAsync(uri, new ImageRequestListener() {
Expand All @@ -298,7 +320,7 @@ public void onRequestSuccess(ImageDataSupplier imageData) {
@Override
public void onRequestFail(Throwable throwable) {
if (sourceType == ImageSourceType.SRC) {
onFetchImageFail();
onFetchImageFail(uri, throwable);
} else {
mDefaultImageFetchState = ImageFetchState.UNLOAD;
}
Expand All @@ -311,17 +333,17 @@ private void fetchImageWithUrl(String uri, ImageSourceType sourceType) {
return;
}
LogUtils.d(TAG, "fetchImageWithUrl: host id " + getHostId() + ", uri " + uri);
ImageDataSupplier imageData = mImageLoader.getImageFromCache(uri);
if (imageData != null && imageData.checkImageData()) {
onFetchImageSuccess(uri, sourceType, imageData, true);
return;
}
if (sourceType == ImageSourceType.SRC) {
mImageFetchState = ImageFetchState.LOADING;
onFetchImageStart();
} else {
mDefaultImageFetchState = ImageFetchState.LOADING;
}
ImageDataSupplier imageData = mImageLoader.getImageFromCache(uri);
if (imageData != null && imageData.checkImageData()) {
onFetchImageSuccess(uri, sourceType, imageData, true);
return;
}
if (UrlUtils.isWebUrl(uri)) {
uri = uri.trim().replaceAll(" ", "%20");
}
Expand Down
Loading

0 comments on commit d9f8b3e

Please sign in to comment.