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 radio from '@ohos.telephony.radio'
168779efd5Sopenharmony_ciimport { HiLog } from '../../../../../../common';
178779efd5Sopenharmony_ciimport telephonySim from '@ohos.telephony.sim';
188779efd5Sopenharmony_ci
198779efd5Sopenharmony_ciconst TAG = 'VoLteState';
208779efd5Sopenharmony_ci
218779efd5Sopenharmony_ciclass VoLteState {
228779efd5Sopenharmony_ci  haveVoLteReg: boolean = false;
238779efd5Sopenharmony_ci  voLteRegStates: boolean[] = [false, false]
248779efd5Sopenharmony_ci  mListener: () => void;
258779efd5Sopenharmony_ci
268779efd5Sopenharmony_ci  public init() {
278779efd5Sopenharmony_ci    try {
288779efd5Sopenharmony_ci      for (let i = 0; i < telephonySim.getMaxSimCount(); i++) {
298779efd5Sopenharmony_ci        this.initVoLteListener(i);
308779efd5Sopenharmony_ci      }
318779efd5Sopenharmony_ci    } catch (error) {
328779efd5Sopenharmony_ci      HiLog.w(TAG, 'VoLteState, get VoLte state error:' + JSON.stringify(error));
338779efd5Sopenharmony_ci    }
348779efd5Sopenharmony_ci  }
358779efd5Sopenharmony_ci
368779efd5Sopenharmony_ci  public removeVoLteListeners() {
378779efd5Sopenharmony_ci    try {
388779efd5Sopenharmony_ci      for (let i = 0; i < telephonySim.getMaxSimCount(); i++) {
398779efd5Sopenharmony_ci        this.removeVoLteListener(i);
408779efd5Sopenharmony_ci      }
418779efd5Sopenharmony_ci    } catch (error) {
428779efd5Sopenharmony_ci      HiLog.w(TAG, 'VoLteState, removeVoLteListeners error:' + JSON.stringify(error));
438779efd5Sopenharmony_ci    }
448779efd5Sopenharmony_ci  }
458779efd5Sopenharmony_ci
468779efd5Sopenharmony_ci  public setListener(listener: () => void) {
478779efd5Sopenharmony_ci    this.mListener = listener;
488779efd5Sopenharmony_ci  }
498779efd5Sopenharmony_ci
508779efd5Sopenharmony_ci  private initVoLteListener(slot: number) {
518779efd5Sopenharmony_ci    HiLog.i(TAG, 'initVoLteListener slot:' + slot);
528779efd5Sopenharmony_ci    radio.on('imsRegStateChange', slot, radio.ImsServiceType.TYPE_VOICE, data => {
538779efd5Sopenharmony_ci      HiLog.i(TAG, `callback: data->${JSON.stringify(data)}`);
548779efd5Sopenharmony_ci      this.parseVoLteState(slot, data.imsRegState)
558779efd5Sopenharmony_ci    });
568779efd5Sopenharmony_ci    radio.getImsRegInfo(slot, radio.ImsServiceType.TYPE_VOICE, (err, data) => {
578779efd5Sopenharmony_ci      if (err) {
588779efd5Sopenharmony_ci        HiLog.w(TAG, 'getImsRegInfo err:' + JSON.stringify(err))
598779efd5Sopenharmony_ci      } else {
608779efd5Sopenharmony_ci        HiLog.i(TAG, `getImsRegInfo: data->${JSON.stringify(data)}`);
618779efd5Sopenharmony_ci        this.parseVoLteState(slot, data.imsRegState);
628779efd5Sopenharmony_ci      }
638779efd5Sopenharmony_ci    });
648779efd5Sopenharmony_ci  }
658779efd5Sopenharmony_ci
668779efd5Sopenharmony_ci  private removeVoLteListener(slot: number) {
678779efd5Sopenharmony_ci    HiLog.d(TAG, `removeVoLteListener slot:${slot}`);
688779efd5Sopenharmony_ci    radio.off('imsRegStateChange', slot, radio.ImsServiceType.TYPE_VOICE);
698779efd5Sopenharmony_ci  }
708779efd5Sopenharmony_ci
718779efd5Sopenharmony_ci  private parseVoLteState(slotId: number, data) {
728779efd5Sopenharmony_ci    const voLte = (data == radio.ImsRegState.IMS_REGISTERED);
738779efd5Sopenharmony_ci    const changed = (voLte != this.voLteRegStates[slotId]);
748779efd5Sopenharmony_ci    if (!changed) {
758779efd5Sopenharmony_ci      HiLog.w(TAG, `parseVoLteState: state not changed slotId${slotId}  data->${data}`);
768779efd5Sopenharmony_ci      return;
778779efd5Sopenharmony_ci    }
788779efd5Sopenharmony_ci    this.voLteRegStates[slotId] = voLte;
798779efd5Sopenharmony_ci    this.haveVoLteReg = this.voLteRegStates[0] || this.voLteRegStates[1];
808779efd5Sopenharmony_ci    AppStorage.SetOrCreate<boolean>('haveVoLteReg', this.haveVoLteReg);
818779efd5Sopenharmony_ci    AppStorage.SetOrCreate<boolean[]>('voLteRegStates', this.voLteRegStates);
828779efd5Sopenharmony_ci    HiLog.i(TAG, `parseVoLteState: state changed slotId${slotId}  volte->${voLte}, haveVoLteReg:${this.haveVoLteReg}`);
838779efd5Sopenharmony_ci    if (this.mListener) {
848779efd5Sopenharmony_ci      this.mListener();
858779efd5Sopenharmony_ci    }
868779efd5Sopenharmony_ci  }
878779efd5Sopenharmony_ci}
888779efd5Sopenharmony_ci
898779efd5Sopenharmony_ciexport default new VoLteState();