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 '@ohos/common' 19import {StatusBarGroupComponentData} from '../../../../../../screenlock/src/main/ets/com/ohos/common/constants' 20import StatusBarVM from '../../../../../../screenlock/src/main/ets/com/ohos/vm/StatusBarVM' 21 22const TAG = 'SignalComponent-SignalIcon' 23 24@Component 25export default struct SignalIcon { 26 @StorageLink('cellularLevel_screenLock') cellularLevel: number = Constants.CELLULAR_NO_SIM_CARD 27 @StorageLink('cellularType_screenLock') cellularType: number = Constants.RADIO_TECHNOLOGY_UNKNOWN 28 @StorageLink('networkState_screenLock') networkState: string = Constants.NET_NULL 29 @StorageLink('signalObserved') signalObserved: boolean = false 30 private mGroupId: string = '' 31 @State mStatusBarGroupComponentData: StatusBarGroupComponentData = new StatusBarGroupComponentData() 32 @State statusBarMarginLeftRight: Resource = $r("app.float.signal_status_margin_Left_right") 33 @State statusBarFontSize: Resource = $r("app.float.signal_fontSize") 34 @State signalTextMaxWeight: Resource = $r('app.float.signal_text_max_width') 35 @State statusBarSignalUnknownFontSize: Resource = $r('app.float.status_bar_signal_unknown_font_size') 36 @State statusBarSignalTypeFontSize: Resource = $r('app.float.status_bar_signal_type_font_size') 37 @State netSignalTextMaxWidth: Resource = $r('app.float.status_bar_signal_net_signal_text_max_width') 38 @State cellularImageWidth: Resource = $r('app.float.signal_component_icon_width') 39 @State cellularImageHeight: Resource = $r('app.float.signal_component_icon_height') 40 41 aboutToAppear() { 42 Log.showInfo(TAG, 'aboutToAppear'); 43 this.mStatusBarGroupComponentData = StatusBarVM.getStatusBarGroupComponentData(this.mGroupId) 44 if (!this.signalObserved) { 45 mSignalModel.initSignalModel(); 46 this.signalObserved = true; 47 } 48 } 49 50 aboutToDisappear() { 51 Log.showInfo(TAG, 'aboutToDisappear'); 52 } 53 54 build() { 55 Row() { 56 Row().width(this.statusBarMarginLeftRight).height('100%') 57 Text(this.updateNetworkState(this.networkState)) 58 .fontSize(this.statusBarFontSize) 59 .fontWeight(FontWeight.Medium) 60 .fontColor(this.mStatusBarGroupComponentData.contentColor) 61 .textOverflow({ overflow: TextOverflow.Ellipsis }) 62 .constraintSize({ maxWidth: this.signalTextMaxWeight }) 63 .flexShrink(0) 64 .maxLines(1) 65 .textAlign(TextAlign.Center) 66 Row().width(this.statusBarMarginLeftRight).height('100%') 67 68 Stack({ alignContent: Alignment.TopStart }) { 69 Text(this.updateCellularType(this.cellularType)) 70 .fontSize(this.cellularType == Constants.RADIO_TECHNOLOGY_UNKNOWN ? this.statusBarSignalUnknownFontSize : this.statusBarSignalTypeFontSize) 71 .fontColor(this.mStatusBarGroupComponentData.contentColor) 72 .width(this.netSignalTextMaxWidth) 73 .fontWeight(FontWeight.Bold) 74 .textAlign(TextAlign.Start) 75 .margin({ left: $r("app.float.signal_margin") }) 76 Image(this.updateCellularImage(this.cellularLevel)) 77 .objectFit(ImageFit.Contain) 78 .width(this.cellularImageWidth) 79 .height(this.cellularImageHeight) 80 .fillColor(this.mStatusBarGroupComponentData.contentColor) 81 }.flexShrink(1) 82 83 Row().width(this.statusBarMarginLeftRight).height('100%') 84 } 85 .height('100%') 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): string | Resource { 95 let typeString: Resource | string | undefined = undefined;; 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 | undefined = undefined; 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: Resource | string | undefined = undefined; 170 if (netWorkState == Constants.NET_NULL) { 171 networkStateName = $r('app.string.net_null'); 172 } else { 173 networkStateName = netWorkState; 174 } 175 return networkStateName; 176 } 177}