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     publishInfo_->SetOrdered(false);
36     screenOffWant_ = new (std::nothrow)IntentWant();
37     screenOffWant_->SetAction(CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
38     screenOnWant_ = new (std::nothrow)IntentWant();
39     screenOnWant_->SetAction(CommonEventSupport::COMMON_EVENT_SCREEN_ON);
40 #ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST
41     enterForceSleepWant_ = new (std::nothrow)IntentWant();
42     enterForceSleepWant_->SetAction(CommonEventSupport::COMMON_EVENT_ENTER_FORCE_SLEEP);
43     exitForceSleepWant_ = new (std::nothrow)IntentWant();
44     exitForceSleepWant_->SetAction(CommonEventSupport::COMMON_EVENT_EXIT_FORCE_SLEEP);
45 #endif
46 }
47 
PublishEvents(int64_t eventTime,sptr<IntentWant> want)48 void PowerMgrNotify::PublishEvents(int64_t eventTime, sptr<IntentWant> want)
49 {
50     if ((want == nullptr) || (publishInfo_ == nullptr)) {
51         POWER_HILOGE(COMP_SVC, "Invalid parameter");
52         return;
53     }
54     CommonEventData event(*want);
55     CommonEventManager::PublishCommonEvent(event, *publishInfo_, nullptr);
56 }
57 
PublishScreenOffEvents(int64_t eventTime)58 void PowerMgrNotify::PublishScreenOffEvents(int64_t eventTime)
59 {
60     POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
61         screenOffWant_->GetAction().c_str(), static_cast<long long>(eventTime));
62     PublishEvents(eventTime, screenOffWant_);
63     POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Publish event %{public}s done", screenOffWant_->GetAction().c_str());
64 }
65 
PublishScreenOnEvents(int64_t eventTime)66 void PowerMgrNotify::PublishScreenOnEvents(int64_t eventTime)
67 {
68     POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
69         screenOnWant_->GetAction().c_str(), static_cast<long long>(eventTime));
70     PublishEvents(eventTime, screenOnWant_);
71     POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Publish event %{public}s done", screenOnWant_->GetAction().c_str());
72 }
73 
74 #ifdef POWER_MANAGER_ENABLE_FORCE_SLEEP_BROADCAST
PublishEnterForceSleepEvents(int64_t eventTime)75 void PowerMgrNotify::PublishEnterForceSleepEvents(int64_t eventTime)
76 {
77     POWER_HILOGI(FEATURE_SUSPEND, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
78         enterForceSleepWant_->GetAction().c_str(), static_cast<long long>(eventTime));
79     PublishEvents(eventTime, enterForceSleepWant_);
80     POWER_HILOGI(
81         FEATURE_SUSPEND, "[UL_POWER] Publish event %{public}s done", enterForceSleepWant_->GetAction().c_str());
82 }
83 
PublishExitForceSleepEvents(int64_t eventTime)84 void PowerMgrNotify::PublishExitForceSleepEvents(int64_t eventTime)
85 {
86     POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Start to publish event %{public}s at %{public}lld",
87         exitForceSleepWant_->GetAction().c_str(), static_cast<long long>(eventTime));
88     PublishEvents(eventTime, exitForceSleepWant_);
89     POWER_HILOGI(FEATURE_WAKEUP, "[UL_POWER] Publish event %{public}s done", exitForceSleepWant_->GetAction().c_str());
90 }
91 #endif
92 } // namespace PowerMgr
93 } // namespace OHOS
94