1/* 2 * Copyright (c) 2024 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 UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 18import { Configuration } from '@ohos.app.ability.Configuration'; 19import emitter from '@ohos.events.emitter'; 20import Want from '@ohos.app.ability.Want'; 21import { HiLog } from '../common/HiLog'; 22import { getDLPInfo, DLPInfo } from '../common/utils'; 23import { EnvironmentCallback } from '@kit.AbilityKit'; 24import Constants from '../common/constant'; 25 26const TAG = 'share'; 27const BG_COLOR = '#00000000'; 28 29export default class EncryptedSharingAbility extends ActionExtensionAbility { 30 private callbackId: number = 0; 31 32 onForeground(): void { 33 HiLog.info(TAG, `EncryptedSharingAbility onForeground.`); 34 } 35 36 onBackground(): void { 37 HiLog.info(TAG, `EncryptedSharingAbility onBackground.`); 38 } 39 40 onConfigurationUpdate(newConfig: Configuration): void { 41 HiLog.info(TAG, 'EncryptedSharingAbility onConfigurationUpdate new language: ' + newConfig.language); 42 emitter.emit('onConfigurationUpdate'); 43 } 44 45 checkValidParameters(want: Want): boolean { 46 let parameters = want.parameters as Record<string, Object>; 47 let inputUris = parameters['ability.params.stream'] as Array<Object>; 48 let callerToken = parameters['ohos.aafwk.param.callerToken'] as number; 49 let callerBundleName: string = parameters['ohos.aafwk.param.callerBundleName'] as string; 50 if ((inputUris instanceof Array) && (typeof inputUris[0] === 'string') && (typeof callerToken === 'number') && 51 (typeof callerBundleName === 'string')) { 52 return true; 53 } 54 HiLog.error(TAG, `want.parameters invalid.`); 55 return false; 56 } 57 58 async onSessionCreate(want: Want, session: UIExtensionContentSession): Promise<void> { 59 HiLog.info(TAG, `EncryptedSharingAbility onSessionCreate.`); 60 if (this.checkValidParameters(want) === false) { 61 return; 62 } 63 64 let envCallback: EnvironmentCallback = { 65 onConfigurationUpdated(config) { 66 AppStorage.setOrCreate<number>(Constants.FONT_SIZE_SCALE_KEY, config.fontSizeScale); 67 }, 68 onMemoryLevel(level) { 69 HiLog.info(TAG, `onMemoryLevel level:${JSON.stringify(level)}`); 70 } 71 }; 72 73 let applicationContext = this.context.getApplicationContext(); 74 try { 75 this.callbackId = applicationContext.on('environment', envCallback); 76 } catch (err) { 77 HiLog.error(TAG, `on environment error:${JSON.stringify(err)}`); 78 } 79 80 let parameters = want.parameters as Record<string, Object>; 81 AppStorage.setOrCreate('hiPkgName', parameters['ohos.aafwk.param.callerBundleName']); 82 let dlpInfo: DLPInfo = await getDLPInfo(); 83 AppStorage.setOrCreate('hiPNameId', dlpInfo.name); 84 AppStorage.setOrCreate('hiPVersionId', dlpInfo.versionCode); 85 let storage: LocalStorage = new LocalStorage({ 86 'session': session, 87 'actionWant': want 88 } as Record<string, UIExtensionContentSession | Want>); 89 try { 90 session.loadContent('pages/encryptedSharing', storage); 91 session.setWindowBackgroundColor(BG_COLOR); 92 } catch (exception) { 93 HiLog.error(TAG, `Failed to set the background color. Cause: ${JSON.stringify(exception)}`); 94 } 95 } 96 97 onSessionDestroy(): void { 98 HiLog.info(TAG, `EncryptedSharingAbility onSessionDestroy.`); 99 } 100 101 onDestroy(): void { 102 HiLog.info(TAG, `EncryptedSharingAbility onDestroy.`); 103 let applicationContext = this.context.getApplicationContext(); 104 try { 105 applicationContext.off('environment', this.callbackId, (error, data) => { 106 if (error && error.code != 0) { 107 HiLog.error(TAG, `off environment error: ${JSON.stringify(error)}`); 108 } else { 109 HiLog.info(TAG, `off environment success: ${JSON.stringify(data)}`); 110 } 111 }); 112 } catch (err) { 113 HiLog.error(TAG, `off environment error:${JSON.stringify(err)}`); 114 } 115 } 116};