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