!67 [screenlock]解决运营商显示和信号显示问题

Merge pull request !67 from lizhi/master
This commit is contained in:
openharmony_ci 2022-09-16 04:17:23 +00:00 committed by Gitee
commit 8f43de88d2
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 121 additions and 95 deletions

View File

@ -14,22 +14,35 @@
*/
export default class Constants {
static readonly RADIO_TECHNOLOGY_UNKNOWN = 0;
static readonly RADIO_TECHNOLOGY_GSM = 1;
static readonly RADIO_TECHNOLOGY_1XRTT = 2;
static readonly RADIO_TECHNOLOGY_WCDMA = 3;
static readonly RADIO_TECHNOLOGY_HSPA = 4;
static readonly RADIO_TECHNOLOGY_HSPAP = 5;
static readonly RADIO_TECHNOLOGY_TD_SCDMA = 6;
static readonly RADIO_TECHNOLOGY_EVDO = 7;
static readonly RADIO_TECHNOLOGY_EHRPD = 8;
static readonly RADIO_TECHNOLOGY_LTE = 9;
static readonly RADIO_TECHNOLOGY_LTE_CA = 10;
static readonly RADIO_TECHNOLOGY_IWLAN = 11;
static readonly RADIO_TECHNOLOGY_NR = 12;
static NETWORK_TYPE_UNKNOWN = 0;
static NETWORK_TYPE_GSM = 1;
static NETWORK_TYPE_CDMA = 2;
static NETWORK_TYPE_W_CDMA = 3;
static NETWORK_TYPE_TDS_CDMA = 4;
static NETWORK_TYPE_LTE = 5;
static NETWORK_TYPE_NR = 6;
static readonly REG_STATE_NO_SERVICE = 0;
static readonly REG_STATE_IN_SERVICE = 1;
static readonly REG_STATE_EMERGENCY_CALL_ONLY = 2;
static readonly REG_STATE_POWER_OFF = 3;
static CELLULAR_SIGNAL_NO = 0;
static CELLULAR_SIGNAL_MIN = 1;
static CELLULAR_SIGNAL_LOW = 2;
static CELLULAR_SIGNAL_HALF = 3;
static CELLULAR_SIGNAL_HIGH = 4;
static CELLULAR_SIGNAL_FULL = 5;
static CELLULAR_NO_SIM_CARD = -1;
static readonly CELLULAR_SIGNAL_NO = 0;
static readonly CELLULAR_SIGNAL_MIN = 1;
static readonly CELLULAR_SIGNAL_LOW = 2;
static readonly CELLULAR_SIGNAL_HALF = 3;
static readonly CELLULAR_SIGNAL_HIGH = 4;
static readonly CELLULAR_SIGNAL_FULL = 5;
static readonly CELLULAR_NO_SIM_CARD = -1;
static NET_NULL = 'NULL';
static readonly NET_NULL = 'NULL';
// Events for Subscribing to Notifications
static readonly COMMON_EVENT_SPN_INFO_CHANGED = "usual.event.SPN_INFO_CHANGED";
}

View File

