diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java index 2927885f..1a5d7274 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricContext.java @@ -3,6 +3,7 @@ package com.github.penfeizhou.doric; import android.content.Context; import com.github.penfeizhou.doric.async.AsyncResult; +import com.github.penfeizhou.doric.extension.render.DoricShader; import com.github.penfeizhou.doric.plugin.DoricJavaPlugin; import com.github.penfeizhou.doric.utils.DoricMetaInfo; import com.github.pengfeizhou.jscore.JSDecoder; @@ -19,6 +20,7 @@ public class DoricContext { private final String mContextId; private final Map mPluginMap = new HashMap<>(); private final Context mContext; + private DoricShader doricShader = new DoricShader(getDriver().getRegistry()); DoricContext(Context context, String contextId) { this.mContext = context; diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricDriver.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricDriver.java index 07141df2..9e6053d5 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricDriver.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricDriver.java @@ -113,4 +113,9 @@ public class DoricDriver implements IDoricDriver { } }); } + + @Override + public DoricRegistry getRegistry() { + return doricJSEngine.getRegistry(); + } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricRegistry.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricRegistry.java index 9d33ca7b..ec8905d9 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricRegistry.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/DoricRegistry.java @@ -2,7 +2,7 @@ package com.github.penfeizhou.doric; import android.text.TextUtils; -import com.github.penfeizhou.doric.render.ViewNode; +import com.github.penfeizhou.doric.extension.render.ViewNode; import com.github.penfeizhou.doric.utils.DoricMetaInfo; import com.github.penfeizhou.doric.plugin.DoricJavaPlugin; import com.github.penfeizhou.doric.plugin.ModalPlugin; diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java index 008daf8d..d026fb57 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/IDoricDriver.java @@ -22,4 +22,6 @@ public interface IDoricDriver { AsyncResult createContext(final String contextId, final String script, final String source); AsyncResult destroyContext(final String contextId); + + DoricRegistry getRegistry(); } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java index 6e4c9706..c6193bed 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/engine/DoricJSEngine.java @@ -25,10 +25,10 @@ import java.util.ArrayList; */ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback { private final Handler mJSHandler; - private final DoricBridgeExtension mDoricBridgeExtension; + private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension(); private IDoricJSE mDoricJSE; - private DoricTimerExtension mTimerExtension; - private DoricRegistry mDoricRegistry = new DoricRegistry(); + private final DoricTimerExtension mTimerExtension; + private final DoricRegistry mDoricRegistry = new DoricRegistry(); public DoricJSEngine() { HandlerThread handlerThread = new HandlerThread(this.getClass().getSimpleName()); @@ -43,7 +43,6 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time } }); mTimerExtension = new DoricTimerExtension(looper, this); - mDoricBridgeExtension = new DoricBridgeExtension(mDoricRegistry); } public Handler getJSHandler() { @@ -192,4 +191,8 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time DoricLog.e("Timer Callback error:%s", e.getLocalizedMessage()); } } + + public DoricRegistry getRegistry() { + return mDoricRegistry; + } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricBridgeExtension.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricBridgeExtension.java index b8bea144..3c1a90d3 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricBridgeExtension.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/bridge/DoricBridgeExtension.java @@ -21,15 +21,12 @@ import java.util.concurrent.Callable; */ public class DoricBridgeExtension { - private final DoricRegistry mRegistry; - - public DoricBridgeExtension(DoricRegistry doricRegistry) { - mRegistry = doricRegistry; + public DoricBridgeExtension() { } public JavaValue callNative(String contextId, String module, String methodName, final String callbackId, final JSDecoder jsDecoder) { final DoricContext context = DoricContextManager.getContext(contextId); - DoricMetaInfo pluginInfo = mRegistry.acquirePluginInfo(module); + DoricMetaInfo pluginInfo = context.getDriver().getRegistry().acquirePluginInfo(module); if (pluginInfo == null) { DoricLog.e("Cannot find plugin class:%s", module); return new JavaValue(false); diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/render/DoricNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/DoricNode.java similarity index 88% rename from Android/doric/src/main/java/com/github/penfeizhou/doric/render/DoricNode.java rename to Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/DoricNode.java index aead141a..4e119a56 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/render/DoricNode.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/DoricNode.java @@ -1,4 +1,4 @@ -package com.github.penfeizhou.doric.render; +package com.github.penfeizhou.doric.extension.render; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/DoricShader.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/DoricShader.java new file mode 100644 index 00000000..bcd271d2 --- /dev/null +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/DoricShader.java @@ -0,0 +1,17 @@ +package com.github.penfeizhou.doric.extension.render; + +import com.github.penfeizhou.doric.DoricRegistry; + +/** + * @Description: com.github.penfeizhou.doric.render + * @Author: pengfei.zhou + * @CreateDate: 2019-07-20 + */ +public class DoricShader { + + private final DoricRegistry mRegistry; + + public DoricShader(DoricRegistry doricRegistry) { + mRegistry = doricRegistry; + } +} diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/ViewNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/ViewNode.java new file mode 100644 index 00000000..2dead1a9 --- /dev/null +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/extension/render/ViewNode.java @@ -0,0 +1,44 @@ +package com.github.penfeizhou.doric.extension.render; + +import android.content.Context; +import android.view.View; +import android.view.ViewGroup; + +import com.github.penfeizhou.doric.DoricContext; +import com.github.penfeizhou.doric.utils.DoricComponent; +import com.github.penfeizhou.doric.utils.DoricUtils; +import com.github.pengfeizhou.jscore.JSObject; + +/** + * @Description: Render + * @Author: pengfei.zhou + * @CreateDate: 2019-07-20 + */ +public abstract class ViewNode extends DoricComponent { + public ViewNode(DoricContext doricContext) { + super(doricContext); + } + + public abstract T build(JSObject jsObject); + + public Context getContext() { + return getDoricContext().getContext(); + } + + public void config(T view, JSObject jsObject) { + setFrame(view.getLayoutParams(), jsObject); + } + + public void setFrame(ViewGroup.LayoutParams layoutParams, JSObject jsObject) { + float width = jsObject.getProperty("width").asNumber().toFloat(); + float height = jsObject.getProperty("height").asNumber().toFloat(); + layoutParams.width = DoricUtils.dp2px(width); + layoutParams.height = DoricUtils.dp2px(height); + if (layoutParams instanceof ViewGroup.MarginLayoutParams) { + float x = jsObject.getProperty("x").asNumber().toFloat(); + float y = jsObject.getProperty("y").asNumber().toFloat(); + ((ViewGroup.MarginLayoutParams) layoutParams).leftMargin = DoricUtils.dp2px(x); + ((ViewGroup.MarginLayoutParams) layoutParams).topMargin = DoricUtils.dp2px(y); + } + } +} diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/render/IShader.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/render/IShader.java deleted file mode 100644 index 5bde34c6..00000000 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/render/IShader.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.github.penfeizhou.doric.render; - -import com.github.pengfeizhou.jscore.JSValue; - -/** - * @Description: com.github.penfeizhou.doric.render - * @Author: pengfei.zhou - * @CreateDate: 2019-07-20 - */ -public interface IShader { - ViewNode create(String type); - - boolean blend(ViewNode node, String propertyName, JSValue property); -} diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/render/ViewNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/render/ViewNode.java deleted file mode 100644 index b18f901d..00000000 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/render/ViewNode.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.github.penfeizhou.doric.render; - -import com.github.penfeizhou.doric.DoricContext; -import com.github.penfeizhou.doric.utils.DoricComponent; - -/** - * @Description: Render - * @Author: pengfei.zhou - * @CreateDate: 2019-07-20 - */ -public abstract class ViewNode extends DoricComponent { - public ViewNode(DoricContext doricContext) { - super(doricContext); - } -} diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/utils/DoricUtils.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/utils/DoricUtils.java index 3aee16c3..7970d3b8 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/utils/DoricUtils.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/utils/DoricUtils.java @@ -1,7 +1,11 @@ package com.github.penfeizhou.doric.utils; +import android.content.Context; import android.content.res.AssetManager; import android.support.annotation.NonNull; +import android.util.DisplayMetrics; +import android.view.Display; +import android.view.WindowManager; import com.github.penfeizhou.doric.Doric; import com.github.pengfeizhou.jscore.JSArray; @@ -14,6 +18,7 @@ import org.json.JSONObject; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Array; +import java.lang.reflect.Field; /** * @Description: Doric @@ -107,4 +112,96 @@ public class DoricUtils { } return null; } + + private static int sScreenWidthPixels; + private static int sScreenHeightPixels; + + public static int getScreenWidth(Context context) { + if (context == null) { + context = Doric.application(); + } + if (sScreenWidthPixels > 0) { + return sScreenWidthPixels; + } + DisplayMetrics dm = new DisplayMetrics(); + WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + + Display display = manager.getDefaultDisplay(); + if (display != null) { + display.getMetrics(dm); + sScreenWidthPixels = dm.widthPixels; + sScreenHeightPixels = dm.heightPixels; + } + return sScreenWidthPixels; + } + + public static int getScreenWidth() { + return getScreenWidth(null); + } + + public static int getScreenHeight(Context context) { + if (context == null) { + context = Doric.application(); + } + if (sScreenHeightPixels > 0) { + return sScreenHeightPixels; + } + DisplayMetrics dm = new DisplayMetrics(); + WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + + Display display = manager.getDefaultDisplay(); + if (display != null) { + display.getMetrics(dm); + sScreenWidthPixels = dm.widthPixels; + sScreenHeightPixels = dm.heightPixels; + } + return sScreenHeightPixels; + } + + public static int getScreenHeight() { + return getScreenHeight(null); + } + + public static float px2dp(int pxValue) { + return px2dp(null, pxValue); + } + + public static float px2dp(Context context, int pxValue) { + if (context == null) { + context = Doric.application(); + } + final float scale = context.getResources().getDisplayMetrics().density; + return pxValue / scale; + } + + public static int dp2px(float dpValue) { + return dp2px(null, dpValue); + } + + public static int dp2px(Context context, float dipValue) { + if (context == null) { + context = Doric.application(); + } + final float scale = context.getResources().getDisplayMetrics().density; + return (int) (dipValue * scale + (dipValue > 0 ? 0.5f : -0.5f)); + } + + + public static int getStatusBarHeight(Context context) { + Class c = null; + Object obj = null; + Field field = null; + int x = 0, sbar = 0; + try { + c = Class.forName("com.android.internal.R$dimen"); + obj = c.newInstance(); + field = c.getField("status_bar_height"); + x = Integer.parseInt(field.get(obj).toString()); + sbar = context.getResources().getDimensionPixelSize(x); + } catch (Exception E) { + E.printStackTrace(); + } + return sbar; + } + } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/GroupNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/GroupNode.java new file mode 100644 index 00000000..786850b2 --- /dev/null +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/GroupNode.java @@ -0,0 +1,27 @@ +package com.github.penfeizhou.doric.widget; + +import android.view.ViewGroup; + +import com.github.penfeizhou.doric.DoricContext; +import com.github.penfeizhou.doric.extension.render.ViewNode; +import com.github.pengfeizhou.jscore.JSArray; +import com.github.pengfeizhou.jscore.JSObject; + +/** + * @Description: com.github.penfeizhou.doric.widget + * @Author: pengfei.zhou + * @CreateDate: 2019-07-20 + */ +public abstract class GroupNode extends ViewNode { + public GroupNode(DoricContext doricContext) { + super(doricContext); + } + + @Override + public void config(ViewGroup view, JSObject jsObject) { + super.config(view, jsObject); + JSArray jsArray = jsObject.getProperty("children").asArray(); + for (int i = 0; i < jsArray.size(); i++) { + } + } +} diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/ImageNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/ImageNode.java index 321f878f..2a625cdc 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/ImageNode.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/ImageNode.java @@ -1,8 +1,11 @@ package com.github.penfeizhou.doric.widget; +import android.widget.ImageView; + import com.github.penfeizhou.doric.DoricContext; -import com.github.penfeizhou.doric.render.DoricNode; -import com.github.penfeizhou.doric.render.ViewNode; +import com.github.penfeizhou.doric.extension.render.DoricNode; +import com.github.penfeizhou.doric.extension.render.ViewNode; +import com.github.pengfeizhou.jscore.JSObject; /** * @Description: com.github.penfeizhou.doric.widget @@ -10,8 +13,13 @@ import com.github.penfeizhou.doric.render.ViewNode; * @CreateDate: 2019-07-20 */ @DoricNode(name = "Image") -public class ImageNode extends ViewNode { +public class ImageNode extends ViewNode { public ImageNode(DoricContext doricContext) { super(doricContext); } + + @Override + public ImageView build(JSObject jsObject) { + return null; + } } diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/StackNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/StackNode.java new file mode 100644 index 00000000..f281ce9c --- /dev/null +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/StackNode.java @@ -0,0 +1,22 @@ +package com.github.penfeizhou.doric.widget; + +import android.view.ViewGroup; + +import com.github.penfeizhou.doric.DoricContext; +import com.github.pengfeizhou.jscore.JSObject; + +/** + * @Description: com.github.penfeizhou.doric.widget + * @Author: pengfei.zhou + * @CreateDate: 2019-07-20 + */ +public class StackNode extends GroupNode { + public StackNode(DoricContext doricContext) { + super(doricContext); + } + + @Override + public ViewGroup build(JSObject jsObject) { + return null; + } +} diff --git a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/TextNode.java b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/TextNode.java index 485cb161..01116050 100644 --- a/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/TextNode.java +++ b/Android/doric/src/main/java/com/github/penfeizhou/doric/widget/TextNode.java @@ -1,8 +1,11 @@ package com.github.penfeizhou.doric.widget; +import android.widget.TextView; + import com.github.penfeizhou.doric.DoricContext; -import com.github.penfeizhou.doric.render.DoricNode; -import com.github.penfeizhou.doric.render.ViewNode; +import com.github.penfeizhou.doric.extension.render.DoricNode; +import com.github.penfeizhou.doric.extension.render.ViewNode; +import com.github.pengfeizhou.jscore.JSObject; /** * @Description: widget @@ -10,8 +13,13 @@ import com.github.penfeizhou.doric.render.ViewNode; * @CreateDate: 2019-07-20 */ @DoricNode(name = "Text") -public class TextNode extends ViewNode { +public class TextNode extends ViewNode { public TextNode(DoricContext doricContext) { super(doricContext); } + + @Override + public TextView build(JSObject jsObject) { + return new TextView(getContext()); + } }