• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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'
17import prompt from '@ohos.prompt'
18import Logger from '../util/Logger'
19
20const TAG: string = 'BundleController'
21
22let bundleName = "com.example.myapplication"
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('ohos.samples.stagemodel', 0, (error, data) => {
47      let digital = data.entityType
48      if (digital === null || error) {
49        Logger.info(TAG, `caller onRelease is error: ${error}`)
50        return
51      }
52      Logger.info(TAG, `caller onRelease is called: ${digital}`)
53      prompt.showToast({
54        message: `caller onRelease is called sucess`
55      })
56    })
57  }
58
59  // 应用程序的进程,如果用户未设置,则值等于bundleName
60  process() {
61    bundle.getApplicationInfo('ohos.samples.stagemodel', 0, (error, data) => {
62      let processData = data.process
63      if (processData === null || error) {
64        Logger.info(TAG, `caller onRelease is called error: ${error}`)
65        return
66      }
67      Logger.info(TAG, `caller onRelease is called: ${processData}`)
68      prompt.showToast({
69        message: `caller onRelease is called sucess`
70      })
71    })
72  }
73
74  // 获取应用程序的本地根目录,对应FA模型的getOrCreateLocalDir()
75  entryDir() {
76    bundle.getApplicationInfo('ohos.samples.stagemodel', 0, (error, data) => {
77      let entryDirData = data.entryDir
78      if (entryDirData === null || error) {
79        Logger.info(TAG, `codePath error: ${error}`)
80        return
81      }
82      Logger.info(TAG, `codePath: ${entryDirData}`)
83      prompt.showToast({
84        message: `codePath: ${entryDirData}`
85      })
86    })
87  }
88
89  // 获取应用的版本信息,对应FA模型的getAppVersionInfo()
90  getDispatcherVersion() {
91    bundle.getDispatcherVersion()
92    Logger.info(TAG, `getDispatcherVersion: ${JSON.stringify(bundle.getDispatcherVersion())}`)
93    prompt.showToast({
94      message: `getDispatcherVersion success`
95    })
96  }
97
98  // 获取有关当前能力的信息,对应FA模型的getElementName()
99  getAbilityInfo() {
100    let bundleName = "ohos.samples.stagemodel"
101    let abilityName = "TestAbility"
102    bundle.getAbilityInfo(bundleName, abilityName)
103      .then((data) => {
104        Logger.info(TAG, `Operation successful. Data: ${JSON.stringify(data)}`)
105        prompt.showToast({
106          message: `Operation successful. Data: ${JSON.stringify(data)}`
107        })
108      }).catch((error) => {
109      Logger.error(TAG, `Operation failed. Cause: ${JSON.stringify(error)}`)
110      prompt.showToast({
111        message: `Operation failed. Cause: ${JSON.stringify(error)}`
112      })
113    })
114  }
115}