1 /*
2  * Copyright (c) 2024 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 <chrono>
17 
18 #include "avsession_log.h"
19 #include "avsession_errors.h"
20 #include "account_manager_adapter.h"
21 
22 namespace OHOS::AVSession {
AccountManagerAdapter()23 AccountManagerAdapter::AccountManagerAdapter()
24 {
25     SLOGI("construct");
26 }
27 
~AccountManagerAdapter()28 AccountManagerAdapter::~AccountManagerAdapter()
29 {
30     SLOGI("AccountManagerAdapter destroy");
31     std::lock_guard<std::mutex> listLockGuard(listenerListMutex_);
32     accountEventsListenerList_.clear();
33 }
34 
GetInstance()35 AccountManagerAdapter& AccountManagerAdapter::GetInstance()
36 {
37     static AccountManagerAdapter accountManagerAdapter;
38     return accountManagerAdapter;
39 }
40 
Init()41 void AccountManagerAdapter::Init()
42 {
43     // no OsAccountSubscriber register as os_account_manager.h dependency will be removed
44     SLOGI("init AccountManagerAdapter from default user: %{public}d", GetCurrentAccountUserId());
45 }
46 
GetCurrentAccountUserId()47 int32_t AccountManagerAdapter::GetCurrentAccountUserId()
48 {
49     int32_t userId = defaultUserId;
50     // temp use userId : defaultUserId as os_account_manager.h dependency will be removed
51     return userId;
52 }
53 
HandleAccountsEvent(const std::string & type,const int & userId)54 void AccountManagerAdapter::HandleAccountsEvent(const std::string &type, const int &userId)
55 {
56     std::lock_guard<std::mutex> listLockGuard(listenerListMutex_);
57     SLOGI("HandleAccountsEvent for %{public}d with event %{public}s", userId, type.c_str());
58     for (const auto& listener : accountEventsListenerList_) {
59         if (listener) {
60             listener(type, userId);
61         }
62     }
63 }
64 
AddAccountEventsListener(const AccountSwitchListener & callback)65 void AccountManagerAdapter::AddAccountEventsListener(const AccountSwitchListener& callback)
66 {
67     std::lock_guard<std::mutex> listLockGuard(listenerListMutex_);
68     accountEventsListenerList_.push_back(callback);
69 }
70 }
71