index.uts 8.1 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import { GetSystemInfoOptions, GetSystemInfo, GetSystemInfoResult, SafeArea, SafeAreaInsets, GetSystemInfoSync, GetWindowInfo, GetWindowInfoResult } from "../interface.uts";
import PackageManager from 'android.content.pm.PackageManager';
import Build from 'android.os.Build';
import { DeviceUtil } from './device/SystemInfoDeviceUtil.uts'
import CountDownLatch from 'java.util.concurrent.CountDownLatch';
import Looper from 'android.os.Looper';
import Handler from 'android.os.Handler';

export const getSystemInfo : GetSystemInfo = function (options : GetSystemInfoOptions) {
	const result = getSystemInfoSync();
	const success = options.success;
	const complete = options.complete;
	success?.(result);
	complete?.(result);
}


export const getSystemInfoSync : GetSystemInfoSync = function () : GetSystemInfoResult {
	const runnable = function(): GetSystemInfoResult | null{
		try {
DCloud-yyl's avatar
DCloud-yyl 已提交
21 22 23 24
			let activity = UTSAndroid.getTopPageActivity();
			if(activity == null){
				activity = UTSAndroid.getUniActivity()!!
			}			
DCloud-yyl's avatar
DCloud-yyl 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
		
			const appId = UTSAndroid.getAppId();
			const appLanguage = DeviceUtil.getOsLanguageNormal(activity);
			const appName = UTSAndroid.getAppName();
			const appVersion = UTSAndroid.getAppVersion()["name"].toString();
			const appVersionCode = UTSAndroid.getAppVersion()["code"].toString();
			const appWgtVersion = !UTSAndroid.isUniAppX()? UTSAndroid.getAppVersion()["name"].toString() : null;
			const brand = Build.MANUFACTURER.toLowerCase();
			const browserName = UTSAndroid.getWebViewInfo(activity)["kernel"].toString();
			const browserVersion = UTSAndroid.getWebViewInfo(activity)["version"].toString();
			const deviceId = UTSAndroid.getDeviceID(activity);
			const deviceModel = Build.MODEL;
			const deviceType = DeviceUtil.getSystemUIModeType(activity);
			const devicePixelRatio = DeviceUtil.getScaledDensity(activity);
			const deviceOrientation = DeviceUtil.getOrientation(activity);
			const language = UTSAndroid.getLanguageInfo(activity)["osLanguage"].toString();
			const osName = "android";
			const osVersion = Build.VERSION.RELEASE;
			const osLanguage = UTSAndroid.getLanguageInfo(activity)["osLanguage"].toString();
			const osTheme = UTSAndroid.getOsTheme();
			const pixelRatio = DeviceUtil.getScaledDensity(activity)
			const screenWidth = DeviceUtil.getScreenWidth();
			const screenHeight = DeviceUtil.getScreenHeight();
			const statusBarHeight = DeviceUtil.getStatusBarHeight();
			const system = "Android " + Build.VERSION.RELEASE;
			const ua = UTSAndroid.getWebViewInfo(activity)["ua"].toString();
DCloud-yyl's avatar
DCloud-yyl 已提交
51
			const uniCompilerVersion = UTSAndroid.getUniCompileVersion();
DCloud-yyl's avatar
DCloud-yyl 已提交
52 53
			const uniPlatform = "app";
			const uniRuntimeVersion = UTSAndroid.getUniRuntimeVersion();
DCloud-yyl's avatar
DCloud-yyl 已提交
54
			const uniCompilerVersionCode = convertVersionCode(uniCompilerVersion);
DCloud-yyl's avatar
DCloud-yyl 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
			const uniRuntimeVersionCode = convertVersionCode(uniRuntimeVersion);
			const version = UTSAndroid.getInnerVersion();
			const romName = DeviceUtil.getRomName();
			const romVersion = DeviceUtil.getRomVersion();
			const windowWidth = screenWidth;
			const windowHeight = UTSAndroid.getWindowHeight();
			const windowTop = 0;
			const windowBottom = 0;
			const safeAreaInsetsTop = UTSAndroid.getSafeAreaInsets()["top"].toString().toInt();
			const safeAreaInsets : SafeAreaInsets = {
				left: 0,
				top: safeAreaInsetsTop,
				right: 0,
				bottom: 0
			}
			
			const safeArea : SafeArea = {
				left: safeAreaInsets.left,
				right: windowWidth - safeAreaInsets.right,
				top: safeAreaInsets.top,
				bottom: windowHeight - safeAreaInsets.bottom,
				width: windowWidth - safeAreaInsets.left - safeAreaInsets.right,
				height: windowHeight - safeAreaInsets.top - safeAreaInsets.bottom
			}
			const osAndroidAPILevel = Build.VERSION.SDK_INT;
		
			const result = {
				SDKVersion: "",
				appId: appId,
				appLanguage: appLanguage,
				appName: appName,
				appVersion: appVersion,
				appVersionCode: appVersionCode,
				appWgtVersion: appWgtVersion,
				brand: brand,
				browserName: browserName,
				browserVersion: browserVersion,
				deviceId: deviceId,
				deviceBrand: brand,
				deviceModel: deviceModel,
				deviceType: deviceType,
				devicePixelRatio: devicePixelRatio,
				deviceOrientation: deviceOrientation,
				language: language,
				model: deviceModel,
				osName: osName,
				osVersion: osVersion,
				osLanguage: osLanguage,
				osTheme: osTheme,
				pixelRatio: pixelRatio,
				platform: osName,
				screenWidth: screenWidth,
				screenHeight: screenHeight,
				statusBarHeight: statusBarHeight,
				system: system,
				ua: ua,
DCloud-yyl's avatar
DCloud-yyl 已提交
111 112
				uniCompileVersion: uniCompilerVersion,
				uniCompilerVersion: uniCompilerVersion,
DCloud-yyl's avatar
DCloud-yyl 已提交
113 114
				uniPlatform: uniPlatform,
				uniRuntimeVersion: uniRuntimeVersion,
DCloud-yyl's avatar
DCloud-yyl 已提交
115 116
				uniCompileVersionCode: uniCompilerVersionCode,
				uniCompilerVersionCode: uniCompilerVersionCode,
DCloud-yyl's avatar
DCloud-yyl 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
				uniRuntimeVersionCode: uniRuntimeVersionCode,
				version: version,
				romName: romName,
				romVersion: romVersion,
				windowWidth: windowWidth,
				windowHeight: windowHeight,
				windowTop: windowTop,
				windowBottom: windowBottom,
				safeAreaInsets: safeAreaInsets,
				safeArea: safeArea,
				osAndroidAPILevel: osAndroidAPILevel,
			} as GetSystemInfoResult;
			return result;
		} catch (e : Exception) {
			return null;
		}
	}
	
	let result : GetSystemInfoResult | null = null;
	if(Looper.myLooper() == Looper.getMainLooper()){
		result = runnable();
	}else{
		const latch = new CountDownLatch(1);
		new RunnableTask(Looper.getMainLooper(), () => {
			result = runnable();
			latch.countDown();
		}).execute();
		latch.await();
	}

	return result!!;
};


