1/* 2 * Copyright (c) 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 UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 17import power from '@ohos.power'; 18import Constants from '../common/constant'; 19 20@Extend(Button) function customizeButton() { 21 .backgroundColor(Color.Transparent) 22 .fontColor($r('app.color.button_text_color')) 23 .fontSize(Constants.BUTTON_TEXT_FONT_SIZE) 24 .fontWeight(Constants.BUTTON_TEXT_FONT_WEIGHT) 25 .height(Constants.BUTTON_HEIGHT) 26 .width(Constants.BUTTON_WIDTH) 27} 28 29@CustomDialog 30struct PowerCustomDialog { 31 controller: CustomDialogController 32 cancel: () => void 33 shutdown: () => void 34 reboot: () => void 35 36 build() { 37 Column() { 38 Column() { 39 Row() { 40 Text($r('app.string.TEXT_POWER_OPTIONS_POWERDIALOG')) 41 .fontSize(Constants.DIALOG_TITLE_FONT_SIZE) 42 .fontColor($r('app.color.title_color')) 43 .fontWeight(Constants.DIALOG_TITLE_FONT_WEIGHT) 44 .lineHeight(Constants.DIALOG_TITLE_LINE_HEIGHT) 45 .opacity(Constants.DIALOG_TITLE_OPACITY) 46 } 47 }.margin({ 48 top: Constants.DIALOG_TITLE_MARGIN_TOP, 49 bottom: Constants.DIALOG_TITLE_MARGIN_BOTTOM 50 }) 51 Row() { 52 Button($r('app.string.BUTTON_CANCEL_POWERDIALOG')) 53 .onClick(() => { 54 this.controller.close() 55 this.cancel(); 56 }) 57 .customizeButton() 58 .margin({right: Constants.BUTTON_MARGIN}) 59 Text('|') 60 .fontSize(Constants.SPLIT_FONT_SIZE) 61 .fontColor($r('app.color.split_color')) 62 Button($r('app.string.BUTTON_REBOOT_POWERDIALOG')) 63 .onClick(() => { 64 this.reboot() 65 }) 66 .customizeButton() 67 .margin({left: Constants.BUTTON_MARGIN, right: Constants.BUTTON_MARGIN}) 68 Text('|') 69 .fontSize(Constants.SPLIT_FONT_SIZE) 70 .fontColor($r('app.color.split_color')) 71 Button($r('app.string.BUTTON_SHUTDOWN_POWERDIALOG')) 72 .onClick(() => { 73 this.shutdown() 74 }) 75 .customizeButton() 76 .margin({left: Constants.BUTTON_MARGIN}) 77 } 78 } 79 .backgroundColor($r('app.color.default_background_color')) 80 .borderRadius(Constants.DIALOG_BORDER_RADIUS) 81 .width(Constants.DIALOG_WIDTH) 82 .height(Constants.DIALOG_HEIGHT) 83 } 84} 85 86@Entry 87@Component 88struct PowerDialog { 89 dialogController: CustomDialogController = new CustomDialogController({ 90 builder: PowerCustomDialog({ 91 cancel: this.onCancel, 92 shutdown: () => { this.onShutdown() }, 93 reboot: () => { this.onReboot() } 94 }), 95 cancel: this.existApp, 96 autoCancel: false, 97 alignment: DialogAlignment.Center, 98 offset: { dx: 0, dy: -20 }, 99 gridCount: 4, 100 customStyle: true 101 }); 102 103 timeoutId: number; 104 timeoutMs: number = 20; 105 106 aboutToDisappear() { 107 clearTimeout(this.timeoutId); 108 } 109 110 onCancel() { 111 try { 112 console.log('power dialog terminateSelf'); 113 let storage = LocalStorage.GetShared() 114 let session = storage.get<UIExtensionContentSession>('session'); 115 session.terminateSelf(); 116 } catch(err) { 117 console.log('power dialog cancel failed: ' + JSON.stringify(err)); 118 } 119 } 120 121 onShutdown() { 122 try { 123 this.timeoutId = setTimeout(this.onCancel, this.timeoutMs); 124 power.shutdown("power_dialog"); 125 } catch(err) { 126 console.log('power dialog shutdown failed: ' + JSON.stringify(err)); 127 } 128 } 129 130 onReboot() { 131 try { 132 this.timeoutId = setTimeout(this.onCancel, this.timeoutMs); 133 power.reboot("power_dialog"); 134 } catch(err) { 135 console.log('power dialog reboot failed: ' + JSON.stringify(err)); 136 } 137 } 138 139 existApp() { 140 this.onCancel(); 141 } 142 143 build() { 144 Column(this.dialogController.open()) {} 145 } 146} 147