1115cd2caSopenharmony_ci/*
2115cd2caSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3115cd2caSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4115cd2caSopenharmony_ci * you may not use this file except in compliance with the License.
5115cd2caSopenharmony_ci * You may obtain a copy of the License at
6115cd2caSopenharmony_ci *
7115cd2caSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8115cd2caSopenharmony_ci *
9115cd2caSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10115cd2caSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11115cd2caSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12115cd2caSopenharmony_ci * See the License for the specific language governing permissions and
13115cd2caSopenharmony_ci * limitations under the License.
14115cd2caSopenharmony_ci */
15115cd2caSopenharmony_ci
16115cd2caSopenharmony_ci#include "account_event_manager.h"
17115cd2caSopenharmony_ci
18115cd2caSopenharmony_ci#include <mutex>
19115cd2caSopenharmony_ci
20115cd2caSopenharmony_ci#include "common_event_manager.h"
21115cd2caSopenharmony_ci#include "want.h"
22115cd2caSopenharmony_ci
23115cd2caSopenharmony_ci#include "account_change_event.h"
24115cd2caSopenharmony_ci
25115cd2caSopenharmony_cinamespace OHOS {
26115cd2caSopenharmony_cinamespace Contacts {
27115cd2caSopenharmony_cinamespace {
28115cd2caSopenharmony_cistd::mutex g_mtx;
29115cd2caSopenharmony_ci}
30115cd2caSopenharmony_ciconst std::string AccountEventManager::EVENT = "com.test.account.change.event";
31115cd2caSopenharmony_cistd::shared_ptr<AccountEventManager> AccountEventManager::instance_ = nullptr;
32115cd2caSopenharmony_ci
33115cd2caSopenharmony_ciAccountEventManager::AccountEventManager(void)
34115cd2caSopenharmony_ci{
35115cd2caSopenharmony_ci}
36115cd2caSopenharmony_ci
37115cd2caSopenharmony_ciAccountEventManager::~AccountEventManager()
38115cd2caSopenharmony_ci{
39115cd2caSopenharmony_ci}
40115cd2caSopenharmony_ci
41115cd2caSopenharmony_cistd::shared_ptr<AccountEventManager> AccountEventManager::GetInstance()
42115cd2caSopenharmony_ci{
43115cd2caSopenharmony_ci    if (instance_ == nullptr) {
44115cd2caSopenharmony_ci        instance_.reset(new AccountEventManager());
45115cd2caSopenharmony_ci    }
46115cd2caSopenharmony_ci    return instance_;
47115cd2caSopenharmony_ci}
48115cd2caSopenharmony_ci
49115cd2caSopenharmony_civoid AccountEventManager::PublishEvent(std::string data)
50115cd2caSopenharmony_ci{
51115cd2caSopenharmony_ci    // make a want
52115cd2caSopenharmony_ci    OHOS::AAFwk::Want want;
53115cd2caSopenharmony_ci    want.SetAction(EVENT);
54115cd2caSopenharmony_ci    // make common event
55115cd2caSopenharmony_ci    OHOS::EventFwk::CommonEventData event;
56115cd2caSopenharmony_ci    event.SetWant(want);
57115cd2caSopenharmony_ci    event.SetData(data);
58115cd2caSopenharmony_ci
59115cd2caSopenharmony_ci    // lock the mutex
60115cd2caSopenharmony_ci    g_mtx.lock();
61115cd2caSopenharmony_ci    // publish a common event
62115cd2caSopenharmony_ci    OHOS::EventFwk::CommonEventManager::PublishCommonEvent(event);
63115cd2caSopenharmony_ci    g_mtx.unlock();
64115cd2caSopenharmony_ci}
65115cd2caSopenharmony_ci
66115cd2caSopenharmony_civoid AccountEventManager::SubscriberAccountEvent()
67115cd2caSopenharmony_ci{
68115cd2caSopenharmony_ci    OHOS::EventFwk::MatchingSkills matchingSkills;
69115cd2caSopenharmony_ci    matchingSkills.AddEvent(EVENT);
70115cd2caSopenharmony_ci    OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
71115cd2caSopenharmony_ci    std::shared_ptr<AccountChangeEvent> subscriber = std::make_shared<AccountChangeEvent>(subscriberInfo);
72115cd2caSopenharmony_ci    OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
73115cd2caSopenharmony_ci}
74115cd2caSopenharmony_ci} // namespace Contacts
75115cd2caSopenharmony_ci} // namespace OHOS