1 /* 2 * Copyright (C) 2021 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 16 #ifndef SERVICES_INCLUDE_IM_COMMON_EVENT_MANAGER_H 17 #define SERVICES_INCLUDE_IM_COMMON_EVENT_MANAGER_H 18 19 #include <functional> 20 #include <mutex> 21 #include <vector> 22 23 #include "common_event_data.h" 24 #include "common_event_manager.h" 25 #include "common_event_subscribe_info.h" 26 #include "common_event_subscriber.h" 27 #include "common_event_support.h" 28 #include "focus_monitor_manager.h" 29 #include "keyboard_event.h" 30 #include "matching_skills.h" 31 #include "system_ability_status_change_stub.h" 32 33 namespace OHOS { 34 namespace MiscServices { 35 using StartInputHandler = std::function<void()>; 36 class ImCommonEventManager : public RefBase { 37 public: 38 ImCommonEventManager(); 39 ~ImCommonEventManager(); 40 static sptr<ImCommonEventManager> GetInstance(); 41 bool SubscribeEvent(const std::string &event); 42 bool SubscribeKeyboardEvent(KeyHandle handle); 43 bool SubscribeWindowManagerService(FocusHandle handle, StartInputHandler inputHandler); 44 45 bool UnsubscribeEvent(); 46 class EventSubscriber : public EventFwk::CommonEventSubscriber { 47 public: 48 EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo); 49 void OnReceiveEvent(const EventFwk::CommonEventData &data); 50 void RemovePackage(const EventFwk::CommonEventData &data); 51 void StartUser(const EventFwk::CommonEventData &data); 52 void RemoveUser(const EventFwk::CommonEventData &data); 53 54 private: 55 using EventListenerFunc = void (EventSubscriber::*)(const EventFwk::CommonEventData &data); 56 std::map<std::string, EventListenerFunc> EventManagerFunc_; 57 }; 58 59 private: 60 class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { 61 public: 62 explicit SystemAbilityStatusChangeListener(std::function<void()>); 63 ~SystemAbilityStatusChangeListener() = default; 64 virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 65 virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 66 67 private: 68 std::function<void()> func_ = nullptr; 69 }; 70 71 private: 72 static std::mutex instanceLock_; 73 static sptr<ImCommonEventManager> instance_; 74 sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr; 75 sptr<ISystemAbilityStatusChange> keyboardEventListener_ = nullptr; 76 sptr<ISystemAbilityStatusChange> focusChangeEventListener_ = nullptr; 77 }; 78 } // namespace MiscServices 79 } // namespace OHOS 80 #endif // SERVICES_INCLUDE_IM_COMMON_EVENT_MANAGER_H 81