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 type update from '@ohos.update';
18import type { OtaStatus } from '@ohos/common/src/main/ets/const/update_const';
19import { MAIN_ABILITY_NAME, PACKAGE_NAME, UpdateState } from '@ohos/common/src/main/ets/const/update_const';
20import { LogUtils } from '@ohos/common/src/main/ets/util/LogUtils';
21import { UpdateUtils } from '@ohos/common/src/main/ets/util/UpdateUtils';
22import { OtaUpdateManager } from '../manager/OtaUpdateManager';
23import RouterUtils from '../util/RouterUtils';
24import { DialogHelper } from './DialogHelper';
25
26const TIME_OUT_FOR_START_ABILITY = 500;
27
28/**
29 * 装饰器--弹框时,前台判断处理
30 */
31function foregroundCheck<T>() {
32  return function inner(target: unknown, propertyKey: string, descriptor: PropertyDescriptor): void {
33    const original = descriptor.value;
34    descriptor.value = function (context: common.Context, otaStatus: OtaStatus,
35      eventId?: update.EventId, ...args): void {
36      if (globalThis.AbilityStatus !== 'ON_FOREGROUND') {
37        globalThis.reNotify = true;
38        globalThis.otaStatusFromService = otaStatus;
39        globalThis.eventIdFromService = eventId;
40        LogUtils.log('foregroundCheck', 'do startMainAbilityIndex.');
41
42        // 应用在后台时,无法弹框,需杀掉ability后,重新拉起界面弹框
43        globalThis.abilityContext?.terminateSelf();
44        setTimeout(() => {
45          startMainAbilityIndex(context);
46        }, TIME_OUT_FOR_START_ABILITY);
47        return;
48      }
49      original.call(this, ...args);
50    };
51  };
52}
53
54function startMainAbilityIndex(context: common.Context): void {
55  let want = {
56    bundleName: PACKAGE_NAME,
57    abilityName: MAIN_ABILITY_NAME,
58    uri: 'pages/newVersion',
59  };
60  UpdateUtils.startAbility(context, want, null);
61}
62
63/**
64 * 重试下载动作
65 */
66const retryDownloadAction = {
67  onConfirm: (): void => {
68    OtaUpdateManager.getInstance().setUpdateState(UpdateState.CHECK_SUCCESS);
69  },
70  onCancel: (): void => {
71    OtaUpdateManager.getInstance().setUpdateState(UpdateState.CHECK_SUCCESS);
72  },
73};
74
75/**
76 * 重试安装动作
77 */
78const retryUpgradeAction = {
79  onConfirm: (): void => {
80    OtaUpdateManager.getInstance().setUpdateState(UpdateState.DOWNLOAD_SUCCESS);
81  },
82  onCancel: (): void => {
83    OtaUpdateManager.getInstance().setUpdateState(UpdateState.DOWNLOAD_SUCCESS);
84  },
85};
86
87/**
88 * 重试检测动作
89 */
90const retryCheckAction = {
91  onConfirm: (): void => {
92    RouterUtils.singletonHomePage();
93  }, onCancel: (): void => {
94    RouterUtils.singletonHomePage();
95  },
96};
97
98/**
99 * 弹框工具类
100 *
101 * @since 2022-12-05
102 */
103export class DialogUtils {
104  /**
105   * 下载空间不足弹框
106   *
107   * @param context 上下文
108   */
109  @foregroundCheck()
110  static showDownloadNotEnoughSpaceDialog(context: common.Context, otaStatus: OtaStatus,
111    eventId?: update.EventId): void {
112    LogUtils.log('DialogUtils', 'showDownloadNotEnoughSpaceDialog');
113    DialogHelper.displayNotEnoughSpaceDialog(retryDownloadAction);
114  }
115
116  /**
117   * 下载断网弹框
118   *
119   * @param context 上下文
120   */
121  @foregroundCheck()
122  static showDownloadNoNetworkDialog(context: common.Context, otaStatus: OtaStatus,
123    eventId?: update.EventId): void {
124    LogUtils.log('DialogUtils', 'showDownloadNoNetworkDialog');
125    DialogHelper.displayNoNetworkDialog();
126  }
127
128  /**
129   * 校验失败弹框
130   *
131   * @param context 上下文
132   */
133  @foregroundCheck()
134  static showVerifyFailDialog(context: common.Context, otaStatus: OtaStatus, eventId?: update.EventId): void {
135    LogUtils.log('DialogUtils', 'showVerifyFailDialog');
136    DialogHelper.displayVerifyFailDialog(retryCheckAction);
137  }
138
139  /**
140   * 下载失败默认弹框
141   *
142   * @param context 上下文
143   */
144  @foregroundCheck()
145  static showDownloadFailDialog(context: common.Context, otaStatus: OtaStatus, eventId?: update.EventId): void {
146    LogUtils.log('DialogUtils', 'showDownloadFailDialog');
147    DialogHelper.displayDownloadFailDialog(retryCheckAction);
148  }
149
150  /**
151   * 安装空间不足弹框
152   *
153   * @param context 上下文
154   */
155  @foregroundCheck()
156  static showUpgradeNotEnoughSpaceDialog(context: common.Context, otaStatus: OtaStatus,
157    eventId?: update.EventId): void {
158    LogUtils.log('DialogUtils', 'showUpgradeNotEnoughSpaceDialog');
159    DialogHelper.displayNotEnoughSpaceDialog(retryUpgradeAction);
160  }
161
162  /**
163   * 安装电量不足弹框
164   *
165   * @param context 上下文
166   */
167  @foregroundCheck()
168  static showUpgradeNotEnoughBatteryDialog(context: common.Context, otaStatus: OtaStatus,
169    eventId?: update.EventId): void {
170    LogUtils.log('DialogUtils', 'showUpgradeNotEnoughBatteryDialog');
171    DialogHelper.displayNotEnoughBatteryDialog();
172  }
173
174  /**
175   * 安装失败默认弹框
176   *
177   * @param context 上下文
178   */
179  @foregroundCheck()
180  static showUpgradeFailDialog(context: common.Context, otaStatus: OtaStatus, eventId?: update.EventId): void {
181    LogUtils.log('DialogUtils', 'showUpgradeFailDialog');
182    DialogHelper.displayUpgradeFailDialog(retryCheckAction);
183  }
184}