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 "account_event_subscribe.h"
17 #include "account_info.h"
18 #include "account_log_wrapper.h"
19 #include "bundle_constants.h"
20 #ifdef HAS_CES_PART
21 #include "common_event_manager.h"
22 #include "common_event_subscriber.h"
23 #include "common_event_support.h"
24 #include "common_event_subscribe_info.h"
25 #include "matching_skills.h"
26 #endif // HAS_CES_PART
27 
28 namespace OHOS {
29 namespace AccountSA {
30 #ifdef HAS_CES_PART
AccountEventSubscriberCallback(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,const AccountCommonEventCallback & callback)31 AccountEventSubscriberCallback::AccountEventSubscriberCallback(
32     const EventFwk::CommonEventSubscribeInfo &subscribeInfo, const AccountCommonEventCallback &callback)
33     : CommonEventSubscriber(subscribeInfo), callback_(callback)
34 {}
35 
OnReceiveEvent(const EventFwk::CommonEventData & data)36 void AccountEventSubscriberCallback::OnReceiveEvent(const EventFwk::CommonEventData &data)
37 {
38     auto want = data.GetWant();
39     std::string action = want.GetAction();
40     ACCOUNT_LOGI("Receive event: %{public}s", action.c_str());
41     if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
42         auto uid = want.GetIntParam(AppExecFwk::Constants::UID, -1);
43         callback_.OnPackageRemoved(uid);
44     }
45 }
46 
AccountEventSubscriber(const AccountCommonEventCallback & callback)47 AccountEventSubscriber::AccountEventSubscriber(const AccountCommonEventCallback &callback) : callback_(callback)
48 {
49     EventFwk::MatchingSkills matchingSkills;
50     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
51     EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
52     subscriber_ = std::make_shared<AccountEventSubscriberCallback>(subscribeInfo, callback_);
53 }
54 
~AccountEventSubscriber()55 AccountEventSubscriber::~AccountEventSubscriber()
56 {
57     EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
58 }
59 
CreateEventSubscribe()60 bool AccountEventSubscriber::CreateEventSubscribe()
61 {
62     return EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber_);
63 }
64 
DestroyEventSubscribe()65 bool AccountEventSubscriber::DestroyEventSubscribe()
66 {
67     return EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber_);
68 }
69 #endif // HAS_CES_PART
70 } // namespace AccountSA
71 } // namespace OHOS