1 /*
2  * Copyright (c) 2022 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_USAGE_STATISTICS_ENABLE
17 #include "work_bundle_group_change_callback.h"
18 #include "work_sched_hilog.h"
19 #include "work_sched_data_manager.h"
20 #include "work_policy_manager.h"
21 #include "work_scheduler_service.h"
22 
23 namespace OHOS {
24 namespace WorkScheduler {
WorkBundleGroupChangeCallback(std::shared_ptr<WorkQueueManager> workQueueManager)25 WorkBundleGroupChangeCallback::WorkBundleGroupChangeCallback(std::shared_ptr<WorkQueueManager>
26     workQueueManager)
27 {
28     workQueueManager_ = workQueueManager;
29 }
30 
OnAppGroupChanged(const DeviceUsageStats::AppGroupCallbackInfo & appGroupCallbackInfo)31 void WorkBundleGroupChangeCallback::OnAppGroupChanged(
32     const DeviceUsageStats::AppGroupCallbackInfo &appGroupCallbackInfo)
33 {
34     int32_t newGroup = appGroupCallbackInfo.GetNewGroup();
35     int32_t oldGroup = appGroupCallbackInfo.GetOldGroup();
36     int32_t userId = appGroupCallbackInfo.GetUserId();
37     std::string bundleName = appGroupCallbackInfo.GetBundleName();
38     WS_HILOGI("Bundle group changed, from %{public}d to %{public}d with userId = %{public}d, bundleName = %{public}s",
39         oldGroup, newGroup, userId, bundleName.c_str());
40     DelayedSingleton<DataManager>::GetInstance()->AddGroup(bundleName, userId, newGroup);
41     auto policy = DelayedSingleton<WorkSchedulerService>::GetInstance()->GetWorkPolicyManager();
42     if (!policy) {
43         WS_HILOGE("OnAppGroupChanged callback error, WorkPolicyManager is nullptr");
44         return;
45     }
46     if (!policy->FindWork(userId, bundleName)) {
47         WS_HILOGE("OnAppGroupChanged no work found, bundleName = %{public}s", bundleName.c_str());
48         return;
49     }
50     if (newGroup < oldGroup) {
51         if (!workQueueManager_) {
52             WS_HILOGE("WorkBundleGroupChangeCallback::OnAppGroupChanged workQueueManger_ is nullptr");
53             return;
54         }
55         workQueueManager_->OnConditionChanged(WorkCondition::Type::GROUP,
56             std::make_shared<DetectorValue>(newGroup, userId, true, bundleName));
57     }
58 }
59 }  // namespace WorkScheduler
60 }  // namespace OHOS
61 #endif