refact repostry holding

This commit is contained in:
pengfei.zhou 2019-07-20 15:02:37 +08:00
parent 920053c0f5
commit e35ab68014
16 changed files with 249 additions and 46 deletions

View File

@ -3,6 +3,7 @@ package com.github.penfeizhou.doric;
import android.content.Context; import android.content.Context;
import com.github.penfeizhou.doric.async.AsyncResult; 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.plugin.DoricJavaPlugin;
import com.github.penfeizhou.doric.utils.DoricMetaInfo; import com.github.penfeizhou.doric.utils.DoricMetaInfo;
import com.github.pengfeizhou.jscore.JSDecoder; import com.github.pengfeizhou.jscore.JSDecoder;
@ -19,6 +20,7 @@ public class DoricContext {
private final String mContextId; private final String mContextId;
private final Map<String, DoricJavaPlugin> mPluginMap = new HashMap<>(); private final Map<String, DoricJavaPlugin> mPluginMap = new HashMap<>();
private final Context mContext; private final Context mContext;
private DoricShader doricShader = new DoricShader(getDriver().getRegistry());
DoricContext(Context context, String contextId) { DoricContext(Context context, String contextId) {
this.mContext = context; this.mContext = context;

View File

@ -113,4 +113,9 @@ public class DoricDriver implements IDoricDriver {
} }
}); });
} }
@Override
public DoricRegistry getRegistry() {
return doricJSEngine.getRegistry();
}
} }

View File

@ -2,7 +2,7 @@ package com.github.penfeizhou.doric;
import android.text.TextUtils; 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.utils.DoricMetaInfo;
import com.github.penfeizhou.doric.plugin.DoricJavaPlugin; import com.github.penfeizhou.doric.plugin.DoricJavaPlugin;
import com.github.penfeizhou.doric.plugin.ModalPlugin; import com.github.penfeizhou.doric.plugin.ModalPlugin;

View File

@ -22,4 +22,6 @@ public interface IDoricDriver {
AsyncResult<Boolean> createContext(final String contextId, final String script, final String source); AsyncResult<Boolean> createContext(final String contextId, final String script, final String source);
AsyncResult<Boolean> destroyContext(final String contextId); AsyncResult<Boolean> destroyContext(final String contextId);
DoricRegistry getRegistry();
} }

View File

@ -25,10 +25,10 @@ import java.util.ArrayList;
*/ */
public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback { public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.TimerCallback {
private final Handler mJSHandler; private final Handler mJSHandler;
private final DoricBridgeExtension mDoricBridgeExtension; private final DoricBridgeExtension mDoricBridgeExtension = new DoricBridgeExtension();
private IDoricJSE mDoricJSE; private IDoricJSE mDoricJSE;
private DoricTimerExtension mTimerExtension; private final DoricTimerExtension mTimerExtension;
private DoricRegistry mDoricRegistry = new DoricRegistry(); private final DoricRegistry mDoricRegistry = new DoricRegistry();
public DoricJSEngine() { public DoricJSEngine() {
HandlerThread handlerThread = new HandlerThread(this.getClass().getSimpleName()); HandlerThread handlerThread = new HandlerThread(this.getClass().getSimpleName());
@ -43,7 +43,6 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
} }
}); });
mTimerExtension = new DoricTimerExtension(looper, this); mTimerExtension = new DoricTimerExtension(looper, this);
mDoricBridgeExtension = new DoricBridgeExtension(mDoricRegistry);
} }
public Handler getJSHandler() { public Handler getJSHandler() {
@ -192,4 +191,8 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
DoricLog.e("Timer Callback error:%s", e.getLocalizedMessage()); DoricLog.e("Timer Callback error:%s", e.getLocalizedMessage());
} }
} }
public DoricRegistry getRegistry() {
return mDoricRegistry;
}
} }

View File

