1/*
2 * Copyright (c) 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 update from '@ohos.update';
17import { LogUtils } from './LogUtils';
18
19/**
20 * 日志TAG
21 */
22const TAG = 'UpdateUtils';
23
24/**
25 * 接口工具
26 *
27 * @since 2022-06-06
28 */
29export namespace UpdateUtils {
30  /**
31   * 获取changelog信息
32   *
33   * @param componentDescriptions 新版本更新日志集合
34   * @param componentId 组件id
35   * @return changelog
36   */
37  export function obtainDescription(componentDescriptions: Array<update.ComponentDescription>, componentId: string): string {
38    if (!componentDescriptions || componentId == null) {
39      return '';
40    }
41    let descArray: update.ComponentDescription[] = componentDescriptions;
42    for (let index = 0; index < descArray.length; index++) {
43      if (componentId === descArray[index]?.componentId) {
44        let description = descArray[index]?.descriptionInfo?.content;
45        let descriptionType = descArray[index]?.descriptionInfo?.descriptionType;
46        if (descriptionType != update.DescriptionType.CONTENT || description == null) {
47          description = '';
48        }
49        return description;
50      }
51    }
52    return '';
53  }
54  /**
55   * 启动Ability
56   *
57   * @param context 要启动Ability的context
58   * @param want 要启动Ability的want
59   * @param options 配置项
60   */
61  export function startAbility(context: any, want, options): void {
62    if (!context || !want) {
63      LogUtils.error(TAG, 'Failed to start ability with error: context or want is null.');
64      return;
65    }
66    context.startAbility(want, options).then((data) => {
67      LogUtils.info(TAG, 'Succeed to start ability with data: ' + JSON.stringify(data));
68    }).catch((error) => {
69      LogUtils.error(TAG, 'Failed to start ability with error: ' + JSON.stringify(error));
70    });
71  }
72
73  /**
74   * 接口调用结果判断
75   *
76   * @param err 返回信息
77   * @param return 接口调用结果
78   */
79  export function isSuccessCallback(result: unknown, err: any): boolean {
80    return result && !err;
81  }
82}