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