1e75ebbc8Sopenharmony_ci/* 2e75ebbc8Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3e75ebbc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e75ebbc8Sopenharmony_ci * you may not use this file except in compliance with the License. 5e75ebbc8Sopenharmony_ci * You may obtain a copy of the License at 6e75ebbc8Sopenharmony_ci * 7e75ebbc8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e75ebbc8Sopenharmony_ci * 9e75ebbc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e75ebbc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e75ebbc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e75ebbc8Sopenharmony_ci * See the License for the specific language governing permissions and 13e75ebbc8Sopenharmony_ci * limitations under the License. 14e75ebbc8Sopenharmony_ci */ 15e75ebbc8Sopenharmony_ci 16e75ebbc8Sopenharmony_ciimport commonEvent from "@ohos.commonEvent"; 17e75ebbc8Sopenharmony_ciimport Radio from '@ohos.telephony.radio'; 18e75ebbc8Sopenharmony_ciimport Sim from '@ohos.telephony.sim'; 19e75ebbc8Sopenharmony_ciimport Observer from '@ohos.telephony.observer'; 20e75ebbc8Sopenharmony_ciimport Log from "../../../../../../common/src/main/ets/default/Log"; 21e75ebbc8Sopenharmony_ciimport Constants from './common/constants'; 22e75ebbc8Sopenharmony_ci 23e75ebbc8Sopenharmony_ciconst TAG = 'SignalStatus-SignalModel'; 24e75ebbc8Sopenharmony_ci 25e75ebbc8Sopenharmony_cilet isInitObserver = false; 26e75ebbc8Sopenharmony_cilet commonEventData = null; 27e75ebbc8Sopenharmony_ci 28e75ebbc8Sopenharmony_civar mLevelLink; 29e75ebbc8Sopenharmony_civar mTypeLink; 30e75ebbc8Sopenharmony_civar mStateLink; 31e75ebbc8Sopenharmony_ci 32e75ebbc8Sopenharmony_ciexport class SignalModel { 33e75ebbc8Sopenharmony_ci constructor() { 34e75ebbc8Sopenharmony_ci mLevelLink = AppStorage.SetAndLink("cellularLevel", Constants.CELLULAR_NO_SIM_CARD); 35e75ebbc8Sopenharmony_ci mTypeLink = AppStorage.SetAndLink("cellularType", Constants.RADIO_TECHNOLOGY_UNKNOWN); 36e75ebbc8Sopenharmony_ci mStateLink = AppStorage.SetAndLink("networkState", Constants.NET_NULL); 37e75ebbc8Sopenharmony_ci this.addSubscriberListener(); 38e75ebbc8Sopenharmony_ci } 39e75ebbc8Sopenharmony_ci 40e75ebbc8Sopenharmony_ci initSignalModel() { 41e75ebbc8Sopenharmony_ci Log.showInfo(TAG, 'initSignalModel'); 42e75ebbc8Sopenharmony_ci this.checkCellularStatus(); 43e75ebbc8Sopenharmony_ci } 44e75ebbc8Sopenharmony_ci 45e75ebbc8Sopenharmony_ci /** 46e75ebbc8Sopenharmony_ci * add mms app subscriber 47e75ebbc8Sopenharmony_ci */ 48e75ebbc8Sopenharmony_ci async addSubscriberListener() { 49e75ebbc8Sopenharmony_ci let events = [Constants.COMMON_EVENT_SPN_INFO_CHANGED]; 50e75ebbc8Sopenharmony_ci let commonEventSubscribeInfo = { 51e75ebbc8Sopenharmony_ci events: events 52e75ebbc8Sopenharmony_ci }; 53e75ebbc8Sopenharmony_ci commonEvent.createSubscriber(commonEventSubscribeInfo, this.createSubscriberCallBack.bind(this)); 54e75ebbc8Sopenharmony_ci } 55e75ebbc8Sopenharmony_ci 56e75ebbc8Sopenharmony_ci createSubscriberCallBack(err, data) { 57e75ebbc8Sopenharmony_ci commonEventData = data; 58e75ebbc8Sopenharmony_ci commonEvent.subscribe(commonEventData, this.subscriberCallBack.bind(this)); 59e75ebbc8Sopenharmony_ci } 60e75ebbc8Sopenharmony_ci 61e75ebbc8Sopenharmony_ci subscriberCallBack(err, data) { 62e75ebbc8Sopenharmony_ci if (data.event === Constants.COMMON_EVENT_SPN_INFO_CHANGED) { 63e75ebbc8Sopenharmony_ci if (data?.parameters?.CUR_PLMN) { 64e75ebbc8Sopenharmony_ci Log.showInfo(TAG, `receive stateLink: ${data.parameters.CUR_PLMN}`); 65e75ebbc8Sopenharmony_ci mStateLink.set(data.parameters.CUR_PLMN); 66e75ebbc8Sopenharmony_ci } else { 67e75ebbc8Sopenharmony_ci Log.showError(TAG, `get stateLink failed.`); 68e75ebbc8Sopenharmony_ci mStateLink.set(Constants.NET_NULL); 69e75ebbc8Sopenharmony_ci } 70e75ebbc8Sopenharmony_ci } 71e75ebbc8Sopenharmony_ci } 72e75ebbc8Sopenharmony_ci 73e75ebbc8Sopenharmony_ci uninitSignalModel() { 74e75ebbc8Sopenharmony_ci Log.showInfo(TAG, 'uninitSignalModel'); 75e75ebbc8Sopenharmony_ci this.unInitObserver(); 76e75ebbc8Sopenharmony_ci } 77e75ebbc8Sopenharmony_ci 78e75ebbc8Sopenharmony_ci /** 79e75ebbc8Sopenharmony_ci * Check the connection type and signal level of cellular network 80e75ebbc8Sopenharmony_ci */ 81e75ebbc8Sopenharmony_ci checkCellularStatus() { 82e75ebbc8Sopenharmony_ci let slotId = 0; 83e75ebbc8Sopenharmony_ci Sim.hasSimCard(slotId, (err, value) => { 84e75ebbc8Sopenharmony_ci if (value === true) { 85e75ebbc8Sopenharmony_ci Radio.getNetworkState((err, value) => { 86e75ebbc8Sopenharmony_ci if (err || !value) { 87e75ebbc8Sopenharmony_ci mTypeLink.set(Constants.RADIO_TECHNOLOGY_UNKNOWN); 88e75ebbc8Sopenharmony_ci mLevelLink.set(Constants.CELLULAR_NO_SIM_CARD); 89e75ebbc8Sopenharmony_ci } else { 90e75ebbc8Sopenharmony_ci // If there is no service, no signal is displayed. 91e75ebbc8Sopenharmony_ci if (value.regState != Constants.REG_STATE_IN_SERVICE) { 92e75ebbc8Sopenharmony_ci mTypeLink.set(Constants.RADIO_TECHNOLOGY_UNKNOWN); 93e75ebbc8Sopenharmony_ci mLevelLink.set(Constants.CELLULAR_NO_SIM_CARD); 94e75ebbc8Sopenharmony_ci } else { 95e75ebbc8Sopenharmony_ci mTypeLink.set(value.cfgTech); 96e75ebbc8Sopenharmony_ci Radio.getSignalInformation(slotId, (err, value) => { 97e75ebbc8Sopenharmony_ci if (err || !value || !value.length) { 98e75ebbc8Sopenharmony_ci mLevelLink.set(Constants.CELLULAR_NO_SIM_CARD); 99e75ebbc8Sopenharmony_ci } else { 100e75ebbc8Sopenharmony_ci mLevelLink.set(value[0].signalLevel); 101e75ebbc8Sopenharmony_ci } 102e75ebbc8Sopenharmony_ci }); 103e75ebbc8Sopenharmony_ci } 104e75ebbc8Sopenharmony_ci } 105e75ebbc8Sopenharmony_ci }); 106e75ebbc8Sopenharmony_ci } else { 107e75ebbc8Sopenharmony_ci Log.showWarn(TAG, `hasSimCard failed to hasSimCard because`); 108e75ebbc8Sopenharmony_ci mLevelLink.set(Constants.CELLULAR_NO_SIM_CARD); 109e75ebbc8Sopenharmony_ci mTypeLink.set(Constants.RADIO_TECHNOLOGY_UNKNOWN); 110e75ebbc8Sopenharmony_ci mStateLink.set(Constants.NET_NULL); 111e75ebbc8Sopenharmony_ci } 112e75ebbc8Sopenharmony_ci if (!isInitObserver) { 113e75ebbc8Sopenharmony_ci this.initObserver(); 114e75ebbc8Sopenharmony_ci } 115e75ebbc8Sopenharmony_ci }); 116e75ebbc8Sopenharmony_ci } 117e75ebbc8Sopenharmony_ci 118e75ebbc8Sopenharmony_ci /** 119e75ebbc8Sopenharmony_ci * init the observer of the cellular and signal 120e75ebbc8Sopenharmony_ci */ 121e75ebbc8Sopenharmony_ci initObserver() { 122e75ebbc8Sopenharmony_ci Log.showInfo(TAG, 'initObserver'); 123e75ebbc8Sopenharmony_ci isInitObserver = true; 124e75ebbc8Sopenharmony_ci Observer.on('signalInfoChange', (signalInfoChange) => { 125e75ebbc8Sopenharmony_ci this.checkCellularStatus(); 126e75ebbc8Sopenharmony_ci }); 127e75ebbc8Sopenharmony_ci Observer.on('networkStateChange', (networkState) => { 128e75ebbc8Sopenharmony_ci this.checkCellularStatus(); 129e75ebbc8Sopenharmony_ci }); 130e75ebbc8Sopenharmony_ci Observer.on('simStateChange', (simStateInfo) => { 131e75ebbc8Sopenharmony_ci this.checkCellularStatus(); 132e75ebbc8Sopenharmony_ci }); 133e75ebbc8Sopenharmony_ci } 134e75ebbc8Sopenharmony_ci 135e75ebbc8Sopenharmony_ci /** 136e75ebbc8Sopenharmony_ci * Uninit the observer of the cellular and signal 137e75ebbc8Sopenharmony_ci */ 138e75ebbc8Sopenharmony_ci unInitObserver() { 139e75ebbc8Sopenharmony_ci Log.showInfo(TAG, 'unInitObserver'); 140e75ebbc8Sopenharmony_ci Observer.off('signalInfoChange'); 141e75ebbc8Sopenharmony_ci Observer.off('networkStateChange'); 142e75ebbc8Sopenharmony_ci Observer.off('simStateChange'); 143e75ebbc8Sopenharmony_ci isInitObserver = false; 144e75ebbc8Sopenharmony_ci } 145e75ebbc8Sopenharmony_ci} 146e75ebbc8Sopenharmony_ci 147e75ebbc8Sopenharmony_cilet mSignalModel = new SignalModel(); 148e75ebbc8Sopenharmony_ci 149e75ebbc8Sopenharmony_ciexport default mSignalModel as SignalModel;