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