• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     TIME_HILOGI(TIME_MODULE_SERVICE, "Publish event %{public}s done", want.GetAction().c_str());
54     return true;
55 }
56 
PublishTimeChangeEvents(int64_t eventTime)57 bool TimeServiceNotify::PublishTimeChangeEvents(int64_t eventTime)
58 {
59     IntentWant timeChangeWant;
60     timeChangeWant.SetAction(CommonEventSupport::COMMON_EVENT_TIME_CHANGED);
61     return PublishEvents(eventTime, timeChangeWant, CommonEventPublishInfo());
62 }
63 
PublishTimeZoneChangeEvents(int64_t eventTime)64 bool TimeServiceNotify::PublishTimeZoneChangeEvents(int64_t eventTime)
65 {
66     IntentWant timeZoneChangeWant;
67     timeZoneChangeWant.SetAction(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED);
68     return PublishEvents(eventTime, timeZoneChangeWant, CommonEventPublishInfo());
69 }
70 
PublishTimeTickEvents(int64_t eventTime)71 bool TimeServiceNotify::PublishTimeTickEvents(int64_t eventTime)
72 {
73     IntentWant timeTickWant;
74     timeTickWant.SetAction(CommonEventSupport::COMMON_EVENT_TIME_TICK);
75     return PublishEvents(eventTime, timeTickWant, CommonEventPublishInfo());
76 }
77 
PublishTimerTriggerEvents()78 bool TimeServiceNotify::PublishTimerTriggerEvents()
79 {
80     IntentWant timeTickWant;
81     timeTickWant.SetAction(COMMON_EVENT_TIMER_TRIGGER);
82     auto currentTime = std::chrono::steady_clock::now().time_since_epoch().count();
83     PublishInfo publishInfo = CommonEventPublishInfo();
84     publishInfo.SetSubscriberUid({UID_RSS});
85     return PublishEvents(currentTime, timeTickWant, publishInfo);
86 }
87 } // namespace MiscServices
88 } // namespace OHOS