1 /*
2  * Copyright (C) 2022 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 "nitz_subscriber.h"
17 
18 #include "ntp_update_time.h"
19 #include "time_common.h"
20 namespace OHOS {
21 namespace MiscServices {
22 using namespace OHOS::EventFwk;
23 using namespace OHOS::AAFwk;
NITZSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & subscriberInfo)24 NITZSubscriber::NITZSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo)
25     : CommonEventSubscriber(subscriberInfo)
26 {
27     memberFuncMap_ = {
28         { NITZ_TIME_CHANGED_BROADCAST_EVENT,
29             [this] (const CommonEventData &data) { NITZTimeChangeBroadcast(data); } },
30         { NITZ_TIMEZONE_CHANGED_BROADCAST_EVENT,
31             [this] (const CommonEventData &data) { NITZTimezoneChangeBroadcast(data); } },
32     };
33 }
34 
OnReceiveEvent(const CommonEventData & data)35 void NITZSubscriber::OnReceiveEvent(const CommonEventData &data)
36 {
37     uint32_t code = UNKNOWN_BROADCAST_EVENT;
38     OHOS::EventFwk::Want want = data.GetWant();
39     std::string action = data.GetWant().GetAction();
40     TIME_HILOGD(TIME_MODULE_SERVICE, "receive one broadcast:%{public}s", action.c_str());
41 
42     if (action == CommonEventSupport::COMMON_EVENT_NITZ_TIME_CHANGED) {
43         code = NITZ_TIME_CHANGED_BROADCAST_EVENT;
44     } else if (action == CommonEventSupport::COMMON_EVENT_NITZ_TIMEZONE_CHANGED) {
45         code = NITZ_TIMEZONE_CHANGED_BROADCAST_EVENT;
46     }
47 
48     auto itFunc = memberFuncMap_.find(code);
49     if (itFunc != memberFuncMap_.end()) {
50         auto memberFunc = itFunc->second;
51         if (memberFunc != nullptr) {
52             return memberFunc(data);
53         }
54     }
55 }
56 
NITZTimeChangeBroadcast(const CommonEventData & data)57 void NITZSubscriber::NITZTimeChangeBroadcast(const CommonEventData &data)
58 {
59     TIME_HILOGD(TIME_MODULE_SERVICE, "NITZ Time changed broadcast code:%{public}d", data.GetCode());
60     NtpUpdateTime::GetInstance().UpdateNITZSetTime();
61 }
62 
NITZTimezoneChangeBroadcast(const CommonEventData & data)63 void NITZSubscriber::NITZTimezoneChangeBroadcast(const CommonEventData &data)
64 {
65     TIME_HILOGD(TIME_MODULE_SERVICE, "NITZ Timezone changed broadcast code:%{public}d", data.GetCode());
66 }
67 } // namespace MiscServices
68 } // namespace OHOS