• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 observer from '@ohos.telephony.observer';
16import telephonySim from '@ohos.telephony.sim';
17import { HiLog } from '../../../../../../common';
18
19export const simId_NONE: number = -1;
20
21export const simId_ONE: number = 0;
22
23export const simId_TWO: number = 1;
24
25const TAG = 'SimCardState';
26
27class SimCardState {
28  mListener: () => void;
29  mSimStateArray: Array<telephonySim.SimState> =
30    [telephonySim.SimState.SIM_STATE_UNKNOWN, telephonySim.SimState.SIM_STATE_UNKNOWN];
31  haveSimCard: boolean = false;
32  haveMultiSimCard: boolean = false;
33
34  /**
35   * isSimReady
36   *
37   * @param slotId the sim slot id number
38   * @return boolean the sim is ready or not
39   */
40  public isSimReady(slotId: number) {
41    return this.mSimStateArray[slotId] == telephonySim.SimState.SIM_STATE_READY || this.mSimStateArray[slotId]
42    == telephonySim.SimState.SIM_STATE_LOADED;
43  }
44
45  /*
46   * Initialization is required only when callback is required. Callback is required to ensure data accuracy and timeliness.
47   */
48  public init() {
49    try {
50      HiLog.i(TAG, 'SimCardState, init.')
51      this.addSimChangeListener();
52      this.getSimCardState();
53    } catch (error) {
54      HiLog.w(TAG, 'SimCardState, get sim state error.')
55    }
56  }
57
58  public removeSimChangeListener() {
59    HiLog.i(TAG, 'removeSimChangeListener ! ');
60    try {
61      observer.off('simStateChange');
62    } catch (error) {
63      HiLog.w(TAG, 'removeSimChangeListener error.' + JSON.stringify(error))
64    }
65  }
66
67  public setListener(listener: () => void) {
68    this.mListener = listener;
69  }
70
71  private addSimChangeListener() {
72    for (let i = 0; i < telephonySim.getMaxSimCount(); i++) {
73      observer.on('simStateChange', {
74        slotId: i
75      }, value => {
76        let simState = value?.state;
77        HiLog.i(TAG, `simStateChange for ${i}, SIM value: ` + simState);
78        this.parseSimCardStateForSlot(i, simState);
79      });
80    }
81  }
82
83  private getSimCardState() {
84    for (let i = 0; i < telephonySim.getMaxSimCount(); i++) {
85      telephonySim.getSimState(i, (err, value) => {
86        if (err) {
87          HiLog.e(TAG, `getSimCardState, ${i} error: ${JSON.stringify(err.message)}`);
88        } else {
89          this.parseSimCardStateForSlot(i, value)
90        }
91      });
92    }
93  }
94
95  private parseSimCardStateForSlot(slotId: number, value) {
96    let changed: boolean = (value != this.mSimStateArray[slotId]);
97    if (!changed) {
98      return;
99    }
100    this.mSimStateArray[slotId] = value;
101    this.haveSimCard = this.isSimReady(simId_ONE) || this.isSimReady(simId_TWO);
102    this.haveMultiSimCard = this.isSimReady(simId_ONE) && this.isSimReady(simId_TWO);
103    AppStorage.SetOrCreate<boolean>('haveMultiSimCard', this.haveMultiSimCard);
104    AppStorage.SetOrCreate<boolean>('haveSimCard', this.haveSimCard);
105    HiLog.i(TAG, `parseSimCardStateForSlot sim ${slotId}} state ${value}}, haveSimCard: ` + this.haveSimCard +
106    ', haveMultiSimCard: ' + this.haveMultiSimCard);
107    this.setDefaultSlot();
108    if (this.mListener) {
109      this.mListener();
110    }
111  }
112
113  private setDefaultSlot() {
114    if (this.haveSimCard) {
115      if (!this.haveMultiSimCard) {
116        if (this.isSimReady(simId_ONE)) {
117          AppStorage.SetOrCreate<number>('defaultSlot', simId_ONE);
118        } else {
119          AppStorage.SetOrCreate<number>('defaultSlot', simId_TWO);
120        }
121      } else {
122        telephonySim.getDefaultVoiceSlotId((err, slot: number) => {
123          if (err) {
124            HiLog.e(TAG, `getDefaultVoiceSlotId, ${slot} error: ${JSON.stringify(err)}`);
125          } else {
126            AppStorage.SetOrCreate<number>('defaultSlot', slot);
127          }
128        })
129      }
130    } else {
131      if (AppStorage.Has('defaultSlot')) {
132        AppStorage.Delete('defaultSlot')
133      }
134    }
135  }
136}
137
138export default new SimCardState();