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 #ifndef LOG_TAG
16 #define LOG_TAG "ClientTypeManager"
17 #endif
18
19 #include "audio_policy_log.h"
20 #include "client_type_manager.h"
21
22 namespace OHOS {
23 namespace AudioStandard {
GetInstance()24 ClientTypeManager *ClientTypeManager::GetInstance()
25 {
26 static ClientTypeManager clientTypeManager;
27 return &clientTypeManager;
28 }
29
GetAndSaveClientType(uint32_t uid,const std::string & bundleName)30 void ClientTypeManager::GetAndSaveClientType(uint32_t uid, const std::string &bundleName)
31 {
32 AUDIO_INFO_LOG("uid: %{public}u, bundle name %{public}s", uid, bundleName.c_str());
33 #ifdef FEATURE_APPGALLERY
34 std::unique_lock<std::mutex> handlerLock(handlerMutex_);
35 if (clientTypeManagerHandler_ == nullptr) {
36 AUDIO_INFO_LOG("Init client type manager");
37 clientTypeManagerHandler_ = std::make_shared<ClientTypeManagerHandler>();
38 if (clientTypeManagerHandler_ != nullptr) {
39 clientTypeManagerHandler_->RegisterClientTypeListener(this);
40 }
41 }
42 handlerLock.unlock();
43
44 std::unique_lock<std::mutex> lock(clientTypeMapMutex_);
45 auto it = clientTypeMap_.find(uid);
46 if (it != clientTypeMap_.end()) {
47 AUDIO_INFO_LOG("Uid already in map");
48 return;
49 }
50 lock.unlock();
51 if (bundleName == "" || uid == 0) {
52 AUDIO_WARNING_LOG("Get bundle name %{public}s for %{public}u failed", bundleName.c_str(), uid);
53 return;
54 }
55 clientTypeManagerHandler_->SendGetClientType(bundleName.c_str(), uid);
56 #else
57 AUDIO_WARNING_LOG("Get client type is not supported");
58 #endif
59 }
60
SetQueryClientTypeCallback(const sptr<IStandardAudioPolicyManagerListener> & callback)61 void ClientTypeManager::SetQueryClientTypeCallback(const sptr<IStandardAudioPolicyManagerListener> &callback)
62 {
63 AUDIO_INFO_LOG("In");
64 #ifdef FEATURE_APPGALLERY
65 std::lock_guard<std::mutex> handlerLock(handlerMutex_);
66 if (clientTypeManagerHandler_ == nullptr) {
67 AUDIO_INFO_LOG("Init client type manager");
68 clientTypeManagerHandler_ = std::make_shared<ClientTypeManagerHandler>();
69 if (clientTypeManagerHandler_ != nullptr) {
70 clientTypeManagerHandler_->RegisterClientTypeListener(this);
71 }
72 }
73 clientTypeManagerHandler_->SetQueryClientTypeCallback(callback);
74 #endif
75 }
76
GetClientTypeByUid(uint32_t uid)77 ClientType ClientTypeManager::GetClientTypeByUid(uint32_t uid)
78 {
79 std::lock_guard<std::mutex> lock(clientTypeMapMutex_);
80 AUDIO_INFO_LOG("uid %{public}u", uid);
81 auto it = clientTypeMap_.find(uid);
82 if (it == clientTypeMap_.end()) {
83 AUDIO_INFO_LOG("Cannot find uid");
84 return CLIENT_TYPE_OTHERS;
85 }
86 return it->second;
87 }
88
OnClientTypeQueryCompleted(uint32_t uid,ClientType clientType)89 void ClientTypeManager::OnClientTypeQueryCompleted(uint32_t uid, ClientType clientType)
90 {
91 std::lock_guard<std::mutex> lock(clientTypeMapMutex_);
92 AUDIO_INFO_LOG("uid: %{public}u, client type: %{public}d", uid, clientType);
93 clientTypeMap_.insert_or_assign(uid, clientType);
94 }
95 } // namespace AudioStandard
96 } // namespace OHOS
97