Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 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 | |
Keun-young Park | e54ac27 | 2016-02-16 19:02:18 -0800 | [diff] [blame] | 18 | import android.car.media.CarAudioManager; |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 19 | import android.media.AudioAttributes; |
| 20 | import android.os.Bundle; |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 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; |
Keun-young Park | 3cb8910 | 2016-05-05 13:16:03 -0700 | [diff] [blame] | 29 | public static final int CAR_AUDIO_USAGE_CARSERVICE_MEDIA_MUTE = 102; |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 30 | |
| 31 | /** Bundle key for storing media type */ |
| 32 | public static final String KEY_CAR_AUDIO_TYPE = "car_audio_type"; |
Keun-young Park | 3cb8910 | 2016-05-05 13:16:03 -0700 | [diff] [blame] | 33 | |
| 34 | private static final int CAR_AUDIO_TYPE_DEFAULT = 0; |
| 35 | private static final int CAR_AUDIO_TYPE_VOICE_COMMAND = 1; |
| 36 | private static final int CAR_AUDIO_TYPE_SAFETY_ALERT = 2; |
| 37 | private static final int CAR_AUDIO_TYPE_RADIO = 3; |
| 38 | private static final int CAR_AUDIO_TYPE_CARSERVICE_BOTTOM = 4; |
| 39 | private static final int CAR_AUDIO_TYPE_CARSERVICE_CAR_PROXY = 5; |
| 40 | private static final int CAR_AUDIO_TYPE_CARSERVICE_MEDIA_MUTE = 6; |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 41 | |
| 42 | public static AudioAttributes getAudioAttributesForCarUsage(int carUsage) { |
| 43 | switch (carUsage) { |
| 44 | case CarAudioManager.CAR_AUDIO_USAGE_MUSIC: |
| 45 | return createAudioAttributes(AudioAttributes.CONTENT_TYPE_MUSIC, |
| 46 | AudioAttributes.USAGE_MEDIA); |
| 47 | case CarAudioManager.CAR_AUDIO_USAGE_RADIO: |
| 48 | return createCustomAudioAttributes(CAR_AUDIO_TYPE_RADIO, |
| 49 | AudioAttributes.CONTENT_TYPE_MUSIC, AudioAttributes.USAGE_MEDIA); |
| 50 | case CarAudioManager.CAR_AUDIO_USAGE_NAVIGATION_GUIDANCE: |
| 51 | return createAudioAttributes(AudioAttributes.CONTENT_TYPE_SPEECH, |
| 52 | AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE); |
| 53 | case CarAudioManager.CAR_AUDIO_USAGE_VOICE_CALL: |
| 54 | return createAudioAttributes(AudioAttributes.CONTENT_TYPE_SPEECH, |
| 55 | AudioAttributes.USAGE_VOICE_COMMUNICATION); |
| 56 | case CarAudioManager.CAR_AUDIO_USAGE_VOICE_COMMAND: |
| 57 | return createCustomAudioAttributes(CAR_AUDIO_TYPE_VOICE_COMMAND, |
| 58 | AudioAttributes.CONTENT_TYPE_SPEECH, |
| 59 | AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE); |
| 60 | case CarAudioManager.CAR_AUDIO_USAGE_ALARM: |
| 61 | return createAudioAttributes(AudioAttributes.CONTENT_TYPE_SONIFICATION, |
| 62 | AudioAttributes.USAGE_ALARM); |
| 63 | case CarAudioManager.CAR_AUDIO_USAGE_NOTIFICATION: |
| 64 | return createAudioAttributes(AudioAttributes.CONTENT_TYPE_SONIFICATION, |
| 65 | AudioAttributes.USAGE_NOTIFICATION); |
| 66 | case CarAudioManager.CAR_AUDIO_USAGE_SYSTEM_SOUND: |
| 67 | return createAudioAttributes(AudioAttributes.CONTENT_TYPE_SONIFICATION, |
| 68 | AudioAttributes.USAGE_ASSISTANCE_SONIFICATION); |
| 69 | case CarAudioManager.CAR_AUDIO_USAGE_SYSTEM_SAFETY_ALERT: |
| 70 | return createCustomAudioAttributes(CAR_AUDIO_TYPE_SAFETY_ALERT, |
| 71 | AudioAttributes.CONTENT_TYPE_SONIFICATION, |
| 72 | AudioAttributes.USAGE_NOTIFICATION); |
| 73 | case CAR_AUDIO_USAGE_CARSERVICE_BOTTOM: |
| 74 | return createCustomAudioAttributes(CAR_AUDIO_TYPE_CARSERVICE_BOTTOM, |
| 75 | AudioAttributes.CONTENT_TYPE_UNKNOWN, |
| 76 | AudioAttributes.USAGE_UNKNOWN); |
| 77 | case CAR_AUDIO_USAGE_CARSERVICE_CAR_PROXY: |
| 78 | return createCustomAudioAttributes(CAR_AUDIO_TYPE_CARSERVICE_CAR_PROXY, |
| 79 | AudioAttributes.CONTENT_TYPE_UNKNOWN, |
| 80 | AudioAttributes.USAGE_UNKNOWN); |
Keun-young Park | 3cb8910 | 2016-05-05 13:16:03 -0700 | [diff] [blame] | 81 | case CAR_AUDIO_USAGE_CARSERVICE_MEDIA_MUTE: |
| 82 | return createCustomAudioAttributes(CAR_AUDIO_TYPE_CARSERVICE_MEDIA_MUTE, |
| 83 | AudioAttributes.CONTENT_TYPE_MUSIC, |
| 84 | AudioAttributes.USAGE_UNKNOWN); |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 85 | case CarAudioManager.CAR_AUDIO_USAGE_DEFAULT: |
| 86 | default: |
| 87 | return createAudioAttributes(AudioAttributes.CONTENT_TYPE_UNKNOWN, |
| 88 | AudioAttributes.USAGE_UNKNOWN); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public static int getCarUsageFromAudioAttributes(AudioAttributes attr) { |
| 93 | int usage = attr.getUsage(); |
| 94 | Bundle bundle = attr.getBundle(); |
| 95 | int type = CAR_AUDIO_TYPE_DEFAULT; |
| 96 | if (bundle != null) { |
| 97 | type = bundle.getInt(KEY_CAR_AUDIO_TYPE, CAR_AUDIO_TYPE_DEFAULT); |
| 98 | } |
| 99 | switch (usage) { |
| 100 | case AudioAttributes.USAGE_MEDIA: |
| 101 | if (type == CAR_AUDIO_TYPE_RADIO) { |
| 102 | return CarAudioManager.CAR_AUDIO_USAGE_RADIO; |
| 103 | } else { |
| 104 | return CarAudioManager.CAR_AUDIO_USAGE_MUSIC; |
| 105 | } |
| 106 | case AudioAttributes.USAGE_ASSISTANCE_NAVIGATION_GUIDANCE: |
| 107 | if (type == CAR_AUDIO_TYPE_VOICE_COMMAND) { |
| 108 | return CarAudioManager.CAR_AUDIO_USAGE_VOICE_COMMAND; |
| 109 | } else { |
| 110 | return CarAudioManager.CAR_AUDIO_USAGE_NAVIGATION_GUIDANCE; |
| 111 | } |
| 112 | case AudioAttributes.USAGE_VOICE_COMMUNICATION: |
| 113 | return CarAudioManager.CAR_AUDIO_USAGE_VOICE_CALL; |
| 114 | case AudioAttributes.USAGE_ALARM: |
| 115 | return CarAudioManager.CAR_AUDIO_USAGE_ALARM; |
| 116 | case AudioAttributes.USAGE_NOTIFICATION: |
| 117 | if (type == CAR_AUDIO_TYPE_SAFETY_ALERT) { |
| 118 | return CarAudioManager.CAR_AUDIO_USAGE_SYSTEM_SAFETY_ALERT; |
| 119 | } else { |
| 120 | return CarAudioManager.CAR_AUDIO_USAGE_NOTIFICATION; |
| 121 | } |
| 122 | case AudioAttributes.USAGE_ASSISTANCE_SONIFICATION: |
| 123 | return CarAudioManager.CAR_AUDIO_USAGE_SYSTEM_SOUND; |
| 124 | case AudioAttributes.USAGE_UNKNOWN: |
Keun-young Park | 3cb8910 | 2016-05-05 13:16:03 -0700 | [diff] [blame] | 125 | default: { |
| 126 | switch (type) { |
| 127 | case CAR_AUDIO_TYPE_CARSERVICE_BOTTOM: |
| 128 | return CAR_AUDIO_USAGE_CARSERVICE_BOTTOM; |
| 129 | case CAR_AUDIO_TYPE_CARSERVICE_CAR_PROXY: |
| 130 | return CAR_AUDIO_USAGE_CARSERVICE_CAR_PROXY; |
| 131 | case CAR_AUDIO_TYPE_CARSERVICE_MEDIA_MUTE: |
| 132 | return CAR_AUDIO_USAGE_CARSERVICE_MEDIA_MUTE; |
| 133 | default: |
| 134 | return CarAudioManager.CAR_AUDIO_USAGE_DEFAULT; |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 135 | } |
Keun-young Park | 3cb8910 | 2016-05-05 13:16:03 -0700 | [diff] [blame] | 136 | } |
Keun-young Park | 5672e85 | 2016-02-09 19:53:48 -0800 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | private static AudioAttributes createAudioAttributes(int contentType, int usage) { |
| 141 | AudioAttributes.Builder builder = new AudioAttributes.Builder(); |
| 142 | return builder.setContentType(contentType).setUsage(usage).build(); |
| 143 | } |
| 144 | |
| 145 | private static AudioAttributes createCustomAudioAttributes(int carAudioType, |
| 146 | int contentType, int usage) { |
| 147 | AudioAttributes.Builder builder = new AudioAttributes.Builder(); |
| 148 | Bundle bundle = new Bundle(); |
| 149 | bundle.putInt(KEY_CAR_AUDIO_TYPE, carAudioType); |
| 150 | return builder.setContentType(contentType).setUsage(usage).addBundle(bundle).build(); |
| 151 | } |
| 152 | } |