• 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.showInfo(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)}, openAbility result: ${result}`));
30  }
31
32  getAbilityWant(listener) {
33    FeatureAbility.getWant((err, data) => {
34      Log.showDebug(TAG, `getAbilityWant callBack err: ${JSON.stringify(err)} data: ${JSON.stringify(data)}`);
35      if (err.code !== 0) {
36        Log.showError(TAG, `failed to getAbilityWant because ${err.message}`);
37        return;
38      } else {
39        if(listener != null && listener != undefined) {
40          listener(data);
41        }
42      }
43    });
44  }
45
46  finishAbilityWithResult(abilityResult) {
47    FeatureAbility.finishWithResult(abilityResult, (err, data) => {
48      if (err.code !== 0) {
49        Log.showError(TAG, `failed to finishWithResult because ${JSON.stringify(err)}`);
50        return;
51      }
52      FeatureAbilityManager.finishAbility();
53    });
54  }
55
56  static finishAbility() {
57    FeatureAbility.terminateAbility((err, data) => {
58      if (err.code !== 0) {
59        Log.showError(TAG, `failed to finishAbility because ${JSON.stringify(err)}`);
60        return;
61      }
62      Log.showInfo(TAG, ` finishAbility callback: data:${data}`);
63    });
64  }
65}