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 }
41
PublishEvents(int64_t eventTime,sptr<IntentWant> want)42 void PowerMgrNotify::PublishEvents(int64_t eventTime, sptr<IntentWant> want)
43 {
44 if ((want == nullptr) || (publishInfo_ == nullptr)) {
45 POWER_HILOGE(COMP_SVC, "Invalid parameter");
46 return;
47 }
48 CommonEventData event(*want);
49 CommonEventManager::PublishCommonEvent(event, *publishInfo_, nullptr);
50 }
51
PublishScreenOffEvents(int64_t eventTime)52 void PowerMgrNotify::PublishScreenOffEvents(int64_t eventTime)
53 {
54 POWER_HILOGI(FEATURE_SUSPEND, "Start to publish event %{public}s at %{public}lld",
55 screenOffWant_->GetAction().c_str(), static_cast<long long>(eventTime));
56 PublishEvents(eventTime, screenOffWant_);
57 POWER_HILOGI(FEATURE_SUSPEND, "Publish event %{public}s done", screenOffWant_->GetAction().c_str());
58 }
59
PublishScreenOnEvents(int64_t eventTime)60 void PowerMgrNotify::PublishScreenOnEvents(int64_t eventTime)
61 {
62 POWER_HILOGI(FEATURE_WAKEUP, "Start to publish event %{public}s at %{public}lld",
63 screenOnWant_->GetAction().c_str(), static_cast<long long>(eventTime));
64 PublishEvents(eventTime, screenOnWant_);
65 POWER_HILOGI(FEATURE_WAKEUP, "Publish event %{public}s done", screenOnWant_->GetAction().c_str());
66 }
67 } // namespace PowerMgr
68 } // namespace OHOS
69