• 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
16
17import { ColumnOperation } from '../components/ColumnOperation';
18import { showAlertDialog } from '../util/AlertDialogUtil';
19import { PowerManagerUtil } from '../util/PowerManagerUtil';
20import { SelectDialog } from '../components/SelectDialog';
21import { getString, getStringArray } from '@ohos/common/src/main/ets/util/ResourceUtil';
22import power from '@ohos.power';
23
24@Preview
25@Component
26export struct PowerManager {
27  @State result: string = '';
28  @State powerModeNames: Array<string> = [];
29  dialogController: CustomDialogController = new CustomDialogController({
30    builder: SelectDialog({
31      menus: this.powerModeNames,
32      doOperation: (index) => {
33        this.setPowerMode(PowerManagerUtil.powerModes[index])
34      }
35    }),
36    alignment: DialogAlignment.Center,
37    offset: { dx: 0, dy: -20 },
38    gridCount: 4,
39    customStyle: false
40  });
41
42  build() {
43    Scroll() {
44      Column() {
45        Row() {
46          Text(this.result)
47            .fontWeight(FontWeight.Medium)
48            .fontSize(20)
49        }
50        .alignItems(VerticalAlign.Top)
51        .width('100%')
52        .backgroundColor($r("app.color.white"))
53        .height(160)
54        .padding(16)
55        .borderRadius(20)
56        .margin({ top: 16 })
57
58        ColumnOperation({ operationRes: $r('app.strarray.power_operations'), doOperation: this.doOperation })
59      }
60      .width('100%')
61      .padding(4)
62    }
63    .scrollBar(BarState.Off)
64  }
65
66  async setPowerMode(mode: power.DevicePowerMode) {
67    try {
68      await PowerManagerUtil.setPowerMode(mode);
69      this.result = `${getString($r('app.string.power_mode'))}${await PowerManagerUtil.getPowerModeName()}`;
70    } catch (err) {
71      this.result = `${getString($r('app.string.power_mode'))}${JSON.stringify(err)}`;
72    }
73  }
74
75  doOperation = async (index: number) => {
76    switch (index) {
77      case 0:
78        PowerManagerUtil.suspend();
79        this.result = `${getString($r('app.string.suspend_status'))}true`;
80        break
81      case 1:
82        showAlertDialog($r('app.string.turnOff'), $r('app.string.questionTurnOff'), () => {
83          this.result = getString($r('app.string.turnOff'));
84          PowerManagerUtil.shutdown('shutdown_test');
85        })
86        break
87      case 2:
88        showAlertDialog($r('app.string.restart'), $r('app.string.questionRestart'), () => {
89          this.result = getString($r('app.string.restart'));
90          PowerManagerUtil.reboot('reboot_test');
91        })
92        break
93      case 3:
94        this.powerModeNames = await getStringArray($r('app.strarray.power_mode'));
95        this.dialogController.open();
96        break
97      default:
98        break
99    }
100  }
101}