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_data.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 
22 using namespace OHOS::AAFwk;
23 using namespace OHOS::EventFwk;
24 
25 namespace OHOS {
26 namespace MiscServices {
GetInstance()27 TimeServiceNotify &TimeServiceNotify::GetInstance()
28 {
29     static TimeServiceNotify instance;
30     return instance;
31 }
32 
RepublishEvents()33 bool TimeServiceNotify::RepublishEvents()
34 {
35     TIME_HILOGD(TIME_MODULE_SERVICE, "start to Republish events");
36     auto currentTime = std::chrono::steady_clock::now().time_since_epoch().count();
37     return PublishTimeChangeEvents(currentTime) && PublishTimeZoneChangeEvents(currentTime) &&
38         PublishTimeTickEvents(currentTime);
39 }
40 
PublishEvents(int64_t eventTime,const IntentWant & want,const PublishInfo & publishInfo)41 bool TimeServiceNotify::PublishEvents(int64_t eventTime, const IntentWant &want, const PublishInfo &publishInfo)
42 {
43     TIME_HILOGD(TIME_MODULE_SERVICE, "Start to publish event %{public}s at %{public}lld", want.GetAction().c_str(),
44         static_cast<long long>(eventTime));
45     CommonEventData event(want);
46     if (!CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr)) {
47         TIME_HILOGE(TIME_MODULE_SERVICE, "failed to Publish event %{public}s", want.GetAction().c_str());
48         return false;
49     }
50     TIME_HILOGI(TIME_MODULE_SERVICE, "Publish event %{public}s done", want.GetAction().c_str());
51     return true;
52 }
53 
PublishTimeChangeEvents(int64_t eventTime)54 bool TimeServiceNotify::PublishTimeChangeEvents(int64_t eventTime)
55 {
56     IntentWant timeChangeWant;
57     timeChangeWant.SetAction(CommonEventSupport::COMMON_EVENT_TIME_CHANGED);
58     return PublishEvents(eventTime, timeChangeWant, CommonEventPublishInfo());
59 }
60 
PublishTimeZoneChangeEvents(int64_t eventTime)61 bool TimeServiceNotify::PublishTimeZoneChangeEvents(int64_t eventTime)
62 {
63     IntentWant timeZoneChangeWant;
64     timeZoneChangeWant.SetAction(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED);
65     return PublishEvents(eventTime, timeZoneChangeWant, CommonEventPublishInfo());
66 }
67 
PublishTimeTickEvents(int64_t eventTime)68 bool TimeServiceNotify::PublishTimeTickEvents(int64_t eventTime)
69 {
70     IntentWant timeTickWant;
71     timeTickWant.SetAction(CommonEventSupport::COMMON_EVENT_TIME_TICK);
72     return PublishEvents(eventTime, timeTickWant, CommonEventPublishInfo());
73 }
74 } // namespace MiscServices
75 } // namespace OHOS