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'; 18import resourceManager from '@ohos.resourceManager'; 19 20let TAG = '[WIFI-NI:WifiDialog]==>'; 21const WIFI_NI_ACCEPT_EVENT: string = 'ohos.event.wifi.DIALOG_ACCEPT'; 22const WIFI_NI_REJECT_EVENT: string = 'ohos.event.wifi.DIALOG_REJECT'; 23const TIMEOUT_NO_ACTION: number = 10 * 1000; 24 25@Entry 26@Component 27struct dialogPlusPage { 28 @State title: string = ''; 29 @State primaryButtonValue: string = ''; 30 @State secondaryButtonValue: string = ''; 31 private WIFI_DIALOG_CDD: number = 0; 32 private WIFI_DIALOG_THREE_VAP: number = 1; 33 private WIFI_DIALOG_CANDIDATE_CONNECT: number = 2; 34 private WIFI_DIALOG_5G_AUTO_IDENTIFY_CONN: number = 3; 35 private WIFI_DIALOG_P2P_WSC_PBC: number = 4; 36 private WIFI_DIALOG_SETTINGS_5G_AUTO_IDENTIFY_CONN: number = 5; 37 private noActionTimer: number | undefined = undefined; 38 private noAction: boolean = true; 39 40 aboutToAppear() { 41 console.info(TAG, 'aboutToAppear') 42 if (AppStorage.get('wifiDialogType') != null) { 43 let type : number = AppStorage.get('wifiDialogType') as number 44 this.clearNoActionTimer() 45 switch (type) { 46 case this.WIFI_DIALOG_CDD: 47 this.title = getContext().resourceManager.getStringSync($r('app.string.wifi_cdd_notify_title').id); 48 this.primaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_cdd_notify_no_button').id); 49 this.secondaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_cdd_notify_yes_button').id); 50 break; 51 case this.WIFI_DIALOG_THREE_VAP: 52 this.title = getContext().resourceManager.getStringSync($r('app.string.wifi_three_vap_notify_title').id); 53 this.primaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_three_vap_notify_no_button').id); 54 this.secondaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_three_vap_notify_yes_button').id); 55 break; 56 case this.WIFI_DIALOG_CANDIDATE_CONNECT: 57 let targetSsid : string = AppStorage.get('targetSsid') as string; 58 this.title = this.getFormatString($r('app.string.wifi_candidate_connect_notify_title'), targetSsid); 59 this.primaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_candidate_connect_notify_no_button').id); 60 this.secondaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_candidate_connect_notify_yes_button').id); 61 this.setNoActionTimer() 62 break; 63 case this.WIFI_DIALOG_5G_AUTO_IDENTIFY_CONN: 64 case this.WIFI_DIALOG_SETTINGS_5G_AUTO_IDENTIFY_CONN: 65 let ssid : string = AppStorage.get('wifi5gSsid') as string; 66 this.title = this.getFormatString($r('app.string.wifipro_auto_connect_dialog_context'), ssid); 67 this.primaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifipro_auto_connect_dialog_no_button').id); 68 this.secondaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifipro_auto_connect_dialog_ok_button').id); 69 break; 70 default: 71 break; 72 } 73 AlertDialog.show( 74 { 75 title: '', 76 message: this.title, 77 textStyle: { wordBreak: WordBreak.BREAK_WORD }, 78 autoCancel: false, 79 primaryButton: { 80 value: this.primaryButtonValue, 81 action: () => { 82 this.onCancel(type); 83 } 84 }, 85 secondaryButton: { 86 value: this.secondaryButtonValue, 87 action: () => { 88 this.onAccept(type); 89 } 90 } 91 } 92 ) 93 console.info(TAG, 'dialog show success') 94 } 95 } 96 97 getFormatString(resource: Resource, subStr: string): string { 98 let result = getContext().resourceManager.getStringSync(resource.id); 99 return result.replace(new RegExp('%s', 'gm'), subStr); 100 } 101 102 aboutToDisappear() { 103 let type : number = AppStorage.get('wifiDialogType') as number 104 if (type === this.WIFI_DIALOG_CANDIDATE_CONNECT && this.noAction) { 105 this.candidateConnectWithNoAction() 106 } 107 108 this.clearNoActionTimer() 109 110 let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession'); 111 if (session) { 112 session.terminateSelf(); 113 } 114 } 115 116 setNoActionTimer(): void { 117 console.info(TAG, 'start no action timer.') 118 this.noActionTimer = setTimeout(() => { 119 let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession'); 120 if (session) { 121 session.terminateSelf(); 122 } 123 }, TIMEOUT_NO_ACTION); 124 } 125 126 clearNoActionTimer(): void { 127 if (this.noActionTimer !== undefined) { 128 clearTimeout(this.noActionTimer); 129 this.noActionTimer = undefined; 130 } 131 } 132 133 candidateConnectWithNoAction(): void { 134 console.info(TAG, 'Wifi candidate dialog no action notify.') 135 136 const options: commonEventManager.CommonEventPublishData = { 137 code: 0, 138 data: 'message', 139 subscriberPermissions: [], 140 isOrdered: true, 141 isSticky: false, 142 parameters: { 'dialogType': this.WIFI_DIALOG_CANDIDATE_CONNECT, 'noAction': true } 143 } 144 145 commonEventManager.publish(WIFI_NI_REJECT_EVENT, options, (err) => { 146 if (err) { 147 console.info(TAG, 'Wifi candidate dialog no action event publish failed .' + JSON.stringify(err)); 148 } else { 149 console.info(TAG, 'Wifi candidate dialog no action event publish success.'); 150 } 151 }) 152 } 153 154 onCancel(type :number) { 155 console.info(TAG, 'Wifi dialog cancel clicked.') 156 this.noAction = false 157 158 const options: commonEventManager.CommonEventPublishData = { 159 code: 0, 160 data: 'message', 161 subscriberPermissions: [], 162 isOrdered: true, 163 isSticky: false, 164 parameters: { 'dialogType': type } 165 } 166 167 commonEventManager.publish(WIFI_NI_REJECT_EVENT, options, (err) => { 168 if (err) { 169 console.info(TAG, 'Wifi dialog cancel event publish failed .' + JSON.stringify(err)); 170 } else { 171 console.info(TAG, 'Wifi dialog cancel event publish success.'); 172 } 173 }) 174 175 let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession'); 176 if (session) { 177 session.terminateSelf(); 178 } 179 } 180 181 onAccept(type : number) { 182 console.info(TAG, 'Wifi dialog accept clicked.') 183 this.noAction = false 184 185 const options: commonEventManager.CommonEventPublishData = { 186 code: 0, 187 data: 'message', 188 subscriberPermissions: [], 189 isOrdered: true, 190 isSticky: false, 191 parameters: { 'dialogType': type } 192 } 193 194 commonEventManager.publish(WIFI_NI_ACCEPT_EVENT, options, (err) => { 195 if (err) { 196 console.info(TAG, 'Wifi dialog accept event publish failed .' + JSON.stringify(err)); 197 } else { 198 console.info(TAG, 'Wifi dialog accept event publish success.'); 199 } 200 }) 201 202 let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession'); 203 if (session) { 204 session.terminateSelf(); 205 } 206 } 207 208 build() { 209 } 210}