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 */ 15import { HiLog } from '../../../../../../common'; 16import commonEvent from '@ohos.commonEventManager'; 17import { PhoneNumber } from '../../../../../../feature/phonenumber'; 18import telephonySim from '@ohos.telephony.sim'; 19 20const TAG = 'MissedCallManager' 21 22class MissedCallManager { 23 requestMissedCallAction(action, data: any) { 24 if ('notification.event.message' == action) { 25 if (data && data.phoneNumber) { 26 PhoneNumber.fromString(data.phoneNumber).sendMessage(); 27 } 28 } else if ('notification.event.dialBack' == action) { 29 if (data && data.phoneNumber) { 30 this.autoCall(data.phoneNumber) 31 } 32 } 33 HiLog.i(TAG, `requestMissedCallAction action: ${action}`); 34 this.cancelNotification(data); 35 } 36 37 private async autoCall(phoneNumber: string) { 38 HiLog.i(TAG, 'autoCall:' + phoneNumber) 39 let readySimCount: number = 0; 40 let readySim = -1; 41 for (let i = 0; i < telephonySim.getMaxSimCount(); i++) { 42 try { 43 const state = await telephonySim.getSimState(i); 44 if (state) { 45 if (this.isSimReady(state)) { 46 readySimCount++; 47 readySim = i; 48 } 49 } 50 } catch (err) { 51 HiLog.e(TAG, `autoCall, ${i} error: ${JSON.stringify(err.message)}`); 52 } 53 } 54 if (readySimCount == 1 && readySim != -1) { 55 PhoneNumber.fromString(phoneNumber).dial({ 56 accountId: readySim, 57 }) 58 } 59 } 60 61 private isSimReady(state) { 62 return state == telephonySim.SimState.SIM_STATE_READY || state == telephonySim.SimState.SIM_STATE_LOADED; 63 } 64 65 cancelNotification(data?: any) { 66 commonEvent.publish("contact.event.CANCEL_MISSED", { 67 bundleName: 'com.ohos.contacts', 68 parameters: { 69 missedCallData: data 70 } 71 }, (err) => { 72 HiLog.w(TAG, JSON.stringify(err)) 73 }) 74 } 75} 76 77export const missedCallManager: MissedCallManager = new MissedCallManager();