• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 systemTime from '@ohos.systemDateTime';
17import { ListModel } from '../model/ListModel';
18import thermal from '@ohos.thermal';
19import { ThermalUtil } from '../util/ThermalUtil';
20import promptAction from '@ohos.promptAction';
21
22@Extend(Text) function fancy() {
23  .height(50)
24  .fontSize(16)
25  .fontWeight(500)
26}
27
28@Component
29export struct Thermal {
30  @State table: ListModel[] = [
31    new ListModel('Thermal level', ThermalUtil.getLevel),
32    new ListModel('Callback level', undefined),
33    new ListModel('Last callback time', undefined)
34  ];
35
36  aboutToAppear() {
37    this.initThermal();
38  }
39
40  aboutToDisappear() {
41    ThermalUtil.unregisterThermalLevelCallback();
42  }
43
44  build() {
45    List() {
46      ForEach(this.table, (item: ListModel) => {
47        ListItem() {
48          Row() {
49            Text(item.title)
50              .fontSize(20)
51              .fontWeight(FontWeight.Medium)
52              .fontColor($r("app.color.list_title"))
53              .textAlign(TextAlign.Start)
54            Blank()
55            Text(item.getValue())
56              .fontSize(18)
57              .fontWeight(FontWeight.Medium)
58              .fontColor($r("app.color.list_sub_content"))
59              .textAlign(TextAlign.End)
60          }
61          .padding({ left: 16, right: 16 })
62          .height(62)
63          .width('100%')
64        }
65      })
66    }
67    .divider({
68      strokeWidth: px2vp(1),
69      color: $r("app.color.divider"),
70      startMargin: 16,
71      endMargin: 16
72    })
73    .borderRadius(20)
74    .backgroundColor($r("app.color.white"))
75    .margin({
76      top: 16,
77      bottom: 16,
78      left: 4,
79      right: 4
80    })
81    .width('100%')
82  }
83
84  initThermal() {
85    const INDEX_0 = 0;
86    const INDEX_1 = 1;
87    ThermalUtil.registerThermalLevelCallback((level: number) => {
88      this.table[INDEX_0] = new ListModel('Thermal level', undefined, ThermalUtil.getLevel());
89      this.table[INDEX_1] = new ListModel('Callback level', undefined, ThermalUtil.getStrLevel(level));
90      this.getCurrentTime();
91      if (level === thermal.ThermalLevel.WARNING) {
92        try {
93          promptAction.showToast({
94            message: $r('app.string.thermal_warning'),
95            duration: 2000,
96          });
97        } catch (error) {
98          console.error(`showToast args error code is ${error.code}, message is ${error.message}`);
99        }
100      }
101    })
102  }
103
104  getCurrentTime() {
105    const INDEX_2 = 2;
106    systemTime.getCurrentTime().then((date: number) => {
107      this.table[INDEX_2] = new ListModel('Last callback time', undefined, ThermalUtil.timestampToTime(date));
108    })
109  }
110}