1 /*
2 * Copyright (C) 2019 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 #define LOG_TAG "APM::AudioPolicyEngine/LastRemovableMediaDevices"
18 //#define LOG_NDEBUG 0
19
20 #include "LastRemovableMediaDevices.h"
21 #include <log/log.h>
22
23 namespace android {
24
setRemovableMediaDevices(sp<DeviceDescriptor> desc,audio_policy_dev_state_t state)25 void LastRemovableMediaDevices::setRemovableMediaDevices(sp<DeviceDescriptor> desc,
26 audio_policy_dev_state_t state)
27 {
28 if (desc == nullptr) {
29 return;
30 } else {
31 if ((state == AUDIO_POLICY_DEVICE_STATE_AVAILABLE) &&
32 (getDeviceOutGroup(desc->type()) != GROUP_NONE)) {
33 setRemovableMediaDevices(desc, AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE);
34 mMediaDevices.insert(mMediaDevices.begin(), {desc, getDeviceOutGroup(desc->type())});
35 } else if (state == AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
36 for (auto iter = mMediaDevices.begin(); iter != mMediaDevices.end(); ++iter) {
37 if ((iter->desc)->equals(desc)) {
38 mMediaDevices.erase(iter);
39 break;
40 }
41 }
42 }
43 }
44 }
45
getLastRemovableMediaDevices(device_out_group_t group,std::vector<audio_devices_t> excludedDevices) const46 std::vector<audio_devices_t> LastRemovableMediaDevices::getLastRemovableMediaDevices(
47 device_out_group_t group, std::vector<audio_devices_t> excludedDevices) const
48 {
49 std::vector<audio_devices_t> ret;
50 for (auto iter = mMediaDevices.begin(); iter != mMediaDevices.end(); ++iter) {
51 audio_devices_t type = (iter->desc)->type();
52 if ((group == GROUP_NONE || group == getDeviceOutGroup(type))
53 && std::find(excludedDevices.begin(), excludedDevices.end(), type) ==
54 excludedDevices.end()) {
55 ret.push_back(type);
56 }
57 }
58 return ret;
59 }
60
getLastRemovableMediaDevice(const DeviceVector & excludedDevices,device_out_group_t group) const61 sp<DeviceDescriptor> LastRemovableMediaDevices::getLastRemovableMediaDevice(
62 const DeviceVector& excludedDevices, device_out_group_t group) const {
63 for (auto iter = mMediaDevices.begin(); iter != mMediaDevices.end(); ++iter) {
64 if ((group == GROUP_NONE || group == getDeviceOutGroup((iter->desc)->type())) &&
65 !excludedDevices.contains(iter->desc)) {
66 return iter->desc;
67 }
68 }
69 return nullptr;
70 }
71
getDeviceOutGroup(audio_devices_t device) const72 device_out_group_t LastRemovableMediaDevices::getDeviceOutGroup(audio_devices_t device) const
73 {
74 switch (device) {
75 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
76 case AUDIO_DEVICE_OUT_LINE:
77 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
78 case AUDIO_DEVICE_OUT_USB_HEADSET:
79 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
80 case AUDIO_DEVICE_OUT_USB_DEVICE:
81 case AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET:
82 case AUDIO_DEVICE_OUT_AUX_DIGITAL:
83 return GROUP_WIRED;
84 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
85 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
86 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
87 // TODO (b/122931261): remove when preferred device for strategy media will be used instead of
88 // AUDIO_POLICY_FORCE_NO_BT_A2DP.
89 case AUDIO_DEVICE_OUT_HEARING_AID:
90 case AUDIO_DEVICE_OUT_BLE_HEADSET:
91 case AUDIO_DEVICE_OUT_BLE_SPEAKER:
92 case AUDIO_DEVICE_OUT_BLE_BROADCAST:
93 return GROUP_BT_A2DP;
94 default:
95 return GROUP_NONE;
96 }
97 }
98
99 } // namespace android
100