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 = { EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON,
28 EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF, EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED,
29 EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED, EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_LOW,
30 EventFwk::CommonEventSupport::COMMON_EVENT_BATTERY_OKAY };
31 }
32
CreateCommonEventObserver(CommonEventHandleType handle)33 std::shared_ptr<CommonEventObserver> CommonEventObserver::CreateCommonEventObserver(CommonEventHandleType handle)
34 {
35 CALL_DEBUG_ENTER;
36 EventFwk::MatchingSkills skill;
37 for (const auto &action : g_commonEvents) {
38 skill.AddEvent(action);
39 }
40 return std::make_shared<CommonEventObserver>(EventFwk::CommonEventSubscribeInfo(skill), handle);
41 }
42
OnReceiveEvent(const EventFwk::CommonEventData & event)43 void CommonEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &event)
44 {
45 CHKPV(handle_);
46 const auto want = event.GetWant();
47 const auto action = want.GetAction();
48 if (g_commonEvents.find(action) == g_commonEvents.end()) {
49 FI_HILOGE("Unexpected action:%{public}s", action.c_str());
50 return;
51 }
52 handle_(action);
53 }
54 } // namespace DeviceStatus
55 } // namespace Msdp
56 } // namespace OHOS