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 ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 17import Want from '@ohos.app.ability.Want'; 18import notificationManager from '@ohos.notificationManager'; 19import Base from '@ohos.base'; 20import image from '@ohos.multimedia.image'; 21import fs from '@ohos.file.fs'; 22import wantAgent from '@ohos.app.ability.wantAgent'; 23import { WantAgent } from '@ohos.app.ability.wantAgent'; 24import LogUtils from '../common/Util'; 25 26const TAG: string = '[HotSpotServiceAbility-In]==>' 27 28export default class HotSpotServiceAbility extends ServiceExtensionAbility { 29 private willPublish: boolean = false; 30 31 onCreate(want: Want): void { 32 LogUtils.i(TAG, `HotSpotServiceAbility onCreate`); 33 } 34 35 onDestroy(): void { 36 LogUtils.i(TAG, `HotSpotServiceAbility onDestroy`); 37 } 38 39 onRequest(want: Want, startId: number): void { 40 LogUtils.i(TAG, `onRequest`); 41 let operateType : number = want.parameters?.['operateType'] as number; 42 let notificationId : number = want.parameters?.['notificationId'] as number; 43 if (operateType == 1) { 44 LogUtils.i(TAG, `onRequest start notification id is:${notificationId}`); 45 this.willPublish = true; 46 this.hotSpsotNotificationStart(notificationId); 47 } else { 48 LogUtils.i(TAG, `onRequest cancel notification id is:${notificationId}`); 49 this.willPublish = false; 50 this.cancelHotSpotNotification(notificationId); 51 } 52 } 53 54 public hotSpsotNotificationStart(id: number) { 55 let file: fs.File | undefined; 56 try { 57 let file = fs.openSync('/system/etc/wifi/ic_connection_notify_hotspot_unusable.png', fs.OpenMode.READ_ONLY); 58 const imageSourceApi: image.ImageSource = image.createImageSource(file.fd); 59 imageSourceApi.createPixelMap() 60 .then((pixelmap: image.PixelMap) => { 61 LogUtils.i(TAG, `Create pixelMap success.`); 62 this.publishHotSpotWantAgentCommonEvent(pixelmap, id); 63 }) 64 .catch((error: string) => { 65 LogUtils.e(TAG, `Create pixelMap failed`); 66 }); 67 } catch (err) { 68 LogUtils.e(TAG, `Create pixelMap error: ${JSON.stringify(err)}`); 69 } finally { 70 if (file) { 71 fs.closeSync(file); 72 } 73 } 74 } 75 76 public async publishHotSpotWantAgentCommonEvent(pixelMap: image.PixelMap, id: number) { 77 let wantAgentObj: WantAgent; 78 wantAgentObj = await this.getHotSpotWantAgent(); 79 LogUtils.i(TAG, `publishHotSpotWantAgentCommonEvent-getWantAgentObj-success`); 80 await this.publishHotSpotWantAgent(wantAgentObj, pixelMap, id) 81 } 82 83 public async getHotSpotWantAgent(): Promise< WantAgent> { 84 let wantAgentObjUse: WantAgent; 85 let wantAgentInfo: wantAgent.WantAgentInfo = { 86 wants: [ 87 { 88 action: 'ohos.event.notification.wifi.TAP_ENABLE_HOTSPOT', 89 parameters: {}, 90 } 91 ], 92 actionType: wantAgent.OperationType.SEND_COMMON_EVENT, 93 requestCode: 0, 94 wantAgentFlags: [wantAgent.WantAgentFlags.CONSTANT_FLAG], 95 }; 96 wantAgentObjUse = await wantAgent.getWantAgent(wantAgentInfo); 97 LogUtils.i(TAG, `getWantAgent-wantAgentObjUse-success`); 98 return wantAgentObjUse; 99 } 100 101 public async publishHotSpotWantAgent(wantAgentObj: WantAgent, pixelmap: image.PixelMap, id: number) { 102 let templateTitle: string = 103 this.context.resourceManager.getStringSync($r('app.string.condition_netshare_hotspot_close').id); 104 let templateText: string = 105 this.context.resourceManager.getStringSync($r('app.string.stop_idle_tether_notification_message').id); 106 let buttonsTitle: string = 107 this.context.resourceManager.getStringSync($r('app.string.stop_idle_tether_notification_action_text').id); 108 let notificationRequest: notificationManager.NotificationRequest = { 109 id: id, 110 notificationSlotType: notificationManager.SlotType.SERVICE_INFORMATION, 111 content: { 112 notificationContentType: notificationManager.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT, 113 normal: { 114 title: templateTitle, 115 text: templateText 116 } 117 }, 118 notificationControlFlags: 2, 119 actionButtons: [ 120 { 121 title: buttonsTitle, wantAgent: wantAgentObj 122 } 123 ], 124 smallIcon: pixelmap 125 }; 126 LogUtils.i(TAG, `publishHotSpotWantAgent-${this.willPublish}`); 127 if (this.willPublish) { 128 notificationManager.publish(notificationRequest).then(() => { 129 LogUtils.i(TAG, `Publish wifi notification success`); 130 }).catch((err: Base.BusinessError) => { 131 LogUtils.i(TAG, `Publish wifi notification fail`); 132 }); 133 } 134 } 135 136 public async cancelHotSpotNotification(id: number) { 137 notificationManager.cancel(id).then(() => { 138 LogUtils.i(TAG, `Cancel wifi notification success`); 139 }).catch((err: Base.BusinessError) => { 140 LogUtils.i(TAG, `Cancel wifi notification fail`); 141 }); 142 } 143}