SystemInfoDeviceUtil.uts 10.0 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 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 111 112 113 114 115 116 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 152 153 154 155 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 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
import Activity from 'android.app.Activity';
import Configuration from 'android.content.res.Configuration';
import Context from 'android.content.Context';
import UiModeManager from 'android.app.UiModeManager';
import WindowManager from 'android.view.WindowManager';
import DisplayMetrics from 'android.util.DisplayMetrics';
import Build from 'android.os.Build';
import File from 'java.io.File';

import TextUtils from 'android.text.TextUtils';

import { UTSAndroid as Device } from "io.dcloud.uts";
import BufferedReader from 'java.io.BufferedReader';
import InputStreamReader from 'java.io.InputStreamReader';
import UTSAndroid from 'io.dcloud.uts.UTSAndroid';


export class DeviceUtil {

	private static customOS : string | null = null
	private static customOSVersion : string | null = null

	private static readonly rootRelatedDirs = [
		"/su", "/su/bin/su", "/sbin/su",
		"/data/local/xbin/su", "/data/local/bin/su", "/data/local/su",
		"/system/xbin/su",
		"/system/bin/su", "/system/sd/xbin/su", "/system/bin/failsafe/su",
		"/system/bin/cufsdosck", "/system/xbin/cufsdosck", "/system/bin/cufsmgr",
		"/system/xbin/cufsmgr", "/system/bin/cufaevdd", "/system/xbin/cufaevdd",
		"/system/bin/conbb", "/system/xbin/conbb"];


	/**
		* HarmonyOS 系统输出的
		* 格式:2.0.0
		*/
	private static KEY_HARMONYOS_VERSION_NAME = "hw_sc.build.platform.version";

	/**
	 * EMUI系统输出的
	 * 格式:EmotionUI_8.0.0
	 */
	private static KEY_EMUI_VERSION_NAME = "ro.build.version.emui";

	/**
	 * MagicUI系统输出的
	 * 格式:3.1.0
	 */
	private static KEY_MAGICUI_VERSION = "ro.build.version.magic";

	/**
	 * MIUI系统输出的
	 * 格式:V12
	 */
	private static KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";

	/**
	 * OPPO手机ColorOS系统输出的
	 * 格式:9
	 */
	private static KEY_COLOROS_VERSION_NAME = "ro.build.version.opporom";

	/**
	 * VIVO手机系统输出的
	 * name格式:funtouch
	 * version格式: 9
	 */
	private static KEY_VIVO_VERSION_NAME = "ro.vivo.os.name";
	private static KEY_VIVO_VERSION = "ro.vivo.os.version";

	/**
	 * OonPlus手机系统输出的
	 * 格式:Hydrogen OS 11.0.7.10.KB05
	 */
	private static KEY_ONEPLUS_VERSION_NAME = "ro.rom.version";

	/**
	 * 魅族手机系统输出的
	 */
	private static KEY_FLYME_VERSION_NAME = "ro.build.display.id";

	/**
	 * nubia手机系统输出的
	 */
	private static KEY_NUBIA_VERSION_NAME = "ro.build.nubia.rom.name";
	private static KEY_NUBIA_VERSION_CODE = "ro.build.nubia.rom.code";



	public static getOrientation(activity : Activity) : string {
		const activityOrientation = activity.getResources().getConfiguration().orientation;
		let outOrientation = "portrait";
		if (activityOrientation == Configuration.ORIENTATION_LANDSCAPE) {
			outOrientation = "landscape";
		}
		return outOrientation;
	}
	
