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 { EmbeddedUIExtensionAbility, UIExtensionContentSession, Want } from '@kit.AbilityKit'; 16import { hilog } from '@kit.PerformanceAnalysisKit'; 17import { BusinessError, commonEventManager } from '@kit.BasicServicesKit'; 18 19 20 21const TAG: string = '[ExampleEmbeddedAbility]' 22 23export default class ExampleEmbeddedAbility extends EmbeddedUIExtensionAbility { 24 onCreate() { 25 console.log(TAG, `onCreate`) 26 let commonEventPublishData: commonEventManager.CommonEventPublishData = { 27 parameters: { 28 data: 'onCreate' 29 } 30 } 31 commonEventManager.publish('EmbeddedAbility_same_Default', commonEventPublishData, async (error) => { 32 hilog.info(0x0000, 'testTag1', '%{public}s', `EmbeddedAbilityDefault publish: ${JSON.stringify(error)}`); 33 }) 34 } 35 36 onForeground() { 37 console.log(TAG, `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 console.log(TAG, `onDestroy`); 54 let commonEventPublishData: commonEventManager.CommonEventPublishData = { 55 parameters: { 56 data: '222' 57 } 58 } 59 commonEventManager.publish('OnDestroy', commonEventPublishData, async (error) => { 60 hilog.info(0x0000, 'testTag1', '%{public}s', `EmbeddedAbility4 publish: ${JSON.stringify(error)}`); 61 }) 62 } 63 64 onSessionCreate(want: Want, session: UIExtensionContentSession) { 65 console.log(TAG, `onSessionCreate, want: ${JSON.stringify(want)}`); 66 let param: Record<string, UIExtensionContentSession> = { 67 'session': session 68 }; 69 let storage: LocalStorage = new LocalStorage(param); 70 session.loadContent('pages/DefaultPage', storage); 71 session?.terminateSelf() 72 .then(() => { 73 console.info(`Successed in terminating self.`); 74 }) 75 .catch((err: BusinessError) => { 76 console.error(`Failed to terminate self, code: ${err.code}, msg: ${err.message}`); 77 }); 78 } 79 80 onSessionDestroy(session: UIExtensionContentSession) { 81 console.log(TAG, `onSessionDestroy`); 82 let commonEventPublishData: commonEventManager.CommonEventPublishData = { 83 parameters: { 84 data: '111' 85 } 86 } 87 commonEventManager.publish('OnSessionDestroy', commonEventPublishData, async (error) => { 88 hilog.info(0x0000, 'testTag1', '%{public}s', `EmbeddedAbility5 publish: ${JSON.stringify(error)}`); 89 }) 90 } 91}