1885b47fbSopenharmony_ci/*
2885b47fbSopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3885b47fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4885b47fbSopenharmony_ci * you may not use this file except in compliance with the License.
5885b47fbSopenharmony_ci * You may obtain a copy of the License at
6885b47fbSopenharmony_ci *
7885b47fbSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8885b47fbSopenharmony_ci *
9885b47fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10885b47fbSopenharmony_ci * distributed under the License is distributed On an "AS IS" BASIS,
11885b47fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12885b47fbSopenharmony_ci * See the License for the specific language governing permissions and
13885b47fbSopenharmony_ci * limitations under the License.
14885b47fbSopenharmony_ci */
15885b47fbSopenharmony_ci
16885b47fbSopenharmony_ci#include "accessibility_common_event.h"
17885b47fbSopenharmony_ci#include <unistd.h>
18885b47fbSopenharmony_ci#include "accessible_ability_manager_service.h"
19885b47fbSopenharmony_ci#include "common_event_manager.h"
20885b47fbSopenharmony_ci#include "common_event_support.h"
21885b47fbSopenharmony_ci#include "hilog_wrapper.h"
22885b47fbSopenharmony_ci
23885b47fbSopenharmony_cinamespace OHOS {
24885b47fbSopenharmony_cinamespace Accessibility {
25885b47fbSopenharmony_cinamespace {
26885b47fbSopenharmony_ci    constexpr int32_t RETRY_SUBSCRIBER = 3;
27885b47fbSopenharmony_ci    const std::string KEY_USER_ID = "userId";
28885b47fbSopenharmony_ci} // namespace
29885b47fbSopenharmony_ci
30885b47fbSopenharmony_ciAccessibilityCommonEvent::AccessibilityCommonEvent()
31885b47fbSopenharmony_ci{
32885b47fbSopenharmony_ci    HILOG_DEBUG();
33885b47fbSopenharmony_ci    handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED] =
34885b47fbSopenharmony_ci        &AccessibilityCommonEvent::HandleUserAdded;
35885b47fbSopenharmony_ci    handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED] =
36885b47fbSopenharmony_ci        &AccessibilityCommonEvent::HandleUserRemoved;
37885b47fbSopenharmony_ci    handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED] =
38885b47fbSopenharmony_ci        &AccessibilityCommonEvent::HandleUserSwitched;
39885b47fbSopenharmony_ci    handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED] =
40885b47fbSopenharmony_ci        &AccessibilityCommonEvent::HandlePackageAdd;
41885b47fbSopenharmony_ci    handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED] =
42885b47fbSopenharmony_ci        &AccessibilityCommonEvent::HandlePackageRemoved;
43885b47fbSopenharmony_ci    handleEventFunc_[EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED] =
44885b47fbSopenharmony_ci        &AccessibilityCommonEvent::HandlePackageChanged;
45885b47fbSopenharmony_ci
46885b47fbSopenharmony_ci    for (auto it = handleEventFunc_.begin(); it != handleEventFunc_.end(); ++it) {
47885b47fbSopenharmony_ci        HILOG_DEBUG("Add event: %{public}s", it->first.c_str());
48885b47fbSopenharmony_ci        eventHandles_.emplace(it->first, [this, it](const OHOS::EventFwk::CommonEventData &eventId) {
49885b47fbSopenharmony_ci            auto requestFunc = it->second;
50885b47fbSopenharmony_ci            if (requestFunc != nullptr) {
51885b47fbSopenharmony_ci                (this->*requestFunc)(eventId);
52885b47fbSopenharmony_ci            }
53885b47fbSopenharmony_ci        });
54885b47fbSopenharmony_ci    }
55885b47fbSopenharmony_ci}
56885b47fbSopenharmony_ci
57885b47fbSopenharmony_ciAccessibilityCommonEvent::~AccessibilityCommonEvent()
58885b47fbSopenharmony_ci{
59885b47fbSopenharmony_ci    UnSubscriberEvent();
60885b47fbSopenharmony_ci}
61885b47fbSopenharmony_ci
62885b47fbSopenharmony_civoid AccessibilityCommonEvent::SubscriberEvent(const std::shared_ptr<AppExecFwk::EventHandler> &handler)
63885b47fbSopenharmony_ci{
64885b47fbSopenharmony_ci    HILOG_DEBUG();
65885b47fbSopenharmony_ci
66885b47fbSopenharmony_ci    if (subscriber_) {
67885b47fbSopenharmony_ci        HILOG_DEBUG("Common Event is already subscribered!");
68885b47fbSopenharmony_ci        return;
69885b47fbSopenharmony_ci    }
70885b47fbSopenharmony_ci
71885b47fbSopenharmony_ci    EventFwk::MatchingSkills matchingSkills;
72885b47fbSopenharmony_ci    for (auto &event : handleEventFunc_) {
73885b47fbSopenharmony_ci        HILOG_DEBUG("Add event: %{public}s", event.first.c_str());
74885b47fbSopenharmony_ci        matchingSkills.AddEvent(event.first);
75885b47fbSopenharmony_ci    }
76885b47fbSopenharmony_ci
77885b47fbSopenharmony_ci    EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
78885b47fbSopenharmony_ci    subscriber_ = std::make_shared<AccessibilityCommonEventSubscriber>(subscribeInfo, *this);
79885b47fbSopenharmony_ci    eventHandler_ = handler;
80885b47fbSopenharmony_ci
81885b47fbSopenharmony_ci    int32_t retry = RETRY_SUBSCRIBER;
82885b47fbSopenharmony_ci    do {
83885b47fbSopenharmony_ci        bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
84885b47fbSopenharmony_ci        if (subscribeResult) {
85885b47fbSopenharmony_ci            HILOG_INFO("SubscriberEvent success.");
86885b47fbSopenharmony_ci            return;
87885b47fbSopenharmony_ci        } else {
88885b47fbSopenharmony_ci            HILOG_DEBUG("SubscriberEvent failed, retry %{public}d", retry);
89885b47fbSopenharmony_ci            retry--;
90885b47fbSopenharmony_ci            sleep(1);
91885b47fbSopenharmony_ci        }
92885b47fbSopenharmony_ci    } while (retry);
93885b47fbSopenharmony_ci
94885b47fbSopenharmony_ci    HILOG_ERROR("SubscriberEvent failed.");
95885b47fbSopenharmony_ci}
96885b47fbSopenharmony_ci
97885b47fbSopenharmony_civoid AccessibilityCommonEvent::UnSubscriberEvent()
98885b47fbSopenharmony_ci{
99885b47fbSopenharmony_ci    HILOG_INFO();
100885b47fbSopenharmony_ci    eventHandles_.clear();
101885b47fbSopenharmony_ci    if (subscriber_) {
102885b47fbSopenharmony_ci        bool unSubscribeResult = EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
103885b47fbSopenharmony_ci        HILOG_INFO("unSubscribeResult = %{public}d", unSubscribeResult);
104885b47fbSopenharmony_ci        subscriber_ = nullptr;
105885b47fbSopenharmony_ci        eventHandler_ = nullptr;
106885b47fbSopenharmony_ci    }
107885b47fbSopenharmony_ci}
108885b47fbSopenharmony_ci
109885b47fbSopenharmony_civoid AccessibilityCommonEvent::OnReceiveEvent(const EventFwk::CommonEventData &data)
110885b47fbSopenharmony_ci{
111885b47fbSopenharmony_ci    HILOG_DEBUG();
112885b47fbSopenharmony_ci    if (!eventHandler_) {
113885b47fbSopenharmony_ci        HILOG_ERROR("eventHandler_ is nullptr.");
114885b47fbSopenharmony_ci        return;
115885b47fbSopenharmony_ci    }
116885b47fbSopenharmony_ci    eventHandler_->PostTask([this, data]() {
117885b47fbSopenharmony_ci        HILOG_DEBUG();
118885b47fbSopenharmony_ci        std::string action = data.GetWant().GetAction();
119885b47fbSopenharmony_ci        HILOG_INFO("Handle event:[%{public}s] eventHandles size[%{public}zu]", action.c_str(), eventHandles_.size());
120885b47fbSopenharmony_ci        auto it = eventHandles_.find(action);
121885b47fbSopenharmony_ci        if (it == eventHandles_.end()) {
122885b47fbSopenharmony_ci            HILOG_ERROR("Ignore event: %{public}s", action.c_str());
123885b47fbSopenharmony_ci            return;
124885b47fbSopenharmony_ci        }
125885b47fbSopenharmony_ci        it->second(data);
126885b47fbSopenharmony_ci        }, "TASK_ON_RECEIVE_EVENT");
127885b47fbSopenharmony_ci}
128885b47fbSopenharmony_ci
129885b47fbSopenharmony_civoid AccessibilityCommonEvent::HandleUserAdded(const EventFwk::CommonEventData &data) const
130885b47fbSopenharmony_ci{
131885b47fbSopenharmony_ci    int32_t accountId = data.GetCode();
132885b47fbSopenharmony_ci    HILOG_INFO();
133885b47fbSopenharmony_ci    if (accountId == -1) {
134885b47fbSopenharmony_ci        HILOG_ERROR("account id is wrong");
135885b47fbSopenharmony_ci        return;
136885b47fbSopenharmony_ci    }
137885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().AddedUser(accountId);
138885b47fbSopenharmony_ci}
139885b47fbSopenharmony_ci
140885b47fbSopenharmony_civoid AccessibilityCommonEvent::HandleUserRemoved(const EventFwk::CommonEventData &data) const
141885b47fbSopenharmony_ci{
142885b47fbSopenharmony_ci    int32_t accountId = data.GetCode();
143885b47fbSopenharmony_ci    HILOG_INFO();
144885b47fbSopenharmony_ci    if (accountId == -1) {
145885b47fbSopenharmony_ci        HILOG_ERROR("account id is wrong");
146885b47fbSopenharmony_ci        return;
147885b47fbSopenharmony_ci    }
148885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().RemovedUser(accountId);
149885b47fbSopenharmony_ci}
150885b47fbSopenharmony_ci
151885b47fbSopenharmony_civoid AccessibilityCommonEvent::HandleUserSwitched(const EventFwk::CommonEventData &data) const
152885b47fbSopenharmony_ci{
153885b47fbSopenharmony_ci    int32_t accountId = data.GetCode();
154885b47fbSopenharmony_ci    HILOG_INFO();
155885b47fbSopenharmony_ci    if (accountId == -1) {
156885b47fbSopenharmony_ci        HILOG_ERROR("account id is wrong");
157885b47fbSopenharmony_ci        return;
158885b47fbSopenharmony_ci    }
159885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().SwitchedUser(accountId);
160885b47fbSopenharmony_ci}
161885b47fbSopenharmony_ci
162885b47fbSopenharmony_civoid AccessibilityCommonEvent::HandlePackageRemoved(const EventFwk::CommonEventData &data) const
163885b47fbSopenharmony_ci{
164885b47fbSopenharmony_ci    std::string bundleName = data.GetWant().GetBundle();
165885b47fbSopenharmony_ci    int userId = data.GetWant().GetIntParam(KEY_USER_ID, 0);
166885b47fbSopenharmony_ci    int32_t accountId = Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountId();
167885b47fbSopenharmony_ci    HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
168885b47fbSopenharmony_ci    if (userId != accountId) {
169885b47fbSopenharmony_ci        HILOG_ERROR("not same user.");
170885b47fbSopenharmony_ci        return;
171885b47fbSopenharmony_ci    }
172885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PackageRemoved(bundleName);
173885b47fbSopenharmony_ci}
174885b47fbSopenharmony_ci
175885b47fbSopenharmony_civoid AccessibilityCommonEvent::HandlePackageAdd(const EventFwk::CommonEventData &data) const
176885b47fbSopenharmony_ci{
177885b47fbSopenharmony_ci    std::string bundleName = data.GetWant().GetBundle();
178885b47fbSopenharmony_ci    int userId = data.GetWant().GetIntParam(KEY_USER_ID, 0);
179885b47fbSopenharmony_ci    int32_t accountId = Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountId();
180885b47fbSopenharmony_ci    HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
181885b47fbSopenharmony_ci    if (userId != accountId) {
182885b47fbSopenharmony_ci        HILOG_ERROR("not same user.");
183885b47fbSopenharmony_ci        return;
184885b47fbSopenharmony_ci    }
185885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PackageAdd(bundleName);
186885b47fbSopenharmony_ci}
187885b47fbSopenharmony_ci
188885b47fbSopenharmony_civoid AccessibilityCommonEvent::HandlePackageChanged(const EventFwk::CommonEventData &data) const
189885b47fbSopenharmony_ci{
190885b47fbSopenharmony_ci    std::string bundleName = data.GetWant().GetBundle();
191885b47fbSopenharmony_ci    int userId = data.GetWant().GetIntParam(KEY_USER_ID, 0);
192885b47fbSopenharmony_ci    int32_t accountId = Singleton<AccessibleAbilityManagerService>::GetInstance().GetCurrentAccountId();
193885b47fbSopenharmony_ci    HILOG_INFO("bundleName is %{public}s", bundleName.c_str());
194885b47fbSopenharmony_ci    if (userId != accountId) {
195885b47fbSopenharmony_ci        HILOG_ERROR("not same user.");
196885b47fbSopenharmony_ci        return;
197885b47fbSopenharmony_ci    }
198885b47fbSopenharmony_ci    Singleton<AccessibleAbilityManagerService>::GetInstance().PackageChanged(bundleName);
199885b47fbSopenharmony_ci}
200885b47fbSopenharmony_ci} // namespace Accessibility
201885b47fbSopenharmony_ci} // namespace OHOS