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 
OnAllowListChanged(int32_t uid,const std::string & name,uint32_t allowType,bool added)50 void WorkStandbyStateChangeCallback::OnAllowListChanged(int32_t uid, const std::string& name,
51     uint32_t allowType, bool added)
52 {
53     if (allowType != DevStandbyMgr::AllowType::WORK_SCHEDULER) {
54         WS_HILOGE("Standby allow list changed, allowType is not WORK_SCHEDULER");
55         return;
56     }
57     WS_HILOGI("%{public}s apply allow, added %{public}d", name.c_str(), added);
58     auto dataManager = DelayedSingleton<DataManager>::GetInstance();
59     dataManager->OnDeviceStandyWhitelistChanged(name, added);
60     if (!dataManager->GetDeviceSleep()) {
61         WS_HILOGI("current device standby state is not sleep");
62         return;
63     }
64     auto policy = DelayedSingleton<WorkSchedulerService>::GetInstance()->GetWorkPolicyManager();
65     if (!policy) {
66         WS_HILOGE("Standby allow list changed callback error, WorkPolicyManager is nullptr");
67         return;
68     }
69     if (!policy->FindWork(uid)) {
70         WS_HILOGI("Standby allow list changed callback return, uid:%{public}d has no work", uid);
71         return;
72     }
73     workQueueManager_->OnConditionChanged(WorkCondition::Type::STANDBY,
74         std::make_shared<DetectorValue>(0, 0, dataManager->GetDeviceSleep(), std::string()));
75 }
76 }  // namespace WorkScheduler
77 }  // namespace OHOS
78 #endif