1/*
2 * Copyright (c) 2024-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 UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
17import commonEventManager from '@ohos.commonEventManager';
18
19let TAG = '[AGNSS-NI:ConfirmDialog]==>';
20const AGNSS_NI_ACCEPT_EVENT: string = 'usual.event.AGNSS_NI_ACCEPT';
21const AGNSS_NI_REJECT_EVENT: string = 'usual.event.AGNSS_NI_REJECT';
22
23@Entry
24@Component
25struct dialogPlusPage {
26  @State title: string = '';
27  @State message: string = '';
28
29  aboutToAppear() {
30    console.info(TAG, 'aboutToAppear')
31    console.info(TAG, 'aboutToAppear execute ConfirmCustomDialog')
32    if (AppStorage.get('title') != null) { // SA侧传过来的参数
33      this.title = AppStorage.get('title') as string
34      console.log('title is ' + this.title)
35    }
36    if (AppStorage.get('message') != null) { // SA侧传过来的参数
37      this.message = AppStorage.get('message') as string
38      console.log('message is ' + this.message)
39    }
40    if (this.message == '') {
41      this.message = 'SUPL Service'
42    }
43
44    AlertDialog.show(
45      {
46        title: $r('app.string.ni_notify_title'),
47        message: this.message,
48        autoCancel: false,
49        primaryButton: {
50          value: $r('app.string.cancel_button'),
51          action: () => {
52            this.onCancel();
53          }
54        },
55        secondaryButton: {
56          enabled: true,
57          defaultFocus: true,
58          style: DialogButtonStyle.HIGHLIGHT,
59          value: $r('app.string.confirm_button'),
60          action: () => {
61            this.onAccept();
62          }
63        },
64      }
65    )
66  }
67
68  aboutToDisappear() {
69    let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession');
70    if (session) {
71      session.terminateSelf();
72    }
73  }
74
75  onCancel() {
76    console.info(TAG, 'Callback when the first button is clicked')
77
78    const options: commonEventManager.CommonEventPublishData = {
79      code: 0,
80      data: 'message',
81      isSticky: false,
82      parameters: { 'message': 'agnss-ni-reject' }
83    }
84
85    commonEventManager.publish(AGNSS_NI_REJECT_EVENT, options, (err) => {
86      if (err) {
87        console.info('[AGNSS-NI:ConfirmDialog]==>', '[CommonEvent] PublishCallBack err=' + JSON.stringify(err));
88      } else {
89        console.info('[AGNSS-NI:ConfirmDialog]==>', '[CommonEvent] Publish success')
90      }
91    })
92
93    let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession');
94    if (session) {
95      session.terminateSelf();
96    }
97  }
98
99  onAccept() {
100    console.info(TAG, 'Callback when the second button is clicked')
101
102    const options: commonEventManager.CommonEventPublishData = {
103      code: 0,
104      data: 'message',
105      isSticky: false,
106      parameters: { 'message': 'agnss-ni-accept' }
107    }
108
109    commonEventManager.publish(AGNSS_NI_ACCEPT_EVENT, options, (err) => {
110      if (err) {
111        console.info('[AGNSS-NI:ConfirmDialog]==>', '[CommonEvent] PublishCallBack err=' + JSON.stringify(err));
112      } else {
113        console.info('[AGNSS-NI:ConfirmDialog]==>', '[CommonEvent] Publish success')
114      }
115    })
116
117    let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession');
118    if (session) {
119      session.terminateSelf();
120    }
121  }
122
123  existApp() {
124    console.info(TAG, 'Click the callback in the blank area')
125    let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession');
126    if (session) {
127      session.terminateSelf();
128    }
129  }
130
131  build() {
132  }
133}