1/*
2 * Copyright (c) 2024 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 abilityManager from '@ohos.app.ability.abilityManager';
17import type { BusinessError } from '@ohos.base';
18import UIExtensionAbility from '@ohos.app.ability.UIExtensionAbility';
19import wantConstant from '@ohos.app.ability.wantConstant';
20import type Want from '@ohos.app.ability.Want';
21import type UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
22
23const TAG = 'AssertFaultDialog_UIExtension';
24const TEXT_DETAIL = 'assertFaultDialogDetail';
25const DEBUG_ASSERT_RESULT = 'assertResult';
26
27export default class UiExtAbility extends UIExtensionAbility {
28  storage: LocalStorage;
29  sessionId: string;
30
31  onCreate(): void {
32    console.info(TAG, 'onCreate');
33  }
34
35  onForeground(): void {
36    console.info(TAG, 'onForeground');
37  }
38
39  onBackground(): void {
40    console.info(TAG, 'onBackground');
41  }
42
43  onSessionCreate(want: Want, session: UIExtensionContentSession): void {
44    try {
45      this.sessionId = want.parameters[wantConstant.Params.ASSERT_FAULT_SESSION_ID] as string,
46      this.storage = new LocalStorage(
47        {
48          'session': session,
49          'textDetail' : want.parameters[TEXT_DETAIL]
50        });
51    } catch (exception) {
52      console.error('Failed to register callback. Code: ' + JSON.stringify(exception));
53    }
54    session.loadContent('pages/assertFaultDialog', this.storage);
55    session.setWindowBackgroundColor('#00000000');
56  }
57
58  onDestroy(): void {
59    console.info(TAG, 'onDestroy');
60  }
61
62  onSessionDestroy(session: UIExtensionContentSession): void {
63    console.info(TAG, 'onSessionDestroy');
64    let assertResult = AppStorage.get<abilityManager.UserStatus>(DEBUG_ASSERT_RESULT);
65    if (assertResult === undefined) {
66      assertResult = abilityManager.UserStatus.ASSERT_TERMINATE;
67    }
68    try {
69      abilityManager.notifyDebugAssertResult(this.sessionId, assertResult).then(() => {
70        console.log(TAG, 'notifyDebugAssertResult success.');
71      }).catch((err: BusinessError) => {
72        console.error(TAG, `notifyDebugAssertResult failed, error: ${JSON.stringify(err)}`);
73      });
74    } catch (error) {
75      console.error(TAG, `try notifyDebugAssertResult failed, error: ${JSON.stringify(error)}`);
76    }
77  }
78};
79