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 "customized_screen_event_rules.h"
17 #ifdef POWER_MANAGER_ENABLE_WATCH_CUSTOMIZED_SCREEN_COMMON_EVENT_RULES
18 #include <cJSON.h>
19 #include "power_ext_intf_wrapper.h"
20 #endif
21
22 namespace OHOS {
23 namespace PowerMgr {
24 #ifdef POWER_MANAGER_ENABLE_WATCH_CUSTOMIZED_SCREEN_COMMON_EVENT_RULES
25 static std::vector<std::string> bundleNames{};
26 static StateChangeReason defaultReason = StateChangeReason::STATE_CHANGE_REASON_POWER_KEY;
27 static std::vector<StateChangeReason> g_watchCurrentStateChangeReason;
28
GetForegroundBundleNames()29 std::vector<std::string> CustomizedScreenEventRules::GetForegroundBundleNames()
30 {
31 if (!bundleNames.empty()) {
32 bundleNames.clear();
33 bundleNames.shrink_to_fit();
34 }
35 std::vector<OHOS::AppExecFwk::AppStateData> appList;
36 AppManagerUtils::GetForegroundApplications(appList);
37 for (const auto &curApp : appList) {
38 if (std::find(bundleNames.begin(), bundleNames.end(), cuiApp.bundleName) == bundleNames.end()) {
39 bundleNames.push_back(curApp.bundleName);
40 }
41 }
42 return bundleNames;
43 }
44
SetScreenOnEventRules(StateChangeReason reason,const std::vector<StateChangeReason> & stateChangeReason,const std::vector<WakeupDeviceType> & wakeupDeviceTypes)45 void CustomizedScreenEventRules::SetScreenOnEventRules(StateChangeReason reason,
46 const std::vector<StateChangeReason>& stateChangeReason, const std::vector<WakeupDeviceType>& wakeupDeviceTypes)
47 {
48 defaultReason = reason;
49 g_watchCurrentStateChangeReason = stateChangeReason;
50 PowerExtIntfWrapper::Instance().SetScreenOnEventRules(reason, stateChangeReason, wakeupDeviceTypes);
51 }
52
PublishCustomizedScreenEvent(PowerState state)53 void CustomizedScreenEventRules::PublishCustomizedScreenEvent(PowerState state)
54 {
55 if (!g_watchCurrentStateChangeReason.empty() &&
56 std::find(g_watchCurrentStateChangeReason.begin(), g_watchCurrentStateChangeReason.end(), defaultReason) !=
57 g_watchCurrentStateChangeReason.end()) {
58 PowerExtIntfWrapper::Instance().PublishCustomizedScreenEvent(state, GetForegroundBundleNames());
59 } else {
60 PowerExtIntfWrapper::Instance().PublishCustomizedScreenEvent(state, bundleNames);
61 }
62 }
63
NotifyScreenOnEventAgain(WakeupDeviceType reason)64 bool CustomizedScreenEventRules::NotifyScreenOnEventAgain(WakeupDeviceType reason)
65 {
66 return PowerExtIntfWrapper::Instance().NotifyScreenOnEventAgain(reason, bundleNames);
67 }
68
NotifyOperateEventAfterScreenOn()69 void CustomizedScreenEventRules::NotifyOperateEventAfterScreenOn()
70 {
71 PowerExtIntfWrapper::Instance().NotifyOperateEventAfterScreenOn(bundleNames);
72 }
73 #endif
74
SendCustomizedScreenEvent(std::shared_ptr<PowerMgrNotify> notify,PowerState state,int64_t callTime)75 void CustomizedScreenEventRules::SendCustomizedScreenEvent(
76 std::shared_ptr<PowerMgrNotify> notify, PowerState state, int64_t callTime)
77 {
78 #ifdef POWER_MANAGER_ENABLE_WATCH_CUSTOMIZED_SCREEN_COMMON_EVENT_RULES
79 if (state == PowerState::AWAKE) {
80 PublishCustomizedScreenEvent(PowerState::AWAKE);
81 } else if (state == PowerState::INACTIVE) {
82 PublishCustomizedScreenEvent(PowerState::INACTIVE);
83 }
84 #else
85 if (state == PowerState::AWAKE) {
86 notify->PublishScreenOnEvents(callTime);
87 } else if (state == PowerState::INACTIVE) {
88 notify->PublishScreenOffEvents(callTime);
89 }
90 #endif
91 }
92
93 } //namespace PowerMgr
94 } //namespace OHOS