• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Fujian Newland Auto-ID Tech.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 */
15import BatteryInfoUtil from '../util/BatteryInfoUtil'
16import { logger, getString } from '@ohos/common'
17
18/**
19 * 电池信息组件
20 * 用于显示电池基础信息,包括:电池剩余电量、充电状态、健康状态、充电器类型、电压、技术型号、温度、电量等级、充电类型
21 */
22
23const TAG: string = '[Sample_BatteryView]'
24
25@Component
26struct BatteryInfoView {
27  private mName: string | Resource = '';
28  private mIsLine?: boolean = true
29  private mViewID?: string = ''
30  @Prop mValue: string
31
32  @Builder CustomDivider() {
33    Divider()
34      .strokeWidth('1px')
35      .color($r('sys.color.ohos_id_color_subheading_separator'))
36      .margin({
37        bottom: px2vp(8)
38      })
39  }
40
41  build() {
42    Column() {
43      Row() {
44        Text(this.mName)
45          .fontColor($r('sys.color.ohos_id_color_text_primary'))
46          .fontSize($r('sys.float.ohos_id_text_size_sub_title2'))
47          .fontWeight(FontWeight.Regular)
48          .width('30%')
49          .maxLines(2)
50          .textOverflow({
51            overflow: TextOverflow.Ellipsis
52          })
53          .id(this.mViewID)
54        Blank()
55        Text(this.mValue)
56          .fontColor($r('app.color.battery_info_value_text'))
57          .fontSize($r('sys.float.ohos_id_text_size_sub_title3'))
58          .width('40%')
59          .textAlign(TextAlign.End)
60      }
61      .width('100%')
62      .height(47)
63      .justifyContent(FlexAlign.Start)
64      .alignItems(VerticalAlign.Center)
65
66      if (this.mIsLine) {
67        Blank()
68        this.CustomDivider()
69      }
70    }
71    .width('100%')
72    .height(48)
73  }
74}
75
76@Preview
77@Component
78export struct BatteryView {
79  @StorageLink('BatteryPresent') isBatteryPresent: boolean = true // 是否支持电池或电池是否在位
80  @StorageLink('BatterySOC') mBatterySOC: string = '0' // 电池电量
81  @StorageLink('ChargingStatus') mChargingStatus: string = '' // 电池充电状态
82  @StorageLink('HealthStatus') mHealthStatus: string = '' // 电池健康状态
83  @StorageLink('PluggedType') mPluggedType: string = '' // 电池充电器类型
84  @StorageLink('Voltage') mVoltage: string = '' // 电池电压
85  @StorageLink('Technology') mTechnology: string = '' // 电池技术型号
86  @StorageLink('Temperature') mTemperature: string = '' // 电池温度
87  @StorageLink('CapacityLevel') mCapacityLevel: string = '' // 电池电量等级
88  private BATTERY_VOLTAGE_SYMBOL: string = ''
89  private BATTERY_TEMPERATURE_SYMBOL: string = ''
90
91  async aboutToAppear() {
92    logger.info(`${TAG} aboutToAppear`)
93    this.BATTERY_VOLTAGE_SYMBOL = getString($r('app.string.battery_voltage_symbol'))
94    this.BATTERY_TEMPERATURE_SYMBOL = getString($r('app.string.battery_temperature_symbol'))
95    // 注册电池电量数据变化监听器
96    BatteryInfoUtil.registerEventBatteryChanged()
97    // 获取电池相关信息
98    this.getInfo()
99  }
100
101  private getInfo() {
102    logger.info(`${TAG} aboutToAppear isBatteryPresent: ${this.isBatteryPresent}`)
103    // 初始化电量信息
104    if (this.isBatteryPresent) {
105      BatteryInfoUtil.getBatterySOC()
106      BatteryInfoUtil.getChargingStatus()
107      BatteryInfoUtil.getHealthStatus()
108      BatteryInfoUtil.getPluggedType()
109      BatteryInfoUtil.getVoltage()
110      BatteryInfoUtil.getTechnology()
111      BatteryInfoUtil.getBatteryTemperature()
112      BatteryInfoUtil.getBatteryCapacityLevel()
113    }
114  }
115
116  aboutToDisappear() {
117    logger.info(`${TAG} aboutToDisappear`)
118    BatteryInfoUtil.unregisterEventBatteryChanged()
119  }
120
121  build() {
122    Scroll() {
123      Column() {
124        Stack({
125          alignContent: Alignment.Center
126        }) {
127          DataPanel({
128            values: [this.isBatteryPresent ? Number(this.mBatterySOC) : 0],
129            max: 100,
130            type: DataPanelType.Circle }
131          )
132            .id('batterySOC')
133            .width(280)
134            .height(280)
135
136
137          Column() {
138            Row() {
139              Text(this.isBatteryPresent ? this.mBatterySOC : $r('app.string.battery_soc_0'))
140                .fontColor($r('app.color.battery_soc_text'))
141                .fontSize($r('sys.float.ohos_id_text_size_headline3'))
142              Text($r('app.string.battery_soc_100'))
143                .fontColor($r('sys.color.ohos_id_color_text_primary'))
144                .fontSize($r('sys.float.ohos_id_text_size_headline8'))
145                .padding({
146                  bottom: px2vp(15)
147                })
148            }
149            .alignItems(VerticalAlign.Bottom)
150            .justifyContent(FlexAlign.Center)
151            .margin({
152              bottom: px2vp(4)
153            })
154
155            if (this.isBatteryPresent) {
156              Text(this.mHealthStatus)
157                .fontColor($r('sys.color.ohos_id_color_text_secondary'))
158                .fontSize($r('sys.float.ohos_id_text_size_body1'))
159            }
160          }
161        }
162        .width('100%')
163        .height(px2vp(390))
164
165        if (this.isBatteryPresent) {
166          Column({
167            space: px2vp(8)
168          }) {
169            BatteryInfoView({
170              mName: $r('app.string.battery_charging_status'),
171              mValue: this.mChargingStatus.toString(),
172              mViewID: 'batteryChargingStatus'
173            })
174
175            BatteryInfoView({
176              mName: $r('app.string.battery_plugged_type'),
177              mValue: this.mPluggedType.toString(),
178              mViewID: 'batteryPluggedType'
179            })
180
181            BatteryInfoView({
182              mName: $r('app.string.battery_voltage'),
183              mValue: `${this.mVoltage} ${this.BATTERY_VOLTAGE_SYMBOL}`,
184              mViewID: 'batteryVoltage'
185            })
186
187            BatteryInfoView({
188              mName: $r('app.string.battery_technology'),
189              mValue: this.mTechnology.toString(),
190              mViewID: 'batteryTechnology'
191            })
192
193            BatteryInfoView({
194              mName: $r('app.string.battery_temperature'),
195              mValue: `${this.mTemperature} ${this.BATTERY_TEMPERATURE_SYMBOL}`,
196              mViewID: 'batteryTemperature'
197            })
198
199            BatteryInfoView({
200              mName: $r('app.string.battery_capacity_level'),
201              mIsLine: false,
202              mValue: this.mCapacityLevel.toString(),
203              mViewID: 'batteryCapacityLevel'
204            })
205          }
206          .padding(px2vp(24))
207          .margin(px2vp(24))
208          .backgroundColor($r('sys.color.ohos_id_color_list_card_bg'))
209          .border({
210            radius: $r('sys.float.ohos_id_corner_radius_default_l')
211          })
212          .id('batteryContext')
213        } else {
214          Row({ space: 10 }) {
215            Image($r('app.media.icon_search_fail'))
216              .width(px2vp(32))
217              .height(px2vp(32))
218              .objectFit(ImageFit.Fill)
219              .id('batteryFailIcon')
220            Text($r('app.string.battery_information_error'))
221              .fontColor($r('sys.color.ohos_id_color_text_secondary'))
222              .fontSize($r('sys.float.ohos_id_text_size_body1'))
223              .id('batteryErrorText')
224          }
225          .height('40%')
226          .id('batterySearchFail')
227        }
228      }
229      .width('100%')
230      .height('100%')
231    }
232    .height('100%')
233  }
234}