199da06d0Sopenharmony_ci/** 299da06d0Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 399da06d0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 499da06d0Sopenharmony_ci * you may not use this file except in compliance with the License. 599da06d0Sopenharmony_ci * You may obtain a copy of the License at 699da06d0Sopenharmony_ci * 799da06d0Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 899da06d0Sopenharmony_ci * 999da06d0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1099da06d0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1199da06d0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1299da06d0Sopenharmony_ci * See the License for the specific language governing permissions and 1399da06d0Sopenharmony_ci * limitations under the License. 1499da06d0Sopenharmony_ci */ 1599da06d0Sopenharmony_ci 1699da06d0Sopenharmony_ci/** 1799da06d0Sopenharmony_ci * @file: Call management 1899da06d0Sopenharmony_ci */ 1999da06d0Sopenharmony_ciimport CallDataManager from './CallDataManager'; 2099da06d0Sopenharmony_ciimport CallUtils from '../common/utils/CallUtils'; 2199da06d0Sopenharmony_ciimport Utils from '../common/utils/utils'; 2299da06d0Sopenharmony_ciimport CallServiceProxy from './CallServiceProxy'; 2399da06d0Sopenharmony_ciimport LogUtils from '../common/utils/LogUtils' 2499da06d0Sopenharmony_ciimport call from '@ohos.telephony.call'; 2599da06d0Sopenharmony_ciimport CallStateConst from '../common/constant/CallStateConst'; 2699da06d0Sopenharmony_ciimport DefaultCallData from '../common/struct/TypeUtils'; 2799da06d0Sopenharmony_ciimport CallListStruct from '../common/struct/CallListStruct'; 2899da06d0Sopenharmony_ciimport VibrationAndProximityUtils from '../common/utils/VibrationAndProximityUtils'; 2999da06d0Sopenharmony_ci 3099da06d0Sopenharmony_ciconst TAG = 'CallManager'; 3199da06d0Sopenharmony_ciconst TIMING = 1000; 3299da06d0Sopenharmony_ci 3399da06d0Sopenharmony_ci/** 3499da06d0Sopenharmony_ci * class CallManager 3599da06d0Sopenharmony_ci */ 3699da06d0Sopenharmony_ciexport default class CallManager { 3799da06d0Sopenharmony_ci private callData: DefaultCallData = new DefaultCallData(); 3899da06d0Sopenharmony_ci private callList: Array<CallListStruct> = []; 3999da06d0Sopenharmony_ci private timer = null; 4099da06d0Sopenharmony_ci public callTimeList = []; 4199da06d0Sopenharmony_ci private ctx = []; 4299da06d0Sopenharmony_ci private sendNotificationHandle: Function; 4399da06d0Sopenharmony_ci private mCallDataManager: CallDataManager; 4499da06d0Sopenharmony_ci private mCallServiceProxy: CallServiceProxy; 4599da06d0Sopenharmony_ci private mUtils: Utils; 4699da06d0Sopenharmony_ci private diffSeconds 4799da06d0Sopenharmony_ci private mTimeMeter; 4899da06d0Sopenharmony_ci private isServiceConnected: boolean = false; 4999da06d0Sopenharmony_ci 5099da06d0Sopenharmony_ci public static getInstance(): CallManager { 5199da06d0Sopenharmony_ci if (globalThis.callManager == null) { 5299da06d0Sopenharmony_ci globalThis.callManager = new CallManager(); 5399da06d0Sopenharmony_ci } 5499da06d0Sopenharmony_ci return globalThis.callManager; 5599da06d0Sopenharmony_ci } 5699da06d0Sopenharmony_ci 5799da06d0Sopenharmony_ci private constructor() { 5899da06d0Sopenharmony_ci this.mCallServiceProxy = CallServiceProxy.getInstance(); 5999da06d0Sopenharmony_ci this.mUtils = Utils.getInstance(); 6099da06d0Sopenharmony_ci this.timer = null; 6199da06d0Sopenharmony_ci this.mCallDataManager = CallDataManager.getInstance(); 6299da06d0Sopenharmony_ci } 6399da06d0Sopenharmony_ci 6499da06d0Sopenharmony_ci init(ctx) { 6599da06d0Sopenharmony_ci this.callData = ctx.callData; 6699da06d0Sopenharmony_ci this.ctx = ctx; 6799da06d0Sopenharmony_ci this.callTimeList = ctx.callTimeList; 6899da06d0Sopenharmony_ci this.mCallDataManager?.init(ctx.callData, ctx.callList, ctx.callTimeList); 6999da06d0Sopenharmony_ci this.openTimer(); 7099da06d0Sopenharmony_ci this.sendNotificationHandle = (arg) => arg; 7199da06d0Sopenharmony_ci this.initCallData(); 7299da06d0Sopenharmony_ci } 7399da06d0Sopenharmony_ci 7499da06d0Sopenharmony_ci /** 7599da06d0Sopenharmony_ci * init CallData 7699da06d0Sopenharmony_ci */ 7799da06d0Sopenharmony_ci private initCallData() { 7899da06d0Sopenharmony_ci if (globalThis.abilityWant && globalThis.abilityWant.parameters && ('callState' in globalThis.abilityWant.parameters)) { 7999da06d0Sopenharmony_ci if (!this.isServiceConnected) { 8099da06d0Sopenharmony_ci globalThis.calluiAbilityContext?.terminateSelf().then((data) => { 8199da06d0Sopenharmony_ci LogUtils.i(TAG, 'calluiAbility terminateSelf because service disconnected'); 8299da06d0Sopenharmony_ci }); 8399da06d0Sopenharmony_ci return; 8499da06d0Sopenharmony_ci } 8599da06d0Sopenharmony_ci let callData = this.getCallDataFromWant(globalThis.abilityWant.parameters); 8699da06d0Sopenharmony_ci this.update(callData); 8799da06d0Sopenharmony_ci LogUtils.i(TAG, 'initCallData featureAbility.getWant :') 8899da06d0Sopenharmony_ci } else { 8999da06d0Sopenharmony_ci this.mCallServiceProxy.publish({ 9099da06d0Sopenharmony_ci key: 'getInitCallData', 9199da06d0Sopenharmony_ci params: [] 9299da06d0Sopenharmony_ci }); 9399da06d0Sopenharmony_ci } 9499da06d0Sopenharmony_ci } 9599da06d0Sopenharmony_ci 9699da06d0Sopenharmony_ci setServiceConnected(isServiceConnected: boolean): void { 9799da06d0Sopenharmony_ci this.isServiceConnected = isServiceConnected; 9899da06d0Sopenharmony_ci } 9999da06d0Sopenharmony_ci 10099da06d0Sopenharmony_ci /** 10199da06d0Sopenharmony_ci * get callData from want parameters 10299da06d0Sopenharmony_ci */ 10399da06d0Sopenharmony_ci private getCallDataFromWant(parameters) { 10499da06d0Sopenharmony_ci return Object.assign({}, { 10599da06d0Sopenharmony_ci accountId: parameters.accountId, 10699da06d0Sopenharmony_ci accountNumber: parameters.accountNumber, 10799da06d0Sopenharmony_ci callId: parameters.callId, 10899da06d0Sopenharmony_ci callState: parameters.callState, 10999da06d0Sopenharmony_ci callType: parameters.callType, 11099da06d0Sopenharmony_ci conferenceState: parameters.conferenceState, 11199da06d0Sopenharmony_ci isEcc: parameters.isEcc, 11299da06d0Sopenharmony_ci startTime: parameters.startTime, 11399da06d0Sopenharmony_ci videoState: parameters.videoState}); 11499da06d0Sopenharmony_ci } 11599da06d0Sopenharmony_ci 11699da06d0Sopenharmony_ci /** 11799da06d0Sopenharmony_ci * update callData callBack 11899da06d0Sopenharmony_ci * 11999da06d0Sopenharmony_ci * @param { Object } callData -Object 12099da06d0Sopenharmony_ci */ 12199da06d0Sopenharmony_ci async update(callData) { 12299da06d0Sopenharmony_ci LogUtils.i(TAG, 'update calldata:') 12399da06d0Sopenharmony_ci if (this.callData != undefined && this.callData.callId === callData.callId) { 12499da06d0Sopenharmony_ci const { callState } = this.callData; 12599da06d0Sopenharmony_ci if (callState === 6 && this.callList.length === 1) { 12699da06d0Sopenharmony_ci globalThis.calluiAbilityContext?.terminateSelf().then((data) => { 12799da06d0Sopenharmony_ci LogUtils.i(TAG, 'calluiAbility terminateSelf because service disconnected'); 12899da06d0Sopenharmony_ci }); 12999da06d0Sopenharmony_ci // remove Proximity Listener 13099da06d0Sopenharmony_ci VibrationAndProximityUtils.wakeupScreen(); 13199da06d0Sopenharmony_ci VibrationAndProximityUtils.stopVibration(); 13299da06d0Sopenharmony_ci return; 13399da06d0Sopenharmony_ci } 13499da06d0Sopenharmony_ci } 13599da06d0Sopenharmony_ci this.callData = callData; 13699da06d0Sopenharmony_ci this.mCallDataManager.update(callData); 13799da06d0Sopenharmony_ci call.formatPhoneNumber(callData.accountNumber, (err, data) => { 13899da06d0Sopenharmony_ci if (err) { 13999da06d0Sopenharmony_ci LogUtils.i(TAG, 'updata calldata formatPhoneNumber err:' + JSON.stringify(err)); 14099da06d0Sopenharmony_ci } else if (data === undefined) { 14199da06d0Sopenharmony_ci AppStorage.SetOrCreate('AccountNumber', callData.accountNumber); 14299da06d0Sopenharmony_ci } else { 14399da06d0Sopenharmony_ci LogUtils.i(TAG, 'updata calldata formatPhoneNumber success:' + JSON.stringify(data)); 14499da06d0Sopenharmony_ci AppStorage.SetOrCreate('AccountNumber', data); 14599da06d0Sopenharmony_ci } 14699da06d0Sopenharmony_ci }); 14799da06d0Sopenharmony_ci CallUtils.isEmergencyPhoneNumber(callData.accountNumber); 14899da06d0Sopenharmony_ci LogUtils.i(TAG, 'update :'); 14999da06d0Sopenharmony_ci } 15099da06d0Sopenharmony_ci 15199da06d0Sopenharmony_ci /** 15299da06d0Sopenharmony_ci * update call time list 15399da06d0Sopenharmony_ci */ 15499da06d0Sopenharmony_ci updateCallTimeList() { 15599da06d0Sopenharmony_ci if (!this.mCallDataManager.hasActiveCall()) { 15699da06d0Sopenharmony_ci LogUtils.i(TAG, 'no active calls to update'); 15799da06d0Sopenharmony_ci return; 15899da06d0Sopenharmony_ci } 15999da06d0Sopenharmony_ci 16099da06d0Sopenharmony_ci this.callTimeList = AppStorage.Get('CallTimeList'); 16199da06d0Sopenharmony_ci this.callTimeList.forEach((item, i) => { 16299da06d0Sopenharmony_ci if (this.mCallDataManager.isActiveCall(item.callId)) { 16399da06d0Sopenharmony_ci item.endTimestamp = new Date().valueOf(); 16499da06d0Sopenharmony_ci const diffSeconds = item.endTimestamp - item.startTimestamp; 16599da06d0Sopenharmony_ci this.diffSeconds = diffSeconds 16699da06d0Sopenharmony_ci item.callTime = this.mUtils.formatTime(diffSeconds); 16799da06d0Sopenharmony_ci this.callTimeList.splice(i, 1, { 16899da06d0Sopenharmony_ci ...item, 16999da06d0Sopenharmony_ci }); 17099da06d0Sopenharmony_ci AppStorage.SetOrCreate('CallTimeList', this.callTimeList); 17199da06d0Sopenharmony_ci } 17299da06d0Sopenharmony_ci }); 17399da06d0Sopenharmony_ci this.mTimeMeter = setTimeout(() => { 17499da06d0Sopenharmony_ci this.updateCallTimeList(); 17599da06d0Sopenharmony_ci }, 1000 - this.diffSeconds % 1000); 17699da06d0Sopenharmony_ci 17799da06d0Sopenharmony_ci } 17899da06d0Sopenharmony_ci 17999da06d0Sopenharmony_ci /** 18099da06d0Sopenharmony_ci * open timer 18199da06d0Sopenharmony_ci * 18299da06d0Sopenharmony_ci * @param { Function } callBack - add updateCallTimeList callBack 18399da06d0Sopenharmony_ci */ 18499da06d0Sopenharmony_ci openTimer() { 18599da06d0Sopenharmony_ci this.timer = setInterval(() => { 18699da06d0Sopenharmony_ci this.updateCallTimeList(); 18799da06d0Sopenharmony_ci if (this.callData.callState === CallStateConst.CALL_STATUS_ACTIVE && this.callList.length === 1) { 18899da06d0Sopenharmony_ci clearInterval(this.timer); 18999da06d0Sopenharmony_ci } 19099da06d0Sopenharmony_ci }, TIMING); 19199da06d0Sopenharmony_ci } 19299da06d0Sopenharmony_ci 19399da06d0Sopenharmony_ci /** 19499da06d0Sopenharmony_ci * clear timer 19599da06d0Sopenharmony_ci */ 19699da06d0Sopenharmony_ci clearTimer() { 19799da06d0Sopenharmony_ci clearInterval(this.timer); 19899da06d0Sopenharmony_ci } 19999da06d0Sopenharmony_ci} 200