1 /* 2 * Copyright (C) 2023 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.server.media; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.media.AudioAttributes; 22 import android.media.AudioDeviceAttributes; 23 import android.media.AudioDeviceInfo; 24 import android.media.MediaRoute2Info; 25 26 /* package */ final class AudioAttributesUtils { 27 28 /* package */ static final AudioAttributes ATTRIBUTES_MEDIA = new AudioAttributes.Builder() 29 .setUsage(AudioAttributes.USAGE_MEDIA) 30 .build(); 31 AudioAttributesUtils()32 private AudioAttributesUtils() { 33 // no-op to prevent instantiation. 34 } 35 36 @MediaRoute2Info.Type mapToMediaRouteType( @onNull AudioDeviceAttributes audioDeviceAttributes)37 /* package */ static int mapToMediaRouteType( 38 @NonNull AudioDeviceAttributes audioDeviceAttributes) { 39 switch (audioDeviceAttributes.getType()) { 40 case AudioDeviceInfo.TYPE_BUILTIN_EARPIECE: 41 case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER: 42 return MediaRoute2Info.TYPE_BUILTIN_SPEAKER; 43 case AudioDeviceInfo.TYPE_WIRED_HEADSET: 44 return MediaRoute2Info.TYPE_WIRED_HEADSET; 45 case AudioDeviceInfo.TYPE_WIRED_HEADPHONES: 46 return MediaRoute2Info.TYPE_WIRED_HEADPHONES; 47 case AudioDeviceInfo.TYPE_DOCK: 48 case AudioDeviceInfo.TYPE_DOCK_ANALOG: 49 return MediaRoute2Info.TYPE_DOCK; 50 case AudioDeviceInfo.TYPE_HDMI: 51 return MediaRoute2Info.TYPE_HDMI; 52 case AudioDeviceInfo.TYPE_USB_DEVICE: 53 return MediaRoute2Info.TYPE_USB_DEVICE; 54 case AudioDeviceInfo.TYPE_BLUETOOTH_A2DP: 55 return MediaRoute2Info.TYPE_BLUETOOTH_A2DP; 56 case AudioDeviceInfo.TYPE_BLE_HEADSET: 57 return MediaRoute2Info.TYPE_BLE_HEADSET; 58 case AudioDeviceInfo.TYPE_HEARING_AID: 59 return MediaRoute2Info.TYPE_HEARING_AID; 60 default: 61 return MediaRoute2Info.TYPE_UNKNOWN; 62 } 63 } 64 65 isDeviceOutputAttributes( @ullable AudioDeviceAttributes audioDeviceAttributes)66 /* package */ static boolean isDeviceOutputAttributes( 67 @Nullable AudioDeviceAttributes audioDeviceAttributes) { 68 if (audioDeviceAttributes == null) { 69 return false; 70 } 71 72 if (audioDeviceAttributes.getRole() != AudioDeviceAttributes.ROLE_OUTPUT) { 73 return false; 74 } 75 76 switch (audioDeviceAttributes.getType()) { 77 case AudioDeviceInfo.TYPE_BUILTIN_EARPIECE: 78 case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER: 79 case AudioDeviceInfo.TYPE_WIRED_HEADSET: 80 case AudioDeviceInfo.TYPE_WIRED_HEADPHONES: 81 case AudioDeviceInfo.TYPE_DOCK: 82 case AudioDeviceInfo.TYPE_DOCK_ANALOG: 83 case AudioDeviceInfo.TYPE_HDMI: 84 case AudioDeviceInfo.TYPE_USB_DEVICE: 85 return true; 86 default: 87 return false; 88 } 89 } 90 isBluetoothOutputAttributes( @ullable AudioDeviceAttributes audioDeviceAttributes)91 /* package */ static boolean isBluetoothOutputAttributes( 92 @Nullable AudioDeviceAttributes audioDeviceAttributes) { 93 if (audioDeviceAttributes == null) { 94 return false; 95 } 96 97 if (audioDeviceAttributes.getRole() != AudioDeviceAttributes.ROLE_OUTPUT) { 98 return false; 99 } 100 101 switch (audioDeviceAttributes.getType()) { 102 case AudioDeviceInfo.TYPE_BLUETOOTH_A2DP: 103 case AudioDeviceInfo.TYPE_BLE_HEADSET: 104 case AudioDeviceInfo.TYPE_BLE_SPEAKER: 105 case AudioDeviceInfo.TYPE_HEARING_AID: 106 return true; 107 default: 108 return false; 109 } 110 } 111 112 } 113