• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "accessibility_common_event.h"
17 #include <unistd.h>
18 #include "accessible_ability_manager_service.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "hilog_wrapper.h"
22 
23 namespace OHOS {
24 namespace Accessibility {
25 namespace {
26     constexpr int32_t RETRY_SUBSCRIBER = 3;
27 } // namespace
28 
AccessibilityCommonEvent()29 AccessibilityCommonEvent::AccessibilityCommonEvent()
30 {
31     HILOG_DEBUG();
32     handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED] =
33         &AccessibilityCommonEvent::HandleUserAdded;
34     handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED] =
35         &AccessibilityCommonEvent::HandleUserRemoved;
36     handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED] =
37         &AccessibilityCommonEvent::HandleUserSwitched;
38     handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED] =
39         &AccessibilityCommonEvent::HandlePackageAdd;
40     handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED] =
41         &AccessibilityCommonEvent::HandlePackageRemoved;
42     handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED] =
43         &AccessibilityCommonEvent::HandlePackageChanged;
44 
45     for (auto it = handleEventFunc_.begin(); it != handleEventFunc_.end(); ++it) {
46         HILOG_DEBUG("Add event: %{public}s", it->first.c_str());
47         eventHandles_.emplace(it->first, std::bind(it->second, this, std::placeholders::_1));
48     }
49 }
50 
~AccessibilityCommonEvent()51 AccessibilityCommonEvent::~AccessibilityCommonEvent()
52 {
53     UnSubscriberEvent();
54 }
55 
SubscriberEvent(const std::shared_ptr<AppExecFwk::EventHandler> & handler)56 void AccessibilityCommonEvent::SubscriberEvent(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
57 {
58     HILOG_DEBUG();
59 
60     if (subscriber_) {
61         HILOG_DEBUG("Common Event is already subscribered!");
62         return;
63     }
64 
65     EventFwk::MatchingSkills matchingSkills;
66     for (auto &event : handleEventFunc_) {
67         HILOG_DEBUG("Add event: %{public}s", event.first.c_str());
68         matchingSkills.AddEvent(event.first);
69     }
70 
71     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
72     subscriber_ = std::make_shared<AccessibilityCommonEventSubscriber>(subscribeInfo, *this);
73     eventHandler_ = handler;
74 
75     int32_t retry = RETRY_SUBSCRIBER;
76     do {
77         bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
78         if (subscribeResult) {
79             HILOG_INFO("SubscriberEvent success.");
80             return;
81         } else {
82             HILOG_DEBUG("SubscriberEvent failed, retry %{public}d", retry);
83             retry--;
84             sleep(1);
85         }
86     } while (retry);
87 
88     HILOG_ERROR("SubscriberEvent failed.");
89 }
90 
UnSubscriberEvent()91 void AccessibilityCommonEvent::UnSubscriberEvent()
92 {
93     HILOG_INFO();
94     eventHandles_.clear();
95     if (subscriber_) {
96         bool unSubscribeResult = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
97         HILOG_INFO("unSubscribeResult = %{public}d", unSubscribeResult);
98         subscriber_ = nullptr;
99         eventHandler_ = nullptr;
100     }
101 }
102 
OnReceiveEvent(const EventFwk::CommonEventData & data)103 void AccessibilityCommonEvent::OnReceiveEvent(const EventFwk::CommonEventData &data)
104 {
105     HILOG_DEBUG();
106     if (!eventHandler_) {
107         HILOG_ERROR("eventHandler_ is nullptr.");
108         return;
109     }
110     eventHandler_->PostTask(std::bind([this, data]() -> void {
111         HILOG_DEBUG();
112         std::string action = data.GetWant().GetAction();
113         HILOG_INFO("Handle event:[%{public}s] eventHandles size[%{public}zu]", action.c_str(), eventHandles_.size());
114         auto it = eventHandles_.find(action);
115         if (it == eventHandles_.end()) {
116             HILOG_ERROR("Ignore event: %{public}s", action.c_str());
117             return;
118         }
119         it->second(data);
120         }), "TASK_ON_RECEIVE_EVENT");
121 }
122 
HandleUserAdded(const EventFwk::CommonEventData & data) const123 void AccessibilityCommonEvent::HandleUserAdded(const EventFwk::CommonEventData &data) const
124 {
125     int32_t accountId = data.GetCode();
126     HILOG_INFO();
127     if (accountId == -1) {
128         HILOG_ERROR("account id is wrong");
129         return;
130     }
131     Singleton<AccessibleAbilityManagerService>::GetInstance().AddedUser(accountId);
132 }
133 
HandleUserRemoved(const EventFwk::CommonEventData & data) const134 void AccessibilityCommonEvent::HandleUserRemoved(const EventFwk::CommonEventData &data) const
135 {
136     int32_t accountId = data.GetCode();
137     HILOG_INFO();
138     if (accountId == -1) {
139         HILOG_ERROR("account id is wrong");
140         return;
141     }
142     Singleton<AccessibleAbilityManagerService>::GetInstance().RemovedUser(accountId);
143 }
144 
HandleUserSwitched(const EventFwk::CommonEventData & data) const145 void AccessibilityCommonEvent::HandleUserSwitched(const EventFwk::CommonEventData &data) const
146 {
147     int32_t accountId = data.GetCode();
148     HILOG_INFO();
149     if (accountId == -1) {
150         HILOG_ERROR("account id is wrong");
151         return;
152     }
153     Singleton<AccessibleAbilityManagerService>::GetInstance().SwitchedUser(accountId);
154 }
155 
HandlePackageRemoved(const EventFwk::CommonEventData & data) const156 void AccessibilityCommonEvent::HandlePackageRemoved(const EventFwk::CommonEventData &data) const
157 {
158     std::string bundleName = data.GetWant().GetBundle();
159     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
160     Singleton<AccessibleAbilityManagerService>::GetInstance().PackageRemoved(bundleName);
161 }
162 
HandlePackageAdd(const EventFwk::CommonEventData & data) const163 void AccessibilityCommonEvent::HandlePackageAdd(const EventFwk::CommonEventData &data) const
164 {
165     std::string bundleName = data.GetWant().GetBundle();
166     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
167     Singleton<AccessibleAbilityManagerService>::GetInstance().PackageAdd(bundleName);
168 }
169 
HandlePackageChanged(const EventFwk::CommonEventData & data) const170 void AccessibilityCommonEvent::HandlePackageChanged(const EventFwk::CommonEventData &data) const
171 {
172     std::string bundleName = data.GetWant().GetBundle();
173     HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
174     Singleton<AccessibleAbilityManagerService>::GetInstance().PackageChanged(bundleName);
175 }
176 } // namespace Accessibility
177 } // namespace OHOS