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'; 23 24@Entry 25@Component 26struct dialogPlusPage { 27 @State title: string = ''; 28 @State primaryButtonValue: string = ''; 29 @State secondaryButtonValue: string = ''; 30 private WIFI_DIALOG_CDD: number = 0; 31 private WIFI_DIALOG_THREE_VAP: number = 1; 32 private WIFI_DIALOG_CANDIDATE_CONNECT: number = 2; 33 private WIFI_DIALOG_5G_AUTO_IDENTIFY_CONN: number = 3; 34 35 aboutToAppear() { 36 console.info(TAG, 'aboutToAppear') 37 if (AppStorage.get('wifiDialogType') != null) { 38 let type : number = AppStorage.get('wifiDialogType') as number 39 switch (type) { 40 case this.WIFI_DIALOG_CDD: 41 this.title = getContext().resourceManager.getStringSync($r('app.string.wifi_cdd_notify_title').id); 42 this.primaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_cdd_notify_no_button').id); 43 this.secondaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_cdd_notify_yes_button').id); 44 break; 45 case this.WIFI_DIALOG_THREE_VAP: 46 this.title = getContext().resourceManager.getStringSync($r('app.string.wifi_three_vap_notify_title').id); 47 this.primaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_three_vap_notify_no_button').id); 48 this.secondaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_three_vap_notify_yes_button').id); 49 break; 50 case this.WIFI_DIALOG_CANDIDATE_CONNECT: 51 this.title = getContext().resourceManager.getStringSync($r('app.string.wifi_candidate_connect_notify_title').id); 52 this.primaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_candidate_connect_notify_no_button').id); 53 this.secondaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifi_candidate_connect_notify_yes_button').id); 54 break; 55 case this.WIFI_DIALOG_5G_AUTO_IDENTIFY_CONN: 56 let ssid : string = AppStorage.get('wifi5gSsid') as string; 57 this.title = this.getFormatString($r('app.string.wifipro_auto_connect_dialog_context'), ssid); 58 this.primaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifipro_auto_connect_dialog_no_button').id); 59 this.secondaryButtonValue = getContext().resourceManager.getStringSync($r('app.string.wifipro_auto_connect_dialog_ok_button').id); 60 break; 61 default: 62 break; 63 } 64 AlertDialog.show( 65 { 66 title: '', 67 message: this.title, 68 autoCancel: false, 69 primaryButton: { 70 value: this.primaryButtonValue, 71 action: () => { 72 this.onCancel(type); 73 } 74 }, 75 secondaryButton: { 76 value: this.secondaryButtonValue, 77 action: () => { 78 this.onAccept(type); 79 } 80 } 81 } 82 ) 83 console.info(TAG, 'dialog show success') 84 } 85 } 86 87 getFormatString(resource: Resource, subStr: string): string { 88 let result = getContext().resourceManager.getStringSync(resource.id); 89 return result.replace(new RegExp('%s', 'gm'), subStr); 90 } 91 92 aboutToDisappear() { 93 let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession'); 94 if (session) { 95 session.terminateSelf(); 96 } 97 } 98 99 onCancel(type :number) { 100 console.info(TAG, 'Wifi dialog cancel clicked.') 101 102 const options: commonEventManager.CommonEventPublishData = { 103 code: 0, 104 data: 'message', 105 subscriberPermissions: [], 106 isOrdered: true, 107 isSticky: false, 108 parameters: { 'dialogType': type } 109 } 110 111 commonEventManager.publish(WIFI_NI_REJECT_EVENT, options, (err) => { 112 if (err) { 113 console.info(TAG, 'Wifi dialog cancel event publish failed .' + JSON.stringify(err)); 114 } else { 115 console.info(TAG, 'Wifi dialog cancel event publish success.'); 116 } 117 }) 118 119 let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession'); 120 if (session) { 121 session.terminateSelf(); 122 } 123 } 124 125 onAccept(type : number) { 126 console.info(TAG, 'Wifi dialog accept clicked.') 127 128 const options: commonEventManager.CommonEventPublishData = { 129 code: 0, 130 data: 'message', 131 subscriberPermissions: [], 132 isOrdered: true, 133 isSticky: false, 134 parameters: { 'dialogType': type } 135 } 136 137 commonEventManager.publish(WIFI_NI_ACCEPT_EVENT, options, (err) => { 138 if (err) { 139 console.info(TAG, 'Wifi dialog accept event publish failed .' + JSON.stringify(err)); 140 } else { 141 console.info(TAG, 'Wifi dialog accept event publish success.'); 142 } 143 }) 144 145 let session = AppStorage.get<UIExtensionContentSession>('ConfirmSession'); 146 if (session) { 147 session.terminateSelf(); 148 } 149 } 150 151 build() { 152 } 153}