DeviceUtil.uts 10.7 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
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 { EmulatorCheckUtil } from './EmulatorCheckUtil.uts'
import TextUtils from 'android.text.TextUtils';

import BufferedReader from 'java.io.BufferedReader';
import InputStreamReader from 'java.io.InputStreamReader';
import Exception from 'java.lang.Exception';
import KotlinArray from 'kotlin.Array'


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 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 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;
	}


	/**
	 * 是否root
	 */
	public static hasRootPrivilege(): boolean {
		let hasRootDir = false;
		let rootDirs = DeviceUtil.rootRelatedDirs;
		let dirCount = rootDirs.length;
		let i = 0;
		while (i < dirCount) {
			let dir = rootDirs[i];
			if ((new File(dir)).exists()) {
				hasRootDir = true;
				break;
			}
			i++;
		}


		return Build.TAGS != null && Build.TAGS.includes("test-keys") || hasRootDir;
	}

	public static isSimulator(context: Context): boolean {
		return EmulatorCheckUtil.getSingleInstance().emulatorCheck(context);
	}


	public static getDeviceID(context: Context): string {
DCloud-yyl's avatar
DCloud-yyl 已提交
182
		return UTSAndroid.getDeviceID(context);
DCloud-yyl's avatar
DCloud-yyl 已提交
183 184 185 186
	}


	public static getOsLanguage(context: Context): string {
DCloud-yyl's avatar
DCloud-yyl 已提交
187
		return UTSAndroid.getLanguageInfo(context)["osLanguage"].toString();
DCloud-yyl's avatar
DCloud-yyl 已提交
188 189 190 191 192
	}

	public static getOsLanguageNormal(context: Context): string {
		const LOCALE_ZH_HANS = 'zh-Hans'
		const LOCALE_ZH_HANT = 'zh-Hant'
DCloud-yyl's avatar
DCloud-yyl 已提交
193
		let locale = UTSAndroid.getLanguageInfo(context)["appLanguage"].toString();
DCloud-yyl's avatar
DCloud-yyl 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		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 getAppInnerVersion(): string {
DCloud-yyl's avatar
DCloud-yyl 已提交
212
		return UTSAndroid.getInnerVersion();
DCloud-yyl's avatar
DCloud-yyl 已提交
213 214 215
	}

	public static getOaid(): string {
DCloud-yyl's avatar
DCloud-yyl 已提交
216
		return UTSAndroid.getOAID();
DCloud-yyl's avatar
DCloud-yyl 已提交
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 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
	}


	/**
	 * 是否为平板 不是太准确
	 */
	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;
	}
	
	public static listeningForADB(): boolean {
		let cmd : KotlinArray<string> = arrayOf<string>("/bin/sh", "-c", "getprop | grep init.svc.adbd");
		try{
			Runtime.getRuntime().exec(cmd);
		}catch(e:Exception){
			cmd = arrayOf<string>("/system/bin/sh", "-c", "getprop | grep init.svc.adbd");
			try{
				Runtime.getRuntime().exec(cmd);
			}catch(e:Exception){
				return false
			}
		}
		let exec = Runtime.getRuntime().exec(cmd);
		let bufferedReader = new BufferedReader(new InputStreamReader(exec.getInputStream(), "utf-8"));
		let tmp = new CharArray(1024);
	
		let result = false;
	
		do {
			let len = bufferedReader.read(tmp);
			if (len == -1) {
				break;
			}
			let res = new String(tmp, 0, len);
			result = res.includes("running");
		} while (true)
	
		exec.getInputStream().close();
		bufferedReader.close();
		return result;
	}
}