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