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