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 "wms_connection_observer.h"
17
18 #include "global.h"
19 namespace OHOS {
20 namespace MiscServices {
21 // LCOV_EXCL_START
22 std::mutex WmsConnectionObserver::lock_;
23 std::set<int32_t> WmsConnectionObserver::connectedUserId_;
OnConnected(int32_t userId,int32_t screenId)24 void WmsConnectionObserver::OnConnected(int32_t userId, int32_t screenId)
25 {
26 IMSA_HILOGI("WMS connect, userId: %{public}d, screenId: %{public}d.", userId, screenId);
27 Add(userId);
28 if (changeHandler_ != nullptr) {
29 changeHandler_(true, userId, screenId);
30 }
31 }
32
OnDisconnected(int32_t userId,int32_t screenId)33 void WmsConnectionObserver::OnDisconnected(int32_t userId, int32_t screenId)
34 {
35 IMSA_HILOGI("WMS disconnect, userId: %{public}d, screenId: %{public}d.", userId, screenId);
36 Remove(userId);
37 if (changeHandler_ != nullptr) {
38 changeHandler_(false, userId, screenId);
39 }
40 }
41
Add(int32_t userId)42 void WmsConnectionObserver::Add(int32_t userId)
43 {
44 std::lock_guard<std::mutex> lock(lock_);
45 connectedUserId_.insert(userId);
46 }
47
Remove(int32_t userId)48 void WmsConnectionObserver::Remove(int32_t userId)
49 {
50 std::lock_guard<std::mutex> lock(lock_);
51 auto it = connectedUserId_.find(userId);
52 if (it == connectedUserId_.end()) {
53 return;
54 }
55 connectedUserId_.erase(it);
56 }
57 // LCOV_EXCL_STOP
IsWmsConnected(int32_t userId)58 bool WmsConnectionObserver::IsWmsConnected(int32_t userId)
59 {
60 std::lock_guard<std::mutex> lock(lock_);
61 auto it = connectedUserId_.find(userId);
62 return it != connectedUserId_.end();
63 }
64 } // namespace MiscServices
65 } // namespace OHOS