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 radio from '@ohos.telephony.radio'
16import { HiLog } from '../../../../../../common';
17import telephonySim from '@ohos.telephony.sim';
18
19const TAG = 'VoLteState';
20
21class VoLteState {
22  haveVoLteReg: boolean = false;
23  voLteRegStates: boolean[] = [false, false]
24  mListener: () => void;
25
26  public init() {
27    try {
28      for (let i = 0; i < telephonySim.getMaxSimCount(); i++) {
29        this.initVoLteListener(i);
30      }
31    } catch (error) {
32      HiLog.w(TAG, 'VoLteState, get VoLte state error:' + JSON.stringify(error));
33    }
34  }
35
36  public removeVoLteListeners() {
37    try {
38      for (let i = 0; i < telephonySim.getMaxSimCount(); i++) {
39        this.removeVoLteListener(i);
40      }
41    } catch (error) {
42      HiLog.w(TAG, 'VoLteState, removeVoLteListeners error:' + JSON.stringify(error));
43    }
44  }
45
46  public setListener(listener: () => void) {
47    this.mListener = listener;
48  }
49
50  private initVoLteListener(slot: number) {
51    HiLog.i(TAG, 'initVoLteListener slot:' + slot);
52    radio.on('imsRegStateChange', slot, radio.ImsServiceType.TYPE_VOICE, data => {
53      HiLog.i(TAG, `callback: data->${JSON.stringify(data)}`);
54      this.parseVoLteState(slot, data.imsRegState)
55    });
56    radio.getImsRegInfo(slot, radio.ImsServiceType.TYPE_VOICE, (err, data) => {
57      if (err) {
58        HiLog.w(TAG, 'getImsRegInfo err:' + JSON.stringify(err))
59      } else {
60        HiLog.i(TAG, `getImsRegInfo: data->${JSON.stringify(data)}`);
61        this.parseVoLteState(slot, data.imsRegState);
62      }
63    });
64  }
65
66  private removeVoLteListener(slot: number) {
67    HiLog.d(TAG, `removeVoLteListener slot:${slot}`);
68    radio.off('imsRegStateChange', slot, radio.ImsServiceType.TYPE_VOICE);
69  }
70
71  private parseVoLteState(slotId: number, data) {
72    const voLte = (data == radio.ImsRegState.IMS_REGISTERED);
73    const changed = (voLte != this.voLteRegStates[slotId]);
74    if (!changed) {
75      HiLog.w(TAG, `parseVoLteState: state not changed slotId${slotId}  data->${data}`);
76      return;
77    }
78    this.voLteRegStates[slotId] = voLte;
79    this.haveVoLteReg = this.voLteRegStates[0] || this.voLteRegStates[1];
80    AppStorage.SetOrCreate<boolean>('haveVoLteReg', this.haveVoLteReg);
81    AppStorage.SetOrCreate<boolean[]>('voLteRegStates', this.voLteRegStates);
82    HiLog.i(TAG, `parseVoLteState: state changed slotId${slotId}  volte->${voLte}, haveVoLteReg:${this.haveVoLteReg}`);
83    if (this.mListener) {
84      this.mListener();
85    }
86  }
87}
88
89export default new VoLteState();