• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 router from '@ohos.router';
17import { UpdateState, UpgradeCallResult } from '@ohos/common/src/main/ets/const/update_const';
18import { StateManager, UpdateAction } from '../manager/StateManager';
19import { OtaUpdateManager } from '../manager/OtaUpdateManager';
20
21/**
22 * 一秒对应的时间(1000)
23 */
24const SECOND_INTERVAL = 1000;
25
26/**
27 * 拉起主页面的等待时间(单位秒)
28 */
29const SECONDS_FOR_PAGE = 0.1;
30
31/**
32 * 路由工具
33 *
34 * @since 2022-06-06
35 */
36namespace RouterUtils {
37  function waitForSeconds(timeInSeconds: number): Promise<void> {
38    return new Promise(resolver => {
39      setTimeout(resolver, timeInSeconds * SECOND_INTERVAL);
40    });
41  }
42
43  /**
44   * 拉起主页面,并清除其他页面
45   */
46  export async function singletonHomePage(): Promise<void> {
47    router.clear();
48    await waitForSeconds(SECONDS_FOR_PAGE);
49    router.replace({ url: 'pages/index' });
50  }
51
52  /**
53   * 打开新版本页面
54   */
55  export function openNewVersionPage(): void {
56    router.push({ url: 'pages/newVersion' });
57  }
58
59  /**
60   * 打开当前版本页面
61   */
62  export function openCurrentVersion(): void {
63    router.push({ url: 'pages/currentVersion' });
64  }
65
66  /**
67   * 清理所有页面
68   */
69  export function clearAllPage(): void {
70    router.clear();
71  }
72
73  /**
74   * 是否能够跳转新版本页面
75   *
76   * @return 是否能够跳转
77   */
78  export async function isCanToNewVersion(): Promise<boolean> {
79    return new Promise((resolve, reject) => {
80      OtaUpdateManager.getInstance().getOtaStatus().then((upgradeData) => {
81        if (upgradeData?.callResult === UpgradeCallResult.OK) {
82          if (upgradeData.data?.status === UpdateState.UPGRADING) {
83            resolve(false);
84          } else {
85            let isAllow: boolean = StateManager.isAllowExecute(upgradeData.data?.status, UpdateAction.SHOW_NEW_VERSION);
86            resolve(isAllow);
87          }
88        } else {
89          resolve(false);
90        }
91      });
92    });
93  }
94}
95
96export default RouterUtils;
97