From b9a2065f8a396dd901a44812a44e30931cbc9603 Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Fri, 10 Jan 2020 14:56:51 +0800 Subject: [PATCH] android:custimze Environment --- .../main/java/pub/doric/DoricRegistry.java | 9 + .../java/pub/doric/engine/DoricJSEngine.java | 15 +- doric-js/index.d.ts | 607 +++++++++--------- doric-js/lib/src/runtime/global.d.ts | 3 +- doric-js/src/runtime/global.ts | 3 +- 5 files changed, 329 insertions(+), 308 deletions(-) diff --git a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java index fdf37b73..bfe10294 100644 --- a/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java +++ b/doric-android/doric/src/main/java/pub/doric/DoricRegistry.java @@ -66,6 +66,7 @@ public class DoricRegistry { private static Map bundles = new ConcurrentHashMap<>(); private static Set doricLibraries = new HashSet<>(); private static Set jsLoaders = new HashSet<>(); + private Map extendedEnvValues = new HashMap<>(); static { addJSLoader(new DoricAssetJSLoader()); @@ -154,4 +155,12 @@ public class DoricRegistry { public static Collection getJSLoaders() { return jsLoaders; } + + public void setEnvironmentVariabel(String key, Object val) { + extendedEnvValues.put(key, val); + } + + public Map getEnvironmentVariables() { + return extendedEnvValues; + } } diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java index 1f9e9e70..b8a19d7a 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -30,6 +30,7 @@ import com.github.pengfeizhou.jscore.JavaFunction; import com.github.pengfeizhou.jscore.JavaValue; import java.util.ArrayList; +import java.util.Map; import pub.doric.Doric; import pub.doric.DoricRegistry; @@ -91,14 +92,22 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time } catch (Exception e) { e.printStackTrace(); } - mDoricJSE.injectGlobalJSObject(DoricConstant.INJECT_ENVIRONMENT, new JavaValue(new JSONBuilder() + JSONBuilder envObject = new JSONBuilder() .put("platform", "Android") .put("platformVersion", String.valueOf(android.os.Build.VERSION.SDK_INT)) .put("appName", appName) .put("appVersion", appVersion) .put("screenWidth", DoricUtils.px2dp(DoricUtils.getScreenWidth())) - .put("screenHeight", DoricUtils.px2dp(DoricUtils.getScreenHeight())) - .toJSONObject())); + .put("screenHeight", DoricUtils.px2dp(DoricUtils.getScreenHeight())); + + Map extend = mDoricRegistry.getEnvironmentVariables(); + for (String key : extend.keySet()) { + envObject.put(key, extend.get(key)); + } + + mDoricJSE.injectGlobalJSObject(DoricConstant.INJECT_ENVIRONMENT, + new JavaValue(envObject.toJSONObject())); + mDoricJSE.injectGlobalJSFunction(DoricConstant.INJECT_LOG, new JavaFunction() { @Override public JavaValue exec(JSDecoder[] args) { diff --git a/doric-js/index.d.ts b/doric-js/index.d.ts index 40273f13..2090bfc5 100644 --- a/doric-js/index.d.ts +++ b/doric-js/index.d.ts @@ -28,12 +28,13 @@ declare module 'doric/lib/src/runtime/global' { libVersion: string; screenWidth: number; screenHeight: number; + [index: string]: number | string | boolean | object | undefined; }; function Entry(constructor: { - new (...args: any[]): {}; + new(...args: any[]): {}; }): any; } - export {}; + export { }; } declare module 'doric/lib/src/ui/index.ui' { @@ -90,157 +91,157 @@ declare module 'doric/lib/src/ui/view' { import { IAnimation } from "doric/lib/src/ui/animation"; export function Property(target: Object, propKey: string): void; export interface IView { - width?: number; - height?: number; - backgroundColor?: Color | GradientColor; - corners?: number | { - leftTop?: number; - rightTop?: number; - leftBottom?: number; - rightBottom?: number; - }; - border?: { - width: number; - color: Color; - }; - shadow?: { - color: Color; - opacity: number; - radius: number; - offsetX: number; - offsetY: number; - }; - /** - * float [0,..1] - */ - alpha?: number; - hidden?: boolean; - padding?: { - left?: number; - right?: number; - top?: number; - bottom?: number; - }; - layoutConfig?: LayoutConfig; - onClick?: Function; - identifier?: string; - /**++++++++++transform++++++++++*/ - translationX?: number; - translationY?: number; - scaleX?: number; - scaleY?: number; - /** - * float [0,..1] - */ - pivotX?: number; - /** - * float [0,..1] - */ - pivotY?: number; - /** - * rotation*PI - */ - rotation?: number; + width?: number; + height?: number; + backgroundColor?: Color | GradientColor; + corners?: number | { + leftTop?: number; + rightTop?: number; + leftBottom?: number; + rightBottom?: number; + }; + border?: { + width: number; + color: Color; + }; + shadow?: { + color: Color; + opacity: number; + radius: number; + offsetX: number; + offsetY: number; + }; + /** + * float [0,..1] + */ + alpha?: number; + hidden?: boolean; + padding?: { + left?: number; + right?: number; + top?: number; + bottom?: number; + }; + layoutConfig?: LayoutConfig; + onClick?: Function; + identifier?: string; + /**++++++++++transform++++++++++*/ + translationX?: number; + translationY?: number; + scaleX?: number; + scaleY?: number; + /** + * float [0,..1] + */ + pivotX?: number; + /** + * float [0,..1] + */ + pivotY?: number; + /** + * rotation*PI + */ + rotation?: number; } export type NativeViewModel = { - id: string; - type: string; - props: { - [index: string]: Model; - }; + id: string; + type: string; + props: { + [index: string]: Model; + }; }; export abstract class View implements Modeling, IView { + width: number; + height: number; + x: number; + y: number; + backgroundColor?: Color | GradientColor; + corners?: number | { + leftTop?: number; + rightTop?: number; + leftBottom?: number; + rightBottom?: number; + }; + border?: { width: number; - height: number; + color: Color; + }; + shadow?: { + color: Color; + opacity: number; + radius: number; + offsetX: number; + offsetY: number; + }; + alpha?: number; + hidden?: boolean; + viewId: string; + padding?: { + left?: number; + right?: number; + top?: number; + bottom?: number; + }; + layoutConfig?: LayoutConfig; + onClick?: Function; + superview?: Superview; + callbacks: Map; + constructor(); + /** Anchor start*/ + get left(): number; + set left(v: number); + get right(): number; + set right(v: number); + get top(): number; + set top(v: number); + get bottom(): number; + set bottom(v: number); + get centerX(): number; + get centerY(): number; + set centerX(v: number); + set centerY(v: number); + get dirtyProps(): { + [index: string]: Model; + }; + nativeViewModel: NativeViewModel; + onPropertyChanged(propKey: string, oldV: Model, newV: Model): void; + clean(): void; + isDirty(): boolean; + responseCallback(id: string, ...args: any): any; + toModel(): NativeViewModel; + let(block: (it: this) => void): void; + also(block: (it: this) => void): this; + apply(config: IView): this; + in(group: Group): this; + nativeChannel(context: any, name: string): (args?: any) => Promise; + getWidth(context: BridgeContext): Promise; + getHeight(context: BridgeContext): Promise; + getLocationOnScreen(context: BridgeContext): Promise<{ x: number; y: number; - backgroundColor?: Color | GradientColor; - corners?: number | { - leftTop?: number; - rightTop?: number; - leftBottom?: number; - rightBottom?: number; - }; - border?: { - width: number; - color: Color; - }; - shadow?: { - color: Color; - opacity: number; - radius: number; - offsetX: number; - offsetY: number; - }; - alpha?: number; - hidden?: boolean; - viewId: string; - padding?: { - left?: number; - right?: number; - top?: number; - bottom?: number; - }; - layoutConfig?: LayoutConfig; - onClick?: Function; - superview?: Superview; - callbacks: Map; - constructor(); - /** Anchor start*/ - get left(): number; - set left(v: number); - get right(): number; - set right(v: number); - get top(): number; - set top(v: number); - get bottom(): number; - set bottom(v: number); - get centerX(): number; - get centerY(): number; - set centerX(v: number); - set centerY(v: number); - get dirtyProps(): { - [index: string]: Model; - }; - nativeViewModel: NativeViewModel; - onPropertyChanged(propKey: string, oldV: Model, newV: Model): void; - clean(): void; - isDirty(): boolean; - responseCallback(id: string, ...args: any): any; - toModel(): NativeViewModel; - let(block: (it: this) => void): void; - also(block: (it: this) => void): this; - apply(config: IView): this; - in(group: Group): this; - nativeChannel(context: any, name: string): (args?: any) => Promise; - getWidth(context: BridgeContext): Promise; - getHeight(context: BridgeContext): Promise; - getLocationOnScreen(context: BridgeContext): Promise<{ - x: number; - y: number; - }>; - /**++++++++++transform++++++++++*/ - translationX?: number; - translationY?: number; - scaleX?: number; - scaleY?: number; - pivotX?: number; - pivotY?: number; - rotation?: number; - /**----------transform----------*/ - doAnimation(context: BridgeContext, animation: IAnimation): Promise; + }>; + /**++++++++++transform++++++++++*/ + translationX?: number; + translationY?: number; + scaleX?: number; + scaleY?: number; + pivotX?: number; + pivotY?: number; + rotation?: number; + /**----------transform----------*/ + doAnimation(context: BridgeContext, animation: IAnimation): Promise; } export abstract class Superview extends View { - subviewById(id: string): View | undefined; - abstract allSubviews(): Iterable; - isDirty(): boolean; - clean(): void; - toModel(): NativeViewModel; + subviewById(id: string): View | undefined; + abstract allSubviews(): Iterable; + isDirty(): boolean; + clean(): void; + toModel(): NativeViewModel; } export abstract class Group extends Superview { - readonly children: View[]; - allSubviews(): View[]; - addChild(view: View): void; + readonly children: View[]; + allSubviews(): View[]; + addChild(view: View): void; } } @@ -269,124 +270,124 @@ declare module 'doric/lib/src/ui/animation' { import { Modeling, Model } from "doric/lib/src/util/types"; export type AnimatedKey = "translationX" | "translationY" | "scaleX" | "scaleY" | "rotation" | "pivotX" | "pivotY"; export enum RepeatMode { - RESTART = 1, - REVERSE = 2 + RESTART = 1, + REVERSE = 2 } export interface IAnimation extends Modeling { - duration: number; - delay?: number; + duration: number; + delay?: number; } export interface Changeable { - fromValue: number; - toValue: number; - key: AnimatedKey; - repeatCount?: number; - repeatMode?: RepeatMode; + fromValue: number; + toValue: number; + key: AnimatedKey; + repeatCount?: number; + repeatMode?: RepeatMode; } export enum FillMode { - /** - * The receiver is removed from the presentation when the animation is completed. - */ - Removed = 0, - /** - * The receiver remains visible in its final state when the animation is completed. - */ - Forward = 1, - /** - * The receiver clamps values before zero to zero when the animation is completed. - */ - Backward = 2, - /** - * The receiver clamps values at both ends of the object’s time space - */ - Both = 3 + /** + * The receiver is removed from the presentation when the animation is completed. + */ + Removed = 0, + /** + * The receiver remains visible in its final state when the animation is completed. + */ + Forward = 1, + /** + * The receiver clamps values before zero to zero when the animation is completed. + */ + Backward = 2, + /** + * The receiver clamps values at both ends of the object’s time space + */ + Both = 3 } export enum TimingFunction { - /** - * The system default timing function. Use this function to ensure that the timing of your animations matches that of most system animations. - */ - Default = 0, - /** - * Linear pacing, which causes an animation to occur evenly over its duration. - */ - Linear = 1, - /** - * Ease-in pacing, which causes an animation to begin slowly and then speed up as it progresses. - */ - EaseIn = 2, - /** - * Ease-out pacing, which causes an animation to begin quickly and then slow as it progresses. - */ - EaseOut = 3, - /** - * Ease-in-ease-out pacing, which causes an animation to begin slowly, accelerate through the middle of its duration, and then slow again before completing. - */ - EaseInEaseOut = 4 + /** + * The system default timing function. Use this function to ensure that the timing of your animations matches that of most system animations. + */ + Default = 0, + /** + * Linear pacing, which causes an animation to occur evenly over its duration. + */ + Linear = 1, + /** + * Ease-in pacing, which causes an animation to begin slowly and then speed up as it progresses. + */ + EaseIn = 2, + /** + * Ease-out pacing, which causes an animation to begin quickly and then slow as it progresses. + */ + EaseOut = 3, + /** + * Ease-in-ease-out pacing, which causes an animation to begin slowly, accelerate through the middle of its duration, and then slow again before completing. + */ + EaseInEaseOut = 4 } abstract class Animation implements IAnimation { - changeables: Map; + changeables: Map; + duration: number; + repeatCount?: number; + repeatMode?: RepeatMode; + delay?: number; + fillMode: FillMode; + timingFunction?: TimingFunction; + toModel(): { + type: string; + delay: number | undefined; duration: number; - repeatCount?: number; - repeatMode?: RepeatMode; - delay?: number; + changeables: { + key: AnimatedKey; + fromValue: number; + toValue: number; + }[]; + repeatCount: number | undefined; + repeatMode: RepeatMode | undefined; fillMode: FillMode; - timingFunction?: TimingFunction; - toModel(): { - type: string; - delay: number | undefined; - duration: number; - changeables: { - key: AnimatedKey; - fromValue: number; - toValue: number; - }[]; - repeatCount: number | undefined; - repeatMode: RepeatMode | undefined; - fillMode: FillMode; - timingFunction: TimingFunction | undefined; - }; + timingFunction: TimingFunction | undefined; + }; } export class ScaleAnimation extends Animation { - constructor(); - set fromScaleX(v: number); - get fromScaleX(): number; - set toScaleX(v: number); - get toScaleX(): number; - set fromScaleY(v: number); - get fromScaleY(): number; - set toScaleY(v: number); - get toScaleY(): number; + constructor(); + set fromScaleX(v: number); + get fromScaleX(): number; + set toScaleX(v: number); + get toScaleX(): number; + set fromScaleY(v: number); + get fromScaleY(): number; + set toScaleY(v: number); + get toScaleY(): number; } export class TranslationAnimation extends Animation { - constructor(); - set fromTranslationX(v: number); - get fromTranslationX(): number; - set toTranslationX(v: number); - get toTranslationX(): number; - set fromTranslationY(v: number); - get fromTranslationY(): number; - set toTranslationY(v: number); - get toTranslationY(): number; + constructor(); + set fromTranslationX(v: number); + get fromTranslationX(): number; + set toTranslationX(v: number); + get toTranslationX(): number; + set fromTranslationY(v: number); + get fromTranslationY(): number; + set toTranslationY(v: number); + get toTranslationY(): number; } export class RotationAnimation extends Animation { - constructor(); - set fromRotation(v: number); - get fromRotation(): number; - set toRotation(v: number); - get toRotation(): number; + constructor(); + set fromRotation(v: number); + get fromRotation(): number; + set toRotation(v: number); + get toRotation(): number; } export class AnimationSet implements IAnimation { - _duration: number; - delay?: number; - addAnimation(anim: IAnimation): void; - get duration(): number; - set duration(v: number); - toModel(): { - animations: Model; - delay: number | undefined; - }; + _duration: number; + delay?: number; + addAnimation(anim: IAnimation): void; + get duration(): number; + set duration(v: number); + toModel(): { + animations: Model; + delay: number | undefined; + }; } - export {}; + export { }; } declare module 'doric/lib/src/widget/layouts' { @@ -417,7 +418,7 @@ declare module 'doric/lib/src/widget/layouts' { export function stack(views: View[], config?: IStack): Stack; export function hlayout(views: View[], config?: IHLayout): HLayout; export function vlayout(views: View[], config?: IVLayout): VLayout; - export {}; + export { }; } declare module 'doric/lib/src/widget/text' { @@ -894,67 +895,67 @@ declare module 'doric/lib/src/util/layoutconfig' { import { Gravity } from "doric/lib/src/util/gravity"; import { Modeling } from "doric/lib/src/util/types"; export enum LayoutSpec { - /** - * Depends on what's been set on width or height. - */ - JUST = 0, - /** - * Depends on it's content. - */ - FIT = 1, - /** - * Extend as much as parent let it take. - */ - MOST = 2 + /** + * Depends on what's been set on width or height. + */ + JUST = 0, + /** + * Depends on it's content. + */ + FIT = 1, + /** + * Extend as much as parent let it take. + */ + MOST = 2 } export interface LayoutConfig { - widthSpec?: LayoutSpec; - heightSpec?: LayoutSpec; - margin?: { - left?: number; - right?: number; - top?: number; - bottom?: number; - }; - alignment?: Gravity; - weight?: number; + widthSpec?: LayoutSpec; + heightSpec?: LayoutSpec; + margin?: { + left?: number; + right?: number; + top?: number; + bottom?: number; + }; + alignment?: Gravity; + weight?: number; } export class LayoutConfigImpl implements LayoutConfig, Modeling { - widthSpec?: LayoutSpec; - heightSpec?: LayoutSpec; - margin?: { - left?: number; - right?: number; - top?: number; - bottom?: number; - }; - alignment?: Gravity; - weight?: number; - fit(): this; - most(): this; - just(): this; - configWidth(w: LayoutSpec): this; - configHeight(h: LayoutSpec): this; - configMargin(m: { - left?: number; - right?: number; - top?: number; - bottom?: number; - }): this; - configAlignment(a: Gravity): this; - configWeight(w: number): this; - toModel(): { - widthSpec: LayoutSpec | undefined; - heightSpec: LayoutSpec | undefined; - margin: { - left?: number | undefined; - right?: number | undefined; - top?: number | undefined; - bottom?: number | undefined; - } | undefined; - alignment: number | undefined; - weight: number | undefined; - }; + widthSpec?: LayoutSpec; + heightSpec?: LayoutSpec; + margin?: { + left?: number; + right?: number; + top?: number; + bottom?: number; + }; + alignment?: Gravity; + weight?: number; + fit(): this; + most(): this; + just(): this; + configWidth(w: LayoutSpec): this; + configHeight(h: LayoutSpec): this; + configMargin(m: { + left?: number; + right?: number; + top?: number; + bottom?: number; + }): this; + configAlignment(a: Gravity): this; + configWeight(w: number): this; + toModel(): { + widthSpec: LayoutSpec | undefined; + heightSpec: LayoutSpec | undefined; + margin: { + left?: number | undefined; + right?: number | undefined; + top?: number | undefined; + bottom?: number | undefined; + } | undefined; + alignment: number | undefined; + weight: number | undefined; + }; } export function layoutConfig(): LayoutConfigImpl; } @@ -981,7 +982,7 @@ declare module 'doric/lib/src/util/types' { bind(binder: Binder): void; static of(v: E): Mutable; } - export {}; + export { }; } declare module 'doric/lib/src/util/uniqueId' { @@ -1009,7 +1010,7 @@ declare module 'doric/lib/src/pattern/provider' { } export class Observable implements IObservable { constructor(provider: IProvider, clz: { - new (...args: any[]): M; + new(...args: any[]): M; }); addObserver(observer: Observer): void; removeObserver(observer: Observer): void; @@ -1018,20 +1019,20 @@ declare module 'doric/lib/src/pattern/provider' { export interface IProvider { provide(obj: Object): void; acquire(clz: { - new (...args: any[]): T; + new(...args: any[]): T; }): T | undefined; remove(clz: { - new (...args: any[]): T; + new(...args: any[]): T; }): void; clear(): void; observe(clz: { - new (...args: any[]): T; + new(...args: any[]): T; }): Observable; } export class Provider implements IProvider { provide(obj: Object): void; acquire(clz: { - new (...args: any[]): T; + new(...args: any[]): T; }): T | undefined; remove(clz: new (...args: any[]) => T): void; clear(): void; diff --git a/doric-js/lib/src/runtime/global.d.ts b/doric-js/lib/src/runtime/global.d.ts index b1838796..98dfacb8 100644 --- a/doric-js/lib/src/runtime/global.d.ts +++ b/doric-js/lib/src/runtime/global.d.ts @@ -7,13 +7,14 @@ export declare type BridgeContext = { declare global { const context: BridgeContext; const Environment: { - platform: "Android" | "iOS" | "Qt" | "h5"; + platform: "Android" | "iOS" | "Qt" | "web"; platformVersion: string; appName: string; appVersion: string; libVersion: string; screenWidth: number; screenHeight: number; + [index: string]: number | string | boolean | object | undefined; }; function Entry(constructor: { new (...args: any[]): {}; diff --git a/doric-js/src/runtime/global.ts b/doric-js/src/runtime/global.ts index cddeccb8..f50ee6ec 100644 --- a/doric-js/src/runtime/global.ts +++ b/doric-js/src/runtime/global.ts @@ -22,7 +22,7 @@ export type BridgeContext = { declare global { const context: BridgeContext const Environment: { - platform: "Android" | "iOS" | "Qt" | "h5", + platform: "Android" | "iOS" | "Qt" | "web", platformVersion: string, @@ -36,6 +36,7 @@ declare global { screenHeight: number, + [index: string]: number | string | boolean | object | undefined } function Entry(constructor: { new(...args: any[]): {} }): any }