index.uts 8.6 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6
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';
DCloud-yyl's avatar
DCloud-yyl 已提交
7 8
import Handler from 'android.os.Handler';
import ViewGroup from "android.view.ViewGroup";
DCloud-yyl's avatar
DCloud-yyl 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21

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 已提交
22 23 24 25
			let activity = UTSAndroid.getTopPageActivity();
			if(activity == null){
				activity = UTSAndroid.getUniActivity()!!
			}			
DCloud-yyl's avatar
DCloud-yyl 已提交
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 51
		
			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 已提交
52
			const uniCompilerVersion = UTSAndroid.getUniCompileVersion();
DCloud-yyl's avatar
DCloud-yyl 已提交
53 54
			const uniPlatform = "app";
			const uniRuntimeVersion = UTSAndroid.getUniRuntimeVersion();
DCloud-yyl's avatar
DCloud-yyl 已提交
55
			const uniCompilerVersionCode = convertVersionCode(uniCompilerVersion);
DCloud-yyl's avatar
DCloud-yyl 已提交
56 57 58 59
			const uniRuntimeVersionCode = convertVersionCode(uniRuntimeVersion);
			const version = UTSAndroid.getInnerVersion();
			const romName = DeviceUtil.getRomName();
			const romVersion = DeviceUtil.getRomVersion();
DCloud-yyl's avatar
DCloud-yyl 已提交
60
			const windowWidth = Math.ceil(UTSAndroid.getUniActivity()!!.findViewById<ViewGroup>(android.R.id.content).width / pixelRatio)
DCloud-yyl's avatar
DCloud-yyl 已提交
61 62 63
			const windowHeight = UTSAndroid.getWindowHeight();
			const windowTop = 0;
			const windowBottom = 0;
DCloud-yyl's avatar
DCloud-yyl 已提交
64
			const safeAreaInsetsTop = statusBarHeight;
DCloud-yyl's avatar
DCloud-yyl 已提交
65 66 67 68 69 70
			const safeAreaInsets : SafeAreaInsets = {
				left: 0,
				top: safeAreaInsetsTop,
				right: 0,
				bottom: 0
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
71 72

			const contentHeight = Math.ceil(UTSAndroid.getUniActivity()!!.findViewById<ViewGroup>(android.R.id.content).height / pixelRatio)
DCloud-yyl's avatar
DCloud-yyl 已提交
73 74 75 76 77
			
			const safeArea : SafeArea = {
				left: safeAreaInsets.left,
				right: windowWidth - safeAreaInsets.right,
				top: safeAreaInsets.top,
DCloud-yyl's avatar
DCloud-yyl 已提交
78
				bottom: contentHeight - safeAreaInsets.bottom,
DCloud-yyl's avatar
DCloud-yyl 已提交
79
				width: windowWidth - safeAreaInsets.left - safeAreaInsets.right,
DCloud-yyl's avatar
DCloud-yyl 已提交
80
				height: contentHeight - safeAreaInsets.top - safeAreaInsets.bottom
DCloud-yyl's avatar
DCloud-yyl 已提交
81 82
			}
			const osAndroidAPILevel = Build.VERSION.SDK_INT;
DCloud-yyl's avatar
DCloud-yyl 已提交
83
			const appTheme = UTSAndroid.getAppTheme();
DCloud-yyl's avatar
DCloud-yyl 已提交
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 111 112 113 114
		
			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 已提交
115 116
				uniCompileVersion: uniCompilerVersion,
				uniCompilerVersion: uniCompilerVersion,
DCloud-yyl's avatar
DCloud-yyl 已提交
117 118
				uniPlatform: uniPlatform,
				uniRuntimeVersion: uniRuntimeVersion,
DCloud-yyl's avatar
DCloud-yyl 已提交
119 120
				uniCompileVersionCode: uniCompilerVersionCode,
				uniCompilerVersionCode: uniCompilerVersionCode,
DCloud-yyl's avatar
DCloud-yyl 已提交
121 122 123 124 125 126 127 128 129 130 131
				uniRuntimeVersionCode: uniRuntimeVersionCode,
				version: version,
				romName: romName,
				romVersion: romVersion,
				windowWidth: windowWidth,
				windowHeight: windowHeight,
				windowTop: windowTop,
				windowBottom: windowBottom,
				safeAreaInsets: safeAreaInsets,
				safeArea: safeArea,
				osAndroidAPILevel: osAndroidAPILevel,
DCloud-yyl's avatar
DCloud-yyl 已提交
132
				appTheme: appTheme,
DCloud-yyl's avatar
DCloud-yyl 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
			} 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 已提交
157 158 159 160
	let activity = UTSAndroid.getTopPageActivity();
	if(activity == null){
		activity = UTSAndroid.getUniActivity()!!
	}			
DCloud-yyl's avatar
DCloud-yyl 已提交
161 162 163 164
	const pixelRatio = DeviceUtil.getScaledDensity(activity)
	const screenWidth = DeviceUtil.getScreenWidth();
	const screenHeight = DeviceUtil.getScreenHeight();
	const statusBarHeight = DeviceUtil.getStatusBarHeight();
DCloud-yyl's avatar
DCloud-yyl 已提交
165
	const windowWidth = Math.ceil(UTSAndroid.getUniActivity()!!.findViewById<ViewGroup>(android.R.id.content).width / pixelRatio)
DCloud-yyl's avatar
DCloud-yyl 已提交
166 167 168
	const windowHeight = UTSAndroid.getWindowHeight();
	const windowTop = 0;
	const windowBottom = 0;
DCloud-yyl's avatar
DCloud-yyl 已提交
169
	const safeAreaInsetsTop = statusBarHeight;
DCloud-yyl's avatar
DCloud-yyl 已提交
170 171 172 173 174 175
	const safeAreaInsets : SafeAreaInsets = {
		left: 0,
		top: safeAreaInsetsTop,
		right: 0,
		bottom: 0
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
176 177 178
	
	const contentHeight = Math.ceil(UTSAndroid.getUniActivity()!!.findViewById<ViewGroup>(android.R.id.content).height / pixelRatio)
	
DCloud-yyl's avatar
DCloud-yyl 已提交
179 180 181 182
	const safeArea : SafeArea = {
		left: safeAreaInsets.left,
		right: windowWidth - safeAreaInsets.right,
		top: safeAreaInsets.top,
DCloud-yyl's avatar
DCloud-yyl 已提交
183
		bottom: contentHeight - safeAreaInsets.bottom,
DCloud-yyl's avatar
DCloud-yyl 已提交
184
		width: windowWidth - safeAreaInsets.left - safeAreaInsets.right,
DCloud-yyl's avatar
DCloud-yyl 已提交
185
		height: contentHeight - safeAreaInsets.top - safeAreaInsets.bottom
DCloud-yyl's avatar
DCloud-yyl 已提交
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 255 256 257 258 259 260 261 262
	}
	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);
		}
	}
}