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 */ 15import { common, EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 16import { hilog } from '@kit.PerformanceAnalysisKit'; 17import { BusinessError, commonEventManager } from '@kit.BasicServicesKit'; 18 19import { bundleManager } from '@kit.AbilityKit'; 20const TAG: string = '[ExampleEmbeddedAbility]' 21 22export default class ExampleEmbeddedAbility extends EmbeddedUIExtensionAbility { 23 onCreate() { 24 console.log(TAG, `onCreate`) 25 let commonEventPublishData: commonEventManager.CommonEventPublishData = { 26 parameters: { 27 data: 'onCreate' 28 } 29 } 30 commonEventManager.publish('EmbeddedAbility_same_bundle', commonEventPublishData, async (error) => { 31 hilog.info(0x0000, 'testTag1', '%{public}s', `EmbeddedAbility3 publish: ${JSON.stringify(error)}`); 32 }) 33 } 34 35 onForeground() { 36 37 hilog.info(0x0000, 'testTag', `onForeground`) 38 } 39 40 onBackground() { 41 hilog.info(0x0000, 'testTag', `onBackground`) 42 let commonEventPublishData: commonEventManager.CommonEventPublishData = { 43 parameters: { 44 data: '333' 45 } 46 } 47 commonEventManager.publish('OnBackground', commonEventPublishData, async (error) => { 48 hilog.info(0x0000, 'testTag1', '%{public}s', `EmbeddedAbility3 publish: ${JSON.stringify(error)}`); 49 }) 50 } 51 52 onDestroy() { 53 54 hilog.info(0x0000, 'testTag', `onDestroy`) 55 let commonEventPublishData: commonEventManager.CommonEventPublishData = { 56 parameters: { 57 data: '222' 58 } 59 } 60 commonEventManager.publish('OnDestroy', commonEventPublishData, async (error) => { 61 hilog.info(0x0000, 'testTag1', '%{public}s', `EmbeddedAbility4 publish: ${JSON.stringify(error)}`); 62 }) 63 } 64 65 onSessionCreate(want: Want, session: UIExtensionContentSession) { 66 console.log(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`); 67 let param: Record<string, UIExtensionContentSession> = { 68 'session': session 69 }; 70 let storage: LocalStorage = new LocalStorage(param); 71 session.loadContent('EmbeddedUIExtAbility/pages/BundlePage', storage); 72 let abilityResult: common.AbilityResult = { 73 resultCode: 0, 74 want: { 75 abilityName: 'EmbeddedUIExtAbilityBundle', 76 parameters: { 77 'result': 123456 78 } 79 } 80 }; 81 session?.terminateSelfWithResult(abilityResult) 82 .then(() => { 83 console.info(`Successed in terminating self with result.`); 84 }) 85 .catch((err: BusinessError) => { 86 console.error(`Failed to terminate self with result, code: ${err.code}, msg: ${err.message}`); 87 }); 88 } 89 90 onSessionDestroy(session: UIExtensionContentSession) { 91 92 console.log(TAG, `onSessionDestroy`); 93 let commonEventPublishData: commonEventManager.CommonEventPublishData = { 94 parameters: { 95 data: '111' 96 } 97 } 98 commonEventManager.publish('OnSessionDestroy', commonEventPublishData, async (error) => { 99 hilog.info(0x0000, 'testTag1', '%{public}s', `EmbeddedAbility5 publish: ${JSON.stringify(error)}`); 100 }) 101 } 102}