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 observer from '@ohos.telephony.observer';
168779efd5Sopenharmony_ciimport telephonySim from '@ohos.telephony.sim';
178779efd5Sopenharmony_ciimport { HiLog } from '../../../../../../common';
188779efd5Sopenharmony_ci
198779efd5Sopenharmony_ciexport const simId_NONE: number = -1;
208779efd5Sopenharmony_ci
218779efd5Sopenharmony_ciexport const simId_ONE: number = 0;
228779efd5Sopenharmony_ci
238779efd5Sopenharmony_ciexport const simId_TWO: number = 1;
248779efd5Sopenharmony_ci
258779efd5Sopenharmony_ciconst TAG = 'SimCardState';
268779efd5Sopenharmony_ci
278779efd5Sopenharmony_ciclass SimCardState {
288779efd5Sopenharmony_ci  mListener: () => void;
298779efd5Sopenharmony_ci  mSimStateArray: Array<telephonySim.SimState> =
308779efd5Sopenharmony_ci    [telephonySim.SimState.SIM_STATE_UNKNOWN, telephonySim.SimState.SIM_STATE_UNKNOWN];
318779efd5Sopenharmony_ci  haveSimCard: boolean = false;
328779efd5Sopenharmony_ci  haveMultiSimCard: boolean = false;
338779efd5Sopenharmony_ci
348779efd5Sopenharmony_ci  /**
358779efd5Sopenharmony_ci   * isSimReady
368779efd5Sopenharmony_ci   *
378779efd5Sopenharmony_ci   * @param slotId the sim slot id number
388779efd5Sopenharmony_ci   * @return boolean the sim is ready or not
398779efd5Sopenharmony_ci   */
408779efd5Sopenharmony_ci  public isSimReady(slotId: number) {
418779efd5Sopenharmony_ci    return this.mSimStateArray[slotId] == telephonySim.SimState.SIM_STATE_READY || this.mSimStateArray[slotId]
428779efd5Sopenharmony_ci    == telephonySim.SimState.SIM_STATE_LOADED;
438779efd5Sopenharmony_ci  }
448779efd5Sopenharmony_ci
458779efd5Sopenharmony_ci  /*
468779efd5Sopenharmony_ci   * Initialization is required only when callback is required. Callback is required to ensure data accuracy and timeliness.
478779efd5Sopenharmony_ci   */
488779efd5Sopenharmony_ci  public init() {
498779efd5Sopenharmony_ci    try {
508779efd5Sopenharmony_ci      HiLog.i(TAG, 'SimCardState, init.')
518779efd5Sopenharmony_ci      this.addSimChangeListener();
528779efd5Sopenharmony_ci      this.getSimCardState();
538779efd5Sopenharmony_ci    } catch (error) {
548779efd5Sopenharmony_ci      HiLog.w(TAG, 'SimCardState, get sim state error.')
558779efd5Sopenharmony_ci    }
568779efd5Sopenharmony_ci  }
578779efd5Sopenharmony_ci
588779efd5Sopenharmony_ci  public removeSimChangeListener() {
598779efd5Sopenharmony_ci    HiLog.i(TAG, 'removeSimChangeListener ! ');
608779efd5Sopenharmony_ci    observer.off('simStateChange');
618779efd5Sopenharmony_ci  }
628779efd5Sopenharmony_ci
638779efd5Sopenharmony_ci  public setListener(listener: () => void) {
648779efd5Sopenharmony_ci    this.mListener = listener;
658779efd5Sopenharmony_ci  }
668779efd5Sopenharmony_ci
678779efd5Sopenharmony_ci  private addSimChangeListener() {
688779efd5Sopenharmony_ci    for (let i = 0; i < telephonySim.getMaxSimCount(); i++) {
698779efd5Sopenharmony_ci      observer.on('simStateChange', {
708779efd5Sopenharmony_ci        slotId: i
718779efd5Sopenharmony_ci      }, value => {
728779efd5Sopenharmony_ci        let simState = value?.state;
738779efd5Sopenharmony_ci        HiLog.i(TAG, `simStateChange for ${i}, SIM value: ` + simState);
748779efd5Sopenharmony_ci        this.parseSimCardStateForSlot(i, simState);
758779efd5Sopenharmony_ci      });
768779efd5Sopenharmony_ci    }
778779efd5Sopenharmony_ci  }
788779efd5Sopenharmony_ci
798779efd5Sopenharmony_ci  private getSimCardState() {
808779efd5Sopenharmony_ci    for (let i = 0; i < telephonySim.getMaxSimCount(); i++) {
818779efd5Sopenharmony_ci      telephonySim.getSimState(i, (err, value) => {
828779efd5Sopenharmony_ci        if (err) {
838779efd5Sopenharmony_ci          HiLog.e(TAG, `getSimCardState, ${i} error: ${JSON.stringify(err.message)}`);
848779efd5Sopenharmony_ci        } else {
858779efd5Sopenharmony_ci          this.parseSimCardStateForSlot(i, value)
868779efd5Sopenharmony_ci        }
878779efd5Sopenharmony_ci      });
888779efd5Sopenharmony_ci    }
898779efd5Sopenharmony_ci  }
908779efd5Sopenharmony_ci
918779efd5Sopenharmony_ci  private parseSimCardStateForSlot(slotId: number, value) {
928779efd5Sopenharmony_ci    let changed: boolean = (value != this.mSimStateArray[slotId]);
938779efd5Sopenharmony_ci    if (!changed) {
948779efd5Sopenharmony_ci      return;
958779efd5Sopenharmony_ci    }
968779efd5Sopenharmony_ci    this.mSimStateArray[slotId] = value;
978779efd5Sopenharmony_ci    this.haveSimCard = this.isSimReady(simId_ONE) || this.isSimReady(simId_TWO);
988779efd5Sopenharmony_ci    this.haveMultiSimCard = this.isSimReady(simId_ONE) && this.isSimReady(simId_TWO);
998779efd5Sopenharmony_ci    AppStorage.SetOrCreate<boolean>('haveMultiSimCard', this.haveMultiSimCard);
1008779efd5Sopenharmony_ci    AppStorage.SetOrCreate<boolean>('haveSimCard', this.haveSimCard);
1018779efd5Sopenharmony_ci    HiLog.i(TAG, `parseSimCardStateForSlot sim ${slotId}} state ${value}}, haveSimCard: ` + this.haveSimCard +
1028779efd5Sopenharmony_ci    ', haveMultiSimCard: ' + this.haveMultiSimCard);
1038779efd5Sopenharmony_ci    this.setDefaultSlot();
1048779efd5Sopenharmony_ci    if (this.mListener) {
1058779efd5Sopenharmony_ci      this.mListener();
1068779efd5Sopenharmony_ci    }
1078779efd5Sopenharmony_ci  }
1088779efd5Sopenharmony_ci
1098779efd5Sopenharmony_ci  private setDefaultSlot() {
1108779efd5Sopenharmony_ci    if (this.haveSimCard) {
1118779efd5Sopenharmony_ci      if (!this.haveMultiSimCard) {
1128779efd5Sopenharmony_ci        if (this.isSimReady(simId_ONE)) {
1138779efd5Sopenharmony_ci          AppStorage.SetOrCreate<number>('defaultSlot', simId_ONE);
1148779efd5Sopenharmony_ci        } else {
1158779efd5Sopenharmony_ci          AppStorage.SetOrCreate<number>('defaultSlot', simId_TWO);
1168779efd5Sopenharmony_ci        }
1178779efd5Sopenharmony_ci      } else {
1188779efd5Sopenharmony_ci        telephonySim.getDefaultVoiceSlotId((err, slot: number) => {
1198779efd5Sopenharmony_ci          if (err) {
1208779efd5Sopenharmony_ci            HiLog.e(TAG, `getDefaultVoiceSlotId, ${slot} error: ${JSON.stringify(err)}`);
1218779efd5Sopenharmony_ci          } else {
1228779efd5Sopenharmony_ci            AppStorage.SetOrCreate<number>('defaultSlot', slot);
1238779efd5Sopenharmony_ci          }
1248779efd5Sopenharmony_ci        })
1258779efd5Sopenharmony_ci      }
1268779efd5Sopenharmony_ci    } else {
1278779efd5Sopenharmony_ci      if (AppStorage.Has('defaultSlot')) {
1288779efd5Sopenharmony_ci        AppStorage.Delete('defaultSlot')
1298779efd5Sopenharmony_ci      }
1308779efd5Sopenharmony_ci    }
1318779efd5Sopenharmony_ci  }
1328779efd5Sopenharmony_ci}
1338779efd5Sopenharmony_ci
1348779efd5Sopenharmony_ciexport default new SimCardState();