1/* 2 * Copyright (c) 2024-2025 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 ActionExtensionAbility from '@ohos.app.ability.ActionExtensionAbility'; 17import { Configuration } from '@ohos.app.ability.Configuration'; 18import Constants from '../common/constant'; 19import emitter from '@ohos.events.emitter'; 20import EncryptSharingHelper from '../component/helper/EncryptSharingHelper'; 21import { EncryptSharingShowCodeEnum, EncryptSharingTerminateCode } from '../common/enum/EncryptSharingShowCodeEnum'; 22import { EnvironmentCallback } from '@kit.AbilityKit'; 23import { getDLPInfo, logErrorShowToast } from '../common/FileUtils/utils'; 24import { HiLog } from '../common/HiLog'; 25import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 26import UriValidator from '../validator/UriValidator'; 27import Want from '@ohos.app.ability.Want'; 28 29const TAG = 'share'; 30const BG_COLOR = '#00000000'; 31 32interface encryptedSharingParams { 33 session: UIExtensionContentSession, 34 actionWant: Want, 35 inputUriArray: string[] 36} 37 38export default class EncryptedSharingAbility extends ActionExtensionAbility { 39 private callbackId: number = 0; 40 private inputUriArray: string[] = []; 41 42 onForeground(): void { 43 HiLog.info(TAG, 'EncryptedSharingAbility onForeground.'); 44 } 45 46 onBackground(): void { 47 HiLog.info(TAG, 'EncryptedSharingAbility onBackground.'); 48 } 49 50 onConfigurationUpdate(newConfig: Configuration): void { 51 HiLog.info(TAG, 'EncryptedSharingAbility onConfigurationUpdate new language: ' + newConfig.language); 52 emitter.emit('onConfigurationUpdate'); 53 } 54 55 checkInputUris(want: Want): boolean { 56 HiLog.info(TAG, 'Check input URIs.'); 57 const parameters = want.parameters as Record<string, Object>; 58 this.inputUriArray = parameters[Constants.PARAMS_STREAM] as string[]; 59 60 let validator = UriValidator.getInstance(); 61 const result = validator.validate(this.inputUriArray); 62 if (result.errorCode === EncryptSharingShowCodeEnum.SUCCESS) { 63 return true; 64 } else if (result.errorCode === EncryptSharingShowCodeEnum.SUPPORTED_TYPE_ERROR) { 65 let typeStr: string = this.context.resourceManager.getStringSync($r('app.string.Share_File_Supported_Types')); 66 result.toastMsg = this.context.resourceManager.getStringSync($r('app.string.Share_File_Type_Error'), typeStr); 67 } 68 logErrorShowToast(this.context, result.logMsg, result.toastMsg); 69 return false; 70 } 71 72 checkValidParameters(want: Want): boolean { 73 let parameters: Record<string, Object> = want.parameters as Record<string, Object>; 74 let inputUris: string[] = parameters[Constants.PARAMS_STREAM] as string[]; 75 let callerToken: number = parameters[Constants.PARAMS_CALLER_TOKEN] as number; 76 let callerBundleName: string = parameters[Constants.PARAMS_CALLER_BUNDLE_NAME] as string; 77 78 let paramsTypeFlag: boolean = 79 Array.isArray(inputUris) && (typeof callerToken === 'number') && (typeof callerBundleName === 'string'); 80 if (!paramsTypeFlag) { 81 let logMsg: string = 'Check the types of inputUris, callerToken and callerBundleName.'; 82 let code: number = EncryptSharingShowCodeEnum.ENCRYPT_FAIL_ERROR; 83 logErrorShowToast(this.context, logMsg, EncryptSharingHelper.getShowErr(code)); 84 return false; 85 } 86 87 let checkUrisFlag: boolean = this.checkInputUris(want); 88 return checkUrisFlag; 89 } 90 91 async onSessionCreate(want: Want, session: UIExtensionContentSession): Promise<void> { 92 HiLog.info(TAG, 'EncryptedSharingAbility onSessionCreate.'); 93 94 if (!this.checkValidParameters(want)) { 95 HiLog.error(TAG, 'The parameters of want are invalid.'); 96 setTimeout(() => { 97 try { 98 session!.terminateSelfWithResult({ 99 resultCode: EncryptSharingTerminateCode.CHECK_URI_FAILED 100 }); 101 } catch (error) { 102 HiLog.error(TAG, `terminateSelfWithResult exception, error is ${JSON.stringify(error)}`); 103 } 104 }, Constants.SHARE_SET_TIMEOUT); 105 return; 106 } 107 108 let envCallback: EnvironmentCallback = { 109 onConfigurationUpdated(config) { 110 AppStorage.setOrCreate<number>(Constants.FONT_SIZE_SCALE_KEY, config.fontSizeScale); 111 }, 112 onMemoryLevel(level) { 113 HiLog.info(TAG, `onMemoryLevel level: ${JSON.stringify(level)}`); 114 } 115 }; 116 117 let applicationContext = this.context.getApplicationContext(); 118 try { 119 this.callbackId = applicationContext.on('environment', envCallback); 120 } catch (err) { 121 HiLog.error(TAG, `on environment error: ${JSON.stringify(err)}`); 122 } 123 124 let parameters = want.parameters as Record<string, Object>; 125 AppStorage.setOrCreate('hiPkgName', parameters[Constants.PARAMS_CALLER_BUNDLE_NAME]); 126 127 let dlpInfoRet = await getDLPInfo(); 128 if (dlpInfoRet.errcode === Constants.ERR_CODE_SUCCESS && dlpInfoRet.result) { 129 AppStorage.setOrCreate('hiPNameId', dlpInfoRet.result.name); 130 AppStorage.setOrCreate('hiPVersionId', dlpInfoRet.result.versionCode); 131 } 132 133 const storageParams: encryptedSharingParams = { 134 'session': session, 135 'actionWant': want, 136 'inputUriArray': this.inputUriArray 137 } 138 const storage: LocalStorage = new LocalStorage(storageParams); 139 try { 140 session.loadContent('pages/encryptedSharing', storage); 141 session.setWindowBackgroundColor(BG_COLOR); 142 } catch (exception) { 143 HiLog.error(TAG, `Failed to set the background color. Cause: ${JSON.stringify(exception)}`); 144 } 145 } 146 147 onSessionDestroy(): void { 148 HiLog.info(TAG, 'EncryptedSharingAbility onSessionDestroy.'); 149 } 150 151 onDestroy(): void { 152 HiLog.info(TAG, 'EncryptedSharingAbility onDestroy.'); 153 let applicationContext = this.context.getApplicationContext(); 154 try { 155 applicationContext.off('environment', this.callbackId, (error, data) => { 156 if (error && error.code !== 0) { 157 HiLog.error(TAG, `off environment error: ${JSON.stringify(error)}`); 158 } else { 159 HiLog.info(TAG, `off environment success: ${JSON.stringify(data)}`); 160 } 161 }); 162 } catch (err) { 163 HiLog.error(TAG, `off environment error:${JSON.stringify(err)}`); 164 } 165 } 166}