1 /* 2 * Copyright (c) 2025 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 #include "adapter/ohos/capability/statusbar/statusbar_event_proxy_ohos.h" 16 17 #include "common_event_manager.h" 18 #include "common_event_support.h" 19 20 #include "frameworks/core/common/container.h" 21 22 namespace OHOS::Ace { 23 std::unique_ptr<StatusBarEventProxy> StatusBarEventProxy::instance_; 24 std::mutex StatusBarEventProxy::mutex_; 25 GetInstance()26StatusBarEventProxy* StatusBarEventProxy::GetInstance() 27 { 28 if (!instance_) { 29 std::scoped_lock lock(mutex_); 30 if (!instance_) { 31 instance_ = std::make_unique<StatusBarEventProxyOhos>(); 32 } 33 } 34 return instance_.get(); 35 } 36 37 using OHOS::EventFwk::CommonEventManager; 38 using OHOS::EventFwk::CommonEventSubscribeInfo; 39 using OHOS::EventFwk::CommonEventSupport; 40 using OHOS::EventFwk::MatchingSkills; 41 OnReceiveEvent(const CommonEventData &)42void StatusBarEventSubscriber::OnReceiveEvent(const CommonEventData& /* data */) 43 { 44 StatusBarEventProxy::GetInstance()->OnStatusBarClick(); 45 } 46 StatusBarEventProxyOhos()47StatusBarEventProxyOhos::StatusBarEventProxyOhos() 48 { 49 MatchingSkills matchingSkills; 50 matchingSkills.AddEvent("usual.event.CLICK_STATUSBAR"); 51 CommonEventSubscribeInfo subscribeInfo(matchingSkills); 52 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::HANDLER); 53 54 eventFwkSubscriber_ = std::make_shared<StatusBarEventSubscriber>(subscribeInfo); 55 } 56 ~StatusBarEventProxyOhos()57StatusBarEventProxyOhos::~StatusBarEventProxyOhos() 58 { 59 CommonEventManager::UnSubscribeCommonEvent(eventFwkSubscriber_); 60 } 61 62 namespace { NotifyCard(const RefPtr<StatusBarClickListener> & listener)63void NotifyCard(const RefPtr<StatusBarClickListener>& listener) 64 { 65 auto taskExecutor = Container::CurrentTaskExecutor(); 66 CHECK_NULL_VOID(taskExecutor); 67 if (taskExecutor->WillRunOnCurrentThread(TaskExecutor::TaskType::UI)) { 68 listener->OnStatusBarClick(); 69 } else { 70 taskExecutor->PostTask( 71 [listener, id = Container::CurrentId()] { 72 ContainerScope scope(id); 73 listener->OnStatusBarClick(); 74 }, 75 TaskExecutor::TaskType::UI, "ArkUINotifyCardStatusBarClick"); 76 } 77 } 78 } // namespace 79 OnStatusBarClick()80void StatusBarEventProxyOhos::OnStatusBarClick() 81 { 82 for (auto it = listeners_.begin(); it != listeners_.end();) { 83 auto listener = it->first.Upgrade(); 84 if (listener) { 85 ContainerScope scope(it->second); 86 auto container = Container::Current(); 87 if (container && container->IsFRSCardContainer()) { 88 NotifyCard(listener); 89 } else { 90 listener->OnStatusBarClick(); 91 } 92 ++it; 93 } else { 94 it = listeners_.erase(it); 95 } 96 } 97 } 98 Register(const WeakPtr<StatusBarClickListener> & listener)99void StatusBarEventProxyOhos::Register(const WeakPtr<StatusBarClickListener>& listener) 100 { 101 if (listeners_.empty()) { 102 CommonEventManager::SubscribeCommonEvent(eventFwkSubscriber_); 103 } 104 listeners_.insert({ listener, Container::CurrentId() }); 105 } 106 UnRegister(const WeakPtr<StatusBarClickListener> & listener)107void StatusBarEventProxyOhos::UnRegister(const WeakPtr<StatusBarClickListener>& listener) 108 { 109 if (listeners_.empty()) { 110 return; 111 } 112 for (auto it = listeners_.begin(); it != listeners_.end(); ++it) { 113 if (it->first == listener) { 114 listeners_.erase(it); 115 break; 116 } 117 } 118 } 119 } // namespace OHOS::Ace 120