1/* 2 * Copyright (c) 2022 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 ServiceExtensionContext from "application/ServiceExtensionContext"; 17import Log from "../Log"; 18import createOrGet from "../SingleInstanceHelper"; 19import { EventParser, START_ABILITY_EVENT, Event, LocalEvent } from "./EventUtil"; 20import { Callback, createEventBus, EventBus } from "./EventBus"; 21import { BusinessError } from '@ohos.base'; 22import {PluginType} from "../../plugindatasource/common/Constants"; 23import {writeFaultLog, FaultID} from '../SysFaultLogger'; 24 25export type unsubscribe = () => void; 26export type Events = string | string[]; 27 28const TAG = "EventManager"; 29 30class EventManager { 31 mEventBus: EventBus<string>; 32 eventParser: EventParser; 33 mContext: ServiceExtensionContext | undefined; 34 35 constructor() { 36 this.mEventBus = createEventBus(); 37 this.eventParser = { 38 local: this.publishLocalEvent, 39 ability: this.startAbility, 40 commonEvent: this.publishCommonEvent, 41 remote: this.publishRemoteEvent, 42 }; 43 } 44 45 setContext(ctx: ServiceExtensionContext) { 46 this.mContext = ctx; 47 } 48 49 publish(event: Event, pluginType?: PluginType): boolean { 50 Log.showInfo(TAG, `PUBLISH : event: ${JSON.stringify(event)}`) 51 return this.eventParser[event.target].call(this, event.data, pluginType); 52 } 53 54 subscribe(eventType: Events, callback: Callback): unsubscribe { 55 return this.mEventBus.on(eventType, callback); 56 } 57 58 subscribeOnce(eventType: string, callback: Callback): unsubscribe { 59 return this.mEventBus.once(eventType, callback); 60 } 61 62 private publishLocalEvent(data: LocalEvent): boolean { 63 Log.showDebug(TAG, `publish localEvent type: ${data.eventName}`); 64 if (data.eventName) { 65 this.mEventBus.emit(data.eventName, data.args); 66 return true; 67 } 68 return false; 69 } 70 71 private startAbility(data: { [key: string]: any }, pluginType?: PluginType): boolean { 72 Log.showInfo(TAG, `start : data: ${JSON.stringify(data)}`) 73 Log.showDebug(TAG, `start Ability: ${data.abilityName}`); 74 if (data.bundleName && data.abilityName && this.mContext) { 75 this.mEventBus.emit(START_ABILITY_EVENT, { abilityName: data.abilityName }); 76 this.mContext.startAbility({ 77 bundleName: data.bundleName, 78 abilityName: data.abilityName, 79 parameters: data.args??undefined 80 }).then(() => { 81 Log.showInfo(TAG, 'startAbility, then'); 82 }).catch((error: BusinessError) => { 83 Log.showError(TAG, `startAbility, error: ${JSON.stringify(error)}`); 84 if (pluginType == PluginType.META) { 85 writeFaultLog({CORE_SYSTEM: "com.ohos.systemui", TARGET_API:data.bundleName, FAULT_ID: FaultID.META_DIAGRAM_JUMP, MSG: "jump ability failure"}) 86 } 87 }); 88 return true; 89 } 90 return false; 91 } 92 93 private publishRemoteEvent(data: { [key: string]: any }): boolean { 94 // todo publish to remote device 95 return false; 96 } 97 98 private publishCommonEvent(data: { [key: string]: any }): boolean { 99 // todo publish commonEvent to other app 100 return false; 101 } 102} 103 104let sEventManager = createOrGet(EventManager, TAG); 105 106export default sEventManager as EventManager;