1 /* 2 * Copyright (C) 2016 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 package com.android.car; 17 18 import android.car.media.CarAudioManager; 19 import android.media.AudioAttributes; 20 import android.os.Bundle; 21 22 /** 23 * Utility class to map car usage into AudioAttributes and the other way around. 24 */ 25 public class CarAudioAttributesUtil { 26 27 public static final int CAR_AUDIO_USAGE_CARSERVICE_BOTTOM = 100; 28 public static final int CAR_AUDIO_USAGE_CARSERVICE_CAR_PROXY = 101; 29 30 /** Bundle key for storing media type */ 31 public static final String KEY_CAR_AUDIO_TYPE = "car_audio_type"; 32 public static final int CAR_AUDIO_TYPE_DEFAULT = 0; 33 public static final int CAR_AUDIO_TYPE_VOICE_COMMAND = 1; 34 public static final int CAR_AUDIO_TYPE_SAFETY_ALERT = 2; 35 public static final int CAR_AUDIO_TYPE_RADIO = 3; 36 public static final int CAR_AUDIO_TYPE_CARSERVICE_BOTTOM = 4; 37 public static final int CAR_AUDIO_TYPE_CARSERVICE_CAR_PROXY = 5; 38 getAudioAttributesForCarUsage(int carUsage)39 public static AudioAttributes getAudioAttributesForCarUsage(int carUsage) { 40 switch (carUsage) { 41 case CarAudioManager.CAR_AUDIO_USAGE_MUSIC: 42 return createAudioAttributes(AudioAttributes.CONTENT_TYPE_MUSIC, 43 AudioAttributes.USAGE_MEDIA); 44 case CarAudioManager.CAR_AUDIO_USAGE_RADIO: 45 return createCustomAudioAttributes(CAR_AUDIO_TYPE_RADIO, 46 AudioAttributes.CONTENT_TYPE_MUSIC, AudioAttributes.USAGE_MEDIA); 47 case CarAudioManager.CAR_AUDIO_USAGE_NAVIGATION_GUIDANCE: 48 return createAudioAttributes(AudioAttributes.CONTENT_TYPE_SPEECH, 49 AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE); 50 case CarAudioManager.CAR_AUDIO_USAGE_VOICE_CALL: 51 return createAudioAttributes(AudioAttributes.CONTENT_TYPE_SPEECH, 52 AudioAttributes.USAGE_VOICE_COMMUNICATION); 53 case CarAudioManager.CAR_AUDIO_USAGE_VOICE_COMMAND: 54 return createCustomAudioAttributes(CAR_AUDIO_TYPE_VOICE_COMMAND, 55 AudioAttributes.CONTENT_TYPE_SPEECH, 56 AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE); 57 case CarAudioManager.CAR_AUDIO_USAGE_ALARM: 58 return createAudioAttributes(AudioAttributes.CONTENT_TYPE_SONIFICATION, 59 AudioAttributes.USAGE_ALARM); 60 case CarAudioManager.CAR_AUDIO_USAGE_NOTIFICATION: 61 return createAudioAttributes(AudioAttributes.CONTENT_TYPE_SONIFICATION, 62 AudioAttributes.USAGE_NOTIFICATION); 63 case CarAudioManager.CAR_AUDIO_USAGE_SYSTEM_SOUND: 64 return createAudioAttributes(AudioAttributes.CONTENT_TYPE_SONIFICATION, 65 AudioAttributes.USAGE_ASSISTANCE_SONIFICATION); 66 case CarAudioManager.CAR_AUDIO_USAGE_SYSTEM_SAFETY_ALERT: 67 return createCustomAudioAttributes(CAR_AUDIO_TYPE_SAFETY_ALERT, 68 AudioAttributes.CONTENT_TYPE_SONIFICATION, 69 AudioAttributes.USAGE_NOTIFICATION); 70 case CAR_AUDIO_USAGE_CARSERVICE_BOTTOM: 71 return createCustomAudioAttributes(CAR_AUDIO_TYPE_CARSERVICE_BOTTOM, 72 AudioAttributes.CONTENT_TYPE_UNKNOWN, 73 AudioAttributes.USAGE_UNKNOWN); 74 case CAR_AUDIO_USAGE_CARSERVICE_CAR_PROXY: 75 return createCustomAudioAttributes(CAR_AUDIO_TYPE_CARSERVICE_CAR_PROXY, 76 AudioAttributes.CONTENT_TYPE_UNKNOWN, 77 AudioAttributes.USAGE_UNKNOWN); 78 case CarAudioManager.CAR_AUDIO_USAGE_DEFAULT: 79 default: 80 return createAudioAttributes(AudioAttributes.CONTENT_TYPE_UNKNOWN, 81 AudioAttributes.USAGE_UNKNOWN); 82 } 83 } 84 getCarUsageFromAudioAttributes(AudioAttributes attr)85 public static int getCarUsageFromAudioAttributes(AudioAttributes attr) { 86 int usage = attr.getUsage(); 87 Bundle bundle = attr.getBundle(); 88 int type = CAR_AUDIO_TYPE_DEFAULT; 89 if (bundle != null) { 90 type = bundle.getInt(KEY_CAR_AUDIO_TYPE, CAR_AUDIO_TYPE_DEFAULT); 91 } 92 switch (usage) { 93 case AudioAttributes.USAGE_MEDIA: 94 if (type == CAR_AUDIO_TYPE_RADIO) { 95 return CarAudioManager.CAR_AUDIO_USAGE_RADIO; 96 } else { 97 return CarAudioManager.CAR_AUDIO_USAGE_MUSIC; 98 } 99 case AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: 100 if (type == CAR_AUDIO_TYPE_VOICE_COMMAND) { 101 return CarAudioManager.CAR_AUDIO_USAGE_VOICE_COMMAND; 102 } else { 103 return CarAudioManager.CAR_AUDIO_USAGE_NAVIGATION_GUIDANCE; 104 } 105 case AudioAttributes.USAGE_VOICE_COMMUNICATION: 106 return CarAudioManager.CAR_AUDIO_USAGE_VOICE_CALL; 107 case AudioAttributes.USAGE_ALARM: 108 return CarAudioManager.CAR_AUDIO_USAGE_ALARM; 109 case AudioAttributes.USAGE_NOTIFICATION: 110 if (type == CAR_AUDIO_TYPE_SAFETY_ALERT) { 111 return CarAudioManager.CAR_AUDIO_USAGE_SYSTEM_SAFETY_ALERT; 112 } else { 113 return CarAudioManager.CAR_AUDIO_USAGE_NOTIFICATION; 114 } 115 case AudioAttributes.USAGE_ASSISTANCE_SONIFICATION: 116 return CarAudioManager.CAR_AUDIO_USAGE_SYSTEM_SOUND; 117 case AudioAttributes.USAGE_UNKNOWN: 118 default: 119 if (type == CAR_AUDIO_TYPE_CARSERVICE_BOTTOM) { 120 return CAR_AUDIO_USAGE_CARSERVICE_BOTTOM; 121 } else if (type == CAR_AUDIO_TYPE_CARSERVICE_CAR_PROXY) { 122 return CAR_AUDIO_USAGE_CARSERVICE_CAR_PROXY; 123 } 124 return CarAudioManager.CAR_AUDIO_USAGE_DEFAULT; 125 } 126 } 127 createAudioAttributes(int contentType, int usage)128 private static AudioAttributes createAudioAttributes(int contentType, int usage) { 129 AudioAttributes.Builder builder = new AudioAttributes.Builder(); 130 return builder.setContentType(contentType).setUsage(usage).build(); 131 } 132 createCustomAudioAttributes(int carAudioType, int contentType, int usage)133 private static AudioAttributes createCustomAudioAttributes(int carAudioType, 134 int contentType, int usage) { 135 AudioAttributes.Builder builder = new AudioAttributes.Builder(); 136 Bundle bundle = new Bundle(); 137 bundle.putInt(KEY_CAR_AUDIO_TYPE, carAudioType); 138 return builder.setContentType(contentType).setUsage(usage).addBundle(bundle).build(); 139 } 140 } 141