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