1e75ebbc8Sopenharmony_ci/*
2e75ebbc8Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3e75ebbc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e75ebbc8Sopenharmony_ci * you may not use this file except in compliance with the License.
5e75ebbc8Sopenharmony_ci * You may obtain a copy of the License at
6e75ebbc8Sopenharmony_ci *
7e75ebbc8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e75ebbc8Sopenharmony_ci *
9e75ebbc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e75ebbc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e75ebbc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e75ebbc8Sopenharmony_ci * See the License for the specific language governing permissions and
13e75ebbc8Sopenharmony_ci * limitations under the License.
14e75ebbc8Sopenharmony_ci */
15e75ebbc8Sopenharmony_ci
16e75ebbc8Sopenharmony_ciimport ServiceExtensionContext from "application/ServiceExtensionContext";
17e75ebbc8Sopenharmony_ciimport Log from "../Log";
18e75ebbc8Sopenharmony_ciimport createOrGet from "../SingleInstanceHelper";
19e75ebbc8Sopenharmony_ciimport { EventParser, START_ABILITY_EVENT, Event, LocalEvent } from "./EventUtil";
20e75ebbc8Sopenharmony_ciimport { Callback, createEventBus, EventBus } from "./EventBus";
21e75ebbc8Sopenharmony_ciimport { BusinessError } from '@ohos.base';
22e75ebbc8Sopenharmony_ciimport {PluginType} from "../../plugindatasource/common/Constants";
23e75ebbc8Sopenharmony_ciimport {writeFaultLog, FaultID} from '../SysFaultLogger';
24e75ebbc8Sopenharmony_ci
25e75ebbc8Sopenharmony_ciexport type unsubscribe = () => void;
26e75ebbc8Sopenharmony_ciexport type Events = string | string[];
27e75ebbc8Sopenharmony_ci
28e75ebbc8Sopenharmony_ciconst TAG = "EventManager";
29e75ebbc8Sopenharmony_ci
30e75ebbc8Sopenharmony_ciclass EventManager {
31e75ebbc8Sopenharmony_ci    mEventBus: EventBus<string>;
32e75ebbc8Sopenharmony_ci    eventParser: EventParser;
33e75ebbc8Sopenharmony_ci    mContext: ServiceExtensionContext | undefined;
34e75ebbc8Sopenharmony_ci
35e75ebbc8Sopenharmony_ci    constructor() {
36e75ebbc8Sopenharmony_ci        this.mEventBus = createEventBus();
37e75ebbc8Sopenharmony_ci        this.eventParser = {
38e75ebbc8Sopenharmony_ci            local: this.publishLocalEvent,
39e75ebbc8Sopenharmony_ci            ability: this.startAbility,
40e75ebbc8Sopenharmony_ci            commonEvent: this.publishCommonEvent,
41e75ebbc8Sopenharmony_ci            remote: this.publishRemoteEvent,
42e75ebbc8Sopenharmony_ci        };
43e75ebbc8Sopenharmony_ci    }
44e75ebbc8Sopenharmony_ci
45e75ebbc8Sopenharmony_ci    setContext(ctx: ServiceExtensionContext) {
46e75ebbc8Sopenharmony_ci        this.mContext = ctx;
47e75ebbc8Sopenharmony_ci    }
48e75ebbc8Sopenharmony_ci
49e75ebbc8Sopenharmony_ci    publish(event: Event, pluginType?: PluginType): boolean {
50e75ebbc8Sopenharmony_ci        Log.showInfo(TAG, `PUBLISH : event: ${JSON.stringify(event)}`)
51e75ebbc8Sopenharmony_ci        return this.eventParser[event.target].call(this, event.data, pluginType);
52e75ebbc8Sopenharmony_ci    }
53e75ebbc8Sopenharmony_ci
54e75ebbc8Sopenharmony_ci    subscribe(eventType: Events, callback: Callback): unsubscribe {
55e75ebbc8Sopenharmony_ci        return this.mEventBus.on(eventType, callback);
56e75ebbc8Sopenharmony_ci    }
57e75ebbc8Sopenharmony_ci
58e75ebbc8Sopenharmony_ci    subscribeOnce(eventType: string, callback: Callback): unsubscribe {
59e75ebbc8Sopenharmony_ci        return this.mEventBus.once(eventType, callback);
60e75ebbc8Sopenharmony_ci    }
61e75ebbc8Sopenharmony_ci
62e75ebbc8Sopenharmony_ci    private publishLocalEvent(data: LocalEvent): boolean {
63e75ebbc8Sopenharmony_ci        Log.showDebug(TAG, `publish localEvent type: ${data.eventName}`);
64e75ebbc8Sopenharmony_ci        if (data.eventName) {
65e75ebbc8Sopenharmony_ci            this.mEventBus.emit(data.eventName, data.args);
66e75ebbc8Sopenharmony_ci            return true;
67e75ebbc8Sopenharmony_ci        }
68e75ebbc8Sopenharmony_ci        return false;
69e75ebbc8Sopenharmony_ci    }
70e75ebbc8Sopenharmony_ci
71e75ebbc8Sopenharmony_ci    private startAbility(data: { [key: string]: any }, pluginType?: PluginType): boolean {
72e75ebbc8Sopenharmony_ci        Log.showInfo(TAG, `start : data: ${JSON.stringify(data)}`)
73e75ebbc8Sopenharmony_ci        Log.showDebug(TAG, `start Ability: ${data.abilityName}`);
74e75ebbc8Sopenharmony_ci        if (data.bundleName && data.abilityName && this.mContext) {
75e75ebbc8Sopenharmony_ci            this.mEventBus.emit(START_ABILITY_EVENT, { abilityName: data.abilityName });
76e75ebbc8Sopenharmony_ci            this.mContext.startAbility({
77e75ebbc8Sopenharmony_ci                bundleName: data.bundleName,
78e75ebbc8Sopenharmony_ci                abilityName: data.abilityName,
79e75ebbc8Sopenharmony_ci                parameters: data.args??undefined
80e75ebbc8Sopenharmony_ci            }).then(() => {
81e75ebbc8Sopenharmony_ci                Log.showInfo(TAG, 'startAbility, then');
82e75ebbc8Sopenharmony_ci            }).catch((error: BusinessError) => {
83e75ebbc8Sopenharmony_ci                Log.showError(TAG, `startAbility, error: ${JSON.stringify(error)}`);
84e75ebbc8Sopenharmony_ci                if (pluginType == PluginType.META) {
85e75ebbc8Sopenharmony_ci                    writeFaultLog({CORE_SYSTEM: "com.ohos.systemui", TARGET_API:data.bundleName, FAULT_ID: FaultID.META_DIAGRAM_JUMP, MSG: "jump ability failure"})
86e75ebbc8Sopenharmony_ci                }
87e75ebbc8Sopenharmony_ci            });
88e75ebbc8Sopenharmony_ci            return true;
89e75ebbc8Sopenharmony_ci        }
90e75ebbc8Sopenharmony_ci        return false;
91e75ebbc8Sopenharmony_ci    }
92e75ebbc8Sopenharmony_ci
93e75ebbc8Sopenharmony_ci    private publishRemoteEvent(data: { [key: string]: any }): boolean {
94e75ebbc8Sopenharmony_ci        // todo publish to remote device
95e75ebbc8Sopenharmony_ci        return false;
96e75ebbc8Sopenharmony_ci    }
97e75ebbc8Sopenharmony_ci
98e75ebbc8Sopenharmony_ci    private publishCommonEvent(data: { [key: string]: any }): boolean {
99e75ebbc8Sopenharmony_ci        // todo publish commonEvent to other app
100e75ebbc8Sopenharmony_ci        return false;
101e75ebbc8Sopenharmony_ci    }
102e75ebbc8Sopenharmony_ci}
103e75ebbc8Sopenharmony_ci
104e75ebbc8Sopenharmony_cilet sEventManager = createOrGet(EventManager, TAG);
105e75ebbc8Sopenharmony_ci
106e75ebbc8Sopenharmony_ciexport default sEventManager as EventManager;