18779efd5Sopenharmony_ci/**
28779efd5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License.
58779efd5Sopenharmony_ci * You may obtain a copy of the License at
68779efd5Sopenharmony_ci *
78779efd5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88779efd5Sopenharmony_ci *
98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and
138779efd5Sopenharmony_ci * limitations under the License.
148779efd5Sopenharmony_ci */
158779efd5Sopenharmony_ciimport { HiLog } from '../../../../../common';
168779efd5Sopenharmony_ciimport { MissedCallService } from '../../../../../feature/call';
178779efd5Sopenharmony_ciimport { PhoneNumber } from '../../../../../feature/phonenumber';
188779efd5Sopenharmony_ciimport call from '@ohos.telephony.call';
198779efd5Sopenharmony_ciimport telephonySim from '@ohos.telephony.sim';
208779efd5Sopenharmony_ci
218779efd5Sopenharmony_ciconst TAG = 'StaticSubscriber'
228779efd5Sopenharmony_civar StaticSubscriberExtensionAbility = globalThis.requireNapi('application.StaticSubscriberExtensionAbility');
238779efd5Sopenharmony_ciexport default class StaticSubscriber extends StaticSubscriberExtensionAbility {
248779efd5Sopenharmony_ci  private async callAction(phoneNumber: string) {
258779efd5Sopenharmony_ci    HiLog.i(TAG, 'callAction')
268779efd5Sopenharmony_ci    let readySimCount: number = 0;
278779efd5Sopenharmony_ci    let readySim = -1;
288779efd5Sopenharmony_ci    for (let i = 0; i < telephonySim.getMaxSimCount(); i++) {
298779efd5Sopenharmony_ci      try {
308779efd5Sopenharmony_ci        const state = await telephonySim.getSimState(i);
318779efd5Sopenharmony_ci        if (state) {
328779efd5Sopenharmony_ci          if (this.isSimReady(state)) {
338779efd5Sopenharmony_ci            readySimCount++;
348779efd5Sopenharmony_ci            readySim = i;
358779efd5Sopenharmony_ci          }
368779efd5Sopenharmony_ci        }
378779efd5Sopenharmony_ci      } catch (err) {
388779efd5Sopenharmony_ci        HiLog.e(TAG, `callAction, ${i} error: ${JSON.stringify(err.message)}`);
398779efd5Sopenharmony_ci      }
408779efd5Sopenharmony_ci    }
418779efd5Sopenharmony_ci    if (readySimCount == 1 && readySim != -1) {
428779efd5Sopenharmony_ci      PhoneNumber.fromString(phoneNumber).dial({
438779efd5Sopenharmony_ci        accountId: readySim,
448779efd5Sopenharmony_ci      })
458779efd5Sopenharmony_ci    } else {
468779efd5Sopenharmony_ci      call.makeCall(phoneNumber).catch(err => {
478779efd5Sopenharmony_ci        HiLog.e(TAG, `callAction,  error: ${JSON.stringify(err)}`);
488779efd5Sopenharmony_ci      })
498779efd5Sopenharmony_ci    }
508779efd5Sopenharmony_ci  }
518779efd5Sopenharmony_ci
528779efd5Sopenharmony_ci  private isSimReady(state) {
538779efd5Sopenharmony_ci    return state == telephonySim.SimState.SIM_STATE_READY || state == telephonySim.SimState.SIM_STATE_LOADED;
548779efd5Sopenharmony_ci  }
558779efd5Sopenharmony_ci
568779efd5Sopenharmony_ci  onReceiveEvent(event) {
578779efd5Sopenharmony_ci    HiLog.i(TAG, 'onReceiveEvent, event:' + JSON.stringify(event));
588779efd5Sopenharmony_ci    const missCallData = JSON.parse(JSON.stringify(event));
598779efd5Sopenharmony_ci    const parameters = JSON.parse(JSON.stringify(missCallData.parameters));
608779efd5Sopenharmony_ci    let updateMissedCallNotificationsMap: Map<string, string> = new Map();
618779efd5Sopenharmony_ci    MissedCallService.getInstance().init(this.context);
628779efd5Sopenharmony_ci    if ('usual.event.INCOMING_CALL_MISSED' == event.event) {
638779efd5Sopenharmony_ci      MissedCallService.getInstance().updateMissedCallNotifications();
648779efd5Sopenharmony_ci      if (parameters.countList != null) {
658779efd5Sopenharmony_ci        updateMissedCallNotificationsMap.set('missedPhoneJson', missCallData.parameters);
668779efd5Sopenharmony_ci        MissedCallService.getInstance().unreadCallNotification(updateMissedCallNotificationsMap);
678779efd5Sopenharmony_ci      }
688779efd5Sopenharmony_ci    } else if ('contact.event.CANCEL_MISSED' == event.event) {
698779efd5Sopenharmony_ci      if (event.parameters?.missedCallData) {
708779efd5Sopenharmony_ci        if ('notification.event.dialBack' == event.parameters?.action) {
718779efd5Sopenharmony_ci          let phoneNum: string = event.parameters.missedCallData.phoneNumber
728779efd5Sopenharmony_ci          if (phoneNum) {
738779efd5Sopenharmony_ci            this.callAction(phoneNum);
748779efd5Sopenharmony_ci          }
758779efd5Sopenharmony_ci        }
768779efd5Sopenharmony_ci        MissedCallService.getInstance().cancelMissedNotificationAction(event.parameters?.missedCallData)
778779efd5Sopenharmony_ci      } else {
788779efd5Sopenharmony_ci        MissedCallService.getInstance().cancelAllMissedNotificationAction()
798779efd5Sopenharmony_ci      }
808779efd5Sopenharmony_ci    }
818779efd5Sopenharmony_ci  }
828779efd5Sopenharmony_ci}