1 /*
2 * Copyright (C) 2021 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 "time_service_notify.h"
17
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20
21 using namespace OHOS::AAFwk;
22 using namespace OHOS::EventFwk;
23
24 namespace OHOS {
25 namespace MiscServices {
26 namespace {
27 const std::string COMMON_EVENT_TIMER_TRIGGER = "common.event.TIMER_TRIGGER";
28 const int UID_RSS = 1096;
29 }
GetInstance()30 TimeServiceNotify &TimeServiceNotify::GetInstance()
31 {
32 static TimeServiceNotify instance;
33 return instance;
34 }
35
RepublishEvents()36 bool TimeServiceNotify::RepublishEvents()
37 {
38 TIME_HILOGD(TIME_MODULE_SERVICE, "start to Republish events");
39 auto currentTime = std::chrono::steady_clock::now().time_since_epoch().count();
40 return PublishTimeChangeEvents(currentTime) && PublishTimeZoneChangeEvents(currentTime) &&
41 PublishTimeTickEvents(currentTime);
42 }
43
PublishEvents(int64_t eventTime,const IntentWant & want,const PublishInfo & publishInfo)44 bool TimeServiceNotify::PublishEvents(int64_t eventTime, const IntentWant &want, const PublishInfo &publishInfo)
45 {
46 TIME_HILOGD(TIME_MODULE_SERVICE, "Start to publish event %{public}s at %{public}lld", want.GetAction().c_str(),
47 static_cast<long long>(eventTime));
48 CommonEventData event(want);
49 if (!CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr)) {
50 TIME_HILOGE(TIME_MODULE_SERVICE, "failed to Publish event %{public}s", want.GetAction().c_str());
51 return false;
52 }
53 if (want.GetAction() == CommonEventSupport::COMMON_EVENT_TIME_TICK) {
54 TIME_SIMPLIFY_HILOGI(TIME_MODULE_SERVICE, "Publish TICK");
55 } else if (want.GetAction() != COMMON_EVENT_TIMER_TRIGGER) {
56 TIME_SIMPLIFY_HILOGI(TIME_MODULE_SERVICE, "Publish %{public}s ", want.GetAction().c_str());
57 }
58 return true;
59 }
60
PublishTimeChangeEvents(int64_t eventTime)61 bool TimeServiceNotify::PublishTimeChangeEvents(int64_t eventTime)
62 {
63 IntentWant timeChangeWant;
64 timeChangeWant.SetAction(CommonEventSupport::COMMON_EVENT_TIME_CHANGED);
65 return PublishEvents(eventTime, timeChangeWant, CommonEventPublishInfo());
66 }
67
PublishTimeZoneChangeEvents(int64_t eventTime)68 bool TimeServiceNotify::PublishTimeZoneChangeEvents(int64_t eventTime)
69 {
70 IntentWant timeZoneChangeWant;
71 timeZoneChangeWant.SetAction(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED);
72 return PublishEvents(eventTime, timeZoneChangeWant, CommonEventPublishInfo());
73 }
74
PublishTimeTickEvents(int64_t eventTime)75 bool TimeServiceNotify::PublishTimeTickEvents(int64_t eventTime)
76 {
77 IntentWant timeTickWant;
78 timeTickWant.SetAction(CommonEventSupport::COMMON_EVENT_TIME_TICK);
79 return PublishEvents(eventTime, timeTickWant, CommonEventPublishInfo());
80 }
81
PublishTimerTriggerEvents()82 bool TimeServiceNotify::PublishTimerTriggerEvents()
83 {
84 IntentWant timeTickWant;
85 timeTickWant.SetAction(COMMON_EVENT_TIMER_TRIGGER);
86 auto currentTime = std::chrono::steady_clock::now().time_since_epoch().count();
87 PublishInfo publishInfo = CommonEventPublishInfo();
88 publishInfo.SetSubscriberUid({UID_RSS});
89 return PublishEvents(currentTime, timeTickWant, publishInfo);
90 }
91 } // namespace MiscServices
92 } // namespace OHOS