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: Voice call API interface call 18 */ 19 20import call from '@ohos.telephony.call'; 21import LogUtils from '../common/utils/LogUtils'; 22 23const prefixLog = 'callui service:@ohos.telephony.call:'; 24const TAG = 'TelephonyApi'; 25 26export default class TelephonyApi { 27 28 /** 29 * register call state callback 30 * 31 * @param { Function } callBack - inject an Function 32 */ 33 public registerCallStateCallback(callBack) { 34 try { 35 call.on('callDetailsChange', (data) => { 36 if (!data) { 37 LogUtils.i(TAG, prefixLog + 'call.on registerCallStateCallback') 38 return; 39 } 40 LogUtils.i(TAG, prefixLog + 'call.on registerCallStateCallback callState: ' + JSON.stringify(data.callState)) 41 callBack(data); 42 }); 43 } catch (err) { 44 LogUtils.i(TAG, 'call.on registerCallStateCallback catch:' + JSON.stringify(err)); 45 } 46 } 47 48 /** 49 * onRegister call state callback 50 */ 51 public unRegisterCallStateCallback() { 52 try { 53 call.off('callDetailsChange', (data) => { 54 if (!data) { 55 LogUtils.i(TAG, prefixLog + 'call.off unRegisterCallStateCallback') 56 return; 57 } 58 LogUtils.i(TAG, prefixLog + 'call.off unRegisterCallStateCallback') 59 }); 60 } catch (err) { 61 LogUtils.i(TAG, 'call.off unRegisterCallStateCallback catch:' + JSON.stringify(err)); 62 } 63 } 64 65 /** 66 * accept call 67 * 68 * @param { number } callId - call id 69 */ 70 public acceptCall = function (callId) { 71 call.answerCall(callId).then((res) => { 72 LogUtils.i(TAG, prefixLog + 'call.answerCall callId: ' + JSON.stringify(callId)) 73 }).catch((err) => { 74 LogUtils.i(TAG, prefixLog + 'call.answerCall catch : ' + JSON.stringify(err)) 75 }) 76 } 77 78 /** 79 * reject call 80 * 81 * @param { number } callId - call id 82 * 83 * @param { boolean } isSendSms - is send sms 84 * 85 * @param { string } msg - message string 86 */ 87 public rejectCall = function (callId, isSendSms = false, msg = '') { 88 const rejectCallPromise = isSendSms ? call.rejectCall(callId, { 89 messageContent: msg 90 }) : call.rejectCall(callId); 91 rejectCallPromise.then((res) => { 92 LogUtils.i(TAG, prefixLog + 'then:rejectCall') 93 }) 94 .catch((err) => { 95 LogUtils.i(TAG, prefixLog + 'catch:rejectCall : ' + JSON.stringify(err)) 96 }); 97 }; 98 99 /** 100 * hang up Call 101 * 102 * @param { number } callId - call id 103 * 104 * @return { Object } promise object 105 */ 106 public hangUpCall = (callId) => new Promise((resolve, reject) => { 107 call.hangUpCall(callId).then((res) => { 108 resolve(res); 109 LogUtils.i(TAG, prefixLog + 'then:hangUpCall : %s' + JSON.stringify(callId)) 110 }).catch((err) => { 111 reject(err); 112 LogUtils.i(TAG, prefixLog + 'catch:hangUpCall : %s' + JSON.stringify(err)) 113 }); 114 }); 115} 116 117 118