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 measure from '@ohos.measure'; 17import window from '@ohos.window'; 18import ability from '@ohos.ability.ability'; 19import account_osAccount from '@ohos.account.osAccount'; 20import emitter from '@ohos.events.emitter'; 21import dlpPermission from '@ohos.dlpPermission'; 22import bundleManager from '@ohos.bundle.bundleManager' 23import fs from '@ohos.file.fs'; 24import fileUri from "@ohos.file.fileuri"; 25import { BusinessError, Callback } from '@ohos.base' 26import deviceInfo from '@ohos.deviceInfo'; 27import Want from '@ohos.app.ability.Want'; 28import common from '@ohos.app.ability.common'; 29import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 30import Constants from '../common/constant'; 31import GlobalContext from './GlobalContext'; 32import hiSysEvent from '@ohos.hiSysEvent'; 33import display from '@ohos.display'; 34import StartOptions from '@ohos.app.ability.StartOptions'; 35 36let TAG = "[DLPManager_Utils]"; 37 38interface AuthAccount { 39 authAccount: string; 40 textContent?: string; 41} 42 43interface PermissionType { 44 value: Resource; 45 data: string; 46 index: number; 47} 48 49interface DLPInfo { 50 name: string; 51 versionCode: string; 52} 53 54export interface FileMsg { 55 fileName: string; 56 filePath: string; 57 fileType: string; 58} 59 60function getFileUriByPath(filePath: string): string { 61 try { 62 let uri = fileUri.getUriFromPath(filePath); 63 return uri; 64 } catch (err) { 65 console.info(TAG, 'getUriFromPath error:', JSON.stringify(err)); 66 return ""; 67 } 68} 69 70function getFileFd(uri: string): number { 71 try { 72 let file = fs.openSync(uri, fs.OpenMode.READ_WRITE); 73 console.info(TAG, 'open', uri, 'as', file.fd); 74 return file.fd; 75 } catch (err) { 76 console.info(TAG, 'openSync error:', JSON.stringify(err)); 77 return -1; 78 } 79} 80 81async function getOsAccountInfo(): Promise<account_osAccount.OsAccountInfo> { 82 let accountMgr = account_osAccount.getAccountManager(); 83 return await accountMgr.getCurrentOsAccount(); 84} 85 86function checkDomainAccountInfo(accountInfo: account_osAccount.OsAccountInfo): number | boolean { 87 if (GlobalContext.load('domainAccount') as boolean) { 88 GlobalContext.store('hiAccountType', 'Domain_Account'); 89 if (accountInfo.domainInfo.accountName === '' && 90 accountInfo.domainInfo.accountId === '') { 91 GlobalContext.store('hiAccountStatus', 0); 92 return Constants.ERR_JS_APP_NO_ACCOUNT_ERROR; 93 } 94 if (!accountInfo.domainInfo.isAuthenticated) { 95 GlobalContext.store('hiAccountStatus', 0); 96 return Constants.ERR_JS_APP_SYSTEM_IS_AUTHENTICATED; 97 } 98 } else { 99 GlobalContext.store('hiAccountType', 'Cloud_Account'); 100 if (accountInfo.distributedInfo.name === 'ohosAnonymousName' && 101 accountInfo.distributedInfo.id === 'ohosAnonymousUid') { 102 GlobalContext.store('hiAccountStatus', 0); 103 return Constants.ERR_JS_APP_NO_ACCOUNT_ERROR; 104 } 105 } 106 GlobalContext.store('hiAccountStatus', 1); 107 return false; 108} 109 110async function getUserId(): Promise<number> { 111 let accountMgr = account_osAccount.getAccountManager(); 112 return await accountMgr.getOsAccountLocalId(); 113} 114 115function getAuthPerm(accountName: string, dlpProperty: dlpPermission.DLPProperty): dlpPermission.DLPFileAccess { 116 let perm: dlpPermission.DLPFileAccess = dlpPermission.DLPFileAccess.NO_PERMISSION; 117 if (accountName === dlpProperty.ownerAccount) { 118 return dlpPermission.DLPFileAccess.FULL_CONTROL; 119 } 120 if ((dlpProperty.everyoneAccessList !== undefined) && (dlpProperty.everyoneAccessList.length > 0)) { 121 perm = Math.max(...dlpProperty.everyoneAccessList); 122 } 123 let authUserList = dlpProperty.authUserList ?? []; 124 for (let i = 0; i < authUserList.length; ++i) { 125 let authUser = authUserList[i]; 126 if (authUser.authAccount === accountName) { 127 return authUser.dlpFileAccess; 128 } 129 } 130 return perm; 131} 132 133function terminateSelfWithResult(resultCode: number, result: string): void { 134 let abilityResult: ability.AbilityResult = { 135 resultCode: resultCode, 136 want: { 137 parameters: { 138 result: result 139 } 140 } 141 }; 142 (GlobalContext.load('context') as common.UIAbilityContext).terminateSelfWithResult(abilityResult); 143} 144 145function getAlertMessage(err: BusinessError, defaultTitle?: Resource, defaultMessage?: Resource) { 146 switch (err.code) { 147 case Constants.ERR_JS_USER_NO_PERMISSION: 148 return { 149 'title': $r('app.string.TITLE_APP_VISIT_FILE_ERROR'), 150 'msg': $r('app.string.MESSAGE_APP_NOT_HAVE_PERM_VISIT', err.message.split(", contact:")?.[1]) 151 } as Record<string, Resource>; 152 case Constants.ERR_JS_ACCOUNT_NOT_FOUND: 153 case Constants.ERR_JS_GET_ACCOUNT_ERROR: 154 return { 'msg': $r('app.string.MESSAGE_APP_GET_ACCOUNT_ERROR') } as Record<string, Resource>; 155 case Constants.ERR_JS_APP_NO_ACCOUNT_ERROR: 156 case Constants.ERR_JS_ACCOUNT_NOT_LOGIN: 157 return { 'msg': $r('app.string.MESSAGE_APP_NO_ACCOUNT_ERROR') } as Record<string, Resource>; 158 case Constants.ERR_JS_APP_PARAM_ERROR: 159 return { 'title': $r('app.string.TITLE_APP_ERROR'), 'msg': $r('app.string.MESSAGE_APP_PARAM_ERROR') } as Record<string, Resource>; 160 case Constants.ERR_JS_APP_GET_FILE_ASSET_ERROR: 161 return { 'msg': $r('app.string.MESSAGE_APP_GET_FILE_ASSET_ERROR') } as Record<string, Resource>; 162 case Constants.ERR_JS_APP_OPEN_REJECTED: 163 return { 'title': $r('app.string.header_title'), 'msg': $r('app.string.MESSAGE_DLP_OPEN_REJECT') } as Record<string, Resource>; 164 case Constants.ERR_JS_APP_ENCRYPTION_REJECTED: 165 return { 'title': $r('app.string.header_title'), 'msg': $r('app.string.MESSAGE_DLP_ENCRYPTION_REJECTED') } as Record<string, Resource>; 166 case Constants.ERR_JS_APP_ENCRYPTING: 167 return { 'title': $r('app.string.header_title'), 'msg': $r('app.string.MESSAGE_DLP_ENCRYPTION', err.data) } as Record<string, Resource>; 168 case Constants.ERR_JS_NOT_AUTHORIZED_APPLICATION: 169 return { 'msg': $r('app.string.MESSAGE_NOT_AUTHORIZED_APPLICATION') } as Record<string, Resource>; 170 case Constants.ERR_JS_NETWORK_INVALID: 171 case Constants.ERR_JS_APP_NETWORK_INVALID: 172 return { 'msg': $r('app.string.network_invalid') } as Record<string, Resource>; 173 case Constants.ERR_JS_APP_SYSTEM_IS_AUTHENTICATED: 174 return { 175 'title': $r('app.string.header_title'), 176 'msg': $r('app.string.MESSAGE_DLP_SYSTEM_IS_AUTHENTICATED'), 177 'cancel': $r('app.string.ban'), 178 'ok': $r('app.string.SYSTEM_IS_AUTHENTICATED_LOGIN') 179 } as Record<string, Resource>; 180 case Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED: 181 return { 'msg': $r('app.string.MESSAGE_DLP_SYSTEM_NEED_TO_BE_UPGRADED')} as Record<string, Resource>; 182 case Constants.ERR_JS_FILE_EXPIRATION: 183 return { 184 'title': $r('app.string.Permission_has_expired'), 185 'msg': $r('app.string.Permission_has_expired_description', err.message.split(", contact:")?.[1]) 186 } as Record<string, Resource>; 187 case Constants.ERR_JS_OFFLINE: 188 return { 189 'title': $r('app.string.Network_not_connected'), 190 'msg': $r('app.string.Network_not_connected_description'), 191 'cancel': $r('app.string.ban'), 192 'ok': $r('app.string.Go_setting') 193 } as Record<string, Resource>; 194 case Constants.ERR_JS_CREDENTIAL_SERVICE_ERROR: 195 case Constants.ERR_JS_CREDENTIAL_SERVER_ERROR: 196 case Constants.ERR_JS_CREDENTIAL_TIMEOUT: 197 case Constants.ERR_JS_APP_INSIDE_ERROR: 198 return { 'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR') } as Record<string, Resource>; 199 case Constants.ERR_JS_NOT_DLP_FILE: 200 return { 'msg': $r('app.string.MESSAGE_APP_FILE_PARAM_ERROR') } as Record<string, Resource>; 201 case Constants.ERR_JS_DLP_FILE_READ_ONLY: 202 return { 203 'title': $r('app.string.TITLE_APP_VISIT_FILE_ERROR'), 204 'msg': $r('app.string.MESSAGE_DLP_READ_ONLY', AppStorage.get('ownerAccount')) 205 } as Record<string, Resource>; 206 default: 207 if (defaultTitle !== undefined && defaultMessage != undefined) { 208 return { 'title': defaultTitle, 'msg': defaultMessage } as Record<string, Resource>; 209 } else { 210 return { 'msg': $r('app.string.MESSAGE_SERVICE_INSIDE_ERROR') } as Record<string, Resource>; 211 } 212 } 213} 214 215async function startAlertAbility( 216 context: common.UIAbilityContext | common.ServiceExtensionContext | common.UIExtensionContext, 217 error: BusinessError, 218 session?: UIExtensionContentSession 219) { 220 let dis = display.getDefaultDisplaySync(); 221 let xNumber = Math.floor((dis.width - Constants.ENCRYPTION_MESSAGE_DIALOG_TIPS) / 2); 222 let yNumber = Math.floor((dis.height - Constants.START_ABILITY_YNUMBER) / 2); 223 let windowHeight = Constants.START_ABILITY_HEIGHT_DEFAULT; 224 if ([ 225 Constants.ERR_JS_APP_ENCRYPTING, 226 ].includes(error.code)) { 227 let errInfo = getAlertMessage(error as BusinessError); 228 let width = measureTextSizeWidth(`${error?.data}${errInfo.msg}`); 229 let rowsNum = Math.ceil(width / Constants.START_ABILITY_BASIC_WIDTH); 230 let rowsNumYu: number = Number((width / Constants.START_ABILITY_BASIC_WIDTH).toFixed(1)); 231 if (Math.floor(rowsNumYu * 10) % 10 > 5) { 232 rowsNum += 1; 233 } 234 console.log(TAG, 'rowsNum', rowsNum, 'rowsNumYu', rowsNumYu); 235 windowHeight = Constants.START_ABILITY_HEIGHT_DEFAULT1 + Constants.START_ABILITY_MSG_HEIGHT_ROWS * rowsNum; 236 } 237 if ([ 238 Constants.ERR_JS_GET_ACCOUNT_ERROR, 239 Constants.ERR_JS_NOT_DLP_FILE, 240 ].includes(error.code)) { 241 windowHeight = Constants.START_ABILITY_HEIGHT_ONE_ROWS; 242 } 243 if ([ 244 Constants.ERR_JS_SYSTEM_NEED_TO_BE_UPGRADED, 245 ].includes(error.code)) { 246 windowHeight = Constants.START_ABILITY_HEIGHT_TWO_ROWS; 247 } 248 if ([ 249 Constants.ERR_JS_APP_SYSTEM_IS_AUTHENTICATED, 250 Constants.ERR_JS_APP_ENCRYPTION_REJECTED, 251 Constants.ERR_JS_DLP_FILE_READ_ONLY, 252 Constants.ERR_JS_FILE_EXPIRATION, 253 Constants.ERR_JS_USER_NO_PERMISSION, 254 Constants.ERR_JS_APP_PARAM_ERROR, 255 Constants.ERR_JS_APP_OPEN_REJECTED, 256 ].includes(error.code)) { 257 windowHeight = Constants.START_ABILITY_HEIGHT_THREE_ROWS; 258 } 259 if ([ 260 Constants.ERR_JS_DLP_FILE_READ_ONLY, 261 Constants.ERR_JS_OFFLINE, 262 ].includes(error.code)) { 263 windowHeight = Constants.START_ABILITY_HEIGHT_FOUR_ROWS; 264 }; 265 let options: StartOptions = { 266 withAnimation: true, 267 windowLeft: xNumber, 268 windowTop: yNumber, 269 windowWidth: Constants.START_ABILITY_WINDOW_WIDTH, 270 windowHeight: windowHeight 271 }; 272 console.log(TAG, 'set options', JSON.stringify(options)); 273 context.startAbility({ 274 bundleName: Constants.DLP_MANAGER_BUNDLE_NAME, 275 abilityName: 'AlertAbility', 276 parameters: { 277 error: error, 278 windowHeight 279 } 280 }, options, async (err: BusinessError) => { 281 if (err.code !== 0) { 282 console.error(TAG, 'start AlertAbility failed', err.code, err.message); 283 } 284 if (session) { 285 session.terminateSelfWithResult({ 286 'resultCode': 0, 287 'want': { 288 'bundleName': Constants.DLP_MANAGER_BUNDLE_NAME, 289 }, 290 }); 291 } else if (GlobalContext.load('context')) { 292 (GlobalContext.load('context') as common.UIAbilityContext).terminateSelf(); 293 } else if (GlobalContext.load('viewContext')) { 294 (GlobalContext.load('viewContext') as common.ServiceExtensionContext).terminateSelf(); 295 } 296 }) 297} 298 299function judgeIsSandBox() { 300 return new Promise<boolean>(async resolve => { 301 let abilityWant: Want = GlobalContext.load('abilityWant') as Want; 302 let callerToken: number = abilityWant.parameters?.['ohos.aafwk.param.callerToken'] as number; 303 let callerBundleName: string = abilityWant.parameters?.['ohos.aafwk.param.callerBundleName'] as string; 304 GlobalContext.store('applicationInfo', await bundleManager.getApplicationInfo( 305 callerBundleName, bundleManager.ApplicationFlag.GET_APPLICATION_INFO_DEFAULT)); 306 if (callerToken === (GlobalContext.load('applicationInfo') as bundleManager.ApplicationInfo).accessTokenId) { 307 resolve(false); 308 } 309 resolve(true); 310 }) 311} 312 313let removeDuplicate = (arr: AuthAccount[], arg: string) => { 314 let map: Map<string, AuthAccount> = new Map(); 315 for (let item of arr) { 316 if (!map.has(item.authAccount)) { 317 map.set(item.authAccount, item); 318 } 319 } 320 return Array.from<AuthAccount>(map.values()); 321} 322 323 324let calculate = (newValue: Area, allNames: AuthAccount[]) => { 325 let editLength = allNames.length; 326 let screenWidth = Number(newValue['width']); 327 let nameChange = GlobalContext.load('domainAccount') ? Constants.ENCRYPTION_STAFF_WIDTH_NAME : Constants.ENCRYPTION_STAFF_WIDTH; 328 let rowNamesLen = Math.floor(screenWidth / (nameChange + Constants.ENCRYPTION_ADD_STAFF_MARGIN_RIGHT)); 329 let showNamesArr = editLength > Constants.ENCRYPTION_DOUBLED_NUMBER * rowNamesLen 330 ? allNames.slice(0, 2 * rowNamesLen - 1) 331 : allNames.slice(0, 2 * rowNamesLen); 332 let hideNamesNum = editLength - showNamesArr.length > 0 333 ? String(editLength - showNamesArr.length) 334 : '0'; 335 return { 336 'rowNamesLen': rowNamesLen, 337 'showNamesArr': showNamesArr, 338 'hideNamesNum': hideNamesNum 339 } as Record<string, number | AuthAccount[] | string> 340} 341 342let toggleShow = (allNames: AuthAccount[], showNamesArr: AuthAccount[], editFlag: boolean, rowNamesLen: number) => { 343 if (showNamesArr.length < allNames.length) { 344 let showFlag = !editFlag; 345 let showNamesArr = allNames; 346 return { 347 'showNamesArr': showNamesArr, 348 'showFlag': showFlag 349 } as Record<string, AuthAccount[] | boolean>; 350 } else { 351 let showFlag = !editFlag; 352 let showNamesArr = allNames.length > Constants.ENCRYPTION_DOUBLED_NUMBER * rowNamesLen 353 ? allNames.slice(0, Constants.ENCRYPTION_DOUBLED_NUMBER * rowNamesLen - 1) 354 : allNames.slice(0, Constants.ENCRYPTION_DOUBLED_NUMBER * rowNamesLen); 355 return { 356 'showNamesArr': showNamesArr, 357 'showFlag': showFlag 358 } as Record<string, AuthAccount[] | boolean>; 359 } 360} 361 362 363function directionStatus(func: Callback<number>) { 364 let innerEvent: emitter.InnerEvent = { 365 eventId: Constants.ENCRYPTION_EMIT_DIRECTION_STATUS 366 }; 367 emitter.on(innerEvent, (eventData: emitter.EventData) => { 368 func(eventData.data?.direction); 369 }); 370} 371 372function colorStatus(func: Callback<number>) { 373 let innerEvent: emitter.InnerEvent = { 374 eventId: Constants.ENCRYPTION_EMIT_COLOR_MODE 375 }; 376 emitter.on(innerEvent, (eventData: emitter.EventData) => { 377 func(eventData.data?.colorMode); 378 }); 379} 380 381function isPC(): boolean { 382 let deviceTypeName = deviceInfo.deviceType; 383 let productModel = deviceInfo.productModel; 384 385 return (deviceTypeName === 'tablet' || deviceTypeName === '2in1') && productModel?.startsWith('HYM') === true 386} 387 388async function showErrorDialogAndExit(error: BusinessError): Promise<void> { 389 let abilityWant = GlobalContext.load('abilityWant') as Want; 390 if (abilityWant.parameters) { 391 abilityWant.parameters.error = error; 392 } 393 let context: common.UIAbilityContext = GlobalContext.load('context') as common.UIAbilityContext; 394 GlobalContext.store('alertContext', context); 395 (GlobalContext.load('windowStage') as window.WindowStage).loadContent('pages/alert', (err: BusinessError) => { 396 if (err.code !== 0) { 397 console.error(TAG, 'loadContent failed', err.code, err.message); 398 } 399 }); 400} 401 402async function goToAlertMessagePage( 403 session: UIExtensionContentSession, 404 title?: Resource, 405 message?: Resource, 406 messageName?: Object, 407 cancel?: Resource, 408 ok?: Resource 409 ) { 410 let storage: LocalStorage = new LocalStorage({ 411 'session': session, 412 'title': title, 413 'message': message, 414 'messageName': messageName, 415 'cancel': cancel, 416 'ok': ok 417 } as Record<string, UIExtensionContentSession | Object | Resource>); 418 session.loadContent('pages/alertMessage', storage); 419 try { 420 session.setWindowBackgroundColor(Constants.TRANSPARENT_BACKGROUND_COLOR); 421 } catch (exception) { 422 console.error('Failed to set the background color. Cause: ' + JSON.stringify(exception)); 423 } 424} 425 426function getAppId(bundleName: string) { 427 return new Promise<string>(async resolve => { 428 let bundleFlags: number = bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_SIGNATURE_INFO; 429 let userId = await getUserId(); 430 try { 431 bundleManager.getBundleInfo(bundleName, bundleFlags, userId, (err, data) => { 432 if (err) { 433 console.error(TAG, 'get appId failed', (err as BusinessError).code, (err as BusinessError).message); 434 resolve(''); 435 } else { 436 resolve(data.signatureInfo.appId); 437 } 438 }) 439 } catch (err) { 440 console.error(TAG, 'get appId failed', (err as BusinessError).code, (err as BusinessError).message); 441 resolve(''); 442 } 443 }) 444} 445 446function getTime() { 447 let permanent: boolean = GlobalContext.load('permanent'); 448 if (permanent) { 449 return $r('app.string.permanently'); 450 } 451 let date: Date = GlobalContext.load('validity'); 452 let year = date.getFullYear(); 453 let month = date.getMonth() + 1; 454 let day = date.getDate(); 455 let hour = date.getHours(); 456 let minute = date.getMinutes(); 457 return `${year}/${month}/${day} ${hour}:${minute}`; 458} 459 460function getFileMsgByUri(uri: string): FileMsg { 461 let strArray: string[] = uri.split('/'); 462 let len: number = strArray.length; 463 if (len < 1) { 464 throw new Error('getFileMsgByUri,srcfile name len is 0'); 465 } 466 let fileName: string = strArray[len - 1]; 467 let filePath: string = strArray.slice(0, len - 1).join(); 468 let pointIndex: number = fileName.indexOf('.'); 469 if (pointIndex < 0) { 470 throw new Error('getFileMsgByUri,srcfile name invalid'); 471 } 472 let fileType: string = fileName.slice(pointIndex, fileName.length); 473 let result: FileMsg = { 474 fileName: fileName.split('.')[0], 475 filePath: filePath, 476 fileType: fileType 477 }; 478 return result; 479} 480 481async function getFileSizeByUri(uri: string): Promise<number> { 482 let inFile: fs.File; 483 484 try { 485 inFile = await fs.open(uri, fs.OpenMode.READ_ONLY); 486 } catch (error) { 487 console.error(TAG, 'open', uri, 'failed', (error as BusinessError).code, (error as BusinessError).message); 488 return -1; 489 } 490 491 try { 492 let stat = await fs.stat(inFile.fd); 493 console.info(TAG, "get file info succeed, the size of file is " + stat.size); 494 return stat.size; 495 } catch (err) { 496 console.info(TAG, "get file info failed with error message: " + err.message + ", error code: " + err.code); 497 return -1; 498 } 499} 500 501function getDLPInfo() { 502 return new Promise<DLPInfo>(async (resolve, reject) => { 503 let bundleFlags = bundleManager.BundleFlag.GET_BUNDLE_INFO_DEFAULT; 504 try { 505 bundleManager.getBundleInfoForSelf(bundleFlags, (err, data) => { 506 if (err) { 507 console.error(TAG, 'getBundleInfoForSelf failed. Cause: %{public}s', (err as BusinessError).message); 508 reject({name: '', versionCode: ''}); 509 } else { 510 resolve({name: data.name, versionCode: data.versionCode.toString()}); 511 } 512 }); 513 } catch (err) { 514 console.error(TAG, 'getBundleInfoForSelf failed. Cause: %{public}s', (err as BusinessError).message); 515 reject({name: '', versionCode: ''}); 516 } 517 }) 518} 519 520function sendDlpManagerAccountLogin(errorCode: number) { 521 let event: hiSysEvent.SysEventInfo = { 522 domain: 'DLP_UE', 523 name: 'DLP_MANAGER_ACCOUNT_LOGIN', 524 eventType: hiSysEvent?.EventType?.BEHAVIOR, 525 params: { 526 'PNAMEID': GlobalContext.load('hiPNameId') ?? '', 527 'PVERSIONID': GlobalContext.load('hiPVersionId') ?? '', 528 'ACCOUNT_TYPE': GlobalContext.load('hiAccountType') ?? '', 529 'ACCOUNT_STATUS': GlobalContext.load('hiAccountStatus') ?? -1, 530 'LOGIN_FAIL_CODE': errorCode ?? -1, 531 'PKG_NAME': GlobalContext.load('hiPkgName') ?? '', 532 } as Record<string, number> 533 }; 534 535 try { 536 hiSysEvent.write(event); 537 } catch (err) { 538 console.error(TAG, 'sendDlpManagerAccountLogin failed'); 539 } 540} 541 542function sendDlpManagerFileConfiguration() { 543 let event: hiSysEvent.SysEventInfo = { 544 domain: 'DLP_UE', 545 name: 'DLP_MANAGER_FILE_CONFIGURATION', 546 eventType: hiSysEvent?.EventType?.BEHAVIOR, 547 params: { 548 'PNAMEID': GlobalContext.load('hiPNameId') ?? '', 549 'PVERSIONID': GlobalContext.load('hiPVersionId') ?? '', 550 'OPERATION': GlobalContext.load('hiOperation') ?? '', 551 'READ_SCOPE': GlobalContext.load('hiReadScope') ?? '', 552 'WRITE_SCOPE': GlobalContext.load('hiWriteScope') ?? '', 553 'ADVANCED_SETTINGS': GlobalContext.load('hiAdvancedSettings') ?? false, 554 'STORE_PATH': GlobalContext.load('hiStorePath') ?? false, 555 'ACCOUNT_VERIFY_SUCC': GlobalContext.load('hiAccountVerifySucc') ?? -1, 556 'ACCOUNT_VERIFY_FAIL': GlobalContext.load('hiAccountVerifyFail') ?? -1, 557 'VALID_DATE': GlobalContext.load('hiValidDate') ?? false, 558 } as Record<string, number> 559 }; 560 561 try { 562 hiSysEvent.write(event); 563 } catch (err) { 564 console.error(TAG, 'sendDlpManagerFileConfiguration failed'); 565 } 566} 567 568function sendDlpFileCreateEvent() { 569 let event: hiSysEvent.SysEventInfo = { 570 domain: 'DLP_UE', 571 name: 'DLP_FILE_CREATE_EVENT', 572 eventType: hiSysEvent?.EventType?.BEHAVIOR, 573 params: { 574 'PNAMEID': GlobalContext.load('hiPNameId') ?? '', 575 'PVERSIONID': GlobalContext.load('hiPVersionId') ?? '', 576 'CODE': GlobalContext.load('hiCode') ?? -1, 577 'FILE_SIZE': GlobalContext.load('hiFileSize') ?? -1, 578 'FILE_TYPE': GlobalContext.load('hiFileType') ?? '', 579 'POLICY_SIZE_ENC': GlobalContext.load('hiPolicySizeEnc') ?? -1, 580 'PKG_NAME': GlobalContext.load('hiPkgName') ?? '', 581 } as Record<string, number> 582 }; 583 584 try { 585 hiSysEvent.write(event); 586 } catch (err) { 587 console.error(TAG, 'sendDlpFileCreateEvent failed'); 588 } 589} 590 591function sendDlpFileOpenEvent() { 592 let event: hiSysEvent.SysEventInfo = { 593 domain: 'DLP_UE', 594 name: 'DLP_FILE_OPEN_EVENT', 595 eventType: hiSysEvent?.EventType?.BEHAVIOR, 596 params: { 597 'PNAMEID': GlobalContext.load('hiPNameId') ?? '', 598 'PVERSIONID': GlobalContext.load('hiPVersionId') ?? '', 599 'CODE': GlobalContext.load('hiCode') ?? -1, 600 'SANDBOX_PKGNAME': GlobalContext.load('hiSandboxPkgName') ?? '', 601 'SANDBOX_INDEX': GlobalContext.load('hiSandboxIndex') ?? -1, 602 'ACCOUNT_TYPE': GlobalContext.load('hiAccountType') ?? '', 603 'FILE_SIZE': GlobalContext.load('hiFileSize') ?? -1, 604 'POLICY_SIZE_ENC': GlobalContext.load('hiPolicySizeEnc') ?? -1, 605 } as Record<string, number> 606 }; 607 608 try { 609 hiSysEvent.write(event); 610 } catch (err) { 611 console.error(TAG, 'sendDlpFileOpenEvent failed'); 612 } 613} 614 615function isValidPath(path: string): Boolean { 616 if (path.indexOf('/./') !== -1 || path.indexOf('/../') !== -1) { 617 return false; 618 } 619 return true; 620} 621 622function measureTextSizeWidth(str: Resource | string) { 623 let itemLenWithPX: SizeOptions = measure.measureTextSize({ 624 textContent: str, 625 fontSize: '16fp' 626 }) 627 return px2vp(Number(itemLenWithPX.width) + Constants.ENCRYPTION_PROTECTION_TIME_MENU_WIDTH); 628} 629 630export { 631 AuthAccount, 632 PermissionType, 633 getOsAccountInfo, 634 checkDomainAccountInfo, 635 getUserId, 636 getAuthPerm, 637 terminateSelfWithResult, 638 startAlertAbility, 639 getAlertMessage, 640 judgeIsSandBox, 641 getFileFd, 642 getFileUriByPath, 643 removeDuplicate, 644 calculate, 645 toggleShow, 646 directionStatus, 647 isPC, 648 showErrorDialogAndExit, 649 goToAlertMessagePage, 650 getAppId, 651 getTime, 652 getFileMsgByUri, 653 getFileSizeByUri, 654 getDLPInfo, 655 sendDlpManagerAccountLogin, 656 sendDlpManagerFileConfiguration, 657 sendDlpFileCreateEvent, 658 sendDlpFileOpenEvent, 659 DLPInfo, 660 isValidPath, 661 measureTextSizeWidth, 662 colorStatus 663}; 664