AppUtils.ets
2.74 KB
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
import bundleManager from '@ohos.bundle.bundleManager';
import common from '@ohos.app.ability.common';
import { Logger } from './Logger';
import BuildProfile from 'BuildProfile';
const TAG: string = 'AppUtils';
/**
* 与应用相关属性或操作
*/
export class AppUtils {
private static buildVersion: string = ''
private static buildProductName: string = ''
private static buildBuildModeName: string = ''
static {
AppUtils.buildVersion = BuildProfile.BUILD_VERSION;
AppUtils.buildProductName = BuildProfile.PRODUCT_NAME;
AppUtils.buildBuildModeName = BuildProfile.BUILD_MODE_NAME;
Logger.isDebug = !AppUtils.isProductRELEASE()
}
public static isProductRELEASE() {
if (AppUtils.buildProductName == "productRELEASE" && AppUtils.buildBuildModeName == "release") {
return true
}
return false
}
// 全局应用context
public static gotApplicationContextFunc?: () => common.UIAbilityContext
/**
* 获取应用名称
* 即:人民日报
*/
static getAppName(context: common.Context): string {
// todo:获取到的是 $string:app_name
// return context.applicationInfo?.label;
return context.resourceManager.getStringByNameSync("app_name");
}
/**
* 获取应用的包名
*/
static getPackageName(context: common.Context): string {
return context.applicationInfo?.name;
}
/**
* 获取应用版本号,如:1.0.0.0
* @returns 版本号字符串
*/
static getAppVersionName(): string {
try {
let bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT)
return bundleInfo?.versionName
} catch (e) {
Logger.warn(TAG, 'get app version error:' + e?.message);
}
return "";
}
/**
* 获取应用版本编码,如:1000000
* @returns 应用版本编码
*/
static getAppVersionCode(): string {
try {
let bundleInfo = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT)
return bundleInfo?.versionCode + ""
} catch (e) {
Logger.warn(TAG, 'get app version error:' + e?.message);
}
return '';
}
static getAppChannel() {
// TODO: 待确认,暂时写死一个
return "rmrb_china_0000"
}
static getOSName() {
return "Harmony"
}
static getFingerprint(): string {
try {
let bundleInfo =
bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO);
let finger = bundleInfo.signatureInfo.fingerprint;
} catch (e) {
Logger.warn(TAG, 'get app signatureinfo error:' + e?.message);
}
return '';
}
/**
* 应用build版本,时间,如:'202405291450'
*/
static getBuildVersion(): string {
return AppUtils.buildVersion;
}
}