AppBaseInfoDeviceUtil.uts 3.6 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5

import PackageManager from 'android.content.pm.PackageManager';
import Context from 'android.content.Context';
import PackageInfo from 'android.content.pm.PackageInfo';
import UiModeManager from 'android.app.UiModeManager';
DCloud-yyl's avatar
DCloud-yyl 已提交
6
import Activity from 'android.app.Activity';
DCloud-yyl's avatar
DCloud-yyl 已提交
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
import Signature from 'android.content.pm.Signature';
import Exception from 'java.lang.Exception';
import MessageDigest from 'java.security.MessageDigest';
export class AppBaseInfoDeviceUtil {

	public static getAppID() : string {
		return UTSAndroid.getAppId();
	}

	public static getAppName(context : Context) : string {
		let packageManager = context.getPackageManager();
		return packageManager.getApplicationLabel(context.getApplicationInfo()).toString()
	}

	public static getPackageName(context : Context) : string {
		return context.getPackageName();
	}

	public static getAppVersionName() : string {
		return UTSAndroid.getAppVersion()["name"].toString();
	}

	public static getAppVersionCode() : string {
		return UTSAndroid.getAppVersion()["code"].toString();
	}

	public static getHostVersion(context : Context) : string {
		let packageManager = context.getPackageManager();
		let applicationInfo = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
		return applicationInfo.versionName;
	}

	public static getHostCode(context : Context) : string {
		let packageManager = context.getPackageManager();
		let applicationInfo = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_ACTIVITIES);
		return applicationInfo.versionCode + "";
	}

	public static isSystemNightMode(activity : Activity) : boolean {
		let uiModeManager = activity.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager;
		return uiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES;
	}

	public static getOsLanguage(context : Context) : string {
		return UTSAndroid.getLanguageInfo(context)["osLanguage"].toString();
	}

	public static getOsLanguageNormal(context : Context) : string {
		const LOCALE_ZH_HANS = 'zh-Hans'
		const LOCALE_ZH_HANT = 'zh-Hant'
		let locale = UTSAndroid.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 getAppInnerVersion() : string {
		return UTSAndroid.getInnerVersion();
	}

	public static getAppSignatureSHA1(context : Context) : string {
		try {
			const packageManager = context.getPackageManager();
			const info = packageManager.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES) as PackageInfo;
			let result = "";
			info.signatures.forEach((value) => {
				result = AppBaseInfoDeviceUtil.getSignatureString(value, "SHA1")
			})
			return result
		} catch (e : Exception) {
			return ""
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
91 92
	}
	
DCloud-yyl's avatar
DCloud-yyl 已提交
93 94 95 96 97 98 99 100 101 102 103 104
	private static getSignatureString(sign: Signature, type : string):string {
		const hexBytes = sign.toByteArray();
		let fingerPrint = "error!";
		try{
			const digest = MessageDigest.getInstance(type);
			if(digest != null){
				const digestBytes = digest.digest(hexBytes);
				const sb = new StringBuffer()
				digestBytes.forEach((digestByte)=>{
					sb.append((Integer.toHexString(((digestByte & 0xFF) | 0x100).toInt())).substring(1, 3));
				})
				fingerPrint = sb.toString();
DCloud-yyl's avatar
DCloud-yyl 已提交
105
			}
DCloud-yyl's avatar
DCloud-yyl 已提交
106 107
			
		}catch(e : Exception){
DCloud-yyl's avatar
DCloud-yyl 已提交
108
		}
DCloud-yyl's avatar
DCloud-yyl 已提交
109 110
		return fingerPrint;
	}
DCloud-yyl's avatar
DCloud-yyl 已提交
111
}