1/** 2 * Copyright (c) 2022 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 16/** 17 * @file: notification manager 18 */ 19 20import wantAgent from '@ohos.wantAgent'; 21import notification from '@ohos.notification'; 22import commonEvent from '@ohos.commonEvent'; 23import callStateConst from '../common/constant/CallStateConst'; 24import LogUtils from '../common/utils/LogUtils'; 25import Constants from '../common/utils/Constants'; 26import GlobalThisHelper from '../common/utils/GlobalThisHelper'; 27 28const TAG = "NotificationManager"; 29const ID = 0; 30 31const notificationRequest = { 32 content: { 33 contentType: notification.ContentType.NOTIFICATION_CONTENT_LONG_TEXT, 34 longText: { 35 title: '', 36 text: '', 37 longText: '', 38 briefText: '', 39 expandedTitle: '' 40 } 41 }, 42 id: ID, 43 wantAgent: null, 44 actionButtons: [], 45 slotType: notification.SlotType.OTHER_TYPES, 46 deliveryTime: new Date().getTime() 47}; 48 49const textMap = 50 { 51 'answer': $r('app.string.answer'), 52 'reject': $r('app.string.reject'), 53 'hangUp': $r('app.string.hangUp'), 54 'mute': $r('app.string.mute'), 55 'callHold': $r('app.string.callHold'), 56 }; 57 58export default class NotificationManager { 59 /** 60 * Send notification 61 * 62 * @param { Object } text - text info 63 * 64 * @param { Object } callData - call data 65 * 66 * @param { FUnction } $t - i18n format 67 */ 68 async sendNotification(callData) { 69 const {callState, accountNumber, contactName, callId} = callData; 70 const actionBtnKeys = this.getMapObj(callState) || []; 71 const {START_ABILITY, SEND_COMMON_EVENT} = wantAgent.OperationType; 72 const wantAgentObj = await this.getWantAgent(callData, START_ABILITY); 73 let titleName = ' '; 74 let expandedName = ' '; 75 if (contactName) { 76 titleName = contactName; 77 expandedName = accountNumber; 78 } else { 79 titleName = accountNumber; 80 expandedName = ' '; 81 } 82 notificationRequest.wantAgent = wantAgentObj; 83 notificationRequest.actionButtons = []; 84 if (actionBtnKeys.length) { 85 for (const key of actionBtnKeys) { 86 const data = { 87 callId, btnType: key 88 }; 89 const wantAgentObj = await this.getWantAgent(data, SEND_COMMON_EVENT); 90 GlobalThisHelper.get<any>(Constants.GLOBALTHIS_CONTEXT)?.resourceManager.getString(textMap[key].id, (error, value) => { 91 if (error != null) { 92 LogUtils.i(TAG, "sendNotification getResourceManager getString err"); 93 } else { 94 LogUtils.i(TAG, "sendNotification getResourceManager getString value" + value); 95 notificationRequest.actionButtons.push({ 96 title: value, 97 wantAgent: wantAgentObj 98 }); 99 Object.assign(notificationRequest.content.longText, { 100 title: titleName, 101 text: expandedName, 102 expandedTitle: titleName, 103 longText: expandedName 104 }); 105 notification.publish(notificationRequest).then((data) => { 106 LogUtils.i(TAG, "sendNotification publish success") 107 }).catch((err) => { 108 LogUtils.i(TAG, "sendNotification public err" + JSON.stringify(err)) 109 }); 110 } 111 }); 112 } 113 } 114 LogUtils.i(TAG, "sendNotification end :") 115 } 116 117 /** 118 * Get call status 119 * @param callState 120 */ 121 getMapObj(callState) { 122 if (callState === callStateConst.CALL_STATUS_INCOMING) { 123 return ['answer', 'reject'] 124 } 125 if (callState === callStateConst.CALL_STATUS_ACTIVE) { 126 return ['hangUp'] 127 } 128 if (callState === callStateConst.CALL_STATUS_HOLDING) { 129 return ['hangUp'] 130 } 131 if (callState === callStateConst.CALL_STATUS_ALERTING) { 132 return ['hangUp'] 133 } 134 if (callState === callStateConst.CALL_STATUS_DIALING) { 135 return ['hangUp'] 136 } 137 if (callState === callStateConst.CALL_STATUS_WAITING) { 138 return ['answer', 'reject'] 139 } 140 } 141 142 /** 143 * get want agent 144 * 145 * @param { object } data - call data 146 * 147 * @param { number } operationType - type 148 */ 149 getWantAgent(data, operationTypes) { 150 return wantAgent.getWantAgent({ 151 wants: [{ 152 deviceId: '', 153 bundleName: Constants.CALL_BUNDLE_NAME, 154 abilityName: Constants.CALL_ABILITY_NAME, 155 uri: '', 156 type: 'phone', 157 action: 'callui.event.click', 158 parameters: data, 159 entities: [] 160 }], 161 requestCode: 0, 162 operationType: operationTypes, 163 wantAgentFlags: [wantAgent.WantAgentFlags.ONE_TIME_FLAG] 164 }); 165 } 166 167 /** 168 * Cancel notice 169 */ 170 cancelNotification() { 171 LogUtils.i(TAG, "cancelNotification") 172 notification.cancel(ID).then((res) => { 173 LogUtils.i(TAG, "notify.cancel res data : %s" + JSON.stringify(res)) 174 }).catch((err) => { 175 LogUtils.i(TAG, "notify.cancel err data : %s" + JSON.stringify(err)) 176 }); 177 } 178 179 /** 180 * send capsule notification 181 * 182 * @param {Object} callData - call data 183 * 184 * @param {boolean} isBackground - is background 185 */ 186 sendCapsuleNotification(callData, isBackground) { 187 LogUtils.i(TAG, "sendCapsuleNotification isBackground : " + JSON.stringify(isBackground)) 188 LogUtils.i(TAG, "sendCapsuleNotification callData.startTime :") 189 const {callState, startTime} = callData; 190 let commonEventPublishData = { 191 bundleName: 'com.ohos.systemui', 192 isOrdered: false, 193 subscriberPermissions: ["ohos.permission.GET_TELEPHONY_STATE"], 194 data: JSON.stringify({ 195 callState, 196 startTime: startTime * 1000, 197 isBackground, 198 wantBundleName: Constants.CALL_BUNDLE_NAME, 199 wantAbilityName: Constants.CALL_ABILITY_NAME 200 }) 201 } 202 203 commonEvent.publish('CAPSULE_EVENT_CALL_UI', commonEventPublishData, (err, data) => { 204 if (err) { 205 LogUtils.i(TAG, "sendCapsuleNotification publish launcher err:" + JSON.stringify(err)) 206 } else { 207 LogUtils.i(TAG, "sendCapsuleNotification publish launcher success:" + JSON.stringify(data)) 208 } 209 }) 210 } 211}