• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     sptr<IRemoteObject> object = callback->AsObject();
55     if (object == nullptr) {
56         ACCESSTOKEN_LOG_ERROR(LABEL, "AudioPolicyManager: AsObject is nullptr.");
57         return -1;
58     }
59     return proxy->SetMicStateChangeCallback(object);
60 }
61 
SetMicrophoneMute(bool isMute)62 int32_t AudioManagerPrivacyClient::SetMicrophoneMute(bool isMute)
63 {
64     auto proxy = GetProxy();
65     if (proxy == nullptr) {
66         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
67         return -1;
68     }
69     return proxy->SetMicrophoneMute(isMute);
70 }
71 
IsMicrophoneMute()72 bool AudioManagerPrivacyClient::IsMicrophoneMute()
73 {
74     auto proxy = GetProxy();
75     if (proxy == nullptr) {
76         ACCESSTOKEN_LOG_ERROR(LABEL, "proxy is null");
77         return false;
78     }
79     return proxy->IsMicrophoneMute();
80 }
81 
InitProxy()82 void AudioManagerPrivacyClient::InitProxy()
83 {
84     auto sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
85     if (sam == nullptr) {
86         ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbilityManager is null");
87         return;
88     }
89     auto audioManagerSa = sam->GetSystemAbility(AUDIO_POLICY_SERVICE_ID);
90     if (audioManagerSa == nullptr) {
91         ACCESSTOKEN_LOG_ERROR(LABEL, "GetSystemAbility %{public}d is null",
92             AUDIO_POLICY_SERVICE_ID);
93         return;
94     }
95 
96     serviceDeathObserver_ = new (std::nothrow) AudioMgrDeathRecipient();
97     if (serviceDeathObserver_ != nullptr) {
98         audioManagerSa->AddDeathRecipient(serviceDeathObserver_);
99     }
100 
101     proxy_ = iface_cast<IAudioPolicy>(audioManagerSa);
102     if (proxy_ == nullptr) {
103         ACCESSTOKEN_LOG_ERROR(LABEL, "iface_cast get null");
104     }
105 }
106 
OnRemoteDiedHandle()107 void AudioManagerPrivacyClient::OnRemoteDiedHandle()
108 {
109     std::lock_guard<std::mutex> lock(proxyMutex_);
110     proxy_ = nullptr;
111 }
112 
GetProxy()113 sptr<IAudioPolicy> AudioManagerPrivacyClient::GetProxy()
114 {
115     std::lock_guard<std::mutex> lock(proxyMutex_);
116     if (proxy_ == nullptr) {
117         InitProxy();
118     }
119     return proxy_;
120 }
121 } // namespace AccessToken
122 } // namespace Security
123 } // namespace OHOS
124 
125