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