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 "audio_manager_adapter.h"
17 #include "accesstoken_common_log.h"
18 #ifdef AUDIO_FRAMEWORK_ENABLE
19 #include "audio_policy_ipc_interface_code.h"
20 #endif
21 #include <iremote_proxy.h>
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24
25 namespace OHOS {
26 namespace Security {
27 namespace AccessToken {
28
GetInstance()29 AudioManagerAdapter& AudioManagerAdapter::GetInstance()
30 {
31 static AudioManagerAdapter *instance = new (std::nothrow) AudioManagerAdapter();
32 return *instance;
33 }
34
AudioManagerAdapter()35 AudioManagerAdapter::AudioManagerAdapter()
36 {}
37
~AudioManagerAdapter()38 AudioManagerAdapter::~AudioManagerAdapter()
39 {}
40
GetPersistentMicMuteState()41 bool AudioManagerAdapter::GetPersistentMicMuteState()
42 {
43 #ifndef AUDIO_FRAMEWORK_ENABLE
44 LOGI(PRI_DOMAIN, PRI_TAG, "audio framework is not support.");
45 return false;
46 #else
47 auto proxy = GetProxy();
48 if (proxy == nullptr) {
49 LOGE(PRI_DOMAIN, PRI_TAG, "Failed to GetProxy.");
50 return false;
51 }
52
53 MessageParcel data;
54 MessageParcel reply;
55 MessageOption option;
56
57 std::u16string AUDIO_MGR_DESCRIPTOR = u"OHOS.AudioStandard.IAudioPolicy";
58 if (!data.WriteInterfaceToken(AUDIO_MGR_DESCRIPTOR)) {
59 LOGE(PRI_DOMAIN, PRI_TAG, "Failed to write WriteInterfaceToken.");
60 return false;
61 }
62 int32_t error = proxy->SendRequest(
63 static_cast<uint32_t>(AudioStandard::AudioPolicyInterfaceCode::GET_MICROPHONE_MUTE_PERSISTENT),
64 data, reply, option);
65 if (error != NO_ERROR) {
66 LOGE(PRI_DOMAIN, PRI_TAG, "SendRequest error: %{public}d", error);
67 return false;
68 }
69 int32_t errorCode = reply.ReadInt32();
70 if (errorCode != NO_ERROR) {
71 LOGE(PRI_DOMAIN, PRI_TAG, "GET_MICROPHONE_MUTE_PERSISTENT error: %{public}d", errorCode);
72 return false;
73 }
74 return reply.ReadInt32() == 1 ? true : false;
75 #endif
76 }
77
78 #ifdef AUDIO_FRAMEWORK_ENABLE
InitProxy()79 void AudioManagerAdapter::InitProxy()
80 {
81 if (proxy_ != nullptr && (!proxy_->IsObjectDead())) {
82 return;
83 }
84 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
85 if (systemManager == nullptr) {
86 LOGE(PRI_DOMAIN, PRI_TAG, "Fail to get system ability registry.");
87 return;
88 }
89 sptr<IRemoteObject> remoteObj = systemManager->CheckSystemAbility(AUDIO_POLICY_SERVICE_ID);
90 if (remoteObj == nullptr) {
91 LOGE(PRI_DOMAIN, PRI_TAG, "Fail to connect ability manager service.");
92 return;
93 }
94
95 deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new (std::nothrow) AudioManagerDeathRecipient());
96 if (deathRecipient_ == nullptr) {
97 LOGE(PRI_DOMAIN, PRI_TAG, "Failed to create AudioManagerDeathRecipient!");
98 return;
99 }
100 if ((remoteObj->IsProxyObject()) && (!remoteObj->AddDeathRecipient(deathRecipient_))) {
101 LOGE(PRI_DOMAIN, PRI_TAG, "Add death recipient to AbilityManagerService failed.");
102 return;
103 }
104 proxy_ = remoteObj;
105 }
106
GetProxy()107 sptr<IRemoteObject> AudioManagerAdapter::GetProxy()
108 {
109 std::lock_guard<std::mutex> lock(proxyMutex_);
110 if (proxy_ == nullptr || proxy_->IsObjectDead()) {
111 InitProxy();
112 }
113 return proxy_;
114 }
115
ReleaseProxy(const wptr<IRemoteObject> & remote)116 void AudioManagerAdapter::ReleaseProxy(const wptr<IRemoteObject>& remote)
117 {
118 std::lock_guard<std::mutex> lock(proxyMutex_);
119 if ((proxy_ != nullptr) && (proxy_ == remote.promote())) {
120 proxy_->RemoveDeathRecipient(deathRecipient_);
121 proxy_ = nullptr;
122 deathRecipient_ = nullptr;
123 }
124 }
125
OnRemoteDied(const wptr<IRemoteObject> & remote)126 void AudioManagerAdapter::AudioManagerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
127 {
128 LOGE(PRI_DOMAIN, PRI_TAG, "AudioManagerDeathRecipient handle remote died.");
129 AudioManagerAdapter::GetInstance().ReleaseProxy(remote);
130 }
131 #endif
132 } // namespace AccessToken
133 } // namespace Security
134 } // namespace OHOS
135