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.NonNull; 20 import android.annotation.TestApi; 21 import android.annotation.UnsupportedAppUsage; 22 import android.bluetooth.BluetoothCodecConfig; 23 import android.content.Context; 24 import android.content.pm.PackageManager; 25 import android.media.audiofx.AudioEffect; 26 import android.media.audiopolicy.AudioMix; 27 import android.util.Log; 28 29 import java.util.ArrayList; 30 import java.util.Map; 31 32 /* IF YOU CHANGE ANY OF THE CONSTANTS IN THIS FILE, DO NOT FORGET 33 * TO UPDATE THE CORRESPONDING NATIVE GLUE AND AudioManager.java. 34 * THANK YOU FOR YOUR COOPERATION. 35 */ 36 37 /** 38 * @hide 39 */ 40 public class AudioSystem 41 { 42 private static final boolean DEBUG_VOLUME = false; 43 44 private static final String TAG = "AudioSystem"; 45 /* These values must be kept in sync with system/audio.h */ 46 /* 47 * If these are modified, please also update Settings.System.VOLUME_SETTINGS 48 * and attrs.xml and AudioManager.java. 49 */ 50 /** Used to identify the default audio stream volume */ 51 public static final int STREAM_DEFAULT = -1; 52 /** Used to identify the volume of audio streams for phone calls */ 53 public static final int STREAM_VOICE_CALL = 0; 54 /** Used to identify the volume of audio streams for system sounds */ 55 public static final int STREAM_SYSTEM = 1; 56 /** Used to identify the volume of audio streams for the phone ring and message alerts */ 57 public static final int STREAM_RING = 2; 58 /** Used to identify the volume of audio streams for music playback */ 59 public static final int STREAM_MUSIC = 3; 60 /** Used to identify the volume of audio streams for alarms */ 61 public static final int STREAM_ALARM = 4; 62 /** Used to identify the volume of audio streams for notifications */ 63 public static final int STREAM_NOTIFICATION = 5; 64 /** Used to identify the volume of audio streams for phone calls when connected on bluetooth */ 65 public static final int STREAM_BLUETOOTH_SCO = 6; 66 /** Used to identify the volume of audio streams for enforced system sounds in certain 67 * countries (e.g camera in Japan) */ 68 @UnsupportedAppUsage 69 public static final int STREAM_SYSTEM_ENFORCED = 7; 70 /** Used to identify the volume of audio streams for DTMF tones */ 71 public static final int STREAM_DTMF = 8; 72 /** Used to identify the volume of audio streams exclusively transmitted through the 73 * speaker (TTS) of the device */ 74 public static final int STREAM_TTS = 9; 75 /** Used to identify the volume of audio streams for accessibility prompts */ 76 public static final int STREAM_ACCESSIBILITY = 10; 77 /** 78 * @deprecated Use {@link #numStreamTypes() instead} 79 */ 80 public static final int NUM_STREAMS = 5; 81 82 /** Maximum value for AudioTrack channel count 83 * @hide public for MediaCode only, do not un-hide or change to a numeric literal 84 */ 85 public static final int OUT_CHANNEL_COUNT_MAX = native_get_FCC_8(); native_get_FCC_8()86 private static native int native_get_FCC_8(); 87 88 // Expose only the getter method publicly so we can change it in the future 89 private static final int NUM_STREAM_TYPES = 11; 90 @UnsupportedAppUsage getNumStreamTypes()91 public static final int getNumStreamTypes() { return NUM_STREAM_TYPES; } 92 93 public static final String[] STREAM_NAMES = new String[] { 94 "STREAM_VOICE_CALL", 95 "STREAM_SYSTEM", 96 "STREAM_RING", 97 "STREAM_MUSIC", 98 "STREAM_ALARM", 99 "STREAM_NOTIFICATION", 100 "STREAM_BLUETOOTH_SCO", 101 "STREAM_SYSTEM_ENFORCED", 102 "STREAM_DTMF", 103 "STREAM_TTS", 104 "STREAM_ACCESSIBILITY" 105 }; 106 107 /* 108 * Sets the microphone mute on or off. 109 * 110 * @param on set <var>true</var> to mute the microphone; 111 * <var>false</var> to turn mute off 112 * @return command completion status see AUDIO_STATUS_OK, see AUDIO_STATUS_ERROR 113 */ 114 @UnsupportedAppUsage muteMicrophone(boolean on)115 public static native int muteMicrophone(boolean on); 116 117 /* 118 * Checks whether the microphone mute is on or off. 119 * 120 * @return true if microphone is muted, false if it's not 121 */ 122 @UnsupportedAppUsage isMicrophoneMuted()123 public static native boolean isMicrophoneMuted(); 124 125 /* modes for setPhoneState, must match AudioSystem.h audio_mode */ 126 public static final int MODE_INVALID = -2; 127 public static final int MODE_CURRENT = -1; 128 public static final int MODE_NORMAL = 0; 129 public static final int MODE_RINGTONE = 1; 130 public static final int MODE_IN_CALL = 2; 131 public static final int MODE_IN_COMMUNICATION = 3; 132 public static final int NUM_MODES = 4; 133 modeToString(int mode)134 public static String modeToString(int mode) { 135 switch (mode) { 136 case MODE_CURRENT: return "MODE_CURRENT"; 137 case MODE_IN_CALL: return "MODE_IN_CALL"; 138 case MODE_IN_COMMUNICATION: return "MODE_IN_COMMUNICATION"; 139 case MODE_INVALID: return "MODE_INVALID"; 140 case MODE_NORMAL: return "MODE_NORMAL"; 141 case MODE_RINGTONE: return "MODE_RINGTONE"; 142 default: return "unknown mode (" + mode + ")"; 143 } 144 } 145 146 /* Formats for A2DP codecs, must match system/audio-base.h audio_format_t */ 147 public static final int AUDIO_FORMAT_INVALID = 0xFFFFFFFF; 148 public static final int AUDIO_FORMAT_DEFAULT = 0; 149 public static final int AUDIO_FORMAT_AAC = 0x04000000; 150 public static final int AUDIO_FORMAT_SBC = 0x1F000000; 151 public static final int AUDIO_FORMAT_APTX = 0x20000000; 152 public static final int AUDIO_FORMAT_APTX_HD = 0x21000000; 153 public static final int AUDIO_FORMAT_LDAC = 0x23000000; 154 155 /** 156 * Convert audio format enum values to Bluetooth codec values 157 */ audioFormatToBluetoothSourceCodec(int audioFormat)158 public static int audioFormatToBluetoothSourceCodec(int audioFormat) { 159 switch (audioFormat) { 160 case AUDIO_FORMAT_AAC: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC; 161 case AUDIO_FORMAT_SBC: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC; 162 case AUDIO_FORMAT_APTX: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX; 163 case AUDIO_FORMAT_APTX_HD: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD; 164 case AUDIO_FORMAT_LDAC: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC; 165 default: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID; 166 } 167 } 168 169 /* Routing bits for the former setRouting/getRouting API */ 170 /** @deprecated */ 171 @Deprecated public static final int ROUTE_EARPIECE = (1 << 0); 172 /** @deprecated */ 173 @Deprecated public static final int ROUTE_SPEAKER = (1 << 1); 174 /** @deprecated use {@link #ROUTE_BLUETOOTH_SCO} */ 175 @Deprecated public static final int ROUTE_BLUETOOTH = (1 << 2); 176 /** @deprecated */ 177 @Deprecated public static final int ROUTE_BLUETOOTH_SCO = (1 << 2); 178 /** @deprecated */ 179 @Deprecated public static final int ROUTE_HEADSET = (1 << 3); 180 /** @deprecated */ 181 @Deprecated public static final int ROUTE_BLUETOOTH_A2DP = (1 << 4); 182 /** @deprecated */ 183 @Deprecated public static final int ROUTE_ALL = 0xFFFFFFFF; 184 185 // Keep in sync with system/media/audio/include/system/audio.h 186 public static final int AUDIO_SESSION_ALLOCATE = 0; 187 188 /* 189 * Checks whether the specified stream type is active. 190 * 191 * return true if any track playing on this stream is active. 192 */ 193 @UnsupportedAppUsage isStreamActive(int stream, int inPastMs)194 public static native boolean isStreamActive(int stream, int inPastMs); 195 196 /* 197 * Checks whether the specified stream type is active on a remotely connected device. The notion 198 * of what constitutes a remote device is enforced by the audio policy manager of the platform. 199 * 200 * return true if any track playing on this stream is active on a remote device. 201 */ isStreamActiveRemotely(int stream, int inPastMs)202 public static native boolean isStreamActiveRemotely(int stream, int inPastMs); 203 204 /* 205 * Checks whether the specified audio source is active. 206 * 207 * return true if any recorder using this source is currently recording 208 */ 209 @UnsupportedAppUsage isSourceActive(int source)210 public static native boolean isSourceActive(int source); 211 212 /* 213 * Returns a new unused audio session ID 214 */ newAudioSessionId()215 public static native int newAudioSessionId(); 216 217 /* 218 * Returns a new unused audio player ID 219 */ newAudioPlayerId()220 public static native int newAudioPlayerId(); 221 222 /** 223 * Returns a new unused audio recorder ID 224 */ newAudioRecorderId()225 public static native int newAudioRecorderId(); 226 227 228 /* 229 * Sets a group generic audio configuration parameters. The use of these parameters 230 * are platform dependent, see libaudio 231 * 232 * param keyValuePairs list of parameters key value pairs in the form: 233 * key1=value1;key2=value2;... 234 */ 235 @UnsupportedAppUsage setParameters(String keyValuePairs)236 public static native int setParameters(String keyValuePairs); 237 238 /* 239 * Gets a group generic audio configuration parameters. The use of these parameters 240 * are platform dependent, see libaudio 241 * 242 * param keys list of parameters 243 * return value: list of parameters key value pairs in the form: 244 * key1=value1;key2=value2;... 245 */ 246 @UnsupportedAppUsage getParameters(String keys)247 public static native String getParameters(String keys); 248 249 // These match the enum AudioError in frameworks/base/core/jni/android_media_AudioSystem.cpp 250 /* Command sucessful or Media server restarted. see ErrorCallback */ 251 public static final int AUDIO_STATUS_OK = 0; 252 /* Command failed or unspecified audio error. see ErrorCallback */ 253 public static final int AUDIO_STATUS_ERROR = 1; 254 /* Media server died. see ErrorCallback */ 255 public static final int AUDIO_STATUS_SERVER_DIED = 100; 256 257 private static ErrorCallback mErrorCallback; 258 259 /* 260 * Handles the audio error callback. 261 */ 262 public interface ErrorCallback 263 { 264 /* 265 * Callback for audio server errors. 266 * param error error code: 267 * - AUDIO_STATUS_OK 268 * - AUDIO_STATUS_SERVER_DIED 269 * - AUDIO_STATUS_ERROR 270 */ onError(int error)271 void onError(int error); 272 }; 273 274 /* 275 * Registers a callback to be invoked when an error occurs. 276 * @param cb the callback to run 277 */ 278 @UnsupportedAppUsage setErrorCallback(ErrorCallback cb)279 public static void setErrorCallback(ErrorCallback cb) 280 { 281 synchronized (AudioSystem.class) { 282 mErrorCallback = cb; 283 if (cb != null) { 284 cb.onError(checkAudioFlinger()); 285 } 286 } 287 } 288 289 @UnsupportedAppUsage errorCallbackFromNative(int error)290 private static void errorCallbackFromNative(int error) 291 { 292 ErrorCallback errorCallback = null; 293 synchronized (AudioSystem.class) { 294 if (mErrorCallback != null) { 295 errorCallback = mErrorCallback; 296 } 297 } 298 if (errorCallback != null) { 299 errorCallback.onError(error); 300 } 301 } 302 303 /** 304 * Handles events from the audio policy manager about dynamic audio policies 305 * @see android.media.audiopolicy.AudioPolicy 306 */ 307 public interface DynamicPolicyCallback 308 { onDynamicPolicyMixStateUpdate(String regId, int state)309 void onDynamicPolicyMixStateUpdate(String regId, int state); 310 } 311 312 //keep in sync with include/media/AudioPolicy.h 313 private final static int DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE = 0; 314 315 private static DynamicPolicyCallback sDynPolicyCallback; 316 setDynamicPolicyCallback(DynamicPolicyCallback cb)317 public static void setDynamicPolicyCallback(DynamicPolicyCallback cb) 318 { 319 synchronized (AudioSystem.class) { 320 sDynPolicyCallback = cb; 321 native_register_dynamic_policy_callback(); 322 } 323 } 324 325 @UnsupportedAppUsage dynamicPolicyCallbackFromNative(int event, String regId, int val)326 private static void dynamicPolicyCallbackFromNative(int event, String regId, int val) 327 { 328 DynamicPolicyCallback cb = null; 329 synchronized (AudioSystem.class) { 330 if (sDynPolicyCallback != null) { 331 cb = sDynPolicyCallback; 332 } 333 } 334 if (cb != null) { 335 switch(event) { 336 case DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE: 337 cb.onDynamicPolicyMixStateUpdate(regId, val); 338 break; 339 default: 340 Log.e(TAG, "dynamicPolicyCallbackFromNative: unknown event " + event); 341 } 342 } 343 } 344 345 /** 346 * Handles events from the audio policy manager about recording events 347 * @see android.media.AudioManager.AudioRecordingCallback 348 */ 349 public interface AudioRecordingCallback 350 { 351 /** 352 * Callback for recording activity notifications events 353 * @param event 354 * @param riid recording identifier 355 * @param uid uid of the client app performing the recording 356 * @param session 357 * @param source 358 * @param recordingFormat an array of ints containing respectively the client and device 359 * recording configurations (2*3 ints), followed by the patch handle: 360 * index 0: client format 361 * 1: client channel mask 362 * 2: client sample rate 363 * 3: device format 364 * 4: device channel mask 365 * 5: device sample rate 366 * 6: patch handle 367 * @param packName package name of the client app performing the recording. NOT SUPPORTED 368 */ 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)369 void onRecordingConfigurationChanged(int event, int riid, int uid, int session, int source, 370 int portId, boolean silenced, int[] recordingFormat, 371 AudioEffect.Descriptor[] clienteffects, AudioEffect.Descriptor[] effects, 372 int activeSource, String packName); 373 } 374 375 private static AudioRecordingCallback sRecordingCallback; 376 setRecordingCallback(AudioRecordingCallback cb)377 public static void setRecordingCallback(AudioRecordingCallback cb) { 378 synchronized (AudioSystem.class) { 379 sRecordingCallback = cb; 380 native_register_recording_callback(); 381 } 382 } 383 384 /** 385 * Callback from native for recording configuration updates. 386 * @param event 387 * @param riid 388 * @param uid 389 * @param session 390 * @param source 391 * @param portId 392 * @param silenced 393 * @param recordingFormat see 394 * {@link AudioRecordingCallback#onRecordingConfigurationChanged(int, int, int, int, int, \ 395 int, boolean, int[], AudioEffect.Descriptor[], AudioEffect.Descriptor[], int, String)} 396 * for the description of the record format. 397 * @param cleintEffects 398 * @param effects 399 * @param activeSource 400 */ 401 @UnsupportedAppUsage recordingCallbackFromNative(int event, int riid, int uid, int session, int source, int portId, boolean silenced, int[] recordingFormat, AudioEffect.Descriptor[] clientEffects, AudioEffect.Descriptor[] effects, int activeSource)402 private static void recordingCallbackFromNative(int event, int riid, int uid, int session, 403 int source, int portId, boolean silenced, int[] recordingFormat, 404 AudioEffect.Descriptor[] clientEffects, AudioEffect.Descriptor[] effects, 405 int activeSource) { 406 AudioRecordingCallback cb = null; 407 synchronized (AudioSystem.class) { 408 cb = sRecordingCallback; 409 } 410 411 String clientEffectName = clientEffects.length == 0 ? "None" : clientEffects[0].name; 412 String effectName = effects.length == 0 ? "None" : effects[0].name; 413 414 if (cb != null) { 415 // TODO receive package name from native 416 cb.onRecordingConfigurationChanged(event, riid, uid, session, source, portId, silenced, 417 recordingFormat, clientEffects, effects, activeSource, ""); 418 } 419 } 420 421 /* 422 * Error codes used by public APIs (AudioTrack, AudioRecord, AudioManager ...) 423 * Must be kept in sync with frameworks/base/core/jni/android_media_AudioErrors.h 424 */ 425 public static final int SUCCESS = 0; 426 public static final int ERROR = -1; 427 public static final int BAD_VALUE = -2; 428 public static final int INVALID_OPERATION = -3; 429 public static final int PERMISSION_DENIED = -4; 430 public static final int NO_INIT = -5; 431 public static final int DEAD_OBJECT = -6; 432 public static final int WOULD_BLOCK = -7; 433 434 /* 435 * AudioPolicyService methods 436 */ 437 438 // 439 // audio device definitions: must be kept in sync with values in system/core/audio.h 440 // 441 442 public static final int DEVICE_NONE = 0x0; 443 // reserved bits 444 public static final int DEVICE_BIT_IN = 0x80000000; 445 public static final int DEVICE_BIT_DEFAULT = 0x40000000; 446 // output devices, be sure to update AudioManager.java also 447 @UnsupportedAppUsage 448 public static final int DEVICE_OUT_EARPIECE = 0x1; 449 @UnsupportedAppUsage 450 public static final int DEVICE_OUT_SPEAKER = 0x2; 451 @UnsupportedAppUsage 452 public static final int DEVICE_OUT_WIRED_HEADSET = 0x4; 453 @UnsupportedAppUsage 454 public static final int DEVICE_OUT_WIRED_HEADPHONE = 0x8; 455 @UnsupportedAppUsage 456 public static final int DEVICE_OUT_BLUETOOTH_SCO = 0x10; 457 @UnsupportedAppUsage 458 public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 0x20; 459 @UnsupportedAppUsage 460 public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 0x40; 461 @UnsupportedAppUsage 462 public static final int DEVICE_OUT_BLUETOOTH_A2DP = 0x80; 463 @UnsupportedAppUsage 464 public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 0x100; 465 @UnsupportedAppUsage 466 public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 0x200; 467 @UnsupportedAppUsage 468 public static final int DEVICE_OUT_AUX_DIGITAL = 0x400; 469 public static final int DEVICE_OUT_HDMI = DEVICE_OUT_AUX_DIGITAL; 470 @UnsupportedAppUsage 471 public static final int DEVICE_OUT_ANLG_DOCK_HEADSET = 0x800; 472 @UnsupportedAppUsage 473 public static final int DEVICE_OUT_DGTL_DOCK_HEADSET = 0x1000; 474 @UnsupportedAppUsage 475 public static final int DEVICE_OUT_USB_ACCESSORY = 0x2000; 476 @UnsupportedAppUsage 477 public static final int DEVICE_OUT_USB_DEVICE = 0x4000; 478 @UnsupportedAppUsage 479 public static final int DEVICE_OUT_REMOTE_SUBMIX = 0x8000; 480 @UnsupportedAppUsage 481 public static final int DEVICE_OUT_TELEPHONY_TX = 0x10000; 482 public static final int DEVICE_OUT_LINE = 0x20000; 483 public static final int DEVICE_OUT_HDMI_ARC = 0x40000; 484 public static final int DEVICE_OUT_SPDIF = 0x80000; 485 @UnsupportedAppUsage 486 public static final int DEVICE_OUT_FM = 0x100000; 487 public static final int DEVICE_OUT_AUX_LINE = 0x200000; 488 public static final int DEVICE_OUT_SPEAKER_SAFE = 0x400000; 489 public static final int DEVICE_OUT_IP = 0x800000; 490 public static final int DEVICE_OUT_BUS = 0x1000000; 491 public static final int DEVICE_OUT_PROXY = 0x2000000; 492 public static final int DEVICE_OUT_USB_HEADSET = 0x4000000; 493 public static final int DEVICE_OUT_HEARING_AID = 0x8000000; 494 495 public static final int DEVICE_OUT_DEFAULT = DEVICE_BIT_DEFAULT; 496 497 public static final int DEVICE_OUT_ALL = (DEVICE_OUT_EARPIECE | 498 DEVICE_OUT_SPEAKER | 499 DEVICE_OUT_WIRED_HEADSET | 500 DEVICE_OUT_WIRED_HEADPHONE | 501 DEVICE_OUT_BLUETOOTH_SCO | 502 DEVICE_OUT_BLUETOOTH_SCO_HEADSET | 503 DEVICE_OUT_BLUETOOTH_SCO_CARKIT | 504 DEVICE_OUT_BLUETOOTH_A2DP | 505 DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | 506 DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER | 507 DEVICE_OUT_HDMI | 508 DEVICE_OUT_ANLG_DOCK_HEADSET | 509 DEVICE_OUT_DGTL_DOCK_HEADSET | 510 DEVICE_OUT_USB_ACCESSORY | 511 DEVICE_OUT_USB_DEVICE | 512 DEVICE_OUT_REMOTE_SUBMIX | 513 DEVICE_OUT_TELEPHONY_TX | 514 DEVICE_OUT_LINE | 515 DEVICE_OUT_HDMI_ARC | 516 DEVICE_OUT_SPDIF | 517 DEVICE_OUT_FM | 518 DEVICE_OUT_AUX_LINE | 519 DEVICE_OUT_SPEAKER_SAFE | 520 DEVICE_OUT_IP | 521 DEVICE_OUT_BUS | 522 DEVICE_OUT_PROXY | 523 DEVICE_OUT_USB_HEADSET | 524 DEVICE_OUT_HEARING_AID | 525 DEVICE_OUT_DEFAULT); 526 public static final int DEVICE_OUT_ALL_A2DP = (DEVICE_OUT_BLUETOOTH_A2DP | 527 DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES | 528 DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER); 529 public static final int DEVICE_OUT_ALL_SCO = (DEVICE_OUT_BLUETOOTH_SCO | 530 DEVICE_OUT_BLUETOOTH_SCO_HEADSET | 531 DEVICE_OUT_BLUETOOTH_SCO_CARKIT); 532 @UnsupportedAppUsage 533 public static final int DEVICE_OUT_ALL_USB = (DEVICE_OUT_USB_ACCESSORY | 534 DEVICE_OUT_USB_DEVICE | 535 DEVICE_OUT_USB_HEADSET); 536 public static final int DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO = (DEVICE_OUT_AUX_LINE | 537 DEVICE_OUT_HDMI_ARC | 538 DEVICE_OUT_SPDIF); 539 public static final int DEVICE_ALL_HDMI_SYSTEM_AUDIO_AND_SPEAKER = 540 (DEVICE_OUT_ALL_HDMI_SYSTEM_AUDIO | 541 DEVICE_OUT_SPEAKER); 542 543 // input devices 544 @UnsupportedAppUsage 545 public static final int DEVICE_IN_COMMUNICATION = DEVICE_BIT_IN | 0x1; 546 @UnsupportedAppUsage 547 public static final int DEVICE_IN_AMBIENT = DEVICE_BIT_IN | 0x2; 548 @UnsupportedAppUsage 549 public static final int DEVICE_IN_BUILTIN_MIC = DEVICE_BIT_IN | 0x4; 550 @UnsupportedAppUsage 551 public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET = DEVICE_BIT_IN | 0x8; 552 @UnsupportedAppUsage 553 public static final int DEVICE_IN_WIRED_HEADSET = DEVICE_BIT_IN | 0x10; 554 @UnsupportedAppUsage 555 public static final int DEVICE_IN_AUX_DIGITAL = DEVICE_BIT_IN | 0x20; 556 public static final int DEVICE_IN_HDMI = DEVICE_IN_AUX_DIGITAL; 557 @UnsupportedAppUsage 558 public static final int DEVICE_IN_VOICE_CALL = DEVICE_BIT_IN | 0x40; 559 public static final int DEVICE_IN_TELEPHONY_RX = DEVICE_IN_VOICE_CALL; 560 @UnsupportedAppUsage 561 public static final int DEVICE_IN_BACK_MIC = DEVICE_BIT_IN | 0x80; 562 @UnsupportedAppUsage 563 public static final int DEVICE_IN_REMOTE_SUBMIX = DEVICE_BIT_IN | 0x100; 564 @UnsupportedAppUsage 565 public static final int DEVICE_IN_ANLG_DOCK_HEADSET = DEVICE_BIT_IN | 0x200; 566 @UnsupportedAppUsage 567 public static final int DEVICE_IN_DGTL_DOCK_HEADSET = DEVICE_BIT_IN | 0x400; 568 @UnsupportedAppUsage 569 public static final int DEVICE_IN_USB_ACCESSORY = DEVICE_BIT_IN | 0x800; 570 @UnsupportedAppUsage 571 public static final int DEVICE_IN_USB_DEVICE = DEVICE_BIT_IN | 0x1000; 572 public static final int DEVICE_IN_FM_TUNER = DEVICE_BIT_IN | 0x2000; 573 public static final int DEVICE_IN_TV_TUNER = DEVICE_BIT_IN | 0x4000; 574 public static final int DEVICE_IN_LINE = DEVICE_BIT_IN | 0x8000; 575 public static final int DEVICE_IN_SPDIF = DEVICE_BIT_IN | 0x10000; 576 @UnsupportedAppUsage 577 public static final int DEVICE_IN_BLUETOOTH_A2DP = DEVICE_BIT_IN | 0x20000; 578 public static final int DEVICE_IN_LOOPBACK = DEVICE_BIT_IN | 0x40000; 579 public static final int DEVICE_IN_IP = DEVICE_BIT_IN | 0x80000; 580 public static final int DEVICE_IN_BUS = DEVICE_BIT_IN | 0x100000; 581 public static final int DEVICE_IN_PROXY = DEVICE_BIT_IN | 0x1000000; 582 public static final int DEVICE_IN_USB_HEADSET = DEVICE_BIT_IN | 0x2000000; 583 public static final int DEVICE_IN_BLUETOOTH_BLE = DEVICE_BIT_IN | 0x4000000; 584 public static final int DEVICE_IN_HDMI_ARC = DEVICE_BIT_IN | 0x8000000; 585 public static final int DEVICE_IN_ECHO_REFERENCE = DEVICE_BIT_IN | 0x10000000; 586 @UnsupportedAppUsage 587 public static final int DEVICE_IN_DEFAULT = DEVICE_BIT_IN | DEVICE_BIT_DEFAULT; 588 589 public static final int DEVICE_IN_ALL = (DEVICE_IN_COMMUNICATION | 590 DEVICE_IN_AMBIENT | 591 DEVICE_IN_BUILTIN_MIC | 592 DEVICE_IN_BLUETOOTH_SCO_HEADSET | 593 DEVICE_IN_WIRED_HEADSET | 594 DEVICE_IN_HDMI | 595 DEVICE_IN_TELEPHONY_RX | 596 DEVICE_IN_BACK_MIC | 597 DEVICE_IN_REMOTE_SUBMIX | 598 DEVICE_IN_ANLG_DOCK_HEADSET | 599 DEVICE_IN_DGTL_DOCK_HEADSET | 600 DEVICE_IN_USB_ACCESSORY | 601 DEVICE_IN_USB_DEVICE | 602 DEVICE_IN_FM_TUNER | 603 DEVICE_IN_TV_TUNER | 604 DEVICE_IN_LINE | 605 DEVICE_IN_SPDIF | 606 DEVICE_IN_BLUETOOTH_A2DP | 607 DEVICE_IN_LOOPBACK | 608 DEVICE_IN_IP | 609 DEVICE_IN_BUS | 610 DEVICE_IN_PROXY | 611 DEVICE_IN_USB_HEADSET | 612 DEVICE_IN_BLUETOOTH_BLE | 613 DEVICE_IN_HDMI_ARC | 614 DEVICE_IN_ECHO_REFERENCE | 615 DEVICE_IN_DEFAULT); 616 public static final int DEVICE_IN_ALL_SCO = DEVICE_IN_BLUETOOTH_SCO_HEADSET; 617 public static final int DEVICE_IN_ALL_USB = (DEVICE_IN_USB_ACCESSORY | 618 DEVICE_IN_USB_DEVICE | 619 DEVICE_IN_USB_HEADSET); 620 621 // device states, must match AudioSystem::device_connection_state 622 @UnsupportedAppUsage 623 public static final int DEVICE_STATE_UNAVAILABLE = 0; 624 @UnsupportedAppUsage 625 public static final int DEVICE_STATE_AVAILABLE = 1; 626 private static final int NUM_DEVICE_STATES = 1; 627 deviceStateToString(int state)628 public static String deviceStateToString(int state) { 629 switch (state) { 630 case DEVICE_STATE_UNAVAILABLE: return "DEVICE_STATE_UNAVAILABLE"; 631 case DEVICE_STATE_AVAILABLE: return "DEVICE_STATE_AVAILABLE"; 632 default: return "unknown state (" + state + ")"; 633 } 634 } 635 636 public static final String DEVICE_OUT_EARPIECE_NAME = "earpiece"; 637 public static final String DEVICE_OUT_SPEAKER_NAME = "speaker"; 638 public static final String DEVICE_OUT_WIRED_HEADSET_NAME = "headset"; 639 public static final String DEVICE_OUT_WIRED_HEADPHONE_NAME = "headphone"; 640 public static final String DEVICE_OUT_BLUETOOTH_SCO_NAME = "bt_sco"; 641 public static final String DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME = "bt_sco_hs"; 642 public static final String DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME = "bt_sco_carkit"; 643 public static final String DEVICE_OUT_BLUETOOTH_A2DP_NAME = "bt_a2dp"; 644 public static final String DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME = "bt_a2dp_hp"; 645 public static final String DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME = "bt_a2dp_spk"; 646 public static final String DEVICE_OUT_AUX_DIGITAL_NAME = "aux_digital"; 647 public static final String DEVICE_OUT_HDMI_NAME = "hdmi"; 648 public static final String DEVICE_OUT_ANLG_DOCK_HEADSET_NAME = "analog_dock"; 649 public static final String DEVICE_OUT_DGTL_DOCK_HEADSET_NAME = "digital_dock"; 650 public static final String DEVICE_OUT_USB_ACCESSORY_NAME = "usb_accessory"; 651 public static final String DEVICE_OUT_USB_DEVICE_NAME = "usb_device"; 652 public static final String DEVICE_OUT_REMOTE_SUBMIX_NAME = "remote_submix"; 653 public static final String DEVICE_OUT_TELEPHONY_TX_NAME = "telephony_tx"; 654 public static final String DEVICE_OUT_LINE_NAME = "line"; 655 public static final String DEVICE_OUT_HDMI_ARC_NAME = "hmdi_arc"; 656 public static final String DEVICE_OUT_SPDIF_NAME = "spdif"; 657 public static final String DEVICE_OUT_FM_NAME = "fm_transmitter"; 658 public static final String DEVICE_OUT_AUX_LINE_NAME = "aux_line"; 659 public static final String DEVICE_OUT_SPEAKER_SAFE_NAME = "speaker_safe"; 660 public static final String DEVICE_OUT_IP_NAME = "ip"; 661 public static final String DEVICE_OUT_BUS_NAME = "bus"; 662 public static final String DEVICE_OUT_PROXY_NAME = "proxy"; 663 public static final String DEVICE_OUT_USB_HEADSET_NAME = "usb_headset"; 664 public static final String DEVICE_OUT_HEARING_AID_NAME = "hearing_aid_out"; 665 666 public static final String DEVICE_IN_COMMUNICATION_NAME = "communication"; 667 public static final String DEVICE_IN_AMBIENT_NAME = "ambient"; 668 public static final String DEVICE_IN_BUILTIN_MIC_NAME = "mic"; 669 public static final String DEVICE_IN_BLUETOOTH_SCO_HEADSET_NAME = "bt_sco_hs"; 670 public static final String DEVICE_IN_WIRED_HEADSET_NAME = "headset"; 671 public static final String DEVICE_IN_AUX_DIGITAL_NAME = "aux_digital"; 672 public static final String DEVICE_IN_TELEPHONY_RX_NAME = "telephony_rx"; 673 public static final String DEVICE_IN_BACK_MIC_NAME = "back_mic"; 674 public static final String DEVICE_IN_REMOTE_SUBMIX_NAME = "remote_submix"; 675 public static final String DEVICE_IN_ANLG_DOCK_HEADSET_NAME = "analog_dock"; 676 public static final String DEVICE_IN_DGTL_DOCK_HEADSET_NAME = "digital_dock"; 677 public static final String DEVICE_IN_USB_ACCESSORY_NAME = "usb_accessory"; 678 public static final String DEVICE_IN_USB_DEVICE_NAME = "usb_device"; 679 public static final String DEVICE_IN_FM_TUNER_NAME = "fm_tuner"; 680 public static final String DEVICE_IN_TV_TUNER_NAME = "tv_tuner"; 681 public static final String DEVICE_IN_LINE_NAME = "line"; 682 public static final String DEVICE_IN_SPDIF_NAME = "spdif"; 683 public static final String DEVICE_IN_BLUETOOTH_A2DP_NAME = "bt_a2dp"; 684 public static final String DEVICE_IN_LOOPBACK_NAME = "loopback"; 685 public static final String DEVICE_IN_IP_NAME = "ip"; 686 public static final String DEVICE_IN_BUS_NAME = "bus"; 687 public static final String DEVICE_IN_PROXY_NAME = "proxy"; 688 public static final String DEVICE_IN_USB_HEADSET_NAME = "usb_headset"; 689 public static final String DEVICE_IN_BLUETOOTH_BLE_NAME = "bt_ble"; 690 public static final String DEVICE_IN_ECHO_REFERENCE_NAME = "echo_reference"; 691 public static final String DEVICE_IN_HDMI_ARC_NAME = "hdmi_arc"; 692 693 @UnsupportedAppUsage getOutputDeviceName(int device)694 public static String getOutputDeviceName(int device) 695 { 696 switch(device) { 697 case DEVICE_OUT_EARPIECE: 698 return DEVICE_OUT_EARPIECE_NAME; 699 case DEVICE_OUT_SPEAKER: 700 return DEVICE_OUT_SPEAKER_NAME; 701 case DEVICE_OUT_WIRED_HEADSET: 702 return DEVICE_OUT_WIRED_HEADSET_NAME; 703 case DEVICE_OUT_WIRED_HEADPHONE: 704 return DEVICE_OUT_WIRED_HEADPHONE_NAME; 705 case DEVICE_OUT_BLUETOOTH_SCO: 706 return DEVICE_OUT_BLUETOOTH_SCO_NAME; 707 case DEVICE_OUT_BLUETOOTH_SCO_HEADSET: 708 return DEVICE_OUT_BLUETOOTH_SCO_HEADSET_NAME; 709 case DEVICE_OUT_BLUETOOTH_SCO_CARKIT: 710 return DEVICE_OUT_BLUETOOTH_SCO_CARKIT_NAME; 711 case DEVICE_OUT_BLUETOOTH_A2DP: 712 return DEVICE_OUT_BLUETOOTH_A2DP_NAME; 713 case DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: 714 return DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES_NAME; 715 case DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER: 716 return DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER_NAME; 717 case DEVICE_OUT_HDMI: 718 return DEVICE_OUT_HDMI_NAME; 719 case DEVICE_OUT_ANLG_DOCK_HEADSET: 720 return DEVICE_OUT_ANLG_DOCK_HEADSET_NAME; 721 case DEVICE_OUT_DGTL_DOCK_HEADSET: 722 return DEVICE_OUT_DGTL_DOCK_HEADSET_NAME; 723 case DEVICE_OUT_USB_ACCESSORY: 724 return DEVICE_OUT_USB_ACCESSORY_NAME; 725 case DEVICE_OUT_USB_DEVICE: 726 return DEVICE_OUT_USB_DEVICE_NAME; 727 case DEVICE_OUT_REMOTE_SUBMIX: 728 return DEVICE_OUT_REMOTE_SUBMIX_NAME; 729 case DEVICE_OUT_TELEPHONY_TX: 730 return DEVICE_OUT_TELEPHONY_TX_NAME; 731 case DEVICE_OUT_LINE: 732 return DEVICE_OUT_LINE_NAME; 733 case DEVICE_OUT_HDMI_ARC: 734 return DEVICE_OUT_HDMI_ARC_NAME; 735 case DEVICE_OUT_SPDIF: 736 return DEVICE_OUT_SPDIF_NAME; 737 case DEVICE_OUT_FM: 738 return DEVICE_OUT_FM_NAME; 739 case DEVICE_OUT_AUX_LINE: 740 return DEVICE_OUT_AUX_LINE_NAME; 741 case DEVICE_OUT_SPEAKER_SAFE: 742 return DEVICE_OUT_SPEAKER_SAFE_NAME; 743 case DEVICE_OUT_IP: 744 return DEVICE_OUT_IP_NAME; 745 case DEVICE_OUT_BUS: 746 return DEVICE_OUT_BUS_NAME; 747 case DEVICE_OUT_PROXY: 748 return DEVICE_OUT_PROXY_NAME; 749 case DEVICE_OUT_USB_HEADSET: 750 return DEVICE_OUT_USB_HEADSET_NAME; 751 case DEVICE_OUT_HEARING_AID: 752 return DEVICE_OUT_HEARING_AID_NAME; 753 case DEVICE_OUT_DEFAULT: 754 default: 755 return Integer.toString(device); 756 } 757 } 758 getInputDeviceName(int device)759 public static String getInputDeviceName(int device) 760 { 761 switch(device) { 762 case DEVICE_IN_COMMUNICATION: 763 return DEVICE_IN_COMMUNICATION_NAME; 764 case DEVICE_IN_AMBIENT: 765 return DEVICE_IN_AMBIENT_NAME; 766 case DEVICE_IN_BUILTIN_MIC: 767 return DEVICE_IN_BUILTIN_MIC_NAME; 768 case DEVICE_IN_BLUETOOTH_SCO_HEADSET: 769 return DEVICE_IN_BLUETOOTH_SCO_HEADSET_NAME; 770 case DEVICE_IN_WIRED_HEADSET: 771 return DEVICE_IN_WIRED_HEADSET_NAME; 772 case DEVICE_IN_AUX_DIGITAL: 773 return DEVICE_IN_AUX_DIGITAL_NAME; 774 case DEVICE_IN_TELEPHONY_RX: 775 return DEVICE_IN_TELEPHONY_RX_NAME; 776 case DEVICE_IN_BACK_MIC: 777 return DEVICE_IN_BACK_MIC_NAME; 778 case DEVICE_IN_REMOTE_SUBMIX: 779 return DEVICE_IN_REMOTE_SUBMIX_NAME; 780 case DEVICE_IN_ANLG_DOCK_HEADSET: 781 return DEVICE_IN_ANLG_DOCK_HEADSET_NAME; 782 case DEVICE_IN_DGTL_DOCK_HEADSET: 783 return DEVICE_IN_DGTL_DOCK_HEADSET_NAME; 784 case DEVICE_IN_USB_ACCESSORY: 785 return DEVICE_IN_USB_ACCESSORY_NAME; 786 case DEVICE_IN_USB_DEVICE: 787 return DEVICE_IN_USB_DEVICE_NAME; 788 case DEVICE_IN_FM_TUNER: 789 return DEVICE_IN_FM_TUNER_NAME; 790 case DEVICE_IN_TV_TUNER: 791 return DEVICE_IN_TV_TUNER_NAME; 792 case DEVICE_IN_LINE: 793 return DEVICE_IN_LINE_NAME; 794 case DEVICE_IN_SPDIF: 795 return DEVICE_IN_SPDIF_NAME; 796 case DEVICE_IN_BLUETOOTH_A2DP: 797 return DEVICE_IN_BLUETOOTH_A2DP_NAME; 798 case DEVICE_IN_LOOPBACK: 799 return DEVICE_IN_LOOPBACK_NAME; 800 case DEVICE_IN_IP: 801 return DEVICE_IN_IP_NAME; 802 case DEVICE_IN_BUS: 803 return DEVICE_IN_BUS_NAME; 804 case DEVICE_IN_PROXY: 805 return DEVICE_IN_PROXY_NAME; 806 case DEVICE_IN_USB_HEADSET: 807 return DEVICE_IN_USB_HEADSET_NAME; 808 case DEVICE_IN_BLUETOOTH_BLE: 809 return DEVICE_IN_BLUETOOTH_BLE_NAME; 810 case DEVICE_IN_ECHO_REFERENCE: 811 return DEVICE_IN_ECHO_REFERENCE_NAME; 812 case DEVICE_IN_HDMI_ARC: 813 return DEVICE_IN_HDMI_ARC_NAME; 814 case DEVICE_IN_DEFAULT: 815 default: 816 return Integer.toString(device); 817 } 818 } 819 820 // phone state, match audio_mode??? 821 public static final int PHONE_STATE_OFFCALL = 0; 822 public static final int PHONE_STATE_RINGING = 1; 823 public static final int PHONE_STATE_INCALL = 2; 824 825 // device categories config for setForceUse, must match audio_policy_forced_cfg_t 826 @UnsupportedAppUsage 827 public static final int FORCE_NONE = 0; 828 public static final int FORCE_SPEAKER = 1; 829 public static final int FORCE_HEADPHONES = 2; 830 public static final int FORCE_BT_SCO = 3; 831 public static final int FORCE_BT_A2DP = 4; 832 public static final int FORCE_WIRED_ACCESSORY = 5; 833 @UnsupportedAppUsage 834 public static final int FORCE_BT_CAR_DOCK = 6; 835 @UnsupportedAppUsage 836 public static final int FORCE_BT_DESK_DOCK = 7; 837 @UnsupportedAppUsage 838 public static final int FORCE_ANALOG_DOCK = 8; 839 @UnsupportedAppUsage 840 public static final int FORCE_DIGITAL_DOCK = 9; 841 public static final int FORCE_NO_BT_A2DP = 10; 842 public static final int FORCE_SYSTEM_ENFORCED = 11; 843 public static final int FORCE_HDMI_SYSTEM_AUDIO_ENFORCED = 12; 844 public static final int FORCE_ENCODED_SURROUND_NEVER = 13; 845 public static final int FORCE_ENCODED_SURROUND_ALWAYS = 14; 846 public static final int FORCE_ENCODED_SURROUND_MANUAL = 15; 847 public static final int NUM_FORCE_CONFIG = 16; 848 public static final int FORCE_DEFAULT = FORCE_NONE; 849 forceUseConfigToString(int config)850 public static String forceUseConfigToString(int config) { 851 switch (config) { 852 case FORCE_NONE: return "FORCE_NONE"; 853 case FORCE_SPEAKER: return "FORCE_SPEAKER"; 854 case FORCE_HEADPHONES: return "FORCE_HEADPHONES"; 855 case FORCE_BT_SCO: return "FORCE_BT_SCO"; 856 case FORCE_BT_A2DP: return "FORCE_BT_A2DP"; 857 case FORCE_WIRED_ACCESSORY: return "FORCE_WIRED_ACCESSORY"; 858 case FORCE_BT_CAR_DOCK: return "FORCE_BT_CAR_DOCK"; 859 case FORCE_BT_DESK_DOCK: return "FORCE_BT_DESK_DOCK"; 860 case FORCE_ANALOG_DOCK: return "FORCE_ANALOG_DOCK"; 861 case FORCE_DIGITAL_DOCK: return "FORCE_DIGITAL_DOCK"; 862 case FORCE_NO_BT_A2DP: return "FORCE_NO_BT_A2DP"; 863 case FORCE_SYSTEM_ENFORCED: return "FORCE_SYSTEM_ENFORCED"; 864 case FORCE_HDMI_SYSTEM_AUDIO_ENFORCED: return "FORCE_HDMI_SYSTEM_AUDIO_ENFORCED"; 865 case FORCE_ENCODED_SURROUND_NEVER: return "FORCE_ENCODED_SURROUND_NEVER"; 866 case FORCE_ENCODED_SURROUND_ALWAYS: return "FORCE_ENCODED_SURROUND_ALWAYS"; 867 case FORCE_ENCODED_SURROUND_MANUAL: return "FORCE_ENCODED_SURROUND_MANUAL"; 868 default: return "unknown config (" + config + ")" ; 869 } 870 } 871 872 // usage for setForceUse, must match audio_policy_force_use_t 873 public static final int FOR_COMMUNICATION = 0; 874 public static final int FOR_MEDIA = 1; 875 public static final int FOR_RECORD = 2; 876 public static final int FOR_DOCK = 3; 877 public static final int FOR_SYSTEM = 4; 878 public static final int FOR_HDMI_SYSTEM_AUDIO = 5; 879 public static final int FOR_ENCODED_SURROUND = 6; 880 public static final int FOR_VIBRATE_RINGING = 7; 881 private static final int NUM_FORCE_USE = 8; 882 forceUseUsageToString(int usage)883 public static String forceUseUsageToString(int usage) { 884 switch (usage) { 885 case FOR_COMMUNICATION: return "FOR_COMMUNICATION"; 886 case FOR_MEDIA: return "FOR_MEDIA"; 887 case FOR_RECORD: return "FOR_RECORD"; 888 case FOR_DOCK: return "FOR_DOCK"; 889 case FOR_SYSTEM: return "FOR_SYSTEM"; 890 case FOR_HDMI_SYSTEM_AUDIO: return "FOR_HDMI_SYSTEM_AUDIO"; 891 case FOR_ENCODED_SURROUND: return "FOR_ENCODED_SURROUND"; 892 case FOR_VIBRATE_RINGING: return "FOR_VIBRATE_RINGING"; 893 default: return "unknown usage (" + usage + ")" ; 894 } 895 } 896 897 /** Wrapper for native methods called from AudioService */ setStreamVolumeIndexAS(int stream, int index, int device)898 public static int setStreamVolumeIndexAS(int stream, int index, int device) { 899 if (DEBUG_VOLUME) { 900 Log.i(TAG, "setStreamVolumeIndex: " + STREAM_NAMES[stream] 901 + " dev=" + Integer.toHexString(device) + " idx=" + index); 902 } 903 return setStreamVolumeIndex(stream, index, device); 904 } 905 906 // usage for AudioRecord.startRecordingSync(), must match AudioSystem::sync_event_t 907 public static final int SYNC_EVENT_NONE = 0; 908 public static final int SYNC_EVENT_PRESENTATION_COMPLETE = 1; 909 910 /** 911 * @return command completion status, one of {@link #AUDIO_STATUS_OK}, 912 * {@link #AUDIO_STATUS_ERROR} or {@link #AUDIO_STATUS_SERVER_DIED} 913 */ 914 @UnsupportedAppUsage setDeviceConnectionState(int device, int state, String device_address, String device_name, int codecFormat)915 public static native int setDeviceConnectionState(int device, int state, 916 String device_address, String device_name, 917 int codecFormat); 918 @UnsupportedAppUsage getDeviceConnectionState(int device, String device_address)919 public static native int getDeviceConnectionState(int device, String device_address); handleDeviceConfigChange(int device, String device_address, String device_name, int codecFormat)920 public static native int handleDeviceConfigChange(int device, 921 String device_address, 922 String device_name, 923 int codecFormat); 924 @UnsupportedAppUsage setPhoneState(int state)925 public static native int setPhoneState(int state); 926 @UnsupportedAppUsage setForceUse(int usage, int config)927 public static native int setForceUse(int usage, int config); 928 @UnsupportedAppUsage getForceUse(int usage)929 public static native int getForceUse(int usage); 930 @UnsupportedAppUsage initStreamVolume(int stream, int indexMin, int indexMax)931 public static native int initStreamVolume(int stream, int indexMin, int indexMax); 932 @UnsupportedAppUsage setStreamVolumeIndex(int stream, int index, int device)933 private static native int setStreamVolumeIndex(int stream, int index, int device); getStreamVolumeIndex(int stream, int device)934 public static native int getStreamVolumeIndex(int stream, int device); 935 /** 936 * @hide 937 * set a volume for the given {@link AudioAttributes} and for all other stream that belong to 938 * the same volume group. 939 * @param attributes the {@link AudioAttributes} to be considered 940 * @param index to be applied 941 * @param device the volume device to be considered 942 * @return command completion status. 943 */ setVolumeIndexForAttributes(@onNull AudioAttributes attributes, int index, int device)944 public static native int setVolumeIndexForAttributes(@NonNull AudioAttributes attributes, 945 int index, int device); 946 /** 947 * @hide 948 * get the volume index for the given {@link AudioAttributes}. 949 * @param attributes the {@link AudioAttributes} to be considered 950 * @param device the volume device to be considered 951 * @return volume index for the given {@link AudioAttributes} and volume device. 952 */ getVolumeIndexForAttributes(@onNull AudioAttributes attributes, int device)953 public static native int getVolumeIndexForAttributes(@NonNull AudioAttributes attributes, 954 int device); 955 /** 956 * @hide 957 * get the minimum volume index for the given {@link AudioAttributes}. 958 * @param attributes the {@link AudioAttributes} to be considered 959 * @return minimum volume index for the given {@link AudioAttributes}. 960 */ getMinVolumeIndexForAttributes(@onNull AudioAttributes attributes)961 public static native int getMinVolumeIndexForAttributes(@NonNull AudioAttributes attributes); 962 /** 963 * @hide 964 * get the maximum volume index for the given {@link AudioAttributes}. 965 * @param attributes the {@link AudioAttributes} to be considered 966 * @return maximum volume index for the given {@link AudioAttributes}. 967 */ getMaxVolumeIndexForAttributes(@onNull AudioAttributes attributes)968 public static native int getMaxVolumeIndexForAttributes(@NonNull AudioAttributes attributes); 969 setMasterVolume(float value)970 public static native int setMasterVolume(float value); getMasterVolume()971 public static native float getMasterVolume(); 972 @UnsupportedAppUsage setMasterMute(boolean mute)973 public static native int setMasterMute(boolean mute); 974 @UnsupportedAppUsage getMasterMute()975 public static native boolean getMasterMute(); 976 @UnsupportedAppUsage getDevicesForStream(int stream)977 public static native int getDevicesForStream(int stream); 978 979 /** @hide returns true if master mono is enabled. */ getMasterMono()980 public static native boolean getMasterMono(); 981 /** @hide enables or disables the master mono mode. */ setMasterMono(boolean mono)982 public static native int setMasterMono(boolean mono); 983 /** @hide enables or disables the RTT mode. */ setRttEnabled(boolean enabled)984 public static native int setRttEnabled(boolean enabled); 985 986 /** @hide returns master balance value in range -1.f -> 1.f, where 0.f is dead center. */ 987 @TestApi getMasterBalance()988 public static native float getMasterBalance(); 989 /** @hide changes the audio balance of the device. */ 990 @TestApi setMasterBalance(float balance)991 public static native int setMasterBalance(float balance); 992 993 // helpers for android.media.AudioManager.getProperty(), see description there for meaning 994 @UnsupportedAppUsage(trackingBug = 134049522) getPrimaryOutputSamplingRate()995 public static native int getPrimaryOutputSamplingRate(); 996 @UnsupportedAppUsage(trackingBug = 134049522) getPrimaryOutputFrameCount()997 public static native int getPrimaryOutputFrameCount(); 998 @UnsupportedAppUsage getOutputLatency(int stream)999 public static native int getOutputLatency(int stream); 1000 setLowRamDevice(boolean isLowRamDevice, long totalMemory)1001 public static native int setLowRamDevice(boolean isLowRamDevice, long totalMemory); 1002 @UnsupportedAppUsage checkAudioFlinger()1003 public static native int checkAudioFlinger(); 1004 listAudioPorts(ArrayList<AudioPort> ports, int[] generation)1005 public static native int listAudioPorts(ArrayList<AudioPort> ports, int[] generation); createAudioPatch(AudioPatch[] patch, AudioPortConfig[] sources, AudioPortConfig[] sinks)1006 public static native int createAudioPatch(AudioPatch[] patch, 1007 AudioPortConfig[] sources, AudioPortConfig[] sinks); releaseAudioPatch(AudioPatch patch)1008 public static native int releaseAudioPatch(AudioPatch patch); listAudioPatches(ArrayList<AudioPatch> patches, int[] generation)1009 public static native int listAudioPatches(ArrayList<AudioPatch> patches, int[] generation); setAudioPortConfig(AudioPortConfig config)1010 public static native int setAudioPortConfig(AudioPortConfig config); 1011 startAudioSource(AudioPortConfig config, AudioAttributes audioAttributes)1012 public static native int startAudioSource(AudioPortConfig config, 1013 AudioAttributes audioAttributes); stopAudioSource(int handle)1014 public static native int stopAudioSource(int handle); 1015 1016 // declare this instance as having a dynamic policy callback handler native_register_dynamic_policy_callback()1017 private static native final void native_register_dynamic_policy_callback(); 1018 // declare this instance as having a recording configuration update callback handler native_register_recording_callback()1019 private static native final void native_register_recording_callback(); 1020 1021 // must be kept in sync with value in include/system/audio.h 1022 public static final int AUDIO_HW_SYNC_INVALID = 0; 1023 getAudioHwSyncForSession(int sessionId)1024 public static native int getAudioHwSyncForSession(int sessionId); 1025 registerPolicyMixes(ArrayList<AudioMix> mixes, boolean register)1026 public static native int registerPolicyMixes(ArrayList<AudioMix> mixes, boolean register); 1027 1028 /** see AudioPolicy.setUidDeviceAffinities() */ setUidDeviceAffinities(int uid, @NonNull int[] types, @NonNull String[] addresses)1029 public static native int setUidDeviceAffinities(int uid, @NonNull int[] types, 1030 @NonNull String[] addresses); 1031 1032 /** see AudioPolicy.removeUidDeviceAffinities() */ removeUidDeviceAffinities(int uid)1033 public static native int removeUidDeviceAffinities(int uid); 1034 systemReady()1035 public static native int systemReady(); 1036 getStreamVolumeDB(int stream, int index, int device)1037 public static native float getStreamVolumeDB(int stream, int index, int device); 1038 1039 /** 1040 * @see AudioManager#setAllowedCapturePolicy() 1041 */ setAllowedCapturePolicy(int uid, int flags)1042 public static native int setAllowedCapturePolicy(int uid, int flags); 1043 isOffloadSupported(@onNull AudioFormat format, @NonNull AudioAttributes attr)1044 static boolean isOffloadSupported(@NonNull AudioFormat format, @NonNull AudioAttributes attr) { 1045 return native_is_offload_supported(format.getEncoding(), format.getSampleRate(), 1046 format.getChannelMask(), format.getChannelIndexMask(), 1047 attr.getVolumeControlStream()); 1048 } 1049 native_is_offload_supported(int encoding, int sampleRate, int channelMask, int channelIndexMask, int streamType)1050 private static native boolean native_is_offload_supported(int encoding, int sampleRate, 1051 int channelMask, int channelIndexMask, int streamType); 1052 getMicrophones(ArrayList<MicrophoneInfo> microphonesInfo)1053 public static native int getMicrophones(ArrayList<MicrophoneInfo> microphonesInfo); 1054 getSurroundFormats(Map<Integer, Boolean> surroundFormats, boolean reported)1055 public static native int getSurroundFormats(Map<Integer, Boolean> surroundFormats, 1056 boolean reported); 1057 1058 /** 1059 * Returns a list of audio formats (codec) supported on the A2DP offload path. 1060 */ getHwOffloadEncodingFormatsSupportedForA2DP( ArrayList<Integer> formatList)1061 public static native int getHwOffloadEncodingFormatsSupportedForA2DP( 1062 ArrayList<Integer> formatList); 1063 setSurroundFormatEnabled(int audioFormat, boolean enabled)1064 public static native int setSurroundFormatEnabled(int audioFormat, boolean enabled); 1065 1066 /** 1067 * Communicate UID of active assistant to audio policy service. 1068 */ setAssistantUid(int uid)1069 public static native int setAssistantUid(int uid); 1070 /** 1071 * Communicate UIDs of active accessibility services to audio policy service. 1072 */ setA11yServicesUids(int[] uids)1073 public static native int setA11yServicesUids(int[] uids); 1074 1075 /** 1076 * @see AudioManager#isHapticPlaybackSupported() 1077 */ isHapticPlaybackSupported()1078 public static native boolean isHapticPlaybackSupported(); 1079 1080 // Items shared with audio service 1081 1082 /** 1083 * The delay before playing a sound. This small period exists so the user 1084 * can press another key (non-volume keys, too) to have it NOT be audible. 1085 * <p> 1086 * PhoneWindow will implement this part. 1087 */ 1088 public static final int PLAY_SOUND_DELAY = 300; 1089 1090 /** 1091 * Constant to identify a focus stack entry that is used to hold the focus while the phone 1092 * is ringing or during a call. Used by com.android.internal.telephony.CallManager when 1093 * entering and exiting calls. 1094 */ 1095 public final static String IN_VOICE_COMM_FOCUS_ID = "AudioFocus_For_Phone_Ring_And_Calls"; 1096 1097 /** 1098 * @see AudioManager#setVibrateSetting(int, int) 1099 */ getValueForVibrateSetting(int existingValue, int vibrateType, int vibrateSetting)1100 public static int getValueForVibrateSetting(int existingValue, int vibrateType, 1101 int vibrateSetting) { 1102 1103 // First clear the existing setting. Each vibrate type has two bits in 1104 // the value. Note '3' is '11' in binary. 1105 existingValue &= ~(3 << (vibrateType * 2)); 1106 1107 // Set into the old value 1108 existingValue |= (vibrateSetting & 3) << (vibrateType * 2); 1109 1110 return existingValue; 1111 } 1112 getDefaultStreamVolume(int streamType)1113 public static int getDefaultStreamVolume(int streamType) { 1114 return DEFAULT_STREAM_VOLUME[streamType]; 1115 } 1116 1117 public static int[] DEFAULT_STREAM_VOLUME = new int[] { 1118 4, // STREAM_VOICE_CALL 1119 7, // STREAM_SYSTEM 1120 5, // STREAM_RING 1121 5, // STREAM_MUSIC 1122 6, // STREAM_ALARM 1123 5, // STREAM_NOTIFICATION 1124 7, // STREAM_BLUETOOTH_SCO 1125 7, // STREAM_SYSTEM_ENFORCED 1126 5, // STREAM_DTMF 1127 5, // STREAM_TTS 1128 5, // STREAM_ACCESSIBILITY 1129 }; 1130 streamToString(int stream)1131 public static String streamToString(int stream) { 1132 if (stream >= 0 && stream < STREAM_NAMES.length) return STREAM_NAMES[stream]; 1133 if (stream == AudioManager.USE_DEFAULT_STREAM_TYPE) return "USE_DEFAULT_STREAM_TYPE"; 1134 return "UNKNOWN_STREAM_" + stream; 1135 } 1136 1137 /** The platform has no specific capabilities */ 1138 public static final int PLATFORM_DEFAULT = 0; 1139 /** The platform is voice call capable (a phone) */ 1140 public static final int PLATFORM_VOICE = 1; 1141 /** The platform is a television or a set-top box */ 1142 public static final int PLATFORM_TELEVISION = 2; 1143 1144 /** 1145 * Return the platform type that this is running on. One of: 1146 * <ul> 1147 * <li>{@link #PLATFORM_VOICE}</li> 1148 * <li>{@link #PLATFORM_TELEVISION}</li> 1149 * <li>{@link #PLATFORM_DEFAULT}</li> 1150 * </ul> 1151 */ getPlatformType(Context context)1152 public static int getPlatformType(Context context) { 1153 if (context.getResources().getBoolean(com.android.internal.R.bool.config_voice_capable)) { 1154 return PLATFORM_VOICE; 1155 } else if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LEANBACK)) { 1156 return PLATFORM_TELEVISION; 1157 } else { 1158 return PLATFORM_DEFAULT; 1159 } 1160 } 1161 1162 /** 1163 * @hide 1164 * @return whether the system uses a single volume stream. 1165 */ isSingleVolume(Context context)1166 public static boolean isSingleVolume(Context context) { 1167 boolean forceSingleVolume = context.getResources().getBoolean( 1168 com.android.internal.R.bool.config_single_volume); 1169 return getPlatformType(context) == PLATFORM_TELEVISION || forceSingleVolume; 1170 } 1171 1172 public static final int DEFAULT_MUTE_STREAMS_AFFECTED = 1173 (1 << STREAM_MUSIC) | 1174 (1 << STREAM_RING) | 1175 (1 << STREAM_NOTIFICATION) | 1176 (1 << STREAM_SYSTEM) | 1177 (1 << STREAM_VOICE_CALL) | 1178 (1 << STREAM_BLUETOOTH_SCO); 1179 1180 /** 1181 * Event posted by AudioTrack and AudioRecord JNI (JNIDeviceCallback) when routing changes. 1182 * Keep in sync with core/jni/android_media_DeviceCallback.h. 1183 */ 1184 final static int NATIVE_EVENT_ROUTING_CHANGE = 1000; 1185 } 1186 1187