export const getWindowInfo: GetWindowInfo = function(): GetWindowInfoResult {
DCloud-yyl's avatar
DCloud-yyl 已提交
152 153 154 155
	let activity = UTSAndroid.getTopPageActivity();
	if(activity == null){
		activity = UTSAndroid.getUniActivity()!!
	}			
DCloud-yyl's avatar
DCloud-yyl 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	const pixelRatio = DeviceUtil.getScaledDensity(activity)
	const screenWidth = DeviceUtil.getScreenWidth();
	const screenHeight = DeviceUtil.getScreenHeight();
	const statusBarHeight = DeviceUtil.getStatusBarHeight();
	const windowWidth = screenWidth;
	const windowHeight = UTSAndroid.getWindowHeight();
	const windowTop = 0;
	const windowBottom = 0;
	const safeAreaInsetsTop = UTSAndroid.getSafeAreaInsets()["top"] as Int;
	const safeAreaInsets : SafeAreaInsets = {
		left: 0,
		top: safeAreaInsetsTop,
		right: 0,
		bottom: 0
	}
	const safeArea : SafeArea = {
		left: safeAreaInsets.left,
		right: windowWidth - safeAreaInsets.right,
		top: safeAreaInsets.top,
		bottom: windowHeight - safeAreaInsets.bottom,
		width: windowWidth - safeAreaInsets.left - safeAreaInsets.right,
		height: windowHeight - safeAreaInsets.top - safeAreaInsets.bottom
	}
	const screenTop = windowTop;
	
    let result = {
		pixelRatio: pixelRatio,
		screenWidth: screenWidth,
		screenHeight: screenHeight,
		windowWidth: windowWidth,
		windowHeight: windowHeight,
		statusBarHeight: statusBarHeight,
		windowTop: windowTop,
		windowBottom: windowBottom,
		safeArea: safeArea,
		safeAreaInsets: safeAreaInsets,
		screenTop: screenTop
	} as GetWindowInfoResult;
	return result;
};

const convertVersionCode = function(version: string): number {
	let str = "";
	let radixLength = 2;
	let findDot = false;
	const dotChar = ".".get(0);
	for (let i = 0; i < version.length; i++) {
		const char = version.get(i);
		if(findDot){
			if(char.isDigit()){
				str += char;
			}
			radixLength --;
			if(radixLength == 0){
				break;
			}
		}else{
			if(char.isDigit()){
				str += char;
			}else{
				if(char == dotChar){
					findDot = true;
					str += char;
				}
			}
		}
	}
	
	return parseFloat(str);
}

const getHostVersion = function () : string {
	const context = UTSAndroid.getAppContext()!!;
	let packageManager = context.getPackageManager();
	let applicationInfo = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
	return applicationInfo.versionName;
}

class RunnableTask extends Runnable {
	private callback : () => void | null;
	private looper : Looper | null = null;
	constructor(looper : Looper | null, callback : () => void) {
		super();
		this.looper = looper;
		this.callback = callback
	}

	override run() {
		this.callback?.()
	}

	public execute() {
		if (this.looper == null) {
			this.run();
		} else {
			new Handler(this.looper!!).post(this);
		}
	}
}