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

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

@ -28,6 +28,7 @@ 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[]): {};

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