1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.car.audio;
18 
19 
20 import static android.media.AudioAttributes.USAGE_MEDIA;
21 
22 import static com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport.BOILERPLATE_CODE;
23 
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26 
27 import android.media.AudioAttributes;
28 import android.media.AudioDeviceInfo;
29 import android.media.AudioPlaybackConfiguration;
30 
31 import com.android.car.internal.ExcludeFromCodeCoverageGeneratedReport;
32 
33 @ExcludeFromCodeCoverageGeneratedReport(reason = BOILERPLATE_CODE)
34 final class AudioPlaybackConfigurationBuilder {
35     private @AudioAttributes.AttributeUsage int mUsage = USAGE_MEDIA;
36     private boolean mIsActive = true;
37     private String mDeviceAddress = "";
38     private int mClientUid = 0;
39 
setUsage(@udioAttributes.AttributeUsage int usage)40     AudioPlaybackConfigurationBuilder setUsage(@AudioAttributes.AttributeUsage int usage) {
41         mUsage = usage;
42         return this;
43     }
44 
setDeviceAddress(String deviceAddress)45     AudioPlaybackConfigurationBuilder setDeviceAddress(String deviceAddress) {
46         mDeviceAddress = deviceAddress;
47         return this;
48     }
49 
setInactive()50     AudioPlaybackConfigurationBuilder setInactive() {
51         mIsActive = false;
52         return this;
53     }
54 
setClientUid(int clientUid)55     AudioPlaybackConfigurationBuilder setClientUid(int clientUid) {
56         mClientUid = clientUid;
57         return this;
58     }
59 
build()60     AudioPlaybackConfiguration build() {
61         AudioPlaybackConfiguration configuration = mock(AudioPlaybackConfiguration.class);
62         AudioAttributes attributes = new AudioAttributes.Builder().setUsage(mUsage).build();
63         AudioDeviceInfo outputDevice = generateOutAudioDeviceInfo(mDeviceAddress);
64         when(configuration.getAudioAttributes()).thenReturn(attributes);
65         when(configuration.getAudioDeviceInfo()).thenReturn(outputDevice);
66         when(configuration.isActive()).thenReturn(mIsActive);
67         when(configuration.getClientUid()).thenReturn(mClientUid);
68         return configuration;
69     }
70 
generateOutAudioDeviceInfo(String address)71     private AudioDeviceInfo generateOutAudioDeviceInfo(String address) {
72         AudioDeviceInfo audioDeviceInfo = mock(AudioDeviceInfo.class);
73         when(audioDeviceInfo.getAddress()).thenReturn(address);
74         when(audioDeviceInfo.getType()).thenReturn(AudioDeviceInfo.TYPE_BUS);
75         when(audioDeviceInfo.isSource()).thenReturn(false);
76         when(audioDeviceInfo.isSink()).thenReturn(true);
77         when(audioDeviceInfo.getInternalType()).thenReturn(AudioDeviceInfo
78                 .convertDeviceTypeToInternalInputDevice(AudioDeviceInfo.TYPE_BUS));
79         return audioDeviceInfo;
80     }
81 }
82