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 26namespace OHOS { 27namespace Rosen { 28namespace { 29constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "CommonEvent"}; 30constexpr int RETRY_MAX_COUNT = 3; 31const std::string THREAD_ID = "WindowCommonEventHandler"; 32 33enum class CommonEventCode : uint32_t { 34 COMMON_EVENT_USER_SWITCHED, 35}; 36 37const std::map<std::string, CommonEventCode> COMMON_EVENT_CODE_MAP { 38 {EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED, CommonEventCode::COMMON_EVENT_USER_SWITCHED}, 39}; 40} 41 42WindowCommonEvent::WindowCommonEvent() 43{ 44 auto runner = AppExecFwk::EventRunner::Create(THREAD_ID); 45 eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner); 46} 47 48WindowCommonEvent::~WindowCommonEvent() 49{ 50} 51 52void WindowCommonEvent::SubscriberEvent() 53{ 54 EventFwk::MatchingSkills skills; 55 skills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED); 56 EventFwk::CommonEventSubscribeInfo info(skills); 57 subscriber_ = std::make_shared<EventSubscriber>(info, shared_from_this()); 58 int retry = RETRY_MAX_COUNT; 59 SubscriberEventInner(retry); 60} 61 62void WindowCommonEvent::SubscriberEventInner(int retry) 63{ 64 if (retry <= 0) { 65 return; 66 } 67 retry--; 68 WLOGI("called action = %{public}d", retry); 69 if (EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_)) { 70 return; 71 } 72 std::function<void()> func = [this, retry]() { this->SubscriberEventInner(retry); }; 73 // post task delay 500ms 74 eventHandler_->PostTask(func, "wms:SubscriberEventInner", 500, AppExecFwk::EventQueue::Priority::HIGH); 75} 76 77void WindowCommonEvent::UnSubscriberEvent() 78{ 79 auto task = [this] { 80 EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_); 81 }; 82 eventHandler_->PostTask(task, "wms:UnSubscriberEvent", 0, AppExecFwk::EventQueue::Priority::HIGH); 83} 84 85void WindowCommonEvent::OnReceiveEvent(const EventFwk::CommonEventData& data) 86{ 87 WLOGI("receive common event, action = %{public}s", data.GetWant().GetAction().c_str()); 88 auto task = [this, data] { 89 std::string action = data.GetWant().GetAction(); 90 WLOGI("called action = %{public}s", action.c_str()); 91 if (auto iter = COMMON_EVENT_CODE_MAP.find(action); iter != COMMON_EVENT_CODE_MAP.end()) { 92 CommonEventCode commonEventCode = iter->second; 93 if (commonEventCode == CommonEventCode::COMMON_EVENT_USER_SWITCHED) { 94 HandleAccountSwitched(data); 95 } 96 } 97 }; 98 eventHandler_->PostTask(task, "wms:OnReceiveEvent", 0, AppExecFwk::EventQueue::Priority::HIGH); 99} 100 101void WindowCommonEvent::HandleAccountSwitched(const EventFwk::CommonEventData& data) const 102{ 103 int accountId = data.GetCode(); 104 WLOGI("handle account switch, account id = %{public}d", accountId); 105 WindowManagerService::GetInstance().OnAccountSwitched(accountId); 106} 107} // Rosen 108} // OHOS