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 */ 15import { HiLog } from '../../../../../common'; 16import { MissedCallService } from '../../../../../feature/call'; 17import { PhoneNumber } from '../../../../../feature/phonenumber'; 18import call from '@ohos.telephony.call'; 19import telephonySim from '@ohos.telephony.sim'; 20 21const TAG = 'StaticSubscriber' 22var StaticSubscriberExtensionAbility = globalThis.requireNapi('application.StaticSubscriberExtensionAbility'); 23export default class StaticSubscriber extends StaticSubscriberExtensionAbility { 24 private async callAction(phoneNumber: string) { 25 HiLog.i(TAG, 'callAction') 26 let readySimCount: number = 0; 27 let readySim = -1; 28 for (let i = 0; i < telephonySim.getMaxSimCount(); i++) { 29 try { 30 const state = await telephonySim.getSimState(i); 31 if (state) { 32 if (this.isSimReady(state)) { 33 readySimCount++; 34 readySim = i; 35 } 36 } 37 } catch (err) { 38 HiLog.e(TAG, `callAction, ${i} error: ${JSON.stringify(err.message)}`); 39 } 40 } 41 if (readySimCount == 1 && readySim != -1) { 42 PhoneNumber.fromString(phoneNumber).dial({ 43 accountId: readySim, 44 }) 45 } else { 46 call.makeCall(phoneNumber).catch(err => { 47 HiLog.e(TAG, `callAction, error: ${JSON.stringify(err)}`); 48 }) 49 } 50 } 51 52 private isSimReady(state) { 53 return state == telephonySim.SimState.SIM_STATE_READY || state == telephonySim.SimState.SIM_STATE_LOADED; 54 } 55 56 onReceiveEvent(event) { 57 HiLog.i(TAG, 'onReceiveEvent, event:' + JSON.stringify(event)); 58 const missCallData = JSON.parse(JSON.stringify(event)); 59 const parameters = JSON.parse(JSON.stringify(missCallData.parameters)); 60 let updateMissedCallNotificationsMap: Map<string, string> = new Map(); 61 MissedCallService.getInstance().init(this.context); 62 if ('usual.event.INCOMING_CALL_MISSED' == event.event) { 63 MissedCallService.getInstance().updateMissedCallNotifications(); 64 if (parameters.countList != null) { 65 updateMissedCallNotificationsMap.set('missedPhoneJson', missCallData.parameters); 66 MissedCallService.getInstance().unreadCallNotification(updateMissedCallNotificationsMap); 67 } 68 } else if ('contact.event.CANCEL_MISSED' == event.event) { 69 if (event.parameters?.missedCallData) { 70 if ('notification.event.dialBack' == event.parameters?.action) { 71 let phoneNum: string = event.parameters.missedCallData.phoneNumber 72 if (phoneNum) { 73 this.callAction(phoneNum); 74 } 75 } 76 MissedCallService.getInstance().cancelMissedNotificationAction(event.parameters?.missedCallData) 77 } else { 78 MissedCallService.getInstance().cancelAllMissedNotificationAction() 79 } 80 } 81 } 82}