AudioManager.java revision f013e1afd1e68af5e3b868c26a653bbfb39538f8
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.content.Context; 22import android.database.ContentObserver; 23import android.os.Binder; 24import android.os.Handler; 25import android.os.IBinder; 26import android.os.RemoteException; 27import android.os.ServiceManager; 28import android.provider.Settings; 29import android.util.Log; 30 31/** 32 * AudioManager provides access to volume and ringer mode control. 33 * <p> 34 * Use <code>Context.getSystemService(Context.AUDIO_SERVICE)</code> to get 35 * an instance of this class. 36 */ 37public class AudioManager { 38 39 private final Context mContext; 40 private final Handler mHandler; 41 42 // used to listen for updates to the sound effects settings so we don't 43 // poll it for every UI sound 44 private ContentObserver mContentObserver; 45 46 47 private static String TAG = "AudioManager"; 48 private static boolean DEBUG = false; 49 private static boolean localLOGV = DEBUG || android.util.Config.LOGV; 50 51 /** 52 * Sticky broadcast intent action indicating that the ringer mode has 53 * changed. Includes the new ringer mode. 54 * 55 * @see #EXTRA_RINGER_MODE 56 */ 57 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 58 public static final String RINGER_MODE_CHANGED_ACTION = "android.media.RINGER_MODE_CHANGED"; 59 60 /** 61 * The new ringer mode. 62 * 63 * @see #RINGER_MODE_CHANGED_ACTION 64 * @see #RINGER_MODE_NORMAL 65 * @see #RINGER_MODE_SILENT 66 * @see #RINGER_MODE_VIBRATE 67 */ 68 public static final String EXTRA_RINGER_MODE = "android.media.EXTRA_RINGER_MODE"; 69 70 /** 71 * Broadcast intent action indicating that the vibrate setting has 72 * changed. Includes the vibrate type and its new setting. 73 * 74 * @see #EXTRA_VIBRATE_TYPE 75 * @see #EXTRA_VIBRATE_SETTING 76 */ 77 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) 78 public static final String VIBRATE_SETTING_CHANGED_ACTION = "android.media.VIBRATE_SETTING_CHANGED"; 79 80 /** 81 * The new vibrate setting for a particular type. 82 * 83 * @see #VIBRATE_SETTING_CHANGED_ACTION 84 * @see #EXTRA_VIBRATE_TYPE 85 * @see #VIBRATE_SETTING_ON 86 * @see #VIBRATE_SETTING_OFF 87 * @see #VIBRATE_SETTING_ONLY_SILENT 88 */ 89 public static final String EXTRA_VIBRATE_SETTING = "android.media.EXTRA_VIBRATE_SETTING"; 90 91 /** 92 * The vibrate type whose setting has changed. 93 * 94 * @see #VIBRATE_SETTING_CHANGED_ACTION 95 * @see #VIBRATE_TYPE_NOTIFICATION 96 * @see #VIBRATE_TYPE_RINGER 97 */ 98 public static final String EXTRA_VIBRATE_TYPE = "android.media.EXTRA_VIBRATE_TYPE"; 99 100 /** The audio stream for phone calls */ 101 public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL; 102 /** The audio stream for system sounds */ 103 public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM; 104 /** The audio stream for the phone ring and message alerts */ 105 public static final int STREAM_RING = AudioSystem.STREAM_RING; 106 /** The audio stream for music playback */ 107 public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC; 108 /** The audio stream for alarms */ 109 public static final int STREAM_ALARM = AudioSystem.STREAM_ALARM; 110 /** The audio stream for notifications */ 111 public static final int STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION; 112 /** Number of audio streams */ 113 /** 114 * @deprecated Use AudioSystem.getNumStreamTypes() instead 115 */ 116 public static final int NUM_STREAMS = AudioSystem.NUM_STREAMS; 117 118 119 /** @hide Maximum volume index values for audio streams */ 120 public static final int[] MAX_STREAM_VOLUME = new int[] { 121 6, // STREAM_VOICE_CALL 122 8, // STREAM_SYSTEM 123 8, // STREAM_RING 124 16, // STREAM_MUSIC 125 8, // STREAM_ALARM 126 8 // STREAM_NOTIFICATION 127 }; 128 129 /** @hide Default volume index values for audio streams */ 130 public static final int[] DEFAULT_STREAM_VOLUME = new int[] { 131 4, // STREAM_VOICE_CALL 132 5, // STREAM_SYSTEM 133 5, // STREAM_RING 134 11, // STREAM_MUSIC 135 6, // STREAM_ALARM 136 5 // STREAM_NOTIFICATION 137 }; 138 139 /** 140 * Increase the ringer volume. 141 * 142 * @see #adjustVolume(int, int) 143 * @see #adjustStreamVolume(int, int, int) 144 */ 145 public static final int ADJUST_RAISE = 1; 146 147 /** 148 * Decrease the ringer volume. 149 * 150 * @see #adjustVolume(int, int) 151 * @see #adjustStreamVolume(int, int, int) 152 */ 153 public static final int ADJUST_LOWER = -1; 154 155 /** 156 * Maintain the previous ringer volume. This may be useful when needing to 157 * show the volume toast without actually modifying the volume. 158 * 159 * @see #adjustVolume(int, int) 160 * @see #adjustStreamVolume(int, int, int) 161 */ 162 public static final int ADJUST_SAME = 0; 163 164 // Flags should be powers of 2! 165 166 /** 167 * Show a toast containing the current volume. 168 * 169 * @see #adjustStreamVolume(int, int, int) 170 * @see #adjustVolume(int, int) 171 * @see #setStreamVolume(int, int, int) 172 * @see #setRingerMode(int) 173 */ 174 public static final int FLAG_SHOW_UI = 1 << 0; 175 176 /** 177 * Whether to include ringer modes as possible options when changing volume. 178 * For example, if true and volume level is 0 and the volume is adjusted 179 * with {@link #ADJUST_LOWER}, then the ringer mode may switch the silent 180 * or vibrate mode. 181 * <p> 182 * By default this is on for stream types that are affected by the ringer 183 * mode (for example, the ring stream type). If this flag is included, this 184 * behavior will be present regardless of the stream type being affected by 185 * the ringer mode. 186 * 187 * @see #adjustVolume(int, int) 188 * @see #adjustStreamVolume(int, int, int) 189 */ 190 public static final int FLAG_ALLOW_RINGER_MODES = 1 << 1; 191 192 /** 193 * Whether to play a sound when changing the volume. 194 * <p> 195 * If this is given to {@link #adjustVolume(int, int)} or 196 * {@link #adjustSuggestedStreamVolume(int, int, int)}, it may be ignored 197 * in some cases (for example, the decided stream type is not 198 * {@link AudioManager#STREAM_RING}, or the volume is being adjusted 199 * downward). 200 * 201 * @see #adjustStreamVolume(int, int, int) 202 * @see #adjustVolume(int, int) 203 * @see #setStreamVolume(int, int, int) 204 */ 205 public static final int FLAG_PLAY_SOUND = 1 << 2; 206 207 /** 208 * Removes any sounds/vibrate that may be in the queue, or are playing (related to 209 * changing volume). 210 */ 211 public static final int FLAG_REMOVE_SOUND_AND_VIBRATE = 1 << 3; 212 213 /** 214 * Whether to vibrate if going into the vibrate ringer mode. 215 */ 216 public static final int FLAG_VIBRATE = 1 << 4; 217 218 /** 219 * Ringer mode that will be silent and will not vibrate. (This overrides the 220 * vibrate setting.) 221 * 222 * @see #setRingerMode(int) 223 * @see #getRingerMode() 224 */ 225 public static final int RINGER_MODE_SILENT = 0; 226 227 /** 228 * Ringer mode that will be silent and will vibrate. (This will cause the 229 * phone ringer to always vibrate, but the notification vibrate to only 230 * vibrate if set.) 231 * 232 * @see #setRingerMode(int) 233 * @see #getRingerMode() 234 */ 235 public static final int RINGER_MODE_VIBRATE = 1; 236 237 /** 238 * Ringer mode that may be audible and may vibrate. It will be audible if 239 * the volume before changing out of this mode was audible. It will vibrate 240 * if the vibrate setting is on. 241 * 242 * @see #setRingerMode(int) 243 * @see #getRingerMode() 244 */ 245 public static final int RINGER_MODE_NORMAL = 2; 246 247 /** 248 * Vibrate type that corresponds to the ringer. 249 * 250 * @see #setVibrateSetting(int, int) 251 * @see #getVibrateSetting(int) 252 * @see #shouldVibrate(int) 253 */ 254 public static final int VIBRATE_TYPE_RINGER = 0; 255 256 /** 257 * Vibrate type that corresponds to notifications. 258 * 259 * @see #setVibrateSetting(int, int) 260 * @see #getVibrateSetting(int) 261 * @see #shouldVibrate(int) 262 */ 263 public static final int VIBRATE_TYPE_NOTIFICATION = 1; 264 265 /** 266 * Vibrate setting that suggests to never vibrate. 267 * 268 * @see #setVibrateSetting(int, int) 269 * @see #getVibrateSetting(int) 270 */ 271 public static final int VIBRATE_SETTING_OFF = 0; 272 273 /** 274 * Vibrate setting that suggests to vibrate when possible. 275 * 276 * @see #setVibrateSetting(int, int) 277 * @see #getVibrateSetting(int) 278 */ 279 public static final int VIBRATE_SETTING_ON = 1; 280 281 /** 282 * Vibrate setting that suggests to only vibrate when in the vibrate ringer 283 * mode. 284 * 285 * @see #setVibrateSetting(int, int) 286 * @see #getVibrateSetting(int) 287 */ 288 public static final int VIBRATE_SETTING_ONLY_SILENT = 2; 289 290 /** 291 * Suggests using the default stream type. This may not be used in all 292 * places a stream type is needed. 293 */ 294 public static final int USE_DEFAULT_STREAM_TYPE = Integer.MIN_VALUE; 295 296 private static IAudioService sService; 297 298 /** 299 * @hide 300 */ 301 public AudioManager(Context context) { 302 mContext = context; 303 mHandler = new Handler(context.getMainLooper()); 304 } 305 306 private static IAudioService getService() 307 { 308 if (sService != null) { 309 return sService; 310 } 311 IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE); 312 sService = IAudioService.Stub.asInterface(b); 313 return sService; 314 } 315 316 /** 317 * Adjusts the volume of a particular stream by one step in a direction. 318 * 319 * @param streamType The stream type to adjust. One of {@link #STREAM_VOICE_CALL}, 320 * {@link #STREAM_SYSTEM}, {@link #STREAM_RING}, {@link #STREAM_MUSIC} or 321 * {@link #STREAM_ALARM} 322 * @param direction The direction to adjust the volume. One of 323 * {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or 324 * {@link #ADJUST_SAME}. 325 * @param flags One or more flags. 326 * @see #adjustVolume(int, int) 327 * @see #setStreamVolume(int, int, int) 328 */ 329 public void adjustStreamVolume(int streamType, int direction, int flags) { 330 IAudioService service = getService(); 331 try { 332 service.adjustStreamVolume(streamType, direction, flags); 333 } catch (RemoteException e) { 334 Log.e(TAG, "Dead object in adjustStreamVolume", e); 335 } 336 } 337 338 /** 339 * Adjusts the volume of the most relevant stream. For example, if a call is 340 * active, it will have the highest priority regardless of if the in-call 341 * screen is showing. Another example, if music is playing in the background 342 * and a call is not active, the music stream will be adjusted. 343 * 344 * @param direction The direction to adjust the volume. One of 345 * {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or 346 * {@link #ADJUST_SAME}. 347 * @param flags One or more flags. 348 * @see #adjustSuggestedStreamVolume(int, int, int) 349 * @see #adjustStreamVolume(int, int, int) 350 * @see #setStreamVolume(int, int, int) 351 */ 352 public void adjustVolume(int direction, int flags) { 353 IAudioService service = getService(); 354 try { 355 service.adjustVolume(direction, flags); 356 } catch (RemoteException e) { 357 Log.e(TAG, "Dead object in adjustVolume", e); 358 } 359 } 360 361 /** 362 * Adjusts the volume of the most relevant stream, or the given fallback 363 * stream. 364 * 365 * @param direction The direction to adjust the volume. One of 366 * {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or 367 * {@link #ADJUST_SAME}. 368 * @param suggestedStreamType The stream type that will be used if there 369 * isn't a relevant stream. {@link #USE_DEFAULT_STREAM_TYPE} is valid here. 370 * @param flags One or more flags. 371 * @see #adjustVolume(int, int) 372 * @see #adjustStreamVolume(int, int, int) 373 * @see #setStreamVolume(int, int, int) 374 */ 375 public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags) { 376 IAudioService service = getService(); 377 try { 378 service.adjustSuggestedStreamVolume(direction, suggestedStreamType, flags); 379 } catch (RemoteException e) { 380 Log.e(TAG, "Dead object in adjustVolume", e); 381 } 382 } 383 384 /** 385 * Returns the current ringtone mode. 386 * 387 * @return The current ringtone mode, one of {@link #RINGER_MODE_NORMAL}, 388 * {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}. 389 * @see #setRingerMode(int) 390 */ 391 public int getRingerMode() { 392 IAudioService service = getService(); 393 try { 394 return service.getRingerMode(); 395 } catch (RemoteException e) { 396 Log.e(TAG, "Dead object in getRingerMode", e); 397 return RINGER_MODE_NORMAL; 398 } 399 } 400 401 /** 402 * Returns the maximum volume index for a particular stream. 403 * 404 * @param streamType The stream type whose maximum volume index is returned. 405 * @return The maximum valid volume index for the stream. 406 * @see #getStreamVolume(int) 407 */ 408 public int getStreamMaxVolume(int streamType) { 409 IAudioService service = getService(); 410 try { 411 return service.getStreamMaxVolume(streamType); 412 } catch (RemoteException e) { 413 Log.e(TAG, "Dead object in getStreamMaxVolume", e); 414 return 0; 415 } 416 } 417 418 /** 419 * Returns the current volume index for a particular stream. 420 * 421 * @param streamType The stream type whose volume index is returned. 422 * @return The current volume index for the stream. 423 * @see #getStreamMaxVolume(int) 424 * @see #setStreamVolume(int, int, int) 425 */ 426 public int getStreamVolume(int streamType) { 427 IAudioService service = getService(); 428 try { 429 return service.getStreamVolume(streamType); 430 } catch (RemoteException e) { 431 Log.e(TAG, "Dead object in getStreamVolume", e); 432 return 0; 433 } 434 } 435 436 /** 437 * Sets the ringer mode. 438 * <p> 439 * Silent mode will mute the volume and will not vibrate. Vibrate mode will 440 * mute the volume and vibrate. Normal mode will be audible and may vibrate 441 * according to user settings. 442 * 443 * @param ringerMode The ringer mode, one of {@link #RINGER_MODE_NORMAL}, 444 * {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}. 445 * @see #getRingerMode() 446 */ 447 public void setRingerMode(int ringerMode) { 448 IAudioService service = getService(); 449 try { 450 service.setRingerMode(ringerMode); 451 } catch (RemoteException e) { 452 Log.e(TAG, "Dead object in setRingerMode", e); 453 } 454 } 455 456 /** 457 * Sets the volume index for a particular stream. 458 * 459 * @param streamType The stream whose volume index should be set. 460 * @param index The volume index to set. See 461 * {@link #getStreamMaxVolume(int)} for the largest valid value. 462 * @param flags One or more flags. 463 * @see #getStreamMaxVolume(int) 464 * @see #getStreamVolume(int) 465 */ 466 public void setStreamVolume(int streamType, int index, int flags) { 467 IAudioService service = getService(); 468 try { 469 service.setStreamVolume(streamType, index, flags); 470 } catch (RemoteException e) { 471 Log.e(TAG, "Dead object in setStreamVolume", e); 472 } 473 } 474 475 /** 476 * Solo or unsolo a particular stream. All other streams are muted. 477 * <p> 478 * The solo command is protected against client process death: if a process 479 * with an active solo request on a stream dies, all streams that were muted 480 * because of this request will be unmuted automatically. 481 * <p> 482 * The solo requests for a given stream are cumulative: the AudioManager 483 * can receive several solo requests from one or more clients and the stream 484 * will be unsoloed only when the same number of unsolo requests are received. 485 * <p> 486 * For a better user experience, applications MUST unsolo a soloed stream 487 * in onPause() and solo is again in onResume() if appropriate. 488 * 489 * @param streamType The stream to be soloed/unsoloed. 490 * @param state The required solo state: true for solo ON, false for solo OFF 491 */ 492 public void setStreamSolo(int streamType, boolean state) { 493 IAudioService service = getService(); 494 try { 495 service.setStreamSolo(streamType, state, mICallBack); 496 } catch (RemoteException e) { 497 Log.e(TAG, "Dead object in setStreamSolo", e); 498 } 499 } 500 501 /** 502 * Mute or unmute an audio stream. 503 * <p> 504 * The mute command is protected against client process death: if a process 505 * with an active mute request on a stream dies, this stream will be unmuted 506 * automatically. 507 * <p> 508 * The mute requests for a given stream are cumulative: the AudioManager 509 * can receive several mute requests from one or more clients and the stream 510 * will be unmuted only when the same number of unmute requests are received. 511 * <p> 512 * For a better user experience, applications MUST unmute a muted stream 513 * in onPause() and mute is again in onResume() if appropriate. 514 * 515 * @param streamType The stream to be muted/unmuted. 516 * @param state The required mute state: true for mute ON, false for mute OFF 517 */ 518 public void setStreamMute(int streamType, boolean state) { 519 IAudioService service = getService(); 520 try { 521 service.setStreamMute(streamType, state, mICallBack); 522 } catch (RemoteException e) { 523 Log.e(TAG, "Dead object in setStreamMute", e); 524 } 525 } 526 527 /** 528 * Returns whether a particular type should vibrate according to user 529 * settings and the current ringer mode. 530 * <p> 531 * This shouldn't be needed by most clients that use notifications to 532 * vibrate. The notification manager will not vibrate if the policy doesn't 533 * allow it, so the client should always set a vibrate pattern and let the 534 * notification manager control whether or not to actually vibrate. 535 * 536 * @param vibrateType The type of vibrate. One of 537 * {@link #VIBRATE_TYPE_NOTIFICATION} or 538 * {@link #VIBRATE_TYPE_RINGER}. 539 * @return Whether the type should vibrate at the instant this method is 540 * called. 541 * @see #setVibrateSetting(int, int) 542 * @see #getVibrateSetting(int) 543 */ 544 public boolean shouldVibrate(int vibrateType) { 545 IAudioService service = getService(); 546 try { 547 return service.shouldVibrate(vibrateType); 548 } catch (RemoteException e) { 549 Log.e(TAG, "Dead object in shouldVibrate", e); 550 return false; 551 } 552 } 553 554 /** 555 * Returns whether the user's vibrate setting for a vibrate type. 556 * <p> 557 * This shouldn't be needed by most clients that want to vibrate, instead 558 * see {@link #shouldVibrate(int)}. 559 * 560 * @param vibrateType The type of vibrate. One of 561 * {@link #VIBRATE_TYPE_NOTIFICATION} or 562 * {@link #VIBRATE_TYPE_RINGER}. 563 * @return The vibrate setting, one of {@link #VIBRATE_SETTING_ON}, 564 * {@link #VIBRATE_SETTING_OFF}, or 565 * {@link #VIBRATE_SETTING_ONLY_SILENT}. 566 * @see #setVibrateSetting(int, int) 567 * @see #shouldVibrate(int) 568 */ 569 public int getVibrateSetting(int vibrateType) { 570 IAudioService service = getService(); 571 try { 572 return service.getVibrateSetting(vibrateType); 573 } catch (RemoteException e) { 574 Log.e(TAG, "Dead object in getVibrateSetting", e); 575 return VIBRATE_SETTING_OFF; 576 } 577 } 578 579 /** 580 * Sets the setting for when the vibrate type should vibrate. 581 * 582 * @param vibrateType The type of vibrate. One of 583 * {@link #VIBRATE_TYPE_NOTIFICATION} or 584 * {@link #VIBRATE_TYPE_RINGER}. 585 * @param vibrateSetting The vibrate setting, one of 586 * {@link #VIBRATE_SETTING_ON}, 587 * {@link #VIBRATE_SETTING_OFF}, or 588 * {@link #VIBRATE_SETTING_ONLY_SILENT}. 589 * @see #getVibrateSetting(int) 590 * @see #shouldVibrate(int) 591 */ 592 public void setVibrateSetting(int vibrateType, int vibrateSetting) { 593 IAudioService service = getService(); 594 try { 595 service.setVibrateSetting(vibrateType, vibrateSetting); 596 } catch (RemoteException e) { 597 Log.e(TAG, "Dead object in setVibrateSetting", e); 598 } 599 } 600 601 /** 602 * Sets the speakerphone on or off. 603 * 604 * @param on set <var>true</var> to turn on speakerphone; 605 * <var>false</var> to turn it off 606 */ 607 public void setSpeakerphoneOn(boolean on){ 608 setRouting(MODE_IN_CALL, on ? ROUTE_SPEAKER : ROUTE_EARPIECE, ROUTE_ALL); 609 } 610 611 /** 612 * Checks whether the speakerphone is on or off. 613 * 614 * @return true if speakerphone is on, false if it's off 615 */ 616 public boolean isSpeakerphoneOn() { 617 return (getRouting(MODE_IN_CALL) & ROUTE_SPEAKER) == 0 ? false : true; 618 } 619 620 /** 621 * Sets audio routing to the Bluetooth headset on or off. 622 * 623 * @param on set <var>true</var> to route SCO (voice) audio to/from Bluetooth 624 * headset; <var>false</var> to route audio to/from phone earpiece 625 */ 626 public void setBluetoothScoOn(boolean on){ 627 // Don't disable A2DP when turning off SCO. 628 // A2DP does not affect in-call routing. 629 setRouting(MODE_RINGTONE, 630 on ? ROUTE_BLUETOOTH_SCO: ROUTE_SPEAKER, ROUTE_ALL & ~ROUTE_BLUETOOTH_A2DP); 631 setRouting(MODE_NORMAL, 632 on ? ROUTE_BLUETOOTH_SCO: ROUTE_SPEAKER, ROUTE_ALL & ~ROUTE_BLUETOOTH_A2DP); 633 setRouting(MODE_IN_CALL, 634 on ? ROUTE_BLUETOOTH_SCO: ROUTE_EARPIECE, ROUTE_ALL); 635 } 636 637 /** 638 * Checks whether audio routing to the Bluetooth headset is on or off. 639 * 640 * @return true if SCO audio is being routed to/from Bluetooth headset; 641 * false if otherwise 642 */ 643 public boolean isBluetoothScoOn() { 644 return (getRouting(MODE_IN_CALL) & ROUTE_BLUETOOTH_SCO) == 0 ? false : true; 645 } 646 647 /** 648 * Sets A2DP audio routing to the Bluetooth headset on or off. 649 * 650 * @param on set <var>true</var> to route A2DP audio to/from Bluetooth 651 * headset; <var>false</var> disable A2DP audio 652 */ 653 public void setBluetoothA2dpOn(boolean on){ 654 // the audio flinger chooses A2DP as a higher priority, 655 // so there is no need to disable other routes. 656 setRouting(MODE_RINGTONE, 657 on ? ROUTE_BLUETOOTH_A2DP: 0, ROUTE_BLUETOOTH_A2DP); 658 setRouting(MODE_NORMAL, 659 on ? ROUTE_BLUETOOTH_A2DP: 0, ROUTE_BLUETOOTH_A2DP); 660 } 661 662 /** 663 * Checks whether A2DP audio routing to the Bluetooth headset is on or off. 664 * 665 * @return true if A2DP audio is being routed to/from Bluetooth headset; 666 * false if otherwise 667 */ 668 public boolean isBluetoothA2dpOn() { 669 return (getRouting(MODE_NORMAL) & ROUTE_BLUETOOTH_A2DP) == 0 ? false : true; 670 } 671 672 /** 673 * Sets the microphone mute on or off. 674 * 675 * @param on set <var>true</var> to mute the microphone; 676 * <var>false</var> to turn mute off 677 */ 678 public void setMicrophoneMute(boolean on){ 679 IAudioService service = getService(); 680 try { 681 service.setMicrophoneMute(on); 682 } catch (RemoteException e) { 683 Log.e(TAG, "Dead object in setMicrophoneMute", e); 684 } 685 } 686 687 /** 688 * Checks whether the microphone mute is on or off. 689 * 690 * @return true if microphone is muted, false if it's not 691 */ 692 public boolean isMicrophoneMute() { 693 IAudioService service = getService(); 694 try { 695 return service.isMicrophoneMute(); 696 } catch (RemoteException e) { 697 Log.e(TAG, "Dead object in isMicrophoneMute", e); 698 return false; 699 } 700 } 701 702 /** 703 * Sets the audio mode. 704 * 705 * @param mode the requested audio mode (NORMAL, RINGTONE, or IN_CALL). 706 * Informs the HAL about the current audio state so that 707 * it can route the audio appropriately. 708 */ 709 public void setMode(int mode) { 710 IAudioService service = getService(); 711 try { 712 service.setMode(mode); 713 } catch (RemoteException e) { 714 Log.e(TAG, "Dead object in setMode", e); 715 } 716 } 717 718 /** 719 * Returns the current audio mode. 720 * 721 * @return the current audio mode (NORMAL, RINGTONE, or IN_CALL). 722 * Returns the current current audio state from the HAL. 723 */ 724 public int getMode() { 725 IAudioService service = getService(); 726 try { 727 return service.getMode(); 728 } catch (RemoteException e) { 729 Log.e(TAG, "Dead object in getMode", e); 730 return MODE_INVALID; 731 } 732 } 733 734 /* modes for setMode/getMode/setRoute/getRoute */ 735 /** 736 * Audio harware modes. 737 */ 738 /** 739 * Invalid audio mode. 740 */ 741 public static final int MODE_INVALID = AudioSystem.MODE_INVALID; 742 /** 743 * Current audio mode. Used to apply audio routing to current mode. 744 */ 745 public static final int MODE_CURRENT = AudioSystem.MODE_CURRENT; 746 /** 747 * Normal audio mode: not ringing and no call established. 748 */ 749 public static final int MODE_NORMAL = AudioSystem.MODE_NORMAL; 750 /** 751 * Ringing audio mode. An incoming is being signaled. 752 */ 753 public static final int MODE_RINGTONE = AudioSystem.MODE_RINGTONE; 754 /** 755 * In call audio mode. A call is established. 756 */ 757 public static final int MODE_IN_CALL = AudioSystem.MODE_IN_CALL; 758 759 /* Routing bits for setRouting/getRouting API */ 760 /** 761 * Routing audio output to earpiece 762 */ 763 public static final int ROUTE_EARPIECE = AudioSystem.ROUTE_EARPIECE; 764 /** 765 * Routing audio output to spaker 766 */ 767 public static final int ROUTE_SPEAKER = AudioSystem.ROUTE_SPEAKER; 768 /** 769 * @deprecated use {@link #ROUTE_BLUETOOTH_SCO} 770 */ 771 @Deprecated public static final int ROUTE_BLUETOOTH = AudioSystem.ROUTE_BLUETOOTH_SCO; 772 /** 773 * Routing audio output to bluetooth SCO 774 */ 775 public static final int ROUTE_BLUETOOTH_SCO = AudioSystem.ROUTE_BLUETOOTH_SCO; 776 /** 777 * Routing audio output to headset 778 */ 779 public static final int ROUTE_HEADSET = AudioSystem.ROUTE_HEADSET; 780 /** 781 * Routing audio output to bluetooth A2DP 782 */ 783 public static final int ROUTE_BLUETOOTH_A2DP = AudioSystem.ROUTE_BLUETOOTH_A2DP; 784 /** 785 * Used for mask parameter of {@link #setRouting(int,int,int)}. 786 */ 787 public static final int ROUTE_ALL = AudioSystem.ROUTE_ALL; 788 789 /** 790 * Sets the audio routing for a specified mode 791 * 792 * @param mode audio mode to change route. E.g., MODE_RINGTONE. 793 * @param routes bit vector of routes requested, created from one or 794 * more of ROUTE_xxx types. Set bits indicate that route should be on 795 * @param mask bit vector of routes to change, created from one or more of 796 * ROUTE_xxx types. Unset bits indicate the route should be left unchanged 797 */ 798 public void setRouting(int mode, int routes, int mask) { 799 IAudioService service = getService(); 800 try { 801 service.setRouting(mode, routes, mask); 802 } catch (RemoteException e) { 803 Log.e(TAG, "Dead object in setRouting", e); 804 } 805 } 806 807 /** 808 * Returns the current audio routing bit vector for a specified mode. 809 * 810 * @param mode audio mode to get route (e.g., MODE_RINGTONE) 811 * @return an audio route bit vector that can be compared with ROUTE_xxx 812 * bits 813 */ 814 public int getRouting(int mode) { 815 IAudioService service = getService(); 816 try { 817 return service.getRouting(mode); 818 } catch (RemoteException e) { 819 Log.e(TAG, "Dead object in getRouting", e); 820 return -1; 821 } 822 } 823 824 /** 825 * Checks whether any music is active. 826 * 827 * @return true if any music tracks are active. 828 */ 829 public boolean isMusicActive() { 830 IAudioService service = getService(); 831 try { 832 return service.isMusicActive(); 833 } catch (RemoteException e) { 834 Log.e(TAG, "Dead object in isMusicActive", e); 835 return false; 836 } 837 } 838 839 /* 840 * Sets a generic audio configuration parameter. The use of these parameters 841 * are platform dependant, see libaudio 842 * 843 * ** Temporary interface - DO NOT USE 844 * 845 * TODO: Replace with a more generic key:value get/set mechanism 846 * 847 * param key name of parameter to set. Must not be null. 848 * param value value of parameter. Must not be null. 849 */ 850 /** 851 * @hide 852 */ 853 public void setParameter(String key, String value) { 854 IAudioService service = getService(); 855 try { 856 service.setParameter(key, value); 857 } catch (RemoteException e) { 858 Log.e(TAG, "Dead object in setParameter", e); 859 } 860 } 861 862 /* Sound effect identifiers */ 863 /** 864 * Keyboard and direction pad click sound 865 * @see #playSoundEffect(int) 866 */ 867 public static final int FX_KEY_CLICK = 0; 868 /** 869 * Focuse has moved up 870 * @see #playSoundEffect(int) 871 */ 872 public static final int FX_FOCUS_NAVIGATION_UP = 1; 873 /** 874 * Focuse has moved down 875 * @see #playSoundEffect(int) 876 */ 877 public static final int FX_FOCUS_NAVIGATION_DOWN = 2; 878 /** 879 * Focuse has moved left 880 * @see #playSoundEffect(int) 881 */ 882 public static final int FX_FOCUS_NAVIGATION_LEFT = 3; 883 /** 884 * Focuse has moved right 885 * @see #playSoundEffect(int) 886 */ 887 public static final int FX_FOCUS_NAVIGATION_RIGHT = 4; 888 /** 889 * @hide Number of sound effects 890 */ 891 public static final int NUM_SOUND_EFFECTS = 5; 892 893 /** 894 * Plays a sound effect (Key clicks, lid open/close...) 895 * @param effectType The type of sound effect. One of 896 * {@link #FX_KEY_CLICK}, 897 * {@link #FX_FOCUS_NAVIGATION_UP}, 898 * {@link #FX_FOCUS_NAVIGATION_DOWN}, 899 * {@link #FX_FOCUS_NAVIGATION_LEFT}, 900 * {@link #FX_FOCUS_NAVIGATION_RIGHT}, 901 */ 902 public void playSoundEffect(int effectType) { 903 if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) { 904 return; 905 } 906 907 if (!querySoundEffectsEnabled()) { 908 return; 909 } 910 911 IAudioService service = getService(); 912 try { 913 service.playSoundEffect(effectType); 914 } catch (RemoteException e) { 915 Log.e(TAG, "Dead object in playSoundEffect"+e); 916 } 917 } 918 919 /** 920 * Settings has an in memory cache, so this is fast. 921 */ 922 private boolean querySoundEffectsEnabled() { 923 return Settings.System.getInt(mContext.getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0; 924 } 925 926 927 /** 928 * Load Sound effects. 929 * This method must be called when sound effects are enabled. 930 */ 931 public void loadSoundEffects() { 932 IAudioService service = getService(); 933 try { 934 service.loadSoundEffects(); 935 } catch (RemoteException e) { 936 Log.e(TAG, "Dead object in loadSoundEffects"+e); 937 } 938 } 939 940 /** 941 * Unload Sound effects. 942 * This method can be called to free some memory when 943 * sound effects are disabled. 944 */ 945 public void unloadSoundEffects() { 946 IAudioService service = getService(); 947 try { 948 service.unloadSoundEffects(); 949 } catch (RemoteException e) { 950 Log.e(TAG, "Dead object in unloadSoundEffects"+e); 951 } 952 } 953 954 /** 955 * {@hide} 956 */ 957 private IBinder mICallBack = new Binder(); 958} 959