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 type common from '@ohos.app.ability.common';
17import {
18  Action,
19  PACKAGE_NAME,
20  MAIN_ABILITY_NAME
21} from '@ohos/common/src/main/ets/const/update_const';
22import { LogUtils } from '@ohos/common/src/main/ets/util/LogUtils';
23import { UpdateUtils } from '@ohos/common/src/main/ets/util/UpdateUtils';
24import RouterUtils from '../util/RouterUtils';
25
26/**
27 * 日志TAG
28 */
29const TAG = 'NotificationManager';
30
31/**
32 * 通知点击事件管理类
33 *
34 * @since 2022-06-05
35 */
36export class NotificationManager {
37  /**
38   * 处理通知点击动作
39   *
40   * @param action 具体动作
41   */
42  static async handleAction(action: string, context: common.Context): Promise<boolean> {
43    switch (action) {
44      case Action.NOTIFICATION_CHECK:
45        this.handleCheckAction(context);
46        return true;
47      case Action.NOTIFICATION_DETAIL:
48        await this.handleDetailAction(context);
49        return true;
50      default:
51        return false;
52    }
53  }
54
55  private static handleCheckAction(context: common.Context): void {
56    LogUtils.log(TAG, 'handleCheckAction');
57    this.startAbility('pages/index', context);
58  }
59
60  private static async handleDetailAction(context: common.Context): Promise<void> {
61    LogUtils.log(TAG, 'handleDetailAction');
62    if (await RouterUtils.isCanToNewVersion()) {
63      this.startAbility('pages/newVersion', context);
64    } else {
65      this.startAbility('pages/index', context);
66    }
67  }
68
69  public static async startToNewVersion(context: common.Context): Promise<void> {
70    if (await RouterUtils.isCanToNewVersion()) {
71      this.startAbility('pages/newVersion', context);
72    }
73  }
74
75  private static startAbility(uri: string, context: common.Context): void {
76    let want = {
77      bundleName: PACKAGE_NAME,
78      abilityName: MAIN_ABILITY_NAME,
79      uri: uri
80    };
81    let options = {
82      windowMode: 0,
83      displayId: 2
84    };
85    UpdateUtils.startAbility(context, want, options);
86  }
87}