DeviceUtil.uts 2.2 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
import { UIDevice, UIApplication, UIInterfaceOrientation, UIScreen } from 'UIKit';
import { NSLocale, URL, FileManager } from 'Foundation';
import { ATTrackingManager } from 'AppTrackingTransparency';
import { ASIdentifierManager } from 'AdSupport';
import { DispatchSemaphore, DispatchQueue } from 'Dispatch';



export class DeviceUtil {

	public static isPad(): boolean {
		return UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad
	}

	public static getOSVersion(): string {
		return UIDevice.current.systemVersion;
	}

	public static getLan(): string {
		let currentLanguage = NSLocale.preferredLanguages[0];
		return currentLanguage == null ? "" : currentLanguage
	}

	public static getOrientation(): string {
		let orientation = "portrait";
		DispatchQueue.main.sync(execute = () => {
			const orient = UIApplication.shared.statusBarOrientation;
			if (orient == UIInterfaceOrientation.landscapeLeft || orient == UIInterfaceOrientation.landscapeRight) {
				orientation = "landscape";
			}
		})
		return orientation;
	}

	public static getScreenScale(): string {
		return UIScreen.main.scale.description;
	}


	public static getIdfa(): string {
		if (UTSiOS.available("iOS 14, *")) {
			if (ATTrackingManager.trackingAuthorizationStatus == ATTrackingManager.AuthorizationStatus.notDetermined) {
				ATTrackingManager.requestTrackingAuthorization(completionHandler = (status) => { });
			} else if (ATTrackingManager.trackingAuthorizationStatus == ATTrackingManager.AuthorizationStatus.authorized) {
				return ASIdentifierManager.shared().advertisingIdentifier.uuidString;
			}
			return "";
		}

		return ASIdentifierManager.shared().advertisingIdentifier.uuidString;
	}

	public static hasRootPrivilege(): boolean {
		let url = new URL(string = "cydia://");
		let canOpenUrl = false;
		DispatchQueue.main.sync(execute = () => {
			canOpenUrl = UIApplication.shared.canOpenURL(url!)
		})
		if (canOpenUrl) {
			return true;
		}

		let jailbreakToolPaths = [
			"/Applications/Cydia.app",
			"/Library/MobileSubstrate/MobileSubstrate.dylib",
			"/bin/bash",
			"/usr/sbin/sshd",
			"/etc/apt"
		];

		let i = 0;
		while (i < jailbreakToolPaths.length) {
			if (FileManager.default.fileExists(atPath = jailbreakToolPaths[i])) {
				return true;
			}
			i++;
		}


		return false;
	}
}