AudioManager.java revision 9272b4b4a44fe1f33e3030810618194f817caaec
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        7,  // STREAM_SYSTEM
160        5,  // STREAM_RING
161        11, // STREAM_MUSIC
162        6,  // STREAM_ALARM
163        5,  // STREAM_NOTIFICATION
164        7,  // STREAM_BLUETOOTH_SCO
165        7,  // 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     * <p>
349     * This method should only be used by applications that replace the platform-wide
350     * management of audio settings or the main telephony application.
351     *
352     * @param streamType The stream type to adjust. One of {@link #STREAM_VOICE_CALL},
353     * {@link #STREAM_SYSTEM}, {@link #STREAM_RING}, {@link #STREAM_MUSIC} or
354     * {@link #STREAM_ALARM}
355     * @param direction The direction to adjust the volume. One of
356     *            {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or
357     *            {@link #ADJUST_SAME}.
358     * @param flags One or more flags.
359     * @see #adjustVolume(int, int)
360     * @see #setStreamVolume(int, int, int)
361     */
362    public void adjustStreamVolume(int streamType, int direction, int flags) {
363        IAudioService service = getService();
364        try {
365            service.adjustStreamVolume(streamType, direction, flags);
366        } catch (RemoteException e) {
367            Log.e(TAG, "Dead object in adjustStreamVolume", e);
368        }
369    }
370
371    /**
372     * Adjusts the volume of the most relevant stream. For example, if a call is
373     * active, it will have the highest priority regardless of if the in-call
374     * screen is showing. Another example, if music is playing in the background
375     * and a call is not active, the music stream will be adjusted.
376     * <p>
377     * This method should only be used by applications that replace the platform-wide
378     * management of audio settings or the main telephony application.
379     *
380     * @param direction The direction to adjust the volume. One of
381     *            {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or
382     *            {@link #ADJUST_SAME}.
383     * @param flags One or more flags.
384     * @see #adjustSuggestedStreamVolume(int, int, int)
385     * @see #adjustStreamVolume(int, int, int)
386     * @see #setStreamVolume(int, int, int)
387     */
388    public void adjustVolume(int direction, int flags) {
389        IAudioService service = getService();
390        try {
391            service.adjustVolume(direction, flags);
392        } catch (RemoteException e) {
393            Log.e(TAG, "Dead object in adjustVolume", e);
394        }
395    }
396
397    /**
398     * Adjusts the volume of the most relevant stream, or the given fallback
399     * stream.
400     * <p>
401     * This method should only be used by applications that replace the platform-wide
402     * management of audio settings or the main telephony application.
403     *
404     * @param direction The direction to adjust the volume. One of
405     *            {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or
406     *            {@link #ADJUST_SAME}.
407     * @param suggestedStreamType The stream type that will be used if there
408     *            isn't a relevant stream. {@link #USE_DEFAULT_STREAM_TYPE} is valid here.
409     * @param flags One or more flags.
410     * @see #adjustVolume(int, int)
411     * @see #adjustStreamVolume(int, int, int)
412     * @see #setStreamVolume(int, int, int)
413     */
414    public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags) {
415        IAudioService service = getService();
416        try {
417            service.adjustSuggestedStreamVolume(direction, suggestedStreamType, flags);
418        } catch (RemoteException e) {
419            Log.e(TAG, "Dead object in adjustVolume", e);
420        }
421    }
422
423    /**
424     * Returns the current ringtone mode.
425     *
426     * @return The current ringtone mode, one of {@link #RINGER_MODE_NORMAL},
427     *         {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}.
428     * @see #setRingerMode(int)
429     */
430    public int getRingerMode() {
431        IAudioService service = getService();
432        try {
433            return service.getRingerMode();
434        } catch (RemoteException e) {
435            Log.e(TAG, "Dead object in getRingerMode", e);
436            return RINGER_MODE_NORMAL;
437        }
438    }
439
440    /**
441     * Returns the maximum volume index for a particular stream.
442     *
443     * @param streamType The stream type whose maximum volume index is returned.
444     * @return The maximum valid volume index for the stream.
445     * @see #getStreamVolume(int)
446     */
447    public int getStreamMaxVolume(int streamType) {
448        IAudioService service = getService();
449        try {
450            return service.getStreamMaxVolume(streamType);
451        } catch (RemoteException e) {
452            Log.e(TAG, "Dead object in getStreamMaxVolume", e);
453            return 0;
454        }
455    }
456
457    /**
458     * Returns the current volume index for a particular stream.
459     *
460     * @param streamType The stream type whose volume index is returned.
461     * @return The current volume index for the stream.
462     * @see #getStreamMaxVolume(int)
463     * @see #setStreamVolume(int, int, int)
464     */
465    public int getStreamVolume(int streamType) {
466        IAudioService service = getService();
467        try {
468            return service.getStreamVolume(streamType);
469        } catch (RemoteException e) {
470            Log.e(TAG, "Dead object in getStreamVolume", e);
471            return 0;
472        }
473    }
474
475    /**
476     * Sets the ringer mode.
477     * <p>
478     * Silent mode will mute the volume and will not vibrate. Vibrate mode will
479     * mute the volume and vibrate. Normal mode will be audible and may vibrate
480     * according to user settings.
481     *
482     * @param ringerMode The ringer mode, one of {@link #RINGER_MODE_NORMAL},
483     *            {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}.
484     * @see #getRingerMode()
485     */
486    public void setRingerMode(int ringerMode) {
487        IAudioService service = getService();
488        try {
489            service.setRingerMode(ringerMode);
490        } catch (RemoteException e) {
491            Log.e(TAG, "Dead object in setRingerMode", e);
492        }
493    }
494
495    /**
496     * Sets the volume index for a particular stream.
497     *
498     * @param streamType The stream whose volume index should be set.
499     * @param index The volume index to set. See
500     *            {@link #getStreamMaxVolume(int)} for the largest valid value.
501     * @param flags One or more flags.
502     * @see #getStreamMaxVolume(int)
503     * @see #getStreamVolume(int)
504     */
505    public void setStreamVolume(int streamType, int index, int flags) {
506        IAudioService service = getService();
507        try {
508            service.setStreamVolume(streamType, index, flags);
509        } catch (RemoteException e) {
510            Log.e(TAG, "Dead object in setStreamVolume", e);
511        }
512    }
513
514    /**
515     * Solo or unsolo a particular stream. All other streams are muted.
516     * <p>
517     * The solo command is protected against client process death: if a process
518     * with an active solo request on a stream dies, all streams that were muted
519     * because of this request will be unmuted automatically.
520     * <p>
521     * The solo requests for a given stream are cumulative: the AudioManager
522     * can receive several solo requests from one or more clients and the stream
523     * will be unsoloed only when the same number of unsolo requests are received.
524     * <p>
525     * For a better user experience, applications MUST unsolo a soloed stream
526     * in onPause() and solo is again in onResume() if appropriate.
527     *
528     * @param streamType The stream to be soloed/unsoloed.
529     * @param state The required solo state: true for solo ON, false for solo OFF
530     */
531    public void setStreamSolo(int streamType, boolean state) {
532        IAudioService service = getService();
533        try {
534            service.setStreamSolo(streamType, state, mICallBack);
535        } catch (RemoteException e) {
536            Log.e(TAG, "Dead object in setStreamSolo", e);
537        }
538    }
539
540    /**
541     * Mute or unmute an audio stream.
542     * <p>
543     * The mute command is protected against client process death: if a process
544     * with an active mute request on a stream dies, this stream will be unmuted
545     * automatically.
546     * <p>
547     * The mute requests for a given stream are cumulative: the AudioManager
548     * can receive several mute requests from one or more clients and the stream
549     * will be unmuted only when the same number of unmute requests are received.
550     * <p>
551     * For a better user experience, applications MUST unmute a muted stream
552     * in onPause() and mute is again in onResume() if appropriate.
553     * <p>
554     * This method should only be used by applications that replace the platform-wide
555     * management of audio settings or the main telephony application.
556     *
557     * @param streamType The stream to be muted/unmuted.
558     * @param state The required mute state: true for mute ON, false for mute OFF
559     */
560    public void setStreamMute(int streamType, boolean state) {
561        IAudioService service = getService();
562        try {
563            service.setStreamMute(streamType, state, mICallBack);
564        } catch (RemoteException e) {
565            Log.e(TAG, "Dead object in setStreamMute", e);
566        }
567    }
568
569    /**
570     * Returns whether a particular type should vibrate according to user
571     * settings and the current ringer mode.
572     * <p>
573     * This shouldn't be needed by most clients that use notifications to
574     * vibrate. The notification manager will not vibrate if the policy doesn't
575     * allow it, so the client should always set a vibrate pattern and let the
576     * notification manager control whether or not to actually vibrate.
577     *
578     * @param vibrateType The type of vibrate. One of
579     *            {@link #VIBRATE_TYPE_NOTIFICATION} or
580     *            {@link #VIBRATE_TYPE_RINGER}.
581     * @return Whether the type should vibrate at the instant this method is
582     *         called.
583     * @see #setVibrateSetting(int, int)
584     * @see #getVibrateSetting(int)
585     */
586    public boolean shouldVibrate(int vibrateType) {
587        IAudioService service = getService();
588        try {
589            return service.shouldVibrate(vibrateType);
590        } catch (RemoteException e) {
591            Log.e(TAG, "Dead object in shouldVibrate", e);
592            return false;
593        }
594    }
595
596    /**
597     * Returns whether the user's vibrate setting for a vibrate type.
598     * <p>
599     * This shouldn't be needed by most clients that want to vibrate, instead
600     * see {@link #shouldVibrate(int)}.
601     *
602     * @param vibrateType The type of vibrate. One of
603     *            {@link #VIBRATE_TYPE_NOTIFICATION} or
604     *            {@link #VIBRATE_TYPE_RINGER}.
605     * @return The vibrate setting, one of {@link #VIBRATE_SETTING_ON},
606     *         {@link #VIBRATE_SETTING_OFF}, or
607     *         {@link #VIBRATE_SETTING_ONLY_SILENT}.
608     * @see #setVibrateSetting(int, int)
609     * @see #shouldVibrate(int)
610     */
611    public int getVibrateSetting(int vibrateType) {
612        IAudioService service = getService();
613        try {
614            return service.getVibrateSetting(vibrateType);
615        } catch (RemoteException e) {
616            Log.e(TAG, "Dead object in getVibrateSetting", e);
617            return VIBRATE_SETTING_OFF;
618        }
619    }
620
621    /**
622     * Sets the setting for when the vibrate type should vibrate.
623     * <p>
624     * This method should only be used by applications that replace the platform-wide
625     * management of audio settings or the main telephony application.
626     *
627     * @param vibrateType The type of vibrate. One of
628     *            {@link #VIBRATE_TYPE_NOTIFICATION} or
629     *            {@link #VIBRATE_TYPE_RINGER}.
630     * @param vibrateSetting The vibrate setting, one of
631     *            {@link #VIBRATE_SETTING_ON},
632     *            {@link #VIBRATE_SETTING_OFF}, or
633     *            {@link #VIBRATE_SETTING_ONLY_SILENT}.
634     * @see #getVibrateSetting(int)
635     * @see #shouldVibrate(int)
636     */
637    public void setVibrateSetting(int vibrateType, int vibrateSetting) {
638        IAudioService service = getService();
639        try {
640            service.setVibrateSetting(vibrateType, vibrateSetting);
641        } catch (RemoteException e) {
642            Log.e(TAG, "Dead object in setVibrateSetting", e);
643        }
644    }
645
646    /**
647     * Sets the speakerphone on or off.
648     * <p>
649     * This method should only be used by applications that replace the platform-wide
650     * management of audio settings or the main telephony application.
651     *
652     * @param on set <var>true</var> to turn on speakerphone;
653     *           <var>false</var> to turn it off
654     */
655    public void setSpeakerphoneOn(boolean on){
656        IAudioService service = getService();
657        try {
658            service.setSpeakerphoneOn(on);
659        } catch (RemoteException e) {
660            Log.e(TAG, "Dead object in setSpeakerphoneOn", e);
661        }
662    }
663
664    /**
665     * Checks whether the speakerphone is on or off.
666     *
667     * @return true if speakerphone is on, false if it's off
668     */
669    public boolean isSpeakerphoneOn() {
670        IAudioService service = getService();
671        try {
672            return service.isSpeakerphoneOn();
673        } catch (RemoteException e) {
674            Log.e(TAG, "Dead object in isSpeakerphoneOn", e);
675            return false;
676        }
677     }
678
679    /**
680     * Request use of Bluetooth SCO headset for communications.
681     * <p>
682     * This method should only be used by applications that replace the platform-wide
683     * management of audio settings or the main telephony application.
684     *
685     * @param on set <var>true</var> to use bluetooth SCO for communications;
686     *               <var>false</var> to not use bluetooth SCO for communications
687     */
688    public void setBluetoothScoOn(boolean on){
689        IAudioService service = getService();
690        try {
691            service.setBluetoothScoOn(on);
692        } catch (RemoteException e) {
693            Log.e(TAG, "Dead object in setBluetoothScoOn", e);
694        }
695    }
696
697    /**
698     * Checks whether communications use Bluetooth SCO.
699     *
700     * @return true if SCO is used for communications;
701     *         false if otherwise
702     */
703    public boolean isBluetoothScoOn() {
704        IAudioService service = getService();
705        try {
706            return service.isBluetoothScoOn();
707        } catch (RemoteException e) {
708            Log.e(TAG, "Dead object in isBluetoothScoOn", e);
709            return false;
710        }
711    }
712
713    /**
714     * @param on set <var>true</var> to route A2DP audio to/from Bluetooth
715     *           headset; <var>false</var> disable A2DP audio
716     * @deprecated Do not use.
717     */
718    @Deprecated public void setBluetoothA2dpOn(boolean on){
719    }
720
721    /**
722     * Checks whether A2DP audio routing to the Bluetooth headset is on or off.
723     *
724     * @return true if A2DP audio is being routed to/from Bluetooth headset;
725     *         false if otherwise
726     */
727    public boolean isBluetoothA2dpOn() {
728        if (AudioSystem.getDeviceConnectionState(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP,"")
729            == AudioSystem.DEVICE_STATE_UNAVAILABLE) {
730            return false;
731        } else {
732            return true;
733        }
734    }
735
736    /**
737     * Sets audio routing to the wired headset on or off.
738     *
739     * @param on set <var>true</var> to route audio to/from wired
740     *           headset; <var>false</var> disable wired headset audio
741     * @deprecated Do not use.
742     */
743    @Deprecated public void setWiredHeadsetOn(boolean on){
744    }
745
746    /**
747     * Checks whether audio routing to the wired headset is on or off.
748     *
749     * @return true if audio is being routed to/from wired headset;
750     *         false if otherwise
751     */
752    public boolean isWiredHeadsetOn() {
753        if (AudioSystem.getDeviceConnectionState(AudioSystem.DEVICE_OUT_WIRED_HEADSET,"")
754                == AudioSystem.DEVICE_STATE_UNAVAILABLE) {
755            return false;
756        } else {
757            return true;
758        }
759    }
760
761    /**
762     * Sets the microphone mute on or off.
763     * <p>
764     * This method should only be used by applications that replace the platform-wide
765     * management of audio settings or the main telephony application.
766     *
767     * @param on set <var>true</var> to mute the microphone;
768     *           <var>false</var> to turn mute off
769     */
770    public void setMicrophoneMute(boolean on){
771        AudioSystem.muteMicrophone(on);
772    }
773
774    /**
775     * Checks whether the microphone mute is on or off.
776     *
777     * @return true if microphone is muted, false if it's not
778     */
779    public boolean isMicrophoneMute() {
780        return AudioSystem.isMicrophoneMuted();
781    }
782
783    /**
784     * Sets the audio mode.
785     * <p>
786     * The audio mode encompasses audio routing AND the behavior of
787     * the telephony layer. Therefore this method should only be used by applications that
788     * replace the platform-wide management of audio settings or the main telephony application.
789     * In particular, the {@link #MODE_IN_CALL} mode should only be used by the telephony
790     * application when it places a phone call, as it will cause signals from the radio layer
791     * to feed the platform mixer.
792     *
793     * @param mode  the requested audio mode (NORMAL, RINGTONE, or IN_CALL).
794     *              Informs the HAL about the current audio state so that
795     *              it can route the audio appropriately.
796     */
797    public void setMode(int mode) {
798        IAudioService service = getService();
799        try {
800            service.setMode(mode, mICallBack);
801        } catch (RemoteException e) {
802            Log.e(TAG, "Dead object in setMode", e);
803        }
804    }
805
806    /**
807     * Returns the current audio mode.
808     *
809     * @return      the current audio mode (NORMAL, RINGTONE, or IN_CALL).
810     *              Returns the current current audio state from the HAL.
811     */
812    public int getMode() {
813        IAudioService service = getService();
814        try {
815            return service.getMode();
816        } catch (RemoteException e) {
817            Log.e(TAG, "Dead object in getMode", e);
818            return MODE_INVALID;
819        }
820    }
821
822    /* modes for setMode/getMode/setRoute/getRoute */
823    /**
824     * Audio harware modes.
825     */
826    /**
827     * Invalid audio mode.
828     */
829    public static final int MODE_INVALID            = AudioSystem.MODE_INVALID;
830    /**
831     * Current audio mode. Used to apply audio routing to current mode.
832     */
833    public static final int MODE_CURRENT            = AudioSystem.MODE_CURRENT;
834    /**
835     * Normal audio mode: not ringing and no call established.
836     */
837    public static final int MODE_NORMAL             = AudioSystem.MODE_NORMAL;
838    /**
839     * Ringing audio mode. An incoming is being signaled.
840     */
841    public static final int MODE_RINGTONE           = AudioSystem.MODE_RINGTONE;
842    /**
843     * In call audio mode. A call is established.
844     */
845    public static final int MODE_IN_CALL            = AudioSystem.MODE_IN_CALL;
846
847    /* Routing bits for setRouting/getRouting API */
848    /**
849     * Routing audio output to earpiece
850     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
851     * setBluetoothScoOn() methods instead.
852     */
853    @Deprecated public static final int ROUTE_EARPIECE          = AudioSystem.ROUTE_EARPIECE;
854    /**
855     * Routing audio output to speaker
856     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
857     * setBluetoothScoOn() methods instead.
858     */
859    @Deprecated public static final int ROUTE_SPEAKER           = AudioSystem.ROUTE_SPEAKER;
860    /**
861     * @deprecated use {@link #ROUTE_BLUETOOTH_SCO}
862     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
863     * setBluetoothScoOn() methods instead.
864     */
865    @Deprecated public static final int ROUTE_BLUETOOTH = AudioSystem.ROUTE_BLUETOOTH_SCO;
866    /**
867     * Routing audio output to bluetooth SCO
868     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
869     * setBluetoothScoOn() methods instead.
870     */
871    @Deprecated public static final int ROUTE_BLUETOOTH_SCO     = AudioSystem.ROUTE_BLUETOOTH_SCO;
872    /**
873     * Routing audio output to headset
874     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
875     * setBluetoothScoOn() methods instead.
876     */
877    @Deprecated public static final int ROUTE_HEADSET           = AudioSystem.ROUTE_HEADSET;
878    /**
879     * Routing audio output to bluetooth A2DP
880     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
881     * setBluetoothScoOn() methods instead.
882     */
883    @Deprecated public static final int ROUTE_BLUETOOTH_A2DP    = AudioSystem.ROUTE_BLUETOOTH_A2DP;
884    /**
885     * Used for mask parameter of {@link #setRouting(int,int,int)}.
886     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
887     * setBluetoothScoOn() methods instead.
888     */
889    @Deprecated public static final int ROUTE_ALL               = AudioSystem.ROUTE_ALL;
890
891    /**
892     * Sets the audio routing for a specified mode
893     *
894     * @param mode   audio mode to change route. E.g., MODE_RINGTONE.
895     * @param routes bit vector of routes requested, created from one or
896     *               more of ROUTE_xxx types. Set bits indicate that route should be on
897     * @param mask   bit vector of routes to change, created from one or more of
898     * ROUTE_xxx types. Unset bits indicate the route should be left unchanged
899     *
900     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
901     * setBluetoothScoOn() methods instead.
902     */
903    @Deprecated
904    public void setRouting(int mode, int routes, int mask) {
905    }
906
907    /**
908     * Returns the current audio routing bit vector for a specified mode.
909     *
910     * @param mode audio mode to get route (e.g., MODE_RINGTONE)
911     * @return an audio route bit vector that can be compared with ROUTE_xxx
912     * bits
913     * @deprecated   Do not query audio routing directly, use isSpeakerphoneOn(),
914     * isBluetoothScoOn(), isBluetoothA2dpOn() and isWiredHeadsetOn() methods instead.
915     */
916    @Deprecated
917    public int getRouting(int mode) {
918        return -1;
919    }
920
921    /**
922     * Checks whether any music is active.
923     *
924     * @return true if any music tracks are active.
925     */
926    public boolean isMusicActive() {
927        return AudioSystem.isMusicActive();
928    }
929
930    /*
931     * Sets a generic audio configuration parameter. The use of these parameters
932     * are platform dependant, see libaudio
933     *
934     * ** Temporary interface - DO NOT USE
935     *
936     * TODO: Replace with a more generic key:value get/set mechanism
937     *
938     * param key   name of parameter to set. Must not be null.
939     * param value value of parameter. Must not be null.
940     */
941    /**
942     * @hide
943     * @deprecated Use {@link #setPrameters(String)} instead
944     */
945    @Deprecated public void setParameter(String key, String value) {
946        setParameters(key+"="+value);
947    }
948
949    /**
950     * Sets a variable number of parameter values to audio hardware.
951     *
952     * @param keyValuePairs list of parameters key value pairs in the form:
953     *    key1=value1;key2=value2;...
954     *
955     */
956    public void setParameters(String keyValuePairs) {
957        AudioSystem.setParameters(keyValuePairs);
958    }
959
960    /**
961     * Sets a varaible number of parameter values to audio hardware.
962     *
963     * @param keys list of parameters
964     * @return list of parameters key value pairs in the form:
965     *    key1=value1;key2=value2;...
966     */
967    public String getParameters(String keys) {
968        return AudioSystem.getParameters(keys);
969    }
970
971    /* Sound effect identifiers */
972    /**
973     * Keyboard and direction pad click sound
974     * @see #playSoundEffect(int)
975     */
976    public static final int FX_KEY_CLICK = 0;
977    /**
978     * Focus has moved up
979     * @see #playSoundEffect(int)
980     */
981    public static final int FX_FOCUS_NAVIGATION_UP = 1;
982    /**
983     * Focus has moved down
984     * @see #playSoundEffect(int)
985     */
986    public static final int FX_FOCUS_NAVIGATION_DOWN = 2;
987    /**
988     * Focus has moved left
989     * @see #playSoundEffect(int)
990     */
991    public static final int FX_FOCUS_NAVIGATION_LEFT = 3;
992    /**
993     * Focus has moved right
994     * @see #playSoundEffect(int)
995     */
996    public static final int FX_FOCUS_NAVIGATION_RIGHT = 4;
997    /**
998     * IME standard keypress sound
999     * @see #playSoundEffect(int)
1000     */
1001    public static final int FX_KEYPRESS_STANDARD = 5;
1002    /**
1003     * IME spacebar keypress sound
1004     * @see #playSoundEffect(int)
1005     */
1006    public static final int FX_KEYPRESS_SPACEBAR = 6;
1007    /**
1008     * IME delete keypress sound
1009     * @see #playSoundEffect(int)
1010     */
1011    public static final int FX_KEYPRESS_DELETE = 7;
1012    /**
1013     * IME return_keypress sound
1014     * @see #playSoundEffect(int)
1015     */
1016    public static final int FX_KEYPRESS_RETURN = 8;
1017    /**
1018     * @hide Number of sound effects
1019     */
1020    public static final int NUM_SOUND_EFFECTS = 9;
1021
1022    /**
1023     * Plays a sound effect (Key clicks, lid open/close...)
1024     * @param effectType The type of sound effect. One of
1025     *            {@link #FX_KEY_CLICK},
1026     *            {@link #FX_FOCUS_NAVIGATION_UP},
1027     *            {@link #FX_FOCUS_NAVIGATION_DOWN},
1028     *            {@link #FX_FOCUS_NAVIGATION_LEFT},
1029     *            {@link #FX_FOCUS_NAVIGATION_RIGHT},
1030     *            {@link #FX_KEYPRESS_STANDARD},
1031     *            {@link #FX_KEYPRESS_SPACEBAR},
1032     *            {@link #FX_KEYPRESS_DELETE},
1033     *            {@link #FX_KEYPRESS_RETURN},
1034     * NOTE: This version uses the UI settings to determine
1035     * whether sounds are heard or not.
1036     */
1037    public void  playSoundEffect(int effectType) {
1038        if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) {
1039            return;
1040        }
1041
1042        if (!querySoundEffectsEnabled()) {
1043            return;
1044        }
1045
1046        IAudioService service = getService();
1047        try {
1048            service.playSoundEffect(effectType);
1049        } catch (RemoteException e) {
1050            Log.e(TAG, "Dead object in playSoundEffect"+e);
1051        }
1052    }
1053
1054    /**
1055     * Plays a sound effect (Key clicks, lid open/close...)
1056     * @param effectType The type of sound effect. One of
1057     *            {@link #FX_KEY_CLICK},
1058     *            {@link #FX_FOCUS_NAVIGATION_UP},
1059     *            {@link #FX_FOCUS_NAVIGATION_DOWN},
1060     *            {@link #FX_FOCUS_NAVIGATION_LEFT},
1061     *            {@link #FX_FOCUS_NAVIGATION_RIGHT},
1062     *            {@link #FX_KEYPRESS_STANDARD},
1063     *            {@link #FX_KEYPRESS_SPACEBAR},
1064     *            {@link #FX_KEYPRESS_DELETE},
1065     *            {@link #FX_KEYPRESS_RETURN},
1066     * @param volume Sound effect volume.
1067     * The volume value is a raw scalar so UI controls should be scaled logarithmically.
1068     * If a volume of -1 is specified, the AudioManager.STREAM_MUSIC stream volume minus 3dB will be used.
1069     * NOTE: This version is for applications that have their own
1070     * settings panel for enabling and controlling volume.
1071     */
1072    public void  playSoundEffect(int effectType, float volume) {
1073        if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) {
1074            return;
1075        }
1076
1077        IAudioService service = getService();
1078        try {
1079            service.playSoundEffectVolume(effectType, volume);
1080        } catch (RemoteException e) {
1081            Log.e(TAG, "Dead object in playSoundEffect"+e);
1082        }
1083    }
1084
1085    /**
1086     * Settings has an in memory cache, so this is fast.
1087     */
1088    private boolean querySoundEffectsEnabled() {
1089        return Settings.System.getInt(mContext.getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0;
1090    }
1091
1092
1093    /**
1094     *  Load Sound effects.
1095     *  This method must be called when sound effects are enabled.
1096     */
1097    public void loadSoundEffects() {
1098        IAudioService service = getService();
1099        try {
1100            service.loadSoundEffects();
1101        } catch (RemoteException e) {
1102            Log.e(TAG, "Dead object in loadSoundEffects"+e);
1103        }
1104    }
1105
1106    /**
1107     *  Unload Sound effects.
1108     *  This method can be called to free some memory when
1109     *  sound effects are disabled.
1110     */
1111    public void unloadSoundEffects() {
1112        IAudioService service = getService();
1113        try {
1114            service.unloadSoundEffects();
1115        } catch (RemoteException e) {
1116            Log.e(TAG, "Dead object in unloadSoundEffects"+e);
1117        }
1118    }
1119
1120    /**
1121     *  @hide
1122     *  Reload audio settings. This method is called by Settings backup
1123     *  agent when audio settings are restored and causes the AudioService
1124     *  to read and apply restored settings.
1125     */
1126    public void reloadAudioSettings() {
1127        IAudioService service = getService();
1128        try {
1129            service.reloadAudioSettings();
1130        } catch (RemoteException e) {
1131            Log.e(TAG, "Dead object in reloadAudioSettings"+e);
1132        }
1133    }
1134
1135     /**
1136      * {@hide}
1137      */
1138     private IBinder mICallBack = new Binder();
1139}
1140