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