feat:Environment add locale

This commit is contained in:
pengfei.zhou 2021-07-06 17:35:56 +08:00 committed by osborn
parent 666998d3a6
commit 70bde4fba9
5 changed files with 42 additions and 13 deletions

View File

@ -18,7 +18,6 @@ package pub.doric.engine;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
@ -35,6 +34,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.Locale;
import java.util.Map; import java.util.Map;
import pub.doric.Doric; import pub.doric.Doric;
@ -60,6 +60,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
protected IDoricJSE mDoricJSE; protected IDoricJSE mDoricJSE;
private final DoricTimerExtension mTimerExtension; private final DoricTimerExtension mTimerExtension;
private final DoricRegistry mDoricRegistry = new DoricRegistry(); private final DoricRegistry mDoricRegistry = new DoricRegistry();
private final JSONBuilder mEnvironment = new JSONBuilder();
public DoricJSEngine() { public DoricJSEngine() {
handlerThread = new HandlerThread(this.getClass().getSimpleName()); handlerThread = new HandlerThread(this.getClass().getSimpleName());
@ -100,7 +101,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
JSONBuilder envObject = new JSONBuilder() mEnvironment
.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)
@ -111,15 +112,17 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
.put("statusBarHeight", DoricUtils.px2dp(DoricUtils.getStatusBarHeight())) .put("statusBarHeight", DoricUtils.px2dp(DoricUtils.getStatusBarHeight()))
.put("hasNotch", false) .put("hasNotch", false)
.put("deviceBrand", Build.BRAND) .put("deviceBrand", Build.BRAND)
.put("deviceModel", Build.MODEL); .put("deviceModel", Build.MODEL)
.put("localeLanguage", context.getResources().getConfiguration().locale.getLanguage())
.put("localeCountry", context.getResources().getConfiguration().locale.getCountry());
Map<String, Object> extend = mDoricRegistry.getEnvironmentVariables(); Map<String, Object> extend = mDoricRegistry.getEnvironmentVariables();
for (String key : extend.keySet()) { for (String key : extend.keySet()) {
envObject.put(key, extend.get(key)); mEnvironment.put(key, extend.get(key));
} }
mDoricJSE.injectGlobalJSObject(DoricConstant.INJECT_ENVIRONMENT, mDoricJSE.injectGlobalJSObject(DoricConstant.INJECT_ENVIRONMENT,
new JavaValue(envObject.toJSONObject())); new JavaValue(mEnvironment.toJSONObject()));
mDoricJSE.injectGlobalJSFunction(DoricConstant.INJECT_LOG, new JavaFunction() { mDoricJSE.injectGlobalJSFunction(DoricConstant.INJECT_LOG, new JavaFunction() {
@Override @Override

View File

@ -44,7 +44,7 @@ - (void)onLog:(DoricLogType)type message:(NSString *)message {
@interface DoricJSEngine () @interface DoricJSEngine ()
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSTimer *> *timers; @property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSTimer *> *timers;
@property(nonatomic, strong) DoricBridgeExtension *bridgeExtension; @property(nonatomic, strong) DoricBridgeExtension *bridgeExtension;
@property(nonatomic, strong) NSDictionary *innerEnvironmentDictionary; @property(nonatomic, strong) NSMutableDictionary *environmentDictionary;
@property(nonatomic, strong) NSThread *jsThread; @property(nonatomic, strong) NSThread *jsThread;
@property(nonatomic, assign) BOOL destroyed; @property(nonatomic, assign) BOOL destroyed;
@end @end
@ -73,8 +73,7 @@ - (instancetype)init {
screenWidth = [[UIScreen mainScreen] bounds].size.height; screenWidth = [[UIScreen mainScreen] bounds].size.height;
screenHeight = [[UIScreen mainScreen] bounds].size.width; screenHeight = [[UIScreen mainScreen] bounds].size.width;
} }
_environmentDictionary = @{
_innerEnvironmentDictionary = @{
@"platform": @"iOS", @"platform": @"iOS",
@"platformVersion": [[UIDevice currentDevice] systemVersion], @"platformVersion": [[UIDevice currentDevice] systemVersion],
@"appName": infoDictionary[@"CFBundleName"], @"appName": infoDictionary[@"CFBundleName"],
@ -86,7 +85,9 @@ - (instancetype)init {
@"hasNotch": @(hasNotch()), @"hasNotch": @(hasNotch()),
@"deviceBrand": @"Apple", @"deviceBrand": @"Apple",
@"deviceModel": platform, @"deviceModel": platform,
}; @"localeLanguage": [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode],
@"localeCountry": [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode],
}.mutableCopy;
self.registry = [[DoricRegistry alloc] init]; self.registry = [[DoricRegistry alloc] init];
[self ensureRunOnJSThread:^() { [self ensureRunOnJSThread:^() {
self.timers = [[NSMutableDictionary alloc] init]; self.timers = [[NSMutableDictionary alloc] init];
@ -135,12 +136,10 @@ - (void)initJSEngine {
- (void)initJSExecutor { - (void)initJSExecutor {
__weak typeof(self) _self = self; __weak typeof(self) _self = self;
NSMutableDictionary *envDic = [self.innerEnvironmentDictionary mutableCopy];
[self.registry.environmentVariables enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) { [self.registry.environmentVariables enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
envDic[key] = obj; self.environmentDictionary[key] = obj;
}]; }];
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[self.environmentDictionary copy]];
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[envDic copy]];
[self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString *type, NSString *message) { [self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString *type, NSString *message) {
if ([type isEqualToString:@"e"]) { if ([type isEqualToString:@"e"]) {
[self.registry onLog:DoricLogTypeError message:message]; [self.registry onLog:DoricLogTypeError message:message];

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

@ -60,6 +60,14 @@ declare module 'doric/lib/src/runtime/global' {
* ex: iPhone12,5 or pixel 3 * ex: iPhone12,5 or pixel 3
*/ */
deviceModel: string; deviceModel: string;
/**
* The language code for current locale
*/
localeLanguage: string;
/**
* The country/region code for current locale
*/
localeCountry: string;
[index: string]: number | string | boolean | object | undefined; [index: string]: number | string | boolean | object | undefined;
}; };
function Entry(constructor: ClassType<Panel>): void; function Entry(constructor: ClassType<Panel>): void;

View File

@ -48,6 +48,14 @@ declare global {
* ex: iPhone12,5 or pixel 3 * ex: iPhone12,5 or pixel 3
*/ */
deviceModel: string; deviceModel: string;
/**
* The language code for current locale
*/
localeLanguage: string;
/**
* The country/region code for current locale
*/
localeCountry: string;
[index: string]: number | string | boolean | object | undefined; [index: string]: number | string | boolean | object | undefined;
}; };
function Entry(constructor: ClassType<Panel>): void; function Entry(constructor: ClassType<Panel>): void;

View File

@ -75,6 +75,17 @@ declare global {
* ex: iPhone12,5 or pixel 3 * ex: iPhone12,5 or pixel 3
*/ */
deviceModel: string, deviceModel: string,
/**
* The language code for current locale
*/
localeLanguage: string
/**
* The country/region code for current locale
*/
localeCountry: string
[index: string]: number | string | boolean | object | undefined [index: string]: number | string | boolean | object | undefined
} }