1/* 2 * Copyright (c) 2025 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 { CustomContentDialog } from '@ohos.arkui.advanced.Dialog'; 17import { HiLog } from '../common/HiLog'; 18import Constants from '../common/constant'; 19import { emitter } from '@kit.BasicServicesKit'; 20import OpeningDialogService from '../rpc/OpeningDialog/service/OpeningDialogService'; 21 22const TAG = 'OpeningDialog'; 23 24@Entry() 25@Component 26struct Index { 27 private timeoutId250: number | null = null; 28 private timeoutId10: number | null = null; 29 private shouldCloseAfterTimeout: boolean = true; 30 private receivedCloseSignal: boolean = false; 31 private requestId: string = ''; 32 private dlpAlertDialog: CustomDialogController = new CustomDialogController({ 33 builder: CustomContentDialog({ 34 contentBuilder: () => { 35 this.contentBuilder(); 36 } 37 }), 38 autoCancel: false, 39 customStyle: false, 40 alignment: DialogAlignment.Center 41 }); 42 private handleShowDialog = (eventData: emitter.EventData) => { 43 HiLog.info(TAG, 'receive SHOW_DIALOG_EVENT'); 44 this.handleShowDialogEvent(eventData.data?.showDialog, eventData.data?.requestId); 45 }; 46 private handleScreenOff = async () => { 47 HiLog.info(TAG, 'receive SCREEN_OFF_EVENT'); 48 this.closeDialog(); 49 await OpeningDialogService.getInstance().terminateOpeningDialog(); 50 }; 51 52 aboutToAppear(): void { 53 HiLog.info(TAG, 'OpeningDialog aboutToAppear'); 54 emitter.on(Constants.SHOW_DIALOG_EVENT, this.handleShowDialog); 55 emitter.on(Constants.SCREEN_OFF_EVENT, this.handleScreenOff); 56 } 57 58 aboutToDisappear(): void { 59 HiLog.info(TAG, 'OpeningDialog aboutToDisappear'); 60 emitter.off(Constants.SHOW_DIALOG_EVENT, this.handleShowDialog); 61 emitter.off(Constants.SCREEN_OFF_EVENT, this.handleScreenOff); 62 this.closeDialog(); 63 } 64 65 handleShowDialogEvent(showDialog: boolean, requestId: string) { 66 HiLog.info(TAG, `handleShowDialogEvent showDialog ${showDialog}, requestId ${requestId}`); 67 if (showDialog) { 68 this.requestId = requestId; 69 this.dlpAlertDialog.open(); 70 this.startTimeoutClose(); 71 return; 72 } 73 74 this.receivedCloseSignal = true; 75 HiLog.info(TAG, `handleShowDialogEvent receivedCloseSignal ${this.receivedCloseSignal}`); 76 if (this.shouldCloseAfterTimeout) { 77 HiLog.info(TAG, 'closeDialog by event'); 78 this.closeDialog(); 79 } 80 } 81 82 private startTimeoutClose() { 83 if (this.timeoutId250) { 84 clearTimeout(this.timeoutId250); 85 this.timeoutId250 = null; 86 } 87 if (this.timeoutId10) { 88 clearTimeout(this.timeoutId10); 89 this.timeoutId10 = null; 90 } 91 this.shouldCloseAfterTimeout = false; 92 this.timeoutId250 = setTimeout(async () => { 93 this.shouldCloseAfterTimeout = true; 94 HiLog.info(TAG, `timeout receivedCloseSignal ${this.receivedCloseSignal}`); 95 if (this.receivedCloseSignal) { 96 HiLog.info(TAG, 'closeDialog by timeout'); 97 this.closeDialog(); 98 } 99 await OpeningDialogService.getInstance().dialogTimeout(this.requestId); 100 }, Constants.OPENING_DIALOG_TIMEOUT); 101 102 this.timeoutId10 = setTimeout(async () => { 103 HiLog.error(TAG, 'closeDialog by decrypt timeout'); 104 await this.closeDialog(); 105 await OpeningDialogService.getInstance().terminateOpeningDialog(); 106 }, Constants.OPENING_DIALOG_TIMEOUT_TIME); 107 } 108 109 private async closeDialog() { 110 if (this.timeoutId250) { 111 clearTimeout(this.timeoutId250); 112 this.timeoutId250 = null; 113 } 114 if (this.timeoutId10) { 115 clearTimeout(this.timeoutId10); 116 this.timeoutId10 = null; 117 } 118 this.dlpAlertDialog.close(); 119 await OpeningDialogService.getInstance().dialogDisappear(this.requestId); 120 } 121 122 build() { 123 } 124 125 @Builder 126 contentBuilder() { 127 Column() { 128 Row() { 129 Text($r('app.string.file_opening')) 130 .fontSize($r('sys.float.Subtitle_M')) 131 .fontWeight(FontWeight.Regular) 132 .fontColor($r('sys.color.font_primary')) 133 .layoutWeight(1) 134 .maxLines(10) 135 .textOverflow({ overflow: TextOverflow.Ellipsis }) 136 LoadingProgress() 137 .color($r('sys.color.icon_secondary')) 138 .width('40vp') 139 .height('40vp') 140 .margin({ left: 12 }) 141 } 142 .constraintSize({ minHeight: 48 }) 143 .onDisAppear(() => { 144 HiLog.info(TAG, 'contentBuilder onDisAppear'); 145 this.closeDialog(); 146 }) 147 } 148 } 149}