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