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