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