@ -26,9 +26,9 @@ const TAG = 'SignalComponent-SignalIcon'
@Component
export default struct SignalIcon {
@StorageLink('cellularLevel') cellularLevel: number = Constants.CELLULAR_NO_SIM_CARD
@StorageLink('cellularType') cellularType: number = Constants.NETWORK_TYPE_UNKNOWN
@StorageLink('networkState') networkState: string = Constants.NET_NULL
@StorageLink('cellularLevel_screenLock') cellularLevel: number = Constants.CELLULAR_NO_SIM_CARD
@StorageLink('cellularType_screenLock') cellularType: number = Constants.RADIO_TECHNOLOGY_UNKNOWN
@StorageLink('networkState_screenLock') networkState: string = Constants.NET_NULL
@StorageLink('signalObserved') signalObserved: boolean = false
private mGroupId: string = ''
@State mStatusBarGroupComponentData: StatusBarGroupComponentData = new StatusBarGroupComponentData()
@ -64,7 +64,7 @@ export default struct SignalIcon {
Stack({ alignContent: Alignment.TopStart }) {
Text(this.updateCellularType(this.cellularType))
.fontSize(this.cellularType == Constants.NETWORK_TYPE_UNKNOWN ? this.style.statusBarSignalUnknownFontSize : this.style.statusBarSignalTypeFontSize)
.fontSize(this.cellularType == Constants.RADIO_TECHNOLOGY_UNKNOWN ? this.style.statusBarSignalUnknownFontSize : this.style.statusBarSignalTypeFontSize)
.fontColor(this.mStatusBarGroupComponentData.contentColor)
.width(this.style.netSignalTextMaxWidth)
.fontWeight(FontWeight.Bold)
@ -81,7 +81,8 @@ export default struct SignalIcon {
}
.height('100%')
}
/*
/**
* Get the string of cellular type
*
* @param {number} type - number of cellular type
@ -91,29 +92,37 @@ export default struct SignalIcon {
Log.showInfo(TAG, `updateCellularType, signalType: ${signalType}`);
let typeString;
switch (signalType) {
case Constants.NETWORK_TYPE_UNKNOWN:
case Constants.RADIO_TECHNOLOGY_UNKNOWN:
typeString = $r('app.string.signal_null');
break;
case Constants.NETWORK_TYPE_GSM:
case Constants.NETWORK_TYPE_CDMA:
case Constants.RADIO_TECHNOLOGY_GSM:
case Constants.RADIO_TECHNOLOGY_1XRTT:
typeString = $r('app.string.2G');
break;
case Constants.NETWORK_TYPE_W_CDMA:
case Constants.NETWORK_TYPE_TDS_CDMA:
case Constants.RADIO_TECHNOLOGY_WCDMA:
case Constants.RADIO_TECHNOLOGY_HSPA:
case Constants.RADIO_TECHNOLOGY_HSPAP:
case Constants.RADIO_TECHNOLOGY_TD_SCDMA:
case Constants.RADIO_TECHNOLOGY_EVDO:
case Constants.RADIO_TECHNOLOGY_EHRPD:
typeString = $r('app.string.3G');
break;
case Constants.NETWORK_TYPE_LTE:
case Constants.RADIO_TECHNOLOGY_LTE:
case Constants.RADIO_TECHNOLOGY_LTE_CA:
case Constants.RADIO_TECHNOLOGY_IWLAN:
typeString = $r('app.string.4G');
break;
case Constants.NETWORK_TYPE_NR:
default:
case Constants.RADIO_TECHNOLOGY_NR:
typeString = $r('app.string.5G');
break;
default:
typeString = $r('app.string.signal_null');
break;
}
return typeString;
}
/*
/**
* Get the cellular signal image
*
* @param {number} level - signal level from signalModel

View File

@ -13,39 +13,66 @@
* limitations under the License.
*/
import commonEvent from "@ohos.commonEvent";
import Radio from '@ohos.telephony.radio';
import Sim from '@ohos.telephony.sim'
import Sim from '@ohos.telephony.sim';
import Observer from '@ohos.telephony.observer';
import Log from "../../../../../../common/src/main/ets/default/Log";
import CheckEmpty from '../../../../../../common/src/main/ets/default/CheckEmptyUtils';
import Constants from './common/constants'
import Constants from './common/constants';
const TAG = 'SignalStatus-SignalModel';
const EMPTY_LEVEL = 0;
let mSignalCallback;
let signalValue = {
cellularLevel: '',
cellularType: '',
networkState: ''
};
let isInitObserver = false
let isInitObserver = false;
let commonEventData = null;
var mLevelLink;
var mTypeLink;
var mStateLink;
export class SignalModel {
constructor() {
mLevelLink = AppStorage.SetAndLink("cellularLevel_screenLock", Constants.CELLULAR_NO_SIM_CARD);
mTypeLink = AppStorage.SetAndLink("cellularType_screenLock", Constants.RADIO_TECHNOLOGY_UNKNOWN);
mStateLink = AppStorage.SetAndLink("networkState_screenLock", Constants.NET_NULL);
this.addSubscriberListener();
}
initSignalModel() {
Log.showDebug(TAG, 'initSignalModel');
mLevelLink = AppStorage.SetAndLink("cellularLevel", Constants.CELLULAR_NO_SIM_CARD);
mTypeLink = AppStorage.SetAndLink("cellularType", Constants.NETWORK_TYPE_UNKNOWN);
mStateLink = AppStorage.SetAndLink("networkState", Constants.NET_NULL);
Log.showInfo(TAG, 'initSignalModel');
this.checkCellularStatus();
}
/**
* add mms app subscriber
*/
async addSubscriberListener() {
let events = [Constants.COMMON_EVENT_SPN_INFO_CHANGED];
let commonEventSubscribeInfo = {
events: events
};
commonEvent.createSubscriber(commonEventSubscribeInfo, this.createSubscriberCallBack.bind(this));
}
createSubscriberCallBack(err, data) {
commonEventData = data;
commonEvent.subscribe(commonEventData, this.subscriberCallBack.bind(this));
}
subscriberCallBack(err, data) {
Log.showInfo(TAG, `subscriberCallBack, err: ${JSON.stringify(err.message)}, event: ${data.event}`);
if (data.event === Constants.COMMON_EVENT_SPN_INFO_CHANGED) {
Log.showInfo(TAG, `receive stateLink: ${data?.parameters?.CUR_PLMN}`);
if (data?.parameters?.CUR_PLMN) {
mStateLink.set(data.parameters.CUR_PLMN);
} else {
Log.showError(TAG, `get stateLink failed.`);
mStateLink.set(Constants.NET_NULL);
}
}
}
uninitSignalModel() {
Log.showDebug(TAG, 'uninitSignalModel');
Log.showInfo(TAG, 'uninitSignalModel');
this.unInitObserver();
}
@ -53,65 +80,42 @@ export class SignalModel {
* Check the connection type and signal level of cellular network
*/
checkCellularStatus() {
let cellularStatus;
let slotId = 0;
Log.showInfo(TAG, 'enter checkCellularStatus');
Sim.hasSimCard(slotId, (err, value) => {
if (value === true) {
//The interface of getting the cellular signal status is unavailable temporarily
Radio.getSignalInformation(slotId, (err, value) => {
if (err) {
// Failed to call the interfaceerror is not null
Log.showError(TAG, `failed to getSimState because ${err.message}`);
// When failed to call the interface, set the result as no signal
Radio.getNetworkState((err, value) => {
if (err || !value) {
Log.showError(TAG, `failed to getnetworkState because ${err.message}`);
mTypeLink.set(Constants.RADIO_TECHNOLOGY_UNKNOWN);
mLevelLink.set(Constants.CELLULAR_NO_SIM_CARD);
mTypeLink.set(Constants.NETWORK_TYPE_UNKNOWN);
} else {
// Call interface succeederror is null
// Since the value might be empty, set it as no signal by hand
if (!value || !value.length) {
Log.showError(TAG, 'value from api is empty, set 0');
Log.showInfo(TAG, `success to getnetworkState: ${JSON.stringify(value)}`);
// If there is no service, no signal is displayed.
if (value.regState != Constants.REG_STATE_IN_SERVICE) {
mTypeLink.set(Constants.RADIO_TECHNOLOGY_UNKNOWN);
mLevelLink.set(Constants.CELLULAR_NO_SIM_CARD);
mTypeLink.set(Constants.NETWORK_TYPE_UNKNOWN);
} else {
Log.showInfo(TAG, 'get signal level by value.');
mLevelLink.set(value[0].signalLevel);
mTypeLink.set(value[0].signalType);
mTypeLink.set(value.cfgTech);
Radio.getSignalInformation(slotId, (err, value) => {
if (err || !value || !value.length) {
Log.showError(TAG, `failed to getSimState because ${err.message}`);
mLevelLink.set(Constants.CELLULAR_NO_SIM_CARD);
} else {
Log.showInfo(TAG, `success to getSignalInfo: ${JSON.stringify(value)}`);
mLevelLink.set(value[0].signalLevel);
}
});
}
}
//The interface of getting the cellular signal status is unavailable temporarily
Radio.getNetworkState((err, value) => {
if (err) {
// Failed to call the interfaceerror is not null
Log.showError(TAG, `failed to getnetworkState because ${err.message}`);
// When failed to call the interface, set the result as no signal
mStateLink.set(Constants.NET_NULL);
} else {
// Call interface succeederror is null
Log.showDebug(TAG, `success to getnetworkState: ${JSON.stringify(value)}`);
// Since the value might be empty, set it as no signal by hand
if (!value) {
Log.showError(TAG, 'value from api is empty, set 0');
mStateLink.set(Constants.NET_NULL);
} else {
mStateLink.set(value.longOperatorName);
}
}
});
});
if (!isInitObserver) {
this.initObserver();
}
} else {
Log.showError(TAG, `hasSimCard failed to hasSimCard because`);
mLevelLink.set(Constants.CELLULAR_NO_SIM_CARD);
mTypeLink.set(Constants.NETWORK_TYPE_UNKNOWN);
mTypeLink.set(Constants.RADIO_TECHNOLOGY_UNKNOWN);
mStateLink.set(Constants.NET_NULL);
if (!isInitObserver) {
this.initObserver();
}
}
if (!isInitObserver) {
this.initObserver();
}
});
}
@ -120,18 +124,18 @@ export class SignalModel {
* init the observer of the cellular and signal
*/
initObserver() {
Log.showDebug(TAG, 'initObserver');
Log.showInfo(TAG, 'initObserver');
isInitObserver = true;
Observer.on('signalInfoChange', (signalInfoChange) => {
Log.showInfo(TAG, 'signalInfoChange');
Log.showInfo(TAG, `signalInfoChange ${JSON.stringify(signalInfoChange)}`);
this.checkCellularStatus();
});
Observer.on('networkStateChange', (networkState) => {
Log.showInfo(TAG, 'networkStateChange');
Log.showInfo(TAG, `networkStateChange ${JSON.stringify(networkState)}`);
this.checkCellularStatus();
});
Observer.on('simStateChange', (simStateInfo) => {
Log.showInfo(TAG, 'simStateChange');
Log.showInfo(TAG, `simStateChange ${JSON.stringify(simStateInfo)}`);
this.checkCellularStatus();
});
}
@ -140,7 +144,7 @@ export class SignalModel {
* Uninit the observer of the cellular and signal
*/
unInitObserver() {
Log.showDebug(TAG, 'unInitObserver');
Log.showInfo(TAG, 'unInitObserver');
Observer.off('signalInfoChange');
Observer.off('networkStateChange');
Observer.off('simStateChange');