• 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 "window_common_event.h"
17 
18 #include <thread>
19 #include <want.h>
20 #include "common_event_subscribe_info.h"
21 #include "event_runner.h"
22 #include "skills.h"
23 #include "window_manager_hilog.h"
24 #include "window_manager_service.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 namespace {
29     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "CommonEvent"};
30     constexpr int RETRY_MAX_COUNT = 3;
31     const std::string THREAD_ID = "WindowCommonEventHandler";
32 }
33 
WindowCommonEvent()34 WindowCommonEvent::WindowCommonEvent()
35 {
36     handleCommonEventFuncs_.insert(make_pair(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED,
37         &WindowCommonEvent::HandleAccountSwitched));
38     auto runner = AppExecFwk::EventRunner::Create(THREAD_ID);
39     eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
40 }
41 
~WindowCommonEvent()42 WindowCommonEvent::~WindowCommonEvent()
43 {
44 }
45 
SubscriberEvent()46 void WindowCommonEvent::SubscriberEvent()
47 {
48     EventFwk::MatchingSkills skills;
49     skills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
50     EventFwk::CommonEventSubscribeInfo info(skills);
51     subscriber_ = std::make_shared<EventSubscriber>(info, shared_from_this());
52     int retry = RETRY_MAX_COUNT;
53     SubscriberEventInner(retry);
54 }
55 
SubscriberEventInner(int retry)56 void WindowCommonEvent::SubscriberEventInner(int retry)
57 {
58     if (retry <= 0) {
59         return;
60     }
61     retry--;
62     WLOGI("called action = %{public}d", retry);
63     if (EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_)) {
64         return;
65     }
66     std::function<void()> func = std::bind(&WindowCommonEvent::SubscriberEventInner, this, retry);
67     // post task delay 500ms
68     eventHandler_->PostTask(func, "wms:SubscriberEventInner", 500, AppExecFwk::EventQueue::Priority::HIGH);
69 }
70 
UnSubscriberEvent()71 void WindowCommonEvent::UnSubscriberEvent()
72 {
73     auto task = [this] {
74         EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
75     };
76     eventHandler_->PostTask(task, "wms:UnSubscriberEvent", 0, AppExecFwk::EventQueue::Priority::HIGH);
77 }
78 
OnReceiveEvent(const EventFwk::CommonEventData & data)79 void WindowCommonEvent::OnReceiveEvent(const EventFwk::CommonEventData& data)
80 {
81     WLOGI("receive common event, action = %{public}s", data.GetWant().GetAction().c_str());
82     auto task = [this, data] {
83         std::string action = data.GetWant().GetAction();
84         WLOGI("called action = %{public}s", action.c_str());
85         if (handleCommonEventFuncs_.count(action)) {
86             (this->*handleCommonEventFuncs_[action])(data);
87         }
88     };
89     eventHandler_->PostTask(task, "wms:OnReceiveEvent", 0, AppExecFwk::EventQueue::Priority::HIGH);
90 }
91 
HandleAccountSwitched(const EventFwk::CommonEventData & data) const92 void WindowCommonEvent::HandleAccountSwitched(const EventFwk::CommonEventData& data) const
93 {
94     int accountId = data.GetCode();
95     WLOGI("handle account switch, account id = %{public}d", accountId);
96     WindowManagerService::GetInstance().OnAccountSwitched(accountId);
97 }
98 } // Rosen
99 } // OHOS