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 
16 #include "date_time_format_adapter_impl.h"
17 
18 #include <cstddef>
19 #include <string>
20 
21 #include "common_event_subscriber.h"
22 #include "matching_skills.h"
23 #include "nweb_log.h"
24 #include "time_service_client.h"
25 
26 namespace OHOS::NWeb {
GetTzId()27 std::string WebTimezoneInfoImpl::GetTzId()
28 {
29     return tzId_;
30 }
31 
GetIsValid()32 bool WebTimezoneInfoImpl::GetIsValid()
33 {
34     return isValid_;
35 }
36 
NWebTimeZoneEventSubscriber(EventFwk::CommonEventSubscribeInfo & in,std::shared_ptr<TimezoneEventCallbackAdapter> cb)37 NWebTimeZoneEventSubscriber::NWebTimeZoneEventSubscriber(
38     EventFwk::CommonEventSubscribeInfo& in, std::shared_ptr<TimezoneEventCallbackAdapter> cb)
39     : EventFwk::CommonEventSubscriber(in), eventCallback_(cb)
40 {}
41 
OnReceiveEvent(const EventFwk::CommonEventData & data)42 void NWebTimeZoneEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& data)
43 {
44     const std::string action = data.GetWant().GetAction();
45     WVLOG_I("receive timezone action: %{public}s", action.c_str());
46     if (action != EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED) {
47         return;
48     }
49     std::string ret = OHOS::MiscServices::TimeServiceClient::GetInstance()->GetTimeZone();
50     std::shared_ptr<WebTimezoneInfoImpl> timezoneinfo = std::make_shared<WebTimezoneInfoImpl>(ret, true);
51     if (eventCallback_ == nullptr) {
52         return;
53     }
54     eventCallback_->TimezoneChanged(timezoneinfo);
55 }
56 
RegTimezoneEvent(std::shared_ptr<TimezoneEventCallbackAdapter> eventCallback)57 void DateTimeFormatAdapterImpl::RegTimezoneEvent(std::shared_ptr<TimezoneEventCallbackAdapter> eventCallback)
58 {
59     WVLOG_I("Reg Timezone Event.");
60     cb_ = std::move(eventCallback);
61 }
62 
StartListen()63 bool DateTimeFormatAdapterImpl::StartListen()
64 {
65     WVLOG_I("start time_zone listen.");
66     EventFwk::MatchingSkills skill = EventFwk::MatchingSkills();
67     skill.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED);
68     EventFwk::CommonEventSubscribeInfo info(skill);
69     this->commonEventSubscriber_ = std::make_shared<NWebTimeZoneEventSubscriber>(info, this->cb_);
70     bool ret = EventFwk::CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber_);
71     if (ret == false) {
72         WVLOG_E("start time_zone listen fail.");
73     }
74     return ret;
75 }
76 
StopListen()77 void DateTimeFormatAdapterImpl::StopListen()
78 {
79     WVLOG_I("stop time_zone listen.");
80     if (this->commonEventSubscriber_ != nullptr) {
81         bool result = EventFwk::CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber_);
82         if (result) {
83             this->commonEventSubscriber_ = nullptr;
84         } else {
85             WVLOG_E("stop time_zone listen fail.");
86         }
87     }
88 }
89 
GetTimezone()90 std::string DateTimeFormatAdapterImpl::GetTimezone()
91 {
92     std::string ret = OHOS::MiscServices::TimeServiceClient::GetInstance()->GetTimeZone();
93     if (ret == "") {
94         WVLOG_E("GetTimezone failed, return NULL.");
95     }
96     return ret;
97 }
98 } // namespace OHOS::NWeb
99