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 ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 17import Want from '@ohos.app.ability.Want'; 18import rpc from '@ohos.rpc'; 19import NotificationManager from '@ohos.notificationManager'; 20import Notification from '@ohos.notification'; 21import Base from '@ohos.base'; 22import image from '@ohos.multimedia.image'; 23import fs from '@ohos.file.fs'; 24import wantAgent from '@ohos.app.ability.wantAgent'; 25import { WantAgent } from '@ohos.app.ability.wantAgent' 26 27const TAG: string = '[WIFI_SERVICE_NI]==>' 28 29export default class WifiServiceAbility extends ServiceExtensionAbility { 30 private WIFI_PORTAL_CONNECTED: number = 0; 31 private WIFI_PORTAL_TIMEOUT: number = 1; 32 private WIFI_PORTAL_FOUND: number = 2; 33 private WIFI_5G_AUTO_IDENTIFY_CONN_FOUND: number = 3; 34 private willPublish: boolean = false; 35 36 onCreate(want: Want): void { 37 console.info(TAG, `WifiServiceAbility onCreate`); 38 } 39 40 onDestroy(): void { 41 console.info(TAG, `WifiServiceAbility onDestroy`); 42 } 43 44 onRequest(want: Want, startId: number): void { 45 console.info(TAG, `WifiServiceAbility onRequest, want: ${JSON.stringify(want)}`); 46 let file: fs.File | undefined; 47 let operateType : number = want.parameters?.['operateType'] as number; 48 let notificationId : number = want.parameters?.['notificationId'] as number; 49 if (operateType == 1) { 50 this.willPublish = true; 51 let status : number = want.parameters?.['status'] as number; 52 let ssid : string = want.parameters?.['ssid'] as string; 53 try { 54 let file = fs.openSync('/system/etc/wifi/portal_notification.png', fs.OpenMode.READ_ONLY); 55 const imageSourceApi: image.ImageSource = image.createImageSource(file.fd); 56 imageSourceApi.createPixelMap() 57 .then((pixelmap: image.PixelMap) => { 58 console.info(TAG, `Create pixelMap success.`); 59 this.publishWantAgentCommonEvent(pixelmap, notificationId, status, ssid); 60 }) 61 .catch((error: string) => { 62 console.info(TAG, `Create pixelMap failed`); 63 }); 64 } catch (err) { 65 console.info(TAG, `Create pixelMap error: ${JSON.stringify(err)}`); 66 } finally { 67 if (file) { 68 fs.closeSync(file); 69 } 70 } 71 } else { 72 this.willPublish = false; 73 this.cancelNotification(notificationId); 74 } 75 } 76 77 publishWantAgentCommonEvent = async (pixelmap: image.PixelMap, id: number, status: number, ssid: string) => { 78 let wantAgentObj: WantAgent; 79 let wantAgentInfo: wantAgent.WantAgentInfo = { 80 wants: [ 81 { 82 action: 'ohos.event.notification.wifi.TAP_NOTIFICATION', 83 parameters: { 'notificationId': id }, 84 } 85 ], 86 actionType: wantAgent.OperationType.SEND_COMMON_EVENT, 87 requestCode: 0, 88 wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG], 89 }; 90 91 wantAgent.getWantAgent(wantAgentInfo, (err: Base.BusinessError, data: WantAgent) => { 92 if (err) { 93 console.info(TAG, `Failed to get want agent. Code is ${err.code}, message is ${err.message}`); 94 return; 95 } 96 console.info(TAG, 'Succeeded in getting want agent.'); 97 wantAgentObj = data; 98 this.publishWantAgent(wantAgentObj, pixelmap, id, status, ssid); 99 }); 100 } 101 102 publishWantAgent = async (wantAgentObj: WantAgent, pixelmap: image.PixelMap, id: number, status: number, ssid: string) => { 103 let templateTitle: string = ''; 104 let templateText: string = ''; 105 switch (status) { 106 case this.WIFI_PORTAL_CONNECTED: 107 templateTitle = this.getFormatString($r('app.string.wifi_portal_connected_notify_title'), ssid); 108 templateText = this.context.resourceManager.getStringSync($r('app.string.wifi_portal_connected_notify_text').id); 109 break; 110 case this.WIFI_PORTAL_TIMEOUT: 111 templateTitle = this.context.resourceManager.getStringSync($r('app.string.wifi_portal_connect_timeout_notify_title').id); 112 templateText = this.getFormatString($r('app.string.wifi_portal_connect_timeout_notify_text'), ssid); 113 break; 114 case this.WIFI_PORTAL_FOUND: 115 templateTitle = this.context.resourceManager.getStringSync($r('app.string.wifi_portal_found_notify_title').id); 116 templateText = this.getFormatString($r('app.string.wifi_portal_found_notify_text'), ssid); 117 break; 118 case this.WIFI_5G_AUTO_IDENTIFY_CONN_FOUND: 119 templateTitle = this.context.resourceManager.getStringSync($r('app.string.wifipro_better_network_pop_up_title').id); 120 templateText = this.getFormatString($r('app.string.wifipro_better_network_notify_text'), ssid); 121 break; 122 default: 123 break; 124 } 125 let notificationRequest: NotificationManager.NotificationRequest = { 126 id: id, 127 content: { 128 contentType: Notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 129 normal: { 130 title: templateTitle, 131 text: templateText 132 } 133 }, 134 slotType: Notification.SlotType.SERVICE_INFORMATION, 135 notificationControlFlags: 2, 136 isUnremovable: true, 137 wantAgent: wantAgentObj, 138 smallIcon: pixelmap 139 }; 140 141 if (this.willPublish) { 142 NotificationManager.publish(notificationRequest).then(() => { 143 console.info(TAG, 'Publish wifi notification success'); 144 }).catch((err: Base.BusinessError) => { 145 console.error(TAG, 'Publish wifi notification fail'); 146 }); 147 } 148 } 149 150 cancelNotification = async (id: number) => { 151 NotificationManager.cancel(id).then(() => { 152 console.info(TAG, 'Cancel wifi notification success'); 153 }).catch((err: Base.BusinessError) => { 154 console.error(TAG, 'Cancel wifi notification fail'); 155 }); 156 } 157 158 getFormatString(resource: Resource, subStr: string): string { 159 let result = this.context.resourceManager.getStringSync(resource.id); 160 return result.replace(new RegExp('%s', 'gm'), subStr); 161 } 162}