AudioManager.java revision 6d5176638c2189595cede38fb92c3e7e8700e221
1/* 2 * Copyright (C) 2007 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 17package android.media; 18 19import android.annotation.SdkConstant; 20import android.annotation.SdkConstant.SdkConstantType; 21import android.app.PendingIntent; 22import android.content.ComponentName; 23import android.content.Context; 24import android.content.Intent; 25import android.os.Binder; 26import android.os.Handler; 27import android.os.IBinder; 28import android.os.Looper; 29import android.os.Message; 30import android.os.RemoteException; 31import android.os.SystemClock; 32import android.os.ServiceManager; 33import android.provider.Settings; 34import android.util.Log; 35import android.view.KeyEvent; 36import android.view.VolumePanel; 37 38import java.util.HashMap; 39 40/** 41 * AudioManager provides access to volume and ringer mode control. 42 * <p> 43 * Use <code>Context.getSystemService(Context.AUDIO_SERVICE)</code> to get 44 * an instance of this class. 45 */ 46public class AudioManager { 47 48 private final Context mContext; 49 private long mVolumeKeyUpTime; 50 private final boolean mUseMasterVolume; 51 private static String TAG = "AudioManager"; 52 53 /** 54 * Broadcast intent, a hint for applications that audio is about to become 55 * 'noisy' due to a change in audio outputs. For example, this intent may 56 * be sent when a wired headset is unplugged, or when an A2DP audio 57 * sink is disconnected, and the audio system is about to automatically 58 * switch audio route to the speaker. Applications that are controlling 59 * audio streams may consider pausing, reducing volume or some other action 60 * on receipt of this intent so as not to surprise the user with audio 61 * from the speaker. 62 */ 63 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 64 public static final String ACTION_AUDIO_BECOMING_NOISY = "android.media.AUDIO_BECOMING_NOISY"; 65 66 /** 67 * Sticky broadcast intent action indicating that the ringer mode has 68 * changed. Includes the new ringer mode. 69 * 70 * @see #EXTRA_RINGER_MODE 71 */ 72 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 73 public static final String RINGER_MODE_CHANGED_ACTION = "android.media.RINGER_MODE_CHANGED"; 74 75 /** 76 * The new ringer mode. 77 * 78 * @see #RINGER_MODE_CHANGED_ACTION 79 * @see #RINGER_MODE_NORMAL 80 * @see #RINGER_MODE_SILENT 81 * @see #RINGER_MODE_VIBRATE 82 */ 83 public static final String EXTRA_RINGER_MODE = "android.media.EXTRA_RINGER_MODE"; 84 85 /** 86 * Broadcast intent action indicating that the vibrate setting has 87 * changed. Includes the vibrate type and its new setting. 88 * 89 * @see #EXTRA_VIBRATE_TYPE 90 * @see #EXTRA_VIBRATE_SETTING 91 */ 92 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 93 public static final String VIBRATE_SETTING_CHANGED_ACTION = 94 "android.media.VIBRATE_SETTING_CHANGED"; 95 96 /** 97 * @hide Broadcast intent when the volume for a particular stream type changes. 98 * Includes the stream, the new volume and previous volumes 99 * 100 * @see #EXTRA_VOLUME_STREAM_TYPE 101 * @see #EXTRA_VOLUME_STREAM_VALUE 102 * @see #EXTRA_PREV_VOLUME_STREAM_VALUE 103 */ 104 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 105 public static final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION"; 106 107 /** 108 * @hide Broadcast intent when the master volume changes. 109 * Includes the new volume 110 * 111 * @see #EXTRA_MASTER_VOLUME_VALUE 112 * @see #EXTRA_PREV_MASTER_VOLUME_VALUE 113 */ 114 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 115 public static final String MASTER_VOLUME_CHANGED_ACTION = 116 "android.media.MASTER_VOLUME_CHANGED_ACTION"; 117 118 /** 119 * @hide Broadcast intent when the master mute state changes. 120 * Includes the the new volume 121 * 122 * @see #EXTRA_MASTER_VOLUME_MUTED 123 */ 124 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 125 public static final String MASTER_MUTE_CHANGED_ACTION = 126 "android.media.MASTER_MUTE_CHANGED_ACTION"; 127 128 /** 129 * The new vibrate setting for a particular type. 130 * 131 * @see #VIBRATE_SETTING_CHANGED_ACTION 132 * @see #EXTRA_VIBRATE_TYPE 133 * @see #VIBRATE_SETTING_ON 134 * @see #VIBRATE_SETTING_OFF 135 * @see #VIBRATE_SETTING_ONLY_SILENT 136 */ 137 public static final String EXTRA_VIBRATE_SETTING = "android.media.EXTRA_VIBRATE_SETTING"; 138 139 /** 140 * The vibrate type whose setting has changed. 141 * 142 * @see #VIBRATE_SETTING_CHANGED_ACTION 143 * @see #VIBRATE_TYPE_NOTIFICATION 144 * @see #VIBRATE_TYPE_RINGER 145 */ 146 public static final String EXTRA_VIBRATE_TYPE = "android.media.EXTRA_VIBRATE_TYPE"; 147 148 /** 149 * @hide The stream type for the volume changed intent. 150 */ 151 public static final String EXTRA_VOLUME_STREAM_TYPE = "android.media.EXTRA_VOLUME_STREAM_TYPE"; 152 153 /** 154 * @hide The volume associated with the stream for the volume changed intent. 155 */ 156 public static final String EXTRA_VOLUME_STREAM_VALUE = 157 "android.media.EXTRA_VOLUME_STREAM_VALUE"; 158 159 /** 160 * @hide The previous volume associated with the stream for the volume changed intent. 161 */ 162 public static final String EXTRA_PREV_VOLUME_STREAM_VALUE = 163 "android.media.EXTRA_PREV_VOLUME_STREAM_VALUE"; 164 165 /** 166 * @hide The new master volume value for the master volume changed intent. 167 * Value is integer between 0 and 100 inclusive. 168 */ 169 public static final String EXTRA_MASTER_VOLUME_VALUE = 170 "android.media.EXTRA_MASTER_VOLUME_VALUE"; 171 172 /** 173 * @hide The previous master volume value for the master volume changed intent. 174 * Value is integer between 0 and 100 inclusive. 175 */ 176 public static final String EXTRA_PREV_MASTER_VOLUME_VALUE = 177 "android.media.EXTRA_PREV_MASTER_VOLUME_VALUE"; 178 179 /** 180 * @hide The new master volume mute state for the master mute changed intent. 181 * Value is boolean 182 */ 183 public static final String EXTRA_MASTER_VOLUME_MUTED = 184 "android.media.EXTRA_MASTER_VOLUME_MUTED"; 185 186 /** The audio stream for phone calls */ 187 public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL; 188 /** The audio stream for system sounds */ 189 public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM; 190 /** The audio stream for the phone ring */ 191 public static final int STREAM_RING = AudioSystem.STREAM_RING; 192 /** The audio stream for music playback */ 193 public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC; 194 /** The audio stream for alarms */ 195 public static final int STREAM_ALARM = AudioSystem.STREAM_ALARM; 196 /** The audio stream for notifications */ 197 public static final int STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION; 198 /** @hide The audio stream for phone calls when connected to bluetooth */ 199 public static final int STREAM_BLUETOOTH_SCO = AudioSystem.STREAM_BLUETOOTH_SCO; 200 /** @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */ 201 public static final int STREAM_SYSTEM_ENFORCED = AudioSystem.STREAM_SYSTEM_ENFORCED; 202 /** The audio stream for DTMF Tones */ 203 public static final int STREAM_DTMF = AudioSystem.STREAM_DTMF; 204 /** @hide The audio stream for text to speech (TTS) */ 205 public static final int STREAM_TTS = AudioSystem.STREAM_TTS; 206 /** Number of audio streams */ 207 /** 208 * @deprecated Use AudioSystem.getNumStreamTypes() instead 209 */ 210 @Deprecated public static final int NUM_STREAMS = AudioSystem.NUM_STREAMS; 211 212 213 /** @hide Default volume index values for audio streams */ 214 public static final int[] DEFAULT_STREAM_VOLUME = new int[] { 215 4, // STREAM_VOICE_CALL 216 7, // STREAM_SYSTEM 217 5, // STREAM_RING 218 11, // STREAM_MUSIC 219 6, // STREAM_ALARM 220 5, // STREAM_NOTIFICATION 221 7, // STREAM_BLUETOOTH_SCO 222 7, // STREAM_SYSTEM_ENFORCED 223 11, // STREAM_DTMF 224 11 // STREAM_TTS 225 }; 226 227 /** 228 * Increase the ringer volume. 229 * 230 * @see #adjustVolume(int, int) 231 * @see #adjustStreamVolume(int, int, int) 232 */ 233 public static final int ADJUST_RAISE = 1; 234 235 /** 236 * Decrease the ringer volume. 237 * 238 * @see #adjustVolume(int, int) 239 * @see #adjustStreamVolume(int, int, int) 240 */ 241 public static final int ADJUST_LOWER = -1; 242 243 /** 244 * Maintain the previous ringer volume. This may be useful when needing to 245 * show the volume toast without actually modifying the volume. 246 * 247 * @see #adjustVolume(int, int) 248 * @see #adjustStreamVolume(int, int, int) 249 */ 250 public static final int ADJUST_SAME = 0; 251 252 // Flags should be powers of 2! 253 254 /** 255 * Show a toast containing the current volume. 256 * 257 * @see #adjustStreamVolume(int, int, int) 258 * @see #adjustVolume(int, int) 259 * @see #setStreamVolume(int, int, int) 260 * @see #setRingerMode(int) 261 */ 262 public static final int FLAG_SHOW_UI = 1 << 0; 263 264 /** 265 * Whether to include ringer modes as possible options when changing volume. 266 * For example, if true and volume level is 0 and the volume is adjusted 267 * with {@link #ADJUST_LOWER}, then the ringer mode may switch the silent or 268 * vibrate mode. 269 * <p> 270 * By default this is on for the ring stream. If this flag is included, 271 * this behavior will be present regardless of the stream type being 272 * affected by the ringer mode. 273 * 274 * @see #adjustVolume(int, int) 275 * @see #adjustStreamVolume(int, int, int) 276 */ 277 public static final int FLAG_ALLOW_RINGER_MODES = 1 << 1; 278 279 /** 280 * Whether to play a sound when changing the volume. 281 * <p> 282 * If this is given to {@link #adjustVolume(int, int)} or 283 * {@link #adjustSuggestedStreamVolume(int, int, int)}, it may be ignored 284 * in some cases (for example, the decided stream type is not 285 * {@link AudioManager#STREAM_RING}, or the volume is being adjusted 286 * downward). 287 * 288 * @see #adjustStreamVolume(int, int, int) 289 * @see #adjustVolume(int, int) 290 * @see #setStreamVolume(int, int, int) 291 */ 292 public static final int FLAG_PLAY_SOUND = 1 << 2; 293 294 /** 295 * Removes any sounds/vibrate that may be in the queue, or are playing (related to 296 * changing volume). 297 */ 298 public static final int FLAG_REMOVE_SOUND_AND_VIBRATE = 1 << 3; 299 300 /** 301 * Whether to vibrate if going into the vibrate ringer mode. 302 */ 303 public static final int FLAG_VIBRATE = 1 << 4; 304 305 /** 306 * Ringer mode that will be silent and will not vibrate. (This overrides the 307 * vibrate setting.) 308 * 309 * @see #setRingerMode(int) 310 * @see #getRingerMode() 311 */ 312 public static final int RINGER_MODE_SILENT = 0; 313 314 /** 315 * Ringer mode that will be silent and will vibrate. (This will cause the 316 * phone ringer to always vibrate, but the notification vibrate to only 317 * vibrate if set.) 318 * 319 * @see #setRingerMode(int) 320 * @see #getRingerMode() 321 */ 322 public static final int RINGER_MODE_VIBRATE = 1; 323 324 /** 325 * Ringer mode that may be audible and may vibrate. It will be audible if 326 * the volume before changing out of this mode was audible. It will vibrate 327 * if the vibrate setting is on. 328 * 329 * @see #setRingerMode(int) 330 * @see #getRingerMode() 331 */ 332 public static final int RINGER_MODE_NORMAL = 2; 333 334 // maximum valid ringer mode value. Values must start from 0 and be contiguous. 335 private static final int RINGER_MODE_MAX = RINGER_MODE_NORMAL; 336 337 /** 338 * Vibrate type that corresponds to the ringer. 339 * 340 * @see #setVibrateSetting(int, int) 341 * @see #getVibrateSetting(int) 342 * @see #shouldVibrate(int) 343 */ 344 public static final int VIBRATE_TYPE_RINGER = 0; 345 346 /** 347 * Vibrate type that corresponds to notifications. 348 * 349 * @see #setVibrateSetting(int, int) 350 * @see #getVibrateSetting(int) 351 * @see #shouldVibrate(int) 352 */ 353 public static final int VIBRATE_TYPE_NOTIFICATION = 1; 354 355 /** 356 * Vibrate setting that suggests to never vibrate. 357 * 358 * @see #setVibrateSetting(int, int) 359 * @see #getVibrateSetting(int) 360 */ 361 public static final int VIBRATE_SETTING_OFF = 0; 362 363 /** 364 * Vibrate setting that suggests to vibrate when possible. 365 * 366 * @see #setVibrateSetting(int, int) 367 * @see #getVibrateSetting(int) 368 */ 369 public static final int VIBRATE_SETTING_ON = 1; 370 371 /** 372 * Vibrate setting that suggests to only vibrate when in the vibrate ringer 373 * mode. 374 * 375 * @see #setVibrateSetting(int, int) 376 * @see #getVibrateSetting(int) 377 */ 378 public static final int VIBRATE_SETTING_ONLY_SILENT = 2; 379 380 /** 381 * Suggests using the default stream type. This may not be used in all 382 * places a stream type is needed. 383 */ 384 public static final int USE_DEFAULT_STREAM_TYPE = Integer.MIN_VALUE; 385 386 private static IAudioService sService; 387 388 /** 389 * @hide 390 */ 391 public AudioManager(Context context) { 392 mContext = context; 393 mUseMasterVolume = mContext.getResources().getBoolean( 394 com.android.internal.R.bool.config_useMasterVolume); 395 } 396 397 private static IAudioService getService() 398 { 399 if (sService != null) { 400 return sService; 401 } 402 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE); 403 sService = IAudioService.Stub.asInterface(b); 404 return sService; 405 } 406 407 /** 408 * @hide 409 */ 410 public void preDispatchKeyEvent(KeyEvent event, int stream) { 411 /* 412 * If the user hits another key within the play sound delay, then 413 * cancel the sound 414 */ 415 int keyCode = event.getKeyCode(); 416 if (keyCode != KeyEvent.KEYCODE_VOLUME_DOWN && keyCode != KeyEvent.KEYCODE_VOLUME_UP 417 && keyCode != KeyEvent.KEYCODE_VOLUME_MUTE 418 && mVolumeKeyUpTime + VolumePanel.PLAY_SOUND_DELAY 419 > SystemClock.uptimeMillis()) { 420 /* 421 * The user has hit another key during the delay (e.g., 300ms) 422 * since the last volume key up, so cancel any sounds. 423 */ 424 if (mUseMasterVolume) { 425 adjustMasterVolume(ADJUST_SAME, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); 426 } else { 427 adjustSuggestedStreamVolume(ADJUST_SAME, 428 stream, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); 429 } 430 } 431 } 432 433 /** 434 * @hide 435 */ 436 public void handleKeyDown(KeyEvent event, int stream) { 437 int keyCode = event.getKeyCode(); 438 switch (keyCode) { 439 case KeyEvent.KEYCODE_VOLUME_UP: 440 case KeyEvent.KEYCODE_VOLUME_DOWN: 441 /* 442 * Adjust the volume in on key down since it is more 443 * responsive to the user. 444 */ 445 int flags = FLAG_SHOW_UI | FLAG_VIBRATE; 446 if (mUseMasterVolume) { 447 adjustMasterVolume( 448 keyCode == KeyEvent.KEYCODE_VOLUME_UP 449 ? ADJUST_RAISE 450 : ADJUST_LOWER, 451 flags); 452 } else { 453 adjustSuggestedStreamVolume( 454 keyCode == KeyEvent.KEYCODE_VOLUME_UP 455 ? ADJUST_RAISE 456 : ADJUST_LOWER, 457 stream, 458 flags); 459 } 460 break; 461 case KeyEvent.KEYCODE_VOLUME_MUTE: 462 if (event.getRepeatCount() == 0) { 463 if (mUseMasterVolume) { 464 setMasterMute(!isMasterMute()); 465 } else { 466 // TODO: Actually handle MUTE. 467 } 468 } 469 break; 470 } 471 } 472 473 /** 474 * @hide 475 */ 476 public void handleKeyUp(KeyEvent event, int stream) { 477 int keyCode = event.getKeyCode(); 478 switch (keyCode) { 479 case KeyEvent.KEYCODE_VOLUME_UP: 480 case KeyEvent.KEYCODE_VOLUME_DOWN: 481 /* 482 * Play a sound. This is done on key up since we don't want the 483 * sound to play when a user holds down volume down to mute. 484 */ 485 if (mUseMasterVolume) { 486 if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { 487 adjustMasterVolume(ADJUST_SAME, FLAG_PLAY_SOUND); 488 } 489 } else { 490 int flags = FLAG_PLAY_SOUND; 491 adjustSuggestedStreamVolume( 492 ADJUST_SAME, 493 stream, 494 flags); 495 } 496 497 mVolumeKeyUpTime = SystemClock.uptimeMillis(); 498 break; 499 } 500 } 501 502 /** 503 * Adjusts the volume of a particular stream by one step in a direction. 504 * <p> 505 * This method should only be used by applications that replace the platform-wide 506 * management of audio settings or the main telephony application. 507 * 508 * @param streamType The stream type to adjust. One of {@link #STREAM_VOICE_CALL}, 509 * {@link #STREAM_SYSTEM}, {@link #STREAM_RING}, {@link #STREAM_MUSIC} or 510 * {@link #STREAM_ALARM} 511 * @param direction The direction to adjust the volume. One of 512 * {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or 513 * {@link #ADJUST_SAME}. 514 * @param flags One or more flags. 515 * @see #adjustVolume(int, int) 516 * @see #setStreamVolume(int, int, int) 517 */ 518 public void adjustStreamVolume(int streamType, int direction, int flags) { 519 IAudioService service = getService(); 520 try { 521 if (mUseMasterVolume) { 522 service.adjustMasterVolume(direction, flags); 523 } else { 524 service.adjustStreamVolume(streamType, direction, flags); 525 } 526 } catch (RemoteException e) { 527 Log.e(TAG, "Dead object in adjustStreamVolume", e); 528 } 529 } 530 531 /** 532 * Adjusts the volume of the most relevant stream. For example, if a call is 533 * active, it will have the highest priority regardless of if the in-call 534 * screen is showing. Another example, if music is playing in the background 535 * and a call is not active, the music stream will be adjusted. 536 * <p> 537 * This method should only be used by applications that replace the platform-wide 538 * management of audio settings or the main telephony application. 539 * 540 * @param direction The direction to adjust the volume. One of 541 * {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or 542 * {@link #ADJUST_SAME}. 543 * @param flags One or more flags. 544 * @see #adjustSuggestedStreamVolume(int, int, int) 545 * @see #adjustStreamVolume(int, int, int) 546 * @see #setStreamVolume(int, int, int) 547 */ 548 public void adjustVolume(int direction, int flags) { 549 IAudioService service = getService(); 550 try { 551 if (mUseMasterVolume) { 552 service.adjustMasterVolume(direction, flags); 553 } else { 554 service.adjustVolume(direction, flags); 555 } 556 } catch (RemoteException e) { 557 Log.e(TAG, "Dead object in adjustVolume", e); 558 } 559 } 560 561 /** 562 * Adjusts the volume of the most relevant stream, or the given fallback 563 * stream. 564 * <p> 565 * This method should only be used by applications that replace the platform-wide 566 * management of audio settings or the main telephony application. 567 * 568 * @param direction The direction to adjust the volume. One of 569 * {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or 570 * {@link #ADJUST_SAME}. 571 * @param suggestedStreamType The stream type that will be used if there 572 * isn't a relevant stream. {@link #USE_DEFAULT_STREAM_TYPE} is valid here. 573 * @param flags One or more flags. 574 * @see #adjustVolume(int, int) 575 * @see #adjustStreamVolume(int, int, int) 576 * @see #setStreamVolume(int, int, int) 577 */ 578 public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags) { 579 IAudioService service = getService(); 580 try { 581 if (mUseMasterVolume) { 582 service.adjustMasterVolume(direction, flags); 583 } else { 584 service.adjustSuggestedStreamVolume(direction, suggestedStreamType, flags); 585 } 586 } catch (RemoteException e) { 587 Log.e(TAG, "Dead object in adjustSuggestedStreamVolume", e); 588 } 589 } 590 591 /** 592 * Adjusts the master volume for the device's audio amplifier. 593 * <p> 594 * 595 * @param steps The number of volume steps to adjust. A positive 596 * value will raise the volume. 597 * @param flags One or more flags. 598 * @hide 599 */ 600 public void adjustMasterVolume(int steps, int flags) { 601 IAudioService service = getService(); 602 try { 603 service.adjustMasterVolume(steps, flags); 604 } catch (RemoteException e) { 605 Log.e(TAG, "Dead object in adjustMasterVolume", e); 606 } 607 } 608 609 /** 610 * Returns the current ringtone mode. 611 * 612 * @return The current ringtone mode, one of {@link #RINGER_MODE_NORMAL}, 613 * {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}. 614 * @see #setRingerMode(int) 615 */ 616 public int getRingerMode() { 617 IAudioService service = getService(); 618 try { 619 return service.getRingerMode(); 620 } catch (RemoteException e) { 621 Log.e(TAG, "Dead object in getRingerMode", e); 622 return RINGER_MODE_NORMAL; 623 } 624 } 625 626 /** 627 * Checks valid ringer mode values. 628 * 629 * @return true if the ringer mode indicated is valid, false otherwise. 630 * 631 * @see #setRingerMode(int) 632 * @hide 633 */ 634 public static boolean isValidRingerMode(int ringerMode) { 635 if (ringerMode < 0 || ringerMode > RINGER_MODE_MAX) { 636 return false; 637 } 638 return true; 639 } 640 641 /** 642 * Returns the maximum volume index for a particular stream. 643 * 644 * @param streamType The stream type whose maximum volume index is returned. 645 * @return The maximum valid volume index for the stream. 646 * @see #getStreamVolume(int) 647 */ 648 public int getStreamMaxVolume(int streamType) { 649 IAudioService service = getService(); 650 try { 651 if (mUseMasterVolume) { 652 return service.getMasterMaxVolume(); 653 } else { 654 return service.getStreamMaxVolume(streamType); 655 } 656 } catch (RemoteException e) { 657 Log.e(TAG, "Dead object in getStreamMaxVolume", e); 658 return 0; 659 } 660 } 661 662 /** 663 * Returns the current volume index for a particular stream. 664 * 665 * @param streamType The stream type whose volume index is returned. 666 * @return The current volume index for the stream. 667 * @see #getStreamMaxVolume(int) 668 * @see #setStreamVolume(int, int, int) 669 */ 670 public int getStreamVolume(int streamType) { 671 IAudioService service = getService(); 672 try { 673 if (mUseMasterVolume) { 674 return service.getMasterVolume(); 675 } else { 676 return service.getStreamVolume(streamType); 677 } 678 } catch (RemoteException e) { 679 Log.e(TAG, "Dead object in getStreamVolume", e); 680 return 0; 681 } 682 } 683 684 /** 685 * Get last audible volume before stream was muted. 686 * 687 * @hide 688 */ 689 public int getLastAudibleStreamVolume(int streamType) { 690 IAudioService service = getService(); 691 try { 692 if (mUseMasterVolume) { 693 return service.getLastAudibleMasterVolume(); 694 } else { 695 return service.getLastAudibleStreamVolume(streamType); 696 } 697 } catch (RemoteException e) { 698 Log.e(TAG, "Dead object in getLastAudibleStreamVolume", e); 699 return 0; 700 } 701 } 702 703 /** 704 * Get the stream type whose volume is driving the UI sounds volume. 705 * UI sounds are screen lock/unlock, camera shutter, key clicks... 706 * @hide 707 */ 708 public int getMasterStreamType() { 709 IAudioService service = getService(); 710 try { 711 return service.getMasterStreamType(); 712 } catch (RemoteException e) { 713 Log.e(TAG, "Dead object in getMasterStreamType", e); 714 return STREAM_RING; 715 } 716 } 717 718 /** 719 * Sets the ringer mode. 720 * <p> 721 * Silent mode will mute the volume and will not vibrate. Vibrate mode will 722 * mute the volume and vibrate. Normal mode will be audible and may vibrate 723 * according to user settings. 724 * 725 * @param ringerMode The ringer mode, one of {@link #RINGER_MODE_NORMAL}, 726 * {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}. 727 * @see #getRingerMode() 728 */ 729 public void setRingerMode(int ringerMode) { 730 if (!isValidRingerMode(ringerMode)) { 731 return; 732 } 733 IAudioService service = getService(); 734 try { 735 service.setRingerMode(ringerMode); 736 } catch (RemoteException e) { 737 Log.e(TAG, "Dead object in setRingerMode", e); 738 } 739 } 740 741 /** 742 * Sets the volume index for a particular stream. 743 * 744 * @param streamType The stream whose volume index should be set. 745 * @param index The volume index to set. See 746 * {@link #getStreamMaxVolume(int)} for the largest valid value. 747 * @param flags One or more flags. 748 * @see #getStreamMaxVolume(int) 749 * @see #getStreamVolume(int) 750 */ 751 public void setStreamVolume(int streamType, int index, int flags) { 752 IAudioService service = getService(); 753 try { 754 if (mUseMasterVolume) { 755 service.setMasterVolume(index, flags); 756 } else { 757 service.setStreamVolume(streamType, index, flags); 758 } 759 } catch (RemoteException e) { 760 Log.e(TAG, "Dead object in setStreamVolume", e); 761 } 762 } 763 764 /** 765 * Returns the maximum volume index for master volume. 766 * 767 * @hide 768 */ 769 public int getMasterMaxVolume() { 770 IAudioService service = getService(); 771 try { 772 return service.getMasterMaxVolume(); 773 } catch (RemoteException e) { 774 Log.e(TAG, "Dead object in getMasterMaxVolume", e); 775 return 0; 776 } 777 } 778 779 /** 780 * Returns the current volume index for master volume. 781 * 782 * @return The current volume index for master volume. 783 * @hide 784 */ 785 public int getMasterVolume() { 786 IAudioService service = getService(); 787 try { 788 return service.getMasterVolume(); 789 } catch (RemoteException e) { 790 Log.e(TAG, "Dead object in getMasterVolume", e); 791 return 0; 792 } 793 } 794 795 /** 796 * Get last audible volume before master volume was muted. 797 * 798 * @hide 799 */ 800 public int getLastAudibleMasterVolume() { 801 IAudioService service = getService(); 802 try { 803 return service.getLastAudibleMasterVolume(); 804 } catch (RemoteException e) { 805 Log.e(TAG, "Dead object in getLastAudibleMasterVolume", e); 806 return 0; 807 } 808 } 809 810 /** 811 * Sets the volume index for master volume. 812 * 813 * @param index The volume index to set. See 814 * {@link #getMasterMaxVolume(int)} for the largest valid value. 815 * @param flags One or more flags. 816 * @see #getMasterMaxVolume(int) 817 * @see #getMasterVolume(int) 818 * @hide 819 */ 820 public void setMasterVolume(int index, int flags) { 821 IAudioService service = getService(); 822 try { 823 service.setMasterVolume(index, flags); 824 } catch (RemoteException e) { 825 Log.e(TAG, "Dead object in setMasterVolume", e); 826 } 827 } 828 829 /** 830 * Solo or unsolo a particular stream. All other streams are muted. 831 * <p> 832 * The solo command is protected against client process death: if a process 833 * with an active solo request on a stream dies, all streams that were muted 834 * because of this request will be unmuted automatically. 835 * <p> 836 * The solo requests for a given stream are cumulative: the AudioManager 837 * can receive several solo requests from one or more clients and the stream 838 * will be unsoloed only when the same number of unsolo requests are received. 839 * <p> 840 * For a better user experience, applications MUST unsolo a soloed stream 841 * in onPause() and solo is again in onResume() if appropriate. 842 * 843 * @param streamType The stream to be soloed/unsoloed. 844 * @param state The required solo state: true for solo ON, false for solo OFF 845 */ 846 public void setStreamSolo(int streamType, boolean state) { 847 IAudioService service = getService(); 848 try { 849 service.setStreamSolo(streamType, state, mICallBack); 850 } catch (RemoteException e) { 851 Log.e(TAG, "Dead object in setStreamSolo", e); 852 } 853 } 854 855 /** 856 * Mute or unmute an audio stream. 857 * <p> 858 * The mute command is protected against client process death: if a process 859 * with an active mute request on a stream dies, this stream will be unmuted 860 * automatically. 861 * <p> 862 * The mute requests for a given stream are cumulative: the AudioManager 863 * can receive several mute requests from one or more clients and the stream 864 * will be unmuted only when the same number of unmute requests are received. 865 * <p> 866 * For a better user experience, applications MUST unmute a muted stream 867 * in onPause() and mute is again in onResume() if appropriate. 868 * <p> 869 * This method should only be used by applications that replace the platform-wide 870 * management of audio settings or the main telephony application. 871 * 872 * @param streamType The stream to be muted/unmuted. 873 * @param state The required mute state: true for mute ON, false for mute OFF 874 */ 875 public void setStreamMute(int streamType, boolean state) { 876 IAudioService service = getService(); 877 try { 878 service.setStreamMute(streamType, state, mICallBack); 879 } catch (RemoteException e) { 880 Log.e(TAG, "Dead object in setStreamMute", e); 881 } 882 } 883 884 /** 885 * get stream mute state. 886 * 887 * @hide 888 */ 889 public boolean isStreamMute(int streamType) { 890 IAudioService service = getService(); 891 try { 892 return service.isStreamMute(streamType); 893 } catch (RemoteException e) { 894 Log.e(TAG, "Dead object in isStreamMute", e); 895 return false; 896 } 897 } 898 899 /** 900 * set master mute state. 901 * 902 * @hide 903 */ 904 public void setMasterMute(boolean state) { 905 setMasterMute(state, FLAG_SHOW_UI); 906 } 907 908 /** 909 * set master mute state with optional flags. 910 * 911 * @hide 912 */ 913 public void setMasterMute(boolean state, int flags) { 914 IAudioService service = getService(); 915 try { 916 service.setMasterMute(state, flags, mICallBack); 917 } catch (RemoteException e) { 918 Log.e(TAG, "Dead object in setMasterMute", e); 919 } 920 } 921 922 /** 923 * get master mute state. 924 * 925 * @hide 926 */ 927 public boolean isMasterMute() { 928 IAudioService service = getService(); 929 try { 930 return service.isMasterMute(); 931 } catch (RemoteException e) { 932 Log.e(TAG, "Dead object in isMasterMute", e); 933 return false; 934 } 935 } 936 937 /** 938 * forces the stream controlled by hard volume keys 939 * specifying streamType == -1 releases control to the 940 * logic. 941 * 942 * @hide 943 */ 944 public void forceVolumeControlStream(int streamType) { 945 IAudioService service = getService(); 946 try { 947 service.forceVolumeControlStream(streamType, mICallBack); 948 } catch (RemoteException e) { 949 Log.e(TAG, "Dead object in forceVolumeControlStream", e); 950 } 951 } 952 953 /** 954 * Returns whether a particular type should vibrate according to user 955 * settings and the current ringer mode. 956 * <p> 957 * This shouldn't be needed by most clients that use notifications to 958 * vibrate. The notification manager will not vibrate if the policy doesn't 959 * allow it, so the client should always set a vibrate pattern and let the 960 * notification manager control whether or not to actually vibrate. 961 * 962 * @param vibrateType The type of vibrate. One of 963 * {@link #VIBRATE_TYPE_NOTIFICATION} or 964 * {@link #VIBRATE_TYPE_RINGER}. 965 * @return Whether the type should vibrate at the instant this method is 966 * called. 967 * @see #setVibrateSetting(int, int) 968 * @see #getVibrateSetting(int) 969 */ 970 public boolean shouldVibrate(int vibrateType) { 971 IAudioService service = getService(); 972 try { 973 return service.shouldVibrate(vibrateType); 974 } catch (RemoteException e) { 975 Log.e(TAG, "Dead object in shouldVibrate", e); 976 return false; 977 } 978 } 979 980 /** 981 * Returns whether the user's vibrate setting for a vibrate type. 982 * <p> 983 * This shouldn't be needed by most clients that want to vibrate, instead 984 * see {@link #shouldVibrate(int)}. 985 * 986 * @param vibrateType The type of vibrate. One of 987 * {@link #VIBRATE_TYPE_NOTIFICATION} or 988 * {@link #VIBRATE_TYPE_RINGER}. 989 * @return The vibrate setting, one of {@link #VIBRATE_SETTING_ON}, 990 * {@link #VIBRATE_SETTING_OFF}, or 991 * {@link #VIBRATE_SETTING_ONLY_SILENT}. 992 * @see #setVibrateSetting(int, int) 993 * @see #shouldVibrate(int) 994 */ 995 public int getVibrateSetting(int vibrateType) { 996 IAudioService service = getService(); 997 try { 998 return service.getVibrateSetting(vibrateType); 999 } catch (RemoteException e) { 1000 Log.e(TAG, "Dead object in getVibrateSetting", e); 1001 return VIBRATE_SETTING_OFF; 1002 } 1003 } 1004 1005 /** 1006 * Sets the setting for when the vibrate type should vibrate. 1007 * <p> 1008 * This method should only be used by applications that replace the platform-wide 1009 * management of audio settings or the main telephony application. 1010 * 1011 * @param vibrateType The type of vibrate. One of 1012 * {@link #VIBRATE_TYPE_NOTIFICATION} or 1013 * {@link #VIBRATE_TYPE_RINGER}. 1014 * @param vibrateSetting The vibrate setting, one of 1015 * {@link #VIBRATE_SETTING_ON}, 1016 * {@link #VIBRATE_SETTING_OFF}, or 1017 * {@link #VIBRATE_SETTING_ONLY_SILENT}. 1018 * @see #getVibrateSetting(int) 1019 * @see #shouldVibrate(int) 1020 */ 1021 public void setVibrateSetting(int vibrateType, int vibrateSetting) { 1022 IAudioService service = getService(); 1023 try { 1024 service.setVibrateSetting(vibrateType, vibrateSetting); 1025 } catch (RemoteException e) { 1026 Log.e(TAG, "Dead object in setVibrateSetting", e); 1027 } 1028 } 1029 1030 /** 1031 * Sets the speakerphone on or off. 1032 * <p> 1033 * This method should only be used by applications that replace the platform-wide 1034 * management of audio settings or the main telephony application. 1035 * 1036 * @param on set <var>true</var> to turn on speakerphone; 1037 * <var>false</var> to turn it off 1038 */ 1039 public void setSpeakerphoneOn(boolean on){ 1040 IAudioService service = getService(); 1041 try { 1042 service.setSpeakerphoneOn(on); 1043 } catch (RemoteException e) { 1044 Log.e(TAG, "Dead object in setSpeakerphoneOn", e); 1045 } 1046 } 1047 1048 /** 1049 * Checks whether the speakerphone is on or off. 1050 * 1051 * @return true if speakerphone is on, false if it's off 1052 */ 1053 public boolean isSpeakerphoneOn() { 1054 IAudioService service = getService(); 1055 try { 1056 return service.isSpeakerphoneOn(); 1057 } catch (RemoteException e) { 1058 Log.e(TAG, "Dead object in isSpeakerphoneOn", e); 1059 return false; 1060 } 1061 } 1062 1063 //==================================================================== 1064 // Bluetooth SCO control 1065 /** 1066 * Sticky broadcast intent action indicating that the bluetoooth SCO audio 1067 * connection state has changed. The intent contains on extra {@link #EXTRA_SCO_AUDIO_STATE} 1068 * indicating the new state which is either {@link #SCO_AUDIO_STATE_DISCONNECTED} 1069 * or {@link #SCO_AUDIO_STATE_CONNECTED} 1070 * 1071 * @see #startBluetoothSco() 1072 * @deprecated Use {@link #ACTION_SCO_AUDIO_STATE_UPDATED} instead 1073 */ 1074 @Deprecated 1075 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 1076 public static final String ACTION_SCO_AUDIO_STATE_CHANGED = 1077 "android.media.SCO_AUDIO_STATE_CHANGED"; 1078 1079 /** 1080 * Sticky broadcast intent action indicating that the bluetoooth SCO audio 1081 * connection state has been updated. 1082 * <p>This intent has two extras: 1083 * <ul> 1084 * <li> {@link #EXTRA_SCO_AUDIO_STATE} - The new SCO audio state. </li> 1085 * <li> {@link #EXTRA_SCO_AUDIO_PREVIOUS_STATE}- The previous SCO audio state. </li> 1086 * </ul> 1087 * <p> EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE can be any of: 1088 * <ul> 1089 * <li> {@link #SCO_AUDIO_STATE_DISCONNECTED}, </li> 1090 * <li> {@link #SCO_AUDIO_STATE_CONNECTING} or </li> 1091 * <li> {@link #SCO_AUDIO_STATE_CONNECTED}, </li> 1092 * </ul> 1093 * @see #startBluetoothSco() 1094 */ 1095 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 1096 public static final String ACTION_SCO_AUDIO_STATE_UPDATED = 1097 "android.media.ACTION_SCO_AUDIO_STATE_UPDATED"; 1098 1099 /** 1100 * Extra for intent {@link #ACTION_SCO_AUDIO_STATE_CHANGED} or 1101 * {@link #ACTION_SCO_AUDIO_STATE_UPDATED} containing the new bluetooth SCO connection state. 1102 */ 1103 public static final String EXTRA_SCO_AUDIO_STATE = 1104 "android.media.extra.SCO_AUDIO_STATE"; 1105 1106 /** 1107 * Extra for intent {@link #ACTION_SCO_AUDIO_STATE_UPDATED} containing the previous 1108 * bluetooth SCO connection state. 1109 */ 1110 public static final String EXTRA_SCO_AUDIO_PREVIOUS_STATE = 1111 "android.media.extra.SCO_AUDIO_PREVIOUS_STATE"; 1112 1113 /** 1114 * Value for extra EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE 1115 * indicating that the SCO audio channel is not established 1116 */ 1117 public static final int SCO_AUDIO_STATE_DISCONNECTED = 0; 1118 /** 1119 * Value for extra {@link #EXTRA_SCO_AUDIO_STATE} or {@link #EXTRA_SCO_AUDIO_PREVIOUS_STATE} 1120 * indicating that the SCO audio channel is established 1121 */ 1122 public static final int SCO_AUDIO_STATE_CONNECTED = 1; 1123 /** 1124 * Value for extra EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE 1125 * indicating that the SCO audio channel is being established 1126 */ 1127 public static final int SCO_AUDIO_STATE_CONNECTING = 2; 1128 /** 1129 * Value for extra EXTRA_SCO_AUDIO_STATE indicating that 1130 * there was an error trying to obtain the state 1131 */ 1132 public static final int SCO_AUDIO_STATE_ERROR = -1; 1133 1134 1135 /** 1136 * Indicates if current platform supports use of SCO for off call use cases. 1137 * Application wanted to use bluetooth SCO audio when the phone is not in call 1138 * must first call thsi method to make sure that the platform supports this 1139 * feature. 1140 * @return true if bluetooth SCO can be used for audio when not in call 1141 * false otherwise 1142 * @see #startBluetoothSco() 1143 */ 1144 public boolean isBluetoothScoAvailableOffCall() { 1145 return mContext.getResources().getBoolean( 1146 com.android.internal.R.bool.config_bluetooth_sco_off_call); 1147 } 1148 1149 /** 1150 * Start bluetooth SCO audio connection. 1151 * <p>Requires Permission: 1152 * {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}. 1153 * <p>This method can be used by applications wanting to send and received audio 1154 * to/from a bluetooth SCO headset while the phone is not in call. 1155 * <p>As the SCO connection establishment can take several seconds, 1156 * applications should not rely on the connection to be available when the method 1157 * returns but instead register to receive the intent {@link #ACTION_SCO_AUDIO_STATE_UPDATED} 1158 * and wait for the state to be {@link #SCO_AUDIO_STATE_CONNECTED}. 1159 * <p>As the ACTION_SCO_AUDIO_STATE_UPDATED intent is sticky, the application can check the SCO 1160 * audio state before calling startBluetoothSco() by reading the intent returned by the receiver 1161 * registration. If the state is already CONNECTED, no state change will be received via the 1162 * intent after calling startBluetoothSco(). It is however useful to call startBluetoothSco() 1163 * so that the connection stays active in case the current initiator stops the connection. 1164 * <p>Unless the connection is already active as described above, the state will always 1165 * transition from DISCONNECTED to CONNECTING and then either to CONNECTED if the connection 1166 * succeeds or back to DISCONNECTED if the connection fails (e.g no headset is connected). 1167 * <p>When finished with the SCO connection or if the establishment fails, the application must 1168 * call {@link #stopBluetoothSco()} to clear the request and turn down the bluetooth connection. 1169 * <p>Even if a SCO connection is established, the following restrictions apply on audio 1170 * output streams so that they can be routed to SCO headset: 1171 * <ul> 1172 * <li> the stream type must be {@link #STREAM_VOICE_CALL} </li> 1173 * <li> the format must be mono </li> 1174 * <li> the sampling must be 16kHz or 8kHz </li> 1175 * </ul> 1176 * <p>The following restrictions apply on input streams: 1177 * <ul> 1178 * <li> the format must be mono </li> 1179 * <li> the sampling must be 8kHz </li> 1180 * </ul> 1181 * <p>Note that the phone application always has the priority on the usage of the SCO 1182 * connection for telephony. If this method is called while the phone is in call 1183 * it will be ignored. Similarly, if a call is received or sent while an application 1184 * is using the SCO connection, the connection will be lost for the application and NOT 1185 * returned automatically when the call ends. 1186 * @see #stopBluetoothSco() 1187 * @see #ACTION_SCO_AUDIO_STATE_UPDATED 1188 */ 1189 public void startBluetoothSco(){ 1190 IAudioService service = getService(); 1191 try { 1192 service.startBluetoothSco(mICallBack); 1193 } catch (RemoteException e) { 1194 Log.e(TAG, "Dead object in startBluetoothSco", e); 1195 } 1196 } 1197 1198 /** 1199 * Stop bluetooth SCO audio connection. 1200 * <p>Requires Permission: 1201 * {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}. 1202 * <p>This method must be called by applications having requested the use of 1203 * bluetooth SCO audio with {@link #startBluetoothSco()} 1204 * when finished with the SCO connection or if connection fails. 1205 * @see #startBluetoothSco() 1206 */ 1207 public void stopBluetoothSco(){ 1208 IAudioService service = getService(); 1209 try { 1210 service.stopBluetoothSco(mICallBack); 1211 } catch (RemoteException e) { 1212 Log.e(TAG, "Dead object in stopBluetoothSco", e); 1213 } 1214 } 1215 1216 /** 1217 * Request use of Bluetooth SCO headset for communications. 1218 * <p> 1219 * This method should only be used by applications that replace the platform-wide 1220 * management of audio settings or the main telephony application. 1221 * 1222 * @param on set <var>true</var> to use bluetooth SCO for communications; 1223 * <var>false</var> to not use bluetooth SCO for communications 1224 */ 1225 public void setBluetoothScoOn(boolean on){ 1226 IAudioService service = getService(); 1227 try { 1228 service.setBluetoothScoOn(on); 1229 } catch (RemoteException e) { 1230 Log.e(TAG, "Dead object in setBluetoothScoOn", e); 1231 } 1232 } 1233 1234 /** 1235 * Checks whether communications use Bluetooth SCO. 1236 * 1237 * @return true if SCO is used for communications; 1238 * false if otherwise 1239 */ 1240 public boolean isBluetoothScoOn() { 1241 IAudioService service = getService(); 1242 try { 1243 return service.isBluetoothScoOn(); 1244 } catch (RemoteException e) { 1245 Log.e(TAG, "Dead object in isBluetoothScoOn", e); 1246 return false; 1247 } 1248 } 1249 1250 /** 1251 * @param on set <var>true</var> to route A2DP audio to/from Bluetooth 1252 * headset; <var>false</var> disable A2DP audio 1253 * @deprecated Do not use. 1254 */ 1255 @Deprecated public void setBluetoothA2dpOn(boolean on){ 1256 } 1257 1258 /** 1259 * Checks whether A2DP audio routing to the Bluetooth headset is on or off. 1260 * 1261 * @return true if A2DP audio is being routed to/from Bluetooth headset; 1262 * false if otherwise 1263 */ 1264 public boolean isBluetoothA2dpOn() { 1265 if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_BLUETOOTH_A2DP,"") 1266 == AudioSystem.DEVICE_STATE_UNAVAILABLE) { 1267 return false; 1268 } else { 1269 return true; 1270 } 1271 } 1272 1273 /** 1274 * Sets audio routing to the wired headset on or off. 1275 * 1276 * @param on set <var>true</var> to route audio to/from wired 1277 * headset; <var>false</var> disable wired headset audio 1278 * @deprecated Do not use. 1279 */ 1280 @Deprecated public void setWiredHeadsetOn(boolean on){ 1281 } 1282 1283 /** 1284 * Checks whether a wired headset is connected or not. 1285 * <p>This is not a valid indication that audio playback is 1286 * actually over the wired headset as audio routing depends on other conditions. 1287 * 1288 * @return true if a wired headset is connected. 1289 * false if otherwise 1290 * @deprecated Use only to check is a headset is connected or not. 1291 */ 1292 public boolean isWiredHeadsetOn() { 1293 if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADSET,"") 1294 == AudioSystem.DEVICE_STATE_UNAVAILABLE && 1295 AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADPHONE,"") 1296 == AudioSystem.DEVICE_STATE_UNAVAILABLE) { 1297 return false; 1298 } else { 1299 return true; 1300 } 1301 } 1302 1303 /** 1304 * Sets the microphone mute on or off. 1305 * <p> 1306 * This method should only be used by applications that replace the platform-wide 1307 * management of audio settings or the main telephony application. 1308 * 1309 * @param on set <var>true</var> to mute the microphone; 1310 * <var>false</var> to turn mute off 1311 */ 1312 public void setMicrophoneMute(boolean on){ 1313 AudioSystem.muteMicrophone(on); 1314 } 1315 1316 /** 1317 * Checks whether the microphone mute is on or off. 1318 * 1319 * @return true if microphone is muted, false if it's not 1320 */ 1321 public boolean isMicrophoneMute() { 1322 return AudioSystem.isMicrophoneMuted(); 1323 } 1324 1325 /** 1326 * Sets the audio mode. 1327 * <p> 1328 * The audio mode encompasses audio routing AND the behavior of 1329 * the telephony layer. Therefore this method should only be used by applications that 1330 * replace the platform-wide management of audio settings or the main telephony application. 1331 * In particular, the {@link #MODE_IN_CALL} mode should only be used by the telephony 1332 * application when it places a phone call, as it will cause signals from the radio layer 1333 * to feed the platform mixer. 1334 * 1335 * @param mode the requested audio mode ({@link #MODE_NORMAL}, {@link #MODE_RINGTONE}, 1336 * {@link #MODE_IN_CALL} or {@link #MODE_IN_COMMUNICATION}). 1337 * Informs the HAL about the current audio state so that 1338 * it can route the audio appropriately. 1339 */ 1340 public void setMode(int mode) { 1341 IAudioService service = getService(); 1342 try { 1343 service.setMode(mode, mICallBack); 1344 } catch (RemoteException e) { 1345 Log.e(TAG, "Dead object in setMode", e); 1346 } 1347 } 1348 1349 /** 1350 * Returns the current audio mode. 1351 * 1352 * @return the current audio mode ({@link #MODE_NORMAL}, {@link #MODE_RINGTONE}, 1353 * {@link #MODE_IN_CALL} or {@link #MODE_IN_COMMUNICATION}). 1354 * Returns the current current audio state from the HAL. 1355 */ 1356 public int getMode() { 1357 IAudioService service = getService(); 1358 try { 1359 return service.getMode(); 1360 } catch (RemoteException e) { 1361 Log.e(TAG, "Dead object in getMode", e); 1362 return MODE_INVALID; 1363 } 1364 } 1365 1366 /* modes for setMode/getMode/setRoute/getRoute */ 1367 /** 1368 * Audio harware modes. 1369 */ 1370 /** 1371 * Invalid audio mode. 1372 */ 1373 public static final int MODE_INVALID = AudioSystem.MODE_INVALID; 1374 /** 1375 * Current audio mode. Used to apply audio routing to current mode. 1376 */ 1377 public static final int MODE_CURRENT = AudioSystem.MODE_CURRENT; 1378 /** 1379 * Normal audio mode: not ringing and no call established. 1380 */ 1381 public static final int MODE_NORMAL = AudioSystem.MODE_NORMAL; 1382 /** 1383 * Ringing audio mode. An incoming is being signaled. 1384 */ 1385 public static final int MODE_RINGTONE = AudioSystem.MODE_RINGTONE; 1386 /** 1387 * In call audio mode. A telephony call is established. 1388 */ 1389 public static final int MODE_IN_CALL = AudioSystem.MODE_IN_CALL; 1390 /** 1391 * In communication audio mode. An audio/video chat or VoIP call is established. 1392 */ 1393 public static final int MODE_IN_COMMUNICATION = AudioSystem.MODE_IN_COMMUNICATION; 1394 1395 /* Routing bits for setRouting/getRouting API */ 1396 /** 1397 * Routing audio output to earpiece 1398 * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), 1399 * setBluetoothScoOn() methods instead. 1400 */ 1401 @Deprecated public static final int ROUTE_EARPIECE = AudioSystem.ROUTE_EARPIECE; 1402 /** 1403 * Routing audio output to speaker 1404 * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), 1405 * setBluetoothScoOn() methods instead. 1406 */ 1407 @Deprecated public static final int ROUTE_SPEAKER = AudioSystem.ROUTE_SPEAKER; 1408 /** 1409 * @deprecated use {@link #ROUTE_BLUETOOTH_SCO} 1410 * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), 1411 * setBluetoothScoOn() methods instead. 1412 */ 1413 @Deprecated public static final int ROUTE_BLUETOOTH = AudioSystem.ROUTE_BLUETOOTH_SCO; 1414 /** 1415 * Routing audio output to bluetooth SCO 1416 * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), 1417 * setBluetoothScoOn() methods instead. 1418 */ 1419 @Deprecated public static final int ROUTE_BLUETOOTH_SCO = AudioSystem.ROUTE_BLUETOOTH_SCO; 1420 /** 1421 * Routing audio output to headset 1422 * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), 1423 * setBluetoothScoOn() methods instead. 1424 */ 1425 @Deprecated public static final int ROUTE_HEADSET = AudioSystem.ROUTE_HEADSET; 1426 /** 1427 * Routing audio output to bluetooth A2DP 1428 * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), 1429 * setBluetoothScoOn() methods instead. 1430 */ 1431 @Deprecated public static final int ROUTE_BLUETOOTH_A2DP = AudioSystem.ROUTE_BLUETOOTH_A2DP; 1432 /** 1433 * Used for mask parameter of {@link #setRouting(int,int,int)}. 1434 * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), 1435 * setBluetoothScoOn() methods instead. 1436 */ 1437 @Deprecated public static final int ROUTE_ALL = AudioSystem.ROUTE_ALL; 1438 1439 /** 1440 * Sets the audio routing for a specified mode 1441 * 1442 * @param mode audio mode to change route. E.g., MODE_RINGTONE. 1443 * @param routes bit vector of routes requested, created from one or 1444 * more of ROUTE_xxx types. Set bits indicate that route should be on 1445 * @param mask bit vector of routes to change, created from one or more of 1446 * ROUTE_xxx types. Unset bits indicate the route should be left unchanged 1447 * 1448 * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), 1449 * setBluetoothScoOn() methods instead. 1450 */ 1451 @Deprecated 1452 public void setRouting(int mode, int routes, int mask) { 1453 } 1454 1455 /** 1456 * Returns the current audio routing bit vector for a specified mode. 1457 * 1458 * @param mode audio mode to get route (e.g., MODE_RINGTONE) 1459 * @return an audio route bit vector that can be compared with ROUTE_xxx 1460 * bits 1461 * @deprecated Do not query audio routing directly, use isSpeakerphoneOn(), 1462 * isBluetoothScoOn(), isBluetoothA2dpOn() and isWiredHeadsetOn() methods instead. 1463 */ 1464 @Deprecated 1465 public int getRouting(int mode) { 1466 return -1; 1467 } 1468 1469 /** 1470 * Checks whether any music is active. 1471 * 1472 * @return true if any music tracks are active. 1473 */ 1474 public boolean isMusicActive() { 1475 return AudioSystem.isStreamActive(STREAM_MUSIC, 0); 1476 } 1477 1478 /* 1479 * Sets a generic audio configuration parameter. The use of these parameters 1480 * are platform dependant, see libaudio 1481 * 1482 * ** Temporary interface - DO NOT USE 1483 * 1484 * TODO: Replace with a more generic key:value get/set mechanism 1485 * 1486 * param key name of parameter to set. Must not be null. 1487 * param value value of parameter. Must not be null. 1488 */ 1489 /** 1490 * @hide 1491 * @deprecated Use {@link #setPrameters(String)} instead 1492 */ 1493 @Deprecated public void setParameter(String key, String value) { 1494 setParameters(key+"="+value); 1495 } 1496 1497 /** 1498 * Sets a variable number of parameter values to audio hardware. 1499 * 1500 * @param keyValuePairs list of parameters key value pairs in the form: 1501 * key1=value1;key2=value2;... 1502 * 1503 */ 1504 public void setParameters(String keyValuePairs) { 1505 AudioSystem.setParameters(keyValuePairs); 1506 } 1507 1508 /** 1509 * Sets a varaible number of parameter values to audio hardware. 1510 * 1511 * @param keys list of parameters 1512 * @return list of parameters key value pairs in the form: 1513 * key1=value1;key2=value2;... 1514 */ 1515 public String getParameters(String keys) { 1516 return AudioSystem.getParameters(keys); 1517 } 1518 1519 /* Sound effect identifiers */ 1520 /** 1521 * Keyboard and direction pad click sound 1522 * @see #playSoundEffect(int) 1523 */ 1524 public static final int FX_KEY_CLICK = 0; 1525 /** 1526 * Focus has moved up 1527 * @see #playSoundEffect(int) 1528 */ 1529 public static final int FX_FOCUS_NAVIGATION_UP = 1; 1530 /** 1531 * Focus has moved down 1532 * @see #playSoundEffect(int) 1533 */ 1534 public static final int FX_FOCUS_NAVIGATION_DOWN = 2; 1535 /** 1536 * Focus has moved left 1537 * @see #playSoundEffect(int) 1538 */ 1539 public static final int FX_FOCUS_NAVIGATION_LEFT = 3; 1540 /** 1541 * Focus has moved right 1542 * @see #playSoundEffect(int) 1543 */ 1544 public static final int FX_FOCUS_NAVIGATION_RIGHT = 4; 1545 /** 1546 * IME standard keypress sound 1547 * @see #playSoundEffect(int) 1548 */ 1549 public static final int FX_KEYPRESS_STANDARD = 5; 1550 /** 1551 * IME spacebar keypress sound 1552 * @see #playSoundEffect(int) 1553 */ 1554 public static final int FX_KEYPRESS_SPACEBAR = 6; 1555 /** 1556 * IME delete keypress sound 1557 * @see #playSoundEffect(int) 1558 */ 1559 public static final int FX_KEYPRESS_DELETE = 7; 1560 /** 1561 * IME return_keypress sound 1562 * @see #playSoundEffect(int) 1563 */ 1564 public static final int FX_KEYPRESS_RETURN = 8; 1565 /** 1566 * @hide Number of sound effects 1567 */ 1568 public static final int NUM_SOUND_EFFECTS = 9; 1569 1570 /** 1571 * Plays a sound effect (Key clicks, lid open/close...) 1572 * @param effectType The type of sound effect. One of 1573 * {@link #FX_KEY_CLICK}, 1574 * {@link #FX_FOCUS_NAVIGATION_UP}, 1575 * {@link #FX_FOCUS_NAVIGATION_DOWN}, 1576 * {@link #FX_FOCUS_NAVIGATION_LEFT}, 1577 * {@link #FX_FOCUS_NAVIGATION_RIGHT}, 1578 * {@link #FX_KEYPRESS_STANDARD}, 1579 * {@link #FX_KEYPRESS_SPACEBAR}, 1580 * {@link #FX_KEYPRESS_DELETE}, 1581 * {@link #FX_KEYPRESS_RETURN}, 1582 * NOTE: This version uses the UI settings to determine 1583 * whether sounds are heard or not. 1584 */ 1585 public void playSoundEffect(int effectType) { 1586 if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) { 1587 return; 1588 } 1589 1590 if (!querySoundEffectsEnabled()) { 1591 return; 1592 } 1593 1594 IAudioService service = getService(); 1595 try { 1596 service.playSoundEffect(effectType); 1597 } catch (RemoteException e) { 1598 Log.e(TAG, "Dead object in playSoundEffect"+e); 1599 } 1600 } 1601 1602 /** 1603 * Plays a sound effect (Key clicks, lid open/close...) 1604 * @param effectType The type of sound effect. One of 1605 * {@link #FX_KEY_CLICK}, 1606 * {@link #FX_FOCUS_NAVIGATION_UP}, 1607 * {@link #FX_FOCUS_NAVIGATION_DOWN}, 1608 * {@link #FX_FOCUS_NAVIGATION_LEFT}, 1609 * {@link #FX_FOCUS_NAVIGATION_RIGHT}, 1610 * {@link #FX_KEYPRESS_STANDARD}, 1611 * {@link #FX_KEYPRESS_SPACEBAR}, 1612 * {@link #FX_KEYPRESS_DELETE}, 1613 * {@link #FX_KEYPRESS_RETURN}, 1614 * @param volume Sound effect volume. 1615 * The volume value is a raw scalar so UI controls should be scaled logarithmically. 1616 * If a volume of -1 is specified, the AudioManager.STREAM_MUSIC stream volume minus 3dB will be used. 1617 * NOTE: This version is for applications that have their own 1618 * settings panel for enabling and controlling volume. 1619 */ 1620 public void playSoundEffect(int effectType, float volume) { 1621 if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) { 1622 return; 1623 } 1624 1625 IAudioService service = getService(); 1626 try { 1627 service.playSoundEffectVolume(effectType, volume); 1628 } catch (RemoteException e) { 1629 Log.e(TAG, "Dead object in playSoundEffect"+e); 1630 } 1631 } 1632 1633 /** 1634 * Settings has an in memory cache, so this is fast. 1635 */ 1636 private boolean querySoundEffectsEnabled() { 1637 return Settings.System.getInt(mContext.getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0; 1638 } 1639 1640 1641 /** 1642 * Load Sound effects. 1643 * This method must be called when sound effects are enabled. 1644 */ 1645 public void loadSoundEffects() { 1646 IAudioService service = getService(); 1647 try { 1648 service.loadSoundEffects(); 1649 } catch (RemoteException e) { 1650 Log.e(TAG, "Dead object in loadSoundEffects"+e); 1651 } 1652 } 1653 1654 /** 1655 * Unload Sound effects. 1656 * This method can be called to free some memory when 1657 * sound effects are disabled. 1658 */ 1659 public void unloadSoundEffects() { 1660 IAudioService service = getService(); 1661 try { 1662 service.unloadSoundEffects(); 1663 } catch (RemoteException e) { 1664 Log.e(TAG, "Dead object in unloadSoundEffects"+e); 1665 } 1666 } 1667 1668 /** 1669 * Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration. 1670 * @see OnAudioFocusChangeListener#onAudioFocusChange(int) 1671 * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int) 1672 */ 1673 public static final int AUDIOFOCUS_GAIN = 1; 1674 /** 1675 * Used to indicate a temporary gain or request of audio focus, anticipated to last a short 1676 * amount of time. Examples of temporary changes are the playback of driving directions, or an 1677 * event notification. 1678 * @see OnAudioFocusChangeListener#onAudioFocusChange(int) 1679 * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int) 1680 */ 1681 public static final int AUDIOFOCUS_GAIN_TRANSIENT = 2; 1682 /** 1683 * Used to indicate a temporary request of audio focus, anticipated to last a short 1684 * amount of time, and where it is acceptable for other audio applications to keep playing 1685 * after having lowered their output level (also referred to as "ducking"). 1686 * Examples of temporary changes are the playback of driving directions where playback of music 1687 * in the background is acceptable. 1688 * @see OnAudioFocusChangeListener#onAudioFocusChange(int) 1689 * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int) 1690 */ 1691 public static final int AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = 3; 1692 /** 1693 * Used to indicate a loss of audio focus of unknown duration. 1694 * @see OnAudioFocusChangeListener#onAudioFocusChange(int) 1695 */ 1696 public static final int AUDIOFOCUS_LOSS = -1 * AUDIOFOCUS_GAIN; 1697 /** 1698 * Used to indicate a transient loss of audio focus. 1699 * @see OnAudioFocusChangeListener#onAudioFocusChange(int) 1700 */ 1701 public static final int AUDIOFOCUS_LOSS_TRANSIENT = -1 * AUDIOFOCUS_GAIN_TRANSIENT; 1702 /** 1703 * Used to indicate a transient loss of audio focus where the loser of the audio focus can 1704 * lower its output volume if it wants to continue playing (also referred to as "ducking"), as 1705 * the new focus owner doesn't require others to be silent. 1706 * @see OnAudioFocusChangeListener#onAudioFocusChange(int) 1707 */ 1708 public static final int AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK = 1709 -1 * AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK; 1710 1711 /** 1712 * Interface definition for a callback to be invoked when the audio focus of the system is 1713 * updated. 1714 */ 1715 public interface OnAudioFocusChangeListener { 1716 /** 1717 * Called on the listener to notify it the audio focus for this listener has been changed. 1718 * The focusChange value indicates whether the focus was gained, 1719 * whether the focus was lost, and whether that loss is transient, or whether the new focus 1720 * holder will hold it for an unknown amount of time. 1721 * When losing focus, listeners can use the focus change information to decide what 1722 * behavior to adopt when losing focus. A music player could for instance elect to lower 1723 * the volume of its music stream (duck) for transient focus losses, and pause otherwise. 1724 * @param focusChange the type of focus change, one of {@link AudioManager#AUDIOFOCUS_GAIN}, 1725 * {@link AudioManager#AUDIOFOCUS_LOSS}, {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT} 1726 * and {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK}. 1727 */ 1728 public void onAudioFocusChange(int focusChange); 1729 } 1730 1731 /** 1732 * Map to convert focus event listener IDs, as used in the AudioService audio focus stack, 1733 * to actual listener objects. 1734 */ 1735 private final HashMap<String, OnAudioFocusChangeListener> mAudioFocusIdListenerMap = 1736 new HashMap<String, OnAudioFocusChangeListener>(); 1737 /** 1738 * Lock to prevent concurrent changes to the list of focus listeners for this AudioManager 1739 * instance. 1740 */ 1741 private final Object mFocusListenerLock = new Object(); 1742 1743 private OnAudioFocusChangeListener findFocusListener(String id) { 1744 return mAudioFocusIdListenerMap.get(id); 1745 } 1746 1747 /** 1748 * Handler for audio focus events coming from the audio service. 1749 */ 1750 private final FocusEventHandlerDelegate mAudioFocusEventHandlerDelegate = 1751 new FocusEventHandlerDelegate(); 1752 1753 /** 1754 * Helper class to handle the forwarding of audio focus events to the appropriate listener 1755 */ 1756 private class FocusEventHandlerDelegate { 1757 private final Handler mHandler; 1758 1759 FocusEventHandlerDelegate() { 1760 Looper looper; 1761 if ((looper = Looper.myLooper()) == null) { 1762 looper = Looper.getMainLooper(); 1763 } 1764 1765 if (looper != null) { 1766 // implement the event handler delegate to receive audio focus events 1767 mHandler = new Handler(looper) { 1768 @Override 1769 public void handleMessage(Message msg) { 1770 OnAudioFocusChangeListener listener = null; 1771 synchronized(mFocusListenerLock) { 1772 listener = findFocusListener((String)msg.obj); 1773 } 1774 if (listener != null) { 1775 listener.onAudioFocusChange(msg.what); 1776 } 1777 } 1778 }; 1779 } else { 1780 mHandler = null; 1781 } 1782 } 1783 1784 Handler getHandler() { 1785 return mHandler; 1786 } 1787 } 1788 1789 private final IAudioFocusDispatcher mAudioFocusDispatcher = new IAudioFocusDispatcher.Stub() { 1790 1791 public void dispatchAudioFocusChange(int focusChange, String id) { 1792 Message m = mAudioFocusEventHandlerDelegate.getHandler().obtainMessage(focusChange, id); 1793 mAudioFocusEventHandlerDelegate.getHandler().sendMessage(m); 1794 } 1795 1796 }; 1797 1798 private String getIdForAudioFocusListener(OnAudioFocusChangeListener l) { 1799 if (l == null) { 1800 return new String(this.toString()); 1801 } else { 1802 return new String(this.toString() + l.toString()); 1803 } 1804 } 1805 1806 /** 1807 * @hide 1808 * Registers a listener to be called when audio focus changes. Calling this method is optional 1809 * before calling {@link #requestAudioFocus(OnAudioFocusChangeListener, int, int)}, as it 1810 * will register the listener as well if it wasn't registered already. 1811 * @param l the listener to be notified of audio focus changes. 1812 */ 1813 public void registerAudioFocusListener(OnAudioFocusChangeListener l) { 1814 synchronized(mFocusListenerLock) { 1815 if (mAudioFocusIdListenerMap.containsKey(getIdForAudioFocusListener(l))) { 1816 return; 1817 } 1818 mAudioFocusIdListenerMap.put(getIdForAudioFocusListener(l), l); 1819 } 1820 } 1821 1822 /** 1823 * @hide 1824 * Causes the specified listener to not be called anymore when focus is gained or lost. 1825 * @param l the listener to unregister. 1826 */ 1827 public void unregisterAudioFocusListener(OnAudioFocusChangeListener l) { 1828 1829 // remove locally 1830 synchronized(mFocusListenerLock) { 1831 mAudioFocusIdListenerMap.remove(getIdForAudioFocusListener(l)); 1832 } 1833 } 1834 1835 1836 /** 1837 * A failed focus change request. 1838 */ 1839 public static final int AUDIOFOCUS_REQUEST_FAILED = 0; 1840 /** 1841 * A successful focus change request. 1842 */ 1843 public static final int AUDIOFOCUS_REQUEST_GRANTED = 1; 1844 1845 1846 /** 1847 * Request audio focus. 1848 * Send a request to obtain the audio focus 1849 * @param l the listener to be notified of audio focus changes 1850 * @param streamType the main audio stream type affected by the focus request 1851 * @param durationHint use {@link #AUDIOFOCUS_GAIN_TRANSIENT} to indicate this focus request 1852 * is temporary, and focus will be abandonned shortly. Examples of transient requests are 1853 * for the playback of driving directions, or notifications sounds. 1854 * Use {@link #AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK} to indicate also that it's ok for 1855 * the previous focus owner to keep playing if it ducks its audio output. 1856 * Use {@link #AUDIOFOCUS_GAIN} for a focus request of unknown duration such 1857 * as the playback of a song or a video. 1858 * @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED} 1859 */ 1860 public int requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint) { 1861 int status = AUDIOFOCUS_REQUEST_FAILED; 1862 if ((durationHint < AUDIOFOCUS_GAIN) || (durationHint > AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK)) 1863 { 1864 Log.e(TAG, "Invalid duration hint, audio focus request denied"); 1865 return status; 1866 } 1867 registerAudioFocusListener(l); 1868 //TODO protect request by permission check? 1869 IAudioService service = getService(); 1870 try { 1871 status = service.requestAudioFocus(streamType, durationHint, mICallBack, 1872 mAudioFocusDispatcher, getIdForAudioFocusListener(l), 1873 mContext.getPackageName() /* package name */); 1874 } catch (RemoteException e) { 1875 Log.e(TAG, "Can't call requestAudioFocus() on AudioService due to "+e); 1876 } 1877 return status; 1878 } 1879 1880 /** 1881 * @hide 1882 * Used internally by telephony package to request audio focus. Will cause the focus request 1883 * to be associated with the "voice communication" identifier only used in AudioService 1884 * to identify this use case. 1885 * @param streamType use STREAM_RING for focus requests when ringing, VOICE_CALL for 1886 * the establishment of the call 1887 * @param durationHint the type of focus request. AUDIOFOCUS_GAIN_TRANSIENT is recommended so 1888 * media applications resume after a call 1889 */ 1890 public void requestAudioFocusForCall(int streamType, int durationHint) { 1891 IAudioService service = getService(); 1892 try { 1893 service.requestAudioFocus(streamType, durationHint, mICallBack, null, 1894 AudioService.IN_VOICE_COMM_FOCUS_ID, 1895 "system" /* dump-friendly package name */); 1896 } catch (RemoteException e) { 1897 Log.e(TAG, "Can't call requestAudioFocusForCall() on AudioService due to "+e); 1898 } 1899 } 1900 1901 /** 1902 * @hide 1903 * Used internally by telephony package to abandon audio focus, typically after a call or 1904 * when ringing ends and the call is rejected or not answered. 1905 * Should match one or more calls to {@link #requestAudioFocusForCall(int, int)}. 1906 */ 1907 public void abandonAudioFocusForCall() { 1908 IAudioService service = getService(); 1909 try { 1910 service.abandonAudioFocus(null, AudioService.IN_VOICE_COMM_FOCUS_ID); 1911 } catch (RemoteException e) { 1912 Log.e(TAG, "Can't call abandonAudioFocusForCall() on AudioService due to "+e); 1913 } 1914 } 1915 1916 /** 1917 * Abandon audio focus. Causes the previous focus owner, if any, to receive focus. 1918 * @param l the listener with which focus was requested. 1919 * @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED} 1920 */ 1921 public int abandonAudioFocus(OnAudioFocusChangeListener l) { 1922 int status = AUDIOFOCUS_REQUEST_FAILED; 1923 unregisterAudioFocusListener(l); 1924 IAudioService service = getService(); 1925 try { 1926 status = service.abandonAudioFocus(mAudioFocusDispatcher, 1927 getIdForAudioFocusListener(l)); 1928 } catch (RemoteException e) { 1929 Log.e(TAG, "Can't call abandonAudioFocus() on AudioService due to "+e); 1930 } 1931 return status; 1932 } 1933 1934 1935 //==================================================================== 1936 // Remote Control 1937 /** 1938 * Register a component to be the sole receiver of MEDIA_BUTTON intents. 1939 * @param eventReceiver identifier of a {@link android.content.BroadcastReceiver} 1940 * that will receive the media button intent. This broadcast receiver must be declared 1941 * in the application manifest. The package of the component must match that of 1942 * the context you're registering from. 1943 */ 1944 public void registerMediaButtonEventReceiver(ComponentName eventReceiver) { 1945 if (eventReceiver == null) { 1946 return; 1947 } 1948 if (!eventReceiver.getPackageName().equals(mContext.getPackageName())) { 1949 Log.e(TAG, "registerMediaButtonEventReceiver() error: " + 1950 "receiver and context package names don't match"); 1951 return; 1952 } 1953 // construct a PendingIntent for the media button and register it 1954 Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); 1955 // the associated intent will be handled by the component being registered 1956 mediaButtonIntent.setComponent(eventReceiver); 1957 PendingIntent pi = PendingIntent.getBroadcast(mContext, 1958 0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/); 1959 registerMediaButtonIntent(pi, eventReceiver); 1960 } 1961 1962 /** 1963 * @hide 1964 * no-op if (pi == null) or (eventReceiver == null) 1965 */ 1966 public void registerMediaButtonIntent(PendingIntent pi, ComponentName eventReceiver) { 1967 if ((pi == null) || (eventReceiver == null)) { 1968 Log.e(TAG, "Cannot call registerMediaButtonIntent() with a null parameter"); 1969 return; 1970 } 1971 IAudioService service = getService(); 1972 try { 1973 // pi != null 1974 service.registerMediaButtonIntent(pi, eventReceiver); 1975 } catch (RemoteException e) { 1976 Log.e(TAG, "Dead object in registerMediaButtonIntent"+e); 1977 } 1978 } 1979 1980 /** 1981 * Unregister the receiver of MEDIA_BUTTON intents. 1982 * @param eventReceiver identifier of a {@link android.content.BroadcastReceiver} 1983 * that was registered with {@link #registerMediaButtonEventReceiver(ComponentName)}. 1984 */ 1985 public void unregisterMediaButtonEventReceiver(ComponentName eventReceiver) { 1986 if (eventReceiver == null) { 1987 return; 1988 } 1989 // construct a PendingIntent for the media button and unregister it 1990 Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); 1991 // the associated intent will be handled by the component being registered 1992 mediaButtonIntent.setComponent(eventReceiver); 1993 PendingIntent pi = PendingIntent.getBroadcast(mContext, 1994 0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/); 1995 unregisterMediaButtonIntent(pi, eventReceiver); 1996 } 1997 1998 /** 1999 * @hide 2000 */ 2001 public void unregisterMediaButtonIntent(PendingIntent pi, ComponentName eventReceiver) { 2002 IAudioService service = getService(); 2003 try { 2004 service.unregisterMediaButtonIntent(pi, eventReceiver); 2005 } catch (RemoteException e) { 2006 Log.e(TAG, "Dead object in unregisterMediaButtonIntent"+e); 2007 } 2008 } 2009 2010 /** 2011 * Registers the remote control client for providing information to display on the remote 2012 * controls. 2013 * @param rcClient The remote control client from which remote controls will receive 2014 * information to display. 2015 * @see RemoteControlClient 2016 */ 2017 public void registerRemoteControlClient(RemoteControlClient rcClient) { 2018 if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) { 2019 return; 2020 } 2021 IAudioService service = getService(); 2022 try { 2023 service.registerRemoteControlClient(rcClient.getRcMediaIntent(), /* mediaIntent */ 2024 rcClient.getIRemoteControlClient(), /* rcClient */ 2025 // used to match media button event receiver and audio focus 2026 mContext.getPackageName()); /* packageName */ 2027 } catch (RemoteException e) { 2028 Log.e(TAG, "Dead object in registerRemoteControlClient"+e); 2029 } 2030 } 2031 2032 /** 2033 * Unregisters the remote control client that was providing information to display on the 2034 * remote controls. 2035 * @param rcClient The remote control client to unregister. 2036 * @see #registerRemoteControlClient(RemoteControlClient) 2037 */ 2038 public void unregisterRemoteControlClient(RemoteControlClient rcClient) { 2039 if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) { 2040 return; 2041 } 2042 IAudioService service = getService(); 2043 try { 2044 service.unregisterRemoteControlClient(rcClient.getRcMediaIntent(), /* mediaIntent */ 2045 rcClient.getIRemoteControlClient()); /* rcClient */ 2046 } catch (RemoteException e) { 2047 Log.e(TAG, "Dead object in unregisterRemoteControlClient"+e); 2048 } 2049 } 2050 2051 /** 2052 * @hide 2053 * Registers a remote control display that will be sent information by remote control clients. 2054 * @param rcd 2055 */ 2056 public void registerRemoteControlDisplay(IRemoteControlDisplay rcd) { 2057 if (rcd == null) { 2058 return; 2059 } 2060 IAudioService service = getService(); 2061 try { 2062 service.registerRemoteControlDisplay(rcd); 2063 } catch (RemoteException e) { 2064 Log.e(TAG, "Dead object in registerRemoteControlDisplay " + e); 2065 } 2066 } 2067 2068 /** 2069 * @hide 2070 * Unregisters a remote control display that was sent information by remote control clients. 2071 * @param rcd 2072 */ 2073 public void unregisterRemoteControlDisplay(IRemoteControlDisplay rcd) { 2074 if (rcd == null) { 2075 return; 2076 } 2077 IAudioService service = getService(); 2078 try { 2079 service.unregisterRemoteControlDisplay(rcd); 2080 } catch (RemoteException e) { 2081 Log.e(TAG, "Dead object in unregisterRemoteControlDisplay " + e); 2082 } 2083 } 2084 2085 /** 2086 * @hide 2087 * Sets the artwork size a remote control display expects when receiving bitmaps. 2088 * @param rcd 2089 * @param w the maximum width of the expected bitmap. Negative values indicate it is 2090 * useless to send artwork. 2091 * @param h the maximum height of the expected bitmap. Negative values indicate it is 2092 * useless to send artwork. 2093 */ 2094 public void remoteControlDisplayUsesBitmapSize(IRemoteControlDisplay rcd, int w, int h) { 2095 if (rcd == null) { 2096 return; 2097 } 2098 IAudioService service = getService(); 2099 try { 2100 service.remoteControlDisplayUsesBitmapSize(rcd, w, h); 2101 } catch (RemoteException e) { 2102 Log.e(TAG, "Dead object in remoteControlDisplayUsesBitmapSize " + e); 2103 } 2104 } 2105 2106 // FIXME remove because we are not using intents anymore between AudioService and RcDisplay 2107 /** 2108 * @hide 2109 * Broadcast intent action indicating that the displays on the remote controls 2110 * should be updated because a new remote control client is now active. If there is no 2111 * {@link #EXTRA_REMOTE_CONTROL_CLIENT}, the remote control display should be cleared 2112 * because there is no valid client to supply it with information. 2113 * 2114 * @see #EXTRA_REMOTE_CONTROL_CLIENT 2115 */ 2116 public static final String REMOTE_CONTROL_CLIENT_CHANGED = 2117 "android.media.REMOTE_CONTROL_CLIENT_CHANGED"; 2118 2119 // FIXME remove because we are not using intents anymore between AudioService and RcDisplay 2120 /** 2121 * @hide 2122 * The IRemoteControlClientDispatcher monotonically increasing generation counter. 2123 * 2124 * @see #REMOTE_CONTROL_CLIENT_CHANGED_ACTION 2125 */ 2126 public static final String EXTRA_REMOTE_CONTROL_CLIENT_GENERATION = 2127 "android.media.EXTRA_REMOTE_CONTROL_CLIENT_GENERATION"; 2128 2129 // FIXME remove because we are not using intents anymore between AudioService and RcDisplay 2130 /** 2131 * @hide 2132 * The name of the RemoteControlClient. 2133 * This String is passed as the client name when calling methods from the 2134 * IRemoteControlClientDispatcher interface. 2135 * 2136 * @see #REMOTE_CONTROL_CLIENT_CHANGED_ACTION 2137 */ 2138 public static final String EXTRA_REMOTE_CONTROL_CLIENT_NAME = 2139 "android.media.EXTRA_REMOTE_CONTROL_CLIENT_NAME"; 2140 2141 // FIXME remove because we are not using intents anymore between AudioService and RcDisplay 2142 /** 2143 * @hide 2144 * The media button event receiver associated with the RemoteControlClient. 2145 * The {@link android.content.ComponentName} value of the event receiver can be retrieved with 2146 * {@link android.content.ComponentName#unflattenFromString(String)} 2147 * 2148 * @see #REMOTE_CONTROL_CLIENT_CHANGED_ACTION 2149 */ 2150 public static final String EXTRA_REMOTE_CONTROL_EVENT_RECEIVER = 2151 "android.media.EXTRA_REMOTE_CONTROL_EVENT_RECEIVER"; 2152 2153 // FIXME remove because we are not using intents anymore between AudioService and RcDisplay 2154 /** 2155 * @hide 2156 * The flags describing what information has changed in the current remote control client. 2157 * 2158 * @see #REMOTE_CONTROL_CLIENT_CHANGED_ACTION 2159 */ 2160 public static final String EXTRA_REMOTE_CONTROL_CLIENT_INFO_CHANGED = 2161 "android.media.EXTRA_REMOTE_CONTROL_CLIENT_INFO_CHANGED"; 2162 2163 /** 2164 * @hide 2165 * Reload audio settings. This method is called by Settings backup 2166 * agent when audio settings are restored and causes the AudioService 2167 * to read and apply restored settings. 2168 */ 2169 public void reloadAudioSettings() { 2170 IAudioService service = getService(); 2171 try { 2172 service.reloadAudioSettings(); 2173 } catch (RemoteException e) { 2174 Log.e(TAG, "Dead object in reloadAudioSettings"+e); 2175 } 2176 } 2177 2178 /** 2179 * {@hide} 2180 */ 2181 private final IBinder mICallBack = new Binder(); 2182 2183 /** 2184 * Checks whether the phone is in silent mode, with or without vibrate. 2185 * 2186 * @return true if phone is in silent mode, with or without vibrate. 2187 * 2188 * @see #getRingerMode() 2189 * 2190 * @hide pending API Council approval 2191 */ 2192 public boolean isSilentMode() { 2193 int ringerMode = getRingerMode(); 2194 boolean silentMode = 2195 (ringerMode == RINGER_MODE_SILENT) || 2196 (ringerMode == RINGER_MODE_VIBRATE); 2197 return silentMode; 2198 } 2199 2200 // This section re-defines new output device constants from AudioSystem, because the AudioSystem 2201 // class is not used by other parts of the framework, which instead use definitions and methods 2202 // from AudioManager. AudioSystem is an internal class used by AudioManager and AudioService. 2203 2204 /** {@hide} The audio output device code for the small speaker at the front of the device used 2205 * when placing calls. Does not refer to an in-ear headphone without attached microphone, 2206 * such as earbuds, earphones, or in-ear monitors (IEM). Those would be handled as a 2207 * {@link #DEVICE_OUT_WIRED_HEADPHONE}. 2208 */ 2209 public static final int DEVICE_OUT_EARPIECE = AudioSystem.DEVICE_OUT_EARPIECE; 2210 /** {@hide} The audio output device code for the built-in speaker */ 2211 public static final int DEVICE_OUT_SPEAKER = AudioSystem.DEVICE_OUT_SPEAKER; 2212 /** {@hide} The audio output device code for a wired headset with attached microphone */ 2213 public static final int DEVICE_OUT_WIRED_HEADSET = AudioSystem.DEVICE_OUT_WIRED_HEADSET; 2214 /** {@hide} The audio output device code for a wired headphone without attached microphone */ 2215 public static final int DEVICE_OUT_WIRED_HEADPHONE = AudioSystem.DEVICE_OUT_WIRED_HEADPHONE; 2216 /** {@hide} The audio output device code for generic Bluetooth SCO, for voice */ 2217 public static final int DEVICE_OUT_BLUETOOTH_SCO = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO; 2218 /** {@hide} The audio output device code for Bluetooth SCO Headset Profile (HSP) and 2219 * Hands-Free Profile (HFP), for voice 2220 */ 2221 public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = 2222 AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET; 2223 /** {@hide} The audio output device code for Bluetooth SCO car audio, for voice */ 2224 public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = 2225 AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT; 2226 /** {@hide} The audio output device code for generic Bluetooth A2DP, for music */ 2227 public static final int DEVICE_OUT_BLUETOOTH_A2DP = AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP; 2228 /** {@hide} The audio output device code for Bluetooth A2DP headphones, for music */ 2229 public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = 2230 AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; 2231 /** {@hide} The audio output device code for Bluetooth A2DP external speaker, for music */ 2232 public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = 2233 AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; 2234 /** {@hide} The audio output device code for S/PDIF or HDMI */ 2235 public static final int DEVICE_OUT_AUX_DIGITAL = AudioSystem.DEVICE_OUT_AUX_DIGITAL; 2236 /** {@hide} The audio output device code for an analog wired headset attached via a 2237 * docking station 2238 */ 2239 public static final int DEVICE_OUT_ANLG_DOCK_HEADSET = AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; 2240 /** {@hide} The audio output device code for a digital wired headset attached via a 2241 * docking station 2242 */ 2243 public static final int DEVICE_OUT_DGTL_DOCK_HEADSET = AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET; 2244 /** {@hide} The audio output device code for a USB audio accessory. The accessory is in USB host 2245 * mode and the Android device in USB device mode 2246 */ 2247 public static final int DEVICE_OUT_USB_ACCESSORY = AudioSystem.DEVICE_OUT_USB_ACCESSORY; 2248 /** {@hide} The audio output device code for a USB audio device. The device is in USB device 2249 * mode and the Android device in USB host mode 2250 */ 2251 public static final int DEVICE_OUT_USB_DEVICE = AudioSystem.DEVICE_OUT_USB_DEVICE; 2252 /** {@hide} This is not used as a returned value from {@link #getDevicesForStream}, but could be 2253 * used in the future in a set method to select whatever default device is chosen by the 2254 * platform-specific implementation. 2255 */ 2256 public static final int DEVICE_OUT_DEFAULT = AudioSystem.DEVICE_OUT_DEFAULT; 2257 2258 /** 2259 * Return the enabled devices for the specified output stream type. 2260 * 2261 * @param streamType The stream type to query. One of 2262 * {@link #STREAM_VOICE_CALL}, 2263 * {@link #STREAM_SYSTEM}, 2264 * {@link #STREAM_RING}, 2265 * {@link #STREAM_MUSIC}, 2266 * {@link #STREAM_ALARM}, 2267 * {@link #STREAM_NOTIFICATION}, 2268 * {@link #STREAM_DTMF}. 2269 * 2270 * @return The bit-mask "or" of audio output device codes for all enabled devices on this 2271 * stream. Zero or more of 2272 * {@link #DEVICE_OUT_EARPIECE}, 2273 * {@link #DEVICE_OUT_SPEAKER}, 2274 * {@link #DEVICE_OUT_WIRED_HEADSET}, 2275 * {@link #DEVICE_OUT_WIRED_HEADPHONE}, 2276 * {@link #DEVICE_OUT_BLUETOOTH_SCO}, 2277 * {@link #DEVICE_OUT_BLUETOOTH_SCO_HEADSET}, 2278 * {@link #DEVICE_OUT_BLUETOOTH_SCO_CARKIT}, 2279 * {@link #DEVICE_OUT_BLUETOOTH_A2DP}, 2280 * {@link #DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}, 2281 * {@link #DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER}, 2282 * {@link #DEVICE_OUT_AUX_DIGITAL}, 2283 * {@link #DEVICE_OUT_ANLG_DOCK_HEADSET}, 2284 * {@link #DEVICE_OUT_DGTL_DOCK_HEADSET}. 2285 * {@link #DEVICE_OUT_DEFAULT} is not used here. 2286 * 2287 * The implementation may support additional device codes beyond those listed, so 2288 * the application should ignore any bits which it does not recognize. 2289 * Note that the information may be imprecise when the implementation 2290 * cannot distinguish whether a particular device is enabled. 2291 * 2292 * {@hide} 2293 */ 2294 public int getDevicesForStream(int streamType) { 2295 switch (streamType) { 2296 case STREAM_VOICE_CALL: 2297 case STREAM_SYSTEM: 2298 case STREAM_RING: 2299 case STREAM_MUSIC: 2300 case STREAM_ALARM: 2301 case STREAM_NOTIFICATION: 2302 case STREAM_DTMF: 2303 return AudioSystem.getDevicesForStream(streamType); 2304 default: 2305 return 0; 2306 } 2307 } 2308 2309} 2310