1 /*
2  * Copyright (C) 2006 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 android.media;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.TestApi;
22 import android.bluetooth.BluetoothCodecConfig;
23 import android.compat.annotation.UnsupportedAppUsage;
24 import android.content.Context;
25 import android.content.pm.PackageManager;
26 import android.media.audiofx.AudioEffect;
27 import android.media.audiopolicy.AudioMix;
28 import android.telephony.TelephonyManager;
29 import android.util.Log;
30 
31 import java.lang.annotation.Retention;
32 import java.lang.annotation.RetentionPolicy;
33 import java.util.ArrayList;
34 import java.util.HashSet;
35 import java.util.Map;
36 import java.util.Objects;
37 import java.util.Set;
38 
39 /* IF YOU CHANGE ANY OF THE CONSTANTS IN THIS FILE, DO NOT FORGET
40  * TO UPDATE THE CORRESPONDING NATIVE GLUE AND AudioManager.java.
41  * THANK YOU FOR YOUR COOPERATION.
42  */
43 
44 /**
45  * @hide
46  */
47 @TestApi
48 public class AudioSystem
49 {
50     private static final boolean DEBUG_VOLUME = false;
51 
52     private static final String TAG = "AudioSystem";
53 
54     // private constructor to prevent instantiating AudioSystem
AudioSystem()55     private AudioSystem() {
56         throw new UnsupportedOperationException("Trying to instantiate AudioSystem");
57     }
58 
59     /* These values must be kept in sync with system/audio.h */
60     /*
61      * If these are modified, please also update Settings.System.VOLUME_SETTINGS
62      * and attrs.xml and AudioManager.java.
63      */
64     /** @hide Used to identify the default audio stream volume */
65     @TestApi
66     public static final int STREAM_DEFAULT = -1;
67     /** @hide Used to identify the volume of audio streams for phone calls */
68     public static final int STREAM_VOICE_CALL = 0;
69     /** @hide Used to identify the volume of audio streams for system sounds */
70     public static final int STREAM_SYSTEM = 1;
71     /** @hide Used to identify the volume of audio streams for the phone ring and message alerts */
72     public static final int STREAM_RING = 2;
73     /** @hide Used to identify the volume of audio streams for music playback */
74     public static final int STREAM_MUSIC = 3;
75     /** @hide Used to identify the volume of audio streams for alarms */
76     public static final int STREAM_ALARM = 4;
77     /** @hide Used to identify the volume of audio streams for notifications */
78     public static final int STREAM_NOTIFICATION = 5;
79     /** @hide
80      *  Used to identify the volume of audio streams for phone calls when connected on bluetooth */
81     public static final int STREAM_BLUETOOTH_SCO = 6;
82     /** @hide Used to identify the volume of audio streams for enforced system sounds in certain
83      * countries (e.g camera in Japan) */
84     @UnsupportedAppUsage
85     public static final int STREAM_SYSTEM_ENFORCED = 7;
86     /** @hide Used to identify the volume of audio streams for DTMF tones */
87     public static final int STREAM_DTMF = 8;
88     /** @hide Used to identify the volume of audio streams exclusively transmitted through the
89      *  speaker (TTS) of the device */
90     public static final int STREAM_TTS = 9;
91     /** @hide Used to identify the volume of audio streams for accessibility prompts */
92     public static final int STREAM_ACCESSIBILITY = 10;
93     /** @hide Used to identify the volume of audio streams for virtual assistant */
94     public static final int STREAM_ASSISTANT = 11;
95     /**
96      * @hide
97      * @deprecated Use {@link #numStreamTypes() instead}
98      */
99     public static final int NUM_STREAMS = 5;
100 
101     /** Maximum value for AudioTrack channel count
102      * @hide public for MediaCode only, do not un-hide or change to a numeric literal
103      */
104     public static final int OUT_CHANNEL_COUNT_MAX = native_get_FCC_8();
native_get_FCC_8()105     private static native int native_get_FCC_8();
106 
107     // Expose only the getter method publicly so we can change it in the future
108     private static final int NUM_STREAM_TYPES = 12;
109 
110     /**
111      * @hide
112      * @return total number of stream types
113      */
114     @UnsupportedAppUsage
115     @TestApi
getNumStreamTypes()116     public static final int getNumStreamTypes() { return NUM_STREAM_TYPES; }
117 
118     /** @hide */
119     public static final String[] STREAM_NAMES = new String[] {
120         "STREAM_VOICE_CALL",
121         "STREAM_SYSTEM",
122         "STREAM_RING",
123         "STREAM_MUSIC",
124         "STREAM_ALARM",
125         "STREAM_NOTIFICATION",
126         "STREAM_BLUETOOTH_SCO",
127         "STREAM_SYSTEM_ENFORCED",
128         "STREAM_DTMF",
129         "STREAM_TTS",
130         "STREAM_ACCESSIBILITY",
131         "STREAM_ASSISTANT"
132     };
133 
134     /**
135      * @hide
136      * Sets the microphone mute on or off.
137      *
138      * @param on set <var>true</var> to mute the microphone;
139      *           <var>false</var> to turn mute off
140      * @return command completion status see AUDIO_STATUS_OK, see AUDIO_STATUS_ERROR
141      */
142     @UnsupportedAppUsage
muteMicrophone(boolean on)143     public static native int muteMicrophone(boolean on);
144 
145     /**
146      * @hide
147      * Checks whether the microphone mute is on or off.
148      *
149      * @return true if microphone is muted, false if it's not
150      */
151     @UnsupportedAppUsage
isMicrophoneMuted()152     public static native boolean isMicrophoneMuted();
153 
154     /* modes for setPhoneState, must match AudioSystem.h audio_mode */
155     /** @hide */
156     public static final int MODE_INVALID            = -2;
157     /** @hide */
158     public static final int MODE_CURRENT            = -1;
159     /** @hide */
160     public static final int MODE_NORMAL             = 0;
161     /** @hide */
162     public static final int MODE_RINGTONE           = 1;
163     /** @hide */
164     public static final int MODE_IN_CALL            = 2;
165     /** @hide */
166     public static final int MODE_IN_COMMUNICATION   = 3;
167     /** @hide */
168     public static final int MODE_CALL_SCREENING     = 4;
169     /** @hide */
170     public static final int NUM_MODES               = 5;
171 
172     /** @hide */
modeToString(int mode)173     public static String modeToString(int mode) {
174         switch (mode) {
175             case MODE_CURRENT: return "MODE_CURRENT";
176             case MODE_IN_CALL: return "MODE_IN_CALL";
177             case MODE_IN_COMMUNICATION: return "MODE_IN_COMMUNICATION";
178             case MODE_INVALID: return "MODE_INVALID";
179             case MODE_NORMAL: return "MODE_NORMAL";
180             case MODE_RINGTONE: return "MODE_RINGTONE";
181             case MODE_CALL_SCREENING: return "MODE_CALL_SCREENING";
182             default: return "unknown mode (" + mode + ")";
183         }
184     }
185 
186     /* Formats for A2DP codecs, must match system/audio-base.h audio_format_t */
187     /** @hide */
188     public static final int AUDIO_FORMAT_INVALID        = 0xFFFFFFFF;
189     /** @hide */
190     public static final int AUDIO_FORMAT_DEFAULT        = 0;
191     /** @hide */
192     public static final int AUDIO_FORMAT_AAC            = 0x04000000;
193     /** @hide */
194     public static final int AUDIO_FORMAT_SBC            = 0x1F000000;
195     /** @hide */
196     public static final int AUDIO_FORMAT_APTX           = 0x20000000;
197     /** @hide */
198     public static final int AUDIO_FORMAT_APTX_HD        = 0x21000000;
199     /** @hide */
200     public static final int AUDIO_FORMAT_LDAC           = 0x23000000;
201 
202     /** @hide */
203     @IntDef(flag = false, prefix = "AUDIO_FORMAT_", value = {
204             AUDIO_FORMAT_INVALID,
205             AUDIO_FORMAT_DEFAULT,
206             AUDIO_FORMAT_AAC,
207             AUDIO_FORMAT_SBC,
208             AUDIO_FORMAT_APTX,
209             AUDIO_FORMAT_APTX_HD,
210             AUDIO_FORMAT_LDAC }
211     )
212     @Retention(RetentionPolicy.SOURCE)
213     public @interface AudioFormatNativeEnumForBtCodec {}
214 
215     /**
216      * @hide
217      * Convert audio format enum values to Bluetooth codec values
218      */
audioFormatToBluetoothSourceCodec( @udioFormatNativeEnumForBtCodec int audioFormat)219     public static int audioFormatToBluetoothSourceCodec(
220             @AudioFormatNativeEnumForBtCodec int audioFormat) {
221         switch (audioFormat) {
222             case AUDIO_FORMAT_AAC: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC;
223             case AUDIO_FORMAT_SBC: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC;
224             case AUDIO_FORMAT_APTX: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX;
225             case AUDIO_FORMAT_APTX_HD: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD;
226             case AUDIO_FORMAT_LDAC: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC;
227             default:
228                 Log.e(TAG, "Unknown audio format 0x" + Integer.toHexString(audioFormat)
229                         + " for conversion to BT codec");
230                 return BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID;
231         }
232     }
233 
234     /**
235      * @hide
236      * Convert a Bluetooth codec to an audio format enum
237      * @param btCodec the codec to convert.
238      * @return the audio format, or {@link #AUDIO_FORMAT_DEFAULT} if unknown
239      */
bluetoothCodecToAudioFormat(int btCodec)240     public static @AudioFormatNativeEnumForBtCodec int bluetoothCodecToAudioFormat(int btCodec) {
241         switch (btCodec) {
242             case BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC:
243                 return AudioSystem.AUDIO_FORMAT_SBC;
244             case BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC:
245                 return AudioSystem.AUDIO_FORMAT_AAC;
246             case BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX:
247                 return AudioSystem.AUDIO_FORMAT_APTX;
248             case BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD:
249                 return AudioSystem.AUDIO_FORMAT_APTX_HD;
250             case BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC:
251                 return AudioSystem.AUDIO_FORMAT_LDAC;
252             default:
253                 Log.e(TAG, "Unknown BT codec 0x" + Integer.toHexString(btCodec)
254                         + " for conversion to audio format");
255                 // TODO returning DEFAULT is the current behavior, should this return INVALID?
256                 return AudioSystem.AUDIO_FORMAT_DEFAULT;
257         }
258     }
259 
260     /**
261      * @hide
262      * Convert a native audio format integer constant to a string.
263      */
audioFormatToString(int audioFormat)264     public static String audioFormatToString(int audioFormat) {
265         switch (audioFormat) {
266             case /* AUDIO_FORMAT_INVALID         */ 0xFFFFFFFF:
267                 return "AUDIO_FORMAT_INVALID";
268             case /* AUDIO_FORMAT_DEFAULT         */ 0:
269                 return "AUDIO_FORMAT_DEFAULT";
270             case /* AUDIO_FORMAT_MP3             */ 0x01000000:
271                 return "AUDIO_FORMAT_MP3";
272             case /* AUDIO_FORMAT_AMR_NB          */ 0x02000000:
273                 return "AUDIO_FORMAT_AMR_NB";
274             case /* AUDIO_FORMAT_AMR_WB          */ 0x03000000:
275                 return "AUDIO_FORMAT_AMR_WB";
276             case /* AUDIO_FORMAT_AAC             */ 0x04000000:
277                 return "AUDIO_FORMAT_AAC";
278             case /* AUDIO_FORMAT_HE_AAC_V1       */ 0x05000000:
279                 return "AUDIO_FORMAT_HE_AAC_V1";
280             case /* AUDIO_FORMAT_HE_AAC_V2       */ 0x06000000:
281                 return "AUDIO_FORMAT_HE_AAC_V2";
282             case /* AUDIO_FORMAT_VORBIS          */ 0x07000000:
283                 return "AUDIO_FORMAT_VORBIS";
284             case /* AUDIO_FORMAT_OPUS            */ 0x08000000:
285                 return "AUDIO_FORMAT_OPUS";
286             case /* AUDIO_FORMAT_AC3             */ 0x09000000:
287                 return "AUDIO_FORMAT_AC3";
288             case /* AUDIO_FORMAT_E_AC3           */ 0x0A000000:
289                 return "AUDIO_FORMAT_E_AC3";
290             case /* AUDIO_FORMAT_DTS             */ 0x0B000000:
291                 return "AUDIO_FORMAT_DTS";
292             case /* AUDIO_FORMAT_DTS_HD          */ 0x0C000000:
293                 return "AUDIO_FORMAT_DTS_HD";
294             case /* AUDIO_FORMAT_IEC61937        */ 0x0D000000:
295                 return "AUDIO_FORMAT_IEC61937";
296             case /* AUDIO_FORMAT_DOLBY_TRUEHD    */ 0x0E000000:
297                 return "AUDIO_FORMAT_DOLBY_TRUEHD";
298             case /* AUDIO_FORMAT_EVRC            */ 0x10000000:
299                 return "AUDIO_FORMAT_EVRC";
300             case /* AUDIO_FORMAT_EVRCB           */ 0x11000000:
301                 return "AUDIO_FORMAT_EVRCB";
302             case /* AUDIO_FORMAT_EVRCWB          */ 0x12000000:
303                 return "AUDIO_FORMAT_EVRCWB";
304             case /* AUDIO_FORMAT_EVRCNW          */ 0x13000000:
305                 return "AUDIO_FORMAT_EVRCNW";
306             case /* AUDIO_FORMAT_AAC_ADIF        */ 0x14000000:
307                 return "AUDIO_FORMAT_AAC_ADIF";
308             case /* AUDIO_FORMAT_WMA             */ 0x15000000:
309                 return "AUDIO_FORMAT_WMA";
310             case /* AUDIO_FORMAT_WMA_PRO         */ 0x16000000:
311                 return "AUDIO_FORMAT_WMA_PRO";
312             case /* AUDIO_FORMAT_AMR_WB_PLUS     */ 0x17000000:
313                 return "AUDIO_FORMAT_AMR_WB_PLUS";
314             case /* AUDIO_FORMAT_MP2             */ 0x18000000:
315                 return "AUDIO_FORMAT_MP2";
316             case /* AUDIO_FORMAT_QCELP           */ 0x19000000:
317                 return "AUDIO_FORMAT_QCELP";
318             case /* AUDIO_FORMAT_DSD             */ 0x1A000000:
319                 return "AUDIO_FORMAT_DSD";
320             case /* AUDIO_FORMAT_FLAC            */ 0x1B000000:
321                 return "AUDIO_FORMAT_FLAC";
322             case /* AUDIO_FORMAT_ALAC            */ 0x1C000000:
323                 return "AUDIO_FORMAT_ALAC";
324             case /* AUDIO_FORMAT_APE             */ 0x1D000000:
325                 return "AUDIO_FORMAT_APE";
326             case /* AUDIO_FORMAT_AAC_ADTS        */ 0x1E000000:
327                 return "AUDIO_FORMAT_AAC_ADTS";
328             case /* AUDIO_FORMAT_SBC             */ 0x1F000000:
329                 return "AUDIO_FORMAT_SBC";
330             case /* AUDIO_FORMAT_APTX            */ 0x20000000:
331                 return "AUDIO_FORMAT_APTX";
332             case /* AUDIO_FORMAT_APTX_HD         */ 0x21000000:
333                 return "AUDIO_FORMAT_APTX_HD";
334             case /* AUDIO_FORMAT_AC4             */ 0x22000000:
335                 return "AUDIO_FORMAT_AC4";
336             case /* AUDIO_FORMAT_LDAC            */ 0x23000000:
337                 return "AUDIO_FORMAT_LDAC";
338             case /* AUDIO_FORMAT_MAT             */ 0x24000000:
339                 return "AUDIO_FORMAT_MAT";
340             case /* AUDIO_FORMAT_AAC_LATM        */ 0x25000000:
341                 return "AUDIO_FORMAT_AAC_LATM";
342             case /* AUDIO_FORMAT_CELT            */ 0x26000000:
343                 return "AUDIO_FORMAT_CELT";
344             case /* AUDIO_FORMAT_APTX_ADAPTIVE   */ 0x27000000:
345                 return "AUDIO_FORMAT_APTX_ADAPTIVE";
346             case /* AUDIO_FORMAT_LHDC            */ 0x28000000:
347                 return "AUDIO_FORMAT_LHDC";
348             case /* AUDIO_FORMAT_LHDC_LL         */ 0x29000000:
349                 return "AUDIO_FORMAT_LHDC_LL";
350             case /* AUDIO_FORMAT_APTX_TWSP       */ 0x2A000000:
351                 return "AUDIO_FORMAT_APTX_TWSP";
352 
353             /* Aliases */
354             case /* AUDIO_FORMAT_PCM_16_BIT        */ 0x1:
355                 return "AUDIO_FORMAT_PCM_16_BIT";        // (PCM | PCM_SUB_16_BIT)
356             case /* AUDIO_FORMAT_PCM_8_BIT         */ 0x2:
357                 return "AUDIO_FORMAT_PCM_8_BIT";        // (PCM | PCM_SUB_8_BIT)
358             case /* AUDIO_FORMAT_PCM_32_BIT        */ 0x3:
359                 return "AUDIO_FORMAT_PCM_32_BIT";        // (PCM | PCM_SUB_32_BIT)
360             case /* AUDIO_FORMAT_PCM_8_24_BIT      */ 0x4:
361                 return "AUDIO_FORMAT_PCM_8_24_BIT";        // (PCM | PCM_SUB_8_24_BIT)
362             case /* AUDIO_FORMAT_PCM_FLOAT         */ 0x5:
363                 return "AUDIO_FORMAT_PCM_FLOAT";        // (PCM | PCM_SUB_FLOAT)
364             case /* AUDIO_FORMAT_PCM_24_BIT_PACKED */ 0x6:
365                 return "AUDIO_FORMAT_PCM_24_BIT_PACKED";        // (PCM | PCM_SUB_24_BIT_PACKED)
366             case /* AUDIO_FORMAT_AAC_MAIN          */ 0x4000001:
367                 return "AUDIO_FORMAT_AAC_MAIN";  // (AAC | AAC_SUB_MAIN)
368             case /* AUDIO_FORMAT_AAC_LC            */ 0x4000002:
369                 return "AUDIO_FORMAT_AAC_LC";  // (AAC | AAC_SUB_LC)
370             case /* AUDIO_FORMAT_AAC_SSR           */ 0x4000004:
371                 return "AUDIO_FORMAT_AAC_SSR";  // (AAC | AAC_SUB_SSR)
372             case /* AUDIO_FORMAT_AAC_LTP           */ 0x4000008:
373                 return "AUDIO_FORMAT_AAC_LTP";  // (AAC | AAC_SUB_LTP)
374             case /* AUDIO_FORMAT_AAC_HE_V1         */ 0x4000010:
375                 return "AUDIO_FORMAT_AAC_HE_V1";  // (AAC | AAC_SUB_HE_V1)
376             case /* AUDIO_FORMAT_AAC_SCALABLE      */ 0x4000020:
377                 return "AUDIO_FORMAT_AAC_SCALABLE";  // (AAC | AAC_SUB_SCALABLE)
378             case /* AUDIO_FORMAT_AAC_ERLC          */ 0x4000040:
379                 return "AUDIO_FORMAT_AAC_ERLC";  // (AAC | AAC_SUB_ERLC)
380             case /* AUDIO_FORMAT_AAC_LD            */ 0x4000080:
381                 return "AUDIO_FORMAT_AAC_LD";  // (AAC | AAC_SUB_LD)
382             case /* AUDIO_FORMAT_AAC_HE_V2         */ 0x4000100:
383                 return "AUDIO_FORMAT_AAC_HE_V2";  // (AAC | AAC_SUB_HE_V2)
384             case /* AUDIO_FORMAT_AAC_ELD           */ 0x4000200:
385                 return "AUDIO_FORMAT_AAC_ELD";  // (AAC | AAC_SUB_ELD)
386             case /* AUDIO_FORMAT_AAC_XHE           */ 0x4000300:
387                 return "AUDIO_FORMAT_AAC_XHE";  // (AAC | AAC_SUB_XHE)
388             case /* AUDIO_FORMAT_AAC_ADTS_MAIN     */ 0x1e000001:
389                 return "AUDIO_FORMAT_AAC_ADTS_MAIN"; // (AAC_ADTS | AAC_SUB_MAIN)
390             case /* AUDIO_FORMAT_AAC_ADTS_LC       */ 0x1e000002:
391                 return "AUDIO_FORMAT_AAC_ADTS_LC"; // (AAC_ADTS | AAC_SUB_LC)
392             case /* AUDIO_FORMAT_AAC_ADTS_SSR      */ 0x1e000004:
393                 return "AUDIO_FORMAT_AAC_ADTS_SSR"; // (AAC_ADTS | AAC_SUB_SSR)
394             case /* AUDIO_FORMAT_AAC_ADTS_LTP      */ 0x1e000008:
395                 return "AUDIO_FORMAT_AAC_ADTS_LTP"; // (AAC_ADTS | AAC_SUB_LTP)
396             case /* AUDIO_FORMAT_AAC_ADTS_HE_V1    */ 0x1e000010:
397                 return "AUDIO_FORMAT_AAC_ADTS_HE_V1"; // (AAC_ADTS | AAC_SUB_HE_V1)
398             case /* AUDIO_FORMAT_AAC_ADTS_SCALABLE */ 0x1e000020:
399                 return "AUDIO_FORMAT_AAC_ADTS_SCALABLE"; // (AAC_ADTS | AAC_SUB_SCALABLE)
400             case /* AUDIO_FORMAT_AAC_ADTS_ERLC     */ 0x1e000040:
401                 return "AUDIO_FORMAT_AAC_ADTS_ERLC"; // (AAC_ADTS | AAC_SUB_ERLC)
402             case /* AUDIO_FORMAT_AAC_ADTS_LD       */ 0x1e000080:
403                 return "AUDIO_FORMAT_AAC_ADTS_LD"; // (AAC_ADTS | AAC_SUB_LD)
404             case /* AUDIO_FORMAT_AAC_ADTS_HE_V2    */ 0x1e000100:
405                 return "AUDIO_FORMAT_AAC_ADTS_HE_V2"; // (AAC_ADTS | AAC_SUB_HE_V2)
406             case /* AUDIO_FORMAT_AAC_ADTS_ELD      */ 0x1e000200:
407                 return "AUDIO_FORMAT_AAC_ADTS_ELD"; // (AAC_ADTS | AAC_SUB_ELD)
408             case /* AUDIO_FORMAT_AAC_ADTS_XHE      */ 0x1e000300:
409                 return "AUDIO_FORMAT_AAC_ADTS_XHE"; // (AAC_ADTS | AAC_SUB_XHE)
410             case /* AUDIO_FORMAT_AAC_LATM_LC       */ 0x25000002:
411                 return "AUDIO_FORMAT_AAC_LATM_LC"; // (AAC_LATM | AAC_SUB_LC)
412             case /* AUDIO_FORMAT_AAC_LATM_HE_V1    */ 0x25000010:
413                 return "AUDIO_FORMAT_AAC_LATM_HE_V1"; // (AAC_LATM | AAC_SUB_HE_V1)
414             case /* AUDIO_FORMAT_AAC_LATM_HE_V2    */ 0x25000100:
415                 return "AUDIO_FORMAT_AAC_LATM_HE_V2"; // (AAC_LATM | AAC_SUB_HE_V2)
416             case /* AUDIO_FORMAT_E_AC3_JOC         */ 0xA000001:
417                 return "AUDIO_FORMAT_E_AC3_JOC";  // (E_AC3 | E_AC3_SUB_JOC)
418             case /* AUDIO_FORMAT_MAT_1_0           */ 0x24000001:
419                 return "AUDIO_FORMAT_MAT_1_0"; // (MAT | MAT_SUB_1_0)
420             case /* AUDIO_FORMAT_MAT_2_0           */ 0x24000002:
421                 return "AUDIO_FORMAT_MAT_2_0"; // (MAT | MAT_SUB_2_0)
422             case /* AUDIO_FORMAT_MAT_2_1           */ 0x24000003:
423                 return "AUDIO_FORMAT_MAT_2_1"; // (MAT | MAT_SUB_2_1)
424             default:
425                 return "AUDIO_FORMAT_(" + audioFormat + ")";
426         }
427     }
428 
429     /* Routing bits for the former setRouting/getRouting API */
430     /** @hide @deprecated */
431     @Deprecated public static final int ROUTE_EARPIECE          = (1 << 0);
432     /** @hide @deprecated */
433     @Deprecated public static final int ROUTE_SPEAKER           = (1 << 1);
434     /** @hide @deprecated use {@link #ROUTE_BLUETOOTH_SCO} */
435     @Deprecated public static final int ROUTE_BLUETOOTH = (1 << 2);
436     /** @hide @deprecated */
437     @Deprecated public static final int ROUTE_BLUETOOTH_SCO     = (1 << 2);
438     /** @hide @deprecated */
439     @Deprecated public static final int ROUTE_HEADSET           = (1 << 3);
440     /** @hide @deprecated */
441     @Deprecated public static final int ROUTE_BLUETOOTH_A2DP    = (1 << 4);
442     /** @hide @deprecated */
443     @Deprecated public static final int ROUTE_ALL               = 0xFFFFFFFF;
444 
445     // Keep in sync with system/media/audio/include/system/audio.h
446     /**  @hide */
447     public static final int AUDIO_SESSION_ALLOCATE = 0;
448 
449     /**
450      * @hide
451      * Checks whether the specified stream type is active.
452      *
453      * return true if any track playing on this stream is active.
454      */
455     @UnsupportedAppUsage
isStreamActive(int stream, int inPastMs)456     public static native boolean isStreamActive(int stream, int inPastMs);
457 
458     /**
459      * @hide
460      * Checks whether the specified stream type is active on a remotely connected device. The notion
461      * of what constitutes a remote device is enforced by the audio policy manager of the platform.
462      *
463      * return true if any track playing on this stream is active on a remote device.
464      */
isStreamActiveRemotely(int stream, int inPastMs)465     public static native boolean isStreamActiveRemotely(int stream, int inPastMs);
466 
467     /**
468      * @hide
469      * Checks whether the specified audio source is active.
470      *
471      * return true if any recorder using this source is currently recording
472      */
473     @UnsupportedAppUsage
isSourceActive(int source)474     public static native boolean isSourceActive(int source);
475 
476     /**
477      * @hide
478      * Returns a new unused audio session ID
479      */
newAudioSessionId()480     public static native int newAudioSessionId();
481 
482     /**
483      * @hide
484      * Returns a new unused audio player ID
485      */
newAudioPlayerId()486     public static native int newAudioPlayerId();
487 
488     /**
489      * @hide
490      * Returns a new unused audio recorder ID
491      */
newAudioRecorderId()492     public static native int newAudioRecorderId();
493 
494 
495     /**
496      * @hide
497      * Sets a group generic audio configuration parameters. The use of these parameters
498      * are platform dependent, see libaudio
499      *
500      * param keyValuePairs  list of parameters key value pairs in the form:
501      *    key1=value1;key2=value2;...
502      */
503     @UnsupportedAppUsage
setParameters(String keyValuePairs)504     public static native int setParameters(String keyValuePairs);
505 
506     /**
507      * @hide
508      * Gets a group generic audio configuration parameters. The use of these parameters
509      * are platform dependent, see libaudio
510      *
511      * param keys  list of parameters
512      * return value: list of parameters key value pairs in the form:
513      *    key1=value1;key2=value2;...
514      */
515     @UnsupportedAppUsage
getParameters(String keys)516     public static native String getParameters(String keys);
517 
518     // These match the enum AudioError in frameworks/base/core/jni/android_media_AudioSystem.cpp
519     /** @hide Command successful or Media server restarted. see ErrorCallback */
520     public static final int AUDIO_STATUS_OK = 0;
521     /** @hide Command failed or unspecified audio error.  see ErrorCallback */
522     public static final int AUDIO_STATUS_ERROR = 1;
523     /** @hide Media server died. see ErrorCallback */
524     public static final int AUDIO_STATUS_SERVER_DIED = 100;
525 
526     private static ErrorCallback sErrorCallback;
527 
528     /** @hide
529      * Handles the audio error callback.
530      */
531     public interface ErrorCallback
532     {
533         /*
534          * Callback for audio server errors.
535          * param error   error code:
536          * - AUDIO_STATUS_OK
537          * - AUDIO_STATUS_SERVER_DIED
538          * - AUDIO_STATUS_ERROR
539          */
onError(int error)540         void onError(int error);
541     };
542 
543     /**
544      * @hide
545      * Registers a callback to be invoked when an error occurs.
546      * @param cb the callback to run
547      */
548     @UnsupportedAppUsage
setErrorCallback(ErrorCallback cb)549     public static void setErrorCallback(ErrorCallback cb)
550     {
551         synchronized (AudioSystem.class) {
552             sErrorCallback = cb;
553             if (cb != null) {
554                 cb.onError(checkAudioFlinger());
555             }
556         }
557     }
558 
559     @UnsupportedAppUsage
errorCallbackFromNative(int error)560     private static void errorCallbackFromNative(int error)
561     {
562         ErrorCallback errorCallback = null;
563         synchronized (AudioSystem.class) {
564             if (sErrorCallback != null) {
565                 errorCallback = sErrorCallback;
566             }
567         }
568         if (errorCallback != null) {
569             errorCallback.onError(error);
570         }
571     }
572 
573     /**
574      * @hide
575      * Handles events from the audio policy manager about dynamic audio policies
576      * @see android.media.audiopolicy.AudioPolicy
577      */
578     public interface DynamicPolicyCallback
579     {
onDynamicPolicyMixStateUpdate(String regId, int state)580         void onDynamicPolicyMixStateUpdate(String regId, int state);
581     }
582 
583     //keep in sync with include/media/AudioPolicy.h
584     private final static int DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE = 0;
585 
586     private static DynamicPolicyCallback sDynPolicyCallback;
587 
588     /** @hide */
setDynamicPolicyCallback(DynamicPolicyCallback cb)589     public static void setDynamicPolicyCallback(DynamicPolicyCallback cb)
590     {
591         synchronized (AudioSystem.class) {
592             sDynPolicyCallback = cb;
593             native_register_dynamic_policy_callback();
594         }
595     }
596 
597     @UnsupportedAppUsage
dynamicPolicyCallbackFromNative(int event, String regId, int val)598     private static void dynamicPolicyCallbackFromNative(int event, String regId, int val)
599     {
600         DynamicPolicyCallback cb = null;
601         synchronized (AudioSystem.class) {
602             if (sDynPolicyCallback != null) {
603                 cb = sDynPolicyCallback;
604             }
605         }
606         if (cb != null) {
607             switch(event) {
608                 case DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE:
609                     cb.onDynamicPolicyMixStateUpdate(regId, val);
610                     break;
611                 default:
612                     Log.e(TAG, "dynamicPolicyCallbackFromNative: unknown event " + event);
613             }
614         }
615     }
616 
617     /**
618      * @hide
619      * Handles events from the audio policy manager about recording events
620      * @see android.media.AudioManager.AudioRecordingCallback
621      */
622     public interface AudioRecordingCallback
623     {
624         /**
625          * Callback for recording activity notifications events
626          * @param event
627          * @param riid recording identifier
628          * @param uid uid of the client app performing the recording
629          * @param session
630          * @param source
631          * @param recordingFormat an array of ints containing respectively the client and device
632          *    recording configurations (2*3 ints), followed by the patch handle:
633          *    index 0: client format
634          *          1: client channel mask
635          *          2: client sample rate
636          *          3: device format
637          *          4: device channel mask
638          *          5: device sample rate
639          *          6: patch handle
640          * @param packName package name of the client app performing the recording. NOT SUPPORTED
641          */
onRecordingConfigurationChanged(int event, int riid, int uid, int session, int source, int portId, boolean silenced, int[] recordingFormat, AudioEffect.Descriptor[] clienteffects, AudioEffect.Descriptor[] effects, int activeSource, String packName)642         void onRecordingConfigurationChanged(int event, int riid, int uid, int session, int source,
643                         int portId, boolean silenced, int[] recordingFormat,
644                         AudioEffect.Descriptor[] clienteffects, AudioEffect.Descriptor[] effects,
645                         int activeSource, String packName);
646     }
647 
648     private static AudioRecordingCallback sRecordingCallback;
649 
650     /** @hide */
setRecordingCallback(AudioRecordingCallback cb)651     public static void setRecordingCallback(AudioRecordingCallback cb) {
652         synchronized (AudioSystem.class) {
653             sRecordingCallback = cb;
654             native_register_recording_callback();
655         }
656     }
657 
658     /**
659      * Callback from native for recording configuration updates.
660      * @param event
661      * @param riid
662      * @param uid
663      * @param session
664      * @param source
665      * @param portId
666      * @param silenced
667      * @param recordingFormat see
668      *     {@link AudioRecordingCallback#onRecordingConfigurationChanged(int, int, int, int, int, \
669      int, boolean, int[], AudioEffect.Descriptor[], AudioEffect.Descriptor[], int, String)}
670      *     for the description of the record format.
671      * @param cleintEffects
672      * @param effects
673      * @param activeSource
674      */
675     @UnsupportedAppUsage
recordingCallbackFromNative(int event, int riid, int uid, int session, int source, int portId, boolean silenced, int[] recordingFormat, AudioEffect.Descriptor[] clientEffects, AudioEffect.Descriptor[] effects, int activeSource)676     private static void recordingCallbackFromNative(int event, int riid, int uid, int session,
677                           int source, int portId, boolean silenced, int[] recordingFormat,
678                           AudioEffect.Descriptor[] clientEffects, AudioEffect.Descriptor[] effects,
679                           int activeSource) {
680         AudioRecordingCallback cb = null;
681         synchronized (AudioSystem.class) {
682             cb = sRecordingCallback;
683         }
684 
685         String clientEffectName =  clientEffects.length == 0 ? "None" : clientEffects[0].name;
686         String effectName =  effects.length == 0 ? "None" : effects[0].name;
687 
688         if (cb != null) {
689             ArrayList<AudioPatch> audioPatches = new ArrayList<>();
690             if (AudioManager.listAudioPatches(audioPatches) == AudioManager.SUCCESS) {
691                 boolean patchFound = false;
692                 int patchHandle = recordingFormat[6];
693                 for (AudioPatch patch : audioPatches) {
694                     if (patch.id() == patchHandle) {
695                         patchFound = true;
696                         break;
697                     }
698                 }
699                 if (!patchFound) {
700                     // The cached audio patches in AudioManager is not up-to-date.
701                     // Reset audio port generation to ensure callback side can
702                     // get up-to-date audio port information.
703                     AudioManager.resetAudioPortGeneration();
704                 }
705             }
706             // TODO receive package name from native
707             cb.onRecordingConfigurationChanged(event, riid, uid, session, source, portId, silenced,
708                                         recordingFormat, clientEffects, effects, activeSource, "");
709         }
710     }
711 
712     /*
713      * Error codes used by public APIs (AudioTrack, AudioRecord, AudioManager ...)
714      * Must be kept in sync with frameworks/base/core/jni/android_media_AudioErrors.h
715      */
716     /** @hide */
717     public static final int SUCCESS            = 0;
718     /** @hide */
719     public static final int ERROR              = -1;
720     /** @hide */
721     public static final int BAD_VALUE          = -2;
722     /** @hide */
723     public static final int INVALID_OPERATION  = -3;
724     /** @hide */
725     public static final int PERMISSION_DENIED  = -4;
726     /** @hide */
727     public static final int NO_INIT            = -5;
728     /** @hide */
729     public static final int DEAD_OBJECT        = -6;
730     /** @hide */
731     public static final int WOULD_BLOCK        = -7;
732 
733     /** @hide */
734     @IntDef({
735             SUCCESS,
736             ERROR,
737             BAD_VALUE,
738             INVALID_OPERATION,
739             PERMISSION_DENIED,
740             NO_INIT,
741             DEAD_OBJECT,
742             WOULD_BLOCK
743     })
744     @Retention(RetentionPolicy.SOURCE)
745     public @interface AudioSystemError {}
746 
747     /**
748      * @hide
749      * Convert an int error value to its String value for readability.
750      * Accepted error values are the java AudioSystem errors, matching android_media_AudioErrors.h,
751      * which map onto the native status_t type.
752      * @param error one of the java AudioSystem errors
753      * @return a human-readable string
754      */
audioSystemErrorToString(@udioSystemError int error)755     public static String audioSystemErrorToString(@AudioSystemError int error) {
756         switch(error) {
757             case SUCCESS:
758                 return "SUCCESS";
759             case ERROR:
760                 return "ERROR";
761             case BAD_VALUE:
762                 return "BAD_VALUE";
763             case INVALID_OPERATION:
764                 return "INVALID_OPERATION";
765             case PERMISSION_DENIED:
766                 return "PERMISSION_DENIED";
767             case NO_INIT:
768                 return "NO_INIT";
769             case DEAD_OBJECT:
770                 return "DEAD_OBJECT";
771             case WOULD_BLOCK:
772                 return "WOULD_BLOCK";
773             default:
774                 return ("unknown error:" + error);
775         }
776     }
777 
778     /*
779      * AudioPolicyService methods
780      */
781 
782     //
783     // audio device definitions: must be kept in sync with values in system/core/audio.h
784     //
785     /** @hide */
786     public static final int DEVICE_NONE = 0x0;
787     // reserved bits
788     /** @hide */
789     public static final int DEVICE_BIT_IN = 0x80000000;
790     /** @hide */
791     public static final int DEVICE_BIT_DEFAULT = 0x40000000;
792     // output devices, be sure to update AudioManager.java also
793     /** @hide */
794     @UnsupportedAppUsage
795     public static final int DEVICE_OUT_EARPIECE = 0x1;
796     /** @hide */
797     @UnsupportedAppUsage
798     public static final int DEVICE_OUT_SPEAKER = 0x2;
799     /** @hide */
800     @UnsupportedAppUsage
801     public static final int DEVICE_OUT_WIRED_HEADSET = 0x4;
802     /** @hide */
803     @UnsupportedAppUsage
804     public static final int DEVICE_OUT_WIRED_HEADPHONE = 0x8;
805     /** @hide */
806     @UnsupportedAppUsage
807     public static final int DEVICE_OUT_BLUETOOTH_SCO = 0x10;
808     /** @hide */
809     @UnsupportedAppUsage
810     public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20;
811     /** @hide */
812     @UnsupportedAppUsage
813     public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40;
814     /** @hide */
815     @UnsupportedAppUsage
816     public static final int DEVICE_OUT_BLUETOOTH_A2DP = 0x80;
817     /** @hide */
818     @UnsupportedAppUsage
819     public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100;
820     /** @hide */
821     @UnsupportedAppUsage
822     public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200;
823     /** @hide */
824     @UnsupportedAppUsage
825     public static final int DEVICE_OUT_AUX_DIGITAL = 0x400;
826     /** @hide */
827     public static final int DEVICE_OUT_HDMI = DEVICE_OUT_AUX_DIGITAL;
828     /** @hide */
829     @UnsupportedAppUsage
830     public static final int DEVICE_OUT_ANLG_DOCK_HEADSET = 0x800;
831     /** @hide */
832     @UnsupportedAppUsage
833     public static final int DEVICE_OUT_DGTL_DOCK_HEADSET = 0x1000;
834     /** @hide */
835     @UnsupportedAppUsage
836     public static final int DEVICE_OUT_USB_ACCESSORY = 0x2000;
837     /** @hide */
838     @UnsupportedAppUsage
839     public static final int DEVICE_OUT_USB_DEVICE = 0x4000;
840     /** @hide */
841     @UnsupportedAppUsage
842     public static final int DEVICE_OUT_REMOTE_SUBMIX = 0x8000;
843     /** @hide */
844     @UnsupportedAppUsage
845     public static final int DEVICE_OUT_TELEPHONY_TX = 0x10000;
846     /** @hide */
847     public static final int DEVICE_OUT_LINE = 0x20000;
848     /** @hide */
849     public static final int DEVICE_OUT_HDMI_ARC = 0x40000;
850     /** @hide */
851     public static final int DEVICE_OUT_SPDIF = 0x80000;
852     /** @hide */
853     @UnsupportedAppUsage
854     public static final int DEVICE_OUT_FM = 0x100000;
855     /** @hide */
856     public static final int DEVICE_OUT_AUX_LINE = 0x200000;
857     /** @hide */
858     public static final int DEVICE_OUT_SPEAKER_SAFE = 0x400000;
859     /** @hide */
860     public static final int DEVICE_OUT_IP = 0x800000;
861     /** @hide */
862     public static final int DEVICE_OUT_BUS = 0x1000000;
863     /** @hide */
864     public static final int DEVICE_OUT_PROXY = 0x2000000;
865     /** @hide */
866     public static final int DEVICE_OUT_USB_HEADSET = 0x4000000;
867     /** @hide */
868     public static final int DEVICE_OUT_HEARING_AID = 0x8000000;
869 
870     /** @hide */
871     public static final int DEVICE_OUT_DEFAULT = DEVICE_BIT_DEFAULT;
872 
873     // Deprecated in R because multiple device types are no longer accessed as a bit mask.
874     // Removing this will get lint warning about changing hidden apis.
875     /** @hide */
876     @UnsupportedAppUsage
877     public static final int DEVICE_OUT_ALL_USB = (DEVICE_OUT_USB_ACCESSORY |
878                                                   DEVICE_OUT_USB_DEVICE |
879                                                   DEVICE_OUT_USB_HEADSET);
880 
881     /** @hide */
882     public static final Set<Integer> DEVICE_OUT_ALL_SET;
883     /** @hide */
884     public static final Set<Integer> DEVICE_OUT_ALL_A2DP_SET;
885     /** @hide */
886     public static final Set<Integer> DEVICE_OUT_ALL_SCO_SET;
887     /** @hide */
888     public static final Set<Integer> DEVICE_OUT_ALL_USB_SET;
889     /** @hide */
890     public static final Set<Integer> DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO_SET;
891     /** @hide */
892     public static final Set<Integer> DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER_SET;
893     static {
894         DEVICE_OUT_ALL_SET = new HashSet<>();
895         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_EARPIECE);
896         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_SPEAKER);
897         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_WIRED_HEADSET);
898         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_WIRED_HEADPHONE);
899         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_BLUETOOTH_SCO);
900         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_BLUETOOTH_SCO_HEADSET);
901         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
902         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_BLUETOOTH_A2DP);
903         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES);
904         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
905         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_HDMI);
906         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_ANLG_DOCK_HEADSET);
907         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_DGTL_DOCK_HEADSET);
908         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_USB_ACCESSORY);
909         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_USB_DEVICE);
910         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_REMOTE_SUBMIX);
911         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_TELEPHONY_TX);
912         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_LINE);
913         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_HDMI_ARC);
914         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_SPDIF);
915         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_FM);
916         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_AUX_LINE);
917         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_SPEAKER_SAFE);
918         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_IP);
919         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_BUS);
920         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_PROXY);
921         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_USB_HEADSET);
922         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_HEARING_AID);
923         DEVICE_OUT_ALL_SET.add(DEVICE_OUT_DEFAULT);
924 
925         DEVICE_OUT_ALL_A2DP_SET = new HashSet<>();
926         DEVICE_OUT_ALL_A2DP_SET.add(DEVICE_OUT_BLUETOOTH_A2DP);
927         DEVICE_OUT_ALL_A2DP_SET.add(DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES);
928         DEVICE_OUT_ALL_A2DP_SET.add(DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
929 
930         DEVICE_OUT_ALL_SCO_SET = new HashSet<>();
931         DEVICE_OUT_ALL_SCO_SET.add(DEVICE_OUT_BLUETOOTH_SCO);
932         DEVICE_OUT_ALL_SCO_SET.add(DEVICE_OUT_BLUETOOTH_SCO_HEADSET);
933         DEVICE_OUT_ALL_SCO_SET.add(DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
934 
935         DEVICE_OUT_ALL_USB_SET = new HashSet<>();
936         DEVICE_OUT_ALL_USB_SET.add(DEVICE_OUT_USB_ACCESSORY);
937         DEVICE_OUT_ALL_USB_SET.add(DEVICE_OUT_USB_DEVICE);
938         DEVICE_OUT_ALL_USB_SET.add(DEVICE_OUT_USB_HEADSET);
939 
940         DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO_SET = new HashSet<>();
941         DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO_SET.add(DEVICE_OUT_AUX_LINE);
942         DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO_SET.add(DEVICE_OUT_HDMI_ARC);
943         DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO_SET.add(DEVICE_OUT_SPDIF);
944 
945         DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER_SET = new HashSet<>();
946         DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER_SET.addAll(DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO_SET);
947         DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER_SET.add(DEVICE_OUT_SPEAKER);
948     }
949 
950     // input devices
951     /** @hide */
952     @UnsupportedAppUsage
953     public static final int DEVICE_IN_COMMUNICATION = DEVICE_BIT_IN | 0x1;
954     /** @hide */
955     @UnsupportedAppUsage
956     public static final int DEVICE_IN_AMBIENT = DEVICE_BIT_IN | 0x2;
957     /** @hide */
958     @UnsupportedAppUsage
959     public static final int DEVICE_IN_BUILTIN_MIC = DEVICE_BIT_IN | 0x4;
960     /** @hide */
961     @UnsupportedAppUsage
962     public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET = DEVICE_BIT_IN | 0x8;
963     /** @hide */
964     @UnsupportedAppUsage
965     public static final int DEVICE_IN_WIRED_HEADSET = DEVICE_BIT_IN | 0x10;
966     /** @hide */
967     @UnsupportedAppUsage
968     public static final int DEVICE_IN_AUX_DIGITAL = DEVICE_BIT_IN | 0x20;
969     /** @hide */
970     public static final int DEVICE_IN_HDMI = DEVICE_IN_AUX_DIGITAL;
971     /** @hide */
972     @UnsupportedAppUsage
973     public static final int DEVICE_IN_VOICE_CALL = DEVICE_BIT_IN | 0x40;
974     /** @hide */
975     public static final int DEVICE_IN_TELEPHONY_RX = DEVICE_IN_VOICE_CALL;
976     /** @hide */
977     @UnsupportedAppUsage
978     public static final int DEVICE_IN_BACK_MIC = DEVICE_BIT_IN | 0x80;
979     /** @hide */
980     @UnsupportedAppUsage
981     public static final int DEVICE_IN_REMOTE_SUBMIX = DEVICE_BIT_IN | 0x100;
982     /** @hide */
983     @UnsupportedAppUsage
984     public static final int DEVICE_IN_ANLG_DOCK_HEADSET = DEVICE_BIT_IN | 0x200;
985     /** @hide */
986     @UnsupportedAppUsage
987     public static final int DEVICE_IN_DGTL_DOCK_HEADSET = DEVICE_BIT_IN | 0x400;
988     /** @hide */
989     @UnsupportedAppUsage
990     public static final int DEVICE_IN_USB_ACCESSORY = DEVICE_BIT_IN | 0x800;
991     /** @hide */
992     @UnsupportedAppUsage
993     public static final int DEVICE_IN_USB_DEVICE = DEVICE_BIT_IN | 0x1000;
994     /** @hide */
995     public static final int DEVICE_IN_FM_TUNER = DEVICE_BIT_IN | 0x2000;
996     /** @hide */
997     public static final int DEVICE_IN_TV_TUNER = DEVICE_BIT_IN | 0x4000;
998     /** @hide */
999     public static final int DEVICE_IN_LINE = DEVICE_BIT_IN | 0x8000;
1000     /** @hide */
1001     public static final int DEVICE_IN_SPDIF = DEVICE_BIT_IN | 0x10000;
1002     /** @hide */
1003     @UnsupportedAppUsage
1004     public static final int DEVICE_IN_BLUETOOTH_A2DP = DEVICE_BIT_IN | 0x20000;
1005     /** @hide */
1006     public static final int DEVICE_IN_LOOPBACK = DEVICE_BIT_IN | 0x40000;
1007     /** @hide */
1008     public static final int DEVICE_IN_IP = DEVICE_BIT_IN | 0x80000;
1009     /** @hide */
1010     public static final int DEVICE_IN_BUS = DEVICE_BIT_IN | 0x100000;
1011     /** @hide */
1012     public static final int DEVICE_IN_PROXY = DEVICE_BIT_IN | 0x1000000;
1013     /** @hide */
1014     public static final int DEVICE_IN_USB_HEADSET = DEVICE_BIT_IN | 0x2000000;
1015     /** @hide */
1016     public static final int DEVICE_IN_BLUETOOTH_BLE = DEVICE_BIT_IN | 0x4000000;
1017     /** @hide */
1018     public static final int DEVICE_IN_HDMI_ARC = DEVICE_BIT_IN | 0x8000000;
1019     /** @hide */
1020     public static final int DEVICE_IN_ECHO_REFERENCE = DEVICE_BIT_IN | 0x10000000;
1021     /** @hide */
1022     @UnsupportedAppUsage
1023     public static final int DEVICE_IN_DEFAULT = DEVICE_BIT_IN | DEVICE_BIT_DEFAULT;
1024 
1025     /** @hide */
1026     public static final Set<Integer> DEVICE_IN_ALL_SET;
1027     /** @hide */
1028     public static final Set<Integer> DEVICE_IN_ALL_SCO_SET;
1029     /** @hide */
1030     public static final Set<Integer> DEVICE_IN_ALL_USB_SET;
1031     static {
1032         DEVICE_IN_ALL_SET = new HashSet<>();
1033         DEVICE_IN_ALL_SET.add(DEVICE_IN_COMMUNICATION);
1034         DEVICE_IN_ALL_SET.add(DEVICE_IN_AMBIENT);
1035         DEVICE_IN_ALL_SET.add(DEVICE_IN_BUILTIN_MIC);
1036         DEVICE_IN_ALL_SET.add(DEVICE_IN_BLUETOOTH_SCO_HEADSET);
1037         DEVICE_IN_ALL_SET.add(DEVICE_IN_WIRED_HEADSET);
1038         DEVICE_IN_ALL_SET.add(DEVICE_IN_HDMI);
1039         DEVICE_IN_ALL_SET.add(DEVICE_IN_TELEPHONY_RX);
1040         DEVICE_IN_ALL_SET.add(DEVICE_IN_BACK_MIC);
1041         DEVICE_IN_ALL_SET.add(DEVICE_IN_REMOTE_SUBMIX);
1042         DEVICE_IN_ALL_SET.add(DEVICE_IN_ANLG_DOCK_HEADSET);
1043         DEVICE_IN_ALL_SET.add(DEVICE_IN_DGTL_DOCK_HEADSET);
1044         DEVICE_IN_ALL_SET.add(DEVICE_IN_USB_ACCESSORY);
1045         DEVICE_IN_ALL_SET.add(DEVICE_IN_USB_DEVICE);
1046         DEVICE_IN_ALL_SET.add(DEVICE_IN_FM_TUNER);
1047         DEVICE_IN_ALL_SET.add(DEVICE_IN_TV_TUNER);
1048         DEVICE_IN_ALL_SET.add(DEVICE_IN_LINE);
1049         DEVICE_IN_ALL_SET.add(DEVICE_IN_SPDIF);
1050         DEVICE_IN_ALL_SET.add(DEVICE_IN_BLUETOOTH_A2DP);
1051         DEVICE_IN_ALL_SET.add(DEVICE_IN_LOOPBACK);
1052         DEVICE_IN_ALL_SET.add(DEVICE_IN_IP);
1053         DEVICE_IN_ALL_SET.add(DEVICE_IN_BUS);
1054         DEVICE_IN_ALL_SET.add(DEVICE_IN_PROXY);
1055         DEVICE_IN_ALL_SET.add(DEVICE_IN_USB_HEADSET);
1056         DEVICE_IN_ALL_SET.add(DEVICE_IN_BLUETOOTH_BLE);
1057         DEVICE_IN_ALL_SET.add(DEVICE_IN_HDMI_ARC);
1058         DEVICE_IN_ALL_SET.add(DEVICE_IN_ECHO_REFERENCE);
1059         DEVICE_IN_ALL_SET.add(DEVICE_IN_DEFAULT);
1060 
1061         DEVICE_IN_ALL_SCO_SET = new HashSet<>();
1062         DEVICE_IN_ALL_SCO_SET.add(DEVICE_IN_BLUETOOTH_SCO_HEADSET);
1063 
1064         DEVICE_IN_ALL_USB_SET = new HashSet<>();
1065         DEVICE_IN_ALL_USB_SET.add(DEVICE_IN_USB_ACCESSORY);
1066         DEVICE_IN_ALL_USB_SET.add(DEVICE_IN_USB_DEVICE);
1067         DEVICE_IN_ALL_USB_SET.add(DEVICE_IN_USB_HEADSET);
1068     }
1069 
1070     /** @hide */
1071     public static final String LEGACY_REMOTE_SUBMIX_ADDRESS = "0";
1072 
1073     // device states, must match AudioSystem::device_connection_state
1074     /** @hide */
1075     @UnsupportedAppUsage
1076     public static final int DEVICE_STATE_UNAVAILABLE = 0;
1077     /** @hide */
1078     @UnsupportedAppUsage
1079     public static final int DEVICE_STATE_AVAILABLE = 1;
1080     private static final int NUM_DEVICE_STATES = 1;
1081 
1082     /** @hide */
deviceStateToString(int state)1083     public static String deviceStateToString(int state) {
1084         switch (state) {
1085             case DEVICE_STATE_UNAVAILABLE: return "DEVICE_STATE_UNAVAILABLE";
1086             case DEVICE_STATE_AVAILABLE: return "DEVICE_STATE_AVAILABLE";
1087             default: return "unknown state (" + state + ")";
1088         }
1089     }
1090 
1091     /** @hide */ public static final String DEVICE_OUT_EARPIECE_NAME = "earpiece";
1092     /** @hide */ public static final String DEVICE_OUT_SPEAKER_NAME = "speaker";
1093     /** @hide */ public static final String DEVICE_OUT_WIRED_HEADSET_NAME = "headset";
1094     /** @hide */ public static final String DEVICE_OUT_WIRED_HEADPHONE_NAME = "headphone";
1095     /** @hide */ public static final String DEVICE_OUT_BLUETOOTH_SCO_NAME = "bt_sco";
1096     /** @hide */ public static final String DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME = "bt_sco_hs";
1097     /** @hide */ public static final String DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME = "bt_sco_carkit";
1098     /** @hide */ public static final String DEVICE_OUT_BLUETOOTH_A2DP_NAME = "bt_a2dp";
1099     /** @hide */
1100     public static final String DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME = "bt_a2dp_hp";
1101     /** @hide */ public static final String DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME = "bt_a2dp_spk";
1102     /** @hide */ public static final String DEVICE_OUT_AUX_DIGITAL_NAME = "aux_digital";
1103     /** @hide */ public static final String DEVICE_OUT_HDMI_NAME = "hdmi";
1104     /** @hide */ public static final String DEVICE_OUT_ANLG_DOCK_HEADSET_NAME = "analog_dock";
1105     /** @hide */ public static final String DEVICE_OUT_DGTL_DOCK_HEADSET_NAME = "digital_dock";
1106     /** @hide */ public static final String DEVICE_OUT_USB_ACCESSORY_NAME = "usb_accessory";
1107     /** @hide */ public static final String DEVICE_OUT_USB_DEVICE_NAME = "usb_device";
1108     /** @hide */ public static final String DEVICE_OUT_REMOTE_SUBMIX_NAME = "remote_submix";
1109     /** @hide */ public static final String DEVICE_OUT_TELEPHONY_TX_NAME = "telephony_tx";
1110     /** @hide */ public static final String DEVICE_OUT_LINE_NAME = "line";
1111     /** @hide */ public static final String DEVICE_OUT_HDMI_ARC_NAME = "hmdi_arc";
1112     /** @hide */ public static final String DEVICE_OUT_SPDIF_NAME = "spdif";
1113     /** @hide */ public static final String DEVICE_OUT_FM_NAME = "fm_transmitter";
1114     /** @hide */ public static final String DEVICE_OUT_AUX_LINE_NAME = "aux_line";
1115     /** @hide */ public static final String DEVICE_OUT_SPEAKER_SAFE_NAME = "speaker_safe";
1116     /** @hide */ public static final String DEVICE_OUT_IP_NAME = "ip";
1117     /** @hide */ public static final String DEVICE_OUT_BUS_NAME = "bus";
1118     /** @hide */ public static final String DEVICE_OUT_PROXY_NAME = "proxy";
1119     /** @hide */ public static final String DEVICE_OUT_USB_HEADSET_NAME = "usb_headset";
1120     /** @hide */ public static final String DEVICE_OUT_HEARING_AID_NAME = "hearing_aid_out";
1121 
1122     /** @hide */ public static final String DEVICE_IN_COMMUNICATION_NAME = "communication";
1123     /** @hide */ public static final String DEVICE_IN_AMBIENT_NAME = "ambient";
1124     /** @hide */ public static final String DEVICE_IN_BUILTIN_MIC_NAME = "mic";
1125     /** @hide */ public static final String DEVICE_IN_BLUETOOTH_SCO_HEADSET_NAME = "bt_sco_hs";
1126     /** @hide */ public static final String DEVICE_IN_WIRED_HEADSET_NAME = "headset";
1127     /** @hide */ public static final String DEVICE_IN_AUX_DIGITAL_NAME = "aux_digital";
1128     /** @hide */ public static final String DEVICE_IN_TELEPHONY_RX_NAME = "telephony_rx";
1129     /** @hide */ public static final String DEVICE_IN_BACK_MIC_NAME = "back_mic";
1130     /** @hide */ public static final String DEVICE_IN_REMOTE_SUBMIX_NAME = "remote_submix";
1131     /** @hide */ public static final String DEVICE_IN_ANLG_DOCK_HEADSET_NAME = "analog_dock";
1132     /** @hide */ public static final String DEVICE_IN_DGTL_DOCK_HEADSET_NAME = "digital_dock";
1133     /** @hide */ public static final String DEVICE_IN_USB_ACCESSORY_NAME = "usb_accessory";
1134     /** @hide */ public static final String DEVICE_IN_USB_DEVICE_NAME = "usb_device";
1135     /** @hide */ public static final String DEVICE_IN_FM_TUNER_NAME = "fm_tuner";
1136     /** @hide */ public static final String DEVICE_IN_TV_TUNER_NAME = "tv_tuner";
1137     /** @hide */ public static final String DEVICE_IN_LINE_NAME = "line";
1138     /** @hide */ public static final String DEVICE_IN_SPDIF_NAME = "spdif";
1139     /** @hide */ public static final String DEVICE_IN_BLUETOOTH_A2DP_NAME = "bt_a2dp";
1140     /** @hide */ public static final String DEVICE_IN_LOOPBACK_NAME = "loopback";
1141     /** @hide */ public static final String DEVICE_IN_IP_NAME = "ip";
1142     /** @hide */ public static final String DEVICE_IN_BUS_NAME = "bus";
1143     /** @hide */ public static final String DEVICE_IN_PROXY_NAME = "proxy";
1144     /** @hide */ public static final String DEVICE_IN_USB_HEADSET_NAME = "usb_headset";
1145     /** @hide */ public static final String DEVICE_IN_BLUETOOTH_BLE_NAME = "bt_ble";
1146     /** @hide */ public static final String DEVICE_IN_ECHO_REFERENCE_NAME = "echo_reference";
1147     /** @hide */ public static final String DEVICE_IN_HDMI_ARC_NAME = "hdmi_arc";
1148 
1149     /** @hide */
1150     @UnsupportedAppUsage
getOutputDeviceName(int device)1151     public static String getOutputDeviceName(int device)
1152     {
1153         switch(device) {
1154         case DEVICE_OUT_EARPIECE:
1155             return DEVICE_OUT_EARPIECE_NAME;
1156         case DEVICE_OUT_SPEAKER:
1157             return DEVICE_OUT_SPEAKER_NAME;
1158         case DEVICE_OUT_WIRED_HEADSET:
1159             return DEVICE_OUT_WIRED_HEADSET_NAME;
1160         case DEVICE_OUT_WIRED_HEADPHONE:
1161             return DEVICE_OUT_WIRED_HEADPHONE_NAME;
1162         case DEVICE_OUT_BLUETOOTH_SCO:
1163             return DEVICE_OUT_BLUETOOTH_SCO_NAME;
1164         case DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
1165             return DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME;
1166         case DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
1167             return DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME;
1168         case DEVICE_OUT_BLUETOOTH_A2DP:
1169             return DEVICE_OUT_BLUETOOTH_A2DP_NAME;
1170         case DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
1171             return DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME;
1172         case DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
1173             return DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME;
1174         case DEVICE_OUT_HDMI:
1175             return DEVICE_OUT_HDMI_NAME;
1176         case DEVICE_OUT_ANLG_DOCK_HEADSET:
1177             return DEVICE_OUT_ANLG_DOCK_HEADSET_NAME;
1178         case DEVICE_OUT_DGTL_DOCK_HEADSET:
1179             return DEVICE_OUT_DGTL_DOCK_HEADSET_NAME;
1180         case DEVICE_OUT_USB_ACCESSORY:
1181             return DEVICE_OUT_USB_ACCESSORY_NAME;
1182         case DEVICE_OUT_USB_DEVICE:
1183             return DEVICE_OUT_USB_DEVICE_NAME;
1184         case DEVICE_OUT_REMOTE_SUBMIX:
1185             return DEVICE_OUT_REMOTE_SUBMIX_NAME;
1186         case DEVICE_OUT_TELEPHONY_TX:
1187             return DEVICE_OUT_TELEPHONY_TX_NAME;
1188         case DEVICE_OUT_LINE:
1189             return DEVICE_OUT_LINE_NAME;
1190         case DEVICE_OUT_HDMI_ARC:
1191             return DEVICE_OUT_HDMI_ARC_NAME;
1192         case DEVICE_OUT_SPDIF:
1193             return DEVICE_OUT_SPDIF_NAME;
1194         case DEVICE_OUT_FM:
1195             return DEVICE_OUT_FM_NAME;
1196         case DEVICE_OUT_AUX_LINE:
1197             return DEVICE_OUT_AUX_LINE_NAME;
1198         case DEVICE_OUT_SPEAKER_SAFE:
1199             return DEVICE_OUT_SPEAKER_SAFE_NAME;
1200         case DEVICE_OUT_IP:
1201             return DEVICE_OUT_IP_NAME;
1202         case DEVICE_OUT_BUS:
1203             return DEVICE_OUT_BUS_NAME;
1204         case DEVICE_OUT_PROXY:
1205             return DEVICE_OUT_PROXY_NAME;
1206         case DEVICE_OUT_USB_HEADSET:
1207             return DEVICE_OUT_USB_HEADSET_NAME;
1208         case DEVICE_OUT_HEARING_AID:
1209             return DEVICE_OUT_HEARING_AID_NAME;
1210         case DEVICE_OUT_DEFAULT:
1211         default:
1212             return Integer.toString(device);
1213         }
1214     }
1215 
1216     /** @hide */
getInputDeviceName(int device)1217     public static String getInputDeviceName(int device)
1218     {
1219         switch(device) {
1220         case DEVICE_IN_COMMUNICATION:
1221             return DEVICE_IN_COMMUNICATION_NAME;
1222         case DEVICE_IN_AMBIENT:
1223             return DEVICE_IN_AMBIENT_NAME;
1224         case DEVICE_IN_BUILTIN_MIC:
1225             return DEVICE_IN_BUILTIN_MIC_NAME;
1226         case DEVICE_IN_BLUETOOTH_SCO_HEADSET:
1227             return DEVICE_IN_BLUETOOTH_SCO_HEADSET_NAME;
1228         case DEVICE_IN_WIRED_HEADSET:
1229             return DEVICE_IN_WIRED_HEADSET_NAME;
1230         case DEVICE_IN_AUX_DIGITAL:
1231             return DEVICE_IN_AUX_DIGITAL_NAME;
1232         case DEVICE_IN_TELEPHONY_RX:
1233             return DEVICE_IN_TELEPHONY_RX_NAME;
1234         case DEVICE_IN_BACK_MIC:
1235             return DEVICE_IN_BACK_MIC_NAME;
1236         case DEVICE_IN_REMOTE_SUBMIX:
1237             return DEVICE_IN_REMOTE_SUBMIX_NAME;
1238         case DEVICE_IN_ANLG_DOCK_HEADSET:
1239             return DEVICE_IN_ANLG_DOCK_HEADSET_NAME;
1240         case DEVICE_IN_DGTL_DOCK_HEADSET:
1241             return DEVICE_IN_DGTL_DOCK_HEADSET_NAME;
1242         case DEVICE_IN_USB_ACCESSORY:
1243             return DEVICE_IN_USB_ACCESSORY_NAME;
1244         case DEVICE_IN_USB_DEVICE:
1245             return DEVICE_IN_USB_DEVICE_NAME;
1246         case DEVICE_IN_FM_TUNER:
1247             return DEVICE_IN_FM_TUNER_NAME;
1248         case DEVICE_IN_TV_TUNER:
1249             return DEVICE_IN_TV_TUNER_NAME;
1250         case DEVICE_IN_LINE:
1251             return DEVICE_IN_LINE_NAME;
1252         case DEVICE_IN_SPDIF:
1253             return DEVICE_IN_SPDIF_NAME;
1254         case DEVICE_IN_BLUETOOTH_A2DP:
1255             return DEVICE_IN_BLUETOOTH_A2DP_NAME;
1256         case DEVICE_IN_LOOPBACK:
1257             return DEVICE_IN_LOOPBACK_NAME;
1258         case DEVICE_IN_IP:
1259             return DEVICE_IN_IP_NAME;
1260         case DEVICE_IN_BUS:
1261             return DEVICE_IN_BUS_NAME;
1262         case DEVICE_IN_PROXY:
1263             return DEVICE_IN_PROXY_NAME;
1264         case DEVICE_IN_USB_HEADSET:
1265             return DEVICE_IN_USB_HEADSET_NAME;
1266         case DEVICE_IN_BLUETOOTH_BLE:
1267             return DEVICE_IN_BLUETOOTH_BLE_NAME;
1268         case DEVICE_IN_ECHO_REFERENCE:
1269             return DEVICE_IN_ECHO_REFERENCE_NAME;
1270         case DEVICE_IN_HDMI_ARC:
1271             return DEVICE_IN_HDMI_ARC_NAME;
1272         case DEVICE_IN_DEFAULT:
1273         default:
1274             return Integer.toString(device);
1275         }
1276     }
1277 
1278     /**
1279      * @hide
1280      * Returns a human readable name for a given device type
1281      * @param device a native device type, NOT an AudioDeviceInfo type
1282      * @return a string describing the device type
1283      */
getDeviceName(int device)1284     public static @NonNull String getDeviceName(int device) {
1285         if ((device & DEVICE_BIT_IN) != 0) {
1286             return getInputDeviceName(device);
1287         }
1288         return getOutputDeviceName(device);
1289     }
1290 
1291     // phone state, match audio_mode???
1292     /** @hide */ public static final int PHONE_STATE_OFFCALL = 0;
1293     /** @hide */ public static final int PHONE_STATE_RINGING = 1;
1294     /** @hide */ public static final int PHONE_STATE_INCALL = 2;
1295 
1296     // device categories config for setForceUse, must match audio_policy_forced_cfg_t
1297     /** @hide */ @UnsupportedAppUsage public static final int FORCE_NONE = 0;
1298     /** @hide */ public static final int FORCE_SPEAKER = 1;
1299     /** @hide */ public static final int FORCE_HEADPHONES = 2;
1300     /** @hide */ public static final int FORCE_BT_SCO = 3;
1301     /** @hide */ public static final int FORCE_BT_A2DP = 4;
1302     /** @hide */ public static final int FORCE_WIRED_ACCESSORY = 5;
1303     /** @hide */ @UnsupportedAppUsage public static final int FORCE_BT_CAR_DOCK = 6;
1304     /** @hide */ @UnsupportedAppUsage public static final int FORCE_BT_DESK_DOCK = 7;
1305     /** @hide */ @UnsupportedAppUsage public static final int FORCE_ANALOG_DOCK = 8;
1306     /** @hide */ @UnsupportedAppUsage public static final int FORCE_DIGITAL_DOCK = 9;
1307     /** @hide */ public static final int FORCE_NO_BT_A2DP = 10;
1308     /** @hide */ public static final int FORCE_SYSTEM_ENFORCED = 11;
1309     /** @hide */ public static final int FORCE_HDMI_SYSTEM_AUDIO_ENFORCED = 12;
1310     /** @hide */ public static final int FORCE_ENCODED_SURROUND_NEVER = 13;
1311     /** @hide */ public static final int FORCE_ENCODED_SURROUND_ALWAYS = 14;
1312     /** @hide */ public static final int FORCE_ENCODED_SURROUND_MANUAL = 15;
1313     /** @hide */ public static final int NUM_FORCE_CONFIG = 16;
1314     /** @hide */ public static final int FORCE_DEFAULT = FORCE_NONE;
1315 
1316     /** @hide */
forceUseConfigToString(int config)1317     public static String forceUseConfigToString(int config) {
1318         switch (config) {
1319             case FORCE_NONE: return "FORCE_NONE";
1320             case FORCE_SPEAKER: return "FORCE_SPEAKER";
1321             case FORCE_HEADPHONES: return "FORCE_HEADPHONES";
1322             case FORCE_BT_SCO: return "FORCE_BT_SCO";
1323             case FORCE_BT_A2DP: return "FORCE_BT_A2DP";
1324             case FORCE_WIRED_ACCESSORY: return "FORCE_WIRED_ACCESSORY";
1325             case FORCE_BT_CAR_DOCK: return "FORCE_BT_CAR_DOCK";
1326             case FORCE_BT_DESK_DOCK: return "FORCE_BT_DESK_DOCK";
1327             case FORCE_ANALOG_DOCK: return "FORCE_ANALOG_DOCK";
1328             case FORCE_DIGITAL_DOCK: return "FORCE_DIGITAL_DOCK";
1329             case FORCE_NO_BT_A2DP: return "FORCE_NO_BT_A2DP";
1330             case FORCE_SYSTEM_ENFORCED: return "FORCE_SYSTEM_ENFORCED";
1331             case FORCE_HDMI_SYSTEM_AUDIO_ENFORCED: return "FORCE_HDMI_SYSTEM_AUDIO_ENFORCED";
1332             case FORCE_ENCODED_SURROUND_NEVER: return "FORCE_ENCODED_SURROUND_NEVER";
1333             case FORCE_ENCODED_SURROUND_ALWAYS: return "FORCE_ENCODED_SURROUND_ALWAYS";
1334             case FORCE_ENCODED_SURROUND_MANUAL: return "FORCE_ENCODED_SURROUND_MANUAL";
1335             default: return "unknown config (" + config + ")" ;
1336         }
1337     }
1338 
1339     // usage for setForceUse, must match audio_policy_force_use_t
1340     /** @hide */ public static final int FOR_COMMUNICATION = 0;
1341     /** @hide */ public static final int FOR_MEDIA = 1;
1342     /** @hide */ public static final int FOR_RECORD = 2;
1343     /** @hide */ public static final int FOR_DOCK = 3;
1344     /** @hide */ public static final int FOR_SYSTEM = 4;
1345     /** @hide */ public static final int FOR_HDMI_SYSTEM_AUDIO = 5;
1346     /** @hide */ public static final int FOR_ENCODED_SURROUND = 6;
1347     /** @hide */ public static final int FOR_VIBRATE_RINGING = 7;
1348     private static final int NUM_FORCE_USE = 8;
1349 
1350     /** @hide */
forceUseUsageToString(int usage)1351     public static String forceUseUsageToString(int usage) {
1352         switch (usage) {
1353             case FOR_COMMUNICATION: return "FOR_COMMUNICATION";
1354             case FOR_MEDIA: return "FOR_MEDIA";
1355             case FOR_RECORD: return "FOR_RECORD";
1356             case FOR_DOCK: return "FOR_DOCK";
1357             case FOR_SYSTEM: return "FOR_SYSTEM";
1358             case FOR_HDMI_SYSTEM_AUDIO: return "FOR_HDMI_SYSTEM_AUDIO";
1359             case FOR_ENCODED_SURROUND: return "FOR_ENCODED_SURROUND";
1360             case FOR_VIBRATE_RINGING: return "FOR_VIBRATE_RINGING";
1361             default: return "unknown usage (" + usage + ")" ;
1362         }
1363     }
1364 
1365     /** @hide Wrapper for native methods called from AudioService */
setStreamVolumeIndexAS(int stream, int index, int device)1366     public static int setStreamVolumeIndexAS(int stream, int index, int device) {
1367         if (DEBUG_VOLUME) {
1368             Log.i(TAG, "setStreamVolumeIndex: " + STREAM_NAMES[stream]
1369                     + " dev=" + Integer.toHexString(device) + " idx=" + index);
1370         }
1371         return setStreamVolumeIndex(stream, index, device);
1372     }
1373 
1374     // usage for AudioRecord.startRecordingSync(), must match AudioSystem::sync_event_t
1375     /** @hide */ public static final int SYNC_EVENT_NONE = 0;
1376     /** @hide */ public static final int SYNC_EVENT_PRESENTATION_COMPLETE = 1;
1377 
1378     /**
1379      * @hide
1380      * @return command completion status, one of {@link #AUDIO_STATUS_OK},
1381      *     {@link #AUDIO_STATUS_ERROR} or {@link #AUDIO_STATUS_SERVER_DIED}
1382      */
1383     @UnsupportedAppUsage
setDeviceConnectionState(int device, int state, String device_address, String device_name, int codecFormat)1384     public static native int setDeviceConnectionState(int device, int state,
1385                                                       String device_address, String device_name,
1386                                                       int codecFormat);
1387     /** @hide */
1388     @UnsupportedAppUsage
getDeviceConnectionState(int device, String device_address)1389     public static native int getDeviceConnectionState(int device, String device_address);
1390     /** @hide */
handleDeviceConfigChange(int device, String device_address, String device_name, int codecFormat)1391     public static native int handleDeviceConfigChange(int device,
1392                                                       String device_address,
1393                                                       String device_name,
1394                                                       int codecFormat);
1395     /** @hide */
1396     @UnsupportedAppUsage
setPhoneState(int state)1397     public static int setPhoneState(int state) {
1398         Log.w(TAG, "Do not use this method! Use AudioManager.setMode() instead.");
1399         return 0;
1400     }
1401     /**
1402      * @hide
1403      * Send the current audio mode to audio policy manager and audio HAL.
1404      * @param state the audio mode
1405      * @param uid the UID of the app owning the audio mode
1406      * @return command completion status.
1407      */
setPhoneState(int state, int uid)1408     public static native int setPhoneState(int state, int uid);
1409     /** @hide */
1410     @UnsupportedAppUsage
setForceUse(int usage, int config)1411     public static native int setForceUse(int usage, int config);
1412     /** @hide */
1413     @UnsupportedAppUsage
getForceUse(int usage)1414     public static native int getForceUse(int usage);
1415     /** @hide */
1416     @UnsupportedAppUsage
initStreamVolume(int stream, int indexMin, int indexMax)1417     public static native int initStreamVolume(int stream, int indexMin, int indexMax);
1418     @UnsupportedAppUsage
setStreamVolumeIndex(int stream, int index, int device)1419     private static native int setStreamVolumeIndex(int stream, int index, int device);
1420     /** @hide */
getStreamVolumeIndex(int stream, int device)1421     public static native int getStreamVolumeIndex(int stream, int device);
1422     /**
1423      * @hide
1424      * set a volume for the given {@link AudioAttributes} and for all other stream that belong to
1425      * the same volume group.
1426      * @param attributes the {@link AudioAttributes} to be considered
1427      * @param index to be applied
1428      * @param device the volume device to be considered
1429      * @return command completion status.
1430      */
setVolumeIndexForAttributes(@onNull AudioAttributes attributes, int index, int device)1431     public static native int setVolumeIndexForAttributes(@NonNull AudioAttributes attributes,
1432                                                          int index, int device);
1433    /**
1434     * @hide
1435     * get the volume index for the given {@link AudioAttributes}.
1436     * @param attributes the {@link AudioAttributes} to be considered
1437     * @param device the volume device to be considered
1438     * @return volume index for the given {@link AudioAttributes} and volume device.
1439     */
getVolumeIndexForAttributes(@onNull AudioAttributes attributes, int device)1440     public static native int getVolumeIndexForAttributes(@NonNull AudioAttributes attributes,
1441                                                          int device);
1442     /**
1443      * @hide
1444      * get the minimum volume index for the given {@link AudioAttributes}.
1445      * @param attributes the {@link AudioAttributes} to be considered
1446      * @return minimum volume index for the given {@link AudioAttributes}.
1447      */
getMinVolumeIndexForAttributes(@onNull AudioAttributes attributes)1448     public static native int getMinVolumeIndexForAttributes(@NonNull AudioAttributes attributes);
1449     /**
1450      * @hide
1451      * get the maximum volume index for the given {@link AudioAttributes}.
1452      * @param attributes the {@link AudioAttributes} to be considered
1453      * @return maximum volume index for the given {@link AudioAttributes}.
1454      */
getMaxVolumeIndexForAttributes(@onNull AudioAttributes attributes)1455     public static native int getMaxVolumeIndexForAttributes(@NonNull AudioAttributes attributes);
1456 
1457     /** @hide */
setMasterVolume(float value)1458     public static native int setMasterVolume(float value);
1459     /** @hide */
getMasterVolume()1460     public static native float getMasterVolume();
1461     /** @hide */
1462     @UnsupportedAppUsage
setMasterMute(boolean mute)1463     public static native int setMasterMute(boolean mute);
1464     /** @hide */
1465     @UnsupportedAppUsage
getMasterMute()1466     public static native boolean getMasterMute();
1467     /** @hide */
1468     @UnsupportedAppUsage
getDevicesForStream(int stream)1469     public static native int getDevicesForStream(int stream);
1470 
1471     /**
1472      * @hide
1473      * Do not use directly, see {@link AudioManager#getDevicesForAttributes(AudioAttributes)}
1474      * Get the audio devices that would be used for the routing of the given audio attributes.
1475      * @param attributes the {@link AudioAttributes} for which the routing is being queried
1476      * @return an empty list if there was an issue with the request, a list of audio devices
1477      *   otherwise (typically one device, except for duplicated paths).
1478      */
getDevicesForAttributes( @onNull AudioAttributes attributes)1479     public static @NonNull ArrayList<AudioDeviceAttributes> getDevicesForAttributes(
1480             @NonNull AudioAttributes attributes) {
1481         Objects.requireNonNull(attributes);
1482         final AudioDeviceAttributes[] devices = new AudioDeviceAttributes[MAX_DEVICE_ROUTING];
1483         final int res = getDevicesForAttributes(attributes, devices);
1484         final ArrayList<AudioDeviceAttributes> routeDevices = new ArrayList<>();
1485         if (res != SUCCESS) {
1486             Log.e(TAG, "error " + res + " in getDevicesForAttributes for " + attributes);
1487             return routeDevices;
1488         }
1489 
1490         for (AudioDeviceAttributes device : devices) {
1491             if (device != null) {
1492                 routeDevices.add(device);
1493             }
1494         }
1495         return routeDevices;
1496     }
1497 
1498     /**
1499      * Maximum number of audio devices a track is ever routed to, determines the size of the
1500      * array passed to {@link #getDevicesForAttributes(AudioAttributes, AudioDeviceAttributes[])}
1501      */
1502     private static final int MAX_DEVICE_ROUTING = 4;
1503 
getDevicesForAttributes(@onNull AudioAttributes aa, @NonNull AudioDeviceAttributes[] devices)1504     private static native int getDevicesForAttributes(@NonNull AudioAttributes aa,
1505                                                       @NonNull AudioDeviceAttributes[] devices);
1506 
1507     /** @hide returns true if master mono is enabled. */
getMasterMono()1508     public static native boolean getMasterMono();
1509     /** @hide enables or disables the master mono mode. */
setMasterMono(boolean mono)1510     public static native int setMasterMono(boolean mono);
1511     /** @hide enables or disables the RTT mode. */
setRttEnabled(boolean enabled)1512     public static native int setRttEnabled(boolean enabled);
1513 
1514     /** @hide returns master balance value in range -1.f -> 1.f, where 0.f is dead center. */
1515     @TestApi
getMasterBalance()1516     public static native float getMasterBalance();
1517     /** @hide Changes the audio balance of the device. */
1518     @TestApi
setMasterBalance(float balance)1519     public static native int setMasterBalance(float balance);
1520 
1521     // helpers for android.media.AudioManager.getProperty(), see description there for meaning
1522     /** @hide */
1523     @UnsupportedAppUsage(trackingBug = 134049522)
getPrimaryOutputSamplingRate()1524     public static native int getPrimaryOutputSamplingRate();
1525     /** @hide */
1526     @UnsupportedAppUsage(trackingBug = 134049522)
getPrimaryOutputFrameCount()1527     public static native int getPrimaryOutputFrameCount();
1528     /** @hide */
1529     @UnsupportedAppUsage
getOutputLatency(int stream)1530     public static native int getOutputLatency(int stream);
1531 
1532     /** @hide */
setLowRamDevice(boolean isLowRamDevice, long totalMemory)1533     public static native int setLowRamDevice(boolean isLowRamDevice, long totalMemory);
1534     /** @hide */
1535     @UnsupportedAppUsage
checkAudioFlinger()1536     public static native int checkAudioFlinger();
1537 
1538     /** @hide */
listAudioPorts(ArrayList<AudioPort> ports, int[] generation)1539     public static native int listAudioPorts(ArrayList<AudioPort> ports, int[] generation);
1540     /** @hide */
createAudioPatch(AudioPatch[] patch, AudioPortConfig[] sources, AudioPortConfig[] sinks)1541     public static native int createAudioPatch(AudioPatch[] patch,
1542                                             AudioPortConfig[] sources, AudioPortConfig[] sinks);
1543     /** @hide */
releaseAudioPatch(AudioPatch patch)1544     public static native int releaseAudioPatch(AudioPatch patch);
1545     /** @hide */
listAudioPatches(ArrayList<AudioPatch> patches, int[] generation)1546     public static native int listAudioPatches(ArrayList<AudioPatch> patches, int[] generation);
1547     /** @hide */
setAudioPortConfig(AudioPortConfig config)1548     public static native int setAudioPortConfig(AudioPortConfig config);
1549 
1550     /** @hide */
startAudioSource(AudioPortConfig config, AudioAttributes audioAttributes)1551     public static native int startAudioSource(AudioPortConfig config,
1552                                               AudioAttributes audioAttributes);
1553     /** @hide */
stopAudioSource(int handle)1554     public static native int stopAudioSource(int handle);
1555 
1556     // declare this instance as having a dynamic policy callback handler
native_register_dynamic_policy_callback()1557     private static native final void native_register_dynamic_policy_callback();
1558     // declare this instance as having a recording configuration update callback handler
native_register_recording_callback()1559     private static native final void native_register_recording_callback();
1560 
1561     // must be kept in sync with value in include/system/audio.h
1562     /** @hide */ public static final int AUDIO_HW_SYNC_INVALID = 0;
1563 
1564     /** @hide */
getAudioHwSyncForSession(int sessionId)1565     public static native int getAudioHwSyncForSession(int sessionId);
1566 
1567     /** @hide */
registerPolicyMixes(ArrayList<AudioMix> mixes, boolean register)1568     public static native int registerPolicyMixes(ArrayList<AudioMix> mixes, boolean register);
1569 
1570     /** @hide see AudioPolicy.setUidDeviceAffinities() */
setUidDeviceAffinities(int uid, @NonNull int[] types, @NonNull String[] addresses)1571     public static native int setUidDeviceAffinities(int uid, @NonNull int[] types,
1572             @NonNull String[] addresses);
1573 
1574     /** @hide see AudioPolicy.removeUidDeviceAffinities() */
removeUidDeviceAffinities(int uid)1575     public static native int removeUidDeviceAffinities(int uid);
1576 
1577     /** @hide see AudioPolicy.setUserIdDeviceAffinities() */
setUserIdDeviceAffinities(int userId, @NonNull int[] types, @NonNull String[] addresses)1578     public static native int setUserIdDeviceAffinities(int userId, @NonNull int[] types,
1579             @NonNull String[] addresses);
1580 
1581     /** @hide see AudioPolicy.removeUserIdDeviceAffinities() */
removeUserIdDeviceAffinities(int userId)1582     public static native int removeUserIdDeviceAffinities(int userId);
1583 
1584     /** @hide */
systemReady()1585     public static native int systemReady();
1586 
1587     /** @hide */
getStreamVolumeDB(int stream, int index, int device)1588     public static native float getStreamVolumeDB(int stream, int index, int device);
1589 
1590     /**
1591      * @hide
1592      * Communicate supported system usages to audio policy service.
1593      */
setSupportedSystemUsages(int[] systemUsages)1594     public static native int setSupportedSystemUsages(int[] systemUsages);
1595 
1596     /**
1597      * @hide
1598      * @see AudioManager#setAllowedCapturePolicy()
1599      */
setAllowedCapturePolicy(int uid, int flags)1600     public static native int setAllowedCapturePolicy(int uid, int flags);
1601 
isOffloadSupported(@onNull AudioFormat format, @NonNull AudioAttributes attr)1602     static boolean isOffloadSupported(@NonNull AudioFormat format, @NonNull AudioAttributes attr) {
1603         return native_is_offload_supported(format.getEncoding(), format.getSampleRate(),
1604                 format.getChannelMask(), format.getChannelIndexMask(),
1605                 attr.getVolumeControlStream());
1606     }
1607 
native_is_offload_supported(int encoding, int sampleRate, int channelMask, int channelIndexMask, int streamType)1608     private static native boolean native_is_offload_supported(int encoding, int sampleRate,
1609             int channelMask, int channelIndexMask, int streamType);
1610 
1611     /** @hide */
getMicrophones(ArrayList<MicrophoneInfo> microphonesInfo)1612     public static native int getMicrophones(ArrayList<MicrophoneInfo> microphonesInfo);
1613 
1614     /** @hide */
getSurroundFormats(Map<Integer, Boolean> surroundFormats, boolean reported)1615     public static native int getSurroundFormats(Map<Integer, Boolean> surroundFormats,
1616                                                 boolean reported);
1617 
1618     /**
1619      * @hide
1620      * Returns a list of audio formats (codec) supported on the A2DP offload path.
1621      */
getHwOffloadEncodingFormatsSupportedForA2DP( ArrayList<Integer> formatList)1622     public static native int getHwOffloadEncodingFormatsSupportedForA2DP(
1623             ArrayList<Integer> formatList);
1624 
1625     /** @hide */
setSurroundFormatEnabled(int audioFormat, boolean enabled)1626     public static native int setSurroundFormatEnabled(int audioFormat, boolean enabled);
1627 
1628     /**
1629      * @hide
1630      * Communicate UID of active assistant to audio policy service.
1631      */
setAssistantUid(int uid)1632     public static native int setAssistantUid(int uid);
1633 
1634     /**
1635      * @hide
1636      * Communicate UIDs of active accessibility services to audio policy service.
1637      */
setA11yServicesUids(int[] uids)1638     public static native int setA11yServicesUids(int[] uids);
1639 
1640     /**
1641      * @hide
1642      * Communicate UID of current InputMethodService to audio policy service.
1643      */
setCurrentImeUid(int uid)1644     public static native int setCurrentImeUid(int uid);
1645 
1646 
1647     /**
1648      * @hide
1649      * @see AudioManager#isHapticPlaybackSupported()
1650      */
isHapticPlaybackSupported()1651     public static native boolean isHapticPlaybackSupported();
1652 
1653     /**
1654      * @hide
1655      * Send audio HAL server process pids to native audioserver process for use
1656      * when generating audio HAL servers tombstones
1657      */
setAudioHalPids(int[] pids)1658     public static native int setAudioHalPids(int[] pids);
1659 
1660     /**
1661      * @hide
1662      * @see AudioManager#isCallScreeningModeSupported()
1663      */
isCallScreeningModeSupported()1664     public static native boolean isCallScreeningModeSupported();
1665 
1666     // use case routing by product strategy
1667 
1668     /**
1669      * @hide
1670      * Sets the preferred device to use for a given audio strategy in the audio policy engine
1671      * @param strategy the id of the strategy to configure
1672      * @param device the device type and address to route to when available
1673      * @return {@link #SUCCESS} if successfully set
1674      */
setPreferredDeviceForStrategy( int strategy, @NonNull AudioDeviceAttributes device)1675     public static int setPreferredDeviceForStrategy(
1676             int strategy, @NonNull AudioDeviceAttributes device) {
1677         return setPreferredDeviceForStrategy(strategy,
1678                 AudioDeviceInfo.convertDeviceTypeToInternalDevice(device.getType()),
1679                 device.getAddress());
1680     }
1681     /**
1682      * @hide
1683      * Set device routing per product strategy.
1684      * @param strategy the id of the strategy to configure
1685      * @param deviceType the native device type, NOT AudioDeviceInfo types
1686      * @param deviceAddress the address of the device
1687      * @return {@link #SUCCESS} if successfully set
1688      */
setPreferredDeviceForStrategy( int strategy, int deviceType, String deviceAddress)1689     private static native int setPreferredDeviceForStrategy(
1690             int strategy, int deviceType, String deviceAddress);
1691 
1692     /**
1693      * @hide
1694      * Remove preferred routing for the strategy
1695      * @param strategy the id of the strategy to configure
1696      * @return {@link #SUCCESS} if successfully removed
1697      */
removePreferredDeviceForStrategy(int strategy)1698     public static native int removePreferredDeviceForStrategy(int strategy);
1699 
1700     /**
1701      * @hide
1702      * Query previously set preferred device for a strategy
1703      * @param strategy the id of the strategy to query for
1704      * @param device an array of size 1 that will contain the preferred device, or null if
1705      *               none was set
1706      * @return {@link #SUCCESS} if there is a preferred device and it was successfully retrieved
1707      *     and written to the array
1708      */
getPreferredDeviceForStrategy(int strategy, AudioDeviceAttributes[] device)1709     public static native int getPreferredDeviceForStrategy(int strategy,
1710                                                            AudioDeviceAttributes[] device);
1711 
1712     // Items shared with audio service
1713 
1714     /**
1715      * @hide
1716      * The delay before playing a sound. This small period exists so the user
1717      * can press another key (non-volume keys, too) to have it NOT be audible.
1718      * <p>
1719      * PhoneWindow will implement this part.
1720      */
1721     public static final int PLAY_SOUND_DELAY = 300;
1722 
1723     /**
1724      * @hide
1725      * Constant to identify a focus stack entry that is used to hold the focus while the phone
1726      * is ringing or during a call. Used by com.android.internal.telephony.CallManager when
1727      * entering and exiting calls.
1728      */
1729     public final static String IN_VOICE_COMM_FOCUS_ID = "AudioFocus_For_Phone_Ring_And_Calls";
1730 
1731     /**
1732      * @hide
1733      * @see AudioManager#setVibrateSetting(int, int)
1734      */
getValueForVibrateSetting(int existingValue, int vibrateType, int vibrateSetting)1735     public static int getValueForVibrateSetting(int existingValue, int vibrateType,
1736             int vibrateSetting) {
1737 
1738         // First clear the existing setting. Each vibrate type has two bits in
1739         // the value. Note '3' is '11' in binary.
1740         existingValue &= ~(3 << (vibrateType * 2));
1741 
1742         // Set into the old value
1743         existingValue |= (vibrateSetting & 3) << (vibrateType * 2);
1744 
1745         return existingValue;
1746     }
1747 
1748     /** @hide */
getDefaultStreamVolume(int streamType)1749     public static int getDefaultStreamVolume(int streamType) {
1750         return DEFAULT_STREAM_VOLUME[streamType];
1751     }
1752 
1753     /** @hide */
1754     public static int[] DEFAULT_STREAM_VOLUME = new int[] {
1755         4,  // STREAM_VOICE_CALL
1756         7,  // STREAM_SYSTEM
1757         5,  // STREAM_RING
1758         5, // STREAM_MUSIC
1759         6,  // STREAM_ALARM
1760         5,  // STREAM_NOTIFICATION
1761         7,  // STREAM_BLUETOOTH_SCO
1762         7,  // STREAM_SYSTEM_ENFORCED
1763         5, // STREAM_DTMF
1764         5, // STREAM_TTS
1765         5, // STREAM_ACCESSIBILITY
1766         5, // STREAM_ASSISTANT
1767     };
1768 
1769     /** @hide */
streamToString(int stream)1770     public static String streamToString(int stream) {
1771         if (stream >= 0 && stream < STREAM_NAMES.length) return STREAM_NAMES[stream];
1772         if (stream == AudioManager.USE_DEFAULT_STREAM_TYPE) return "USE_DEFAULT_STREAM_TYPE";
1773         return "UNKNOWN_STREAM_" + stream;
1774     }
1775 
1776     /** @hide The platform has no specific capabilities */
1777     public static final int PLATFORM_DEFAULT = 0;
1778     /** @hide The platform is voice call capable (a phone) */
1779     public static final int PLATFORM_VOICE = 1;
1780     /** @hide The platform is a television or a set-top box */
1781     public static final int PLATFORM_TELEVISION = 2;
1782 
1783     /**
1784      * @hide
1785      * Return the platform type that this is running on. One of:
1786      * <ul>
1787      * <li>{@link #PLATFORM_VOICE}</li>
1788      * <li>{@link #PLATFORM_TELEVISION}</li>
1789      * <li>{@link #PLATFORM_DEFAULT}</li>
1790      * </ul>
1791      */
getPlatformType(Context context)1792     public static int getPlatformType(Context context) {
1793         if (((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
1794                 .isVoiceCapable()) {
1795             return PLATFORM_VOICE;
1796         } else if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) {
1797             return PLATFORM_TELEVISION;
1798         } else {
1799             return PLATFORM_DEFAULT;
1800         }
1801     }
1802 
1803     /**
1804      * @hide
1805      * @return whether the system uses a single volume stream.
1806      */
isSingleVolume(Context context)1807     public static boolean isSingleVolume(Context context) {
1808         boolean forceSingleVolume = context.getResources().getBoolean(
1809                 com.android.internal.R.bool.config_single_volume);
1810         return getPlatformType(context) == PLATFORM_TELEVISION || forceSingleVolume;
1811     }
1812 
1813     /**
1814      * @hide
1815      * Return a set of audio device types from a bit mask audio device type, which may
1816      * represent multiple audio device types.
1817      * FIXME: Remove this when getting ride of bit mask usage of audio device types.
1818      */
generateAudioDeviceTypesSet(int types)1819     public static Set<Integer> generateAudioDeviceTypesSet(int types) {
1820         Set<Integer> deviceTypes = new HashSet<>();
1821         Set<Integer> allDeviceTypes =
1822                 (types & DEVICE_BIT_IN) == 0 ? DEVICE_OUT_ALL_SET : DEVICE_IN_ALL_SET;
1823         for (int deviceType : allDeviceTypes) {
1824             if ((types & deviceType) == deviceType) {
1825                 deviceTypes.add(deviceType);
1826             }
1827         }
1828         return deviceTypes;
1829     }
1830 
1831     /**
1832      * @hide
1833      * Return the intersection of two audio device types collections.
1834      */
intersectionAudioDeviceTypes( @onNull Set<Integer> a, @NonNull Set<Integer> b)1835     public static Set<Integer> intersectionAudioDeviceTypes(
1836             @NonNull Set<Integer> a, @NonNull Set<Integer> b) {
1837         Set<Integer> intersection = new HashSet<>(a);
1838         intersection.retainAll(b);
1839         return intersection;
1840     }
1841 
1842     /**
1843      * @hide
1844      * Return true if the audio device types collection only contains the given device type.
1845      */
isSingleAudioDeviceType(@onNull Set<Integer> types, int type)1846     public static boolean isSingleAudioDeviceType(@NonNull Set<Integer> types, int type) {
1847         return types.size() == 1 && types.contains(type);
1848     }
1849 
1850     /** @hide */
1851     public static final int DEFAULT_MUTE_STREAMS_AFFECTED =
1852             (1 << STREAM_MUSIC) |
1853             (1 << STREAM_RING) |
1854             (1 << STREAM_NOTIFICATION) |
1855             (1 << STREAM_SYSTEM) |
1856             (1 << STREAM_VOICE_CALL) |
1857             (1 << STREAM_BLUETOOTH_SCO);
1858 
1859     /**
1860      * @hide
1861      * Event posted by AudioTrack and AudioRecord JNI (JNIDeviceCallback) when routing changes.
1862      * Keep in sync with core/jni/android_media_DeviceCallback.h.
1863      */
1864     final static int NATIVE_EVENT_ROUTING_CHANGE = 1000;
1865 }
1866 
1867