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 "account_event_manager.h" 17 18 #include <mutex> 19 20 #include "common_event_manager.h" 21 #include "want.h" 22 23 #include "account_change_event.h" 24 25 namespace OHOS { 26 namespace Contacts { 27 namespace { 28 std::mutex g_mtx; 29 } 30 const std::string AccountEventManager::EVENT = "com.test.account.change.event"; 31 std::shared_ptr<AccountEventManager> AccountEventManager::instance_ = nullptr; 32 AccountEventManager(void)33AccountEventManager::AccountEventManager(void) 34 { 35 } 36 ~AccountEventManager()37AccountEventManager::~AccountEventManager() 38 { 39 } 40 GetInstance()41std::shared_ptr<AccountEventManager> AccountEventManager::GetInstance() 42 { 43 if (instance_ == nullptr) { 44 instance_.reset(new AccountEventManager()); 45 } 46 return instance_; 47 } 48 PublishEvent(std::string data)49void AccountEventManager::PublishEvent(std::string data) 50 { 51 // make a want 52 OHOS::AAFwk::Want want; 53 want.SetAction(EVENT); 54 // make common event 55 OHOS::EventFwk::CommonEventData event; 56 event.SetWant(want); 57 event.SetData(data); 58 59 // lock the mutex 60 g_mtx.lock(); 61 // publish a common event 62 OHOS::EventFwk::CommonEventManager::PublishCommonEvent(event); 63 g_mtx.unlock(); 64 } 65 SubscriberAccountEvent()66void AccountEventManager::SubscriberAccountEvent() 67 { 68 OHOS::EventFwk::MatchingSkills matchingSkills; 69 matchingSkills.AddEvent(EVENT); 70 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills); 71 std::shared_ptr<AccountChangeEvent> subscriber = std::make_shared<AccountChangeEvent>(subscriberInfo); 72 OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber); 73 } 74 } // namespace Contacts 75 } // namespace OHOS