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 #include "common_event_observer.h"
17
18 #include "bundle_constants.h"
19 #include "common_event_support.h"
20
21 #include "standby_service_impl.h"
22
23 namespace OHOS {
24 namespace DevStandbyMgr {
CommonEventObserver(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,const std::shared_ptr<AppExecFwk::EventHandler> & handler)25 CommonEventObserver::CommonEventObserver(const EventFwk::CommonEventSubscribeInfo &subscribeInfo,
26 const std::shared_ptr<AppExecFwk::EventHandler>& handler)
27 : EventFwk::CommonEventSubscriber(subscribeInfo), handler_(handler) {}
28
Subscribe()29 bool WEAK_FUNC CommonEventObserver::Subscribe()
30 {
31 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(shared_from_this())) {
32 STANDBYSERVICE_LOGI("SubscribeCommonEvent occur exception.");
33 return false;
34 }
35 return true;
36 }
37
Unsubscribe()38 bool WEAK_FUNC CommonEventObserver::Unsubscribe()
39 {
40 if (!EventFwk::CommonEventManager::UnSubscribeCommonEvent(shared_from_this())) {
41 STANDBYSERVICE_LOGI("UnsubscribeCommonEvent occur exception.");
42 return false;
43 }
44 return true;
45 }
46
OnReceiveEvent(const EventFwk::CommonEventData & eventData)47 void CommonEventObserver::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
48 {
49 AAFwk::Want want = eventData.GetWant();
50 std::string action = want.GetAction();
51 STANDBYSERVICE_LOGI("OnReceiveEvent get action: %{public}s", action.c_str());
52 if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED ||
53 action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED ||
54 action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED ||
55 action == EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_REMOVED ||
56 action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED) {
57 std::string bundleName = want.GetElement().GetBundleName();
58 int32_t uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
59 handler_->PostTask([uid, bundleName]() {
60 StandbyServiceImpl::GetInstance()->RemoveAppAllowRecord(uid, bundleName, true);
61 });
62 } else {
63 handler_->PostTask([]() { StandbyServiceImpl::GetInstance()->ResetTimeObserver(); });
64 }
65 }
66
CreateCommonEventObserver(const std::shared_ptr<AppExecFwk::EventHandler> & handler)67 std::shared_ptr<CommonEventObserver> CommonEventObserver::CreateCommonEventObserver(
68 const std::shared_ptr<AppExecFwk::EventHandler>& handler)
69 {
70 EventFwk::MatchingSkills matchingSkills;
71 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
72 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
73 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED);
74 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_REMOVED);
75 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_FULLY_REMOVED);
76 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED);
77 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_NITZ_TIMEZONE_CHANGED);
78 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_TIME_CHANGED);
79 matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_NITZ_TIME_CHANGED);
80 EventFwk::CommonEventSubscribeInfo commonEventSubscribeInfo(matchingSkills);
81 auto observer = std::make_shared<CommonEventObserver>(commonEventSubscribeInfo, handler);
82 return observer;
83 }
84 } // namespace DevStandbyMgr
85 } // namespace OHOS