• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "button_event.h"
17 #include "battery_log.h"
18 
19 namespace {
20 constexpr int32_t POWERUID = 5528;
21 }
22 namespace OHOS {
23 namespace PowerMgr {
AddEventHandle(const std::string & action,EventHandle func)24 void ButtonCes::AddEventHandle(const std::string& action, EventHandle func)
25 {
26     eventHandles_[action] = func;
27 }
28 
RegisterCesEvent()29 void ButtonCes::RegisterCesEvent()
30 {
31     if (isCesEventSubscribered_) {
32         return;
33     }
34     OHOS::EventFwk::MatchingSkills matchingSkills;
35     for (auto& itFunc : eventHandles_) {
36         matchingSkills.AddEvent(itFunc.first);
37     }
38     EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
39     subscriberInfo.SetPublisherUid(POWERUID);
40     cesEventSubscriber_ = std::make_shared<Ces>(subscriberInfo, eventHandles_);
41     if (cesEventSubscriber_ == nullptr) {
42         BATTERY_HILOGE(COMP_SVC, "cesEventSubscriber_ nullptr");
43         return;
44     }
45     if (!EventFwk::CommonEventManager::SubscribeCommonEvent(cesEventSubscriber_)) {
46         cesEventSubscriber_ = nullptr;
47         BATTERY_HILOGE(COMP_SVC, "SubscriberCommonEvent fail");
48     } else {
49         isCesEventSubscribered_ = true;
50     }
51 }
52 
UnRegisterCesEvent()53 void ButtonCes::UnRegisterCesEvent()
54 {
55     if (!isCesEventSubscribered_) {
56         return;
57     }
58     EventFwk::CommonEventManager::UnSubscribeCommonEvent(cesEventSubscriber_);
59     cesEventSubscriber_ = nullptr;
60     isCesEventSubscribered_ = false;
61 }
62 
Ces(const CommonEventSubscribeInfo & subscriberInfo,const std::unordered_map<std::string,EventHandle> & handles)63 ButtonCes::Ces::Ces(const CommonEventSubscribeInfo &subscriberInfo,
64     const std::unordered_map<std::string, EventHandle>& handles)
65     : CommonEventSubscriber(subscriberInfo), eventHandles_(handles)
66 {
67     BATTERY_HILOGI(COMP_SVC, "Button CesEventSubscriber");
68 }
69 
~Ces()70 ButtonCes::Ces::~Ces()
71 {
72     BATTERY_HILOGI(COMP_SVC, "Button ~CesEventSubscriber");
73 }
74 
OnReceiveEvent(const CommonEventData & eventData)75 void ButtonCes::Ces::OnReceiveEvent(const CommonEventData &eventData)
76 {
77     auto action = eventData.GetWant().GetAction();
78     auto it = eventHandles_.find(action);
79     if (it == eventHandles_.end()) {
80         BATTERY_HILOGI(COMP_SVC, "Ignore Event: %{public}s", action.c_str());
81         return;
82     }
83     BATTERY_HILOGD(COMP_SVC, "Handle Event: %{public}s", action.c_str());
84     auto func = it->second;
85     if (func) {
86         it->second(eventData);
87     }
88 }
89 }
90 }