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