1 /*
2 * Copyright (c) 2024 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 #define LOG_TAG "SysEventSubscriber"
16 #include "sys_event_subscriber.h"
17 
18 #include "common_event_manager.h"
19 #include "common_event_support.h"
20 #include "log_print.h"
21 
22 namespace OHOS::DataShare {
SysEventSubscriber(const EventFwk::CommonEventSubscribeInfo & info)23 SysEventSubscriber::SysEventSubscriber(const EventFwk::CommonEventSubscribeInfo& info)
24     : CommonEventSubscriber(info)
25 {
26     callbacks_ = { { EventFwk::CommonEventSupport::COMMON_EVENT_BUNDLE_SCAN_FINISHED,
27         &SysEventSubscriber::OnBMSReady } };
28 }
29 
OnReceiveEvent(const EventFwk::CommonEventData & event)30 void SysEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData& event)
31 {
32     EventFwk::Want want = event.GetWant();
33     std::string action = want.GetAction();
34     auto it = callbacks_.find(action);
35     if (it != callbacks_.end()) {
36         (this->*(it->second))();
37     }
38 }
39 
OnBMSReady()40 void SysEventSubscriber::OnBMSReady()
41 {
42     NotifyDataShareReady();
43 }
44 
NotifyDataShareReady()45 void SysEventSubscriber::NotifyDataShareReady()
46 {
47     AAFwk::Want want;
48     want.SetAction("usual.event.DATA_SHARE_READY");
49     EventFwk::CommonEventData CommonEventData { want };
50     EventFwk::CommonEventPublishInfo publishInfo;
51     publishInfo.SetSticky(true);
52     if (!EventFwk::CommonEventManager::PublishCommonEvent(CommonEventData, publishInfo)) {
53         ZLOGE("Notify dataShare ready failed.");
54         return;
55     }
56     ZLOGI("Notify dataShare ready succeed.");
57 }
58 }
59