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