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 call.on('callDetailsChange', (data) => { 35 if (!data) { 36 LogUtils.i(TAG, prefixLog + "call.on registerCallStateCallback") 37 return; 38 } 39 LogUtils.i(TAG, prefixLog + "call.on registerCallStateCallback callState: " + JSON.stringify(data.callState)) 40 callBack(data); 41 }); 42 } 43 44 /** 45 * onRegister call state callback 46 */ 47 public unRegisterCallStateCallback() { 48 call.off('callDetailsChange', (data) => { 49 if (!data) { 50 LogUtils.i(TAG, prefixLog + "call.off unRegisterCallStateCallback") 51 return; 52 } 53 LogUtils.i(TAG, prefixLog + "call.off unRegisterCallStateCallback") 54 }); 55 } 56 57 /** 58 * accept call 59 * 60 * @param { number } callId - call id 61 */ 62 public acceptCall = function (callId) { 63 call.answerCall(callId).then((res) => { 64 LogUtils.i(TAG, prefixLog + "call.answerCall callId: " + JSON.stringify(callId)) 65 }).catch((err) => { 66 LogUtils.i(TAG, prefixLog + "call.answerCall catch : " + JSON.stringify(err)) 67 }) 68 } 69 70 /** 71 * reject call 72 * 73 * @param { number } callId - call id 74 * 75 * @param { boolean } isSendSms - is send sms 76 * 77 * @param { string } msg - message string 78 */ 79 public rejectCall = function (callId, isSendSms = false, msg = '') { 80 const rejectCallPromise = isSendSms ? call.rejectCall(callId, { 81 messageContent: msg 82 }) : call.rejectCall(callId); 83 rejectCallPromise.then((res) => { 84 LogUtils.i(TAG, prefixLog + "then:rejectCall") 85 }) 86 .catch((err) => { 87 LogUtils.i(TAG, prefixLog + "catch:rejectCall : " + JSON.stringify(err)) 88 }); 89 }; 90 91 /** 92 * hang up Call 93 * 94 * @param { number } callId - call id 95 * 96 * @return { Object } promise object 97 */ 98 public hangUpCall = (callId) => new Promise((resolve, reject) => { 99 call.hangUpCall(callId).then((res) => { 100 resolve(res); 101 LogUtils.i(TAG, prefixLog + "then:hangUpCall : %s" + JSON.stringify(callId)) 102 }).catch((err) => { 103 reject(err); 104 LogUtils.i(TAG, prefixLog + "catch:hangUpCall : %s" + JSON.stringify(err)) 105 }); 106 }); 107} 108 109 110