• 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 */
15import hilog from '@ohos.hilog';
16import Ability from '@ohos.app.ability.UIAbility'
17import Window from '@ohos.window'
18import commonEvent from '@ohos.commonEvent'
19import ErrorManager from "@ohos.app.ability.errorManager"
20let message;
21let commonEventData={
22    parameters:{
23        message:message,
24    }
25}
26
27export default class EntryAbility extends Ability {
28    onCreate(want, launchParam) {
29        globalThis.want = want;
30        globalThis.context = this.context;
31        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
32        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
33        hilog.info(0x0000, 'testTag', '%{public}s', 'want param:' + JSON.stringify(want) ?? '');
34        hilog.info(0x0000, 'testTag', '%{public}s', 'launchParam:' + JSON.stringify(launchParam) ?? '');
35    }
36
37    onDestroy() {
38        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
39        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
40    }
41
42    onWindowStageCreate(windowStage: Window.WindowStage) {
43        // Main window is created, set main page for this ability
44        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
45        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
46
47        windowStage.loadContent('pages/Index', (err, data) => {
48            if (err.code) {
49                hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.ERROR);
50                hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
51                return;
52            }
53            hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
54            hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
55        });
56    }
57
58    onWindowStageDestroy() {
59        // Main window is destroyed, release UI related resources
60        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
61        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
62    }
63
64    onForeground() {
65        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
66        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
67        let observer = undefined;
68        if (globalThis.want.action == "Acts_RegisterJsErrorCallback_0100") {
69            observer = {
70                onUnhandledException(strMsg){
71                    commonEventData.parameters.message = strMsg;
72                    console.info("====>Acts_RegisterJsErrorCallback_0100 onUnhandledException Message: " + strMsg);
73                    commonEvent.publish("ACTS_RegisterEvent_First", commonEventData, (err) => {
74                        console.info("====>Acts_RegisterJsErrorCallback_0100 publish err: " + JSON.stringify(err));
75                        setTimeout(()=>{
76                            globalThis.context.terminateSelf().then(()=>{
77                                console.info("====>Acts_RegisterJsErrorCallback_0100 rely terminateSelf end");
78                            }).catch((err)=>{
79                                console.info("====>Acts_RegisterJsErrorCallback_0100 rely terminateSelf err: " +
80                                JSON.stringify(err));
81                            });
82                        }, 100);
83                    })
84                }
85            }
86
87            ErrorManager.on("error", observer);
88            setTimeout(() => {
89                globalThis.contest.terminateSelf();
90            }, 300);
91        } else if (globalThis.want.action == "Acts_RegisterJsErrorCallback_0200") {
92            observer = {
93                onUnhandledException(strMsg: String){
94                    commonEventData.parameters.message = strMsg;
95                    console.info("====>Acts_RegisterJsErrorCallback_0200 onUnhandledException Message: " + strMsg);
96                    commonEvent.publish("ACTS_RegisterEvent_First", commonEventData, (err) => {
97                        console.info("====>Acts_RegisterJsErrorCallback_0200 onUnhandledException publish err: " +
98                            JSON.stringify(err));
99                    })
100                },
101                onException(errObject: Error){
102                    commonEventData.parameters.message = errObject;
103                    console.info("====>Acts_RegisterJsErrorCallback_0200 onException Message: " + JSON.stringify(errObject));
104                    commonEvent.publish("ACTS_RegisterEvent_Second", commonEventData, (err) => {
105                        console.info("====>Acts_RegisterJsErrorCallback_0200 onException publish err: " +
106                            JSON.stringify(err));
107                        setTimeout(()=>{
108                            globalThis.context.terminateSelf().then(()=>{
109                                console.info("====>Acts_RegisterJsErrorCallback_0200 rely terminateSelf end");
110                            }).catch((err)=>{
111                                console.info("====>Acts_RegisterJsErrorCallback_0200 rely terminateSelf err: " +
112                                    JSON.stringify(err));
113                            })
114                        }, 100)
115                    })
116                }
117            }
118            ErrorManager.on("error", observer);
119            setTimeout(() => {
120                globalThis.contest.terminateSelf();
121            }, 300);
122        } else if (globalThis.want.action == "Acts_UnregisterJsErrorCallback_0800") {
123            observer = {
124                onUnhandledException(strMsg: String){
125                    commonEventData.parameters.message = strMsg;
126                    console.info("====>Acts_UnregisterJsErrorCallback_0800 onUnhandledException Message: " + strMsg);
127                    commonEvent.publish("ACTS_RegisterEvent_First", commonEventData, (err) => {
128                        console.info("====>Acts_UnregisterJsErrorCallback_0800 onUnhandledException publish err: " +
129                        JSON.stringify(err));
130                    })
131                },
132                onException(errObject: Error){
133                    commonEventData.parameters.message = errObject;
134                    console.info("====>Acts_UnregisterJsErrorCallback_0800 onException Message: " + JSON.stringify(errObject));
135                    commonEvent.publish("ACTS_RegisterEvent_Second", commonEventData, (err) => {
136                        console.info("====>Acts_UnregisterJsErrorCallback_0800 onException publish err: " +
137                        JSON.stringify(err));
138                        setTimeout(()=>{
139                            globalThis.context.terminateSelf().then(()=>{
140                                console.info("====>Acts_UnregisterJsErrorCallback_0800 rely terminateSelf end");
141                            }).catch((err)=>{
142                                console.info("====>Acts_UnregisterJsErrorCallback_0800 rely terminateSelf err: " +
143                                JSON.stringify(err));
144                            })
145                        }, 100)
146                    })
147                }
148            }
149            ErrorManager.on("error", observer);
150            let observerId = ErrorManager.on("error", observer);
151            ErrorManager.off("error", observerId).then(()=>{
152                console.info("====>Acts_UnregisterJsErrorCallback_0800 rely off end");
153            }).catch((err)=>{
154                console.info("====>Acts_UnregisterJsErrorCallback_0800 rely off err: " +
155                JSON.stringify(err));
156            })
157            setTimeout(() => {
158                globalThis.contest.terminateSelf();
159            }, 300);
160        }
161    }
162
163    onBackground() {
164        hilog.isLoggable(0x0000, 'testTag', hilog.LogLevel.INFO);
165        hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
166    }
167}
168