1 /* 2 * Copyright (c) 2021 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 #define LOG_TAG "BroadcastSenderImpl" 17 18 #include "broadcast_sender_impl.h" 19 #include "common_event_manager.h" 20 #include "common_event_support.h" 21 #include "log_print.h" 22 #include "string_wrapper.h" 23 24 namespace OHOS::DistributedKv { 25 using namespace OHOS::EventFwk; 26 using namespace OHOS::AAFwk; 27 class CommonEventSubscriberListener : public CommonEventSubscriber { 28 public: 29 explicit CommonEventSubscriberListener(const CommonEventSubscribeInfo &subscriberInfo); ~CommonEventSubscriberListener()30 virtual ~CommonEventSubscriberListener() {} 31 virtual void OnReceiveEvent(const CommonEventData &data); 32 }; 33 CommonEventSubscriberListener(const CommonEventSubscribeInfo &subscriberInfo)34CommonEventSubscriberListener::CommonEventSubscriberListener(const CommonEventSubscribeInfo &subscriberInfo) 35 : CommonEventSubscriber(subscriberInfo) 36 {} 37 OnReceiveEvent(const CommonEventData &data)38void CommonEventSubscriberListener::OnReceiveEvent(const CommonEventData &data) 39 { 40 (void) data; 41 ZLOGI("receive event."); 42 } 43 SendEvent(const EventParams ¶ms)44void BroadcastSenderImpl::SendEvent(const EventParams ¶ms) 45 { 46 ZLOGI("SendEvent code."); 47 bool result = false; 48 WantParams parameters; 49 parameters.SetParam(PKG_NAME, String::Box(params.appId)); 50 Want want; 51 want.SetAction(ACTION_NAME).SetParams(parameters); 52 CommonEventData commonEventData(want); 53 MatchingSkills matchingSkills; 54 matchingSkills.AddEvent(ACTION_NAME); 55 CommonEventSubscribeInfo subscribeInfo(matchingSkills); 56 auto subscriberPtr = std::make_shared<CommonEventSubscriberListener>(subscribeInfo); 57 if (CommonEventManager::SubscribeCommonEvent(subscriberPtr)) { 58 result = CommonEventManager::PublishCommonEvent(commonEventData); 59 } 60 CommonEventManager::UnSubscribeCommonEvent(subscriberPtr); 61 ZLOGI("SendEvent result:%{public}d.", result); 62 } 63 } // namespace OHOS::DistributedKv 64