• 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 "customized_screen_event_rules.h"
17 #ifdef POWER_MANAGER_ENABLE_WATCH_CUSTOMIZED_SCREEN_COMMON_EVENT_RULES
18 #include "json/json.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 
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(), curApp.bundleName) == bundleNames.end()) {
39             bundleNames.push_back(curApp.bundleName);
40         }
41     }
42     return bundleNames;
43 }
44 
SetScreenOnEventRules(StateChangeReason reason)45 void CustomizedScreenEventRules::SetScreenOnEventRules(StateChangeReason reason)
46 {
47     defaultReason = reason;
48     PowerExtIntfWrapper::Instance().SetScreenOnEventRules(reason);
49 }
50 
NotifyOperateEventAfterScreenOn()51 void CustomizedScreenEventRules::NotifyOperateEventAfterScreenOn()
52 {
53     PowerExtIntfWrapper::Instance().NotifyOperateEventAfterScreenOn(bundleNames);
54 }
55 
PublishCustomizedScreenEvent(PowerState state)56 void CustomizedScreenEventRules::PublishCustomizedScreenEvent(PowerState state)
57 {
58     if (defaultReason == StateChangeReason::STATE_CHANGE_REASON_PICKUP) {
59         PowerExtIntfWrapper::Instance().PublishCustomizedScreenEvent(state, GetForegroundBundleNames());
60     } else {
61         PowerExtIntfWrapper::Instance().PublishCustomizedScreenEvent(state, bundleNames);
62     }
63 }
64 
NotifyScreenOnEventAgain(WakeupDeviceType reason)65 bool CustomizedScreenEventRules::NotifyScreenOnEventAgain(WakeupDeviceType reason)
66 {
67     return PowerExtIntfWrapper::Instance().NotifyScreenOnEventAgain(reason, bundleNames);
68 }
69 #endif
70 
SendCustomizedScreenEvent(std::shared_ptr<PowerMgrNotify> notify,PowerState state,int64_t callTime)71 void CustomizedScreenEventRules::SendCustomizedScreenEvent(
72     std::shared_ptr<PowerMgrNotify> notify, PowerState state, int64_t callTime)
73 {
74 #ifdef POWER_MANAGER_ENABLE_WATCH_CUSTOMIZED_SCREEN_COMMON_EVENT_RULES
75     if (state == PowerState::AWAKE) {
76         PublishCustomizedScreenEvent(PowerState::AWAKE);
77     } else if (state == PowerState::INACTIVE) {
78         PublishCustomizedScreenEvent(PowerState::INACTIVE);
79     }
80 #else
81     if (state == PowerState::AWAKE) {
82         notify->PublishScreenOnEvents(callTime);
83     } else if (state == PowerState::INACTIVE) {
84         notify->PublishScreenOffEvents(callTime);
85     }
86 #endif
87 }
88 
89 }  // namespace PowerMgr
90 }  // namespace OHOS