1/**
2 * Copyright (c) 2024-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import bundleManager from '@ohos.bundle.bundleManager';
17import HiSysEventConstant from '../constants/HiSysEventConstant';
18import Logger from './Logger';
19import { BusinessError } from '@ohos.base';
20
21const TAG = 'SPS';
22
23class GetSelfBundleInfoUtils {
24  private versionName: string = '';
25
26  /**
27   * Obtains the version name of SecurityPrivacyCenter
28   *
29   * @returns Promise<string> versionName
30   */
31  getVersionName(): Promise<string> {
32    return new Promise<string>(async (resolve) => {
33      if (this.versionName !== '') {
34        Logger.info(TAG, `get versionName from object variable : ${this.versionName}`);
35        resolve(this.versionName)
36      } else {
37        try {
38          await bundleManager.getBundleInfo(HiSysEventConstant.BUNDLE_NAME,
39            bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT).then((data) => {
40            this.versionName = data.versionName;
41            Logger.info(TAG, `get versionName from BMS : ${this.versionName}`);
42            resolve(this.versionName);
43          }).catch((err: BusinessError) => {
44            Logger.error(TAG, `getBundleInfo failed, BusinessError: ${err.message}`);
45          })
46        } catch (err) {
47          Logger.error(TAG, `getBundleInfo failed: ${err.message}`)
48        }
49      }
50    })
51  }
52}
53
54let getSelfBundleInfoUtils = new GetSelfBundleInfoUtils();
55
56export default getSelfBundleInfoUtils as GetSelfBundleInfoUtils;