1/** 2 * Copyright (c) 2023 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 FuncBtn from './FuncBtn'; 17import InComDialog from './InComDialog'; 18import ImagePathConst from '../constant/ImagePathConst'; 19import CallStateConst from '../constant/CallStateConst'; 20import CallServiceProxy from '../../model/CallServiceProxy'; 21import sms from '@ohos.telephony.sms'; 22import resourceManager from '@ohos.resourceManager'; 23import prompt from '@system.prompt'; 24import LogUtils from '../utils/LogUtils'; 25import DefaultCallData from '../struct/TypeUtils'; 26import CallListStruct from '../struct/CallListStruct' 27import VibrationAndProximityUtils from '../utils/VibrationAndProximityUtils'; 28 29const TAG = "IncomingCom"; 30const SMS_REJECTION = `${ImagePathConst.BASE_URL}ic_public_message.svg`; 31 32/** 33 * SMS sent successfully 34 */ 35const SEND_SMS_SUCCESS = 0; 36 37/** 38 * Failed to send SMS for unknown reason 39 */ 40const SEND_SMS_FAILURE_UNKNOWN = 1; 41 42/** 43 * The wireless module for sending text messages is turned off 44 */ 45const SEND_SMS_FAILURE_RADIO_OFF = 2; 46 47/** 48 * The network sending the text message is unavailable 49 */ 50const SEND_SMS_FAILURE_SERVICE_UNAVAILABLE = 3; 51 52@Component 53export default struct IncomingCom { 54 @Link callList: Array<CallListStruct>; 55 @Link callData: DefaultCallData; 56 @Link hangup: boolean; 57 private btnList = []; 58 private msgList = []; 59 mCallServiceProxy: CallServiceProxy = null; 60 msgDialogController: CustomDialogController = new CustomDialogController 61 ({ 62 builder: InComDialog({ 63 cancel: () => { 64 }, 65 confirm: (item) => { 66 this.msgItemClick(item); 67 }, 68 cancelHandle: () => { 69 }, 70 // @ts-ignore 71 List: this.msgList 72 }), 73 cancel: () => { 74 }, 75 customStyle: true 76 }); 77 78 aboutToAppear() { 79 this.mCallServiceProxy = CallServiceProxy.getInstance(); 80 this.getMsgList(); 81 this.getBtnList(); 82 } 83 84 /** 85 * send Message slotId : card slot, destinationHost : accountNumber ,content: content 86 * 87 * @param obj 88 */ 89 private sendMessage(obj) { 90 let slotId = this.callData.accountId; 91 let destinationHost = this.callData.accountNumber 92 let contactName = this.callData.contactName 93 globalThis.calluiAbilityContext?.resourceManager.getString(obj.msg.id, (err, typeName) => { 94 LogUtils.i(TAG, "sendMessage"); 95 sms.sendMessage({ 96 slotId: slotId, 97 destinationHost: destinationHost, 98 content: typeName, 99 sendCallback: (err, sendResult) => { 100 if (err) { 101 return; 102 } 103 if (sendResult.result === SEND_SMS_SUCCESS) { 104 globalThis.calluiAbilityContext?.resourceManager.getString($r("app.string.SMS_Sent") 105 .id, (err, typeName) => { 106 if (err) { 107 return; 108 } 109 prompt.showToast({ 110 message: contactName ? typeName + `${contactName}` : typeName + `${destinationHost}`, 111 duration: 2000, 112 }); 113 }) 114 } else { 115 globalThis.calluiAbilityContext?.resourceManager.getString($r("app.string.message_Failed") 116 .id, (err, typeName) => { 117 if (err) { 118 return; 119 } 120 prompt.showToast({ 121 message: typeName, 122 duration: 2000, 123 }); 124 }) 125 } 126 } 127 }); 128 }) 129 } 130 131 private getMsgList() { 132 this.msgList = [ 133 { 134 id: 1, 135 msg: $r("app.string.backLater") 136 }, 137 { 138 id: 2, 139 msg: $r("app.string.answerThePhone") 140 }, 141 { 142 id: 3, 143 msg: $r("app.string.contactMeLater") 144 }, 145 { 146 id: 4, 147 msg: $r("app.string.beThereSoon") 148 }, 149 ]; 150 } 151 152 private getBtnList() { 153 this.btnList = [ 154 { 155 type: 'msg', 156 iconText: $r("app.string.sms"), 157 iconDefaultUrl: SMS_REJECTION, 158 iconDisableUrl: SMS_REJECTION, 159 isDisable: false 160 }, 161 ]; 162 } 163 164 /** 165 * Call rejection interface 166 * 167 * @param {string} msg - Send SMS content 168 */ 169 private msgItemClick(obj) { 170 LogUtils.i(TAG, "msgItemClick rejectCall"); 171 this.mCallServiceProxy.rejectCall(this.callData.callId, obj.msg); 172 this.sendMessage(obj); 173 } 174 175 /** 176 * Handling interface call hangup and rejection 177 */ 178 private async onReject() { 179 if (this.callList.length > 1) { 180 const incomingData = this.callList.find((v) => v.callState === CallStateConst.callStateObj.CALL_STATUS_WAITING || v.callState === CallStateConst.callStateObj.CALL_STATUS_INCOMING); 181 Object.assign(this.callData, { 182 ...incomingData 183 }); 184 } 185 const {callId, callState} = this.callData; 186 if (callState !== CallStateConst.CALL_STATUS_WAITING) { 187 this.mCallServiceProxy.rejectCall(callId); 188 LogUtils.i(TAG, "onReject this.mCallServiceProxy.rejectCall :"); 189 } else { 190 this.mCallServiceProxy.hangUpCall(callId); 191 LogUtils.i(TAG, "onReject this.mCallServiceProxy.hangUpCall :"); 192 } 193 if (this.callList.length === 1) { 194 this.hangup = true; 195 globalThis.calluiAbilityContext?.terminateSelf().then((data) => { 196 LogUtils.i(TAG, "onReject terminateSelfCallBack"); 197 }); 198 } 199 } 200 201 /** 202 * Enable the SMS reply pop-up 203 */ 204 private btnClick(type) { 205 LogUtils.i(TAG, "btnClick : %s" + JSON.stringify(type)); 206 if (type === 'msg') { 207 this.msgDialogController.open(); 208 } 209 } 210 211 /** 212 * Answer the phone interface 213 */ 214 private onAnswer() { 215 LogUtils.i(TAG, "onAnswer :"); 216 const incomingData = this.callList.find((v) => v.callState === CallStateConst.callStateObj.CALL_STATUS_WAITING || v.callState === CallStateConst.callStateObj.CALL_STATUS_INCOMING); 217 if (incomingData !== undefined) { 218 this.mCallServiceProxy.acceptCall(incomingData.callId); 219 } else { 220 this.mCallServiceProxy.acceptCall(this.callData.callId); 221 } 222 VibrationAndProximityUtils.suspendScreen(); 223 VibrationAndProximityUtils.stopVibration(); 224 } 225 226 build() { 227 GridRow({ columns: { sm: 4, md: 8, lg: 12 }, gutter: 24 }) { 228 GridCol({ span: { sm: 4, md: 6, lg: 6 }, offset: { sm: 0, md: 1, lg: 3 } }) { 229 Column() { 230 Row() { 231 ForEach(this.btnList, (item) => { 232 Column() { 233 FuncBtn({ 234 btnType: item.type, 235 iconDisableUrl: item.iconDefaultUrl, 236 iconDefaultUrl: item.iconDefaultUrl, 237 iconText: item.iconText, 238 isDisable: item.isDisable, 239 btnClick: (type) => { 240 this.btnClick(type) 241 } 242 }) 243 } 244 }) 245 } 246 .height(51) 247 248 Row() { 249 Column() { 250 Image($r("app.media.ic_public_ring_off")) 251 .width(56) 252 .height(56) 253 .onClick(() => { 254 this.onReject() 255 }) 256 }.layoutWeight(1) 257 258 Column() { 259 Image($r("app.media.ic_public_answer")) 260 .width(56) 261 .height(56) 262 .onClick(() => { 263 this.onAnswer() 264 }) 265 }.layoutWeight(1) 266 } 267 .margin({ top: 32 }) 268 } 269 } 270 } 271 .width('100%') 272 .margin({ left: 24, right: 24 }) 273 } 274} 275