1/* 2 * Copyright (c) 2021-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 */ 15 16import Constants from '../common/constants'; 17import mSignalModel from '../signalModel'; 18import Log from '../../../../../../../common/src/main/ets/default/Log' 19import StyleConfigurationCommon from '../../../../../../../common/src/main/ets/default/StyleConfiguration' 20import StyleConfiguration from '../common/StyleConfiguration' 21import { StatusBarGroupComponentData 22} from '../../../../../../screenlock/src/main/ets/com/ohos/common/constants' 23import StatusBarVM from '../../../../../../screenlock/src/main/ets/com/ohos/vm/StatusBarVM' 24 25const TAG = 'SignalComponent-SignalIcon' 26 27@Component 28export default struct SignalIcon { 29 @StorageLink('cellularLevel_screenLock') cellularLevel: number = Constants.CELLULAR_NO_SIM_CARD 30 @StorageLink('cellularType_screenLock') cellularType: number = Constants.RADIO_TECHNOLOGY_UNKNOWN 31 @StorageLink('networkState_screenLock') networkState: string = Constants.NET_NULL 32 @StorageLink('signalObserved') signalObserved: boolean = false 33 private mGroupId: string = '' 34 @State mStatusBarGroupComponentData: StatusBarGroupComponentData = new StatusBarGroupComponentData() 35 @State statusBarMarginLeftRight: Resource = $r("app.float.signal_status_margin_Left_right") 36 @State statusBarFontSize: Resource = $r("app.float.signal_fontSize") 37 @State signalTextMaxWeight: Resource = $r('app.float.signal_text_max_width') 38 @State statusBarSignalUnknownFontSize: Resource = $r('app.float.status_bar_signal_unknown_font_size') 39 @State statusBarSignalTypeFontSize: Resource = $r('app.float.status_bar_signal_type_font_size') 40 @State netSignalTextMaxWidth: Resource = $r('app.float.status_bar_signal_net_signal_text_max_width') 41 @State cellularImageWidth: Resource = $r('app.float.signal_component_icon_width') 42 @State cellularImageHeight: Resource = $r('app.float.signal_component_icon_height') 43 44 aboutToAppear() { 45 Log.showInfo(TAG, 'aboutToAppear'); 46 this.mStatusBarGroupComponentData = StatusBarVM.getStatusBarGroupComponentData(this.mGroupId) 47 if (!this.signalObserved) { 48 mSignalModel.initSignalModel(); 49 this.signalObserved = true; 50 } 51 } 52 53 aboutToDisappear() { 54 Log.showInfo(TAG, 'aboutToDisappear'); 55 } 56 57 build() { 58 Row() { 59 Row().width(this.statusBarMarginLeftRight).height('100%') 60 Text(this.updateNetworkState(this.networkState)) 61 .fontSize(this.statusBarFontSize) 62 .fontWeight(FontWeight.Medium) 63 .fontColor(this.mStatusBarGroupComponentData.contentColor) 64 .textOverflow({ overflow: TextOverflow.Ellipsis }) 65 .constraintSize({ maxWidth: this.signalTextMaxWeight }) 66 .flexShrink(0) 67 .maxLines(1) 68 .textAlign(TextAlign.Center) 69 Row().width(this.statusBarMarginLeftRight).height('100%') 70 71 Stack({ alignContent: Alignment.TopStart }) { 72 Text(this.updateCellularType(this.cellularType)) 73 .fontSize(this.cellularType == Constants.RADIO_TECHNOLOGY_UNKNOWN ? this.statusBarSignalUnknownFontSize : this.statusBarSignalTypeFontSize) 74 .fontColor(this.mStatusBarGroupComponentData.contentColor) 75 .width(this.netSignalTextMaxWidth) 76 .fontWeight(FontWeight.Bold) 77 .textAlign(TextAlign.Start) 78 .margin({ left: $r("app.float.signal_margin") }) 79 Image(this.updateCellularImage(this.cellularLevel)) 80 .objectFit(ImageFit.Contain) 81 .width(this.cellularImageWidth) 82 .height(this.cellularImageHeight) 83 .fillColor(this.mStatusBarGroupComponentData.contentColor) 84 }.flexShrink(1) 85 86 Row().width(this.statusBarMarginLeftRight).height('100%') 87 } 88 .height('100%') 89 } 90 91 /** 92 * Get the string of cellular type 93 * 94 * @param {number} type - number of cellular type 95 * @return {string} typeString type of cellular type 96 */ 97 private updateCellularType(signalType): string { 98 let typeString; 99 switch (signalType) { 100 case Constants.RADIO_TECHNOLOGY_UNKNOWN: 101 typeString = $r('app.string.signal_null'); 102 break; 103 case Constants.RADIO_TECHNOLOGY_GSM: 104 case Constants.RADIO_TECHNOLOGY_1XRTT: 105 typeString = $r('app.string.2G'); 106 break; 107 case Constants.RADIO_TECHNOLOGY_WCDMA: 108 case Constants.RADIO_TECHNOLOGY_HSPA: 109 case Constants.RADIO_TECHNOLOGY_HSPAP: 110 case Constants.RADIO_TECHNOLOGY_TD_SCDMA: 111 case Constants.RADIO_TECHNOLOGY_EVDO: 112 case Constants.RADIO_TECHNOLOGY_EHRPD: 113 typeString = $r('app.string.3G'); 114 break; 115 case Constants.RADIO_TECHNOLOGY_LTE: 116 case Constants.RADIO_TECHNOLOGY_LTE_CA: 117 case Constants.RADIO_TECHNOLOGY_IWLAN: 118 typeString = $r('app.string.4G'); 119 break; 120 case Constants.RADIO_TECHNOLOGY_NR: 121 typeString = $r('app.string.5G'); 122 break; 123 default: 124 typeString = $r('app.string.signal_null'); 125 break; 126 } 127 return typeString; 128 } 129 130 /** 131 * Get the cellular signal image 132 * 133 * @param {number} level - signal level from signalModel 134 * @return {string} cellularImage image of cellular signal 135 */ 136 private updateCellularImage(level) { 137 let cellularImage; 138 switch (level) { 139 case Constants.CELLULAR_SIGNAL_NO: 140 cellularImage = $r('app.media.ic_statusbar_signal_no'); 141 break; 142 case Constants.CELLULAR_SIGNAL_MIN: 143 cellularImage = $r('app.media.ic_statusbar_signal_1'); 144 break; 145 case Constants.CELLULAR_SIGNAL_LOW: 146 cellularImage = $r('app.media.ic_statusbar_signal_2'); 147 break; 148 case Constants.CELLULAR_SIGNAL_HALF: 149 cellularImage = $r('app.media.ic_statusbar_signal_3'); 150 break; 151 case Constants.CELLULAR_SIGNAL_HIGH: 152 cellularImage = $r('app.media.ic_statusbar_signal_4'); 153 break; 154 case Constants.CELLULAR_SIGNAL_FULL: 155 cellularImage = $r('app.media.ic_statusbar_signal_full'); 156 break; 157 case Constants.CELLULAR_NO_SIM_CARD: 158 default: 159 cellularImage = $r('app.media.ic_statusbar_signal_no'); 160 break; 161 } 162 return cellularImage; 163 } 164 165 /* 166 * Get the NetworkState signal name 167 * 168 * @param {string} netWorkState - network state from signal model 169 * @return {string} vendor's name or signal state 170 */ 171 private updateNetworkState(netWorkState) { 172 let networkStateName; 173 if (netWorkState == Constants.NET_NULL) { 174 networkStateName = $r('app.string.net_null'); 175 } else { 176 networkStateName = netWorkState; 177 } 178 return networkStateName; 179 } 180}