1/* 2 * Copyright (c) 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 GlobalContext from '../common/GlobalContext'; 17import { getAlertMessage } from '../common/utils'; 18import common from '@ohos.app.ability.common'; 19import Want from '@ohos.app.ability.Want'; 20import { BusinessError } from '@ohos.base'; 21import Constants from '../common/constant'; 22import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 23import ability from '@ohos.ability.ability'; 24import account_osAccount from '@ohos.account.osAccount'; 25import { sendDlpManagerAccountLogin } from '../common/utils'; 26import { isPC } from '../common/utils'; 27 28const TAG = '[DLPManager_Alert]'; 29let abilityResult: ability.AbilityResult = { 30 "resultCode": 0, 31 "want": {} 32}; 33 34let storage = LocalStorage.getShared(); 35@Entry(storage) 36@Component 37struct Index { 38 @State session: UIExtensionContentSession | undefined = storage === undefined ? undefined : storage.get<UIExtensionContentSession>('session'); 39 @State title: string | Resource = ''; 40 @State message: string | Resource = ''; 41 @State cancel: string | Resource = ''; 42 @State ok: string | Resource = ''; 43 @State messageHeight: string = ''; 44 @State windowHeight: number = 0; 45 46 authWithPop(): void { 47 console.log(TAG, 'authwithpop start'); 48 try { 49 account_osAccount.DomainAccountManager.authWithPopup({ 50 onResult: async (resultCode: number, authResult: account_osAccount.AuthResult) => { 51 sendDlpManagerAccountLogin(resultCode); 52 if (resultCode === Constants.ERR_JS_NETWORK_INVALID) { 53 let errInfo = getAlertMessage({ code: Constants.ERR_JS_APP_NETWORK_INVALID } as BusinessError); 54 this.title = ''; 55 this.message = errInfo.msg; 56 this.cancel = errInfo.cancel; 57 this.ok = errInfo.ok; 58 try { 59 GlobalContext.load('mainWindow').resize(Constants.START_ABILITY_WINDOW_WIDTH, Constants.START_ABILITY_HEIGHT_DEFAULT, (err: BusinessError) => { 60 const errCode: number = err.code; 61 if (errCode) { 62 console.error('Failed to change the window size. Cause:' + JSON.stringify(err)); 63 return; 64 } 65 console.info('Succeeded in changing the window size.'); 66 }); 67 } catch (exception) { 68 console.error('Failed to change the window size. Cause:' + JSON.stringify(exception)) 69 } 70 return; 71 } 72 if (this.session !== undefined) { 73 this.session.terminateSelfWithResult({ 74 'resultCode': 0, 75 'want': { 76 'bundleName': Constants.DLP_MANAGER_BUNDLE_NAME, 77 }, 78 }); 79 } 80 console.log(TAG, 'auth resultCode = ' + resultCode); 81 console.log(TAG, 'auth authResult = ' + JSON.stringify(authResult)); 82 } 83 }) 84 } catch (err) { 85 console.log(TAG, 'auth exception = ' + JSON.stringify(err)); 86 } 87 } 88 89 async aboutToAppear() { 90 console.log(TAG, 'alert aboutToAppear start') 91 try { 92 let messageCode = ((GlobalContext.load('abilityWant') as Want).parameters?.error as BusinessError).code; 93 let errInfo = getAlertMessage((GlobalContext.load('abilityWant') as Want).parameters?.error as BusinessError); 94 this.windowHeight = (GlobalContext.load('abilityWant') as Want).parameters?.windowHeight as number; 95 this.windowHeight = Number(this.windowHeight) - Constants.START_ABILITY_MSG_HEIGHT_ROWS; 96 this.title = errInfo.title; 97 this.message = errInfo.msg; 98 this.cancel = errInfo.cancel; 99 this.ok = errInfo.ok; 100 if ([ 101 Constants.ERR_JS_APP_ENCRYPTION_REJECTED, 102 ].includes(messageCode)) { 103 this.messageHeight = Constants.START_ABILITY_MESSAGE_HEIGHT; 104 } 105 if ([ 106 Constants.ERR_JS_OFFLINE, 107 ].includes(messageCode)) { 108 this.messageHeight = Constants.START_ABILITY_MESSAGE_HEIGHT1; 109 } 110 if ([ 111 Constants.ERR_JS_APP_PARAM_ERROR, 112 Constants.ERR_JS_APP_OPEN_REJECTED, 113 ].includes(messageCode)) { 114 this.messageHeight = Constants.START_ABILITY_MESSAGE_HEIGHT_ONE; 115 } 116 if ([ 117 Constants.ERR_JS_DLP_FILE_READ_ONLY, 118 ].includes(messageCode)) { 119 this.messageHeight = Constants.START_ABILITY_MESSAGE_HEIGHT_TWO; 120 } 121 if ([ 122 Constants.ERR_JS_GET_ACCOUNT_ERROR, 123 Constants.ERR_JS_NOT_DLP_FILE, 124 ].includes(messageCode)) { 125 this.messageHeight = Constants.START_ABILITY_MESSAGE_HEIGHT_THREE; 126 } 127 if ([ 128 Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED 129 ].includes(messageCode)) { 130 this.messageHeight = Constants.START_ABILITY_MESSAGE_HEIGHT_FOUR; 131 } 132 } catch (err) { 133 console.error(TAG, 'showErrorDialog failed ' + JSON.stringify(err as BusinessError)); 134 } 135 } 136 137 build() { 138 Column() { 139 if (this.title) { 140 Row() { 141 Text(this.title) 142 .lineHeight(Constants.DA_TEXT_LINE_HEIGHT) 143 .width(Constants.DU_WIDTH_FULL) 144 .fontSize($r('sys.float.ohos_id_text_size_dialog_tittle')) 145 .fontColor($r('sys.color.ohos_id_color_text_primary')) 146 .fontWeight(FontWeight.Medium) 147 .align(Alignment.Start) 148 } 149 .width(Constants.HEADER_COLUMN_WIDTH) 150 .height(this.title ? Constants.DA_ROW_HEIGHT : '') 151 .padding({ 152 left: Constants.HEADER_COLUMN_PADDING_LEFT, 153 right: Constants.HEADER_COLUMN_PADDING_RIGHT 154 }) 155 } 156 Row() { 157 Text() { 158 Span(this.message) 159 } 160 .wordBreak(WordBreak.BREAK_ALL) 161 .fontWeight(FontWeight.Regular) 162 .fontColor($r('sys.color.ohos_id_color_text_primary')) 163 .fontSize($r('sys.float.ohos_id_text_size_body1')) 164 .textAlign(TextAlign.Start) 165 } 166 .height(this.messageHeight) 167 .width(this.title ? Constants.HEADER_TEXT_WIDTH : '') 168 .align(Alignment.Center) 169 .padding({ 170 left: Constants.HEADER_COLUMN_PADDING_LEFT, 171 right: Constants.HEADER_COLUMN_PADDING_RIGHT, 172 top: this.title ? Constants.ENCRYPTION_MESSAGE_ALERT_MESSAGE_TOP : Constants.ENCRYPTION_MESSAGE_ALERT_MESSAGE_TOP1 173 }) 174 Flex({ direction: FlexDirection.Row }) { 175 Button(this.cancel? this.cancel : $r('app.string.da_button'), { type: ButtonType.Capsule, stateEffect: true }) 176 .backgroundColor($r('sys.color.ohos_id_color_button_normal')) 177 .width(Constants.HEADER_TEXT_WIDTH) 178 .focusable(false) 179 .fontColor($r('sys.color.ohos_id_color_text_primary_activated')) 180 .height(Constants.FOOTER_HEIGHT) 181 .onClick(async (event) => { 182 if (this.session !== undefined) { 183 this.session.terminateSelfWithResult({ 184 'resultCode': 0, 185 'want': { 186 'bundleName': Constants.DLP_MANAGER_BUNDLE_NAME, 187 }, 188 }); 189 } else { 190 abilityResult.resultCode = 0; 191 (GlobalContext.load('alertContext') as common.UIAbilityContext).terminateSelfWithResult(abilityResult); 192 } 193 }) 194 .margin({ right: Constants.ENCRYPTION_PROTECTION_BUTTON_MARGIN }) 195 if (this.ok) { 196 Button(this.ok, { type: ButtonType.Capsule, stateEffect: true }) 197 .width(Constants.HEADER_TEXT_WIDTH) 198 .focusable(false) 199 .backgroundColor($r('sys.color.ohos_id_color_text_primary_activated')) 200 .height(Constants.FOOTER_BUTTON_HEIGHT) 201 .onClick(async (event) => { 202 let error = (GlobalContext.load('abilityWant') as Want).parameters?.error as BusinessError; 203 if (error.code === Constants.ERR_JS_OFFLINE) { 204 let want: Want = { 205 action: 'action.system.home', 206 entities: ['entity.system.home'], 207 uri: 'wifi_entry' 208 }; 209 (GlobalContext.load('alertContext') as common.UIAbilityContext).startAbility(want); 210 (GlobalContext.load('alertContext') as common.UIAbilityContext).terminateSelf(); 211 return; 212 } 213 this.authWithPop(); 214 }) 215 .margin({ left: Constants.ENCRYPTION_PROTECTION_BUTTON_MARGIN }) 216 } 217 } 218 .margin({ 219 left: this.ok ? Constants.ENCRYPTION_BUTTON_TO_BUTTON_WIDTH : Constants.ENCRYPTION_MESSAGE_DIALOG_TIPS_PADDING, 220 right: this.ok ? Constants.ENCRYPTION_BUTTON_TO_BUTTON_WIDTH : Constants.ENCRYPTION_MESSAGE_DIALOG_TIPS_PADDING, 221 bottom: Constants.ENCRYPTION_BUTTON_MARGIN_BOTTOM, 222 top: Constants.ENCRYPTION_BUTTON_TO_BUTTON_WIDTH 223 }) 224 } 225 .width(Constants.ENCRYPTION_MESSAGE_DIALOG_TIPS) 226 .borderRadius($r('sys.float.ohos_id_corner_radius_dialog')) 227 .alignItems(HorizontalAlign.Center) 228 .backgroundColor($r('sys.color.ohos_id_color_dialog_bg')) 229 .height( isPC() ? Constants.HEADER_COLUMN_WIDTH : this.windowHeight) 230 } 231} 232