1 /* 2 * Copyright (c) 2023 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/time/time_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<TimeEventProxy> TimeEventProxy::instance_; 24 std::mutex TimeEventProxy::mutex_; 25 GetInstance()26TimeEventProxy* TimeEventProxy::GetInstance() 27 { 28 if (!instance_) { 29 std::scoped_lock lock(mutex_); 30 if (!instance_) { 31 instance_ = std::make_unique<TimeEventProxyOhos>(); 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 TimeEventSubscriber::OnReceiveEvent(const CommonEventData& /* data */) 43 { 44 TimeEventProxy::GetInstance()->OnTimeChange(); 45 } 46 TimeEventProxyOhos()47TimeEventProxyOhos::TimeEventProxyOhos() 48 { 49 MatchingSkills matchingSkills; 50 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED); 51 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); 52 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SCREEN_ON); 53 auto container = Container::Current(); 54 if (container && container->IsFRSCardContainer()) { 55 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_TICK); 56 } 57 58 CommonEventSubscribeInfo subscribeInfo(matchingSkills); 59 subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::HANDLER); 60 61 eventFwkSubscriber_ = std::make_shared<TimeEventSubscriber>(subscribeInfo); 62 } 63 ~TimeEventProxyOhos()64TimeEventProxyOhos::~TimeEventProxyOhos() 65 { 66 CommonEventManager::UnSubscribeCommonEvent(eventFwkSubscriber_); 67 } 68 69 namespace { NotifyCard(const RefPtr<TimeChangeListener> & listener)70void NotifyCard(const RefPtr<TimeChangeListener>& listener) 71 { 72 auto taskExecutor = Container::CurrentTaskExecutor(); 73 CHECK_NULL_VOID(taskExecutor); 74 if (taskExecutor->WillRunOnCurrentThread(TaskExecutor::TaskType::UI)) { 75 listener->OnTimeChange(); 76 } else { 77 taskExecutor->PostTask( 78 [listener, id = Container::CurrentId()] { 79 ContainerScope scope(id); 80 listener->OnTimeChange(); 81 }, 82 TaskExecutor::TaskType::UI, "ArkUINotifyCardTimeChange"); 83 } 84 } 85 } // namespace 86 OnTimeChange()87void TimeEventProxyOhos::OnTimeChange() 88 { 89 for (auto it = listeners_.begin(); it != listeners_.end();) { 90 auto listener = it->first.Upgrade(); 91 if (listener) { 92 ContainerScope scope(it->second); 93 auto container = Container::Current(); 94 if (container && container->IsFRSCardContainer()) { 95 NotifyCard(listener); 96 } else { 97 listener->OnTimeChange(); 98 } 99 ++it; 100 } else { 101 it = listeners_.erase(it); 102 } 103 } 104 } 105 Register(const WeakPtr<TimeChangeListener> & listener)106void TimeEventProxyOhos::Register(const WeakPtr<TimeChangeListener>& listener) 107 { 108 if (listeners_.empty()) { 109 CommonEventManager::SubscribeCommonEvent(eventFwkSubscriber_); 110 } 111 listeners_.insert({ listener, Container::CurrentId() }); 112 } 113 } // namespace OHOS::Ace 114