diff --git a/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java b/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java index 083dc90d55..ec53b9c082 100644 --- a/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java +++ b/android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java @@ -67,11 +67,15 @@ import com.taobao.weex.instance.InstanceOnFireEventInterceptor; import com.taobao.weex.layout.ContentBoxMeasurement; import com.taobao.weex.performance.WXInstanceApm; +import com.taobao.weex.performance.WXAnalyzerDataTransfer; +import com.taobao.weex.prerender.PreRenderContext; import com.taobao.weex.tracing.WXTracing; import com.taobao.weex.ui.action.GraphicActionAddElement; +import com.taobao.weex.ui.action.GraphicActionBaseAddElement; import com.taobao.weex.ui.component.NestedContainer; import com.taobao.weex.ui.component.WXComponent; import com.taobao.weex.ui.component.WXEmbed; +import com.taobao.weex.ui.component.node.WXComponentNode; import com.taobao.weex.ui.flat.FlatGUIContext; import com.taobao.weex.ui.view.WXScrollView; import com.taobao.weex.utils.Trace; @@ -90,6 +94,7 @@ import java.util.Map; import java.util.PriorityQueue; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; /** @@ -139,8 +144,14 @@ public class WXSDKInstance implements IWXActivityStateListener,View.OnLayoutChan private @NonNull FlatGUIContext mFlatGUIContext =new FlatGUIContext(); + private float mRealWidth = 0; + private float mRealHeight = 0; + private Map mContainerInfo; + @NonNull + private PreRenderContext mPrerenderContext = new PreRenderContext(); + public boolean isNewFsEnd = false; /** @@ -206,7 +217,7 @@ public void removeLayerOverFlowListener(String ref) { private boolean mCurrentGround = false; private ComponentObserver mComponentObserver; - private Map inactiveAddElementAction = new ArrayMap<>(); + private Map inactiveAddElementAction = new ArrayMap<>(); private Map mContentBoxMeasurements = new ArrayMap<>(); @@ -266,6 +277,85 @@ public void setUseSingleProcess(boolean flag) { WXBridgeManager.getInstance().setUseSingleProcess(flag); } + public void preRenderByUrl(String pageName, + final String url, + Map options, + final String jsonInitData, + final int width, + final int height, + final WXRenderStrategy flag, + final int layoutMode, final float offset) { + if (mPrerenderContext.interceptRenderState.compareAndSet(PreRenderContext.INTERCEPT_RENDER_CLOSE, PreRenderContext.INTERCEPT_RENDER_OPEN)) { + mPrerenderContext.width = width; + mPrerenderContext.height = height; + mPrerenderContext.enableOffset = offset; + mPrerenderContext.setLayoutMode(layoutMode); + mPrerenderContext.onPreRender(); + renderByUrl(pageName, url, options, jsonInitData, flag); + } + } + + public void preRenderByTemplateWithLayoutMode(String pageName, + final String template, + Map options, + final String jsonInitData, + final int width, + final int height, + final WXRenderStrategy flag, + final int layoutMode, + final float offset) { + if (mPrerenderContext.interceptRenderState.compareAndSet(PreRenderContext.INTERCEPT_RENDER_CLOSE, PreRenderContext.INTERCEPT_RENDER_OPEN)) { + mPrerenderContext.width = width; + mPrerenderContext.height = height; + mPrerenderContext.enableOffset = offset; + mPrerenderContext.setLayoutMode(layoutMode); + mPrerenderContext.onPreRender(); + render(pageName, template, options, jsonInitData, flag); + } + } + + public boolean realRender(@NonNull Context context) { + if (mPrerenderContext.interceptRenderState.compareAndSet(PreRenderContext.INTERCEPT_RENDER_OPEN, PreRenderContext.INTERCEPT_RENDER_CLOSE)) { + mContext = context; + mPrerenderContext.onRealRender(); + WXComponentNode rootNode = mPrerenderContext.rootNode; + if (rootNode != null) { + mPrerenderContext.rootNode.startTransform(); + return true; + } else { + WXLogUtils.w("real render failed, rootNode is null"); + } + } else { + WXLogUtils.w("real render failed, state wrong"); + } + return false; + } + + public void specifiedRootNode(WXComponentNode rootNode) { + mPrerenderContext.rootNode = rootNode; + } + + public void updateRootNode() { + updateDefaultRootSize(mPrerenderContext.getRenderWidth(), mPrerenderContext.getRenderHeight()); + } + + public AtomicBoolean isRenderSuccess() { + return mPrerenderContext.isRenderSuccess; + } + + @NonNull + public PreRenderContext getPrerenderContext() { + return mPrerenderContext; + } + + public Map getNodeMap() { + return mPrerenderContext.nodeMap; + } + + public boolean getNeedInterceptRender() { + return mPrerenderContext.interceptRenderState.get() == PreRenderContext.INTERCEPT_RENDER_OPEN; + } + /** * set open SandBox * @param flag @@ -288,7 +378,7 @@ public void setMaxHiddenEmbedsNum(int maxHiddenEmbedsNum) { @WorkerThread @RestrictTo(Scope.LIBRARY) - public void addInActiveAddElementAction(String ref, GraphicActionAddElement action){ + public void addInActiveAddElementAction(String ref, GraphicActionBaseAddElement action){ inactiveAddElementAction.put(ref, action); } @@ -300,7 +390,7 @@ public void removeInActiveAddElmentAction(String ref){ @WorkerThread @RestrictTo(Scope.LIBRARY) - public GraphicActionAddElement getInActiveAddElementAction(String ref){ + public GraphicActionBaseAddElement getInActiveAddElementAction(String ref){ return inactiveAddElementAction.get(ref); } @@ -1712,27 +1802,51 @@ public void setSize(int width, int height) { } ViewGroup.LayoutParams layoutParams = mRenderContainer.getLayoutParams(); if (layoutParams != null) { - final float realWidth = width; - final float realHeight = height; + + if (mRenderContainer.getWidth() != width || mRenderContainer.getHeight() != height) { layoutParams.width = width; layoutParams.height = height; mRenderContainer.setLayoutParams(layoutParams); } - if (mRootComp != null && layoutParams != null) { - final boolean isWidthWrapContent = layoutParams.width == ViewGroup.LayoutParams.WRAP_CONTENT; - final boolean isHeightWrapContent = layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT; + updateDefaultRootSize(width, height); + } + } + } + + private void updateDefaultRootSize(final int width, final int height) { + // 预渲染中定下的宽高需要有个误差值,防止浮点的计算误差 + float offset = mPrerenderContext.enableOffset; + if (Math.abs(mRealWidth - width) < offset && Math.abs(mRealHeight - height) < offset) { + WXLogUtils.w("update default root size failed, size not changed"); + return; + } + int paramWidth = width; + int paramHeight = height; - WXBridgeManager.getInstance().post(new Runnable() { - @Override - public void run() { - WXBridgeManager.getInstance().setDefaultRootSize(getInstanceId(), realWidth, realHeight, isWidthWrapContent, - isHeightWrapContent); - } - }); + if (mRenderContainer != null && mRenderContainer.getLayoutParams() != null) { + paramWidth = mRenderContainer.getLayoutParams().width; + paramHeight = mRenderContainer.getLayoutParams().height; + } + + WXLogUtils.i("update default size, oldSize:(" + mRealWidth + ", " + mRealHeight + "), newSize:(" + width + ", " + height + ")"); + if (mRootComp != null || mPrerenderContext.rootNode != null) { + final boolean isWidthWrapContent = (paramWidth == ViewGroup.LayoutParams.WRAP_CONTENT); + final boolean isHeightWrapContent = (paramHeight == ViewGroup.LayoutParams.WRAP_CONTENT); + + final float realWidth = width; + final float realHeight = height; + mRealWidth = realWidth; + mRealHeight = realHeight; + + WXBridgeManager.getInstance().post(new Runnable() { + @Override + public void run() { + WXBridgeManager.getInstance().setDefaultRootSize(getInstanceId(), realWidth, realHeight, isWidthWrapContent, + isHeightWrapContent); } - } + }); } } @@ -2035,4 +2149,8 @@ public void addInstanceOnFireEventInterceptor(InstanceOnFireEventInterceptor ins getInstanceOnFireEventInterceptorList().add(instanceOnFireEventInterceptor); } } + + public boolean hasPrerender() { + return mPrerenderContext.isPrerenderMode(); + } } diff --git a/android/sdk/src/main/java/com/taobao/weex/bridge/WXBridgeManager.java b/android/sdk/src/main/java/com/taobao/weex/bridge/WXBridgeManager.java index 792c0e79ac..3010ec5862 100644 --- a/android/sdk/src/main/java/com/taobao/weex/bridge/WXBridgeManager.java +++ b/android/sdk/src/main/java/com/taobao/weex/bridge/WXBridgeManager.java @@ -58,14 +58,13 @@ import com.taobao.weex.dom.CSSShorthand; import com.taobao.weex.layout.ContentBoxMeasurement; import com.taobao.weex.performance.WXInstanceApm; +import com.taobao.weex.ui.IActionCreator; import com.taobao.weex.ui.WXComponentRegistry; import com.taobao.weex.ui.action.ActionReloadPage; import com.taobao.weex.ui.action.BasicGraphicAction; -import com.taobao.weex.ui.action.GraphicActionAddElement; -import com.taobao.weex.ui.action.GraphicActionAddEvent; import com.taobao.weex.ui.action.GraphicActionAppendTreeCreateFinish; -import com.taobao.weex.ui.action.GraphicActionCreateBody; -import com.taobao.weex.ui.action.GraphicActionCreateFinish; +import com.taobao.weex.ui.action.GraphicActionBaseAddElement; +import com.taobao.weex.ui.action.GraphicActionCreator; import com.taobao.weex.ui.action.GraphicActionLayout; import com.taobao.weex.ui.action.GraphicActionMoveElement; import com.taobao.weex.ui.action.GraphicActionRefreshFinish; @@ -78,6 +77,7 @@ import com.taobao.weex.ui.action.GraphicSize; import com.taobao.weex.ui.component.WXComponent; import com.taobao.weex.ui.module.WXDomModule; +import com.taobao.weex.ui.prerenderaction.PrerenderActionCreator; import com.taobao.weex.utils.WXExceptionUtils; import com.taobao.weex.utils.WXFileUtils; import com.taobao.weex.utils.WXJsonUtils; @@ -206,6 +206,9 @@ public enum BundType { private static Map mWeexCoreEnvOptions = new HashMap<>(); + private IActionCreator mPrerenderCreator = new PrerenderActionCreator(); + private IActionCreator mNormalCreator = new GraphicActionCreator(); + /** * package **/ @@ -2456,7 +2459,7 @@ public int callCreateBody(String pageId, String componentType, String ref, try { WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(pageId); if (instance != null) { - final BasicGraphicAction action = new GraphicActionCreateBody(instance, ref, componentType, + final BasicGraphicAction action = getActionCreator(instance).createGraphicActionCreateBody(instance, ref, componentType, styles, attributes, events, margins, paddings, borders); WXSDKManager.getInstance().getWXRenderManager().postGraphicAction(action.getPageId(), action); } @@ -2501,8 +2504,10 @@ public int callAddElement(String pageId, String componentType, String ref, int i try { WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(pageId); if (instance != null) { - final GraphicActionAddElement action = new GraphicActionAddElement(instance, ref, componentType, parentRef, index, - styles, attributes, events, margins, paddings, borders); + final GraphicActionBaseAddElement action = getActionCreator(instance) + .createGraphicActionAddElement(instance, + ref, componentType, parentRef, index, + styles, attributes, events, margins, paddings, borders); if(willLayout) { instance.addInActiveAddElementAction(ref, action); }else{ @@ -2545,7 +2550,7 @@ public int callRemoveElement(String instanceId, String ref) { try { WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId); if (instance != null) { - final BasicGraphicAction action = new GraphicActionRemoveElement(instance, ref); + final BasicGraphicAction action = getActionCreator(instance).createGraphicActionRemoveElement(instance, ref); if(instance.getInActiveAddElementAction(ref)!=null){ instance.removeInActiveAddElmentAction(ref); } @@ -2592,7 +2597,7 @@ public int callMoveElement(String instanceId, String ref, String parentref, int try { WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId); if (instance != null) { - final BasicGraphicAction action = new GraphicActionMoveElement(instance, ref, parentref, index); + final BasicGraphicAction action = getActionCreator(instance).createGraphicActionMoveElement(instance, ref, parentref, index); WXSDKManager.getInstance().getWXRenderManager().postGraphicAction(action.getPageId(), action); } } catch (Exception e) { @@ -2632,7 +2637,7 @@ public int callAddEvent(String instanceId, String ref, String event) { try { WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId); if (instance != null) { - new GraphicActionAddEvent(instance, ref, event).executeActionOnRender(); + getActionCreator(instance).createGraphicActionAddEvent(instance, ref, event).executeActionOnRender(); } } catch (Exception e) { WXLogUtils.e("[WXBridgeManager] callAddEvent exception: ", e); @@ -2673,7 +2678,7 @@ public int callRemoveEvent(String instanceId, String ref, String event) { try { WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId); if (instance != null) { - new GraphicActionRemoveEvent(instance, ref, event).executeActionOnRender(); + getActionCreator(instance).createGraphicActionRemoveEvent(instance, ref, event).executeActionOnRender(); } } catch (Exception e) { WXLogUtils.e("[WXBridgeManager] callRemoveEvent exception: ", e); @@ -2720,7 +2725,7 @@ public int callUpdateStyle(String instanceId, String ref, HashMap entry : mWeexCoreEnvOptions.entrySet()) { mWXBridge.registerCoreEnv(entry.getKey(), entry.getValue()); diff --git a/android/sdk/src/main/java/com/taobao/weex/common/WXRenderStrategy.java b/android/sdk/src/main/java/com/taobao/weex/common/WXRenderStrategy.java index 36ef8aa528..b4f7c29b31 100644 --- a/android/sdk/src/main/java/com/taobao/weex/common/WXRenderStrategy.java +++ b/android/sdk/src/main/java/com/taobao/weex/common/WXRenderStrategy.java @@ -24,6 +24,7 @@ public enum WXRenderStrategy { DATA_RENDER("DATA_RENDER"), DATA_RENDER_BINARY("DATA_RENDER_BINARY"); + private String flag; WXRenderStrategy(String flag) { diff --git a/android/sdk/src/main/java/com/taobao/weex/dom/action/TraceableAction.java b/android/sdk/src/main/java/com/taobao/weex/dom/action/TraceableAction.java new file mode 100644 index 0000000000..e69de29bb2 diff --git a/android/sdk/src/main/java/com/taobao/weex/prerender/PreRenderContext.java b/android/sdk/src/main/java/com/taobao/weex/prerender/PreRenderContext.java new file mode 100644 index 0000000000..d50bdbac59 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/prerender/PreRenderContext.java @@ -0,0 +1,132 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.prerender; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.ViewGroup; + +import com.taobao.weex.WeexFrameRateControl; +import com.taobao.weex.ui.component.node.WXComponentNode; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Created by luciolong on 18/07/2018. + */ +public class PreRenderContext implements WeexFrameRateControl.VSyncListener { + public static final int INTERCEPT_RENDER_CLOSE = 0; + public static final int INTERCEPT_RENDER_OPEN = 1; + + public static final int LAYOUT_MODE_SPECIFIC = 0; + public static final int LAYOUT_MODE_ADAPTIVE = 1; + public static final int LAYOUT_MODE_MATCH_PARENT = 2; + + public static final float DEFAULT_OFFSET = 0.00000001f; + + // need intercept real render + public AtomicInteger interceptRenderState = new AtomicInteger(INTERCEPT_RENDER_CLOSE); + + private WeexFrameRateControl mFrameRateControl; + private int mLayoutMode = LAYOUT_MODE_SPECIFIC; + + @Nullable + public WXComponentNode rootNode; + + @NonNull + public Map nodeMap = new HashMap<>(); + + public int width = 0; + public int height = 0; + + public float enableOffset = DEFAULT_OFFSET; + + public AtomicBoolean isRenderSuccess = new AtomicBoolean(false); + + private boolean mIsPrerenderMode = false; + + @Override + public void OnVSync() { + if (rootNode != null) { + rootNode.getWxInstance().OnVSync(); + } + } + + public void onPreRender() { + if (mFrameRateControl == null) { + mFrameRateControl = new WeexFrameRateControl(this); + } + mFrameRateControl.start(); + mIsPrerenderMode = true; + } + + public void setLayoutMode(int layoutMode) { + switch (layoutMode) { + case LAYOUT_MODE_SPECIFIC: + if (width <= 0 || height <= 0) { + mLayoutMode = LAYOUT_MODE_ADAPTIVE; + break; + } + mLayoutMode = LAYOUT_MODE_SPECIFIC; + default: + mLayoutMode = layoutMode; + break; + } + } + + public int getLayoutMode() { + return mLayoutMode; + } + + public void onRealRender() { + if (mFrameRateControl != null) { + mFrameRateControl.stop(); + mFrameRateControl = null; + } + } + + public int getRenderWidth() { + switch (mLayoutMode) { + case LAYOUT_MODE_ADAPTIVE: + return ViewGroup.LayoutParams.WRAP_CONTENT; + case LAYOUT_MODE_MATCH_PARENT: + return ViewGroup.LayoutParams.MATCH_PARENT; + default: + return width; + } + } + + public int getRenderHeight() { + switch (mLayoutMode) { + case LAYOUT_MODE_ADAPTIVE: + return ViewGroup.LayoutParams.WRAP_CONTENT; + case LAYOUT_MODE_MATCH_PARENT: + return ViewGroup.LayoutParams.MATCH_PARENT; + default: + return height; + } + } + + public boolean isPrerenderMode() { + return mIsPrerenderMode; + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/prerender/WXPrerenderManager.java b/android/sdk/src/main/java/com/taobao/weex/prerender/WXPrerenderManager.java new file mode 100644 index 0000000000..0fa449dc68 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/prerender/WXPrerenderManager.java @@ -0,0 +1,108 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.prerender; + +import android.content.Context; +import android.support.annotation.Nullable; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.common.WXRenderStrategy; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Created by luciolong on 03/08/2018. + */ +public class WXPrerenderManager { + private volatile ConcurrentHashMap mPreloadInstanceMap = new ConcurrentHashMap<>(); + + private static class SingletonHolder { + private static final WXPrerenderManager INSTANCE = new WXPrerenderManager(); + } + + public static WXPrerenderManager getInstance() { + return SingletonHolder.INSTANCE; + } + + private WXPrerenderManager() { + } + + public WXSDKInstance preRenderByUrl(Context context, String pageName, final String url, + Map options, final String jsonInitData, + final int width, final int height, final WXRenderStrategy flag) { + return preRenderByUrl(context, pageName, url, options, jsonInitData, width, height, flag, PreRenderContext.LAYOUT_MODE_SPECIFIC, PreRenderContext.DEFAULT_OFFSET); + } + + public WXSDKInstance preRenderByUrl(Context context, String pageName, final String url, + Map options, final String jsonInitData, + final int width, final int height, final WXRenderStrategy flag, final int layoutMode) { + return preRenderByUrl(context, pageName, url, options, jsonInitData, width, height, flag, layoutMode, PreRenderContext.DEFAULT_OFFSET); + } + + public WXSDKInstance preRenderByUrl(Context context, String pageName, final String url, + Map options, final String jsonInitData, + final int width, final int height, final WXRenderStrategy flag, + final int layoutMode, final float offset) { + if (!mPreloadInstanceMap.containsKey(url)) { + WXSDKInstance instance = new WXSDKInstance(context); + instance.preRenderByUrl(pageName, url, options, jsonInitData, width, height, flag, layoutMode, offset); + mPreloadInstanceMap.put(url, instance); + return instance; + } else { + return mPreloadInstanceMap.get(url); + } + } + + public WXSDKInstance preRenderByTemplate(Context context, String pageName, final String url, + String template, Map options, final String jsonInitData, + final int width, final int height, final WXRenderStrategy flag) { + return preRenderByTemplate(context, pageName, url, template, options, jsonInitData, width, height, flag, PreRenderContext.LAYOUT_MODE_SPECIFIC, PreRenderContext.DEFAULT_OFFSET); + } + + public WXSDKInstance preRenderByTemplate(Context context, String pageName, final String url, + String template, Map options, final String jsonInitData, + final int width, final int height, final WXRenderStrategy flag, final int layoutMode) { + return preRenderByTemplate(context, pageName, url, template, options, jsonInitData, width, height, flag, layoutMode, PreRenderContext.DEFAULT_OFFSET); + } + + public WXSDKInstance preRenderByTemplate(Context context, String pageName, final String url, + String template, Map options, final String jsonInitData, + final int width, final int height, final WXRenderStrategy flag, + final int layoutMode, final float offset) { + if (!mPreloadInstanceMap.containsKey(url)) { + WXSDKInstance instance = new WXSDKInstance(context); + instance.preRenderByTemplateWithLayoutMode(pageName, template, options, jsonInitData, width, height, flag, layoutMode, offset); + mPreloadInstanceMap.put(url, instance); + return instance; + } else { + return mPreloadInstanceMap.get(url); + } + } + + @Nullable + public WXSDKInstance fetchPreload(String url) { + WXSDKInstance instance = mPreloadInstanceMap.get(url); + if (instance != null) { + mPreloadInstanceMap.remove(url); + } + return instance; + } + +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/IActionCreator.java b/android/sdk/src/main/java/com/taobao/weex/ui/IActionCreator.java new file mode 100644 index 0000000000..3dd3c539de --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/IActionCreator.java @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.action.GraphicActionBaseAddElement; +import com.taobao.weex.ui.action.GraphicPosition; +import com.taobao.weex.ui.action.GraphicSize; +import com.taobao.weex.ui.action.IExecutable; + +import java.util.Map; +import java.util.Set; + +/** + * Created by luciolong on 2019/1/23. + */ +public interface IActionCreator { + GraphicActionBaseAddElement createGraphicActionAddElement(@NonNull WXSDKInstance instance, String ref, + String componentType, String parentRef, + int index, + Map style, + Map attributes, + Set events, + float[] margins, + float[] paddings, + float[] borders); + + BasicGraphicAction createGraphicActionLayout(WXSDKInstance instance, + String ref, + GraphicPosition layoutPosition, + GraphicSize layoutSize, + boolean isRTL); + + IExecutable createActionInvokeMethod(String pageId, String ref, String method, JSONArray args); + + BasicGraphicAction createGraphicActionAddEvent(WXSDKInstance instance, String ref, Object event); + + BasicGraphicAction createGraphicActionAnimation(@NonNull WXSDKInstance instance, @NonNull String ref, @Nullable String animation, + @Nullable final String callBack); + + BasicGraphicAction createGraphicActionCreateBody(@NonNull WXSDKInstance instance, String ref, + String componentType, + Map style, + Map attributes, + Set events, + float[] margins, + float[] paddings, + float[] borders); + + BasicGraphicAction createGraphicActionCreateFinish(@NonNull WXSDKInstance instance); + + BasicGraphicAction createGraphicActionMoveElement(WXSDKInstance instance, String ref, String parentRef, int index); + + BasicGraphicAction createGraphicActionRemoveElement(WXSDKInstance instance, String ref); + + BasicGraphicAction createGraphicActionRemoveEvent(WXSDKInstance instance, String ref, Object event); + + BasicGraphicAction createGraphicActionRenderSuccess(@NonNull WXSDKInstance instance); + + BasicGraphicAction createGraphicActionScrollToElement(WXSDKInstance instance, String ref, JSONObject options); + + BasicGraphicAction createGraphicActionUpdateAttr(WXSDKInstance instance, String ref, + Map attrs); + + BasicGraphicAction createGraphicActionUpdateStyle(WXSDKInstance instance, String ref, + Map style, + Map paddings, + Map margins, + Map borders); +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/WXRenderManager.java b/android/sdk/src/main/java/com/taobao/weex/ui/WXRenderManager.java index ac02fa8278..eb91424ac6 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/WXRenderManager.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/WXRenderManager.java @@ -28,10 +28,12 @@ import com.taobao.weex.common.WXRuntimeException; import com.taobao.weex.common.WXThread; import com.taobao.weex.dom.RenderContext; +import com.taobao.weex.prerender.PreRenderContext; import com.taobao.weex.performance.WXInstanceApm; import com.taobao.weex.ui.action.BasicGraphicAction; import com.taobao.weex.ui.action.GraphicActionBatchAction; import com.taobao.weex.ui.component.WXComponent; +import com.taobao.weex.ui.component.node.WXComponentNode; import com.taobao.weex.utils.WXExceptionUtils; import com.taobao.weex.utils.WXUtils; @@ -48,6 +50,7 @@ public class WXRenderManager { private volatile ConcurrentHashMap mRenderContext; + private volatile ConcurrentHashMap mPreRenderContextMap; private WXRenderHandler mWXRenderHandler; private ArrayList> mBatchActions = new ArrayList<>(); private final int MAX_DROP_FRAME_NATIVE_BATCH = 2000; @@ -56,6 +59,7 @@ public class WXRenderManager { public WXRenderManager() { mRenderContext = new ConcurrentHashMap<>(); + mPreRenderContextMap = new ConcurrentHashMap<>(); mWXRenderHandler = new WXRenderHandler(); } @@ -63,6 +67,10 @@ public RenderContext getRenderContext(String instanceId) { return mRenderContext.get(instanceId); } + public PreRenderContext getPreRenderContext(String instanceId) { + return mPreRenderContextMap.get(instanceId); + } + public @Nullable WXComponent getWXComponent(String instanceId, String ref) { if (instanceId == null || TextUtils.isEmpty(ref)) { @@ -72,6 +80,16 @@ WXComponent getWXComponent(String instanceId, String ref) { return stmt == null ? null : stmt.getComponent(ref); } + public @Nullable + WXComponentNode getWXComponentNode(String instanceId, String ref) { + if (instanceId == null || TextUtils.isEmpty(ref)) { + return null; + } + + PreRenderContext preRenderContext = getPreRenderContext(instanceId); + return preRenderContext == null ? null : preRenderContext.nodeMap.get(ref); + } + public WXSDKInstance getWXSDKInstance(String instanceId) { RenderContextImpl statement = mRenderContext.get(instanceId); if (statement == null) { @@ -110,6 +128,7 @@ public void removeRenderStatement(String instanceId) { throw new WXRuntimeException("[WXRenderManager] removeRenderStatement can only be called in main thread"); } RenderContextImpl statement = mRenderContext.remove(instanceId); + mPreRenderContextMap.remove(instanceId); if (statement != null) { statement.destroy(); @@ -171,6 +190,7 @@ public void registerInstance(WXSDKInstance instance) { null); } else { mRenderContext.put(instance.getInstanceId(), new RenderContextImpl(instance)); + mPreRenderContextMap.put(instance.getInstanceId(), instance.getPrerenderContext()); } } diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/ActionInvokeMethod.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/ActionInvokeMethod.java index f078d1b684..2b387cec50 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/ActionInvokeMethod.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/ActionInvokeMethod.java @@ -35,7 +35,7 @@ public class ActionInvokeMethod implements IExecutable { private String mPageId; private String mRef; - public ActionInvokeMethod(String pageId, String ref, String method, JSONArray args) { + ActionInvokeMethod(String pageId, String ref, String method, JSONArray args) { this.mPageId = pageId; this.mRef = ref; this.mMethod = method; diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/BasicComponentData.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/BasicComponentData.java index 9d124504b2..d6b78065b0 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/BasicComponentData.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/BasicComponentData.java @@ -19,6 +19,7 @@ package com.taobao.weex.ui.action; import android.support.annotation.NonNull; +import android.text.TextUtils; import android.view.View; import com.taobao.weex.common.Constants; @@ -89,6 +90,34 @@ public final void addEvent(Set events) { mEvents.addAll(events); } + public final void addEvent(String event) { + if (TextUtils.isEmpty(event)) { + return; + } + + if (mEvents == null) { + mEvents = new WXEvent(); + } + + if (!mEvents.contains(event)) { + mEvents.add(event); + } + } + + public final void removeEvent(String event) { + if (TextUtils.isEmpty(event)) { + return; + } + + if (mEvents == null) { + mEvents = new WXEvent(); + } + + if (mEvents.contains(event)) { + mEvents.remove(event); + } + } + public final void addShorthand(float[] shorthand, CSSShorthand.TYPE type) { if (shorthand == null) { shorthand = new float[] {0, 0, 0, 0}; diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAbstractAddElement.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAbstractAddElement.java index 88baced440..f593da1e19 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAbstractAddElement.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAbstractAddElement.java @@ -29,7 +29,7 @@ import java.util.Map; import java.util.Set; -public abstract class GraphicActionAbstractAddElement extends BasicGraphicAction { +public abstract class GraphicActionAbstractAddElement extends GraphicActionBaseAddElement { protected String mComponentType; protected String mParentRef; @@ -99,4 +99,5 @@ public Map getAttributes() { public Set getEvents() { return mEvents; } + } diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAddElement.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAddElement.java index 9db1ee16ab..0cc15769c3 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAddElement.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAddElement.java @@ -25,7 +25,6 @@ import android.support.v4.util.ArrayMap; import android.text.TextUtils; import android.util.Log; -import com.taobao.weex.BuildConfig; import com.taobao.weex.WXSDKInstance; import com.taobao.weex.WXSDKManager; import com.taobao.weex.common.WXErrorCode; @@ -48,7 +47,7 @@ public class GraphicActionAddElement extends GraphicActionAbstractAddElement { private GraphicSize layoutSize; private boolean isLayoutRTL; - public GraphicActionAddElement(@NonNull WXSDKInstance instance, String ref, + GraphicActionAddElement(@NonNull WXSDKInstance instance, String ref, String componentType, String parentRef, int index, Map style, @@ -154,24 +153,28 @@ public GraphicActionAddElement(@NonNull WXSDKInstance instance, String ref, @RestrictTo(Scope.LIBRARY) @WorkerThread + @Override public void setRTL(boolean isRTL){ this.isLayoutRTL = isRTL; } @RestrictTo(Scope.LIBRARY) @WorkerThread + @Override public void setSize(GraphicSize graphicSize){ this.layoutSize = graphicSize; } @RestrictTo(Scope.LIBRARY) @WorkerThread + @Override public void setPosition(GraphicPosition position){ this.layoutPosition = position; } @RestrictTo(Scope.LIBRARY) @WorkerThread + @Override public void setIndex(int index){ mIndex = index; } diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAddEvent.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAddEvent.java index 3ef4a6c79e..ed7d23e587 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAddEvent.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionAddEvent.java @@ -31,7 +31,7 @@ public class GraphicActionAddEvent extends BasicGraphicAction { private final String mEvent; - public GraphicActionAddEvent(WXSDKInstance instance, String ref, Object event) { + GraphicActionAddEvent(WXSDKInstance instance, String ref, Object event) { super(instance, ref); this.mEvent = WXEvent.getEventName(event); } diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBaseAddElement.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBaseAddElement.java new file mode 100644 index 0000000000..984ca22c84 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionBaseAddElement.java @@ -0,0 +1,57 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.action; + +import android.support.annotation.RestrictTo; +import android.support.annotation.WorkerThread; + +import com.taobao.weex.WXSDKInstance; + +/** + * Created by luciolong on 2019/1/23. + */ +public abstract class GraphicActionBaseAddElement extends BasicGraphicAction { + public GraphicActionBaseAddElement(WXSDKInstance instance, String ref) { + super(instance, ref); + } + + @RestrictTo(RestrictTo.Scope.LIBRARY) + @WorkerThread + public void setRTL(boolean isRTL) { + // empty + } + + @RestrictTo(RestrictTo.Scope.LIBRARY) + @WorkerThread + public void setSize(GraphicSize graphicSize) { + // empty + } + + @RestrictTo(RestrictTo.Scope.LIBRARY) + @WorkerThread + public void setPosition(GraphicPosition position) { + // empty + } + + @RestrictTo(RestrictTo.Scope.LIBRARY) + @WorkerThread + public void setIndex(int index) { + // empty + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionCreateBody.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionCreateBody.java index 49eef03112..b162e0d940 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionCreateBody.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionCreateBody.java @@ -22,7 +22,6 @@ import android.widget.ScrollView; import com.taobao.weex.WXSDKInstance; -import com.taobao.weex.WXSDKManager; import com.taobao.weex.common.WXRenderStrategy; import com.taobao.weex.dom.transition.WXTransition; import com.taobao.weex.ui.component.WXComponent; @@ -36,7 +35,7 @@ public class GraphicActionCreateBody extends GraphicActionAbstractAddElement { private WXComponent component; - public GraphicActionCreateBody(@NonNull WXSDKInstance instance, String ref, + GraphicActionCreateBody(@NonNull WXSDKInstance instance, String ref, String componentType, Map style, Map attributes, diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionCreateFinish.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionCreateFinish.java index 1addfd383a..1621a7ad24 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionCreateFinish.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionCreateFinish.java @@ -34,7 +34,7 @@ public class GraphicActionCreateFinish extends BasicGraphicAction { private int mLayoutWidth; private int mLayoutHeight; - public GraphicActionCreateFinish(@NonNull WXSDKInstance instance) { + GraphicActionCreateFinish(@NonNull WXSDKInstance instance) { super(instance, ""); WXComponent component = instance.getRootComponent(); if (null != component) { diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionCreator.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionCreator.java new file mode 100644 index 0000000000..f0f8f7c978 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionCreator.java @@ -0,0 +1,137 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.action; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.ui.IActionCreator; + +import java.util.Map; +import java.util.Set; + +/** + * Created by luciolong on 2019/1/23. + */ +public class GraphicActionCreator implements IActionCreator { + @Override + public GraphicActionBaseAddElement createGraphicActionAddElement(@NonNull WXSDKInstance instance, + String ref, + String componentType, + String parentRef, + int index, + Map style, + Map attributes, + Set events, float[] margins, + float[] paddings, + float[] borders) { + return new GraphicActionAddElement(instance, + ref, componentType, parentRef, + index, style, attributes, events, + margins, paddings, borders); + } + + @Override + public BasicGraphicAction createGraphicActionLayout(WXSDKInstance instance, + String ref, + GraphicPosition layoutPosition, + GraphicSize layoutSize, + boolean isRTL) { + return new GraphicActionLayout(instance, ref, layoutPosition, layoutSize, isRTL); + } + + @Override + public IExecutable createActionInvokeMethod(String pageId, String ref, String method, JSONArray args) { + return new ActionInvokeMethod(pageId, ref, method, args); + } + + @Override + public BasicGraphicAction createGraphicActionAddEvent(WXSDKInstance instance, String ref, Object event) { + return new GraphicActionAddEvent(instance, ref,event); + } + + @Override + public BasicGraphicAction createGraphicActionAnimation(@NonNull WXSDKInstance instance, + @NonNull String ref, + @Nullable String animation, + @Nullable String callBack) { + return new GraphicActionAnimation(instance, ref, animation, callBack); + } + + @Override + public BasicGraphicAction createGraphicActionCreateBody(@NonNull WXSDKInstance instance, + String ref, + String componentType, + Map style, + Map attributes, + Set events, + float[] margins, + float[] paddings, + float[] borders) { + return new GraphicActionCreateBody(instance, ref, componentType, style, attributes, events, margins, paddings, borders); + } + + @Override + public BasicGraphicAction createGraphicActionCreateFinish(@NonNull WXSDKInstance instance) { + return new GraphicActionCreateFinish(instance); + } + + @Override + public BasicGraphicAction createGraphicActionMoveElement(WXSDKInstance instance, String ref, String parentRef, int index) { + return new GraphicActionMoveElement(instance, ref, parentRef, index); + } + + @Override + public BasicGraphicAction createGraphicActionRemoveElement(WXSDKInstance instance, String ref) { + return new GraphicActionRemoveElement(instance, ref); + } + + @Override + public BasicGraphicAction createGraphicActionRemoveEvent(WXSDKInstance instance, String ref, Object event) { + return new GraphicActionRemoveEvent(instance, ref, event); + } + + @Override + public BasicGraphicAction createGraphicActionRenderSuccess(@NonNull WXSDKInstance instance) { + return new GraphicActionRenderSuccess(instance); + } + + @Override + public BasicGraphicAction createGraphicActionScrollToElement(WXSDKInstance instance, String ref, JSONObject options) { + return new GraphicActionScrollToElement(instance, ref, options); + } + + @Override + public BasicGraphicAction createGraphicActionUpdateAttr(WXSDKInstance instance, String ref, Map attrs) { + return new GraphicActionUpdateAttr(instance, ref, attrs); + } + + @Override + public BasicGraphicAction createGraphicActionUpdateStyle(WXSDKInstance instance, + String ref, + Map style, + Map paddings, + Map margins, + Map borders) { + return new GraphicActionUpdateStyle(instance, ref, style, paddings, margins, borders); + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionLayout.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionLayout.java index cffef17511..e8e362325e 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionLayout.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionLayout.java @@ -28,7 +28,7 @@ public class GraphicActionLayout extends BasicGraphicAction { private final GraphicSize mLayoutSize; private final boolean mIsLayoutRTL; - public GraphicActionLayout(WXSDKInstance instance, String ref, GraphicPosition layoutPosition, GraphicSize layoutSize, boolean isRTL) { + GraphicActionLayout(WXSDKInstance instance, String ref, GraphicPosition layoutPosition, GraphicSize layoutSize, boolean isRTL) { super(instance, ref); this.mLayoutPosition = layoutPosition; this.mLayoutSize = layoutSize; diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionMoveElement.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionMoveElement.java index 424ee8416e..393a9a2b22 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionMoveElement.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionMoveElement.java @@ -30,7 +30,7 @@ public class GraphicActionMoveElement extends BasicGraphicAction { private String mParentref; private int mIndex; - public GraphicActionMoveElement(WXSDKInstance instance, String ref, String parentRef, int index) { + GraphicActionMoveElement(WXSDKInstance instance, String ref, String parentRef, int index) { super(instance, ref); this.mParentref = parentRef; this.mIndex = index; diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRemoveElement.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRemoveElement.java index b7e45512ef..91febda830 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRemoveElement.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRemoveElement.java @@ -27,7 +27,7 @@ public class GraphicActionRemoveElement extends BasicGraphicAction { - public GraphicActionRemoveElement(WXSDKInstance instance, String ref) { + GraphicActionRemoveElement(WXSDKInstance instance, String ref) { super(instance, ref); } diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRemoveEvent.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRemoveEvent.java index 67fe1335b1..c0e13a51aa 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRemoveEvent.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRemoveEvent.java @@ -31,7 +31,7 @@ public class GraphicActionRemoveEvent extends BasicGraphicAction { private final String mEvent; - public GraphicActionRemoveEvent(WXSDKInstance instance, String ref, Object event) { + GraphicActionRemoveEvent(WXSDKInstance instance, String ref, Object event) { super(instance, ref); this.mEvent = WXEvent.getEventName(event); } diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRenderSuccess.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRenderSuccess.java index ecd12b1102..b82a68988b 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRenderSuccess.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionRenderSuccess.java @@ -25,7 +25,7 @@ public class GraphicActionRenderSuccess extends BasicGraphicAction { - public GraphicActionRenderSuccess(@NonNull WXSDKInstance instance) { + GraphicActionRenderSuccess(@NonNull WXSDKInstance instance) { super(instance, ""); } diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionScrollToElement.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionScrollToElement.java index c03e93094a..6c5937a6ff 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionScrollToElement.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionScrollToElement.java @@ -31,7 +31,7 @@ public class GraphicActionScrollToElement extends BasicGraphicAction { private final JSONObject mOptions; - public GraphicActionScrollToElement(WXSDKInstance instance, String ref, JSONObject options) { + GraphicActionScrollToElement(WXSDKInstance instance, String ref, JSONObject options) { super(instance, ref); this.mOptions = options; } diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionUpdateAttr.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionUpdateAttr.java index 4e6a00f295..69a6134c1d 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionUpdateAttr.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionUpdateAttr.java @@ -29,7 +29,7 @@ public class GraphicActionUpdateAttr extends BasicGraphicAction { private Map mAttrs; private WXComponent component; - public GraphicActionUpdateAttr(WXSDKInstance instance, String ref, + GraphicActionUpdateAttr(WXSDKInstance instance, String ref, Map attrs) { super(instance, ref); this.mAttrs = attrs; diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionUpdateStyle.java b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionUpdateStyle.java index bddb338df2..e7a484802d 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionUpdateStyle.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/action/GraphicActionUpdateStyle.java @@ -37,7 +37,7 @@ public class GraphicActionUpdateStyle extends BasicGraphicAction { private boolean mIsCausedByPesudo; private boolean mIsBorderSet; - public GraphicActionUpdateStyle(WXSDKInstance instance, String ref, + GraphicActionUpdateStyle(WXSDKInstance instance, String ref, Map style, Map paddings, Map margins, diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/animation/WXAnimationModule.java b/android/sdk/src/main/java/com/taobao/weex/ui/animation/WXAnimationModule.java index a614ba3202..744bfc94e2 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/animation/WXAnimationModule.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/animation/WXAnimationModule.java @@ -24,7 +24,9 @@ import com.taobao.weex.WXSDKInstance; import com.taobao.weex.WXSDKManager; import com.taobao.weex.annotation.JSMethod; +import com.taobao.weex.bridge.WXBridgeManager; import com.taobao.weex.common.WXModule; +import com.taobao.weex.ui.action.BasicGraphicAction; import com.taobao.weex.ui.action.GraphicActionAnimation; import com.taobao.weex.ui.component.WXComponent; import com.taobao.weex.ui.component.list.template.TemplateDom; @@ -36,7 +38,7 @@ public void transition(@Nullable String ref, @Nullable String animation, @Nullab if (!TextUtils.isEmpty(ref) && !TextUtils.isEmpty(animation) && mWXSDKInstance != null) { //Due to animation module rely on the result of the css-layout and the batch mechanism of //css-layout, the animation.transition must be delayed the batch time. - GraphicActionAnimation action = new GraphicActionAnimation(mWXSDKInstance, ref, animation, callBack); + BasicGraphicAction action = WXBridgeManager.getInstance().getActionCreator(mWXSDKInstance).createGraphicActionAnimation(mWXSDKInstance, ref, animation, callBack); WXSDKManager.getInstance().getWXRenderManager().postGraphicAction(action.getPageId(), action); } } diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/node/WXComponentNode.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/node/WXComponentNode.java new file mode 100644 index 0000000000..1c0cbf0b7c --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/node/WXComponentNode.java @@ -0,0 +1,518 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package com.taobao.weex.ui.component.node; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.util.ArrayMap; +import android.text.TextUtils; +import android.widget.ScrollView; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.bridge.WXBridgeManager; +import com.taobao.weex.common.Constants; +import com.taobao.weex.common.WXRenderStrategy; +import com.taobao.weex.dom.WXStyle; +import com.taobao.weex.dom.transition.WXTransition; +import com.taobao.weex.tracing.Stopwatch; +import com.taobao.weex.ui.action.BasicComponentData; +import com.taobao.weex.ui.action.GraphicPosition; +import com.taobao.weex.ui.action.GraphicSize; +import com.taobao.weex.ui.component.Scrollable; +import com.taobao.weex.ui.component.WXComponent; +import com.taobao.weex.ui.component.WXComponentFactory; +import com.taobao.weex.ui.component.WXScroller; +import com.taobao.weex.ui.component.WXVContainer; +import com.taobao.weex.ui.prerenderaction.PrerenderGraphicActionTransformNode; +import com.taobao.weex.utils.WXLogUtils; + +import java.util.ArrayList; +import java.util.Map; + +/** + * Created by luciolong on 14/07/2018. + */ +public class WXComponentNode implements Runnable { + private static final String TAG = "WXComponentNode"; + + private static final int EXECUTE_STATE_CREATE_BODY = 1; + private static final int EXECUTE_STATE_ADD_ELEMENT = 2; + + @NonNull + private BasicComponentData mComponentData; + + // parent node + @Nullable + private WXComponentNode mParentNode; + + // child node + @NonNull + private final ArrayList mChildNodes = new ArrayList<>(); + + @NonNull + private WXSDKInstance mWxInstance; + + @Nullable + public WXComponent data; + + private int mIndex = 0; + + private GraphicPosition mLayoutPosition; + private GraphicSize mLayoutSize; + private boolean mIsLayoutRTL; + private JSONObject mScrollOptions; + + private boolean mIsRenderSuccuess = false; + + private int mExecuteState = EXECUTE_STATE_ADD_ELEMENT; + + WXComponentNode(@NonNull BasicComponentData data, + @NonNull Map map, + @NonNull WXSDKInstance instance) { + this.mComponentData = data; + this.mWxInstance = instance; + map.put(data.mRef, this); + if (!TextUtils.isEmpty(data.mParentRef)) { + this.mParentNode = map.get(data.mParentRef); + } + } + + public void setIndex(int index) { + this.mIndex = index; + } + + public void setLayoutPosition(GraphicPosition layoutPosition) { + this.mLayoutPosition = layoutPosition; + } + + public void setLayoutSize(GraphicSize layoutSize) { + this.mLayoutSize = layoutSize; + } + + public void setIsLayoutRTL(boolean isRTL) { + this.mIsLayoutRTL = isRTL; + } + + public void startTransform() { + postTransformAction(); + if (mChildNodes.size() > 0) { + int i = 0; + for (WXComponentNode node : mChildNodes) { + node.startTransform(); + node.setIndex(i); + i++; + } + } + mIsRenderSuccuess = true; + } + + private void postTransformAction() { + WXBridgeManager.getInstance().post(this); + } + + public void transformNode() { + switch (mExecuteState) { + case EXECUTE_STATE_CREATE_BODY: + createBody(); + mWxInstance.firstScreenCreateInstanceTime(System.currentTimeMillis()); + onCreateFinish(); + break; + case EXECUTE_STATE_ADD_ELEMENT: + addElement(); + if (mIsRenderSuccuess && mWxInstance.isRenderSuccess().compareAndSet(false, true)) { + onRenderSuccess(); + } + break; + default: + break; + } + } + + public void createComponent() { + if (mWxInstance.getNeedInterceptRender()) { + return; + } + + if (data == null) { + WXLogUtils.d(TAG, "node real create component, type:" + mComponentData.mComponentType); + long createComponentStart = System.currentTimeMillis(); + String pageId = mWxInstance.getInstanceId(); + WXVContainer parent = null; + if (mParentNode != null) { + parent = (WXVContainer) WXSDKManager.getInstance().getWXRenderManager() + .getWXComponent(pageId, mParentNode.mComponentData.mRef); + } + + WXComponent component = WXComponentFactory.newInstance(mWxInstance, parent, mComponentData); + WXSDKManager.getInstance().getWXRenderManager().registerComponent(pageId, mComponentData.mRef, component); + WXStyle style = mComponentData.getStyles(); + if (style.containsKey(Constants.Name.TRANSFORM) && component.getTransition() == null) { + Map animationMap = new ArrayMap<>(2); + animationMap.put(Constants.Name.TRANSFORM, style.get(Constants.Name.TRANSFORM)); + animationMap + .put(Constants.Name.TRANSFORM_ORIGIN, style.get(Constants.Name.TRANSFORM_ORIGIN)); + component.addAnimationForElement(animationMap); + } + mWxInstance.onComponentCreate(component, System.currentTimeMillis() - createComponentStart); + component.setTransition(WXTransition.fromMap(component.getStyles(), component)); + + if (null != parent && parent.isIgnoreInteraction){ + component.isIgnoreInteraction = true; + } + if (!component.isIgnoreInteraction ){ + Object flag = component.getAttrs().get("ignoreInteraction"); + if ("1".equals(flag) || "true".equals(flag) || component.isFixed()){ + component.isIgnoreInteraction = true; + } + } + data = component; + } + } + + public void createBody() { + mWxInstance.specifiedRootNode(this); + if (mWxInstance.getNeedInterceptRender()) { + mExecuteState = EXECUTE_STATE_CREATE_BODY; + mWxInstance.updateRootNode(); + return; + } + try { + ensureDataNotNull(); + if (data != null) { + WXLogUtils.d(TAG, "node real create body"); + data.mIsAddElementToTree = true; + data.createView(); + data.applyLayoutAndEvent(data); + data.bindData(data); + + if (data instanceof WXScroller) { + WXScroller scroller = (WXScroller) data; + if (scroller.getInnerView() instanceof ScrollView) { + mWxInstance.setRootScrollView((ScrollView) scroller.getInnerView()); + } + } + + mWxInstance.onRootCreated(data); + + if (mWxInstance.getRenderStrategy() != WXRenderStrategy.APPEND_ONCE) { + mWxInstance.onCreateFinish(); + } + } + + } catch (Exception e) { + WXLogUtils.e("create body failed.", e); + } + } + + public void addElement() { + if (mParentNode == null) { + return; + } + mParentNode.addNode(this, mIndex); + if (mWxInstance.getNeedInterceptRender()) { + mExecuteState = EXECUTE_STATE_ADD_ELEMENT; + return; + } + + try { + ensureDataNotNull(); + if (data != null) { + if (!TextUtils.equals(mComponentData.mComponentType, "video") && !TextUtils.equals(mComponentData.mComponentType, "videoplus")) + data.mIsAddElementToTree = true; + mParentNode.addChild(this, mIndex); + mParentNode.createChildViewAt(mIndex); + + data.setIsLayoutRTL(mIsLayoutRTL); + if (mLayoutPosition != null && mLayoutSize != null) { + data.setDemission(mLayoutSize, mLayoutPosition); + } + + data.applyLayoutAndEvent(data); + + data.bindData(data); + scrollToElementInternal(); + } + + } catch (Exception e) { + WXLogUtils.e("add component failed.", e); + } + } + + private void addElementInternal(WXComponentNode childNode, int index) { + if (childNode.mParentNode != null) { + childNode.mParentNode.removeElementInternal(childNode, false); + } + childNode.mIndex = index; + addNode(childNode, index); + if (!mWxInstance.getNeedInterceptRender()) { + ensureDataNotNull(); + childNode.ensureDataNotNull(); + if (data instanceof WXVContainer && childNode.data != null) { + ((WXVContainer) data).addChild(childNode.data, index); + if (!data.isVirtualComponent()) { + ((WXVContainer) data).addSubView(childNode.data.getHostView(), mIndex); + } + } + } + } + + private void addNode(@NonNull WXComponentNode node, int index) { + int count = mChildNodes.size(); + index = index >= count ? -1 : index; + if (index == -1) { + mChildNodes.add(node); + } else { + mChildNodes.add(index, node); + } + } + + private void addChild(@NonNull WXComponentNode childNode, int index) { + ensureDataNotNull(); + childNode.ensureDataNotNull(); + + WXVContainer curContainer = (WXVContainer) WXSDKManager.getInstance().getWXRenderManager() + .getWXComponent(mWxInstance.getInstanceId(), mComponentData.mRef); + if (curContainer != null) { + curContainer.addChild(childNode.data, index); + } + } + + private void createChildViewAt(int index) { + ensureDataNotNull(); + + WXVContainer curContainer = (WXVContainer) WXSDKManager.getInstance().getWXRenderManager() + .getWXComponent(mWxInstance.getInstanceId(), mComponentData.mRef); + if (curContainer != null) { + curContainer.createChildViewAt(index); + } + } + + private void ensureDataNotNull() { + if (data == null) { + createComponent(); + } + } + + public void addEvent(String event) { + if (mWxInstance.getNeedInterceptRender()) { + mComponentData.addEvent(event); + return; + } + ensureDataNotNull(); + if (data != null) { + Stopwatch.tick(); + if (!data.getEvents().contains(event)) { + data.getEvents().addEvent(event); + } + data.addEvent(event); + Stopwatch.split("addEventToComponent"); + } + } + + public void invokeMethod(String method, JSONArray args) { + if (mWxInstance.getNeedInterceptRender()) { + return; + } + + ensureDataNotNull(); + if(data == null){ + WXLogUtils.e(WXLogUtils.WEEX_TAG, "invoke method, target component not found."); + return; + } + data.invoke(method, args); + } + + public void updateLayout(GraphicPosition layoutPosition, GraphicSize layoutSize, boolean isLayoutRTL) { + mLayoutSize = layoutSize; + mLayoutPosition = layoutPosition; + mIsLayoutRTL = isLayoutRTL; + if (mWxInstance.getNeedInterceptRender()) { + return; + } + + ensureDataNotNull(); + if (data == null) { + WXLogUtils.e(WXLogUtils.WEEX_TAG, "update layout, target component not found."); + return; + } + + data.setIsLayoutRTL(mIsLayoutRTL); + data.setDemission(mLayoutSize, mLayoutPosition); + data.setSafeLayout(data); + data.setPadding(data.getPadding(), data.getBorder()); + } + + public void moveElement(String parentRef, int index) { + WXComponentNode oldParent = mParentNode; + WXComponentNode newParent = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(mWxInstance.getInstanceId(), parentRef); + + if (oldParent == null || newParent == null) { + WXLogUtils.e(WXLogUtils.WEEX_TAG, "move element, find parent error"); + return; + } + + oldParent.removeElementInternal(this, false); + newParent.addElementInternal(this, index); + } + + public void removeElement() { + if (!mWxInstance.getNeedInterceptRender() && data != null) { + clearRegistryForComponent(data); + } + if (mParentNode != null && data != null) { + mParentNode.removeElementInternal(this, true); + } + } + + private void removeElementInternal(@NonNull WXComponentNode node, boolean destroy) { + if (this.mChildNodes.contains(node)) { + this.mChildNodes.remove(node); + } + node.mParentNode = null; + + if (!mWxInstance.getNeedInterceptRender() && data instanceof WXVContainer && node.data != null) { + ((WXVContainer)data).remove(node.data, destroy); + } + } + + private void clearRegistryForComponent(WXComponent component) { + WXComponent removedComponent = WXSDKManager.getInstance().getWXRenderManager().unregisterComponent(mWxInstance.getInstanceId(), mComponentData.mRef); + if (removedComponent != null) { + removedComponent.removeAllEvent(); + removedComponent.removeStickyStyle(); + } + if (component instanceof WXVContainer) { + WXVContainer container = (WXVContainer) component; + int count = container.childCount(); + for (int i = count - 1; i >= 0; --i) { + clearRegistryForComponent(container.getChild(i)); + } + } + } + + public void removeEvent(String event) { + if (mWxInstance.getNeedInterceptRender()) { + mComponentData.removeEvent(event); + return; + } + ensureDataNotNull(); + if (data != null) { + Stopwatch.tick(); + data.removeEvent(event); + Stopwatch.split("removeEventFromComponent"); + } + } + + public void scrollToElement(JSONObject scrollOptions) { + mScrollOptions = scrollOptions; + if (!mWxInstance.getNeedInterceptRender()) { + scrollToElementInternal(); + } + } + + private void scrollToElementInternal() { + ensureDataNotNull(); + if (data != null && mScrollOptions != null) { + Scrollable scroller = data.getParentScroller(); + if (scroller == null) { + return; + } + scroller.scrollTo(data, mScrollOptions); + mScrollOptions = null; + } + } + + public void addAttrs(Map attrs) { + if (attrs != null) { + mComponentData.addAttr(attrs); + + if (!mWxInstance.getNeedInterceptRender()) { + ensureDataNotNull(); + if (data != null) { + data.addAttr(attrs); + } + } + } + } + + public void updateAttrs(Map attrs) { + if (attrs != null) { + mComponentData.getAttrs().mergeAttr(); + if (!mWxInstance.getNeedInterceptRender()) { + ensureDataNotNull(); + if (data != null) { + data.updateAttrs(attrs); + } + } + } + } + + @NonNull + public BasicComponentData getComponentData() { + return mComponentData; + } + + @NonNull + public WXSDKInstance getWxInstance() { + return mWxInstance; + } + + @Override + public void run() { + new PrerenderGraphicActionTransformNode(this, mWxInstance, mComponentData.mRef).executeActionOnRender(); + } + + public void onCreateFinish() { + if (mWxInstance.getNeedInterceptRender()) { + return; + } + + mWxInstance.mHasCreateFinish = true; + + if (mWxInstance.getRenderStrategy() == WXRenderStrategy.APPEND_ONCE) { + mWxInstance.onCreateFinish(); + } + + if (null != mWxInstance.getWXPerformance()){ + mWxInstance.getWXPerformance().callCreateFinishTime = System.currentTimeMillis() - mWxInstance.getWXPerformance().renderTimeOrigin; + } + mWxInstance.onOldFsRenderTimeLogic(); + } + + public void onRenderSuccess() { + if (mWxInstance.getNeedInterceptRender()) { + return; + } + + ensureDataNotNull(); + + int layoutWidth = 0; + int layoutHeight = 0; + if (null != data) { + layoutWidth = (int) data.getLayoutWidth(); + layoutHeight = (int) data.getLayoutHeight(); + } + mWxInstance.onRenderSuccess(layoutWidth, layoutHeight); + } + +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/component/node/WXComponentNodeBuilder.java b/android/sdk/src/main/java/com/taobao/weex/ui/component/node/WXComponentNodeBuilder.java new file mode 100644 index 0000000000..1877b74678 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/component/node/WXComponentNodeBuilder.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package com.taobao.weex.ui.component.node; + +import android.support.annotation.NonNull; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.ui.action.BasicComponentData; +import com.taobao.weex.ui.action.GraphicPosition; +import com.taobao.weex.ui.action.GraphicSize; + +/** + * Created by luciolong on 30/07/2018. + */ +public class WXComponentNodeBuilder { + @NonNull + private final WXSDKInstance mInstance; + + @NonNull + private final BasicComponentData mData; + + private int mIndex = 0; + + private GraphicPosition mLayoutPosition; + private GraphicSize mLayoutSize; + + private WXComponentNodeBuilder(@NonNull WXSDKInstance instance, @NonNull BasicComponentData data) { + mInstance = instance; + mData = data; + } + + public static WXComponentNodeBuilder createInstance(@NonNull WXSDKInstance instance, + @NonNull BasicComponentData data) { + return new WXComponentNodeBuilder(instance, data); + } + + public WXComponentNode build() { + WXComponentNode node = new WXComponentNode(mData, mInstance.getNodeMap(), mInstance); + node.setIndex(mIndex); + node.setLayoutPosition(mLayoutPosition); + node.setLayoutSize(mLayoutSize); + return node; + } + + public WXComponentNodeBuilder setIndex(int index) { + this.mIndex = index; + return this; + } + + public WXComponentNodeBuilder setLayoutPosition(GraphicPosition layoutPosition) { + this.mLayoutPosition = layoutPosition; + return this; + } + + public WXComponentNodeBuilder setLayoutSize(GraphicSize layoutSize) { + this.mLayoutSize = layoutSize; + return this; + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/module/WXDomModule.java b/android/sdk/src/main/java/com/taobao/weex/ui/module/WXDomModule.java index 8f8c957380..10966d4e31 100644 --- a/android/sdk/src/main/java/com/taobao/weex/ui/module/WXDomModule.java +++ b/android/sdk/src/main/java/com/taobao/weex/ui/module/WXDomModule.java @@ -21,6 +21,8 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.bridge.WXBridge; import com.taobao.weex.bridge.WXBridgeManager; import com.taobao.weex.common.WXModule; import com.taobao.weex.dom.binding.JSONUtils; @@ -98,7 +100,7 @@ public Object callDomMethod(String method, JSONArray args, long... parseNanos) { } String ref = args.size() >= 1 ? args.getString(0) : null; JSONObject options = args.size() >= 2 ? args.getJSONObject(1) : null; - new GraphicActionScrollToElement(mWXSDKInstance, ref, options) + WXBridgeManager.getInstance().getActionCreator(mWXSDKInstance).createGraphicActionScrollToElement(mWXSDKInstance, ref, options) .executeActionOnRender(); break; } @@ -123,8 +125,8 @@ public Object callDomMethod(String method, JSONArray args, long... parseNanos) { return null; } // todo:no sure where the request com from - new ActionInvokeMethod(mWXSDKInstance.getInstanceId(), args.getString(0), args.getString(1), args.getJSONArray(2)) - .executeAction(); + WXBridgeManager.getInstance().getActionCreator(mWXSDKInstance).createActionInvokeMethod(mWXSDKInstance.getInstanceId(), + args.getString(0), args.getString(1), args.getJSONArray(2)).executeAction(); break; } case UPDATE_COMPONENT_DATA: @@ -192,8 +194,8 @@ public void invokeMethod(String ref, String method, JSONArray args){ if(ref == null || method == null){ return; } - - new ActionInvokeMethod(mWXSDKInstance.getInstanceId(), ref, method, args) + WXBridgeManager.getInstance().getActionCreator(mWXSDKInstance) + .createActionInvokeMethod(mWXSDKInstance.getInstanceId(), ref, method, args) .executeAction(); } } diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderActionCreator.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderActionCreator.java new file mode 100644 index 0000000000..145cfd09aa --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderActionCreator.java @@ -0,0 +1,125 @@ +package com.taobao.weex.ui.prerenderaction; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.ui.IActionCreator; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.action.GraphicActionBaseAddElement; +import com.taobao.weex.ui.action.GraphicPosition; +import com.taobao.weex.ui.action.GraphicSize; +import com.taobao.weex.ui.action.IExecutable; + +import java.util.Map; +import java.util.Set; + +/** + * Description + * Created by luciolong on 2019/1/23. + */ +public class PrerenderActionCreator implements IActionCreator { + @Override + public GraphicActionBaseAddElement createGraphicActionAddElement(@NonNull WXSDKInstance instance, + String ref, + String componentType, + String parentRef, + int index, + Map style, + Map attributes, + Set events, float[] margins, + float[] paddings, + float[] borders) { + return new PrerenderGraphicActionAddElement(instance, + ref, componentType, parentRef, + index, style, attributes, events, + margins, paddings, borders); + } + + @Override + public BasicGraphicAction createGraphicActionLayout(WXSDKInstance instance, + String ref, + GraphicPosition layoutPosition, + GraphicSize layoutSize, + boolean isRTL) { + return new PrerenderGraphicActionLayout(instance, ref, layoutPosition, layoutSize, isRTL); + } + + @Override + public IExecutable createActionInvokeMethod(String pageId, String ref, String method, JSONArray args) { + return new PrerenderActionInvokeMethod(pageId, ref, method, args); + } + + @Override + public BasicGraphicAction createGraphicActionAddEvent(WXSDKInstance instance, String ref, Object event) { + return new PrerenderGraphicActionAddEvent(instance, ref,event); + } + + @Override + public BasicGraphicAction createGraphicActionAnimation(@NonNull WXSDKInstance instance, + @NonNull String ref, + @Nullable String animation, + @Nullable String callBack) { + return new PrerenderGraphicActionAnimation(instance, ref, animation, callBack); + } + + @Override + public BasicGraphicAction createGraphicActionCreateBody(@NonNull WXSDKInstance instance, + String ref, + String componentType, + Map style, + Map attributes, + Set events, + float[] margins, + float[] paddings, + float[] borders) { + return new PrerenderGraphicActionCreateBody(instance, ref, componentType, style, attributes, events, margins, paddings, borders); + } + + @Override + public BasicGraphicAction createGraphicActionCreateFinish(@NonNull WXSDKInstance instance) { + return new PrerenderGraphicActionCreateFinish(instance); + } + + @Override + public BasicGraphicAction createGraphicActionMoveElement(WXSDKInstance instance, String ref, String parentRef, int index) { + return new PrerenderGraphicActionMoveElement(instance, ref, parentRef, index); + } + + @Override + public BasicGraphicAction createGraphicActionRemoveElement(WXSDKInstance instance, String ref) { + return new PrerenderGraphicActionRemoveElement(instance, ref); + } + + @Override + public BasicGraphicAction createGraphicActionRemoveEvent(WXSDKInstance instance, String ref, Object event) { + return new PrerenderGraphicActionRemoveEvent(instance, ref, event); + } + + @Override + public BasicGraphicAction createGraphicActionRenderSuccess(@NonNull WXSDKInstance instance) { + return new PrerenderGraphicActionRenderSuccess(instance); + } + + @Override + public BasicGraphicAction createGraphicActionScrollToElement(WXSDKInstance instance, String ref, JSONObject options) { + return new PrerenderGraphicActionScrollToElement(instance, ref, options); + } + + @Override + public BasicGraphicAction createGraphicActionUpdateAttr(WXSDKInstance instance, String ref, Map attrs) { + return new PrerenderGraphicActionUpdateAttr(instance, ref, attrs); + } + + @Override + public BasicGraphicAction createGraphicActionUpdateStyle(WXSDKInstance instance, + String ref, + Map style, + Map paddings, + Map margins, + Map borders) { + return new PrerenderGraphicActionUpdateStyle(instance, ref, style, paddings, margins, borders); + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderActionInvokeMethod.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderActionInvokeMethod.java new file mode 100644 index 0000000000..9aa80bd6d6 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderActionInvokeMethod.java @@ -0,0 +1,52 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import com.alibaba.fastjson.JSONArray; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.ui.action.IExecutable; +import com.taobao.weex.ui.component.node.WXComponentNode; + +/** + * Created by listen on 18/01/10. + */ +public class PrerenderActionInvokeMethod implements IExecutable { + + private static final String TAG = "PrerenderActionInvokeMethod"; + + private final String mMethod; + private final JSONArray mArgs; + private String mPageId; + private String mRef; + + PrerenderActionInvokeMethod(String pageId, String ref, String method, JSONArray args) { + this.mPageId = pageId; + this.mRef = ref; + this.mMethod = method; + this.mArgs = args; + } + + @Override + public void executeAction() { + WXComponentNode node = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(mPageId, mRef); + if (node != null) { + node.invokeMethod(mMethod, mArgs); + } + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAbstractAddElement.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAbstractAddElement.java new file mode 100644 index 0000000000..0091530c43 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAbstractAddElement.java @@ -0,0 +1,89 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import android.support.annotation.NonNull; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.dom.CSSShorthand; +import com.taobao.weex.ui.action.BasicComponentData; +import com.taobao.weex.ui.action.GraphicActionBaseAddElement; +import com.taobao.weex.ui.component.node.WXComponentNodeBuilder; + +import java.util.Map; +import java.util.Set; + +public abstract class PrerenderGraphicActionAbstractAddElement extends GraphicActionBaseAddElement { + + protected String mComponentType; + protected String mParentRef; + protected int mIndex = -1; + protected Map mStyle; + protected Map mAttributes; + protected Set mEvents; + protected float[] mMargins; + protected float[] mPaddings; + protected float[] mBorders; + private long startTime; + + public PrerenderGraphicActionAbstractAddElement(WXSDKInstance instance, String ref) { + super(instance, ref); + startTime = System.currentTimeMillis(); + } + + protected WXComponentNodeBuilder createNode(WXSDKInstance instance, @NonNull BasicComponentData basicComponentData) { + basicComponentData.addStyle(mStyle); + basicComponentData.addAttr(mAttributes); + basicComponentData.addEvent(mEvents); + basicComponentData.addShorthand(mMargins, CSSShorthand.TYPE.MARGIN); + basicComponentData.addShorthand(mPaddings, CSSShorthand.TYPE.PADDING); + basicComponentData.addShorthand(mBorders, CSSShorthand.TYPE.BORDER); + + return WXComponentNodeBuilder.createInstance(instance, basicComponentData); + } + + @Override + public void executeAction() { + getWXSDKIntance().callActionAddElementTime(System.currentTimeMillis() - startTime); + } + + public String getComponentType() { + return mComponentType; + } + + public String getParentRef() { + return mParentRef; + } + + public int getIndex() { + return mIndex; + } + + public Map getStyle() { + return mStyle; + } + + public Map getAttributes() { + return mAttributes; + } + + public Set getEvents() { + return mEvents; + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAddElement.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAddElement.java new file mode 100644 index 0000000000..4842ac6fdc --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAddElement.java @@ -0,0 +1,180 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import android.support.annotation.NonNull; +import android.support.annotation.RestrictTo; +import android.support.annotation.RestrictTo.Scope; +import android.support.annotation.WorkerThread; +import android.support.v4.util.ArrayMap; +import android.util.Log; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.common.WXErrorCode; +import com.taobao.weex.performance.WXAnalyzerDataTransfer; +import com.taobao.weex.ui.action.BasicComponentData; +import com.taobao.weex.ui.action.GraphicPosition; +import com.taobao.weex.ui.action.GraphicSize; +import com.taobao.weex.ui.component.WXComponent; +import com.taobao.weex.ui.component.node.WXComponentNode; +import com.taobao.weex.utils.WXExceptionUtils; + +import java.util.Arrays; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +public class PrerenderGraphicActionAddElement extends PrerenderGraphicActionAbstractAddElement { + + private GraphicPosition layoutPosition; + private GraphicSize layoutSize; + private boolean isLayoutRTL; + private WXComponentNode childNode; + + PrerenderGraphicActionAddElement(@NonNull WXSDKInstance instance, String ref, + String componentType, String parentRef, + int index, + Map style, + Map attributes, + Set events, + float[] margins, + float[] paddings, + float[] borders) { + super(instance, ref); + this.mComponentType = componentType; + this.mParentRef = parentRef; + this.mIndex = index; + this.mStyle = style; + this.mAttributes = attributes; + this.mEvents = events; + this.mPaddings = paddings; + this.mMargins = margins; + this.mBorders = borders; + + if (WXAnalyzerDataTransfer.isInteractionLogOpen()){ + Log.d(WXAnalyzerDataTransfer.INTERACTION_TAG, "[client][addelementStart]"+instance.getInstanceId()+","+componentType+","+ref); + } + try { + BasicComponentData basicComponentData = new BasicComponentData(ref, mComponentType, + mParentRef); + childNode = createNode(instance, basicComponentData) + .setIndex(mIndex) + .setLayoutPosition(layoutPosition) + .setLayoutSize(layoutSize).build(); + childNode.createComponent(); + + } catch (ClassCastException e) { + Map ext = new ArrayMap<>(); + WXComponent parent = WXSDKManager.getInstance().getWXRenderManager() + .getWXComponent(getPageId(), mParentRef); + + if (parent != null) { + if (mStyle != null && !mStyle.isEmpty()) { + ext.put("child.style", mStyle.toString()); + } + if (parent != null && parent.getStyles() != null && !parent.getStyles().isEmpty()) { + ext.put("parent.style", parent.getStyles().toString()); + } + + if (mAttributes != null && !mAttributes.isEmpty()) { + ext.put("child.attr", mAttributes.toString()); + } + if (parent != null && parent.getAttrs() != null && !parent.getAttrs().isEmpty()) { + ext.put("parent.attr", parent.getAttrs().toString()); + } + + if (mEvents != null && !mEvents.isEmpty()) { + ext.put("child.event", mEvents.toString()); + } + if (parent != null && parent.getEvents() != null && !parent.getEvents().isEmpty()) { + ext.put("parent.event", parent.getEvents().toString()); + } + + if (mMargins != null && mMargins.length > 0) { + ext.put("child.margin", Arrays.toString(mMargins)); + } + if (parent != null && parent.getMargin() != null) { + ext.put("parent.margin", parent.getMargin().toString()); + } + + if (mPaddings != null && mPaddings.length > 0) { + ext.put("child.padding", Arrays.toString(mPaddings)); + } + if (parent != null && parent.getPadding() != null) { + ext.put("parent.padding", parent.getPadding().toString()); + } + + if (mBorders != null && mBorders.length > 0) { + ext.put("child.border", Arrays.toString(mBorders)); + } + if (parent != null && parent.getBorder() != null) { + ext.put("parent.border", parent.getBorder().toString()); + } + + WXExceptionUtils.commitCriticalExceptionRT(instance.getInstanceId(), + WXErrorCode.WX_RENDER_ERR_CONTAINER_TYPE, + "PrerenderGraphicActionAddElement", + String.format(Locale.ENGLISH, "You are trying to add a %s to a %2$s, which is illegal as %2$s is not a container", + componentType, + WXSDKManager.getInstance().getWXRenderManager().getWXComponent(getPageId(), mParentRef).getComponentType()), + ext); + } + } + + } + + @RestrictTo(Scope.LIBRARY) + @WorkerThread + @Override + public void setRTL(boolean isRTL){ + isLayoutRTL = isRTL; + childNode.setIsLayoutRTL(isRTL); + } + + @RestrictTo(Scope.LIBRARY) + @WorkerThread + @Override + public void setSize(GraphicSize graphicSize){ + this.layoutSize = graphicSize; + childNode.setLayoutSize(graphicSize); + } + + @RestrictTo(Scope.LIBRARY) + @WorkerThread + @Override + public void setPosition(GraphicPosition position){ + this.layoutPosition = position; + childNode.setLayoutPosition(position); + } + + @RestrictTo(Scope.LIBRARY) + @WorkerThread + @Override + public void setIndex(int index){ + childNode.setIndex(index); + } + + @Override + public void executeAction() { + super.executeAction(); + childNode.setIsLayoutRTL(isLayoutRTL); + childNode.addElement(); + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAddEvent.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAddEvent.java new file mode 100644 index 0000000000..04d8bdf893 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAddEvent.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.dom.WXEvent; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.component.node.WXComponentNode; + +/** + * Created by listen on 18/01/11. + */ +public class PrerenderGraphicActionAddEvent extends BasicGraphicAction { + + private final String mEvent; + + PrerenderGraphicActionAddEvent(WXSDKInstance instance, String ref, Object event) { + super(instance, ref); + this.mEvent = WXEvent.getEventName(event); + } + + @Override + public void executeAction() { + WXSDKInstance instance = WXSDKManager.getInstance().getWXRenderManager().getWXSDKInstance(getPageId()); + if (instance == null) { + return; + } + + WXComponentNode node = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(getPageId(), getRef()); + if (node != null) { + node.addEvent(mEvent); + } + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAnimation.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAnimation.java new file mode 100644 index 0000000000..bb965c441e --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionAnimation.java @@ -0,0 +1,47 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.ui.action.GraphicActionAnimation; + +public class PrerenderGraphicActionAnimation extends GraphicActionAnimation { + + + PrerenderGraphicActionAnimation(@NonNull WXSDKInstance instance, @NonNull String ref, @Nullable String animation, + @Nullable final String callBack) { + super(instance, ref, animation, callBack); + + } + + @Override + public void executeAction() { + + WXSDKInstance instance = WXSDKManager.getInstance().getWXRenderManager().getWXSDKInstance(getPageId()); + if (instance == null || instance.getNeedInterceptRender()) { + return; + } + + super.executeAction(); + } +} \ No newline at end of file diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionCreateBody.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionCreateBody.java new file mode 100644 index 0000000000..40dd837aaf --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionCreateBody.java @@ -0,0 +1,63 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import android.support.annotation.NonNull; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.ui.action.BasicComponentData; +import com.taobao.weex.ui.component.node.WXComponentNode; + +import java.util.Map; +import java.util.Set; + +public class PrerenderGraphicActionCreateBody extends PrerenderGraphicActionAbstractAddElement { + + private WXComponentNode node; + + PrerenderGraphicActionCreateBody(@NonNull WXSDKInstance instance, String ref, + String componentType, + Map style, + Map attributes, + Set events, + float[] margins, + float[] paddings, + float[] borders) { + super(instance, ref); + this.mComponentType = componentType; + this.mStyle = style; + this.mAttributes = attributes; + this.mEvents = events; + this.mMargins = margins; + this.mPaddings = paddings; + this.mBorders = borders; + + BasicComponentData basicComponentData = new BasicComponentData(getRef(), mComponentType, null); + node = createNode(instance, basicComponentData).build(); + node.createComponent(); + } + + @Override + public void executeAction() { + super.executeAction(); + if (node != null) { + node.createBody(); + } + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionCreateFinish.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionCreateFinish.java new file mode 100644 index 0000000000..084cb2592c --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionCreateFinish.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import android.support.annotation.NonNull; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.performance.WXInstanceApm; +import com.taobao.weex.ui.action.BasicGraphicAction; + +/** + * Created by listen on 18/01/09. + */ +public class PrerenderGraphicActionCreateFinish extends BasicGraphicAction { + + PrerenderGraphicActionCreateFinish(@NonNull WXSDKInstance instance) { + super(instance, ""); + instance.getApmForInstance().onStage(WXInstanceApm.KEY_PAGE_STAGES_CREATE_FINISH); + instance.getApmForInstance().extInfo.put(WXInstanceApm.KEY_PAGE_STAGES_CREATE_FINISH,true); + + } + + @Override + public void executeAction() { + final WXSDKInstance instance = getWXSDKIntance(); + if (instance == null) { + return; + } + + if (instance.getPrerenderContext().rootNode != null) { + instance.getPrerenderContext().rootNode.onCreateFinish(); + } + + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionLayout.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionLayout.java new file mode 100644 index 0000000000..2d97c58142 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionLayout.java @@ -0,0 +1,55 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.action.GraphicPosition; +import com.taobao.weex.ui.action.GraphicSize; +import com.taobao.weex.ui.component.node.WXComponentNode; +import com.taobao.weex.utils.WXLogUtils; + +public class PrerenderGraphicActionLayout extends BasicGraphicAction { + + private final GraphicPosition mLayoutPosition; + private final GraphicSize mLayoutSize; + private final boolean mIsLayoutRTL; + + PrerenderGraphicActionLayout(WXSDKInstance instance, String ref, GraphicPosition layoutPosition, GraphicSize layoutSize, boolean isRTL) { + super(instance, ref); + this.mLayoutPosition = layoutPosition; + this.mLayoutSize = layoutSize; + this.mIsLayoutRTL = isRTL; + } + + @Override + public void executeAction() { + WXSDKInstance instance = WXSDKManager.getInstance().getWXRenderManager().getWXSDKInstance(getPageId()); + if (instance == null) { + WXLogUtils.w(WXLogUtils.WEEX_TAG); + return; + } + + WXComponentNode node = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(getPageId(), getRef()); + if (node != null) { + node.updateLayout(mLayoutPosition, mLayoutSize, mIsLayoutRTL); + } + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionMoveElement.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionMoveElement.java new file mode 100644 index 0000000000..7af99d23d6 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionMoveElement.java @@ -0,0 +1,44 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.component.node.WXComponentNode; + +public class PrerenderGraphicActionMoveElement extends BasicGraphicAction { + + private String mParentref; + private int mIndex; + + PrerenderGraphicActionMoveElement(WXSDKInstance instance, String ref, String parentRef, int index) { + super(instance, ref); + this.mParentref = parentRef; + this.mIndex = index; + } + + @Override + public void executeAction() { + WXComponentNode node = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(getPageId(), getRef()); + if (node != null) { + node.moveElement(mParentref, mIndex); + } + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionRemoveElement.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionRemoveElement.java new file mode 100644 index 0000000000..d08510cef7 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionRemoveElement.java @@ -0,0 +1,39 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.component.node.WXComponentNode; + +public class PrerenderGraphicActionRemoveElement extends BasicGraphicAction { + + PrerenderGraphicActionRemoveElement(WXSDKInstance instance, String ref) { + super(instance, ref); + } + + @Override + public void executeAction() { + WXComponentNode node = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(getPageId(), getRef()); + if (node != null) { + node.removeElement(); + } + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionRemoveEvent.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionRemoveEvent.java new file mode 100644 index 0000000000..828ef6b160 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionRemoveEvent.java @@ -0,0 +1,46 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.dom.WXEvent; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.component.node.WXComponentNode; + +/** + * Created by listen on 18/01/11. + */ +public class PrerenderGraphicActionRemoveEvent extends BasicGraphicAction { + + private final String mEvent; + + PrerenderGraphicActionRemoveEvent(WXSDKInstance instance, String ref, Object event) { + super(instance, ref); + this.mEvent = WXEvent.getEventName(event); + } + + @Override + public void executeAction() { + WXComponentNode node = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(getPageId(), getRef()); + if (node != null) { + node.removeEvent(mEvent); + } + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionRenderSuccess.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionRenderSuccess.java new file mode 100644 index 0000000000..9c361f0a2c --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionRenderSuccess.java @@ -0,0 +1,45 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import android.support.annotation.NonNull; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.component.node.WXComponentNode; + +public class PrerenderGraphicActionRenderSuccess extends BasicGraphicAction { + + PrerenderGraphicActionRenderSuccess(@NonNull WXSDKInstance instance) { + super(instance, ""); + } + + @Override + public void executeAction() { + final WXSDKInstance instance = getWXSDKIntance(); + if (instance == null) { + return; + } + WXComponentNode rootNode = instance.getPrerenderContext().rootNode; + if (rootNode != null) { + rootNode.onRenderSuccess(); + } + } + +} \ No newline at end of file diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionScrollToElement.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionScrollToElement.java new file mode 100644 index 0000000000..8365f4d869 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionScrollToElement.java @@ -0,0 +1,46 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import com.alibaba.fastjson.JSONObject; +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.component.node.WXComponentNode; + +/** + * Created by listen on 18/01/09. + */ +public class PrerenderGraphicActionScrollToElement extends BasicGraphicAction { + + private final JSONObject mOptions; + + PrerenderGraphicActionScrollToElement(WXSDKInstance instance, String ref, JSONObject options) { + super(instance, ref); + this.mOptions = options; + } + + @Override + public void executeAction() { + WXComponentNode node = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(getPageId(), getRef()); + if (node != null) { + node.scrollToElement(mOptions); + } + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionTransformNode.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionTransformNode.java new file mode 100644 index 0000000000..32375ee1d7 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionTransformNode.java @@ -0,0 +1,45 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import android.support.annotation.NonNull; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.component.node.WXComponentNode; + +/** + * Description + * Created by luciolong on 02/08/2018. + */ +public class PrerenderGraphicActionTransformNode extends BasicGraphicAction { + @NonNull + private final WXComponentNode mNode; + + public PrerenderGraphicActionTransformNode(@NonNull WXComponentNode node, WXSDKInstance instance, String ref) { + super(instance, ref); + this.mNode = node; + this.mNode.createComponent(); + } + + @Override + public void executeAction() { + mNode.transformNode(); + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionUpdateAttr.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionUpdateAttr.java new file mode 100644 index 0000000000..7381ade410 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionUpdateAttr.java @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.component.node.WXComponentNode; + +import java.util.Map; + +public class PrerenderGraphicActionUpdateAttr extends BasicGraphicAction { + + private Map mAttrs; + private WXComponentNode mNode; + + PrerenderGraphicActionUpdateAttr(WXSDKInstance instance, String ref, + Map attrs) { + super(instance, ref); + this.mAttrs = attrs; + + mNode = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(getPageId(), getRef()); + if (mNode != null) { + mNode.addAttrs(mAttrs); + } + } + + @Override + public void executeAction() { + if (mNode == null) { + return; + } + mNode.updateAttrs(mAttrs); + } +} diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionUpdateStyle.java b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionUpdateStyle.java new file mode 100644 index 0000000000..089b6097e6 --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/prerenderaction/PrerenderGraphicActionUpdateStyle.java @@ -0,0 +1,155 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.prerenderaction; + +import android.support.v4.util.ArrayMap; + +import com.taobao.weex.WXSDKInstance; +import com.taobao.weex.WXSDKManager; +import com.taobao.weex.bridge.WXBridgeManager; +import com.taobao.weex.common.Constants; +import com.taobao.weex.dom.CSSShorthand; +import com.taobao.weex.dom.transition.WXTransition; +import com.taobao.weex.ui.action.BasicGraphicAction; +import com.taobao.weex.ui.component.WXComponent; +import com.taobao.weex.ui.component.node.WXComponentNode; + +import java.util.Map; + +public class PrerenderGraphicActionUpdateStyle extends BasicGraphicAction { + + private Map mStyle; + private WXComponentNode mNode; + private boolean mIsCausedByPesudo; + private boolean mIsBorderSet; + + PrerenderGraphicActionUpdateStyle(WXSDKInstance instance, String ref, + Map style, + Map paddings, + Map margins, + Map borders) { + this(instance, ref, style, paddings, margins, borders, false); + } + + public PrerenderGraphicActionUpdateStyle(WXSDKInstance instance, String ref, + Map style, + CSSShorthand paddings, + CSSShorthand margins, + CSSShorthand borders, boolean byPesudo) { + super(instance, ref); + this.mStyle = style; + this.mIsCausedByPesudo = byPesudo; + + mNode = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(getPageId(), getRef()); + if (mNode == null) { + return; + } + if (null != mStyle) { + mNode.getComponentData().getStyles().updateStyle(mStyle, mIsCausedByPesudo); + if (!mNode.getWxInstance().getNeedInterceptRender() && mNode.data != null) { + if (style.containsKey(Constants.Name.TRANSFORM) && mNode.data.getTransition() == null) { + Map animationMap = new ArrayMap<>(2); + animationMap.put(Constants.Name.TRANSFORM, style.get(Constants.Name.TRANSFORM)); + animationMap + .put(Constants.Name.TRANSFORM_ORIGIN, style.get(Constants.Name.TRANSFORM_ORIGIN)); + mNode.data.addAnimationForElement(animationMap); + } + } + } + + if (null != paddings) { + mNode.getComponentData().setPaddings(paddings); + } + + if (null != margins) { + mNode.getComponentData().setMargins(margins); + } + + if (null != borders) { + mIsBorderSet = true; + mNode.getComponentData().setBorders(borders); + } + } + + public PrerenderGraphicActionUpdateStyle(WXSDKInstance instance, String ref, + Map style, + Map paddings, + Map margins, + Map borders, boolean byPesudo) { + super(instance, ref); + this.mStyle = style; + this.mIsCausedByPesudo = byPesudo; + + mNode = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(getPageId(), getRef()); + if (mNode == null) { + return; + } + if (null != mStyle) { + mNode.getComponentData().addStyle(mStyle, mIsCausedByPesudo); + if (!mNode.getWxInstance().getNeedInterceptRender() && mNode.data != null) { + if (style.containsKey(Constants.Name.TRANSFORM) && mNode.data.getTransition() == null) { + Map animationMap = new ArrayMap<>(2); + animationMap.put(Constants.Name.TRANSFORM, style.get(Constants.Name.TRANSFORM)); + animationMap.put(Constants.Name.TRANSFORM_ORIGIN, style.get(Constants.Name.TRANSFORM_ORIGIN)); + mNode.data.addAnimationForElement(animationMap); + WXBridgeManager.getInstance().markDirty(getPageId(), getRef(), true); + } + } + } + + if (null != paddings) { + mNode.getComponentData().addShorthand(paddings); + } + + if (null != margins) { + mNode.getComponentData().addShorthand(margins); + } + + if (null != borders) { + mIsBorderSet = true; + mNode.getComponentData().addShorthand(borders); + } + } + + @Override + public void executeAction() { // need merge + mNode = WXSDKManager.getInstance().getWXRenderManager().getWXComponentNode(getPageId(), getRef()); + if (mNode == null) { + return; + } + + if (!mNode.getWxInstance().getNeedInterceptRender() && mNode.data != null) { + WXComponent component = mNode.data; + if (mStyle != null) { + if (component.getTransition() != null) { + component.getTransition().updateTranstionParams(mStyle); + if (component.getTransition().hasTransitionProperty(mStyle)) { + component.getTransition().startTransition(mStyle); + } + } else { + component.setTransition(WXTransition.fromMap(mStyle, component)); + component.updateStyles(mStyle); + } + } else if (mIsBorderSet) { + component.updateStyles(component); + } + } + } +} +