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 { BusinessError } from '@ohos.base'; 17df226684Sopenharmony_ciimport display from '@ohos.display'; 18df226684Sopenharmony_ciimport StartOptions from '@ohos.app.ability.StartOptions'; 19df226684Sopenharmony_ciimport common from '@ohos.app.ability.common'; 20df226684Sopenharmony_ciimport UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 21df226684Sopenharmony_ciimport osAccount from '@ohos.account.osAccount'; 22df226684Sopenharmony_ciimport account_osAccount from '@ohos.account.osAccount'; 23df226684Sopenharmony_ciimport Constants from './constant'; 24df226684Sopenharmony_ciimport GlobalContext from './GlobalContext'; 25df226684Sopenharmony_ciimport deviceInfo from '@ohos.deviceInfo'; 26df226684Sopenharmony_ciimport promptAction from '@ohos.promptAction'; 27df226684Sopenharmony_ciimport { HiLog } from '../common/HiLog'; 28df226684Sopenharmony_ciimport AccountManager from '../manager/AccountManager'; 29df226684Sopenharmony_ciimport CommonUtil from './CommonUtil'; 30df226684Sopenharmony_ci 31df226684Sopenharmony_ciconst TAG = 'AlertMessage'; 32df226684Sopenharmony_ci 33df226684Sopenharmony_ciexport class GetAlertMessage { 34df226684Sopenharmony_ci 35df226684Sopenharmony_ci public static async startAlertAbility( 36df226684Sopenharmony_ci context: common.UIAbilityContext | common.ServiceExtensionContext | common.UIExtensionContext, 37df226684Sopenharmony_ci error: BusinessError, 38df226684Sopenharmony_ci session?: UIExtensionContentSession 39df226684Sopenharmony_ci ) { 40df226684Sopenharmony_ci if (deviceInfo.deviceType !== '2in1') { 41df226684Sopenharmony_ci GetAlertMessage.phoneHandle(context, error); 42df226684Sopenharmony_ci return; 43df226684Sopenharmony_ci } 44df226684Sopenharmony_ci let dis = display.getDefaultDisplaySync(); 45df226684Sopenharmony_ci let xNumber = Math.floor( 46df226684Sopenharmony_ci (dis.width - Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_WIDTH) * Constants.RATIO_HALF 47df226684Sopenharmony_ci ); 48df226684Sopenharmony_ci let yNumber = Math.floor( 49df226684Sopenharmony_ci (dis.height - Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_HEIGHT) * Constants.RATIO_HALF 50df226684Sopenharmony_ci ); 51df226684Sopenharmony_ci let options: StartOptions = { 52df226684Sopenharmony_ci withAnimation: true, 53df226684Sopenharmony_ci windowLeft: xNumber, 54df226684Sopenharmony_ci windowTop: yNumber, 55df226684Sopenharmony_ci windowWidth: Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_WIDTH, 56df226684Sopenharmony_ci windowHeight: Constants.START_ABILITY_CUSTOM_CONTENT_DIALOG_HEIGHT 57df226684Sopenharmony_ci }; 58df226684Sopenharmony_ci HiLog.info(TAG, `set options: ${JSON.stringify(options)}`); 59df226684Sopenharmony_ci context.startAbility({ 60df226684Sopenharmony_ci bundleName: Constants.DLP_MANAGER_BUNDLE_NAME, 61df226684Sopenharmony_ci abilityName: 'AlertAbility', 62df226684Sopenharmony_ci parameters: { 63df226684Sopenharmony_ci error: error, 64df226684Sopenharmony_ci accountType: GlobalContext.load('accountType') 65df226684Sopenharmony_ci } 66df226684Sopenharmony_ci }, options, async (err: BusinessError) => { 67df226684Sopenharmony_ci if (err && err.code !== 0) { 68df226684Sopenharmony_ci HiLog.error(TAG, `start AlertAbility failed: ${JSON.stringify(err)}`); 69df226684Sopenharmony_ci } 70df226684Sopenharmony_ci if (session) { 71df226684Sopenharmony_ci session.terminateSelfWithResult({ 72df226684Sopenharmony_ci 'resultCode': 0, 73df226684Sopenharmony_ci 'want': { 74df226684Sopenharmony_ci 'bundleName': Constants.DLP_MANAGER_BUNDLE_NAME, 75df226684Sopenharmony_ci }, 76df226684Sopenharmony_ci }); 77df226684Sopenharmony_ci } else { 78df226684Sopenharmony_ci context.terminateSelf(); 79df226684Sopenharmony_ci } 80df226684Sopenharmony_ci }) 81df226684Sopenharmony_ci } 82df226684Sopenharmony_ci 83df226684Sopenharmony_ci public static async phoneHandle( 84df226684Sopenharmony_ci context: common.UIAbilityContext | common.ServiceExtensionContext | common.UIExtensionContext, 85df226684Sopenharmony_ci error: BusinessError 86df226684Sopenharmony_ci ) { 87df226684Sopenharmony_ci try { 88df226684Sopenharmony_ci let resource = GetAlertMessage.getToastMessage(error); 89df226684Sopenharmony_ci let toastMessage = await context.resourceManager.getStringValue(resource); 90df226684Sopenharmony_ci promptAction.showToast({ 91df226684Sopenharmony_ci message: toastMessage, 92df226684Sopenharmony_ci duration: Constants.SHARE_SET_TIMEOUT 93df226684Sopenharmony_ci }); 94df226684Sopenharmony_ci HiLog.info(TAG, 'showToast end.'); 95df226684Sopenharmony_ci } catch (error) { 96df226684Sopenharmony_ci HiLog.error(TAG, `showToast error: ${JSON.stringify(error)}`); 97df226684Sopenharmony_ci } finally { 98df226684Sopenharmony_ci setTimeout(() => { 99df226684Sopenharmony_ci context.terminateSelf(); 100df226684Sopenharmony_ci }, Constants.SHARE_SET_TIMEOUT); 101df226684Sopenharmony_ci } 102df226684Sopenharmony_ci } 103df226684Sopenharmony_ci 104df226684Sopenharmony_ci public static getAlertMessage(err: BusinessError, defaultTitle?: Resource, defaultMessage?: Resource) { 105df226684Sopenharmony_ci switch (err && err.code) { 106df226684Sopenharmony_ci case Constants.ERR_JS_ACCOUNT_NOT_FOUND: 107df226684Sopenharmony_ci case Constants.ERR_JS_GET_ACCOUNT_ERROR: 108df226684Sopenharmony_ci return { 'msg': $r('app.string.MESSAGE_APP_GET_ACCOUNT_ERROR') } as Record<string, Resource>; 109df226684Sopenharmony_ci case Constants.ERR_JS_APP_NO_ACCOUNT_ERROR: 110df226684Sopenharmony_ci case Constants.ERR_JS_ACCOUNT_NOT_LOGIN: 111df226684Sopenharmony_ci return { 'msg': $r('app.string.MESSAGE_APP_NO_ACCOUNT_ERROR') } as Record<string, Resource>; 112df226684Sopenharmony_ci case Constants.ERR_JS_APP_GET_FILE_ASSET_ERROR: 113df226684Sopenharmony_ci return { 'msg': $r('app.string.MESSAGE_APP_GET_FILE_ASSET_ERROR') } as Record<string, Resource>; 114df226684Sopenharmony_ci case Constants.ERR_JS_RELEASE_FILE_OPEN: 115df226684Sopenharmony_ci case Constants.ERR_JS_NOT_AUTHORIZED_APPLICATION: 116df226684Sopenharmony_ci return { 'msg': $r('app.string.MESSAGE_NOT_AUTHORIZED_APPLICATION') } as Record<string, Resource>; 117df226684Sopenharmony_ci case Constants.ERR_JS_NETWORK_INVALID: 118df226684Sopenharmony_ci case Constants.ERR_JS_APP_NETWORK_INVALID: 119df226684Sopenharmony_ci return { 'msg': $r('app.string.network_invalid') } as Record<string, Resource>; 120df226684Sopenharmony_ci case Constants.ERR_JS_CREDENTIAL_SERVICE_ERROR: 121df226684Sopenharmony_ci case Constants.ERR_JS_CREDENTIAL_SERVER_ERROR: 122df226684Sopenharmony_ci case Constants.ERR_JS_CREDENTIAL_TIMEOUT: 123df226684Sopenharmony_ci case Constants.ERR_JS_APP_INSIDE_ERROR: 124df226684Sopenharmony_ci return { 'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR') } as Record<string, Resource>; 125df226684Sopenharmony_ci case Constants.ERR_JS_NOT_DLP_FILE: 126df226684Sopenharmony_ci return { 'msg': $r('app.string.MESSAGE_APP_FILE_PARAM_ERROR') } as Record<string, Resource>; 127df226684Sopenharmony_ci case Constants.ERR_JS_USER_NO_PERMISSION: 128df226684Sopenharmony_ci return { 'msg': $r('app.string.MESSAGE_APP_NOT_HAVE_PERM') } as Record<string, Resource>; 129df226684Sopenharmony_ci case Constants.ERR_JS_OTHER_APP_OPEN_FILE: 130df226684Sopenharmony_ci return { 'msg': $r('app.string.This_File_is_Open_By_Other_App') } as Record<string, Resource>; 131df226684Sopenharmony_ci case Constants.ERR_JS_OFFLINE: 132df226684Sopenharmony_ci return { 'msg': $r('app.string.network_invalid') } as Record<string, Resource>; 133df226684Sopenharmony_ci default: 134df226684Sopenharmony_ci if (defaultTitle !== undefined && defaultMessage != undefined) { 135df226684Sopenharmony_ci return { 'title': defaultTitle, 'msg': defaultMessage } as Record<string, Resource>; 136df226684Sopenharmony_ci } else { 137df226684Sopenharmony_ci return { 'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR') } as Record<string, Resource>; 138df226684Sopenharmony_ci } 139df226684Sopenharmony_ci } 140df226684Sopenharmony_ci } 141df226684Sopenharmony_ci 142df226684Sopenharmony_ci public static getAlertTitleMessage(err: BusinessError) { 143df226684Sopenharmony_ci switch (err && err.code) { 144df226684Sopenharmony_ci case Constants.ERR_JS_USER_NO_PERMISSION: 145df226684Sopenharmony_ci return { 146df226684Sopenharmony_ci 'title': $r('app.string.TITLE_APP_VISIT_FILE_ERROR'), 147df226684Sopenharmony_ci 'msg': $r('app.string.MESSAGE_APP_NOT_HAVE_PERM_VISIT', err.message.split(', contact:')?.[1]) 148df226684Sopenharmony_ci } as Record<string, Resource>; 149df226684Sopenharmony_ci case Constants.ERR_JS_APP_PARAM_ERROR: 150df226684Sopenharmony_ci return { 151df226684Sopenharmony_ci 'title': $r('app.string.TITLE_APP_ERROR'), 152df226684Sopenharmony_ci 'msg': $r('app.string.MESSAGE_APP_PARAM_ERROR') 153df226684Sopenharmony_ci } as Record<string, Resource>; 154df226684Sopenharmony_ci case Constants.ERR_JS_APP_OPEN_REJECTED: 155df226684Sopenharmony_ci return { 156df226684Sopenharmony_ci 'title': $r('app.string.header_title'), 157df226684Sopenharmony_ci 'msg': $r('app.string.MESSAGE_DLP_OPEN_REJECT') 158df226684Sopenharmony_ci } as Record<string, Resource>; 159df226684Sopenharmony_ci case Constants.ERR_JS_APP_ENCRYPTION_REJECTED: 160df226684Sopenharmony_ci return { 161df226684Sopenharmony_ci 'title': $r('app.string.header_title'), 162df226684Sopenharmony_ci 'msg': $r('app.string.MESSAGE_DLP_ENCRYPTION_REJECTED') 163df226684Sopenharmony_ci } as Record<string, Resource>; 164df226684Sopenharmony_ci case Constants.ERR_JS_APP_ENCRYPTING: 165df226684Sopenharmony_ci return { 166df226684Sopenharmony_ci 'title': $r('app.string.header_title'), 167df226684Sopenharmony_ci 'msg': $r('app.string.MESSAGE_DLP_ENCRYPTION', err.data) 168df226684Sopenharmony_ci } as Record<string, Resource>; 169df226684Sopenharmony_ci case Constants.ERR_JS_FILE_EXPIRATION: 170df226684Sopenharmony_ci return { 171df226684Sopenharmony_ci 'title': $r('app.string.Permission_has_expired'), 172df226684Sopenharmony_ci 'msg': $r('app.string.Permission_has_expired_description', err.message.split(', contact:')?.[1]) 173df226684Sopenharmony_ci } as Record<string, Resource>; 174df226684Sopenharmony_ci case Constants.ERR_JS_DLP_FILE_READ_ONLY: 175df226684Sopenharmony_ci return { 176df226684Sopenharmony_ci 'title': $r('app.string.TITLE_APP_VISIT_FILE_ERROR'), 177df226684Sopenharmony_ci 'msg': $r('app.string.MESSAGE_DLP_READ_ONLY', AppStorage.get('contactAccount')) 178df226684Sopenharmony_ci } as Record<string, Resource>; 179df226684Sopenharmony_ci case Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED: 180df226684Sopenharmony_ci return { 181df226684Sopenharmony_ci 'title': $r('app.string.TITLE_APP_VERSION_LOWER'), 182df226684Sopenharmony_ci 'msg': $r('app.string.MESSAGE_DLP_SYSTEM_NEED_TO_BE_UPGRADED') 183df226684Sopenharmony_ci } as Record<string, Resource>; 184df226684Sopenharmony_ci default: 185df226684Sopenharmony_ci return { 186df226684Sopenharmony_ci 'title': $r('app.string.header_title'), 187df226684Sopenharmony_ci 'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR') 188df226684Sopenharmony_ci } as Record<string, Resource>; 189df226684Sopenharmony_ci } 190df226684Sopenharmony_ci } 191df226684Sopenharmony_ci 192df226684Sopenharmony_ci public static getAlertButtonMessage(err: BusinessError) { 193df226684Sopenharmony_ci switch (err && err.code) { 194df226684Sopenharmony_ci case Constants.ERR_JS_APP_SYSTEM_IS_AUTHENTICATED: 195df226684Sopenharmony_ci return { 196df226684Sopenharmony_ci 'title': $r('app.string.header_title'), 197df226684Sopenharmony_ci 'msg': $r('app.string.MESSAGE_DLP_SYSTEM_IS_AUTHENTICATED'), 198df226684Sopenharmony_ci 'cancel': $r('app.string.ban'), 199df226684Sopenharmony_ci 'ok': $r('app.string.SYSTEM_IS_AUTHENTICATED_LOGIN') 200df226684Sopenharmony_ci } as Record<string, Resource>; 201df226684Sopenharmony_ci default: 202df226684Sopenharmony_ci return { 203df226684Sopenharmony_ci 'title': $r('app.string.header_title'), 204df226684Sopenharmony_ci 'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR') 205df226684Sopenharmony_ci } as Record<string, Resource>; 206df226684Sopenharmony_ci } 207df226684Sopenharmony_ci } 208df226684Sopenharmony_ci 209df226684Sopenharmony_ci public static getToastMessage(err: BusinessError) { 210df226684Sopenharmony_ci switch (err && err.code) { 211df226684Sopenharmony_ci case Constants.ERR_JS_APP_GET_FILE_ASSET_ERROR: 212df226684Sopenharmony_ci return $r('app.string.MESSAGE_APP_GET_FILE_ASSET_ERROR'); 213df226684Sopenharmony_ci case Constants.ERR_JS_NETWORK_INVALID: 214df226684Sopenharmony_ci case Constants.ERR_JS_APP_NETWORK_INVALID: 215df226684Sopenharmony_ci case Constants.ERR_JS_OFFLINE: 216df226684Sopenharmony_ci return $r('app.string.network_invalid'); 217df226684Sopenharmony_ci case Constants.ERR_JS_NOT_DLP_FILE: 218df226684Sopenharmony_ci return $r('app.string.MESSAGE_APP_FILE_PARAM_ERROR'); 219df226684Sopenharmony_ci case Constants.ERR_JS_USER_NO_PERMISSION: 220df226684Sopenharmony_ci return $r('app.string.MESSAGE_APP_NOT_HAVE_PERM'); 221df226684Sopenharmony_ci case Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED: 222df226684Sopenharmony_ci return $r('app.string.MESSAGE_DLP_SYSTEM_NEED_TO_BE_UPGRADED'); 223df226684Sopenharmony_ci case Constants.ERR_JS_APP_CANNOT_OPEN: 224df226684Sopenharmony_ci return $r('app.string.THIS_FILE_NOT_SUPPORT_ENCRYPTION_PROTECTION'); 225df226684Sopenharmony_ci case Constants.ERR_JS_OTHER_APP_OPEN_FILE: 226df226684Sopenharmony_ci return $r('app.string.This_File_is_Open_By_Other_App'); 227df226684Sopenharmony_ci default: 228df226684Sopenharmony_ci return $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR'); 229df226684Sopenharmony_ci } 230df226684Sopenharmony_ci } 231df226684Sopenharmony_ci 232df226684Sopenharmony_ci public static async checkAccountInfo(accountName: string): Promise<boolean> { 233df226684Sopenharmony_ci 234df226684Sopenharmony_ci let domainAccount = await AccountManager.getDomainAccountByAccountName(accountName); 235df226684Sopenharmony_ci return !CommonUtil.isEmptyStr(domainAccount.accountName); 236df226684Sopenharmony_ci } 237df226684Sopenharmony_ci 238df226684Sopenharmony_ci public static async requestModalUIExtension( 239df226684Sopenharmony_ci context: common.UIAbilityContext | common.ServiceExtensionContext, error: BusinessError 240df226684Sopenharmony_ci ){ 241df226684Sopenharmony_ci if (deviceInfo.deviceType !== '2in1') { 242df226684Sopenharmony_ci GetAlertMessage.phoneHandle(context, error); 243df226684Sopenharmony_ci return; 244df226684Sopenharmony_ci }; 245df226684Sopenharmony_ci let uiExtWant: Want = { 246df226684Sopenharmony_ci bundleName: Constants.DLP_MANAGER_BUNDLE_NAME, 247df226684Sopenharmony_ci abilityName: 'DialogUIExtAbility', 248df226684Sopenharmony_ci moduleName: 'entry', 249df226684Sopenharmony_ci parameters: { 250df226684Sopenharmony_ci 'bundleName': AppStorage.get('paramCallerBundleName') ?? '', 251df226684Sopenharmony_ci 'ability.want.params.uiExtensionType': 'sys/commonUI', 252df226684Sopenharmony_ci 'error': error, 253df226684Sopenharmony_ci 'accountType': GlobalContext.load('accountType') 254df226684Sopenharmony_ci } 255df226684Sopenharmony_ci }; 256df226684Sopenharmony_ci try { 257df226684Sopenharmony_ci context.requestModalUIExtension(uiExtWant) 258df226684Sopenharmony_ci .then(() => { 259df226684Sopenharmony_ci HiLog.info(TAG, 'requestModalUIExtension succeed'); 260df226684Sopenharmony_ci }) 261df226684Sopenharmony_ci .catch((err: BusinessError) => { 262df226684Sopenharmony_ci HiLog.error(TAG, `requestModalUIExtension failed. Cause: ${JSON.stringify(err)}`); 263df226684Sopenharmony_ci }) 264df226684Sopenharmony_ci } catch (err) { 265df226684Sopenharmony_ci let code = (err as BusinessError).code; 266df226684Sopenharmony_ci let message = (err as BusinessError).message; 267df226684Sopenharmony_ci HiLog.error(TAG, `requestModalUIExtension failed. Cause: ${JSON.stringify(err)}`); 268df226684Sopenharmony_ci } 269df226684Sopenharmony_ci } 270df226684Sopenharmony_ci}