1df226684Sopenharmony_ci/* 2df226684Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3df226684Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4df226684Sopenharmony_ci * you may not use this file except in compliance with the License. 5df226684Sopenharmony_ci * You may obtain a copy of the License at 6df226684Sopenharmony_ci * 7df226684Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8df226684Sopenharmony_ci * 9df226684Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10df226684Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11df226684Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12df226684Sopenharmony_ci * See the License for the specific language governing permissions and 13df226684Sopenharmony_ci * limitations under the License. 14df226684Sopenharmony_ci */ 15df226684Sopenharmony_ci 16df226684Sopenharmony_ciimport UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 17df226684Sopenharmony_ciimport { CustomContentDialog, ButtonOptions } from '@ohos.arkui.advanced.Dialog'; 18df226684Sopenharmony_ciimport GlobalContext from '../common/GlobalContext'; 19df226684Sopenharmony_ciimport { GetAlertMessage } from '../common/GetAlertMessage'; 20df226684Sopenharmony_ciimport common from '@ohos.app.ability.common'; 21df226684Sopenharmony_ciimport Want from '@ohos.app.ability.Want'; 22df226684Sopenharmony_ciimport { BusinessError } from '@ohos.base'; 23df226684Sopenharmony_ciimport Constants from '../common/constant'; 24df226684Sopenharmony_ciimport ability from '@ohos.ability.ability'; 25df226684Sopenharmony_ciimport account_osAccount from '@ohos.account.osAccount'; 26df226684Sopenharmony_ciimport dlpPermission from '@ohos.dlpPermission'; 27df226684Sopenharmony_ciimport { sendDlpManagerAccountLogin, checkNetworkStatus } from '../common/utils'; 28df226684Sopenharmony_ciimport { HiLog } from '../common/HiLog'; 29df226684Sopenharmony_ci 30df226684Sopenharmony_ciconst TAG = 'Alert'; 31df226684Sopenharmony_cilet abilityResult: ability.AbilityResult = { 32df226684Sopenharmony_ci 'resultCode': 0, 33df226684Sopenharmony_ci 'want': {} 34df226684Sopenharmony_ci}; 35df226684Sopenharmony_ci 36df226684Sopenharmony_cilet storage = LocalStorage.getShared(); 37df226684Sopenharmony_ci@Entry(storage) 38df226684Sopenharmony_ci@Component 39df226684Sopenharmony_cistruct Index { 40df226684Sopenharmony_ci @State dialogUIExtWant: Want | undefined = GlobalContext.load('dialogUIExtWant'); 41df226684Sopenharmony_ci @State session: UIExtensionContentSession | undefined = 42df226684Sopenharmony_ci storage === undefined ? undefined : storage.get<UIExtensionContentSession>('session'); 43df226684Sopenharmony_ci @State title: ResourceStr = ''; 44df226684Sopenharmony_ci @State message: ResourceStr = ''; 45df226684Sopenharmony_ci @State cancel: ResourceStr = ''; 46df226684Sopenharmony_ci @State actionButton: ResourceStr = ''; 47df226684Sopenharmony_ci @State buttonOptions: ButtonOptions[] = []; 48df226684Sopenharmony_ci 49df226684Sopenharmony_ci dialogControllerButton: CustomDialogController | null = new CustomDialogController({ 50df226684Sopenharmony_ci builder: CustomContentDialog({ 51df226684Sopenharmony_ci primaryTitle: this.title, 52df226684Sopenharmony_ci contentBuilder: () => { 53df226684Sopenharmony_ci this.buildContent(); 54df226684Sopenharmony_ci }, 55df226684Sopenharmony_ci buttons: this.buttonOptions 56df226684Sopenharmony_ci }), 57df226684Sopenharmony_ci autoCancel: false, 58df226684Sopenharmony_ci maskColor: Constants.TRANSPARENT_BACKGROUND_COLOR 59df226684Sopenharmony_ci }); 60df226684Sopenharmony_ci 61df226684Sopenharmony_ci async authWithPop(): Promise<void> { 62df226684Sopenharmony_ci HiLog.info(TAG, `authwithpop start`); 63df226684Sopenharmony_ci try { 64df226684Sopenharmony_ci await checkNetworkStatus(); 65df226684Sopenharmony_ci } catch { 66df226684Sopenharmony_ci let errInfo = GetAlertMessage.getAlertMessage( 67df226684Sopenharmony_ci { code: Constants.ERR_JS_APP_NETWORK_INVALID } as BusinessError); 68df226684Sopenharmony_ci this.title = ''; 69df226684Sopenharmony_ci this.message = errInfo.msg; 70df226684Sopenharmony_ci this.cancel = errInfo.cancel; 71df226684Sopenharmony_ci this.buttonOptions = [{ 72df226684Sopenharmony_ci value: this.cancel ? this.cancel : $r('app.string.da_button'), 73df226684Sopenharmony_ci background: $r('sys.color.ohos_id_color_button_normal'), action: () => { 74df226684Sopenharmony_ci abilityResult.resultCode = 0; 75df226684Sopenharmony_ci (getContext(this) as common.UIAbilityContext).terminateSelfWithResult(abilityResult); 76df226684Sopenharmony_ci }}]; 77df226684Sopenharmony_ci this.dialogControllerButton?.open(); 78df226684Sopenharmony_ci HiLog.info(TAG, `network failed`); 79df226684Sopenharmony_ci return; 80df226684Sopenharmony_ci }; 81df226684Sopenharmony_ci try { 82df226684Sopenharmony_ci account_osAccount.DomainAccountManager.authWithPopup({ 83df226684Sopenharmony_ci onResult: async (resultCode: number, authResult: account_osAccount.AuthResult) => { 84df226684Sopenharmony_ci sendDlpManagerAccountLogin(resultCode); 85df226684Sopenharmony_ci HiLog.info(TAG, `auth resultCode: ${resultCode}`); 86df226684Sopenharmony_ci HiLog.info(TAG, `auth authResult: ${JSON.stringify(authResult)}`); 87df226684Sopenharmony_ci } 88df226684Sopenharmony_ci }) 89df226684Sopenharmony_ci abilityResult.resultCode = 0; 90df226684Sopenharmony_ci (getContext(this) as common.UIAbilityContext).terminateSelfWithResult(abilityResult); 91df226684Sopenharmony_ci } catch (err) { 92df226684Sopenharmony_ci HiLog.info(TAG, `auth exception: ${JSON.stringify(err)}`); 93df226684Sopenharmony_ci } 94df226684Sopenharmony_ci } 95df226684Sopenharmony_ci 96df226684Sopenharmony_ci getErrInfo(messageCode: number, errorMsg: BusinessError, accountType: number) { 97df226684Sopenharmony_ci let errInfo: Record<string, Resource> = {}; 98df226684Sopenharmony_ci if ([ 99df226684Sopenharmony_ci Constants.ERR_JS_APP_PARAM_ERROR, 100df226684Sopenharmony_ci Constants.ERR_JS_APP_OPEN_REJECTED, 101df226684Sopenharmony_ci Constants.ERR_JS_APP_ENCRYPTION_REJECTED, 102df226684Sopenharmony_ci Constants.ERR_JS_APP_ENCRYPTING, 103df226684Sopenharmony_ci Constants.ERR_JS_FILE_EXPIRATION, 104df226684Sopenharmony_ci Constants.ERR_JS_DLP_FILE_READ_ONLY, 105df226684Sopenharmony_ci Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED, 106df226684Sopenharmony_ci ].includes(messageCode)) { 107df226684Sopenharmony_ci errInfo = GetAlertMessage.getAlertTitleMessage(errorMsg); 108df226684Sopenharmony_ci } else if ([Constants.ERR_JS_APP_SYSTEM_IS_AUTHENTICATED].includes(messageCode)) { 109df226684Sopenharmony_ci errInfo = GetAlertMessage.getAlertButtonMessage(errorMsg); 110df226684Sopenharmony_ci } else if ([Constants.ERR_JS_USER_NO_PERMISSION].includes(messageCode)) { 111df226684Sopenharmony_ci errInfo = accountType === dlpPermission.AccountType.CLOUD_ACCOUNT ? 112df226684Sopenharmony_ci GetAlertMessage.getAlertMessage(errorMsg) : GetAlertMessage.getAlertTitleMessage(errorMsg); 113df226684Sopenharmony_ci } else { 114df226684Sopenharmony_ci errInfo = GetAlertMessage.getAlertMessage(errorMsg); 115df226684Sopenharmony_ci } 116df226684Sopenharmony_ci this.title = errInfo.title; 117df226684Sopenharmony_ci this.message = errInfo.msg; 118df226684Sopenharmony_ci this.cancel = errInfo.cancel; 119df226684Sopenharmony_ci this.actionButton = errInfo.ok; 120df226684Sopenharmony_ci this.buttonOptions = [{ 121df226684Sopenharmony_ci value: this.cancel ? this.cancel : $r('app.string.da_button'), 122df226684Sopenharmony_ci background: $r('sys.color.ohos_id_color_button_normal'), action: () => { 123df226684Sopenharmony_ci abilityResult.resultCode = 0; 124df226684Sopenharmony_ci (getContext(this) as common.UIExtensionContext).terminateSelfWithResult(abilityResult); 125df226684Sopenharmony_ci }}]; 126df226684Sopenharmony_ci if (errInfo.ok) { 127df226684Sopenharmony_ci this.buttonOptions.push({ value: this.actionButton, background: $r('sys.color.ohos_id_color_text_primary_activated'), 128df226684Sopenharmony_ci fontColor: $r('sys.color.font_on_primary'), action: () => { 129df226684Sopenharmony_ci this.authWithPop(); 130df226684Sopenharmony_ci }}); 131df226684Sopenharmony_ci } 132df226684Sopenharmony_ci this.dialogControllerButton?.open(); 133df226684Sopenharmony_ci } 134df226684Sopenharmony_ci 135df226684Sopenharmony_ci async aboutToAppear() { 136df226684Sopenharmony_ci HiLog.info(TAG, `alert aboutToAppear start`); 137df226684Sopenharmony_ci try { 138df226684Sopenharmony_ci let messageCode = -1; 139df226684Sopenharmony_ci let errorMsg = {} as BusinessError; 140df226684Sopenharmony_ci let accountType = -1; 141df226684Sopenharmony_ci if (this.session === undefined) { 142df226684Sopenharmony_ci errorMsg = this.dialogUIExtWant?.parameters?.error as BusinessError; 143df226684Sopenharmony_ci messageCode = errorMsg.code; 144df226684Sopenharmony_ci accountType = this.dialogUIExtWant?.parameters?.accountType as number; 145df226684Sopenharmony_ci } else { 146df226684Sopenharmony_ci errorMsg = storage.get('error') as BusinessError; 147df226684Sopenharmony_ci messageCode = errorMsg.code; 148df226684Sopenharmony_ci accountType = dlpPermission.AccountType.DOMAIN_ACCOUNT; 149df226684Sopenharmony_ci } 150df226684Sopenharmony_ci this.getErrInfo(messageCode, errorMsg, accountType); 151df226684Sopenharmony_ci } catch (err) { 152df226684Sopenharmony_ci HiLog.error(TAG, `showErrorDialog failed: ${JSON.stringify(err)}`); 153df226684Sopenharmony_ci } 154df226684Sopenharmony_ci } 155df226684Sopenharmony_ci 156df226684Sopenharmony_ci aboutToDisappear() { 157df226684Sopenharmony_ci this.dialogControllerButton = null; 158df226684Sopenharmony_ci } 159df226684Sopenharmony_ci 160df226684Sopenharmony_ci build() { 161df226684Sopenharmony_ci } 162df226684Sopenharmony_ci 163df226684Sopenharmony_ci @Builder 164df226684Sopenharmony_ci buildContent(): void { 165df226684Sopenharmony_ci Column() { 166df226684Sopenharmony_ci Text() { 167df226684Sopenharmony_ci Span(this.message) 168df226684Sopenharmony_ci } 169df226684Sopenharmony_ci } 170df226684Sopenharmony_ci .width(Constants.HEADER_TEXT_WIDTH) 171df226684Sopenharmony_ci .align(this.title ? Alignment.Start : Alignment.Center) 172df226684Sopenharmony_ci .margin({ 173df226684Sopenharmony_ci bottom: Constants.START_ABILITY_CUSTOM_CONTENT_MARGIN_BOTTOM 174df226684Sopenharmony_ci }) 175df226684Sopenharmony_ci } 176df226684Sopenharmony_ci} 177