• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "common_event.h"
17 #include "log.h"
18 
19 namespace OHOS::Request::Download {
20 std::shared_ptr<CommonEvent> CommonEvent::subscriber = nullptr;
21 
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)22 void CommonEvent::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
23 {
24     OHOS::AAFwk::Want want = data.GetWant();
25     std::string action = data.GetWant().GetAction();
26     DOWNLOAD_HILOGI("CommonEvent::OnReceiveEvent action = %{public}s", action.c_str());
27     int msgCode = data.GetCode();
28     std::string msgData = data.GetData();
29     DOWNLOAD_HILOGI("CommonEvent::OnReceiveEvent msgData = %{public}s", msgData.c_str());
30     DOWNLOAD_HILOGI("CommonEvent::OnReceiveEvent msgCode = %{public}d", msgCode);
31 }
32 
PublishEvent(const OHOS::AAFwk::Want & want,int eventCode,const std::string & eventData)33 bool CommonEvent::PublishEvent(const OHOS::AAFwk::Want &want, int eventCode, const std::string &eventData)
34 {
35     OHOS::EventFwk::CommonEventData data;
36     data.SetWant(want);
37     data.SetCode(eventCode);
38     data.SetData(eventData);
39     OHOS::EventFwk::CommonEventPublishInfo publishInfo;
40     publishInfo.SetOrdered(true);
41     bool publishResult = OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
42     DOWNLOAD_HILOGI("PublishEvent end publishResult = %{public}d", publishResult);
43     return publishResult;
44 }
45 
UnregisterSubscriber(std::shared_ptr<OHOS::EventFwk::CommonEventSubscriber> subscriber)46 void CommonEvent::UnregisterSubscriber(std::shared_ptr<OHOS::EventFwk::CommonEventSubscriber> subscriber)
47 {
48     if (subscriber != nullptr) {
49         bool subscribeResult = OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber);
50         subscriber = nullptr;
51         DOWNLOAD_HILOGI("UnregisterSubscriber end###subscribeResult = %{public}d", subscribeResult);
52     }
53 }
54 
RegisterSubscriber()55 void CommonEvent::RegisterSubscriber()
56 {
57     OHOS::EventFwk::MatchingSkills matchingSkills;
58     matchingSkills.AddEvent(DOWNLOAD_EVENT);
59     OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
60     subscriber = std::make_shared<CommonEvent>(subscriberInfo);
61     bool subscribeResult = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
62     DOWNLOAD_HILOGI("RegisterSubscriber end###subscribeResult = %{public}d", subscribeResult);
63 }
64 
SendChange(int actionCode)65 void CommonEvent::SendChange(int actionCode)
66 {
67     OHOS::AAFwk::Want want;
68     int32_t eventCode = DOWNLOAD_EVENT_CODE;
69     want.SetParam("ActionCode", actionCode);
70     want.SetAction(DOWNLOAD_EVENT);
71     std::string eventData("DataChange");
72     PublishEvent(want, eventCode, eventData);
73 }
74 } // namespace OHOS::Request::Download