• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "im_common_event_manager.h"
17 
18 #include <utility>
19 
20 #include "global.h"
21 #include "input_method_system_ability_stub.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "message_handler.h"
25 #include "system_ability_definition.h"
26 #include "itypes_util.h"
27 
28 namespace OHOS {
29 namespace MiscServices {
30     using namespace MessageID;
31     sptr<ImCommonEventManager> ImCommonEventManager::instance_;
32     std::mutex ImCommonEventManager::instanceLock_;
33     using namespace OHOS::EventFwk;
34     /*! Constructor
35     */
ImCommonEventManager()36     ImCommonEventManager::ImCommonEventManager()
37     {}
38 
39     /*! Destructor
40     */
~ImCommonEventManager()41     ImCommonEventManager::~ImCommonEventManager()
42     {}
43 
GetInstance()44     sptr<ImCommonEventManager> ImCommonEventManager::GetInstance()
45     {
46         if (!instance_) {
47             std::lock_guard<std::mutex> autoLock(instanceLock_);
48             if (!instance_) {
49                 IMSA_HILOGI("ImCommonEventManager::GetInstance instance_ is nullptr");
50                 instance_ = new ImCommonEventManager();
51             }
52         }
53         return instance_;
54     }
55 
SubscribeEvent(const std::string & event)56     bool ImCommonEventManager::SubscribeEvent(const std::string &event)
57     {
58         EventFwk::MatchingSkills matchingSkills;
59         matchingSkills.AddEvent(event);
60         matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USER_REMOVED);
61         matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
62 
63         EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
64 
65         std::shared_ptr<EventSubscriber> subscriber = std::make_shared<EventSubscriber>(subscriberInfo);
66         if (subscriber == nullptr) {
67             IMSA_HILOGI("ImCommonEventManager::SubscribeEvent subscriber is nullptr");
68             return false;
69         }
70         auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
71         if (abilityManager == nullptr) {
72             IMSA_HILOGE("SubscribeEvent abilityManager is nullptr");
73             return false;
74         }
75         sptr<ISystemAbilityStatusChange> listener = new (std::nothrow)
76             SystemAbilityStatusChangeListener([subscriber]() {
77                 bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
78                 IMSA_HILOGI("ImCommonEventManager::OnAddSystemAbility subscribeResult = %{public}d", subscribeResult);
79             });
80         if (listener == nullptr) {
81             IMSA_HILOGE("SubscribeEvent listener is nullptr");
82             return false;
83         }
84         int32_t ret = abilityManager->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, listener);
85         if (ret != ERR_OK) {
86             IMSA_HILOGE("SubscribeEvent SubscribeSystemAbility failed. ret = %{public}d", ret);
87             return false;
88         }
89         statusChangeListener_ = listener;
90         return true;
91     }
92 
SubscribeKeyboardEvent(KeyHandle handle)93     bool ImCommonEventManager::SubscribeKeyboardEvent(KeyHandle handle)
94     {
95         IMSA_HILOGI("ImCommonEventManager::SubscribeKeyboardEvent");
96         auto abilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
97         if (abilityManager == nullptr) {
98             IMSA_HILOGE("SubscribeKeyboardEvent abilityManager is nullptr");
99             return false;
100         }
101         sptr<ISystemAbilityStatusChange> listener = new (std::nothrow) SystemAbilityStatusChangeListener([handle]() {
102             int32_t ret = KeyboardEvent::GetInstance().AddKeyEventMonitor(handle);
103             IMSA_HILOGI(
104                 "SubscribeKeyboardEvent add monitor %{public}s", ret == ErrorCode::NO_ERROR ? "success" : "failed");
105         });
106         if (listener == nullptr) {
107             IMSA_HILOGE("SubscribeKeyboardEvent listener is nullptr");
108             return false;
109         }
110         int32_t ret = abilityManager->SubscribeSystemAbility(MULTIMODAL_INPUT_SERVICE_ID, listener);
111         if (ret != ERR_OK) {
112             IMSA_HILOGE("SubscribeKeyboardEvent SubscribeSystemAbility failed. ret = %{public}d", ret);
113             return false;
114         }
115         keyboardEventListener_ = listener;
116         return true;
117     }
118 
UnsubscribeEvent()119     bool ImCommonEventManager::UnsubscribeEvent()
120     {
121         return true;
122     }
123 
EventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)124 ImCommonEventManager::EventSubscriber::EventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
125     : EventFwk::CommonEventSubscriber(subscribeInfo)
126 {
127     EventManagerFunc_[CommonEventSupport::COMMON_EVENT_USER_SWITCHED] = &EventSubscriber::StartUser;
128     EventManagerFunc_[CommonEventSupport::COMMON_EVENT_USER_REMOVED] = &EventSubscriber::RemoveUser;
129     EventManagerFunc_[CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED] = &EventSubscriber::RemovePackage;
130 }
131 
OnReceiveEvent(const EventFwk::CommonEventData & data)132 void ImCommonEventManager::EventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data)
133 {
134     auto const &want = data.GetWant();
135     std::string action = want.GetAction();
136     IMSA_HILOGI("ImCommonEventManager::action = %{public}s!", action.c_str());
137     auto iter = EventManagerFunc_.find(action);
138     if (iter == EventManagerFunc_.end()) {
139         return;
140     }
141     auto EventListenerFunc = iter->second;
142     if (EventListenerFunc != nullptr) {
143         (this->*EventListenerFunc)(data);
144     }
145 }
146 
StartUser(const CommonEventData & data)147 void ImCommonEventManager::EventSubscriber::StartUser(const CommonEventData &data)
148 {
149     auto newUserId = data.GetCode();
150     IMSA_HILOGI("ImCommonEventManager::StartUser, userId = %{public}d", newUserId);
151     MessageParcel *parcel = new MessageParcel();
152     parcel->WriteInt32(newUserId);
153     Message *msg = new Message(MessageID::MSG_ID_USER_START, parcel);
154     MessageHandler::Instance()->SendMessage(msg);
155 }
156 
RemoveUser(const CommonEventData & data)157 void ImCommonEventManager::EventSubscriber::RemoveUser(const CommonEventData &data)
158 {
159     auto userId = data.GetCode();
160     IMSA_HILOGI("ImCommonEventManager::RemoveUser, userId = %{public}d", userId);
161     MessageParcel *parcel = new MessageParcel();
162     parcel->WriteInt32(userId);
163     Message *msg = new Message(MessageID::MSG_ID_USER_REMOVED, parcel);
164     MessageHandler::Instance()->SendMessage(msg);
165 }
166 
RemovePackage(const CommonEventData & data)167 void ImCommonEventManager::EventSubscriber::RemovePackage(const CommonEventData &data)
168 {
169     auto const &want = data.GetWant();
170     auto element = want.GetElement();
171     std::string bundleName = element.GetBundleName();
172     int32_t userId = want.GetIntParam("userId", 0);
173     IMSA_HILOGD("ImCommonEventManager::RemovePackage, bundleName = %{public}s, userId = %{public}d",
174         bundleName.c_str(), userId);
175     MessageParcel *parcel = new (std::nothrow) MessageParcel();
176     if (parcel == nullptr) {
177         IMSA_HILOGE("parcel is nullptr");
178         return;
179     }
180     if (!ITypesUtil::Marshal(*parcel, userId, bundleName)) {
181         IMSA_HILOGE("Failed to write message parcel");
182         delete parcel;
183         return;
184     }
185     Message *msg = new Message(MessageID::MSG_ID_PACKAGE_REMOVED, parcel);
186     MessageHandler::Instance()->SendMessage(msg);
187 }
188 
SystemAbilityStatusChangeListener(std::function<void ()> func)189     ImCommonEventManager::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
190         std::function<void()> func) : func_(std::move(func))
191     {
192     }
193 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)194     void ImCommonEventManager::SystemAbilityStatusChangeListener::OnAddSystemAbility(
195         int32_t systemAbilityId, const std::string& deviceId)
196     {
197         if (systemAbilityId != COMMON_EVENT_SERVICE_ID && systemAbilityId != MULTIMODAL_INPUT_SERVICE_ID) {
198             IMSA_HILOGE("ImCommonEventManager::OnAddSystemAbility systemAbilityId %{public}d", systemAbilityId);
199             return;
200         }
201         if (func_ != nullptr) {
202             func_();
203         }
204     }
205 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)206     void ImCommonEventManager::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
207         int32_t systemAbilityId, const std::string& deviceId)
208     {
209     }
210 } // namespace MiscServices
211 } // namespace OHOS
212