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 { common } from '@kit.AbilityKit'; 17import { deviceInfo } from '@kit.BasicServicesKit'; 18import { GetAlertMessage } from '../../common/AlertMessage/GetAlertMessage'; 19import { BusinessError } from '@ohos.base'; 20import { HiLog } from '../../common/HiLog'; 21import Constants from '../../common/constant'; 22import { OpenDlpFileError } from '../common/OpenDlpFileError/OpenDlpFileError'; 23 24const TAG: string = 'ErrorManager'; 25 26abstract class ErrorManager { 27 public abstract handle(error: BusinessError): Promise<void>; 28} 29 30class PhoneManager extends ErrorManager { 31 private async requestPhoneDialog(context: common.ServiceExtensionContext, error: BusinessError) { 32 let code = error.code; 33 let message = error.message; 34 let uiExtWant: Want = { 35 bundleName: Constants.DLP_MANAGER_BUNDLE_NAME, 36 abilityName: 'PhoneDialogUIExtAbility', 37 moduleName: 'entry', 38 parameters: { 39 'ability.want.params.uiExtensionType': 'sys/commonUI', 40 'errorCode': code, 41 'errorMessage': message, 42 } 43 }; 44 try { 45 await context.requestModalUIExtension(uiExtWant); 46 HiLog.info(TAG, 'requestPhoneDialog success'); 47 } catch (err) { 48 HiLog.wrapError(TAG, err, 'requestModalUIExtension failed'); 49 } 50 } 51 52 public async handle(error: BusinessError): Promise<void> { 53 const viewContext = AppStorage.get('viewContext') as common.ServiceExtensionContext; 54 if (OpenDlpFileError.NOT_NEED_TOAST.has(error.code)) { 55 HiLog.info(TAG, `PhoneManager not need toast, code ${error.code}`); 56 return; 57 } 58 if (OpenDlpFileError.NEED_DIALOG.has(error.code)) { 59 HiLog.info(TAG, `PhoneManager need dialog, code ${error.code}`); 60 await this.requestPhoneDialog(viewContext, error); 61 return; 62 } 63 await GetAlertMessage.viewAbilityPhoneToast(viewContext, error); 64 } 65} 66 67class PCManager extends ErrorManager { 68 public async handle(error: BusinessError): Promise<void> { 69 const viewContext = AppStorage.get('viewContext') as common.ServiceExtensionContext; 70 if (OpenDlpFileError.NOT_NEED_TOAST.has(error.code)) { 71 HiLog.info(TAG, `PCManager not need toast, code ${error.code}`); 72 return; 73 } 74 await GetAlertMessage.requestModalUIExtension(viewContext, error); 75 } 76} 77 78export class ErrorHandlerFactory { 79 static createErrorHandle(): ErrorManager { 80 if (deviceInfo.deviceType !== Constants.DEVICE_2IN1) { 81 HiLog.info(TAG, 'create PhoneHandle'); 82 return new PhoneManager(); 83 } 84 HiLog.info(TAG, 'create PCHandle'); 85 return new PCManager(); 86 } 87}