	public static getAppVersionCode():number{
		const activity = UTSAndroid.getUniActivity()!!;
		const packageInfo = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0);
		return packageInfo.versionCode;
	}

	/**
	 * 获取屏幕像素比
	 */
	public static getScaledDensity(activity : Activity) : number {
		const windowManager = activity.getSystemService(Context.WINDOW_SERVICE) as WindowManager;
		const displayMetrics = new DisplayMetrics();

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
			windowManager.getDefaultDisplay().getRealMetrics(displayMetrics);
		} else {
			windowManager.getDefaultDisplay().getMetrics(displayMetrics);
		}
		return displayMetrics.scaledDensity;
	}

	public static getScreenWidth() : number {
		return UTSAndroid.getScreenInfo()["screenWidth"] as Int;
	}

	public static getScreenHeight() : number {
		return UTSAndroid.getScreenInfo()["screenHeight"] as Int;
	}
	
	public static getStatusBarHeight():number {
		return UTSAndroid.getStatusBarHeight();
	}
	
	public static getRomName():string{
		DeviceUtil.setCustomInfo(Build.MANUFACTURER);
		return DeviceUtil.customOS ?? "";
	}
	public static getRomVersion():string{
		DeviceUtil.setCustomInfo(Build.MANUFACTURER);
		return DeviceUtil.customOSVersion ?? "";
	}

	public static getSystemUIModeType(activity : Activity) : string {

		let uiModeManager = activity.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager;
		let modeType = "unknown";

		switch (uiModeManager.getCurrentModeType()) {
			case Configuration.UI_MODE_TYPE_TELEVISION:
				modeType = "tv";
				break;
			case Configuration.UI_MODE_TYPE_WATCH:
				modeType = "watch";
				break;
			case Configuration.UI_MODE_TYPE_NORMAL:
				if (DeviceUtil.isTablet(activity)) {
					modeType = "pad";
				} else {
					modeType = "phone";
				}
				break;
			case Configuration.UI_MODE_TYPE_DESK:
				modeType = "pc";
				break;
			case Configuration.UI_MODE_TYPE_UNDEFINED:
				modeType = "undefined";
				break;
			case Configuration.UI_MODE_TYPE_CAR:
				modeType = "car";
				break;
			case Configuration.UI_MODE_TYPE_VR_HEADSET:
				modeType = "vr";
				break;
			case Configuration.UI_MODE_TYPE_APPLIANCE:
				modeType = "appliance";
				break;
		}

		return modeType;
	}


	public static getOsLanguageNormal(context : Context) : string {
		const LOCALE_ZH_HANS = 'zh-Hans'
		const LOCALE_ZH_HANT = 'zh-Hant'
		let locale = Device.getLanguageInfo(context)["appLanguage"].toString();
		if (locale.indexOf('zh') === 0) {
			if (locale.indexOf('-hans') > -1) {
				return LOCALE_ZH_HANS;
			}
			if (locale.indexOf('-hant') > -1) {
				return LOCALE_ZH_HANT;
			}
			if (locale.includes("-tw") || locale.includes("-hk") || locale.includes("-mo") || locale.includes("-cht")) {
				return LOCALE_ZH_HANT;
			}

			return LOCALE_ZH_HANS;
		} else {
			return locale;
		}
	}

	/**
	 * 是否为平板 不是太准确
	 */
	public static isTablet(activity : Activity) : boolean {
		return (activity.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
	}



	private static setCustomInfo(phoneBrand : string) {
		try {
			switch (DeviceUtil.deleteSpaceAndToUpperCase(phoneBrand)) {
				case "HUAWEI":
					if (DeviceUtil.isHarmonyOS()) {
						DeviceUtil.customOSVersion = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_HARMONYOS_VERSION_NAME);
						DeviceUtil.customOS = "HarmonyOS";
					} else {
						DeviceUtil.customOS = "EMUI";
						DeviceUtil.customOSVersion = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_EMUI_VERSION_NAME);;
					}
					break;
				case "HONOR":
					if (DeviceUtil.isHarmonyOS()) {
						DeviceUtil.customOS = "HarmonyOS";
						if (!TextUtils.isEmpty(DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_HARMONYOS_VERSION_NAME))) {
							DeviceUtil.customOSVersion = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_HARMONYOS_VERSION_NAME);;
						} else {
							DeviceUtil.customOSVersion = "";
						}
					} else if (!TextUtils.isEmpty(DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_MAGICUI_VERSION))) {
						DeviceUtil.customOS = "MagicUI";
						DeviceUtil.customOSVersion = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_MAGICUI_VERSION);
					} else {
						//格式:EmotionUI_8.0.0
						DeviceUtil.customOS = "EMUI";
						DeviceUtil.customOSVersion = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_EMUI_VERSION_NAME);
					}
					break;
				case "XIAOMI":
				case "REDMI":
					//格式:MIUIV12
					DeviceUtil.customOS = "MIUI";
					DeviceUtil.customOSVersion = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_MIUI_VERSION_NAME);
					break;
				case "REALME":
				case "OPPO":
					//格式:ColorOSV2.1
					DeviceUtil.customOS = "ColorOS";
					DeviceUtil.customOSVersion = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_COLOROS_VERSION_NAME);
					break;
				case "VIVO":
					//格式:Funtouch9
					DeviceUtil.customOS = "Funtouch";
					DeviceUtil.customOSVersion = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_VIVO_VERSION);
					break;
				case "ONEPLUS":
					//格式:Hydrogen OS 11.0.7.10.KB05
					DeviceUtil.customOS = "HydrogenOS";
					DeviceUtil.customOSVersion = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_ONEPLUS_VERSION_NAME);
					break;
				case "MEIZU":
					//格式:Flyme 6.3.5.1G
					DeviceUtil.customOS = "Flyme";
					DeviceUtil.customOSVersion = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_FLYME_VERSION_NAME);
					break;
				case "NUBIA":
					//格式:nubiaUIV3.0
					DeviceUtil.customOS = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_NUBIA_VERSION_NAME);
					DeviceUtil.customOSVersion = DeviceUtil.getSystemPropertyValue(DeviceUtil.KEY_NUBIA_VERSION_CODE);
					break;
				default:
					DeviceUtil.customOS = "Android";
					DeviceUtil.customOSVersion = Build.VERSION.RELEASE;
					break;
			}
		} catch (e : Exception) {
		}
	}



	/**
		* 判断是否是华为鸿蒙系统,能否识别荣耀鸿蒙未知
		*
		* @return
		*/
	private static isHarmonyOS() : boolean {
		try {
			let classType = Class.forName("com.huawei.system.BuildEx");
			let getMethod = classType.getMethod("getOsBrand");
			let value = getMethod.invoke(classType) as string;
			return !TextUtils.isEmpty(value);
		} catch (e : Exception) {
			return false;
		}
	}






	/**
		 * 删除字符串中的空格并全部转成大写
		 * @param str
		 * @return
		 */
	public static deleteSpaceAndToUpperCase(str : string | null) : string {
		if (TextUtils.isEmpty(str)) {
			return "";
		}
		return str!.replace(" ", "").toUpperCase();
	}

	private static getSystemPropertyValue(propName : string) : string | null {
		let value : string | null = null;
		let roSecureObj : any | null;
		try {
			// Class.forName("java.lang.String")
			const method = Class.forName("android.os.SystemProperties").getMethod("get", Class.forName("java.lang.String"));
			roSecureObj = method.invoke(null, propName);
			if (roSecureObj != null) {
				value = roSecureObj as string;
			}
		} catch (e : Exception) {
		}
		return value;
	}
}