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) {

21
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' {
@ -386,7 +387,7 @@ declare module 'doric/lib/src/ui/animation' {
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' {
@ -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
} }