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 UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 17import { GetAlertMessage } from '../common/AlertMessage/GetAlertMessage'; 18import Want from '@ohos.app.ability.Want'; 19import { BusinessError } from '@ohos.base'; 20import { HiLog } from '../common/HiLog'; 21import { AlertDialog } from '@kit.ArkUI'; 22import Constants from '../common/constant'; 23 24const TAG = 'PhoneDialog'; 25 26@Entry() 27@Component 28struct Index { 29 private want = AppStorage.get('PhoneDialogUIExtWant') as Want | undefined; 30 private session = AppStorage.get('PhoneDialogUIExtSession') as UIExtensionContentSession | undefined; 31 private errorDialog: CustomDialogController | null = null; 32 33 private cancelAction(): void { 34 HiLog.info(TAG, 'cancelAction'); 35 if (!this.session) { 36 return; 37 } 38 try { 39 this.session.terminateSelfWithResult({ resultCode: 0 }); 40 } catch (error) { 41 HiLog.wrapError(TAG, error, 'PhoneDialog terminateSelfWithResult failed'); 42 } 43 } 44 45 private getDialogConfig(message: Resource): void { 46 if (!this.errorDialog) { 47 this.errorDialog = new CustomDialogController({ 48 builder: AlertDialog({ 49 content: message, 50 primaryButton: { 51 value: $r('app.string.da_button'), 52 action: () => { 53 this.cancelAction(); 54 } 55 } 56 }), 57 cancel: () => { 58 this.cancelAction(); 59 } 60 }); 61 } 62 } 63 64 private showErrorDialog(): void { 65 try { 66 let errorMsg = {} as BusinessError; 67 const errorCode = this.want?.parameters?.errorCode ?? Constants.DEFAULT_ERROR_CODE; 68 const errorMessage = this.want?.parameters?.errorMessage ?? ''; 69 errorMsg.code = errorCode as number; 70 errorMsg.message = errorMessage as string; 71 const errInfo: Record<string, Resource> = GetAlertMessage.getAlertMessage(errorMsg); 72 this.getDialogConfig(errInfo.msg); 73 this.errorDialog?.open(); 74 } catch (err) { 75 HiLog.error(TAG, `showErrorDialog failed: ${JSON.stringify(err)}`); 76 this.cancelAction(); 77 } 78 } 79 80 aboutToAppear() { 81 HiLog.info(TAG, 'PhoneDialog aboutToAppear'); 82 this.showErrorDialog(); 83 } 84 85 aboutToDisappear() { 86 HiLog.info(TAG, 'PhoneDialog aboutToDisappear'); 87 this.errorDialog?.close(); 88 this.errorDialog = null; 89 } 90 91 build() { 92 } 93} 94