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 telephonySim from '@ohos.telephony.sim';
168779efd5Sopenharmony_ciimport { HiLog } from '../../../../../../common';
178779efd5Sopenharmony_ciimport radio from '@ohos.telephony.radio';
188779efd5Sopenharmony_ciimport observer from '@ohos.telephony.observer';
198779efd5Sopenharmony_ci
208779efd5Sopenharmony_ciconst TAG = 'SimOpName'
218779efd5Sopenharmony_ci
228779efd5Sopenharmony_ciclass SimOpName {
238779efd5Sopenharmony_ci  spnList: Array<string | Resource> = [];
248779efd5Sopenharmony_ci
258779efd5Sopenharmony_ci  constructor() {
268779efd5Sopenharmony_ci    for (let index = 0; index < telephonySim.getMaxSimCount(); index++) {
278779efd5Sopenharmony_ci      this.spnList[index] = '';
288779efd5Sopenharmony_ci    }
298779efd5Sopenharmony_ci  }
308779efd5Sopenharmony_ci
318779efd5Sopenharmony_ci  /**
328779efd5Sopenharmony_ci   * recycle
338779efd5Sopenharmony_ci   */
348779efd5Sopenharmony_ci  public unsubscribeSpnObserver() {
358779efd5Sopenharmony_ci      observer.off('networkStateChange');
368779efd5Sopenharmony_ci  }
378779efd5Sopenharmony_ci
388779efd5Sopenharmony_ci  public initSpnObserver() {
398779efd5Sopenharmony_ci    try {
408779efd5Sopenharmony_ci      for (let index = 0; index < telephonySim.getMaxSimCount(); index++) {
418779efd5Sopenharmony_ci        observer.on('networkStateChange', { slotId: index }, data => {
428779efd5Sopenharmony_ci          if (!data) {
438779efd5Sopenharmony_ci            HiLog.e(TAG, 'there is something wrong with networkState');
448779efd5Sopenharmony_ci            return
458779efd5Sopenharmony_ci          } else {
468779efd5Sopenharmony_ci            HiLog.i(TAG, 'observer ON data : ' + JSON.stringify(data));
478779efd5Sopenharmony_ci            let spn = data.longOperatorName ? data.longOperatorName : data.plmnNumeric;
488779efd5Sopenharmony_ci            if (spn) {
498779efd5Sopenharmony_ci              HiLog.i(TAG, `networkStateChange notify Sim${index} Name:${spn}`);
508779efd5Sopenharmony_ci              this.notifySimName(index, spn);
518779efd5Sopenharmony_ci            }
528779efd5Sopenharmony_ci          }
538779efd5Sopenharmony_ci        });
548779efd5Sopenharmony_ci      }
558779efd5Sopenharmony_ci    } catch (err) {
568779efd5Sopenharmony_ci      HiLog.e(TAG, 'get error: ' + JSON.stringify(err));
578779efd5Sopenharmony_ci    }
588779efd5Sopenharmony_ci  }
598779efd5Sopenharmony_ci
608779efd5Sopenharmony_ci  public initSimName(simId: number) {
618779efd5Sopenharmony_ci    radio.getNetworkState(simId, (error, data) => {
628779efd5Sopenharmony_ci      if (error || !data) {
638779efd5Sopenharmony_ci        HiLog.e(TAG, 'getNetworkState error: ' + JSON.stringify(error));
648779efd5Sopenharmony_ci      } else {
658779efd5Sopenharmony_ci        this.notifySimName(simId, data.longOperatorName);
668779efd5Sopenharmony_ci      }
678779efd5Sopenharmony_ci    })
688779efd5Sopenharmony_ci  }
698779efd5Sopenharmony_ci
708779efd5Sopenharmony_ci  public notifySimName(slot: number, spn: string) {
718779efd5Sopenharmony_ci    HiLog.i(TAG, `notify Sim${slot} Name:${spn}`);
728779efd5Sopenharmony_ci    this.spnList[slot] = spn;
738779efd5Sopenharmony_ci    AppStorage.SetOrCreate<Array<string | Resource>>('spnList', this.spnList);
748779efd5Sopenharmony_ci  }
758779efd5Sopenharmony_ci}
768779efd5Sopenharmony_ci
778779efd5Sopenharmony_ciexport default new SimOpName();