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_removed_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
24 using namespace std;
25
26 namespace OHOS {
27 namespace WorkScheduler {
28 static const std::string UID_PARAM = "uid";
AppRemovedSubscriber(const CommonEventSubscribeInfo & subscribeInfo,AppRemovedListener & listener)29 AppRemovedSubscriber::AppRemovedSubscriber(const CommonEventSubscribeInfo &subscribeInfo, AppRemovedListener &listener)
30 : CommonEventSubscriber(subscribeInfo), listener_(listener) {}
31
OnReceiveEvent(const CommonEventData & data)32 void AppRemovedSubscriber::OnReceiveEvent(const CommonEventData &data)
33 {
34 const string action = data.GetWant().GetAction();
35 WS_HILOGI("OnReceiveEvent get action: %{public}s", action.c_str());
36 if ((action == CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED)
37 || (action == CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED)) {
38 string bundle = data.GetWant().GetBundle();
39 int32_t uid = data.GetWant().GetIntParam(UID_PARAM, -1);
40 WS_HILOGI("bundleName: %{public}s , uid: %{public}d", bundle.c_str(), uid);
41 auto detectorVal = make_shared<DetectorValue>(uid, 0, 0, bundle);
42 listener_.OnPolicyChanged(PolicyType::APP_REMOVED, detectorVal);
43 }
44 }
45
CreateAppRemovedSubscriber(AppRemovedListener & listener)46 shared_ptr<CommonEventSubscriber> CreateAppRemovedSubscriber(AppRemovedListener &listener)
47 {
48 MatchingSkills skill = MatchingSkills();
49 skill.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
50 skill.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
51 CommonEventSubscribeInfo info(skill);
52 return make_shared<AppRemovedSubscriber>(info, listener);
53 }
54
AppRemovedListener(std::shared_ptr<WorkPolicyManager> workPolicyManager)55 AppRemovedListener::AppRemovedListener(std::shared_ptr<WorkPolicyManager> workPolicyManager)
56 {
57 workPolicyManager_ = workPolicyManager;
58 }
59
~AppRemovedListener()60 AppRemovedListener::~AppRemovedListener()
61 {
62 this->Stop();
63 }
64
Start()65 bool AppRemovedListener::Start()
66 {
67 this->commonEventSubscriber = CreateAppRemovedSubscriber(*this);
68 return CommonEventManager::SubscribeCommonEvent(this->commonEventSubscriber);
69 }
70
Stop()71 bool AppRemovedListener::Stop()
72 {
73 if (this->commonEventSubscriber != nullptr) {
74 bool result = CommonEventManager::UnSubscribeCommonEvent(this->commonEventSubscriber);
75 if (result) {
76 this->commonEventSubscriber = nullptr;
77 }
78 return result;
79 }
80 return true;
81 }
82
OnPolicyChanged(PolicyType policyType,shared_ptr<DetectorValue> detectorVal)83 void AppRemovedListener::OnPolicyChanged(PolicyType policyType, shared_ptr<DetectorValue> detectorVal)
84 {
85 workPolicyManager_->OnPolicyChanged(policyType, detectorVal);
86 }
87 } // namespace WorkScheduler
88 } // namespace OHOS