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 
getDeviceOutGroup(audio_devices_t device) const58 device_out_group_t LastRemovableMediaDevices::getDeviceOutGroup(audio_devices_t device) const
59 {
60     switch (device) {
61     case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
62     case AUDIO_DEVICE_OUT_LINE:
63     case AUDIO_DEVICE_OUT_WIRED_HEADSET:
64     case AUDIO_DEVICE_OUT_USB_HEADSET:
65     case AUDIO_DEVICE_OUT_USB_ACCESSORY:
66     case AUDIO_DEVICE_OUT_USB_DEVICE:
67     case AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET:
68         return GROUP_WIRED;
69     case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
70     case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
71     case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
72         return GROUP_BT_A2DP;
73     default:
74         return GROUP_NONE;
75     }
76 }
77 
78 } // namespace android
79