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 16/** 17 * @file: User list component 18 */ 19 20import CallStateConst from '../constant/CallStateConst'; 21import CallServiceProxy from '../../model/CallServiceProxy'; 22import LogUtils from '../utils/LogUtils'; 23import DefaultCallData from '../struct/TypeUtils'; 24import CallListStruct from '../struct/CallListStruct' 25import CallTimeListStruct from '../struct/CallTimeListStruct' 26 27const TAG = "CallList"; 28 29@Component 30export default struct CallList { 31 @Link callList: Array<CallListStruct>; 32 @Link callData: DefaultCallData; 33 @StorageLink("CallTimeList") callTimeList: Array<CallTimeListStruct> = []; 34 private mCallStateConst: CallStateConst; 35 private mCallServiceProxy: CallServiceProxy; 36 37 public aboutToAppear(): void { 38 LogUtils.i(TAG, "aboutToAppear"); 39 this.mCallStateConst = new CallStateConst(); 40 this.mCallServiceProxy = CallServiceProxy.getInstance(); 41 } 42 43 /** 44 * Three-way call list 45 * 46 * @return {boolean} - Three-way call list 47 */ 48 getCallList() { 49 let arr = this.callList.map((item) => { 50 const obj = {}; 51 return Object.assign({}, { 52 ...obj, 53 ...item 54 }); 55 }); 56 return arr.filter((v) => v.callState !== CallStateConst.CALL_STATUS_WAITING); 57 } 58 59 /** 60 * Phone number display 61 * 62 * @return {string} Phone number 63 */ 64 public getContactName(item) { 65 return (item.contactName ? item.contactName : item.accountNumber) || $r('app.string.unknownNumber'); 66 } 67 68 /** 69 * Phone number display 70 * 71 * @return {string} Phone number 72 */ 73 public getCallTime(item) { 74 let callTimeObj = this.callTimeList.find((o) => o.callId === item.callId); 75 return callTimeObj ? callTimeObj.callTime : null; 76 } 77 78 /** 79 * Call status 80 * 81 * @return {number} - callState 82 */ 83 private callState() { 84 return this.callData.callState; 85 } 86 87 /** 88 * Hang up call 89 * 90 * @param {number} callId - callId 91 */ 92 public onHangUp(callId) { 93 LogUtils.i(TAG, "onHangUp : " + callId); 94 this.mCallServiceProxy.hangUpCall(callId); 95 if (this.callList.length === 1) { 96 globalThis.calluiAbilityContext?.terminateSelf().then((data) => { 97 LogUtils.i(TAG, "onHangUp terminateSelfCallBack"); 98 }); 99 } 100 } 101 102 /** 103 * UnHold call 104 * 105 * @param {number} callId - callId 106 */ 107 public onUnHold(callId) { 108 LogUtils.i(TAG, "onUnHold : " + callId); 109 110 this.getCallList().forEach((item) => { 111 if (item.callState === CallStateConst.CALL_STATUS_HOLDING) { 112 this.mCallServiceProxy.unHoldCall(item.callId); 113 return; 114 } 115 }); 116 } 117 118 getInComingCallState() { 119 let incomingState = false; 120 this.callList.forEach((v) => { 121 if (v.callState === CallStateConst.callStateObj.CALL_STATUS_WAITING || v.callState === CallStateConst.callStateObj.CALL_STATUS_INCOMING) { 122 incomingState = true; 123 } 124 }); 125 LogUtils.i(TAG, "getInComingCallState incomingState:" + JSON.stringify(incomingState)); 126 return incomingState; 127 } 128 129 build() { 130 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) { 131 List() { 132 ForEach(this.getCallList(), (item) => { 133 ListItem() { 134 Column() { 135 Row() { 136 Flex({ justifyContent: FlexAlign.SpaceBetween, alignItems: ItemAlign.Center }) { 137 Text(this.getContactName(item)) 138 .fontColor('#FFFFFF') 139 .fontSize(16) 140 .height(21) 141 .lineHeight(19) 142 143 Row() { 144 if (item.callState === CallStateConst.callStateObj.CALL_STATUS_ACTIVE) { 145 Text(this.getCallTime(item)) 146 .fontSize(14) 147 .fontColor('#FFFFFF') 148 .height(19) 149 .lineHeight(19) 150 .opacity(0.60) 151 } else { 152 Text(CallStateConst.callStateTextMap[item.callState]) 153 .fontSize(14) 154 .fontColor('#FFFFFF') 155 .height(19) 156 .lineHeight(19) 157 .opacity(0.60) 158 } 159 160 if (this.getInComingCallState()) { 161 Image($r("app.media.ic_hangup_list")) 162 .width(30) 163 .height(30) 164 .onClick(() => { 165 this.onHangUp(item.callId); 166 }) 167 .margin({ left: 16 }) 168 } 169 } 170 } 171 } 172 .onClick(() => { 173 this.onUnHold(item.callId); 174 }) 175 .height(64) 176 } 177 } 178 }) 179 } 180 .divider({ strokeWidth: 1, color: $r('app.color.divider_calllist') }) 181 .margin({ left: 24, right: 24 }) 182 .width("100%") 183 .listDirection(Axis.Vertical) 184 185 Divider() 186 .color($r('app.color.divider_calllist')) 187 .strokeWidth(1) 188 } 189 .height(128) 190 } 191}