• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef POWER_MANAGER_POWER_ENABLE_S4
57     enterHibernateWant_ = new (std::nothrow) IntentWant();
58     if (enterHibernateWant_ != nullptr) {
59         enterHibernateWant_->SetAction(CommonEventSupport::COMMON_EVENT_ENTER_HIBERNATE);
60     }
61     exitHibernateWant_ = new (std::nothrow) IntentWant();
62     if (exitHibernateWant_ != nullptr) {
63         exitHibernateWant_->SetAction(CommonEventSupport::COMMON_EVENT_EXIT_HIBERNATE);
64     }
65 #endif
66 }
67 
PublishEvents(int64_t eventTime,sptr<IntentWant> want)68 void PowerMgrNotify::PublishEvents(int64_t eventTime, sptr<IntentWant> want)
69 {
70     if ((want == nullptr) || (publishInfo_ == nullptr)) {
71         POWER_HILOGE(COMP_SVC, "Invalid parameter");
72         return;
73     }
74     CommonEventData event(*want);
75     CommonEventManager::PublishCommonEvent(event, *publishInfo_, nullptr);
76 }
77 
PublishScreenOffEvents(int64_t eventTime)78 void PowerMgrNotify::PublishScreenOffEvents(int64_t eventTime)
79 {
80     if (screenOffWant_ == nullptr) {
81         POWER_HILOGE(COMP_SVC, "%{public}s: Invalid parameter", __func__);
82         return;
83     }
84     POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
85         screenOffWant_->GetAction().c_str(), static_cast<long long>(eventTime));
86     PublishEvents(eventTime, screenOffWant_);
87     POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Publish event %{public}s done", screenOffWant_->GetAction().c_str());
88 }
89 
PublishScreenOnEvents(int64_t eventTime)90 void PowerMgrNotify::PublishScreenOnEvents(int64_t eventTime)
91 {
92     if (screenOnWant_ == nullptr) {
93         POWER_HILOGE(COMP_SVC, "%{public}s: Invalid parameter", __func__);
94         return;
95     }
96     POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
97         screenOnWant_->GetAction().c_str(), static_cast<long long>(eventTime));
98     PublishEvents(eventTime, screenOnWant_);
99     POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Publish event %{public}s done", screenOnWant_->GetAction().c_str());
100 }
101 
102 #ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST
PublishEnterForceSleepEvents(int64_t eventTime)103 void PowerMgrNotify::PublishEnterForceSleepEvents(int64_t eventTime)
104 {
105     if (enterForceSleepWant_ == nullptr) {
106         POWER_HILOGE(COMP_SVC, "%{public}s: Invalid parameter", __func__);
107         return;
108     }
109     POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
110         enterForceSleepWant_->GetAction().c_str(), static_cast<long long>(eventTime));
111     PublishEvents(eventTime, enterForceSleepWant_);
112     POWER_HILOGI(
113         FEATURE_SUSPEND, "[UL_POWER] Publish event %{public}s done", enterForceSleepWant_->GetAction().c_str());
114 }
115 
PublishExitForceSleepEvents(int64_t eventTime)116 void PowerMgrNotify::PublishExitForceSleepEvents(int64_t eventTime)
117 {
118     if (exitForceSleepWant_ == nullptr) {
119         POWER_HILOGE(COMP_SVC, "%{public}s: Invalid parameter", __func__);
120         return;
121     }
122     POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
123         exitForceSleepWant_->GetAction().c_str(), static_cast<long long>(eventTime));
124     PublishEvents(eventTime, exitForceSleepWant_);
125     POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Publish event %{public}s done", exitForceSleepWant_->GetAction().c_str());
126 }
127 #endif
128 
129 #ifdef POWER_MANAGER_POWER_ENABLE_S4
PublishEnterHibernateEvent(int64_t eventTime,bool clearMemory)130 void PowerMgrNotify::PublishEnterHibernateEvent(int64_t eventTime, bool clearMemory)
131 {
132     if (enterHibernateWant_ == nullptr) {
133         POWER_HILOGE(COMP_SVC, "%{public}s: Invalid parameter", __func__);
134         return;
135     }
136     POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
137         enterHibernateWant_->GetAction().c_str(), static_cast<long long>(eventTime));
138     enterHibernateWant_->SetParam("clearMemory", clearMemory);
139     PublishEvents(eventTime, enterHibernateWant_);
140     POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Publish event %{public}s done", enterHibernateWant_->GetAction().c_str());
141 }
142 
PublishExitHibernateEvent(int64_t eventTime,bool clearMemory)143 void PowerMgrNotify::PublishExitHibernateEvent(int64_t eventTime, bool clearMemory)
144 {
145     if (exitHibernateWant_ == nullptr) {
146         POWER_HILOGE(COMP_SVC, "%{public}s: Invalid parameter", __func__);
147         return;
148     }
149     POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
150         exitHibernateWant_->GetAction().c_str(), static_cast<long long>(eventTime));
151     exitHibernateWant_->SetParam("clearMemory", clearMemory);
152     PublishEvents(eventTime, exitHibernateWant_);
153     POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Publish event %{public}s done", exitHibernateWant_->GetAction().c_str());
154 }
155 #endif
156 } // namespace PowerMgr
157 } // namespace OHOS
158