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 #include "policy/app_data_clear_listener.h"
17
18 #include "common_event_support.h"
19 #include "common_event_manager.h"
20 #include "matching_skills.h"
21 #include "want.h"
22 #include "work_sched_hilog.h"
23 #include "work_scheduler_service.h"
24
25 using namespace std;
26
27 namespace OHOS {
28 namespace WorkScheduler {
29 static const std::string UID_PARAM = "uid";
30
AppDataClearSubscriber(const CommonEventSubscribeInfo & subscribeInfo,AppDataClearListener & listener)31 AppDataClearSubscriber::AppDataClearSubscriber(const CommonEventSubscribeInfo &subscribeInfo,
32 AppDataClearListener &listener) : CommonEventSubscriber(subscribeInfo), listener_(listener) {}
33
OnReceiveEvent(const CommonEventData & data)34 void AppDataClearSubscriber::OnReceiveEvent(const CommonEventData &data)
35 {
36 const string action = data.GetWant().GetAction();
37 string bundle = data.GetWant().GetBundle();
38 int32_t uid = data.GetWant().GetIntParam(UID_PARAM, -1);
39 WS_HILOGI("OnReceiveEvent get action: %{public}s, bundleName: %{public}s , uid: %{public}d",
40 action.c_str(), bundle.c_str(), uid);
41 auto detectorVal = make_shared<DetectorValue>(uid, 0, 0, bundle);
42 if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED) {
43 listener_.OnPolicyChanged(PolicyType::APP_DATA_CLEAR, detectorVal);
44 } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
45 listener_.OnPolicyChanged(PolicyType::APP_REMOVED, detectorVal);
46 } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
47 listener_.OnPolicyChanged(PolicyType::APP_CHANGED, detectorVal);
48 } else if (action == CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) {
49 listener_.OnPolicyChanged(PolicyType::APP_ADDED, detectorVal);
50 } else if (action == CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
51 listener_.OnPolicyChanged(PolicyType::USER_SWITCHED, detectorVal);
52 }
53 }
54
CreateAppDataClearSubscriber(AppDataClearListener & listener)55 shared_ptr<CommonEventSubscriber> CreateAppDataClearSubscriber(AppDataClearListener &listener)
56 {
57 MatchingSkills skill = MatchingSkills();
58 skill.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_DATA_CLEARED);
59 skill.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
60 skill.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
61 skill.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
62 skill.AddEvent(CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
63 CommonEventSubscribeInfo info(skill);
64 return make_shared<AppDataClearSubscriber>(info, listener);
65 }
66
AppDataClearListener(std::shared_ptr<WorkPolicyManager> workPolicyManager)67 AppDataClearListener::AppDataClearListener(std::shared_ptr<WorkPolicyManager> workPolicyManager)
68 {
69 workPolicyManager_ = workPolicyManager;
70 }
71
~AppDataClearListener()72 AppDataClearListener::~AppDataClearListener()
73 {
74 this->Stop();
75 }
76
Start()77 bool AppDataClearListener::Start()
78 {
79 this->commonEventSubscriber = CreateAppDataClearSubscriber(*this);
80 return CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber);
81 }
82
Stop()83 bool AppDataClearListener::Stop()
84 {
85 if (this->commonEventSubscriber != nullptr) {
86 bool result = CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber);
87 if (result) {
88 this->commonEventSubscriber = nullptr;
89 }
90 return result;
91 }
92 return true;
93 }
94
OnPolicyChanged(PolicyType policyType,shared_ptr<DetectorValue> detectorVal)95 void AppDataClearListener::OnPolicyChanged(PolicyType policyType, shared_ptr<DetectorValue> detectorVal)
96 {
97 auto handler = DelayedSingleton<WorkSchedulerService>::GetInstance()->GetHandler();
98 if (handler) {
99 handler->PostTask([weak = weak_from_this(), policyType, detectorVal]() {
100 auto strong = weak.lock();
101 if (!strong) {
102 WS_HILOGE("AppDataClearListener::OnPolicyChanged strong is null");
103 return;
104 }
105 strong->workPolicyManager_->OnPolicyChanged(policyType, detectorVal);
106 });
107 }
108 }
109 } // namespace WorkScheduler
110 } // namespace OHOS