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 "common_event_observer.h"
17
18 #include "devicestatus_define.h"
19
20 #undef LOG_TAG
21 #define LOG_TAG "CommonEventObserver"
22
23 namespace OHOS {
24 namespace Msdp {
25 namespace DeviceStatus {
26 namespace {
27 std::set<std::string> g_commonEvents = {
28 EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON,
29 EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF,
30 EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED,
31 EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED,
32 EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_LOW,
33 EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY
34 };
35 }
36
CreateCommonEventObserver(CommonEventHandleType handle)37 std::shared_ptr<CommonEventObserver> CommonEventObserver::CreateCommonEventObserver(CommonEventHandleType handle)
38 {
39 CALL_DEBUG_ENTER;
40 EventFwk::MatchingSkills skill;
41 for (const auto &action : g_commonEvents) {
42 skill.AddEvent(action);
43 }
44 return std::make_shared<CommonEventObserver>(EventFwk::CommonEventSubscribeInfo(skill), handle);
45 }
46
OnReceiveEvent(const EventFwk::CommonEventData & event)47 void CommonEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &event)
48 {
49 CHKPV(handle_);
50 const auto want = event.GetWant();
51 const auto action = want.GetAction();
52 if (g_commonEvents.find(action) == g_commonEvents.end()) {
53 FI_HILOGE("Unexpected action:%{public}s", action.c_str());
54 return;
55 }
56 handle_(action);
57 }
58 } // namespace DeviceStatus
59 } // namespace Msdp
60 } // namespace OHOS