• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022-2023 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 bundle from '@ohos.bundle.bundleManager'
17import prompt from '@ohos.promptAction'
18import Logger from '../util/Logger'
19
20const TAG: string = 'BundleController'
21
22let bundleName = 'ohos.samples.stagemodel'
23let bundleFlags = 0
24let userId = 100
25
26export default class BundleController {
27
28  // 获取有关当前应用程序的信息,对应FA模型的getApplicationInfo()
29  getApplicationInfo() {
30    bundle.getApplicationInfo(bundleName, bundleFlags, userId)
31      .then((data) => {
32        Logger.info(TAG, `getApplicationInfo successful. Data: ${JSON.stringify(data)}`)
33        prompt.showToast({
34          message: `getApplicationInfo successful`
35        })
36      }).catch((error) => {
37      Logger.error(TAG, `getApplicationInfo. Cause: ${JSON.stringify(error)}`)
38      prompt.showToast({
39        message: `getApplicationInfo. Cause: ${JSON.stringify(error)}`
40      })
41    })
42  }
43
44  // 指示应用程序的实体类型,对应FA模型中AppContext的getAppType()
45  entityType() {
46    bundle.getApplicationInfo(bundleName, bundleFlags, userId)
47      .then((data) => {
48        let digital = data.description
49        if (digital === null) {
50          Logger.info(TAG, 'digital is null')
51          return
52        }
53        Logger.info(TAG, `caller onRelease is called: ${digital}`)
54        prompt.showToast({
55          message: `caller onRelease is called sucess`
56        })
57      }).catch((error) => {
58        Logger.info(TAG, `caller onRelease is error: ${error}`)
59      })
60  }
61
62  // 应用程序的进程,如果用户未设置,则值等于bundleName
63  process() {
64    bundle.getApplicationInfo(bundleName, bundleFlags, userId)
65      .then((data) => {
66        let process = data.process
67        if (process === null) {
68          Logger.info(TAG, 'process is null')
69          return
70        }
71        Logger.info(TAG, `caller onRelease is called: ${process}`)
72        prompt.showToast({
73          message: `caller onRelease is called sucess`
74        })
75      }).catch((error) => {
76        Logger.info(TAG, `caller onRelease is error: ${error}`)
77      })
78  }
79
80  // 获取应用程序的本地根目录,对应FA模型的getOrCreateLocalDir()
81  entryDir() {
82    bundle.getApplicationInfo(bundleName, bundleFlags, userId)
83      .then((data) => {
84        let entryDirData = data.codePath
85        if (entryDirData === null) {
86          Logger.info(TAG, 'codePath is null')
87          return
88        }
89        Logger.info(TAG, `codePath: ${entryDirData}`)
90        prompt.showToast({
91          message: `codePath: ${entryDirData}`
92        })
93      }).catch((error) => {
94        Logger.info(TAG, `caller onRelease is error: ${error}`)
95      })
96  }
97
98  // 获取应用的版本信息,对应FA模型的getAppVersionInfo()
99  getDispatcherVersion() {
100    let bundleName = "ohos.samples.stagemodel"
101    let bundleFlags = bundle.BundleFlag.GET_BUNDLE_INFO_WITH_HAP_MODULE | bundle.BundleFlag.GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY;
102
103    try {
104      bundle.getBundleInfo(bundleName, bundleFlags, (err, data) => {
105        if (err) {
106          Logger.error(TAG, `getDispatcherVersion() failed. Cause: ${JSON.stringify(err.message)}`)
107          prompt.showToast({
108            message: `Operation failed. Cause: ${JSON.stringify(err)}`
109          })
110        } else {
111          prompt.showToast({
112            message: `Operation successful. Data: ${JSON.stringify(data.targetVersion)}`
113          })
114        }
115      })
116    } catch (err) {
117      Logger.error(TAG, `getDispatcherVersion() failed. Cause: ${JSON.stringify(err.message)}`)
118      prompt.showToast({
119        message: `Operation failed. Cause: ${JSON.stringify(err)}`
120      })
121    }
122  }
123
124  // 获取有关当前能力的信息,对应FA模型的getElementName()
125  getAbilityInfo() {
126    let bundleName = "ohos.samples.stagemodel"
127    let bundleFlags = bundle.BundleFlag.GET_BUNDLE_INFO_WITH_HAP_MODULE | bundle.BundleFlag.GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY | bundle.BundleFlag.GET_BUNDLE_INFO_WITH_ABILITY;
128
129    try {
130      bundle.getBundleInfo(bundleName, bundleFlags, (err, data) => {
131        if (err) {
132          Logger.error(TAG, `getAbilityInfo() failed. Cause: ${JSON.stringify(err.message)}`)
133          prompt.showToast({
134            message: `Operation failed. Cause: ${JSON.stringify(err)}`
135          })
136        } else {
137          prompt.showToast({
138            message: `Operation successful. Data: ${JSON.stringify(data)}`
139          })
140        }
141      })
142    } catch (err) {
143      Logger.error(TAG, `getAbilityInfo() failed. Cause: ${JSON.stringify(err.message)}`)
144      prompt.showToast({
145        message: `Operation failed. Cause: ${JSON.stringify(err)}`
146      })
147    }
148  }
149}