1 /*
2 * Copyright (c) 2021-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 #include "contacts_common_event.h"
17
18 namespace OHOS {
19 namespace Contacts {
20 std::shared_ptr<ContactsCommonEvent> ContactsCommonEvent::subscriber = nullptr;
21
OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)22 void ContactsCommonEvent::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
23 {
24 OHOS::AAFwk::Want want = data.GetWant();
25 std::string action = data.GetWant().GetAction();
26 HILOG_INFO("ContactsCommonEvent::OnReceiveEvent action = %{public}s", action.c_str());
27 int msgCode = data.GetCode();
28 std::string msgData = data.GetData();
29 HILOG_INFO("ContactsCommonEvent::OnReceiveEvent msgData = %{public}s", msgData.c_str());
30 HILOG_INFO("ContactsCommonEvent::OnReceiveEvent msgCode = %{public}d", msgCode);
31 }
32
PublishContactEvent( const OHOS::AAFwk::Want &want, int eventCode, const std::string &eventData)33 bool ContactsCommonEvent::PublishContactEvent(
34 const OHOS::AAFwk::Want &want, int eventCode, const std::string &eventData)
35 {
36 OHOS::EventFwk::CommonEventData data;
37 data.SetWant(want);
38 data.SetCode(eventCode);
39 data.SetData(eventData);
40 OHOS::EventFwk::CommonEventPublishInfo publishInfo;
41 publishInfo.SetOrdered(true);
42 bool publishResult = OHOS::EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
43 HILOG_INFO("PublishContactEvent end publishResult = %{public}d", publishResult);
44 return publishResult;
45 }
46
UnregisterSubscriber(std::shared_ptr<OHOS::EventFwk::CommonEventSubscriber> subscriber)47 void ContactsCommonEvent::UnregisterSubscriber(std::shared_ptr<OHOS::EventFwk::CommonEventSubscriber> subscriber)
48 {
49 if (subscriber != nullptr) {
50 bool subscribeResult = OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriber);
51 subscriber = nullptr;
52 HILOG_INFO("UnregisterSubscriber contacts end###subscribeResult = %{public}d", subscribeResult);
53 }
54 }
55
RegisterSubscriber()56 void ContactsCommonEvent::RegisterSubscriber()
57 {
58 OHOS::EventFwk::MatchingSkills matchingSkills;
59 matchingSkills.AddEvent(CONTACT_EVENT);
60 matchingSkills.AddEvent(CALL_LOG_EVENT);
61 matchingSkills.AddEvent(VOICEMAIL_EVENT);
62 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
63 subscriber = std::make_shared<ContactsCommonEvent>(subscriberInfo);
64 bool subscribeResult = OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
65 HILOG_INFO("RegisterSubscriber contacts end###subscribeResult = %{public}d", subscribeResult);
66 }
67
SendContactChange(int actionCode)68 void ContactsCommonEvent::SendContactChange(int actionCode)
69 {
70 OHOS::AAFwk::Want want;
71 int32_t eventCode = CONTACT_EVENT_CODE;
72 want.SetParam("contactsActionCode", actionCode);
73 want.SetAction(CONTACT_EVENT);
74 std::string eventData("ContactChange");
75 PublishContactEvent(want, eventCode, eventData);
76 }
77
SendCallLogChange(int actionCode)78 void ContactsCommonEvent::SendCallLogChange(int actionCode)
79 {
80 OHOS::AAFwk::Want want;
81 int32_t eventCode = CALL_LOG_EVENT_CODE;
82 want.SetParam("contactsActionCode", actionCode);
83 want.SetAction(CALL_LOG_EVENT);
84 std::string eventData("CallLogChange");
85 PublishContactEvent(want, eventCode, eventData);
86 }
87
SendVoiceMailChange(int actionCode)88 void ContactsCommonEvent::SendVoiceMailChange(int actionCode)
89 {
90 OHOS::AAFwk::Want want;
91 int32_t eventCode = VOICEMAIL_EVENT_CODE;
92 want.SetParam("contactsActionCode", actionCode);
93 want.SetAction(VOICEMAIL_EVENT);
94 std::string eventData("voicemailChange");
95 PublishContactEvent(want, eventCode, eventData);
96 }
97 } // namespace Contacts
98 } // namespace OHOS