1 /*
2 * Copyright (c) 2022 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 #include "audio_manager_privacy_client.h"
16 #include <unistd.h>
17
18 #include "accesstoken_log.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {
27 LOG_CORE, SECURITY_DOMAIN_PRIVACY, "AudioManagerPrivacyClient"
28 };
29 } // namespace
30
GetInstance()31 AudioManagerPrivacyClient& AudioManagerPrivacyClient::GetInstance()
32 {
33 static AudioManagerPrivacyClient instance;
34 return instance;
35 }
36
AudioManagerPrivacyClient()37 AudioManagerPrivacyClient::AudioManagerPrivacyClient()
38 {}
39
~AudioManagerPrivacyClient()40 AudioManagerPrivacyClient::~AudioManagerPrivacyClient()
41 {}
42
SetMicStateChangeCallback(const sptr<AudioRoutingManagerListenerStub> & callback)43 int32_t AudioManagerPrivacyClient::SetMicStateChangeCallback(const sptr<AudioRoutingManagerListenerStub>& callback)
44 {
45 if (callback == nullptr) {
46 ACCESSTOKEN_LOG_ERROR(LABEL, "AudioPolicyManager: callback is nullptr");
47 return -1;
48 }
49 auto proxy = GetProxy();
50 if (proxy == nullptr) {
51 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
52 return -1;
53 }
54 int32_t clientId = static_cast<int32_t>(getpid());
55 sptr<IRemoteObject> object = callback->AsObject();
56 if (object == nullptr) {
57 ACCESSTOKEN_LOG_ERROR(LABEL, "AudioPolicyManager: AsObject is nullptr.");
58 return -1;
59 }
60 return proxy->SetMicStateChangeCallback(clientId, object);
61 }
62
SetMicrophoneMute(bool isMute)63 int32_t AudioManagerPrivacyClient::SetMicrophoneMute(bool isMute)
64 {
65 auto proxy = GetProxy();
66 if (proxy == nullptr) {
67 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
68 return -1;
69 }
70 return proxy->SetMicrophoneMute(isMute);
71 }
72
IsMicrophoneMute()73 bool AudioManagerPrivacyClient::IsMicrophoneMute()
74 {
75 auto proxy = GetProxy();
76 if (proxy == nullptr) {
77 ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
78 return false;
79 }
80 return proxy->IsMicrophoneMute();
81 }
82
InitProxy()83 void AudioManagerPrivacyClient::InitProxy()
84 {
85 auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
86 if (sam == nullptr) {
87 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
88 return;
89 }
90 auto audioManagerSa = sam->GetSystemAbility(AUDIO_POLICY_SERVICE_ID);
91 if (audioManagerSa == nullptr) {
92 ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
93 AUDIO_POLICY_SERVICE_ID);
94 return;
95 }
96
97 serviceDeathObserver_ = new (std::nothrow) AudioMgrDeathRecipient();
98 if (serviceDeathObserver_ != nullptr) {
99 audioManagerSa->AddDeathRecipient(serviceDeathObserver_);
100 }
101
102 proxy_ = iface_cast<IAudioPolicy>(audioManagerSa);
103 if (proxy_ == nullptr) {
104 ACCESSTOKEN_LOG_ERROR(LABEL, "iface_cast get null");
105 }
106 }
107
OnRemoteDiedHandle()108 void AudioManagerPrivacyClient::OnRemoteDiedHandle()
109 {
110 std::lock_guard<std::mutex> lock(proxyMutex_);
111 proxy_ = nullptr;
112 }
113
GetProxy()114 sptr<IAudioPolicy> AudioManagerPrivacyClient::GetProxy()
115 {
116 std::lock_guard<std::mutex> lock(proxyMutex_);
117 if (proxy_ == nullptr) {
118 InitProxy();
119 }
120 return proxy_;
121 }
122 } // namespace AccessToken
123 } // namespace Security
124 } // namespace OHOS
125
126