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