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 "keyboard_event.h" 29 #include "matching_skills.h" 30 #include "system_ability_status_change_stub.h" 31 32 namespace OHOS { 33 namespace MiscServices { 34 class ImCommonEventManager : public RefBase { 35 public: 36 ImCommonEventManager(); 37 ~ImCommonEventManager(); 38 static sptr<ImCommonEventManager> GetInstance(); 39 bool SubscribeEvent(const std::string &event); 40 bool SubscribeKeyboardEvent(KeyHandle handle); 41 42 bool UnsubscribeEvent(); 43 class EventSubscriber : public EventFwk::CommonEventSubscriber { 44 public: 45 EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo); 46 void OnReceiveEvent(const EventFwk::CommonEventData &data); 47 void RemovePackage(const EventFwk::CommonEventData &data); 48 void StartUser(const EventFwk::CommonEventData &data); 49 void RemoveUser(const EventFwk::CommonEventData &data); 50 51 private: 52 using EventListenerFunc = void (EventSubscriber::*)(const EventFwk::CommonEventData &data); 53 std::map<std::string, EventListenerFunc> EventManagerFunc_; 54 }; 55 private: 56 class SystemAbilityStatusChangeListener : public SystemAbilityStatusChangeStub { 57 public: 58 explicit SystemAbilityStatusChangeListener(std::function<void()>); 59 ~SystemAbilityStatusChangeListener() = default; 60 virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 61 virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 62 63 private: 64 std::function<void()> func_ = nullptr; 65 }; 66 private: 67 static std::mutex instanceLock_; 68 69 static sptr<ImCommonEventManager> instance_; 70 sptr<ISystemAbilityStatusChange> statusChangeListener_ = nullptr; 71 sptr<ISystemAbilityStatusChange> keyboardEventListener_ = nullptr; 72 }; 73 } // namespace MiscServices 74 } // namespace OHOS 75 #endif // SERVICES_INCLUDE_IM_COMMON_EVENT_MANAGER_H 76