• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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
16/**
17 * This module provides the capability of app recovery.
18 * You can use this capability to save state and restart the application
19 * which let end user continue their workflow when app error occurs.
20 * This api support restart the app when js crash or app freeze occurs currently.
21 * @syscap SystemCapability.Ability.AbilityRuntime.Core
22 * @since 9
23 */
24declare namespace appRecovery {
25    /**
26     * The flag that determines when to restart you app.
27     * @enum { number }
28     * @syscap SystemCapability.Ability.AbilityRuntime.Core
29     * @since 9
30     */
31    enum RestartFlag {
32        /**
33         * No restart restrictions.
34         */
35        ALWAYS_RESTART = 0,
36
37        /**
38         * Restart if current app process encounter uncaught js/ts/ets exception.
39         */
40        RESTART_WHEN_JS_CRASH = 0x0001,
41
42        /**
43         * Restart if the main thread of current app process block more than 6 seconds.
44         */
45        RESTART_WHEN_APP_FREEZE = 0x0002,
46
47        /**
48         * Do not restart in any scenario.
49         */
50        NO_RESTART = 0xFFFF,
51    }
52
53    /**
54     * The flag that determines when to save ability state.
55     * When start saving ability state, the { ohos.app.ability.UiAbility.onSaveState } will be called and
56     * the page stack of current ability will be saved automatically.
57     * @enum { number }
58     * @syscap SystemCapability.Ability.AbilityRuntime.Core
59     * @since 9
60     */
61    enum SaveOccasionFlag {
62        /**
63         * Saving ability state when an error occurs.
64         * The error in current situation has the same semantic with { errorManager } defines
65         * which means the state that the application cannot continue to work but allows developer to handle.
66         */
67        SAVE_WHEN_ERROR = 0x0001,
68
69        /**
70         * Saving ability state when ability is in background.
71         */
72        SAVE_WHEN_BACKGROUND = 0x0002,
73    }
74
75    /**
76     * The flag that determines how to save the ability state.
77     * @enum { number }
78     * @syscap SystemCapability.Ability.AbilityRuntime.Core
79     * @since 9
80     */
81    enum SaveModeFlag {
82        /**
83         * Save state to file immediately.
84         */
85        SAVE_WITH_FILE = 0x0001,
86
87        /**
88         * Keep state in memory and flush to file when error occurs or { restartApp } is invoked.
89         */
90        SAVE_WITH_SHARED_MEMORY = 0x0002,
91    }
92
93    /**
94     * Enable appRecovery function.
95     * @param { RestartFlag } The flag that determines the restart cases of your app, default value is { ALWAYS_RESTART }.
96     * @param { SaveOccasionFlag } The flag that determines when to save ability state, default value is { SAVE_WHEN_ERROR }.
97     * @param { SaveModeFlag } The flag that determines how to save the ability state, default value is { SAVE_WITH_FILE }.
98     * @syscap SystemCapability.Ability.AbilityRuntime.Core
99     * @StageModelOnly
100     * @since 9
101     */
102    function enableAppRecovery(restart?: RestartFlag, saveOccasion?: SaveOccasionFlag, saveMode?: SaveModeFlag) : void;
103
104    /**
105     * Restart current process and launch the first ability(the entry ability in most cases) of current process.
106     * The previous saved state will be filled in the { want.wantParams } of { UIAbility.onCreate } interface.
107     * and the { param } of { UIAbility.onCreate } will be set to APP_RECOVERY.
108     * @syscap SystemCapability.Ability.AbilityRuntime.Core
109     * @StageModelOnly
110     * @since 9
111     */
112    function restartApp(): void;
113
114    /**
115     * Actively save application state.
116     * The ability framework will call { UIAbility.onSaveState } of first launched ability and
117     * persist state as { saveOccasion } flag from { enableAppRecovery } interface.
118     * @returns true if save data successfully, otherwise false.
119     * @syscap SystemCapability.Ability.AbilityRuntime.Core
120     * @StageModelOnly
121     * @since 9
122     */
123    function saveAppState(): boolean;
124}
125
126export default appRecovery;