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) const46 std::vector<audio_devices_t> LastRemovableMediaDevices::getLastRemovableMediaDevices(
47 device_out_group_t group) const
48 {
49 std::vector<audio_devices_t> ret;
50 for (auto iter = mMediaDevices.begin(); iter != mMediaDevices.end(); ++iter) {
51 if ((group == GROUP_NONE) || (group == getDeviceOutGroup((iter->desc)->type()))) {
52 ret.push_back((iter->desc)->type());
53 }
54 }
55 return ret;
56 }
57
getLastRemovableMediaDevice(const DeviceVector & excludedDevices,device_out_group_t group) const58 sp<DeviceDescriptor> LastRemovableMediaDevices::getLastRemovableMediaDevice(
59 const DeviceVector& excludedDevices, device_out_group_t group) const {
60 for (auto iter = mMediaDevices.begin(); iter != mMediaDevices.end(); ++iter) {
61 if ((group == GROUP_NONE || group == getDeviceOutGroup((iter->desc)->type())) &&
62 !excludedDevices.contains(iter->desc)) {
63 return iter->desc;
64 }
65 }
66 return nullptr;
67 }
68
getDeviceOutGroup(audio_devices_t device) const69 device_out_group_t LastRemovableMediaDevices::getDeviceOutGroup(audio_devices_t device) const
70 {
71 switch (device) {
72 case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
73 case AUDIO_DEVICE_OUT_LINE:
74 case AUDIO_DEVICE_OUT_WIRED_HEADSET:
75 case AUDIO_DEVICE_OUT_USB_HEADSET:
76 case AUDIO_DEVICE_OUT_USB_ACCESSORY:
77 case AUDIO_DEVICE_OUT_USB_DEVICE:
78 case AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET:
79 return GROUP_WIRED;
80 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
81 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
82 case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
83 // TODO (b/122931261): remove when preferred device for strategy media will be used instead of
84 // AUDIO_POLICY_FORCE_NO_BT_A2DP.
85 case AUDIO_DEVICE_OUT_HEARING_AID:
86 case AUDIO_DEVICE_OUT_BLE_HEADSET:
87 case AUDIO_DEVICE_OUT_BLE_SPEAKER:
88 return GROUP_BT_A2DP;
89 default:
90 return GROUP_NONE;
91 }
92 }
93
94 } // namespace android
95