1 /*
2  * Copyright (c) 2021-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 "bundle_monitor.h"
17 
18 namespace OHOS {
19 namespace AppExecFwk {
BundleMonitor(const EventFwk::CommonEventSubscribeInfo & subscribeInfo)20 BundleMonitor::BundleMonitor(const EventFwk::CommonEventSubscribeInfo &subscribeInfo)
21     : EventFwk::CommonEventSubscriber(subscribeInfo)
22 {
23     APP_LOGI("BundleMonitor constructor with subscribeInfo");
24 }
25 
Subscribe(const sptr<IBundleStatusCallback> & callback)26 bool BundleMonitor::Subscribe(const sptr<IBundleStatusCallback> &callback)
27 {
28     APP_LOGI("Subscribe called");
29     {
30         std::lock_guard<std::mutex> lock(mutex_);
31         callback_ = callback;
32     }
33 
34     if (EventFwk::CommonEventManager::SubscribeCommonEvent(shared_from_this()) != true) {
35         APP_LOGE("SubscribeCommonEvent occur exception");
36         return false;
37     }
38     return true;
39 }
40 
UnSubscribe()41 bool BundleMonitor::UnSubscribe()
42 {
43     APP_LOGI("unsubscribe called");
44     if (EventFwk::CommonEventManager::UnSubscribeCommonEvent(shared_from_this()) != true) {
45         APP_LOGE("UnsubscribeCommonEvent occur exception");
46         return false;
47     }
48     {
49         std::lock_guard<std::mutex> lock(mutex_);
50         callback_ = nullptr;
51     }
52     return true;
53 }
54 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)55 void BundleMonitor::OnReceiveEvent(const EventFwk::CommonEventData &eventData)
56 {
57     APP_LOGD("OnReceiveEvent common event onReceiveEvent called");
58     Want want = eventData.GetWant();
59     std::string action = want.GetAction();
60     std::string bundleName = want.GetElement().GetBundleName();
61     int userId = want.GetIntParam(Constants::USER_ID, Constants::INVALID_USERID);
62     int32_t appIndex = want.GetIntParam(Constants::APP_INDEX, Constants::DEFAULT_APP_INDEX);
63     APP_LOGI("OnReceiveEvent action = %{public}s, bundle = %{public}s, userId = %{public}d, appIndex = %{public}d",
64         action.c_str(), bundleName.c_str(), userId, appIndex);
65     std::lock_guard<std::mutex> lock(mutex_);
66     if ((action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED) && (callback_ != nullptr)) {
67         callback_->OnBundleAdded(bundleName, userId, appIndex);
68     } else if ((action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) && (callback_ != nullptr)) {
69         callback_->OnBundleUpdated(bundleName, userId, appIndex);
70     } else if ((action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) && (callback_ != nullptr)) {
71         callback_->OnBundleRemoved(bundleName, userId, appIndex);
72     } else {
73         APP_LOGI("%{public}s not support", action.c_str());
74     }
75 }
76 }  // namespace AppExecFwk
77 }  // namespace OHOS