index.uts 2.3 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 21 22 23 24 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
import Build from 'android.os.Build';
import { DeviceUtil } from './device/DeviceUtil.uts';

import { GetDeviceInfo, GetDeviceInfoOptions, GetDeviceInfoResult } from '../interface.uts'


export const getDeviceInfo : GetDeviceInfo = (config : GetDeviceInfoOptions | null) : GetDeviceInfoResult => {
	let filter : Array<string> = [];
	if (config != null && config.filter != null) {
		filter = config.filter;
	}

	if (config == null || filter.length == 0) {
		const defaultFilter = [
			"brand",
			"deviceBrand",
			"deviceId",
			"model",
			"deviceModel",
			"deviceType",
			"deviceOrientation",
			"devicePixelRatio",
			"system",
			"platform",
			"isRoot",
			"isSimulator",
			"isUSBDebugging"
		];
		filter = defaultFilter;
	}
	return getBaseInfo(filter);
}

function getBaseInfo(filterArray : Array<string>) : GetDeviceInfoResult {
	const activity = UTSAndroid.getUniActivity()!;
	let result : GetDeviceInfoResult = {};
	if (filterArray.indexOf("brand") != -1) {
		result.brand = Build.MANUFACTURER;
	}
	if (filterArray.indexOf("deviceBrand") != -1) {
		result.deviceBrand = Build.MANUFACTURER;
	}
	if (filterArray.indexOf("model") != -1) {
		result.model = Build.MODEL;
	}
	if (filterArray.indexOf("deviceModel") != -1) {
		result.deviceModel = Build.MODEL;
	}
	if (filterArray.indexOf("deviceType") != -1) {
		result.deviceType = DeviceUtil.getSystemUIModeType(activity);
	}
	if (filterArray.indexOf("deviceOrientation") != -1) {
		result.deviceOrientation = DeviceUtil.getOrientation(activity);
	}
	if (filterArray.indexOf("deviceId") != -1) {
		result.deviceId = DeviceUtil.getDeviceID(activity);
	}
	if (filterArray.indexOf("devicePixelRatio") != -1) {
		result.devicePixelRatio = DeviceUtil.getScaledDensity(activity) + "";
	}
	if (filterArray.indexOf("system") != -1) {
		result.system = "Android " + Build.VERSION.RELEASE;
	}
	if (filterArray.indexOf("platform") != -1) {
		result.platform = "android";
	}
	if (filterArray.indexOf("isRoot") != -1) {
		result.isRoot = DeviceUtil.hasRootPrivilege();
	}
	if (filterArray.indexOf("isSimulator") != -1) {
		result.isSimulator = DeviceUtil.isSimulator(activity);
	}
	if (filterArray.indexOf("isUSBDebugging") != -1) {
		result.isUSBDebugging = DeviceUtil.listeningForADB();
	}
	return result;
}