1 /*
2  * Copyright (C) 2015 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 android.car.media;
17 
18 import android.annotation.IntDef;
19 import android.content.Context;
20 import android.media.AudioAttributes;
21 import android.media.AudioManager;
22 import android.media.AudioManager.OnAudioFocusChangeListener;
23 import android.os.IBinder;
24 import android.os.RemoteException;
25 import android.car.CarManagerBase;
26 
27 import java.lang.annotation.Retention;
28 import java.lang.annotation.RetentionPolicy;
29 
30 /**
31  * APIs for handling car specific audio stuffs.
32  */
33 public class CarAudioManager implements CarManagerBase {
34 
35     /**
36      * Audio usage for unspecified type.
37      */
38     public static final int CAR_AUDIO_USAGE_DEFAULT = 0;
39     /**
40      * Audio usage for playing music.
41      */
42     public static final int CAR_AUDIO_USAGE_MUSIC = 1;
43     /**
44      * Audio usage for H/W radio.
45      */
46     public static final int CAR_AUDIO_USAGE_RADIO = 2;
47     /**
48      * Audio usage for playing navigation guidance.
49      */
50     public static final int CAR_AUDIO_USAGE_NAVIGATION_GUIDANCE = 3;
51     /**
52      * Audio usage for voice call
53      */
54     public static final int CAR_AUDIO_USAGE_VOICE_CALL = 4;
55     /**
56      * Audio usage for voice search or voice command.
57      */
58     public static final int CAR_AUDIO_USAGE_VOICE_COMMAND = 5;
59     /**
60      * Audio usage for playing alarm.
61      */
62     public static final int CAR_AUDIO_USAGE_ALARM = 6;
63     /**
64      * Audio usage for notification sound.
65      */
66     public static final int CAR_AUDIO_USAGE_NOTIFICATION = 7;
67     /**
68      * Audio usage for system sound like UI feedback.
69      */
70     public static final int CAR_AUDIO_USAGE_SYSTEM_SOUND = 8;
71     /**
72      * Audio usage for playing safety alert.
73      */
74     public static final int CAR_AUDIO_USAGE_SYSTEM_SAFETY_ALERT = 9;
75 
76     /** @hide */
77     public static final int CAR_AUDIO_USAGE_MAX = CAR_AUDIO_USAGE_SYSTEM_SAFETY_ALERT;
78 
79     /** @hide */
80     @IntDef({CAR_AUDIO_USAGE_DEFAULT, CAR_AUDIO_USAGE_MUSIC, CAR_AUDIO_USAGE_RADIO,
81         CAR_AUDIO_USAGE_NAVIGATION_GUIDANCE, CAR_AUDIO_USAGE_VOICE_CALL,
82         CAR_AUDIO_USAGE_VOICE_COMMAND, CAR_AUDIO_USAGE_ALARM, CAR_AUDIO_USAGE_NOTIFICATION,
83         CAR_AUDIO_USAGE_SYSTEM_SOUND, CAR_AUDIO_USAGE_SYSTEM_SAFETY_ALERT})
84     @Retention(RetentionPolicy.SOURCE)
85     public @interface CarAudioUsage {}
86 
87     private final ICarAudio mService;
88     private final AudioManager mAudioManager;
89 
90     /**
91      * Get {@link AudioAttributes} relevant for the given usage in car.
92      * @param carUsage
93      * @return
94      */
getAudioAttributesForCarUsage(@arAudioUsage int carUsage)95     public AudioAttributes getAudioAttributesForCarUsage(@CarAudioUsage int carUsage) {
96         try {
97             return mService.getAudioAttributesForCarUsage(carUsage);
98         } catch (RemoteException e) {
99             AudioAttributes.Builder builder = new AudioAttributes.Builder();
100             return builder.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN).
101                     setUsage(AudioAttributes.USAGE_UNKNOWN).build();
102         }
103     }
104 
105     /**
106      * Request audio focus.
107      * Send a request to obtain the audio focus.
108      * @param l
109      * @param requestAttributes
110      * @param durationHint
111      * @param flags
112      */
requestAudioFocus(OnAudioFocusChangeListener l, AudioAttributes requestAttributes, int durationHint, int flags)113     public int requestAudioFocus(OnAudioFocusChangeListener l,
114                                  AudioAttributes requestAttributes,
115                                  int durationHint,
116                                  int flags) throws IllegalArgumentException {
117         return mAudioManager.requestAudioFocus(l, requestAttributes, durationHint, flags);
118     }
119 
120     /**
121      * Abandon audio focus. Causes the previous focus owner, if any, to receive focus.
122      * @param l
123      * @param aa
124      * @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED}
125      */
abandonAudioFocus(OnAudioFocusChangeListener l, AudioAttributes aa)126     public int abandonAudioFocus(OnAudioFocusChangeListener l, AudioAttributes aa) {
127         return mAudioManager.abandonAudioFocus(l, aa);
128     }
129 
130     @Override
onCarDisconnected()131     public void onCarDisconnected() {
132         // TODO Auto-generated method stub
133     }
134 
135     /** @hide */
CarAudioManager(IBinder service, Context context)136     public CarAudioManager(IBinder service, Context context) {
137         mService = ICarAudio.Stub.asInterface(service);
138         mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
139     }
140 }
141