android:custimze Environment

This commit is contained in:
pengfei.zhou 2020-01-10 14:56:51 +08:00 committed by osborn
parent ade6d63479
commit b9a2065f8a
5 changed files with 329 additions and 308 deletions

View File

@ -66,6 +66,7 @@ public class DoricRegistry {
private static Map<String, String> bundles = new ConcurrentHashMap<>(); private static Map<String, String> bundles = new ConcurrentHashMap<>();
private static Set<DoricLibrary> doricLibraries = new HashSet<>(); private static Set<DoricLibrary> doricLibraries = new HashSet<>();
private static Set<IDoricJSLoader> jsLoaders = new HashSet<>(); private static Set<IDoricJSLoader> jsLoaders = new HashSet<>();
private Map<String, Object> extendedEnvValues = new HashMap<>();
static { static {
addJSLoader(new DoricAssetJSLoader()); addJSLoader(new DoricAssetJSLoader());
@ -154,4 +155,12 @@ public class DoricRegistry {
public static Collection<IDoricJSLoader> getJSLoaders() { public static Collection<IDoricJSLoader> getJSLoaders() {
return jsLoaders; return jsLoaders;
} }
public void setEnvironmentVariabel(String key, Object val) {
extendedEnvValues.put(key, val);
}
public Map<String, Object> getEnvironmentVariables() {
return extendedEnvValues;
}
} }

View File

@ -30,6 +30,7 @@ import com.github.pengfeizhou.jscore.JavaFunction;
import com.github.pengfeizhou.jscore.JavaValue; import com.github.pengfeizhou.jscore.JavaValue;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map;
import pub.doric.Doric; import pub.doric.Doric;
import pub.doric.DoricRegistry; import pub.doric.DoricRegistry;
@ -91,14 +92,22 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
mDoricJSE.injectGlobalJSObject(DoricConstant.INJECT_ENVIRONMENT, new JavaValue(new JSONBuilder() JSONBuilder envObject = new JSONBuilder()
.put("platform", "Android") .put("platform", "Android")
.put("platformVersion", String.valueOf(android.os.Build.VERSION.SDK_INT)) .put("platformVersion", String.valueOf(android.os.Build.VERSION.SDK_INT))
.put("appName", appName) .put("appName", appName)
.put("appVersion", appVersion) .put("appVersion", appVersion)
.put("screenWidth", DoricUtils.px2dp(DoricUtils.getScreenWidth())) .put("screenWidth", DoricUtils.px2dp(DoricUtils.getScreenWidth()))
.put("screenHeight", DoricUtils.px2dp(DoricUtils.getScreenHeight())) .put("screenHeight", DoricUtils.px2dp(DoricUtils.getScreenHeight()));
.toJSONObject()));
Map<String, Object> 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() { mDoricJSE.injectGlobalJSFunction(DoricConstant.INJECT_LOG, new JavaFunction() {
@Override @Override
public JavaValue exec(JSDecoder[] args) { public JavaValue exec(JSDecoder[] args) {

607
doric-js/index.d.ts vendored
View File

@ -28,12 +28,13 @@ declare module 'doric/lib/src/runtime/global' {
libVersion: string; libVersion: string;
screenWidth: number; screenWidth: number;
screenHeight: number; screenHeight: number;
[index: string]: number | string | boolean | object | undefined;
}; };
function Entry(constructor: { function Entry(constructor: {
new (...args: any[]): {}; new(...args: any[]): {};
}): any; }): any;
} }
export {}; export { };
} }
declare module 'doric/lib/src/ui/index.ui' { 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"; import { IAnimation } from "doric/lib/src/ui/animation";
export function Property(target: Object, propKey: string): void; export function Property(target: Object, propKey: string): void;
export interface IView { export interface IView {
width?: number; width?: number;
height?: number; height?: number;
backgroundColor?: Color | GradientColor; backgroundColor?: Color | GradientColor;
corners?: number | { corners?: number | {
leftTop?: number; leftTop?: number;
rightTop?: number; rightTop?: number;
leftBottom?: number; leftBottom?: number;
rightBottom?: number; rightBottom?: number;
}; };
border?: { border?: {
width: number; width: number;
color: Color; color: Color;
}; };
shadow?: { shadow?: {
color: Color; color: Color;
opacity: number; opacity: number;
radius: number; radius: number;
offsetX: number; offsetX: number;
offsetY: number; offsetY: number;
}; };
/** /**
* float [0,..1] * float [0,..1]
*/ */
alpha?: number; alpha?: number;
hidden?: boolean; hidden?: boolean;
padding?: { padding?: {
left?: number; left?: number;
right?: number; right?: number;
top?: number; top?: number;
bottom?: number; bottom?: number;
}; };
layoutConfig?: LayoutConfig; layoutConfig?: LayoutConfig;
onClick?: Function; onClick?: Function;
identifier?: string; identifier?: string;
/**++++++++++transform++++++++++*/ /**++++++++++transform++++++++++*/
translationX?: number; translationX?: number;
translationY?: number; translationY?: number;
scaleX?: number; scaleX?: number;
scaleY?: number; scaleY?: number;
/** /**
* float [0,..1] * float [0,..1]
*/ */
pivotX?: number; pivotX?: number;
/** /**
* float [0,..1] * float [0,..1]
*/ */
pivotY?: number; pivotY?: number;
/** /**
* rotation*PI * rotation*PI
*/ */
rotation?: number; rotation?: number;
} }
export type NativeViewModel = { export type NativeViewModel = {
id: string; id: string;
type: string; type: string;
props: { props: {
[index: string]: Model; [index: string]: Model;
}; };
}; };
export abstract class View implements Modeling, IView { 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; 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<String, Function>;
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<any>;
getWidth(context: BridgeContext): Promise<number>;
getHeight(context: BridgeContext): Promise<number>;
getLocationOnScreen(context: BridgeContext): Promise<{
x: number; x: number;
y: number; y: number;
backgroundColor?: Color | GradientColor; }>;
corners?: number | { /**++++++++++transform++++++++++*/
leftTop?: number; translationX?: number;
rightTop?: number; translationY?: number;
leftBottom?: number; scaleX?: number;
rightBottom?: number; scaleY?: number;
}; pivotX?: number;
border?: { pivotY?: number;
width: number; rotation?: number;
color: Color; /**----------transform----------*/
}; doAnimation(context: BridgeContext, animation: IAnimation): Promise<void>;
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<String, Function>;
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<any>;
getWidth(context: BridgeContext): Promise<number>;
getHeight(context: BridgeContext): Promise<number>;
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<void>;
} }
export abstract class Superview extends View { export abstract class Superview extends View {
subviewById(id: string): View | undefined; subviewById(id: string): View | undefined;
abstract allSubviews(): Iterable<View>; abstract allSubviews(): Iterable<View>;
isDirty(): boolean; isDirty(): boolean;
clean(): void; clean(): void;
toModel(): NativeViewModel; toModel(): NativeViewModel;
} }
export abstract class Group extends Superview { export abstract class Group extends Superview {
readonly children: View[]; readonly children: View[];
allSubviews(): View[]; allSubviews(): View[];
addChild(view: View): void; addChild(view: View): void;
} }
} }
@ -269,124 +270,124 @@ declare module 'doric/lib/src/ui/animation' {
import { Modeling, Model } from "doric/lib/src/util/types"; import { Modeling, Model } from "doric/lib/src/util/types";
export type AnimatedKey = "translationX" | "translationY" | "scaleX" | "scaleY" | "rotation" | "pivotX" | "pivotY"; export type AnimatedKey = "translationX" | "translationY" | "scaleX" | "scaleY" | "rotation" | "pivotX" | "pivotY";
export enum RepeatMode { export enum RepeatMode {
RESTART = 1, RESTART = 1,
REVERSE = 2 REVERSE = 2
} }
export interface IAnimation extends Modeling { export interface IAnimation extends Modeling {
duration: number; duration: number;
delay?: number; delay?: number;
} }
export interface Changeable { export interface Changeable {
fromValue: number; fromValue: number;
toValue: number; toValue: number;
key: AnimatedKey; key: AnimatedKey;
repeatCount?: number; repeatCount?: number;
repeatMode?: RepeatMode; repeatMode?: RepeatMode;
} }
export enum FillMode { export enum FillMode {
/** /**
* The receiver is removed from the presentation when the animation is completed. * The receiver is removed from the presentation when the animation is completed.
*/ */
Removed = 0, Removed = 0,
/** /**
* The receiver remains visible in its final state when the animation is completed. * The receiver remains visible in its final state when the animation is completed.
*/ */
Forward = 1, Forward = 1,
/** /**
* The receiver clamps values before zero to zero when the animation is completed. * The receiver clamps values before zero to zero when the animation is completed.
*/ */
Backward = 2, Backward = 2,
/** /**
* The receiver clamps values at both ends of the objects time space * The receiver clamps values at both ends of the objects time space
*/ */
Both = 3 Both = 3
} }
export enum TimingFunction { 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. * The system default timing function. Use this function to ensure that the timing of your animations matches that of most system animations.
*/ */
Default = 0, Default = 0,
/** /**
* Linear pacing, which causes an animation to occur evenly over its duration. * Linear pacing, which causes an animation to occur evenly over its duration.
*/ */
Linear = 1, Linear = 1,
/** /**
* Ease-in pacing, which causes an animation to begin slowly and then speed up as it progresses. * Ease-in pacing, which causes an animation to begin slowly and then speed up as it progresses.
*/ */
EaseIn = 2, EaseIn = 2,
/** /**
* Ease-out pacing, which causes an animation to begin quickly and then slow as it progresses. * Ease-out pacing, which causes an animation to begin quickly and then slow as it progresses.
*/ */
EaseOut = 3, 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. * 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 EaseInEaseOut = 4
} }
abstract class Animation implements IAnimation { abstract class Animation implements IAnimation {
changeables: Map<AnimatedKey, Changeable>; changeables: Map<AnimatedKey, Changeable>;
duration: number;
repeatCount?: number;
repeatMode?: RepeatMode;
delay?: number;
fillMode: FillMode;
timingFunction?: TimingFunction;
toModel(): {
type: string;
delay: number | undefined;
duration: number; duration: number;
repeatCount?: number; changeables: {
repeatMode?: RepeatMode; key: AnimatedKey;
delay?: number; fromValue: number;
toValue: number;
}[];
repeatCount: number | undefined;
repeatMode: RepeatMode | undefined;
fillMode: FillMode; fillMode: FillMode;
timingFunction?: TimingFunction; timingFunction: TimingFunction | undefined;
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;
};
} }
export class ScaleAnimation extends Animation { export class ScaleAnimation extends Animation {
constructor(); constructor();
set fromScaleX(v: number); set fromScaleX(v: number);
get fromScaleX(): number; get fromScaleX(): number;
set toScaleX(v: number); set toScaleX(v: number);
get toScaleX(): number; get toScaleX(): number;
set fromScaleY(v: number); set fromScaleY(v: number);
get fromScaleY(): number; get fromScaleY(): number;
set toScaleY(v: number); set toScaleY(v: number);
get toScaleY(): number; get toScaleY(): number;
} }
export class TranslationAnimation extends Animation { export class TranslationAnimation extends Animation {
constructor(); constructor();
set fromTranslationX(v: number); set fromTranslationX(v: number);
get fromTranslationX(): number; get fromTranslationX(): number;
set toTranslationX(v: number); set toTranslationX(v: number);
get toTranslationX(): number; get toTranslationX(): number;
set fromTranslationY(v: number); set fromTranslationY(v: number);
get fromTranslationY(): number; get fromTranslationY(): number;
set toTranslationY(v: number); set toTranslationY(v: number);
get toTranslationY(): number; get toTranslationY(): number;
} }
export class RotationAnimation extends Animation { export class RotationAnimation extends Animation {
constructor(); constructor();
set fromRotation(v: number); set fromRotation(v: number);
get fromRotation(): number; get fromRotation(): number;
set toRotation(v: number); set toRotation(v: number);
get toRotation(): number; get toRotation(): number;
} }
export class AnimationSet implements IAnimation { export class AnimationSet implements IAnimation {
_duration: number; _duration: number;
delay?: number; delay?: number;
addAnimation(anim: IAnimation): void; addAnimation(anim: IAnimation): void;
get duration(): number; get duration(): number;
set duration(v: number); set duration(v: number);
toModel(): { toModel(): {
animations: Model; animations: Model;
delay: number | undefined; delay: number | undefined;
}; };
} }
export {}; export { };
} }
declare module 'doric/lib/src/widget/layouts' { 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 stack(views: View[], config?: IStack): Stack;
export function hlayout(views: View[], config?: IHLayout): HLayout; export function hlayout(views: View[], config?: IHLayout): HLayout;
export function vlayout(views: View[], config?: IVLayout): VLayout; export function vlayout(views: View[], config?: IVLayout): VLayout;
export {}; export { };
} }
declare module 'doric/lib/src/widget/text' { 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 { Gravity } from "doric/lib/src/util/gravity";
import { Modeling } from "doric/lib/src/util/types"; import { Modeling } from "doric/lib/src/util/types";
export enum LayoutSpec { export enum LayoutSpec {
/** /**
* Depends on what's been set on width or height. * Depends on what's been set on width or height.
*/ */
JUST = 0, JUST = 0,
/** /**
* Depends on it's content. * Depends on it's content.
*/ */
FIT = 1, FIT = 1,
/** /**
* Extend as much as parent let it take. * Extend as much as parent let it take.
*/ */
MOST = 2 MOST = 2
} }
export interface LayoutConfig { export interface LayoutConfig {
widthSpec?: LayoutSpec; widthSpec?: LayoutSpec;
heightSpec?: LayoutSpec; heightSpec?: LayoutSpec;
margin?: { margin?: {
left?: number; left?: number;
right?: number; right?: number;
top?: number; top?: number;
bottom?: number; bottom?: number;
}; };
alignment?: Gravity; alignment?: Gravity;
weight?: number; weight?: number;
} }
export class LayoutConfigImpl implements LayoutConfig, Modeling { export class LayoutConfigImpl implements LayoutConfig, Modeling {
widthSpec?: LayoutSpec; widthSpec?: LayoutSpec;
heightSpec?: LayoutSpec; heightSpec?: LayoutSpec;
margin?: { margin?: {
left?: number; left?: number;
right?: number; right?: number;
top?: number; top?: number;
bottom?: number; bottom?: number;
}; };
alignment?: Gravity; alignment?: Gravity;
weight?: number; weight?: number;
fit(): this; fit(): this;
most(): this; most(): this;
just(): this; just(): this;
configWidth(w: LayoutSpec): this; configWidth(w: LayoutSpec): this;
configHeight(h: LayoutSpec): this; configHeight(h: LayoutSpec): this;
configMargin(m: { configMargin(m: {
left?: number; left?: number;
right?: number; right?: number;
top?: number; top?: number;
bottom?: number; bottom?: number;
}): this; }): this;
configAlignment(a: Gravity): this; configAlignment(a: Gravity): this;
configWeight(w: number): this; configWeight(w: number): this;
toModel(): { toModel(): {
widthSpec: LayoutSpec | undefined; widthSpec: LayoutSpec | undefined;
heightSpec: LayoutSpec | undefined; heightSpec: LayoutSpec | undefined;
margin: { margin: {
left?: number | undefined; left?: number | undefined;
right?: number | undefined; right?: number | undefined;
top?: number | undefined; top?: number | undefined;
bottom?: number | undefined; bottom?: number | undefined;
} | undefined; } | undefined;
alignment: number | undefined; alignment: number | undefined;
weight: number | undefined; weight: number | undefined;
}; };
} }
export function layoutConfig(): LayoutConfigImpl; export function layoutConfig(): LayoutConfigImpl;
} }
@ -981,7 +982,7 @@ declare module 'doric/lib/src/util/types' {
bind(binder: Binder<T>): void; bind(binder: Binder<T>): void;
static of<E>(v: E): Mutable<E>; static of<E>(v: E): Mutable<E>;
} }
export {}; export { };
} }
declare module 'doric/lib/src/util/uniqueId' { declare module 'doric/lib/src/util/uniqueId' {
@ -1009,7 +1010,7 @@ declare module 'doric/lib/src/pattern/provider' {
} }
export class Observable<M> implements IObservable<M> { export class Observable<M> implements IObservable<M> {
constructor(provider: IProvider, clz: { constructor(provider: IProvider, clz: {
new (...args: any[]): M; new(...args: any[]): M;
}); });
addObserver(observer: Observer<M | undefined>): void; addObserver(observer: Observer<M | undefined>): void;
removeObserver(observer: Observer<M | undefined>): void; removeObserver(observer: Observer<M | undefined>): void;
@ -1018,20 +1019,20 @@ declare module 'doric/lib/src/pattern/provider' {
export interface IProvider { export interface IProvider {
provide(obj: Object): void; provide(obj: Object): void;
acquire<T>(clz: { acquire<T>(clz: {
new (...args: any[]): T; new(...args: any[]): T;
}): T | undefined; }): T | undefined;
remove<T>(clz: { remove<T>(clz: {
new (...args: any[]): T; new(...args: any[]): T;
}): void; }): void;
clear(): void; clear(): void;
observe<T>(clz: { observe<T>(clz: {
new (...args: any[]): T; new(...args: any[]): T;
}): Observable<T>; }): Observable<T>;
} }
export class Provider implements IProvider { export class Provider implements IProvider {
provide(obj: Object): void; provide(obj: Object): void;
acquire<T>(clz: { acquire<T>(clz: {
new (...args: any[]): T; new(...args: any[]): T;
}): T | undefined; }): T | undefined;
remove<T>(clz: new (...args: any[]) => T): void; remove<T>(clz: new (...args: any[]) => T): void;
clear(): void; clear(): void;

View File

@ -7,13 +7,14 @@ export declare type BridgeContext = {
declare global { declare global {
const context: BridgeContext; const context: BridgeContext;
const Environment: { const Environment: {
platform: "Android" | "iOS" | "Qt" | "h5"; platform: "Android" | "iOS" | "Qt" | "web";
platformVersion: string; platformVersion: string;
appName: string; appName: string;
appVersion: string; appVersion: string;
libVersion: string; libVersion: string;
screenWidth: number; screenWidth: number;
screenHeight: number; screenHeight: number;
[index: string]: number | string | boolean | object | undefined;
}; };
function Entry(constructor: { function Entry(constructor: {
new (...args: any[]): {}; new (...args: any[]): {};

View File

@ -22,7 +22,7 @@ export type BridgeContext = {
declare global { declare global {
const context: BridgeContext const context: BridgeContext
const Environment: { const Environment: {
platform: "Android" | "iOS" | "Qt" | "h5", platform: "Android" | "iOS" | "Qt" | "web",
platformVersion: string, platformVersion: string,
@ -36,6 +36,7 @@ declare global {
screenHeight: number, screenHeight: number,
[index: string]: number | string | boolean | object | undefined
} }
function Entry(constructor: { new(...args: any[]): {} }): any function Entry(constructor: { new(...args: any[]): {} }): any
} }