1/* 2 * Copyright (c) 2025 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'; 18import resourceManager from '@ohos.resourceManager'; 19import promptAction from '@ohos.promptAction' 20import { BusinessError } from '@ohos.base'; 21import deviceInfo from '@ohos.deviceInfo'; 22 23const TAG = '[WIFI-NI:WifiDialog]==>'; 24const DEVICE_TYPE_TV = 'tv'; 25 26const WIFI_NI_ACCEPT_EVENT: string = 'ohos.event.wifi.DIALOG_ACCEPT'; 27const WIFI_NI_REJECT_EVENT: string = 'ohos.event.wifi.DIALOG_REJECT'; 28const DIALOG_TIMEOUT_SECONDS = 30 * 1000; 29 30@Entry 31@Component 32struct dialogPlusPage { 33 @State text: string = ''; 34 private dialogType: number; 35 private customDialogComponentId: number = -1; 36 private closeDialogTaskId: number = -1; 37 private WIFI_DIALOG_P2P_INVITATION_RECEIVE: number = 4; 38 39 @Builder 40 buildDialog() { 41 Column() { 42 Text($r('app.string.p2p_wsc_pbc_dialog_title')) 43 .fontColor($r('app.color.tv_dialog_font_color')) 44 .fontSize($r('app.float.value_vp_20')) 45 .lineHeight($r('app.float.value_vp_23')) 46 .fontWeight(FontWeight.Medium) 47 .textAlign(TextAlign.Center) 48 Text(this.text) 49 .margin({ top: $r('app.float.value_vp_24') }) 50 .fontColor($r('app.color.tv_dialog_font_color')) 51 .fontSize($r('app.float.value_fp_16')) 52 .fontWeight(FontWeight.Regular) 53 .lineHeight($r('app.float.value_vp_19')) 54 .textAlign(TextAlign.Center) 55 Row({ space: $r('app.float.value_vp_24') }) { 56 Button($r('app.string.p2p_wsc_pbc_dialog_no_button')) 57 .backgroundColor($r('app.color.tv_dialog_button_background_color')) 58 .layoutWeight(1) 59 .fontColor($r('app.color.tv_dialog_font_color')) 60 .fontSize($r('app.float.value_fp_18')) 61 .fontWeight(FontWeight.Medium) 62 .borderRadius($r('app.float.value_vp_20')) 63 .onClick(() => { 64 console.info(TAG, 'user click cancel'); 65 this.setAcceptResult(this.dialogType, false); 66 }) 67 Button($r('app.string.p2p_wsc_pbc_dialog_ok_button')) 68 .layoutWeight(1) 69 .backgroundColor($r('app.color.tv_dialog_button_background_color')) 70 .fontColor($r('app.color.tv_dialog_font_color')) 71 .fontSize($r('app.float.value_fp_18')) 72 .fontWeight(FontWeight.Medium) 73 .borderRadius($r('app.float.value_vp_20')) 74 .onClick(() => { 75 console.info(TAG, 'user click trust'); 76 this.setAcceptResult(this.dialogType, true); 77 }) 78 }.margin({ top: $r('app.float.value_vp_24') }) 79 .width('100%') 80 .justifyContent(FlexAlign.SpaceBetween) 81 .alignItems(VerticalAlign.Center) 82 } 83 .width('100%') 84 .padding($r('app.float.value_vp_24')) 85 .justifyContent(FlexAlign.Center) 86 .alignItems(HorizontalAlign.Center) 87 .backgroundColor($r('app.color.tv_dialog_background_color')) 88 } 89 90 private openDialog(): void { 91 console.info(TAG, 'openDialog'); 92 promptAction.openCustomDialog({ 93 builder: () => this.buildDialog(), 94 autoCancel: false, 95 onWillDismiss: (dismissDialogAction: DismissDialogAction) => { 96 console.info(TAG, `not allow dismiss dialog by reason ${dismissDialogAction.reason}`); 97 } 98 }).then((dialogId: number) => { 99 console.info(TAG, 'dialog open success'); 100 this.customDialogComponentId = dialogId; 101 this.closeDialogTaskId = setTimeout(() => { 102 console.info(TAG, 'user not select and time out'); 103 this.setAcceptResult(this.dialogType, false); 104 }, DIALOG_TIMEOUT_SECONDS); 105 }).catch((err: BusinessError) => { 106 console.info(TAG, `fail to open dialog, err=${JSON.stringify(err)}`); 107 this.setAcceptResult(this.dialogType, false); 108 }) 109 } 110 111 aboutToAppear() { 112 console.info(TAG, 'aboutToAppear'); 113 let deviceName : string = AppStorage.get('p2pDeviceName') as string; 114 this.dialogType = AppStorage.get('wifiDialogType') as number; 115 this.text = this.getFormatString($r('app.string.p2p_wsc_pbc_dialog_text'), deviceName); 116 if (deviceInfo.deviceType == DEVICE_TYPE_TV) { 117 this.openDialog(); 118 } else { 119 AlertDialog.show( 120 { 121 title: $r('app.string.p2p_wsc_pbc_dialog_title'), 122 message: this.text, 123 autoCancel: false, 124 primaryButton: { 125 value: $r('app.string.p2p_wsc_pbc_dialog_no_button'), 126 action: () => { 127 this.setAcceptResult(this.dialogType, false); 128 } 129 }, 130 secondaryButton: { 131 value: $r('app.string.p2p_wsc_pbc_dialog_ok_button'), 132 action: () => { 133 this.setAcceptResult(this.dialogType, true); 134 } 135 } 136 } 137 ) 138 } 139 } 140 141 aboutToDisappear() { 142 let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession'); 143 if (session) { 144 session.terminateSelf(); 145 } 146 } 147 148 getFormatString(resource: Resource, subStr: string): string { 149 let result = getContext().resourceManager.getStringSync(resource.id); 150 return result.replace(new RegExp('%s', 'gm'), subStr); 151 } 152 153 private async setAcceptResult(dialogType : number, isAccept : boolean): Promise<void> { 154 console.info(TAG, 'Dialog type ${dialogType} click ${isAccept}.'); 155 clearTimeout(this.closeDialogTaskId); 156 try { 157 promptAction.closeCustomDialog(this.customDialogComponentId); 158 } catch (err) { 159 console.info(TAG, `fail to close custom dialog, err:${JSON.stringify(err)}`); 160 } 161 const options: commonEventManager.CommonEventPublishData = { 162 code: 0, 163 data: 'message', 164 subscriberPermissions: [], 165 isOrdered: true, 166 isSticky: false, 167 parameters: { 'dialogType': dialogType } 168 } 169 170 if (isAccept) { 171 commonEventManager.publish(WIFI_NI_ACCEPT_EVENT, options, (err) => { 172 if (err) { 173 console.info(TAG, 'Wifi dialog accept event publish failed .' + JSON.stringify(err)); 174 } else { 175 console.info(TAG, 'Wifi dialog accept event publish success.'); 176 } 177 }) 178 } else { 179 commonEventManager.publish(WIFI_NI_REJECT_EVENT, options, (err) => { 180 if (err) { 181 console.info(TAG, 'Wifi dialog cancel event publish failed .' + JSON.stringify(err)); 182 } else { 183 console.info(TAG, 'Wifi dialog cancel event publish success.'); 184 } 185 }) 186 } 187 188 let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession'); 189 if (session) { 190 session.terminateSelf(); 191 } 192 } 193 194 build() { 195 } 196}