1 /*
2  * Copyright (c) 2021-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 #ifndef LOG_TAG
16 #define LOG_TAG "AudioPolicyManagerListenerProxy"
17 #endif
18 
19 #include "audio_policy_manager_listener_proxy.h"
20 #include "audio_policy_log.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
AudioPolicyManagerListenerProxy(const sptr<IRemoteObject> & impl)24 AudioPolicyManagerListenerProxy::AudioPolicyManagerListenerProxy(const sptr<IRemoteObject> &impl)
25     : IRemoteProxy<IStandardAudioPolicyManagerListener>(impl)
26 {
27     AUDIO_DEBUG_LOG("Instances create");
28 }
29 
~AudioPolicyManagerListenerProxy()30 AudioPolicyManagerListenerProxy::~AudioPolicyManagerListenerProxy()
31 {
32     AUDIO_DEBUG_LOG("~AudioPolicyManagerListenerProxy: Instance destroy");
33 }
34 
WriteInterruptEventParams(MessageParcel & data,const InterruptEventInternal & interruptEvent)35 void AudioPolicyManagerListenerProxy::WriteInterruptEventParams(MessageParcel &data,
36                                                                 const InterruptEventInternal &interruptEvent)
37 {
38     data.WriteInt32(static_cast<int32_t>(interruptEvent.eventType));
39     data.WriteInt32(static_cast<int32_t>(interruptEvent.forceType));
40     data.WriteInt32(static_cast<int32_t>(interruptEvent.hintType));
41     data.WriteFloat(interruptEvent.duckVolume);
42     data.WriteBool(interruptEvent.callbackToApp);
43 }
44 
OnInterrupt(const InterruptEventInternal & interruptEvent)45 void AudioPolicyManagerListenerProxy::OnInterrupt(const InterruptEventInternal &interruptEvent)
46 {
47     MessageParcel data;
48     MessageParcel reply;
49     MessageOption option(MessageOption::TF_ASYNC);
50     if (!data.WriteInterfaceToken(GetDescriptor())) {
51         AUDIO_ERR_LOG("AudioPolicyManagerListenerProxy: WriteInterfaceToken failed");
52         return;
53     }
54 
55     WriteInterruptEventParams(data, interruptEvent);
56     int error = Remote()->SendRequest(ON_INTERRUPT, data, reply, option);
57     if (error != ERR_NONE) {
58         AUDIO_ERR_LOG("OnInterrupt failed, error: %{public}d", error);
59     }
60 }
61 
AudioPolicyManagerListenerCallback(const sptr<IStandardAudioPolicyManagerListener> & listener)62 AudioPolicyManagerListenerCallback::AudioPolicyManagerListenerCallback(
63     const sptr<IStandardAudioPolicyManagerListener> &listener) : listener_(listener)
64 {
65         AUDIO_DEBUG_LOG("AudioPolicyManagerListenerCallback: Instance create");
66 }
67 
~AudioPolicyManagerListenerCallback()68 AudioPolicyManagerListenerCallback::~AudioPolicyManagerListenerCallback()
69 {
70     AUDIO_DEBUG_LOG("AudioPolicyManagerListenerCallback: Instance destroy");
71 }
72 
OnInterrupt(const InterruptEventInternal & interruptEvent)73 void AudioPolicyManagerListenerCallback::OnInterrupt(const InterruptEventInternal &interruptEvent)
74 {
75     if (listener_ != nullptr) {
76         listener_->OnInterrupt(interruptEvent);
77     }
78 }
79 
OnAvailableDeviceChange(const AudioDeviceUsage usage,const DeviceChangeAction & deviceChangeAction)80 void AudioPolicyManagerListenerProxy::OnAvailableDeviceChange(const AudioDeviceUsage usage,
81     const DeviceChangeAction &deviceChangeAction)
82 {
83     AUDIO_DEBUG_LOG("AudioPolicyManagerListenerProxy: OnAvailableDeviceChange at listener proxy");
84 
85     MessageParcel data;
86     MessageParcel reply;
87     MessageOption option(MessageOption::TF_ASYNC);
88 
89     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(GetDescriptor()),
90         "AudioPolicyManagerListenerProxy: WriteInterfaceToken failed");
91 
92     auto devices = deviceChangeAction.deviceDescriptors;
93     size_t size = deviceChangeAction.deviceDescriptors.size();
94 
95     bool token = data.WriteInt32(usage) && data.WriteInt32(deviceChangeAction.type) &&
96         data.WriteInt32(deviceChangeAction.flag) && data.WriteInt32(static_cast<int32_t>(size));
97     CHECK_AND_RETURN_LOG(token, "Write data failed");
98 
99     for (size_t i = 0; i < size; i++) {
100         devices[i]->Marshalling(data);
101     }
102 
103     int error = Remote()->SendRequest(ON_AVAILABLE_DEVICE_CAHNGE, data, reply, option);
104     CHECK_AND_RETURN_LOG(error == ERR_NONE, "OnAvailableDeviceChange failed, error: %{public}d", error);
105 }
106 
OnQueryClientType(const std::string & bundleName,uint32_t uid)107 bool AudioPolicyManagerListenerProxy::OnQueryClientType(const std::string &bundleName, uint32_t uid)
108 {
109     AUDIO_DEBUG_LOG("In");
110 
111     MessageParcel data;
112     MessageParcel reply;
113     MessageOption option;
114 
115     CHECK_AND_RETURN_RET_LOG(data.WriteInterfaceToken(GetDescriptor()), false,
116         "AudioPolicyManagerListenerProxy: WriteInterfaceToken failed");
117     data.WriteString(bundleName);
118     data.WriteUint32(uid);
119 
120     int error = Remote()->SendRequest(ON_QUERY_CLIENT_TYPE, data, reply, option);
121     CHECK_AND_RETURN_RET_LOG(error == ERR_NONE, false, "failed, error: %{public}d", error);
122     return reply.ReadBool();
123 }
124 } // namespace AudioStandard
125 } // namespace OHOS
126