1 /*
2 * Copyright (c) 2021-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 "power_mgr_notify.h"
17
18 #include <common_event_data.h>
19 #include <common_event_manager.h>
20 #include <common_event_support.h>
21
22 #include "power_log.h"
23
24 using namespace OHOS::AAFwk;
25 using namespace OHOS::EventFwk;
26
27 namespace OHOS {
28 namespace PowerMgr {
RegisterPublishEvents()29 void PowerMgrNotify::RegisterPublishEvents()
30 {
31 if (publishInfo_ != nullptr) {
32 return;
33 }
34 publishInfo_ = new (std::nothrow)CommonEventPublishInfo();
35 if (publishInfo_ != nullptr) {
36 publishInfo_->SetOrdered(false);
37 }
38 screenOffWant_ = new (std::nothrow)IntentWant();
39 if (screenOffWant_ != nullptr) {
40 screenOffWant_->SetAction(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
41 }
42 screenOnWant_ = new (std::nothrow)IntentWant();
43 if (screenOnWant_ != nullptr) {
44 screenOnWant_->SetAction(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
45 }
46 #ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST
47 enterForceSleepWant_ = new (std::nothrow)IntentWant();
48 if (enterForceSleepWant_ != nullptr) {
49 enterForceSleepWant_->SetAction(CommonEventSupport::COMMON_EVENT_ENTER_FORCE_SLEEP);
50 }
51 exitForceSleepWant_ = new (std::nothrow)IntentWant();
52 if (exitForceSleepWant_ != nullptr) {
53 exitForceSleepWant_->SetAction(CommonEventSupport::COMMON_EVENT_EXIT_FORCE_SLEEP);
54 }
55 #endif
56 }
57
PublishEvents(int64_t eventTime,sptr<IntentWant> want)58 void PowerMgrNotify::PublishEvents(int64_t eventTime, sptr<IntentWant> want)
59 {
60 if ((want == nullptr) || (publishInfo_ == nullptr)) {
61 POWER_HILOGE(COMP_SVC, "Invalid parameter");
62 return;
63 }
64 CommonEventData event(*want);
65 CommonEventManager::PublishCommonEvent(event, *publishInfo_, nullptr);
66 }
67
PublishScreenOffEvents(int64_t eventTime)68 void PowerMgrNotify::PublishScreenOffEvents(int64_t eventTime)
69 {
70 if (screenOffWant_ == nullptr) {
71 POWER_HILOGE(COMP_SVC, "%{public}s: Invalid parameter", __func__);
72 return;
73 }
74 POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
75 screenOffWant_->GetAction().c_str(), static_cast<long long>(eventTime));
76 PublishEvents(eventTime, screenOffWant_);
77 POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Publish event %{public}s done", screenOffWant_->GetAction().c_str());
78 }
79
PublishScreenOnEvents(int64_t eventTime)80 void PowerMgrNotify::PublishScreenOnEvents(int64_t eventTime)
81 {
82 if (screenOnWant_ == nullptr) {
83 POWER_HILOGE(COMP_SVC, "%{public}s: Invalid parameter", __func__);
84 return;
85 }
86 POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
87 screenOnWant_->GetAction().c_str(), static_cast<long long>(eventTime));
88 PublishEvents(eventTime, screenOnWant_);
89 POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Publish event %{public}s done", screenOnWant_->GetAction().c_str());
90 }
91
92 #ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST
PublishEnterForceSleepEvents(int64_t eventTime)93 void PowerMgrNotify::PublishEnterForceSleepEvents(int64_t eventTime)
94 {
95 if (enterForceSleepWant_ == nullptr) {
96 POWER_HILOGE(COMP_SVC, "%{public}s: Invalid parameter", __func__);
97 return;
98 }
99 POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
100 enterForceSleepWant_->GetAction().c_str(), static_cast<long long>(eventTime));
101 PublishEvents(eventTime, enterForceSleepWant_);
102 POWER_HILOGI(
103 FEATURE_SUSPEND, "[UL_POWER] Publish event %{public}s done", enterForceSleepWant_->GetAction().c_str());
104 }
105
PublishExitForceSleepEvents(int64_t eventTime)106 void PowerMgrNotify::PublishExitForceSleepEvents(int64_t eventTime)
107 {
108 if (exitForceSleepWant_ == nullptr) {
109 POWER_HILOGE(COMP_SVC, "%{public}s: Invalid parameter", __func__);
110 return;
111 }
112 POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
113 exitForceSleepWant_->GetAction().c_str(), static_cast<long long>(eventTime));
114 PublishEvents(eventTime, exitForceSleepWant_);
115 POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Publish event %{public}s done", exitForceSleepWant_->GetAction().c_str());
116 }
117 #endif
118
119 #ifdef POWER_MANAGER_POWER_ENABLE_S4
PublishEnterHibernateEvent(int64_t eventTime)120 void PowerMgrNotify::PublishEnterHibernateEvent(int64_t eventTime)
121 {
122 std::string action = CommonEventSupport::COMMON_EVENT_ENTER_HIBERNATE;
123 POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Start to publish event %{public}s at %{public}lld", action.c_str(),
124 static_cast<long long>(eventTime));
125 CommonEventPublishInfo publishInfo;
126 publishInfo.SetOrdered(false);
127 IntentWant enterHibernateWant;
128 enterHibernateWant.SetAction(action);
129 CommonEventData event(enterHibernateWant);
130 if (!CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr)) {
131 POWER_HILOGE(FEATURE_WAKEUP, "[UL_POWER] Publish event %{public}s fail", action.c_str());
132 return;
133 }
134 POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Publish event %{public}s done", action.c_str());
135 }
136
PublishExitHibernateEvent(int64_t eventTime)137 void PowerMgrNotify::PublishExitHibernateEvent(int64_t eventTime)
138 {
139 std::string action = CommonEventSupport::COMMON_EVENT_EXIT_HIBERNATE;
140 POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Start to publish event %{public}s at %{public}lld", action.c_str(),
141 static_cast<long long>(eventTime));
142 CommonEventPublishInfo publishInfo;
143 publishInfo.SetOrdered(false);
144 IntentWant exitHibernateWant;
145 exitHibernateWant.SetAction(action);
146 CommonEventData event(exitHibernateWant);
147 if (!CommonEventManager::PublishCommonEvent(event, publishInfo, nullptr)) {
148 POWER_HILOGE(FEATURE_WAKEUP, "[UL_POWER] Publish event %{public}s fail", action.c_str());
149 return;
150 }
151 POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Publish event %{public}s done", action.c_str());
152 }
153 #endif
154 } // namespace PowerMgr
155 } // namespace OHOS
156