1 /*
2 * Copyright (C) 2025 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 <thread>
17 #include "event_manager.h"
18 #include "ntp_update_time.h"
19 #include "time_tick_notify.h"
20 #include "timer_manager.h"
21
22 namespace OHOS {
23 namespace MiscServices {
24 using namespace OHOS::EventFwk;
25 using namespace OHOS::AAFwk;
26 static constexpr uint32_t CONNECTED_EVENT = 3;
27
EventManager(const OHOS::EventFwk::CommonEventSubscribeInfo & subscriberInfo)28 EventManager::EventManager(const OHOS::EventFwk::CommonEventSubscribeInfo &subscriberInfo)
29 : CommonEventSubscriber(subscriberInfo)
30 {
31 memberFuncMap_ = {
32 { CONNECTED,
33 [this] (const CommonEventData &data) { NetConnStateConnected(data); } },
34 { POWER_BROADCAST_EVENT,
35 [this] (const CommonEventData &data) { PowerBroadcast(data); } },
36 { NITZ_TIME_CHANGED_BROADCAST_EVENT,
37 [this] (const CommonEventData &data) { NITZTimeChangeBroadcast(data); } },
38 { PACKAGE_REMOVED_EVENT,
39 [this] (const CommonEventData &data) { PackageRemovedBroadcast(data); } },
40 };
41 }
42
OnReceiveEvent(const CommonEventData & data)43 void EventManager::OnReceiveEvent(const CommonEventData &data)
44 {
45 uint32_t code = UNKNOWN_BROADCAST_EVENT;
46 std::string action = data.GetWant().GetAction();
47 TIME_HILOGD(TIME_MODULE_SERVICE, "receive one broadcast:%{public}s", action.c_str());
48
49 if (action == CommonEventSupport::COMMON_EVENT_CONNECTIVITY_CHANGE && data.GetCode() == CONNECTED_EVENT) {
50 code = CONNECTED;
51 } else if (action == CommonEventSupport::COMMON_EVENT_SCREEN_ON) {
52 code = POWER_BROADCAST_EVENT;
53 } else if (action == CommonEventSupport::COMMON_EVENT_NITZ_TIME_CHANGED) {
54 code = NITZ_TIME_CHANGED_BROADCAST_EVENT;
55 } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED ||
56 action == CommonEventSupport::COMMON_EVENT_BUNDLE_REMOVED ||
57 action == CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED) {
58 code = PACKAGE_REMOVED_EVENT;
59 }
60
61 auto itFunc = memberFuncMap_.find(code);
62 if (itFunc != memberFuncMap_.end()) {
63 auto memberFunc = itFunc->second;
64 if (memberFunc != nullptr) {
65 return memberFunc(data);
66 }
67 }
68 }
69
NetConnStateConnected(const CommonEventData & data)70 void EventManager::NetConnStateConnected(const CommonEventData &data)
71 {
72 TIME_HILOGI(TIME_MODULE_SERVICE, "Internet ready");
73 if (NtpUpdateTime::IsInUpdateInterval()) {
74 NtpUpdateTime::SetSystemTime();
75 } else {
76 auto setSystemTime = [this]() { NtpUpdateTime::SetSystemTime(); };
77 std::thread thread(setSystemTime);
78 thread.detach();
79 }
80 }
81
PowerBroadcast(const CommonEventData & data)82 void EventManager::PowerBroadcast(const CommonEventData &data)
83 {
84 TimeTickNotify::GetInstance().Callback();
85 }
86
NITZTimeChangeBroadcast(const CommonEventData & data)87 void EventManager::NITZTimeChangeBroadcast(const CommonEventData &data)
88 {
89 TIME_HILOGD(TIME_MODULE_SERVICE, "NITZ Time changed broadcast code:%{public}d", data.GetCode());
90 NtpUpdateTime::GetInstance().UpdateNITZSetTime();
91 }
92
PackageRemovedBroadcast(const CommonEventData & data)93 void EventManager::PackageRemovedBroadcast(const CommonEventData &data)
94 {
95 auto uid = data.GetWant().GetIntParam(std::string("uid"), -1);
96 auto timerManager = TimerManager::GetInstance();
97 if (timerManager == nullptr) {
98 return;
99 }
100 timerManager->OnPackageRemoved(uid);
101 }
102 } // namespace MiscServices
103 } // namespace OHOS