• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-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 FeatureAbility from '@ohos.ability.featureAbility';
17import Log from '../Log';
18
19const TAG = 'FeatureAbilityManager';
20
21export default class FeatureAbilityManager {
22
23  openAbility(tag, want) {
24    Log.showDebug(TAG, `openAbility from: ${tag}`));
25    let result = FeatureAbility.startAbility(want)
26      .then(data =>
27    Log.showInfo(TAG, `tag: ${tag} promise then: ${JSON.stringify(data)}`))
28      .catch(error =>
29    Log.showError(TAG, `tag: ${tag} promise catch: ${JSON.stringify(error)}`));
30    Log.showInfo(TAG, `tag: ${tag} openAbility result: ${result}`);
31  }
32
33  getAbilityWant(listener) {
34    FeatureAbility.getWant((err, data) => {
35      Log.showInfo(TAG, `getAbilityWant callBack err: ${JSON.stringify(err)}`);
36      if (err.code !== 0) {
37        Log.showError(TAG, `failed to getAbilityWant because ${err.message}`);
38        return;
39      } else {
40        if(listener != null && listener != undefined) {
41          listener(data);
42        }
43      }
44    });
45  }
46
47  finishAbilityWithResult(abilityResult) {
48    FeatureAbility.finishWithResult(abilityResult, (err, data) => {
49      if (err.code !== 0) {
50        Log.showError(TAG, `failed to finishWithResult because ${JSON.stringify(err)}`);
51        return;
52      }
53      FeatureAbilityManager.finishAbility();
54    });
55  }
56
57  static finishAbility() {
58    FeatureAbility.terminateAbility((err, data) => {
59      if (err.code !== 0) {
60        Log.showError(TAG, `failed to finishAbility because ${JSON.stringify(err)}`);
61        return;
62      }
63      Log.showDebug(TAG, ` finishAbility callback err: ${JSON.stringify(err)} data:${data}`);
64    });
65  }
66}