• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "frameworks/core/common/container_scope.h"
22 
23 namespace OHOS::Ace {
24 std::unique_ptr<TimeEventProxy> TimeEventProxy::instance_;
25 std::mutex TimeEventProxy::mutex_;
26 
GetInstance()27 TimeEventProxy* TimeEventProxy::GetInstance()
28 {
29     if (!instance_) {
30         std::scoped_lock lock(mutex_);
31         if (!instance_) {
32             instance_ = std::make_unique<TimeEventProxyOhos>();
33         }
34     }
35     return instance_.get();
36 }
37 
38 using OHOS::EventFwk::CommonEventManager;
39 using OHOS::EventFwk::CommonEventSubscribeInfo;
40 using OHOS::EventFwk::CommonEventSupport;
41 using OHOS::EventFwk::MatchingSkills;
42 
OnReceiveEvent(const CommonEventData &)43 void TimeEventSubscriber::OnReceiveEvent(const CommonEventData& /* data */)
44 {
45     LOGI("Time Change Common Event Received");
46     TimeEventProxy::GetInstance()->OnTimeChange();
47 }
48 
TimeEventProxyOhos()49 TimeEventProxyOhos::TimeEventProxyOhos()
50 {
51     MatchingSkills matchingSkills;
52     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIME_CHANGED);
53     matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED);
54 
55     CommonEventSubscribeInfo subscribeInfo(matchingSkills);
56     subscribeInfo.SetThreadMode(CommonEventSubscribeInfo::ThreadMode::HANDLER);
57 
58     eventFwkSubscriber_ = std::make_shared<TimeEventSubscriber>(subscribeInfo);
59 }
60 
~TimeEventProxyOhos()61 TimeEventProxyOhos::~TimeEventProxyOhos()
62 {
63     CommonEventManager::UnSubscribeCommonEvent(eventFwkSubscriber_);
64 }
65 
66 namespace {
NotifyCard(const RefPtr<TimeChangeListener> & listener)67 void NotifyCard(const RefPtr<TimeChangeListener>& listener)
68 {
69     auto taskExecutor = Container::CurrentTaskExecutor();
70     CHECK_NULL_VOID(taskExecutor);
71     if (taskExecutor->WillRunOnCurrentThread(TaskExecutor::TaskType::UI)) {
72         listener->OnTimeChange();
73     } else {
74         taskExecutor->PostTask(
75             [listener, id = Container::CurrentId()] {
76                 ContainerScope scope(id);
77                 listener->OnTimeChange();
78             },
79             TaskExecutor::TaskType::UI);
80     }
81 }
82 } // namespace
83 
OnTimeChange()84 void TimeEventProxyOhos::OnTimeChange()
85 {
86     for (auto it = listeners_.begin(); it != listeners_.end();) {
87         auto listener = it->first.Upgrade();
88         if (listener) {
89             ContainerScope scope(it->second);
90             auto container = Container::Current();
91             if (container && container->IsFRSCardContainer()) {
92                 NotifyCard(listener);
93             } else {
94                 listener->OnTimeChange();
95             }
96             ++it;
97         } else {
98             it = listeners_.erase(it);
99         }
100     }
101 }
102 
Register(const WeakPtr<TimeChangeListener> & listener)103 void TimeEventProxyOhos::Register(const WeakPtr<TimeChangeListener>& listener)
104 {
105     if (listeners_.empty()) {
106         CommonEventManager::SubscribeCommonEvent(eventFwkSubscriber_);
107     }
108     listeners_.insert({ listener, Container::CurrentId() });
109 }
110 } // namespace OHOS::Ace
111