1 /*
2 * Copyright (c) 2023-2025 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 #ifdef DEVICE_STANDBY_ENABLE
17 #include "work_standby_state_change_callback.h"
18 #include "allow_type.h"
19 #include "work_sched_hilog.h"
20 #include "work_scheduler_service.h"
21 #include "work_policy_manager.h"
22 #include "work_sched_data_manager.h"
23 #include "work_sched_hisysevent_report.h"
24
25 namespace OHOS {
26 namespace WorkScheduler {
WorkStandbyStateChangeCallback(std::shared_ptr<WorkQueueManager> workQueueManager)27 WorkStandbyStateChangeCallback::WorkStandbyStateChangeCallback(std::shared_ptr<WorkQueueManager>
28 workQueueManager)
29 {
30 workQueueManager_ = workQueueManager;
31 }
32
OnDeviceIdleMode(bool napped,bool sleeping)33 void WorkStandbyStateChangeCallback::OnDeviceIdleMode(bool napped, bool sleeping)
34 {
35 WS_HILOGI("napped is %{public}d, sleeping is %{public}d", napped, sleeping);
36 if (napped && !sleeping) {
37 WS_HILOGI("Device standby state is nap, do not need process");
38 return;
39 }
40 if (!napped && sleeping) {
41 WS_HILOGI("Device standby state is sleeping");
42 } else {
43 // (1, 0) or (0, 0)
44 WS_HILOGI("Device standby exit sleeping state");
45 }
46 auto dataManager = DelayedSingleton<DataManager>::GetInstance();
47 if (dataManager->GetDeviceSleep() == sleeping) {
48 WS_HILOGI("Device standby state is not changed");
49 return;
50 }
51 WorkSchedUtil::HiSysEventStateChanged({"DEVICE_STANDBY", sleeping});
52 dataManager->SetDeviceSleep(sleeping);
53 workQueueManager_->OnConditionChanged(WorkCondition::Type::STANDBY,
54 std::make_shared<DetectorValue>(0, 0, sleeping, std::string()));
55 }
56
OnRestrictListChanged(int32_t uid,const std::string & name,uint32_t resourceType,bool added)57 void WorkStandbyStateChangeCallback::OnRestrictListChanged(int32_t uid, const std::string& name,
58 uint32_t resourceType, bool added)
59 {
60 if (resourceType != DevStandbyMgr::AllowType::WORK_SCHEDULER) {
61 WS_HILOGE("Standby restrict list changed, restrictType is not WORK_SCHEDULER");
62 return;
63 }
64 WS_HILOGD("%{public}s apply restrict, added %{public}d", name.c_str(), added);
65 auto dataManager = DelayedSingleton<DataManager>::GetInstance();
66 dataManager->OnDeviceStandyRestrictlistChanged(name, added);
67 auto policy = DelayedSingleton<WorkSchedulerService>::GetInstance()->GetWorkPolicyManager();
68 if (!policy) {
69 return;
70 }
71 if (!policy->FindWork(uid)) {
72 return;
73 }
74 workQueueManager_->OnConditionChanged(WorkCondition::Type::STANDBY,
75 std::make_shared<DetectorValue>(0, 0, dataManager->GetDeviceSleep(), std::string()));
76 }
77
OnAllowListChanged(int32_t uid,const std::string & name,uint32_t allowType,bool added)78 void WorkStandbyStateChangeCallback::OnAllowListChanged(int32_t uid, const std::string& name,
79 uint32_t allowType, bool added)
80 {
81 if (allowType != DevStandbyMgr::AllowType::WORK_SCHEDULER) {
82 WS_HILOGE("Standby allow list changed, allowType is not WORK_SCHEDULER");
83 return;
84 }
85 WS_HILOGI("%{public}s apply allow, added %{public}d", name.c_str(), added);
86 auto dataManager = DelayedSingleton<DataManager>::GetInstance();
87 dataManager->OnDeviceStandyWhitelistChanged(name, added);
88 if (!dataManager->GetDeviceSleep()) {
89 WS_HILOGI("current device standby state is not sleep");
90 return;
91 }
92 auto policy = DelayedSingleton<WorkSchedulerService>::GetInstance()->GetWorkPolicyManager();
93 if (!policy) {
94 WS_HILOGE("Standby allow list changed callback error, WorkPolicyManager is nullptr");
95 return;
96 }
97 if (!policy->FindWork(uid)) {
98 WS_HILOGI("Standby allow list changed callback return, uid:%{public}d has no work", uid);
99 return;
100 }
101 workQueueManager_->OnConditionChanged(WorkCondition::Type::STANDBY,
102 std::make_shared<DetectorValue>(0, 0, dataManager->GetDeviceSleep(), std::string()));
103 }
104 } // namespace WorkScheduler
105 } // namespace OHOS
106 #endif