diff --git a/entry/src/main/ets/ViewAbility/ViewAbility.ets b/entry/src/main/ets/ViewAbility/ViewAbility.ets index 8edbe5f..d5db713 100644 --- a/entry/src/main/ets/ViewAbility/ViewAbility.ets +++ b/entry/src/main/ets/ViewAbility/ViewAbility.ets @@ -39,7 +39,7 @@ import { isValidPath, defaultDlpFile, getAccountType, - checkNetworkStatus + getConnectionStatus } from '../common/utils'; import { DlpFileInfo, @@ -76,7 +76,6 @@ export default class ViewAbility extends ServiceExtensionAbility { private dlpFile: dlpPermission.DLPFile = defaultDlpFile; private authPerm: dlpPermission.DLPFileAccess = dlpPermission.DLPFileAccess.READ_ONLY; private needCallAccount: boolean = true; - private needCheckAccountType: boolean = false; private sandboxBundleName: string = ''; private sandboxAbilityName: string = ''; private sandboxModuleName: string = ''; @@ -308,13 +307,11 @@ export default class ViewAbility extends ServiceExtensionAbility { bundleName: 'com.huawei.hmos.dlpcredmgr', abilityName: 'DlpCredLoginAbility' }; - try { - await checkNetworkStatus(); - } catch { + if (await getConnectionStatus() === false) { GetAlertMessage.requestModalUIExtension(this.context, { code: Constants.ERR_JS_APP_NETWORK_INVALID } as BusinessError); return; - }; + } try { await this.context.startAbility(accountWant); } catch (err) { @@ -746,7 +743,6 @@ export default class ViewAbility extends ServiceExtensionAbility { } async checkPermissionWithPluginId(want: Want): Promise { - let paramCallerBundleName = want.parameters?.['ohos.aafwk.param.callerBundleName'] as string; AppStorage.setOrCreate('paramCallerBundleName', paramCallerBundleName); @@ -762,13 +758,12 @@ export default class ViewAbility extends ServiceExtensionAbility { return false; } - this.needCheckAccountType = true; + this.needCallAccount = await this.checkNeedCallAccount(); return true; } async onRequest(want: Want, startId: number): Promise { HiLog.debug(TAG, `enter onRequest`); - if (!this.checkPermissionWithPluginId(want)) { return; } @@ -783,9 +778,6 @@ export default class ViewAbility extends ServiceExtensionAbility { this.sandboxBundleName = want.parameters?.['ohos.dlp.params.bundleName'] as string; this.sandboxAbilityName = want.parameters?.['ohos.dlp.params.abilityName'] as string; this.sandboxModuleName = want.parameters?.['ohos.dlp.params.moduleName'] as string; - if (this.needCheckAccountType) { - this.needCallAccount = await this.checkNeedCallAccount(); - } if (this.fileName === undefined || this.dlpFd === undefined || this.uri === undefined || this.sandboxBundleName === undefined || this.sandboxAbilityName === undefined || this.sandboxModuleName === undefined || !this.uri.endsWith('.dlp')) { diff --git a/entry/src/main/ets/common/utils.ets b/entry/src/main/ets/common/utils.ets index 6d56c7b..9a0a793 100644 --- a/entry/src/main/ets/common/utils.ets +++ b/entry/src/main/ets/common/utils.ets @@ -28,17 +28,40 @@ import { BusinessError, Callback } from '@ohos.base' import Want from '@ohos.app.ability.Want'; import common from '@ohos.app.ability.common'; import connection from '@ohos.net.connection'; -import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; import Constants from '../common/constant'; import GlobalContext from './GlobalContext'; import hiSysEvent from '@ohos.hiSysEvent'; import { HiLog } from '../common/HiLog'; +import { systemDateTime } from '@kit.BasicServicesKit'; const TAG = 'Utils'; const HEAD_LENGTH_IN_BYTE = 28; const HEAD_LENGTH_IN_U32 = 7; const CERT_OFFSET = 5; const CERT_SIZE = 6; + +const NET_CAPABILITY_INTERNET: number = 12; +const NET_CAPABILITY_VALIDATED: number = 16; +const NET_CAPABILITY_CHECKING_CONNECTIVITY: number = 31; +const TIME_OUT_SECONDS: number = 3; +const SLEEP_MILLISECONDS: number = 200; + +const DAY_TO_MILLISECOND: number = 1000 * 60 * 60 * 24; +const MINUTE_TO_MILLISECOND: number = 1000 * 60; +const SECOND_TO_MILLISECOND: number = 1000; + +const enum UnitType { + DAY_UNIT = 1, + MINUTE_UNIT = 2, + SECOND_UNIT = 3, +} + +const UNIT_MAP = new Map([ + [UnitType.DAY_UNIT, DAY_TO_MILLISECOND], + [UnitType.MINUTE_UNIT, MINUTE_TO_MILLISECOND], + [UnitType.SECOND_UNIT, SECOND_TO_MILLISECOND], +]); + class ChangeOption { public offset: number = 0 public length: number = 0 @@ -488,6 +511,64 @@ function removeDir(file: string) { } } +function getCurrentTime(isNano: boolean = false): number { + return systemDateTime.getTime(isNano); +} + +function isExpire(timestamp: number, diff: number, unit: number): boolean { + if (isNaN(timestamp) || diff <= 0) { + return true; + } + let multipleMill = UNIT_MAP.get(unit); + if (!multipleMill) { + HiLog.error(TAG, 'Invalid unit'); + return false; + } + + let currentTime = getCurrentTime(); + const diffMilliSecond = Number(currentTime) - Number(timestamp); + const compareDiffSec = diff * multipleMill; + return diffMilliSecond > compareDiffSec; +} + +async function sleep(milliSeconds: number): Promise { + return new Promise(resolve => setTimeout(resolve, milliSeconds)); +} + +async function getConnectionStatus(exactly: boolean = true): Promise { + HiLog.info(TAG, `Enter getConnectionStatus, exactly mode: ${exactly}}`); + try { + const startTime = getCurrentTime(); + let net: connection.NetHandle = await connection.getDefaultNet(); + let capabilities: connection.NetCapabilities = await connection.getNetCapabilities(net); + HiLog.info(TAG, `Succeeded to get net capabilities, ${JSON.stringify(capabilities)}`); + let networkCap = capabilities.networkCap; + while (exactly && networkCap?.includes(NET_CAPABILITY_CHECKING_CONNECTIVITY)) { + HiLog.info(TAG, `Checking connectivity...`); + if (isExpire(startTime, TIME_OUT_SECONDS, UnitType.SECOND_UNIT)) { + HiLog.error(TAG, `Network connection check failed.`); + return false; + } + await sleep(SLEEP_MILLISECONDS); + net = await connection.getDefaultNet(); + capabilities = await connection.getNetCapabilities(net); + networkCap = capabilities.networkCap; + } + + let internetExists = networkCap?.includes(NET_CAPABILITY_INTERNET); + let validatedExists = networkCap?.includes(NET_CAPABILITY_VALIDATED); + if (internetExists && validatedExists) { + HiLog.debug(TAG, 'Net connection is valid.'); + return true; + } else { + HiLog.error(TAG, 'Net connection is invalid.'); + } + } catch (error) { + HiLog.error(TAG, `GetConnectionStatus failed: ${JSON.stringify(error)}`); + } + return false; +} + function checkNetworkStatus(): Promise { return new Promise((resolve, reject) => { let netHandle = connection.getDefaultNetSync(); @@ -536,5 +617,6 @@ export { isValidPath, defaultDlpFile, getAccountType, - checkNetworkStatus + checkNetworkStatus, + getConnectionStatus }; diff --git a/entry/src/main/ets/pages/encryptedSharing.ets b/entry/src/main/ets/pages/encryptedSharing.ets index 33fb5b3..85a1c2b 100644 --- a/entry/src/main/ets/pages/encryptedSharing.ets +++ b/entry/src/main/ets/pages/encryptedSharing.ets @@ -35,7 +35,7 @@ import { sendDlpManagerAccountLogin, getAppId, getOsAccountInfo, - checkNetworkStatus + getConnectionStatus } from '../common/utils'; import { SystemUtils } from '../common/systemUtils'; @@ -69,6 +69,7 @@ let storage = LocalStorage.getShared(); @Entry(storage) @Component struct encryptedSharing { + static readonly GET_ACCOUNT_INFO_RESET = 'clear'; private connectService: ConnectService = new ConnectService(getContext(this)); @State titlebarMargin: LocalizedMargin = { start: LengthMetrics.vp(Constants.SHARE_TITLE_HEAD_MARGIN_RIGHT), @@ -76,7 +77,6 @@ struct encryptedSharing { }; @LocalStorageLink('commandSearchUserInfo') @Watch('beginToGenerateDLPFile') isInputInvalid: string = ''; @LocalStorageLink('commandGetAccountInfo') @Watch('changeAccountInfo') commandGetAccountInfo: string = ''; - @State commandGetAccountInfoFlag: boolean = false; @State dlpProperty: dlpPermission.DLPProperty = defaultDlpProperty; @State enabledFocus: boolean = true; @State isConfirmButtonEnabled: boolean = false; @@ -93,7 +93,7 @@ struct encryptedSharing { storage === undefined ? undefined : storage.get('session'); @State placeHolderStr: ResourceStr = ''; @State contactPerson: string = ''; - @State recordHashUid: string = ''; + @State recordSuccessUid: string = ''; @State osVersion: ResourceStr = ''; @State isTextInputFocus: boolean = false; @State generalType: string = 'general.file'; @@ -139,16 +139,10 @@ struct encryptedSharing { } changeAccountInfo() { + if (this.commandGetAccountInfo == encryptedSharing.GET_ACCOUNT_INFO_RESET) { + return; + } HiLog.info(TAG, `changeAccountInfo start`); - if (!this.checkCloudPhone(this.inputValue)) { - return; - } - if (!this.commandGetAccountInfo) { - this.enabledFocus = true; - this.isConfirmButtonEnabled = true; - this.showToast($r('app.string.Share_File_Encrypted_Failed')); - return; - } try { let commandGetAccountInfoCallBack = JSON.parse(this.commandGetAccountInfo) as Record; HiLog.info(TAG, `commandGetAccountInfo Call Back errorCode: ${commandGetAccountInfoCallBack.errorCode}`); @@ -157,25 +151,31 @@ struct encryptedSharing { this.ownerAccount = res?.uid; this.ownerAccountID = res?.uid; this.connectService.connectServiceShareAbility(Constants.COMMAND_SEARCH_USER_INFO); + return; } else { this.enabledFocus = true; this.isConfirmButtonEnabled = true; - this.recordHashUid = ''; + this.recordSuccessUid = ''; + this.commandGetAccountInfo = encryptedSharing.GET_ACCOUNT_INFO_RESET; if ([ Constants.ERR_CODE_NETWORK_ERROR, Constants.ERR_CODE_CONNECTION_FAIL, Constants.ERR_CODE_CONNECTION_TIME_OUT ].includes(Number(commandGetAccountInfoCallBack.errorCode))) { this.showToast($r('app.string.network_invalid')); - return; + } else { + this.showToast($r('app.string.Share_File_Encrypted_Failed')); } - this.showToast($r('app.string.Share_File_Encrypted_Failed')); + return; } } catch (error) { HiLog.error(TAG, `get account info failed: ${JSON.stringify(error)}`); this.showToast($r('app.string.Share_File_Encrypted_Failed')); this.enabledFocus = true; - this.isConfirmButtonEnabled = this.inputValue.length > 0; + this.isConfirmButtonEnabled = true; + this.recordSuccessUid = ''; + this.commandGetAccountInfo = encryptedSharing.GET_ACCOUNT_INFO_RESET; + return; } } @@ -304,7 +304,7 @@ struct encryptedSharing { if (!credentialCallBack.status && Number(credentialCallBack.errorCode) === Constants.ERR_CODE_SUCCESS) { HiLog.info(TAG, `credentialCallBack msg`); let str = this.getExternalResourceString(Constants.DLP_CREDMGR_BUNDLE_NAME, 'entry', 'no_user'); - this.credentialCallBackMsg = str.length > 0 ? str : credentialCallBack.msg; + this.credentialCallBackMsg = str.length > 0 ? str : credentialCallBack.errorMsg; this.phoneFormatTips = true; storage.setOrCreate('commandSearchUserInfo', ''); return false; @@ -354,15 +354,13 @@ struct encryptedSharing { async getLoginStatus() { HiLog.info(TAG, `get login status start.`); - try { - await checkNetworkStatus(); - } catch { + if (await getConnectionStatus() === false) { this.showToast($r('app.string.network_invalid')); this.enabledFocus = true; this.isConfirmButtonEnabled = this.inputValue.length > 0; this.isTextInputFocus = true; this.textInputGetFocus(); - return + return; } try { let accountInfo = await getOsAccountInfo(); @@ -380,9 +378,7 @@ struct encryptedSharing { async getAccountInfo() { HiLog.info(TAG, `get Account Info start`); - try { - await checkNetworkStatus(); - } catch { + if (await getConnectionStatus() === false) { this.showToast($r('app.string.network_invalid')); this.enabledFocus = true; this.isConfirmButtonEnabled = this.inputValue.length > 0; @@ -397,14 +393,13 @@ struct encryptedSharing { this.showUIExtensionForAccountLogin = true; return; } - if (accountInfo.distributedInfo.id !== this.recordHashUid) { + if (accountInfo.distributedInfo.id !== this.recordSuccessUid) { HiLog.info(TAG, `COMMAND_GET_ACCOUNT_INFO start`); this.connectService.connectServiceShareAbility(Constants.COMMAND_GET_ACCOUNT_INFO); - this.commandGetAccountInfoFlag = true; - this.recordHashUid = accountInfo.distributedInfo.id; + this.recordSuccessUid = accountInfo.distributedInfo.id; return; } else { - this.changeAccountInfo(); + this.connectService.connectServiceShareAbility(Constants.COMMAND_SEARCH_USER_INFO); } } catch (err) { HiLog.error(TAG, `getOsAccountInfo failed: ${JSON.stringify(err)}`);