@ -21,15 +21,12 @@ import java.util.concurrent.Callable;
*/ */
public class DoricBridgeExtension { public class DoricBridgeExtension {
private final DoricRegistry mRegistry; public DoricBridgeExtension() {
public DoricBridgeExtension(DoricRegistry doricRegistry) {
mRegistry = doricRegistry;
} }
public JavaValue callNative(String contextId, String module, String methodName, final String callbackId, final JSDecoder jsDecoder) { public JavaValue callNative(String contextId, String module, String methodName, final String callbackId, final JSDecoder jsDecoder) {
final DoricContext context = DoricContextManager.getContext(contextId); final DoricContext context = DoricContextManager.getContext(contextId);
DoricMetaInfo<DoricJavaPlugin> pluginInfo = mRegistry.acquirePluginInfo(module); DoricMetaInfo<DoricJavaPlugin> pluginInfo = context.getDriver().getRegistry().acquirePluginInfo(module);
if (pluginInfo == null) { if (pluginInfo == null) {
DoricLog.e("Cannot find plugin class:%s", module); DoricLog.e("Cannot find plugin class:%s", module);
return new JavaValue(false); return new JavaValue(false);

View File

@ -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.Documented;
import java.lang.annotation.ElementType; import java.lang.annotation.ElementType;

View File

@ -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;
}
}

View File

@ -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<T extends View> 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);
}
}
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -1,7 +1,11 @@
package com.github.penfeizhou.doric.utils; package com.github.penfeizhou.doric.utils;
import android.content.Context;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.support.annotation.NonNull; 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.penfeizhou.doric.Doric;
import com.github.pengfeizhou.jscore.JSArray; import com.github.pengfeizhou.jscore.JSArray;
@ -14,6 +18,7 @@ import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field;
/** /**
* @Description: Doric * @Description: Doric
@ -107,4 +112,96 @@ public class DoricUtils {
} }
return null; 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;
}
} }

View File

@ -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<ViewGroup> {
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++) {
}
}
}

View File

@ -1,8 +1,11 @@
package com.github.penfeizhou.doric.widget; package com.github.penfeizhou.doric.widget;
import android.widget.ImageView;
import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.render.DoricNode; import com.github.penfeizhou.doric.extension.render.DoricNode;
import com.github.penfeizhou.doric.render.ViewNode; import com.github.penfeizhou.doric.extension.render.ViewNode;
import com.github.pengfeizhou.jscore.JSObject;
/** /**
* @Description: com.github.penfeizhou.doric.widget * @Description: com.github.penfeizhou.doric.widget
@ -10,8 +13,13 @@ import com.github.penfeizhou.doric.render.ViewNode;
* @CreateDate: 2019-07-20 * @CreateDate: 2019-07-20
*/ */
@DoricNode(name = "Image") @DoricNode(name = "Image")
public class ImageNode extends ViewNode { public class ImageNode extends ViewNode <ImageView>{
public ImageNode(DoricContext doricContext) { public ImageNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
} }
@Override
public ImageView build(JSObject jsObject) {
return null;
}
} }

View File

@ -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;
}
}

View File

@ -1,8 +1,11 @@
package com.github.penfeizhou.doric.widget; package com.github.penfeizhou.doric.widget;
import android.widget.TextView;
import com.github.penfeizhou.doric.DoricContext; import com.github.penfeizhou.doric.DoricContext;
import com.github.penfeizhou.doric.render.DoricNode; import com.github.penfeizhou.doric.extension.render.DoricNode;
import com.github.penfeizhou.doric.render.ViewNode; import com.github.penfeizhou.doric.extension.render.ViewNode;
import com.github.pengfeizhou.jscore.JSObject;
/** /**
* @Description: widget * @Description: widget
@ -10,8 +13,13 @@ import com.github.penfeizhou.doric.render.ViewNode;
* @CreateDate: 2019-07-20 * @CreateDate: 2019-07-20
*/ */
@DoricNode(name = "Text") @DoricNode(name = "Text")
public class TextNode extends ViewNode { public class TextNode extends ViewNode<TextView> {
public TextNode(DoricContext doricContext) { public TextNode(DoricContext doricContext) {
super(doricContext); super(doricContext);
} }
@Override
public TextView build(JSObject jsObject) {
return new TextView(getContext());
}
} }