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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_DBMS_INCLUDE_DISTRIBUTED_MONITOR_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_DBMS_INCLUDE_DISTRIBUTED_MONITOR_H
18 
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "common_event_subscriber.h"
22 #include "common_event_subscribe_info.h"
23 #include "distributed_data_storage.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
27 class DistributedMonitor : public EventFwk::CommonEventSubscriber {
28 public:
DistributedMonitor(const EventFwk::CommonEventSubscribeInfo & sp)29     DistributedMonitor(const EventFwk::CommonEventSubscribeInfo& sp):CommonEventSubscriber(sp) {}
~DistributedMonitor()30     ~DistributedMonitor() {}
31 
OnReceiveEvent(const EventFwk::CommonEventData & eventData)32     void OnReceiveEvent(const EventFwk::CommonEventData &eventData)
33     {
34         auto want = eventData.GetWant();
35         std::string action = want.GetAction();
36         APP_LOGI("OnReceiveEvent action:%{public}s", action.c_str());
37         if (action == EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED) {
38             int32_t userId = eventData.GetCode();
39             APP_LOGI("OnReceiveEvent switched userId:%{public}d", userId);
40             DistributedDataStorage::GetInstance()->UpdateDistributedData(userId);
41             return;
42         }
43         int32_t userId = want.GetIntParam(Constants::USER_ID, Constants::INVALID_USERID);
44         std::string bundleName = want.GetElement().GetBundleName();
45         if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED ||
46             action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED) {
47             DistributedDataStorage::GetInstance()->SaveStorageDistributeInfo(bundleName, userId);
48         } else if (action == EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED) {
49             DistributedDataStorage::GetInstance()->DeleteStorageDistributeInfo(bundleName, userId);
50         } else {
51             APP_LOGW("OnReceiveEvent undefined action");
52         }
53     }
54 };
55 }  // namespace AppExecFwk
56 }  // namespace OHOS
57 #endif  // FOUNDATION_APPEXECFWK_SERVICES_DBMS_INCLUDE_DISTRIBUTED_MONITOR_H