AudioManager.java revision f108cdd9ee5efe354d87edd02a07b323298c116c
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.Manifest;
20import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
22import android.app.PendingIntent;
23import android.bluetooth.BluetoothDevice;
24import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
27import android.media.RemoteController.OnClientUpdateListener;
28import android.os.Binder;
29import android.os.Build;
30import android.os.Handler;
31import android.os.IBinder;
32import android.os.Looper;
33import android.os.Message;
34import android.os.RemoteException;
35import android.os.SystemClock;
36import android.os.ServiceManager;
37import android.provider.Settings;
38import android.util.Log;
39import android.view.KeyEvent;
40import android.view.VolumePanel;
41
42import java.util.HashMap;
43
44/**
45 * AudioManager provides access to volume and ringer mode control.
46 * <p>
47 * Use <code>Context.getSystemService(Context.AUDIO_SERVICE)</code> to get
48 * an instance of this class.
49 */
50public class AudioManager {
51
52    private final Context mContext;
53    private long mVolumeKeyUpTime;
54    private final boolean mUseMasterVolume;
55    private final boolean mUseVolumeKeySounds;
56    private final Binder mToken = new Binder();
57    private static String TAG = "AudioManager";
58
59    /**
60     * Broadcast intent, a hint for applications that audio is about to become
61     * 'noisy' due to a change in audio outputs. For example, this intent may
62     * be sent when a wired headset is unplugged, or when an A2DP audio
63     * sink is disconnected, and the audio system is about to automatically
64     * switch audio route to the speaker. Applications that are controlling
65     * audio streams may consider pausing, reducing volume or some other action
66     * on receipt of this intent so as not to surprise the user with audio
67     * from the speaker.
68     */
69    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
70    public static final String ACTION_AUDIO_BECOMING_NOISY = "android.media.AUDIO_BECOMING_NOISY";
71
72    /**
73     * Sticky broadcast intent action indicating that the ringer mode has
74     * changed. Includes the new ringer mode.
75     *
76     * @see #EXTRA_RINGER_MODE
77     */
78    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
79    public static final String RINGER_MODE_CHANGED_ACTION = "android.media.RINGER_MODE_CHANGED";
80
81    /**
82     * The new ringer mode.
83     *
84     * @see #RINGER_MODE_CHANGED_ACTION
85     * @see #RINGER_MODE_NORMAL
86     * @see #RINGER_MODE_SILENT
87     * @see #RINGER_MODE_VIBRATE
88     */
89    public static final String EXTRA_RINGER_MODE = "android.media.EXTRA_RINGER_MODE";
90
91    /**
92     * Broadcast intent action indicating that the vibrate setting has
93     * changed. Includes the vibrate type and its new setting.
94     *
95     * @see #EXTRA_VIBRATE_TYPE
96     * @see #EXTRA_VIBRATE_SETTING
97     * @deprecated Applications should maintain their own vibrate policy based on
98     * current ringer mode and listen to {@link #RINGER_MODE_CHANGED_ACTION} instead.
99     */
100    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
101    public static final String VIBRATE_SETTING_CHANGED_ACTION =
102        "android.media.VIBRATE_SETTING_CHANGED";
103
104    /**
105     * @hide Broadcast intent when the volume for a particular stream type changes.
106     * Includes the stream, the new volume and previous volumes.
107     * Notes:
108     *  - for internal platform use only, do not make public,
109     *  - never used for "remote" volume changes
110     *
111     * @see #EXTRA_VOLUME_STREAM_TYPE
112     * @see #EXTRA_VOLUME_STREAM_VALUE
113     * @see #EXTRA_PREV_VOLUME_STREAM_VALUE
114     */
115    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
116    public static final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION";
117
118    /**
119     * @hide Broadcast intent when the master volume changes.
120     * Includes the new volume
121     *
122     * @see #EXTRA_MASTER_VOLUME_VALUE
123     * @see #EXTRA_PREV_MASTER_VOLUME_VALUE
124     */
125    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
126    public static final String MASTER_VOLUME_CHANGED_ACTION =
127        "android.media.MASTER_VOLUME_CHANGED_ACTION";
128
129    /**
130     * @hide Broadcast intent when the master mute state changes.
131     * Includes the the new volume
132     *
133     * @see #EXTRA_MASTER_VOLUME_MUTED
134     */
135    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
136    public static final String MASTER_MUTE_CHANGED_ACTION =
137        "android.media.MASTER_MUTE_CHANGED_ACTION";
138
139    /**
140     * The new vibrate setting for a particular type.
141     *
142     * @see #VIBRATE_SETTING_CHANGED_ACTION
143     * @see #EXTRA_VIBRATE_TYPE
144     * @see #VIBRATE_SETTING_ON
145     * @see #VIBRATE_SETTING_OFF
146     * @see #VIBRATE_SETTING_ONLY_SILENT
147     * @deprecated Applications should maintain their own vibrate policy based on
148     * current ringer mode and listen to {@link #RINGER_MODE_CHANGED_ACTION} instead.
149     */
150    public static final String EXTRA_VIBRATE_SETTING = "android.media.EXTRA_VIBRATE_SETTING";
151
152    /**
153     * The vibrate type whose setting has changed.
154     *
155     * @see #VIBRATE_SETTING_CHANGED_ACTION
156     * @see #VIBRATE_TYPE_NOTIFICATION
157     * @see #VIBRATE_TYPE_RINGER
158     * @deprecated Applications should maintain their own vibrate policy based on
159     * current ringer mode and listen to {@link #RINGER_MODE_CHANGED_ACTION} instead.
160     */
161    public static final String EXTRA_VIBRATE_TYPE = "android.media.EXTRA_VIBRATE_TYPE";
162
163    /**
164     * @hide The stream type for the volume changed intent.
165     */
166    public static final String EXTRA_VOLUME_STREAM_TYPE = "android.media.EXTRA_VOLUME_STREAM_TYPE";
167
168    /**
169     * @hide The volume associated with the stream for the volume changed intent.
170     */
171    public static final String EXTRA_VOLUME_STREAM_VALUE =
172        "android.media.EXTRA_VOLUME_STREAM_VALUE";
173
174    /**
175     * @hide The previous volume associated with the stream for the volume changed intent.
176     */
177    public static final String EXTRA_PREV_VOLUME_STREAM_VALUE =
178        "android.media.EXTRA_PREV_VOLUME_STREAM_VALUE";
179
180    /**
181     * @hide The new master volume value for the master volume changed intent.
182     * Value is integer between 0 and 100 inclusive.
183     */
184    public static final String EXTRA_MASTER_VOLUME_VALUE =
185        "android.media.EXTRA_MASTER_VOLUME_VALUE";
186
187    /**
188     * @hide The previous master volume value for the master volume changed intent.
189     * Value is integer between 0 and 100 inclusive.
190     */
191    public static final String EXTRA_PREV_MASTER_VOLUME_VALUE =
192        "android.media.EXTRA_PREV_MASTER_VOLUME_VALUE";
193
194    /**
195     * @hide The new master volume mute state for the master mute changed intent.
196     * Value is boolean
197     */
198    public static final String EXTRA_MASTER_VOLUME_MUTED =
199        "android.media.EXTRA_MASTER_VOLUME_MUTED";
200
201    /** The audio stream for phone calls */
202    public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL;
203    /** The audio stream for system sounds */
204    public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM;
205    /** The audio stream for the phone ring */
206    public static final int STREAM_RING = AudioSystem.STREAM_RING;
207    /** The audio stream for music playback */
208    public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC;
209    /** The audio stream for alarms */
210    public static final int STREAM_ALARM = AudioSystem.STREAM_ALARM;
211    /** The audio stream for notifications */
212    public static final int STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION;
213    /** @hide The audio stream for phone calls when connected to bluetooth */
214    public static final int STREAM_BLUETOOTH_SCO = AudioSystem.STREAM_BLUETOOTH_SCO;
215    /** @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */
216    public static final int STREAM_SYSTEM_ENFORCED = AudioSystem.STREAM_SYSTEM_ENFORCED;
217    /** The audio stream for DTMF Tones */
218    public static final int STREAM_DTMF = AudioSystem.STREAM_DTMF;
219    /** @hide The audio stream for text to speech (TTS) */
220    public static final int STREAM_TTS = AudioSystem.STREAM_TTS;
221    /** Number of audio streams */
222    /**
223     * @deprecated Use AudioSystem.getNumStreamTypes() instead
224     */
225    @Deprecated public static final int NUM_STREAMS = AudioSystem.NUM_STREAMS;
226
227
228    /**  @hide Default volume index values for audio streams */
229    public static final int[] DEFAULT_STREAM_VOLUME = new int[] {
230        4,  // STREAM_VOICE_CALL
231        7,  // STREAM_SYSTEM
232        5,  // STREAM_RING
233        11, // STREAM_MUSIC
234        6,  // STREAM_ALARM
235        5,  // STREAM_NOTIFICATION
236        7,  // STREAM_BLUETOOTH_SCO
237        7,  // STREAM_SYSTEM_ENFORCED
238        11, // STREAM_DTMF
239        11  // STREAM_TTS
240    };
241
242    /**
243     * Increase the ringer volume.
244     *
245     * @see #adjustVolume(int, int)
246     * @see #adjustStreamVolume(int, int, int)
247     */
248    public static final int ADJUST_RAISE = 1;
249
250    /**
251     * Decrease the ringer volume.
252     *
253     * @see #adjustVolume(int, int)
254     * @see #adjustStreamVolume(int, int, int)
255     */
256    public static final int ADJUST_LOWER = -1;
257
258    /**
259     * Maintain the previous ringer volume. This may be useful when needing to
260     * show the volume toast without actually modifying the volume.
261     *
262     * @see #adjustVolume(int, int)
263     * @see #adjustStreamVolume(int, int, int)
264     */
265    public static final int ADJUST_SAME = 0;
266
267    // Flags should be powers of 2!
268
269    /**
270     * Show a toast containing the current volume.
271     *
272     * @see #adjustStreamVolume(int, int, int)
273     * @see #adjustVolume(int, int)
274     * @see #setStreamVolume(int, int, int)
275     * @see #setRingerMode(int)
276     */
277    public static final int FLAG_SHOW_UI = 1 << 0;
278
279    /**
280     * Whether to include ringer modes as possible options when changing volume.
281     * For example, if true and volume level is 0 and the volume is adjusted
282     * with {@link #ADJUST_LOWER}, then the ringer mode may switch the silent or
283     * vibrate mode.
284     * <p>
285     * By default this is on for the ring stream. If this flag is included,
286     * this behavior will be present regardless of the stream type being
287     * affected by the ringer mode.
288     *
289     * @see #adjustVolume(int, int)
290     * @see #adjustStreamVolume(int, int, int)
291     */
292    public static final int FLAG_ALLOW_RINGER_MODES = 1 << 1;
293
294    /**
295     * Whether to play a sound when changing the volume.
296     * <p>
297     * If this is given to {@link #adjustVolume(int, int)} or
298     * {@link #adjustSuggestedStreamVolume(int, int, int)}, it may be ignored
299     * in some cases (for example, the decided stream type is not
300     * {@link AudioManager#STREAM_RING}, or the volume is being adjusted
301     * downward).
302     *
303     * @see #adjustStreamVolume(int, int, int)
304     * @see #adjustVolume(int, int)
305     * @see #setStreamVolume(int, int, int)
306     */
307    public static final int FLAG_PLAY_SOUND = 1 << 2;
308
309    /**
310     * Removes any sounds/vibrate that may be in the queue, or are playing (related to
311     * changing volume).
312     */
313    public static final int FLAG_REMOVE_SOUND_AND_VIBRATE = 1 << 3;
314
315    /**
316     * Whether to vibrate if going into the vibrate ringer mode.
317     */
318    public static final int FLAG_VIBRATE = 1 << 4;
319
320    /**
321     * Indicates to VolumePanel that the volume slider should be disabled as user
322     * cannot change the stream volume
323     * @hide
324     */
325    public static final int FLAG_FIXED_VOLUME = 1 << 5;
326
327    /**
328     * Indicates the volume set/adjust call is for Bluetooth absolute volume
329     * @hide
330     */
331    public static final int FLAG_BLUETOOTH_ABS_VOLUME = 1 << 6;
332
333    /**
334     * Ringer mode that will be silent and will not vibrate. (This overrides the
335     * vibrate setting.)
336     *
337     * @see #setRingerMode(int)
338     * @see #getRingerMode()
339     */
340    public static final int RINGER_MODE_SILENT = 0;
341
342    /**
343     * Ringer mode that will be silent and will vibrate. (This will cause the
344     * phone ringer to always vibrate, but the notification vibrate to only
345     * vibrate if set.)
346     *
347     * @see #setRingerMode(int)
348     * @see #getRingerMode()
349     */
350    public static final int RINGER_MODE_VIBRATE = 1;
351
352    /**
353     * Ringer mode that may be audible and may vibrate. It will be audible if
354     * the volume before changing out of this mode was audible. It will vibrate
355     * if the vibrate setting is on.
356     *
357     * @see #setRingerMode(int)
358     * @see #getRingerMode()
359     */
360    public static final int RINGER_MODE_NORMAL = 2;
361
362    // maximum valid ringer mode value. Values must start from 0 and be contiguous.
363    private static final int RINGER_MODE_MAX = RINGER_MODE_NORMAL;
364
365    /**
366     * Vibrate type that corresponds to the ringer.
367     *
368     * @see #setVibrateSetting(int, int)
369     * @see #getVibrateSetting(int)
370     * @see #shouldVibrate(int)
371     * @deprecated Applications should maintain their own vibrate policy based on
372     * current ringer mode that can be queried via {@link #getRingerMode()}.
373     */
374    public static final int VIBRATE_TYPE_RINGER = 0;
375
376    /**
377     * Vibrate type that corresponds to notifications.
378     *
379     * @see #setVibrateSetting(int, int)
380     * @see #getVibrateSetting(int)
381     * @see #shouldVibrate(int)
382     * @deprecated Applications should maintain their own vibrate policy based on
383     * current ringer mode that can be queried via {@link #getRingerMode()}.
384     */
385    public static final int VIBRATE_TYPE_NOTIFICATION = 1;
386
387    /**
388     * Vibrate setting that suggests to never vibrate.
389     *
390     * @see #setVibrateSetting(int, int)
391     * @see #getVibrateSetting(int)
392     * @deprecated Applications should maintain their own vibrate policy based on
393     * current ringer mode that can be queried via {@link #getRingerMode()}.
394     */
395    public static final int VIBRATE_SETTING_OFF = 0;
396
397    /**
398     * Vibrate setting that suggests to vibrate when possible.
399     *
400     * @see #setVibrateSetting(int, int)
401     * @see #getVibrateSetting(int)
402     * @deprecated Applications should maintain their own vibrate policy based on
403     * current ringer mode that can be queried via {@link #getRingerMode()}.
404     */
405    public static final int VIBRATE_SETTING_ON = 1;
406
407    /**
408     * Vibrate setting that suggests to only vibrate when in the vibrate ringer
409     * mode.
410     *
411     * @see #setVibrateSetting(int, int)
412     * @see #getVibrateSetting(int)
413     * @deprecated Applications should maintain their own vibrate policy based on
414     * current ringer mode that can be queried via {@link #getRingerMode()}.
415     */
416    public static final int VIBRATE_SETTING_ONLY_SILENT = 2;
417
418    /**
419     * Suggests using the default stream type. This may not be used in all
420     * places a stream type is needed.
421     */
422    public static final int USE_DEFAULT_STREAM_TYPE = Integer.MIN_VALUE;
423
424    private static IAudioService sService;
425
426    /**
427     * @hide
428     */
429    public AudioManager(Context context) {
430        mContext = context;
431        mUseMasterVolume = mContext.getResources().getBoolean(
432                com.android.internal.R.bool.config_useMasterVolume);
433        mUseVolumeKeySounds = mContext.getResources().getBoolean(
434                com.android.internal.R.bool.config_useVolumeKeySounds);
435    }
436
437    private static IAudioService getService()
438    {
439        if (sService != null) {
440            return sService;
441        }
442        IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
443        sService = IAudioService.Stub.asInterface(b);
444        return sService;
445    }
446
447    /**
448     * Sends a simulated key event for a media button.
449     * To simulate a key press, you must first send a KeyEvent built with a
450     * {@link KeyEvent#ACTION_DOWN} action, then another event with the {@link KeyEvent#ACTION_UP}
451     * action.
452     * <p>The key event will be sent to the current media key event consumer which registered with
453     * {@link AudioManager#registerMediaButtonEventReceiver(PendingIntent)}.
454     * @param keyEvent a {@link KeyEvent} instance whose key code is one of
455     *     {@link KeyEvent#KEYCODE_MUTE},
456     *     {@link KeyEvent#KEYCODE_HEADSETHOOK},
457     *     {@link KeyEvent#KEYCODE_MEDIA_PLAY},
458     *     {@link KeyEvent#KEYCODE_MEDIA_PAUSE},
459     *     {@link KeyEvent#KEYCODE_MEDIA_PLAY_PAUSE},
460     *     {@link KeyEvent#KEYCODE_MEDIA_STOP},
461     *     {@link KeyEvent#KEYCODE_MEDIA_NEXT},
462     *     {@link KeyEvent#KEYCODE_MEDIA_PREVIOUS},
463     *     {@link KeyEvent#KEYCODE_MEDIA_REWIND},
464     *     {@link KeyEvent#KEYCODE_MEDIA_RECORD},
465     *     {@link KeyEvent#KEYCODE_MEDIA_FAST_FORWARD},
466     *     {@link KeyEvent#KEYCODE_MEDIA_CLOSE},
467     *     {@link KeyEvent#KEYCODE_MEDIA_EJECT},
468     *     or {@link KeyEvent#KEYCODE_MEDIA_AUDIO_TRACK}.
469     */
470    public void dispatchMediaKeyEvent(KeyEvent keyEvent) {
471        IAudioService service = getService();
472        try {
473            service.dispatchMediaKeyEvent(keyEvent);
474        } catch (RemoteException e) {
475            Log.e(TAG, "dispatchMediaKeyEvent threw exception ", e);
476        }
477    }
478
479    /**
480     * @hide
481     */
482    public void preDispatchKeyEvent(KeyEvent event, int stream) {
483        /*
484         * If the user hits another key within the play sound delay, then
485         * cancel the sound
486         */
487        int keyCode = event.getKeyCode();
488        if (keyCode != KeyEvent.KEYCODE_VOLUME_DOWN && keyCode != KeyEvent.KEYCODE_VOLUME_UP
489                && keyCode != KeyEvent.KEYCODE_VOLUME_MUTE
490                && mVolumeKeyUpTime + VolumePanel.PLAY_SOUND_DELAY
491                        > SystemClock.uptimeMillis()) {
492            /*
493             * The user has hit another key during the delay (e.g., 300ms)
494             * since the last volume key up, so cancel any sounds.
495             */
496            if (mUseMasterVolume) {
497                adjustMasterVolume(ADJUST_SAME, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
498            } else {
499                adjustSuggestedStreamVolume(ADJUST_SAME,
500                        stream, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
501            }
502        }
503    }
504
505    /**
506     * @hide
507     */
508    public void handleKeyDown(KeyEvent event, int stream) {
509        int keyCode = event.getKeyCode();
510        switch (keyCode) {
511            case KeyEvent.KEYCODE_VOLUME_UP:
512            case KeyEvent.KEYCODE_VOLUME_DOWN:
513                /*
514                 * Adjust the volume in on key down since it is more
515                 * responsive to the user.
516                 */
517                int flags = FLAG_SHOW_UI | FLAG_VIBRATE;
518
519                if (mUseMasterVolume) {
520                    adjustMasterVolume(
521                            keyCode == KeyEvent.KEYCODE_VOLUME_UP
522                                    ? ADJUST_RAISE
523                                    : ADJUST_LOWER,
524                            flags);
525                } else {
526                    adjustSuggestedStreamVolume(
527                            keyCode == KeyEvent.KEYCODE_VOLUME_UP
528                                    ? ADJUST_RAISE
529                                    : ADJUST_LOWER,
530                            stream,
531                            flags);
532                }
533                break;
534            case KeyEvent.KEYCODE_VOLUME_MUTE:
535                if (event.getRepeatCount() == 0) {
536                    if (mUseMasterVolume) {
537                        setMasterMute(!isMasterMute());
538                    } else {
539                        // TODO: Actually handle MUTE.
540                    }
541                }
542                break;
543        }
544    }
545
546    /**
547     * @hide
548     */
549    public void handleKeyUp(KeyEvent event, int stream) {
550        int keyCode = event.getKeyCode();
551        switch (keyCode) {
552            case KeyEvent.KEYCODE_VOLUME_UP:
553            case KeyEvent.KEYCODE_VOLUME_DOWN:
554                /*
555                 * Play a sound. This is done on key up since we don't want the
556                 * sound to play when a user holds down volume down to mute.
557                 */
558                if (mUseVolumeKeySounds) {
559                    if (mUseMasterVolume) {
560                        adjustMasterVolume(ADJUST_SAME, FLAG_PLAY_SOUND);
561                    } else {
562                        int flags = FLAG_PLAY_SOUND;
563                        adjustSuggestedStreamVolume(
564                                ADJUST_SAME,
565                                stream,
566                                flags);
567                    }
568                }
569                mVolumeKeyUpTime = SystemClock.uptimeMillis();
570                break;
571        }
572    }
573
574    /**
575     * Adjusts the volume of a particular stream by one step in a direction.
576     * <p>
577     * This method should only be used by applications that replace the platform-wide
578     * management of audio settings or the main telephony application.
579     *
580     * @param streamType The stream type to adjust. One of {@link #STREAM_VOICE_CALL},
581     * {@link #STREAM_SYSTEM}, {@link #STREAM_RING}, {@link #STREAM_MUSIC} or
582     * {@link #STREAM_ALARM}
583     * @param direction The direction to adjust the volume. One of
584     *            {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or
585     *            {@link #ADJUST_SAME}.
586     * @param flags One or more flags.
587     * @see #adjustVolume(int, int)
588     * @see #setStreamVolume(int, int, int)
589     */
590    public void adjustStreamVolume(int streamType, int direction, int flags) {
591        IAudioService service = getService();
592        try {
593            if (mUseMasterVolume) {
594                service.adjustMasterVolume(direction, flags, mContext.getOpPackageName());
595            } else {
596                service.adjustStreamVolume(streamType, direction, flags,
597                        mContext.getOpPackageName());
598            }
599        } catch (RemoteException e) {
600            Log.e(TAG, "Dead object in adjustStreamVolume", e);
601        }
602    }
603
604    /**
605     * Adjusts the volume of the most relevant stream. For example, if a call is
606     * active, it will have the highest priority regardless of if the in-call
607     * screen is showing. Another example, if music is playing in the background
608     * and a call is not active, the music stream will be adjusted.
609     * <p>
610     * This method should only be used by applications that replace the platform-wide
611     * management of audio settings or the main telephony application.
612     *
613     * @param direction The direction to adjust the volume. One of
614     *            {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or
615     *            {@link #ADJUST_SAME}.
616     * @param flags One or more flags.
617     * @see #adjustSuggestedStreamVolume(int, int, int)
618     * @see #adjustStreamVolume(int, int, int)
619     * @see #setStreamVolume(int, int, int)
620     */
621    public void adjustVolume(int direction, int flags) {
622        IAudioService service = getService();
623        try {
624            if (mUseMasterVolume) {
625                service.adjustMasterVolume(direction, flags, mContext.getOpPackageName());
626            } else {
627                service.adjustVolume(direction, flags, mContext.getOpPackageName());
628            }
629        } catch (RemoteException e) {
630            Log.e(TAG, "Dead object in adjustVolume", e);
631        }
632    }
633
634    /**
635     * Adjusts the volume of the most relevant stream, or the given fallback
636     * stream.
637     * <p>
638     * This method should only be used by applications that replace the platform-wide
639     * management of audio settings or the main telephony application.
640     *
641     * @param direction The direction to adjust the volume. One of
642     *            {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or
643     *            {@link #ADJUST_SAME}.
644     * @param suggestedStreamType The stream type that will be used if there
645     *            isn't a relevant stream. {@link #USE_DEFAULT_STREAM_TYPE} is valid here.
646     * @param flags One or more flags.
647     * @see #adjustVolume(int, int)
648     * @see #adjustStreamVolume(int, int, int)
649     * @see #setStreamVolume(int, int, int)
650     */
651    public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags) {
652        IAudioService service = getService();
653        try {
654            if (mUseMasterVolume) {
655                service.adjustMasterVolume(direction, flags, mContext.getOpPackageName());
656            } else {
657                service.adjustSuggestedStreamVolume(direction, suggestedStreamType, flags,
658                        mContext.getOpPackageName());
659            }
660        } catch (RemoteException e) {
661            Log.e(TAG, "Dead object in adjustSuggestedStreamVolume", e);
662        }
663    }
664
665    /**
666     * Adjusts the master volume for the device's audio amplifier.
667     * <p>
668     *
669     * @param steps The number of volume steps to adjust. A positive
670     *            value will raise the volume.
671     * @param flags One or more flags.
672     * @hide
673     */
674    public void adjustMasterVolume(int steps, int flags) {
675        IAudioService service = getService();
676        try {
677            service.adjustMasterVolume(steps, flags, mContext.getOpPackageName());
678        } catch (RemoteException e) {
679            Log.e(TAG, "Dead object in adjustMasterVolume", e);
680        }
681    }
682
683    /**
684     * Returns the current ringtone mode.
685     *
686     * @return The current ringtone mode, one of {@link #RINGER_MODE_NORMAL},
687     *         {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}.
688     * @see #setRingerMode(int)
689     */
690    public int getRingerMode() {
691        IAudioService service = getService();
692        try {
693            return service.getRingerMode();
694        } catch (RemoteException e) {
695            Log.e(TAG, "Dead object in getRingerMode", e);
696            return RINGER_MODE_NORMAL;
697        }
698    }
699
700    /**
701     * Checks valid ringer mode values.
702     *
703     * @return true if the ringer mode indicated is valid, false otherwise.
704     *
705     * @see #setRingerMode(int)
706     * @hide
707     */
708    public static boolean isValidRingerMode(int ringerMode) {
709        if (ringerMode < 0 || ringerMode > RINGER_MODE_MAX) {
710            return false;
711        }
712        return true;
713    }
714
715    /**
716     * Returns the maximum volume index for a particular stream.
717     *
718     * @param streamType The stream type whose maximum volume index is returned.
719     * @return The maximum valid volume index for the stream.
720     * @see #getStreamVolume(int)
721     */
722    public int getStreamMaxVolume(int streamType) {
723        IAudioService service = getService();
724        try {
725            if (mUseMasterVolume) {
726                return service.getMasterMaxVolume();
727            } else {
728                return service.getStreamMaxVolume(streamType);
729            }
730        } catch (RemoteException e) {
731            Log.e(TAG, "Dead object in getStreamMaxVolume", e);
732            return 0;
733        }
734    }
735
736    /**
737     * Returns the current volume index for a particular stream.
738     *
739     * @param streamType The stream type whose volume index is returned.
740     * @return The current volume index for the stream.
741     * @see #getStreamMaxVolume(int)
742     * @see #setStreamVolume(int, int, int)
743     */
744    public int getStreamVolume(int streamType) {
745        IAudioService service = getService();
746        try {
747            if (mUseMasterVolume) {
748                return service.getMasterVolume();
749            } else {
750                return service.getStreamVolume(streamType);
751            }
752        } catch (RemoteException e) {
753            Log.e(TAG, "Dead object in getStreamVolume", e);
754            return 0;
755        }
756    }
757
758    /**
759     * Get last audible volume before stream was muted.
760     *
761     * @hide
762     */
763    public int getLastAudibleStreamVolume(int streamType) {
764        IAudioService service = getService();
765        try {
766            if (mUseMasterVolume) {
767                return service.getLastAudibleMasterVolume();
768            } else {
769                return service.getLastAudibleStreamVolume(streamType);
770            }
771        } catch (RemoteException e) {
772            Log.e(TAG, "Dead object in getLastAudibleStreamVolume", e);
773            return 0;
774        }
775    }
776
777    /**
778     * Get the stream type whose volume is driving the UI sounds volume.
779     * UI sounds are screen lock/unlock, camera shutter, key clicks...
780     * @hide
781     */
782    public int getMasterStreamType() {
783        IAudioService service = getService();
784        try {
785            return service.getMasterStreamType();
786        } catch (RemoteException e) {
787            Log.e(TAG, "Dead object in getMasterStreamType", e);
788            return STREAM_RING;
789        }
790    }
791
792    /**
793     * Sets the ringer mode.
794     * <p>
795     * Silent mode will mute the volume and will not vibrate. Vibrate mode will
796     * mute the volume and vibrate. Normal mode will be audible and may vibrate
797     * according to user settings.
798     *
799     * @param ringerMode The ringer mode, one of {@link #RINGER_MODE_NORMAL},
800     *            {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}.
801     * @see #getRingerMode()
802     */
803    public void setRingerMode(int ringerMode) {
804        if (!isValidRingerMode(ringerMode)) {
805            return;
806        }
807        IAudioService service = getService();
808        try {
809            service.setRingerMode(ringerMode);
810        } catch (RemoteException e) {
811            Log.e(TAG, "Dead object in setRingerMode", e);
812        }
813    }
814
815    /**
816     * Sets the volume index for a particular stream.
817     *
818     * @param streamType The stream whose volume index should be set.
819     * @param index The volume index to set. See
820     *            {@link #getStreamMaxVolume(int)} for the largest valid value.
821     * @param flags One or more flags.
822     * @see #getStreamMaxVolume(int)
823     * @see #getStreamVolume(int)
824     */
825    public void setStreamVolume(int streamType, int index, int flags) {
826        IAudioService service = getService();
827        try {
828            if (mUseMasterVolume) {
829                service.setMasterVolume(index, flags, mContext.getOpPackageName());
830            } else {
831                service.setStreamVolume(streamType, index, flags, mContext.getOpPackageName());
832            }
833        } catch (RemoteException e) {
834            Log.e(TAG, "Dead object in setStreamVolume", e);
835        }
836    }
837
838    /**
839     * Returns the maximum volume index for master volume.
840     *
841     * @hide
842     */
843    public int getMasterMaxVolume() {
844        IAudioService service = getService();
845        try {
846            return service.getMasterMaxVolume();
847        } catch (RemoteException e) {
848            Log.e(TAG, "Dead object in getMasterMaxVolume", e);
849            return 0;
850        }
851    }
852
853    /**
854     * Returns the current volume index for master volume.
855     *
856     * @return The current volume index for master volume.
857     * @hide
858     */
859    public int getMasterVolume() {
860        IAudioService service = getService();
861        try {
862            return service.getMasterVolume();
863        } catch (RemoteException e) {
864            Log.e(TAG, "Dead object in getMasterVolume", e);
865            return 0;
866        }
867    }
868
869    /**
870     * Get last audible volume before master volume was muted.
871     *
872     * @hide
873     */
874    public int getLastAudibleMasterVolume() {
875        IAudioService service = getService();
876        try {
877            return service.getLastAudibleMasterVolume();
878        } catch (RemoteException e) {
879            Log.e(TAG, "Dead object in getLastAudibleMasterVolume", e);
880            return 0;
881        }
882    }
883
884    /**
885     * Sets the volume index for master volume.
886     *
887     * @param index The volume index to set. See
888     *            {@link #getMasterMaxVolume()} for the largest valid value.
889     * @param flags One or more flags.
890     * @see #getMasterMaxVolume()
891     * @see #getMasterVolume()
892     * @hide
893     */
894    public void setMasterVolume(int index, int flags) {
895        IAudioService service = getService();
896        try {
897            service.setMasterVolume(index, flags, mContext.getOpPackageName());
898        } catch (RemoteException e) {
899            Log.e(TAG, "Dead object in setMasterVolume", e);
900        }
901    }
902
903    /**
904     * Solo or unsolo a particular stream. All other streams are muted.
905     * <p>
906     * The solo command is protected against client process death: if a process
907     * with an active solo request on a stream dies, all streams that were muted
908     * because of this request will be unmuted automatically.
909     * <p>
910     * The solo requests for a given stream are cumulative: the AudioManager
911     * can receive several solo requests from one or more clients and the stream
912     * will be unsoloed only when the same number of unsolo requests are received.
913     * <p>
914     * For a better user experience, applications MUST unsolo a soloed stream
915     * in onPause() and solo is again in onResume() if appropriate.
916     *
917     * @param streamType The stream to be soloed/unsoloed.
918     * @param state The required solo state: true for solo ON, false for solo OFF
919     */
920    public void setStreamSolo(int streamType, boolean state) {
921        IAudioService service = getService();
922        try {
923            service.setStreamSolo(streamType, state, mICallBack);
924        } catch (RemoteException e) {
925            Log.e(TAG, "Dead object in setStreamSolo", e);
926        }
927    }
928
929    /**
930     * Mute or unmute an audio stream.
931     * <p>
932     * The mute command is protected against client process death: if a process
933     * with an active mute request on a stream dies, this stream will be unmuted
934     * automatically.
935     * <p>
936     * The mute requests for a given stream are cumulative: the AudioManager
937     * can receive several mute requests from one or more clients and the stream
938     * will be unmuted only when the same number of unmute requests are received.
939     * <p>
940     * For a better user experience, applications MUST unmute a muted stream
941     * in onPause() and mute is again in onResume() if appropriate.
942     * <p>
943     * This method should only be used by applications that replace the platform-wide
944     * management of audio settings or the main telephony application.
945     *
946     * @param streamType The stream to be muted/unmuted.
947     * @param state The required mute state: true for mute ON, false for mute OFF
948     */
949    public void setStreamMute(int streamType, boolean state) {
950        IAudioService service = getService();
951        try {
952            service.setStreamMute(streamType, state, mICallBack);
953        } catch (RemoteException e) {
954            Log.e(TAG, "Dead object in setStreamMute", e);
955        }
956    }
957
958    /**
959     * get stream mute state.
960     *
961     * @hide
962     */
963    public boolean isStreamMute(int streamType) {
964        IAudioService service = getService();
965        try {
966            return service.isStreamMute(streamType);
967        } catch (RemoteException e) {
968            Log.e(TAG, "Dead object in isStreamMute", e);
969            return false;
970        }
971    }
972
973    /**
974     * set master mute state.
975     *
976     * @hide
977     */
978    public void setMasterMute(boolean state) {
979        setMasterMute(state, FLAG_SHOW_UI);
980    }
981
982    /**
983     * set master mute state with optional flags.
984     *
985     * @hide
986     */
987    public void setMasterMute(boolean state, int flags) {
988        IAudioService service = getService();
989        try {
990            service.setMasterMute(state, flags, mICallBack);
991        } catch (RemoteException e) {
992            Log.e(TAG, "Dead object in setMasterMute", e);
993        }
994    }
995
996    /**
997     * get master mute state.
998     *
999     * @hide
1000     */
1001    public boolean isMasterMute() {
1002        IAudioService service = getService();
1003        try {
1004            return service.isMasterMute();
1005        } catch (RemoteException e) {
1006            Log.e(TAG, "Dead object in isMasterMute", e);
1007            return false;
1008        }
1009    }
1010
1011    /**
1012     * forces the stream controlled by hard volume keys
1013     * specifying streamType == -1 releases control to the
1014     * logic.
1015     *
1016     * @hide
1017     */
1018    public void forceVolumeControlStream(int streamType) {
1019        IAudioService service = getService();
1020        try {
1021            service.forceVolumeControlStream(streamType, mICallBack);
1022        } catch (RemoteException e) {
1023            Log.e(TAG, "Dead object in forceVolumeControlStream", e);
1024        }
1025    }
1026
1027    /**
1028     * Returns whether a particular type should vibrate according to user
1029     * settings and the current ringer mode.
1030     * <p>
1031     * This shouldn't be needed by most clients that use notifications to
1032     * vibrate. The notification manager will not vibrate if the policy doesn't
1033     * allow it, so the client should always set a vibrate pattern and let the
1034     * notification manager control whether or not to actually vibrate.
1035     *
1036     * @param vibrateType The type of vibrate. One of
1037     *            {@link #VIBRATE_TYPE_NOTIFICATION} or
1038     *            {@link #VIBRATE_TYPE_RINGER}.
1039     * @return Whether the type should vibrate at the instant this method is
1040     *         called.
1041     * @see #setVibrateSetting(int, int)
1042     * @see #getVibrateSetting(int)
1043     * @deprecated Applications should maintain their own vibrate policy based on
1044     * current ringer mode that can be queried via {@link #getRingerMode()}.
1045     */
1046    public boolean shouldVibrate(int vibrateType) {
1047        IAudioService service = getService();
1048        try {
1049            return service.shouldVibrate(vibrateType);
1050        } catch (RemoteException e) {
1051            Log.e(TAG, "Dead object in shouldVibrate", e);
1052            return false;
1053        }
1054    }
1055
1056    /**
1057     * Returns whether the user's vibrate setting for a vibrate type.
1058     * <p>
1059     * This shouldn't be needed by most clients that want to vibrate, instead
1060     * see {@link #shouldVibrate(int)}.
1061     *
1062     * @param vibrateType The type of vibrate. One of
1063     *            {@link #VIBRATE_TYPE_NOTIFICATION} or
1064     *            {@link #VIBRATE_TYPE_RINGER}.
1065     * @return The vibrate setting, one of {@link #VIBRATE_SETTING_ON},
1066     *         {@link #VIBRATE_SETTING_OFF}, or
1067     *         {@link #VIBRATE_SETTING_ONLY_SILENT}.
1068     * @see #setVibrateSetting(int, int)
1069     * @see #shouldVibrate(int)
1070     * @deprecated Applications should maintain their own vibrate policy based on
1071     * current ringer mode that can be queried via {@link #getRingerMode()}.
1072     */
1073    public int getVibrateSetting(int vibrateType) {
1074        IAudioService service = getService();
1075        try {
1076            return service.getVibrateSetting(vibrateType);
1077        } catch (RemoteException e) {
1078            Log.e(TAG, "Dead object in getVibrateSetting", e);
1079            return VIBRATE_SETTING_OFF;
1080        }
1081    }
1082
1083    /**
1084     * Sets the setting for when the vibrate type should vibrate.
1085     * <p>
1086     * This method should only be used by applications that replace the platform-wide
1087     * management of audio settings or the main telephony application.
1088     *
1089     * @param vibrateType The type of vibrate. One of
1090     *            {@link #VIBRATE_TYPE_NOTIFICATION} or
1091     *            {@link #VIBRATE_TYPE_RINGER}.
1092     * @param vibrateSetting The vibrate setting, one of
1093     *            {@link #VIBRATE_SETTING_ON},
1094     *            {@link #VIBRATE_SETTING_OFF}, or
1095     *            {@link #VIBRATE_SETTING_ONLY_SILENT}.
1096     * @see #getVibrateSetting(int)
1097     * @see #shouldVibrate(int)
1098     * @deprecated Applications should maintain their own vibrate policy based on
1099     * current ringer mode that can be queried via {@link #getRingerMode()}.
1100     */
1101    public void setVibrateSetting(int vibrateType, int vibrateSetting) {
1102        IAudioService service = getService();
1103        try {
1104            service.setVibrateSetting(vibrateType, vibrateSetting);
1105        } catch (RemoteException e) {
1106            Log.e(TAG, "Dead object in setVibrateSetting", e);
1107        }
1108    }
1109
1110    /**
1111     * Sets the speakerphone on or off.
1112     * <p>
1113     * This method should only be used by applications that replace the platform-wide
1114     * management of audio settings or the main telephony application.
1115     *
1116     * @param on set <var>true</var> to turn on speakerphone;
1117     *           <var>false</var> to turn it off
1118     */
1119    public void setSpeakerphoneOn(boolean on){
1120        IAudioService service = getService();
1121        try {
1122            service.setSpeakerphoneOn(on);
1123        } catch (RemoteException e) {
1124            Log.e(TAG, "Dead object in setSpeakerphoneOn", e);
1125        }
1126    }
1127
1128    /**
1129     * Checks whether the speakerphone is on or off.
1130     *
1131     * @return true if speakerphone is on, false if it's off
1132     */
1133    public boolean isSpeakerphoneOn() {
1134        IAudioService service = getService();
1135        try {
1136            return service.isSpeakerphoneOn();
1137        } catch (RemoteException e) {
1138            Log.e(TAG, "Dead object in isSpeakerphoneOn", e);
1139            return false;
1140        }
1141     }
1142
1143    //====================================================================
1144    // Bluetooth SCO control
1145    /**
1146     * Sticky broadcast intent action indicating that the bluetoooth SCO audio
1147     * connection state has changed. The intent contains on extra {@link #EXTRA_SCO_AUDIO_STATE}
1148     * indicating the new state which is either {@link #SCO_AUDIO_STATE_DISCONNECTED}
1149     * or {@link #SCO_AUDIO_STATE_CONNECTED}
1150     *
1151     * @see #startBluetoothSco()
1152     * @deprecated Use  {@link #ACTION_SCO_AUDIO_STATE_UPDATED} instead
1153     */
1154    @Deprecated
1155    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
1156    public static final String ACTION_SCO_AUDIO_STATE_CHANGED =
1157            "android.media.SCO_AUDIO_STATE_CHANGED";
1158
1159     /**
1160     * Sticky broadcast intent action indicating that the bluetoooth SCO audio
1161     * connection state has been updated.
1162     * <p>This intent has two extras:
1163     * <ul>
1164     *   <li> {@link #EXTRA_SCO_AUDIO_STATE} - The new SCO audio state. </li>
1165     *   <li> {@link #EXTRA_SCO_AUDIO_PREVIOUS_STATE}- The previous SCO audio state. </li>
1166     * </ul>
1167     * <p> EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE can be any of:
1168     * <ul>
1169     *   <li> {@link #SCO_AUDIO_STATE_DISCONNECTED}, </li>
1170     *   <li> {@link #SCO_AUDIO_STATE_CONNECTING} or </li>
1171     *   <li> {@link #SCO_AUDIO_STATE_CONNECTED}, </li>
1172     * </ul>
1173     * @see #startBluetoothSco()
1174     */
1175    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
1176    public static final String ACTION_SCO_AUDIO_STATE_UPDATED =
1177            "android.media.ACTION_SCO_AUDIO_STATE_UPDATED";
1178
1179    /**
1180     * Extra for intent {@link #ACTION_SCO_AUDIO_STATE_CHANGED} or
1181     * {@link #ACTION_SCO_AUDIO_STATE_UPDATED} containing the new bluetooth SCO connection state.
1182     */
1183    public static final String EXTRA_SCO_AUDIO_STATE =
1184            "android.media.extra.SCO_AUDIO_STATE";
1185
1186    /**
1187     * Extra for intent {@link #ACTION_SCO_AUDIO_STATE_UPDATED} containing the previous
1188     * bluetooth SCO connection state.
1189     */
1190    public static final String EXTRA_SCO_AUDIO_PREVIOUS_STATE =
1191            "android.media.extra.SCO_AUDIO_PREVIOUS_STATE";
1192
1193    /**
1194     * Value for extra EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE
1195     * indicating that the SCO audio channel is not established
1196     */
1197    public static final int SCO_AUDIO_STATE_DISCONNECTED = 0;
1198    /**
1199     * Value for extra {@link #EXTRA_SCO_AUDIO_STATE} or {@link #EXTRA_SCO_AUDIO_PREVIOUS_STATE}
1200     * indicating that the SCO audio channel is established
1201     */
1202    public static final int SCO_AUDIO_STATE_CONNECTED = 1;
1203    /**
1204     * Value for extra EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE
1205     * indicating that the SCO audio channel is being established
1206     */
1207    public static final int SCO_AUDIO_STATE_CONNECTING = 2;
1208    /**
1209     * Value for extra EXTRA_SCO_AUDIO_STATE indicating that
1210     * there was an error trying to obtain the state
1211     */
1212    public static final int SCO_AUDIO_STATE_ERROR = -1;
1213
1214
1215    /**
1216     * Indicates if current platform supports use of SCO for off call use cases.
1217     * Application wanted to use bluetooth SCO audio when the phone is not in call
1218     * must first call this method to make sure that the platform supports this
1219     * feature.
1220     * @return true if bluetooth SCO can be used for audio when not in call
1221     *         false otherwise
1222     * @see #startBluetoothSco()
1223    */
1224    public boolean isBluetoothScoAvailableOffCall() {
1225        return mContext.getResources().getBoolean(
1226               com.android.internal.R.bool.config_bluetooth_sco_off_call);
1227    }
1228
1229    /**
1230     * Start bluetooth SCO audio connection.
1231     * <p>Requires Permission:
1232     *   {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}.
1233     * <p>This method can be used by applications wanting to send and received audio
1234     * to/from a bluetooth SCO headset while the phone is not in call.
1235     * <p>As the SCO connection establishment can take several seconds,
1236     * applications should not rely on the connection to be available when the method
1237     * returns but instead register to receive the intent {@link #ACTION_SCO_AUDIO_STATE_UPDATED}
1238     * and wait for the state to be {@link #SCO_AUDIO_STATE_CONNECTED}.
1239     * <p>As the ACTION_SCO_AUDIO_STATE_UPDATED intent is sticky, the application can check the SCO
1240     * audio state before calling startBluetoothSco() by reading the intent returned by the receiver
1241     * registration. If the state is already CONNECTED, no state change will be received via the
1242     * intent after calling startBluetoothSco(). It is however useful to call startBluetoothSco()
1243     * so that the connection stays active in case the current initiator stops the connection.
1244     * <p>Unless the connection is already active as described above, the state will always
1245     * transition from DISCONNECTED to CONNECTING and then either to CONNECTED if the connection
1246     * succeeds or back to DISCONNECTED if the connection fails (e.g no headset is connected).
1247     * <p>When finished with the SCO connection or if the establishment fails, the application must
1248     * call {@link #stopBluetoothSco()} to clear the request and turn down the bluetooth connection.
1249     * <p>Even if a SCO connection is established, the following restrictions apply on audio
1250     * output streams so that they can be routed to SCO headset:
1251     * <p>NOTE: up to and including API version
1252     * {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}, this method initiates a virtual
1253     * voice call to the bluetooth headset.
1254     * After API version {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2} only a raw SCO audio
1255     * connection is established.
1256     * <ul>
1257     *   <li> the stream type must be {@link #STREAM_VOICE_CALL} </li>
1258     *   <li> the format must be mono </li>
1259     *   <li> the sampling must be 16kHz or 8kHz </li>
1260     * </ul>
1261     * <p>The following restrictions apply on input streams:
1262     * <ul>
1263     *   <li> the format must be mono </li>
1264     *   <li> the sampling must be 8kHz </li>
1265     * </ul>
1266     * <p>Note that the phone application always has the priority on the usage of the SCO
1267     * connection for telephony. If this method is called while the phone is in call
1268     * it will be ignored. Similarly, if a call is received or sent while an application
1269     * is using the SCO connection, the connection will be lost for the application and NOT
1270     * returned automatically when the call ends.
1271     * @see #stopBluetoothSco()
1272     * @see #ACTION_SCO_AUDIO_STATE_UPDATED
1273     */
1274    public void startBluetoothSco(){
1275        IAudioService service = getService();
1276        try {
1277            service.startBluetoothSco(mICallBack, mContext.getApplicationInfo().targetSdkVersion);
1278        } catch (RemoteException e) {
1279            Log.e(TAG, "Dead object in startBluetoothSco", e);
1280        }
1281    }
1282
1283    /**
1284     * Stop bluetooth SCO audio connection.
1285     * <p>Requires Permission:
1286     *   {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}.
1287     * <p>This method must be called by applications having requested the use of
1288     * bluetooth SCO audio with {@link #startBluetoothSco()}
1289     * when finished with the SCO connection or if connection fails.
1290     * @see #startBluetoothSco()
1291     */
1292    public void stopBluetoothSco(){
1293        IAudioService service = getService();
1294        try {
1295            service.stopBluetoothSco(mICallBack);
1296        } catch (RemoteException e) {
1297            Log.e(TAG, "Dead object in stopBluetoothSco", e);
1298        }
1299    }
1300
1301    /**
1302     * Request use of Bluetooth SCO headset for communications.
1303     * <p>
1304     * This method should only be used by applications that replace the platform-wide
1305     * management of audio settings or the main telephony application.
1306     *
1307     * @param on set <var>true</var> to use bluetooth SCO for communications;
1308     *               <var>false</var> to not use bluetooth SCO for communications
1309     */
1310    public void setBluetoothScoOn(boolean on){
1311        IAudioService service = getService();
1312        try {
1313            service.setBluetoothScoOn(on);
1314        } catch (RemoteException e) {
1315            Log.e(TAG, "Dead object in setBluetoothScoOn", e);
1316        }
1317    }
1318
1319    /**
1320     * Checks whether communications use Bluetooth SCO.
1321     *
1322     * @return true if SCO is used for communications;
1323     *         false if otherwise
1324     */
1325    public boolean isBluetoothScoOn() {
1326        IAudioService service = getService();
1327        try {
1328            return service.isBluetoothScoOn();
1329        } catch (RemoteException e) {
1330            Log.e(TAG, "Dead object in isBluetoothScoOn", e);
1331            return false;
1332        }
1333    }
1334
1335    /**
1336     * @param on set <var>true</var> to route A2DP audio to/from Bluetooth
1337     *           headset; <var>false</var> disable A2DP audio
1338     * @deprecated Do not use.
1339     */
1340    @Deprecated public void setBluetoothA2dpOn(boolean on){
1341    }
1342
1343    /**
1344     * Checks whether A2DP audio routing to the Bluetooth headset is on or off.
1345     *
1346     * @return true if A2DP audio is being routed to/from Bluetooth headset;
1347     *         false if otherwise
1348     */
1349    public boolean isBluetoothA2dpOn() {
1350        if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_BLUETOOTH_A2DP,"")
1351            == AudioSystem.DEVICE_STATE_UNAVAILABLE) {
1352            return false;
1353        } else {
1354            return true;
1355        }
1356    }
1357
1358    /**
1359     * Sets audio routing to the wired headset on or off.
1360     *
1361     * @param on set <var>true</var> to route audio to/from wired
1362     *           headset; <var>false</var> disable wired headset audio
1363     * @deprecated Do not use.
1364     */
1365    @Deprecated public void setWiredHeadsetOn(boolean on){
1366    }
1367
1368    /**
1369     * Checks whether a wired headset is connected or not.
1370     * <p>This is not a valid indication that audio playback is
1371     * actually over the wired headset as audio routing depends on other conditions.
1372     *
1373     * @return true if a wired headset is connected.
1374     *         false if otherwise
1375     * @deprecated Use only to check is a headset is connected or not.
1376     */
1377    public boolean isWiredHeadsetOn() {
1378        if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADSET,"")
1379                == AudioSystem.DEVICE_STATE_UNAVAILABLE &&
1380            AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADPHONE,"")
1381                == AudioSystem.DEVICE_STATE_UNAVAILABLE) {
1382            return false;
1383        } else {
1384            return true;
1385        }
1386    }
1387
1388    /**
1389     * Sets the microphone mute on or off.
1390     * <p>
1391     * This method should only be used by applications that replace the platform-wide
1392     * management of audio settings or the main telephony application.
1393     *
1394     * @param on set <var>true</var> to mute the microphone;
1395     *           <var>false</var> to turn mute off
1396     */
1397    public void setMicrophoneMute(boolean on){
1398        AudioSystem.muteMicrophone(on);
1399    }
1400
1401    /**
1402     * Checks whether the microphone mute is on or off.
1403     *
1404     * @return true if microphone is muted, false if it's not
1405     */
1406    public boolean isMicrophoneMute() {
1407        return AudioSystem.isMicrophoneMuted();
1408    }
1409
1410    /**
1411     * Sets the audio mode.
1412     * <p>
1413     * The audio mode encompasses audio routing AND the behavior of
1414     * the telephony layer. Therefore this method should only be used by applications that
1415     * replace the platform-wide management of audio settings or the main telephony application.
1416     * In particular, the {@link #MODE_IN_CALL} mode should only be used by the telephony
1417     * application when it places a phone call, as it will cause signals from the radio layer
1418     * to feed the platform mixer.
1419     *
1420     * @param mode  the requested audio mode ({@link #MODE_NORMAL}, {@link #MODE_RINGTONE},
1421     *              {@link #MODE_IN_CALL} or {@link #MODE_IN_COMMUNICATION}).
1422     *              Informs the HAL about the current audio state so that
1423     *              it can route the audio appropriately.
1424     */
1425    public void setMode(int mode) {
1426        IAudioService service = getService();
1427        try {
1428            service.setMode(mode, mICallBack);
1429        } catch (RemoteException e) {
1430            Log.e(TAG, "Dead object in setMode", e);
1431        }
1432    }
1433
1434    /**
1435     * Returns the current audio mode.
1436     *
1437     * @return      the current audio mode ({@link #MODE_NORMAL}, {@link #MODE_RINGTONE},
1438     *              {@link #MODE_IN_CALL} or {@link #MODE_IN_COMMUNICATION}).
1439     *              Returns the current current audio state from the HAL.
1440     */
1441    public int getMode() {
1442        IAudioService service = getService();
1443        try {
1444            return service.getMode();
1445        } catch (RemoteException e) {
1446            Log.e(TAG, "Dead object in getMode", e);
1447            return MODE_INVALID;
1448        }
1449    }
1450
1451    /* modes for setMode/getMode/setRoute/getRoute */
1452    /**
1453     * Audio harware modes.
1454     */
1455    /**
1456     * Invalid audio mode.
1457     */
1458    public static final int MODE_INVALID            = AudioSystem.MODE_INVALID;
1459    /**
1460     * Current audio mode. Used to apply audio routing to current mode.
1461     */
1462    public static final int MODE_CURRENT            = AudioSystem.MODE_CURRENT;
1463    /**
1464     * Normal audio mode: not ringing and no call established.
1465     */
1466    public static final int MODE_NORMAL             = AudioSystem.MODE_NORMAL;
1467    /**
1468     * Ringing audio mode. An incoming is being signaled.
1469     */
1470    public static final int MODE_RINGTONE           = AudioSystem.MODE_RINGTONE;
1471    /**
1472     * In call audio mode. A telephony call is established.
1473     */
1474    public static final int MODE_IN_CALL            = AudioSystem.MODE_IN_CALL;
1475    /**
1476     * In communication audio mode. An audio/video chat or VoIP call is established.
1477     */
1478    public static final int MODE_IN_COMMUNICATION   = AudioSystem.MODE_IN_COMMUNICATION;
1479
1480    /* Routing bits for setRouting/getRouting API */
1481    /**
1482     * Routing audio output to earpiece
1483     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
1484     * setBluetoothScoOn() methods instead.
1485     */
1486    @Deprecated public static final int ROUTE_EARPIECE          = AudioSystem.ROUTE_EARPIECE;
1487    /**
1488     * Routing audio output to speaker
1489     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
1490     * setBluetoothScoOn() methods instead.
1491     */
1492    @Deprecated public static final int ROUTE_SPEAKER           = AudioSystem.ROUTE_SPEAKER;
1493    /**
1494     * @deprecated use {@link #ROUTE_BLUETOOTH_SCO}
1495     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
1496     * setBluetoothScoOn() methods instead.
1497     */
1498    @Deprecated public static final int ROUTE_BLUETOOTH = AudioSystem.ROUTE_BLUETOOTH_SCO;
1499    /**
1500     * Routing audio output to bluetooth SCO
1501     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
1502     * setBluetoothScoOn() methods instead.
1503     */
1504    @Deprecated public static final int ROUTE_BLUETOOTH_SCO     = AudioSystem.ROUTE_BLUETOOTH_SCO;
1505    /**
1506     * Routing audio output to headset
1507     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
1508     * setBluetoothScoOn() methods instead.
1509     */
1510    @Deprecated public static final int ROUTE_HEADSET           = AudioSystem.ROUTE_HEADSET;
1511    /**
1512     * Routing audio output to bluetooth A2DP
1513     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
1514     * setBluetoothScoOn() methods instead.
1515     */
1516    @Deprecated public static final int ROUTE_BLUETOOTH_A2DP    = AudioSystem.ROUTE_BLUETOOTH_A2DP;
1517    /**
1518     * Used for mask parameter of {@link #setRouting(int,int,int)}.
1519     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
1520     * setBluetoothScoOn() methods instead.
1521     */
1522    @Deprecated public static final int ROUTE_ALL               = AudioSystem.ROUTE_ALL;
1523
1524    /**
1525     * Sets the audio routing for a specified mode
1526     *
1527     * @param mode   audio mode to change route. E.g., MODE_RINGTONE.
1528     * @param routes bit vector of routes requested, created from one or
1529     *               more of ROUTE_xxx types. Set bits indicate that route should be on
1530     * @param mask   bit vector of routes to change, created from one or more of
1531     * ROUTE_xxx types. Unset bits indicate the route should be left unchanged
1532     *
1533     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
1534     * setBluetoothScoOn() methods instead.
1535     */
1536    @Deprecated
1537    public void setRouting(int mode, int routes, int mask) {
1538    }
1539
1540    /**
1541     * Returns the current audio routing bit vector for a specified mode.
1542     *
1543     * @param mode audio mode to get route (e.g., MODE_RINGTONE)
1544     * @return an audio route bit vector that can be compared with ROUTE_xxx
1545     * bits
1546     * @deprecated   Do not query audio routing directly, use isSpeakerphoneOn(),
1547     * isBluetoothScoOn(), isBluetoothA2dpOn() and isWiredHeadsetOn() methods instead.
1548     */
1549    @Deprecated
1550    public int getRouting(int mode) {
1551        return -1;
1552    }
1553
1554    /**
1555     * Checks whether any music is active.
1556     *
1557     * @return true if any music tracks are active.
1558     */
1559    public boolean isMusicActive() {
1560        return AudioSystem.isStreamActive(STREAM_MUSIC, 0);
1561    }
1562
1563    /**
1564     * @hide
1565     * Checks whether any music or media is actively playing on a remote device (e.g. wireless
1566     *   display). Note that BT audio sinks are not considered remote devices.
1567     * @return true if {@link AudioManager#STREAM_MUSIC} is active on a remote device
1568     */
1569    public boolean isMusicActiveRemotely() {
1570        return AudioSystem.isStreamActiveRemotely(STREAM_MUSIC, 0);
1571    }
1572
1573    /**
1574     * @hide
1575     * Checks whether the current audio focus is exclusive.
1576     * @return true if the top of the audio focus stack requested focus
1577     *     with {@link #AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE}
1578     */
1579    public boolean isAudioFocusExclusive() {
1580        IAudioService service = getService();
1581        try {
1582            return service.getCurrentAudioFocus() == AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE;
1583        } catch (RemoteException e) {
1584            Log.e(TAG, "Dead object in isAudioFocusExclusive()", e);
1585            return false;
1586        }
1587    }
1588
1589    /**
1590     * @hide
1591     * If the stream is active locally or remotely, adjust its volume according to the enforced
1592     * priority rules.
1593     * Note: only AudioManager.STREAM_MUSIC is supported at the moment
1594     */
1595    public void adjustLocalOrRemoteStreamVolume(int streamType, int direction) {
1596        if (streamType != STREAM_MUSIC) {
1597            Log.w(TAG, "adjustLocalOrRemoteStreamVolume() doesn't support stream " + streamType);
1598        }
1599        IAudioService service = getService();
1600        try {
1601            service.adjustLocalOrRemoteStreamVolume(streamType, direction,
1602                    mContext.getOpPackageName());
1603        } catch (RemoteException e) {
1604            Log.e(TAG, "Dead object in adjustLocalOrRemoteStreamVolume", e);
1605        }
1606    }
1607
1608    /*
1609     * Sets a generic audio configuration parameter. The use of these parameters
1610     * are platform dependant, see libaudio
1611     *
1612     * ** Temporary interface - DO NOT USE
1613     *
1614     * TODO: Replace with a more generic key:value get/set mechanism
1615     *
1616     * param key   name of parameter to set. Must not be null.
1617     * param value value of parameter. Must not be null.
1618     */
1619    /**
1620     * @hide
1621     * @deprecated Use {@link #setParameters(String)} instead
1622     */
1623    @Deprecated public void setParameter(String key, String value) {
1624        setParameters(key+"="+value);
1625    }
1626
1627    /**
1628     * Sets a variable number of parameter values to audio hardware.
1629     *
1630     * @param keyValuePairs list of parameters key value pairs in the form:
1631     *    key1=value1;key2=value2;...
1632     *
1633     */
1634    public void setParameters(String keyValuePairs) {
1635        AudioSystem.setParameters(keyValuePairs);
1636    }
1637
1638    /**
1639     * Gets a variable number of parameter values from audio hardware.
1640     *
1641     * @param keys list of parameters
1642     * @return list of parameters key value pairs in the form:
1643     *    key1=value1;key2=value2;...
1644     */
1645    public String getParameters(String keys) {
1646        return AudioSystem.getParameters(keys);
1647    }
1648
1649    /* Sound effect identifiers */
1650    /**
1651     * Keyboard and direction pad click sound
1652     * @see #playSoundEffect(int)
1653     */
1654    public static final int FX_KEY_CLICK = 0;
1655    /**
1656     * Focus has moved up
1657     * @see #playSoundEffect(int)
1658     */
1659    public static final int FX_FOCUS_NAVIGATION_UP = 1;
1660    /**
1661     * Focus has moved down
1662     * @see #playSoundEffect(int)
1663     */
1664    public static final int FX_FOCUS_NAVIGATION_DOWN = 2;
1665    /**
1666     * Focus has moved left
1667     * @see #playSoundEffect(int)
1668     */
1669    public static final int FX_FOCUS_NAVIGATION_LEFT = 3;
1670    /**
1671     * Focus has moved right
1672     * @see #playSoundEffect(int)
1673     */
1674    public static final int FX_FOCUS_NAVIGATION_RIGHT = 4;
1675    /**
1676     * IME standard keypress sound
1677     * @see #playSoundEffect(int)
1678     */
1679    public static final int FX_KEYPRESS_STANDARD = 5;
1680    /**
1681     * IME spacebar keypress sound
1682     * @see #playSoundEffect(int)
1683     */
1684    public static final int FX_KEYPRESS_SPACEBAR = 6;
1685    /**
1686     * IME delete keypress sound
1687     * @see #playSoundEffect(int)
1688     */
1689    public static final int FX_KEYPRESS_DELETE = 7;
1690    /**
1691     * IME return_keypress sound
1692     * @see #playSoundEffect(int)
1693     */
1694    public static final int FX_KEYPRESS_RETURN = 8;
1695
1696    /**
1697     * Invalid keypress sound
1698     * @see #playSoundEffect(int)
1699     */
1700    public static final int FX_KEYPRESS_INVALID = 9;
1701    /**
1702     * @hide Number of sound effects
1703     */
1704    public static final int NUM_SOUND_EFFECTS = 10;
1705
1706    /**
1707     * Plays a sound effect (Key clicks, lid open/close...)
1708     * @param effectType The type of sound effect. One of
1709     *            {@link #FX_KEY_CLICK},
1710     *            {@link #FX_FOCUS_NAVIGATION_UP},
1711     *            {@link #FX_FOCUS_NAVIGATION_DOWN},
1712     *            {@link #FX_FOCUS_NAVIGATION_LEFT},
1713     *            {@link #FX_FOCUS_NAVIGATION_RIGHT},
1714     *            {@link #FX_KEYPRESS_STANDARD},
1715     *            {@link #FX_KEYPRESS_SPACEBAR},
1716     *            {@link #FX_KEYPRESS_DELETE},
1717     *            {@link #FX_KEYPRESS_RETURN},
1718     *            {@link #FX_KEYPRESS_INVALID},
1719     * NOTE: This version uses the UI settings to determine
1720     * whether sounds are heard or not.
1721     */
1722    public void  playSoundEffect(int effectType) {
1723        if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) {
1724            return;
1725        }
1726
1727        if (!querySoundEffectsEnabled()) {
1728            return;
1729        }
1730
1731        IAudioService service = getService();
1732        try {
1733            service.playSoundEffect(effectType);
1734        } catch (RemoteException e) {
1735            Log.e(TAG, "Dead object in playSoundEffect"+e);
1736        }
1737    }
1738
1739    /**
1740     * Plays a sound effect (Key clicks, lid open/close...)
1741     * @param effectType The type of sound effect. One of
1742     *            {@link #FX_KEY_CLICK},
1743     *            {@link #FX_FOCUS_NAVIGATION_UP},
1744     *            {@link #FX_FOCUS_NAVIGATION_DOWN},
1745     *            {@link #FX_FOCUS_NAVIGATION_LEFT},
1746     *            {@link #FX_FOCUS_NAVIGATION_RIGHT},
1747     *            {@link #FX_KEYPRESS_STANDARD},
1748     *            {@link #FX_KEYPRESS_SPACEBAR},
1749     *            {@link #FX_KEYPRESS_DELETE},
1750     *            {@link #FX_KEYPRESS_RETURN},
1751     *            {@link #FX_KEYPRESS_INVALID},
1752     * @param volume Sound effect volume.
1753     * The volume value is a raw scalar so UI controls should be scaled logarithmically.
1754     * If a volume of -1 is specified, the AudioManager.STREAM_MUSIC stream volume minus 3dB will be used.
1755     * NOTE: This version is for applications that have their own
1756     * settings panel for enabling and controlling volume.
1757     */
1758    public void  playSoundEffect(int effectType, float volume) {
1759        if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) {
1760            return;
1761        }
1762
1763        IAudioService service = getService();
1764        try {
1765            service.playSoundEffectVolume(effectType, volume);
1766        } catch (RemoteException e) {
1767            Log.e(TAG, "Dead object in playSoundEffect"+e);
1768        }
1769    }
1770
1771    /**
1772     * Settings has an in memory cache, so this is fast.
1773     */
1774    private boolean querySoundEffectsEnabled() {
1775        return Settings.System.getInt(mContext.getContentResolver(), Settings.System.SOUND_EFFECTS_ENABLED, 0) != 0;
1776    }
1777
1778
1779    /**
1780     *  Load Sound effects.
1781     *  This method must be called when sound effects are enabled.
1782     */
1783    public void loadSoundEffects() {
1784        IAudioService service = getService();
1785        try {
1786            service.loadSoundEffects();
1787        } catch (RemoteException e) {
1788            Log.e(TAG, "Dead object in loadSoundEffects"+e);
1789        }
1790    }
1791
1792    /**
1793     *  Unload Sound effects.
1794     *  This method can be called to free some memory when
1795     *  sound effects are disabled.
1796     */
1797    public void unloadSoundEffects() {
1798        IAudioService service = getService();
1799        try {
1800            service.unloadSoundEffects();
1801        } catch (RemoteException e) {
1802            Log.e(TAG, "Dead object in unloadSoundEffects"+e);
1803        }
1804    }
1805
1806    /**
1807     * @hide
1808     * Used to indicate no audio focus has been gained or lost.
1809     */
1810    public static final int AUDIOFOCUS_NONE = 0;
1811
1812    /**
1813     * Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration.
1814     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
1815     * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
1816     */
1817    public static final int AUDIOFOCUS_GAIN = 1;
1818    /**
1819     * Used to indicate a temporary gain or request of audio focus, anticipated to last a short
1820     * amount of time. Examples of temporary changes are the playback of driving directions, or an
1821     * event notification.
1822     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
1823     * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
1824     */
1825    public static final int AUDIOFOCUS_GAIN_TRANSIENT = 2;
1826    /**
1827     * Used to indicate a temporary request of audio focus, anticipated to last a short
1828     * amount of time, and where it is acceptable for other audio applications to keep playing
1829     * after having lowered their output level (also referred to as "ducking").
1830     * Examples of temporary changes are the playback of driving directions where playback of music
1831     * in the background is acceptable.
1832     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
1833     * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
1834     */
1835    public static final int AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = 3;
1836    /**
1837     * Used to indicate a temporary request of audio focus, anticipated to last a short
1838     * amount of time, during which no other applications, or system components, should play
1839     * anything. Examples of exclusive and transient audio focus requests are voice
1840     * memo recording and speech recognition, during which the system shouldn't play any
1841     * notifications, and media playback should have paused.
1842     * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
1843     */
1844    public static final int AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = 4;
1845    /**
1846     * Used to indicate a loss of audio focus of unknown duration.
1847     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
1848     */
1849    public static final int AUDIOFOCUS_LOSS = -1 * AUDIOFOCUS_GAIN;
1850    /**
1851     * Used to indicate a transient loss of audio focus.
1852     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
1853     */
1854    public static final int AUDIOFOCUS_LOSS_TRANSIENT = -1 * AUDIOFOCUS_GAIN_TRANSIENT;
1855    /**
1856     * Used to indicate a transient loss of audio focus where the loser of the audio focus can
1857     * lower its output volume if it wants to continue playing (also referred to as "ducking"), as
1858     * the new focus owner doesn't require others to be silent.
1859     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
1860     */
1861    public static final int AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK =
1862            -1 * AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK;
1863
1864    /**
1865     * Interface definition for a callback to be invoked when the audio focus of the system is
1866     * updated.
1867     */
1868    public interface OnAudioFocusChangeListener {
1869        /**
1870         * Called on the listener to notify it the audio focus for this listener has been changed.
1871         * The focusChange value indicates whether the focus was gained,
1872         * whether the focus was lost, and whether that loss is transient, or whether the new focus
1873         * holder will hold it for an unknown amount of time.
1874         * When losing focus, listeners can use the focus change information to decide what
1875         * behavior to adopt when losing focus. A music player could for instance elect to lower
1876         * the volume of its music stream (duck) for transient focus losses, and pause otherwise.
1877         * @param focusChange the type of focus change, one of {@link AudioManager#AUDIOFOCUS_GAIN},
1878         *   {@link AudioManager#AUDIOFOCUS_LOSS}, {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT}
1879         *   and {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK}.
1880         */
1881        public void onAudioFocusChange(int focusChange);
1882    }
1883
1884    /**
1885     * Map to convert focus event listener IDs, as used in the AudioService audio focus stack,
1886     * to actual listener objects.
1887     */
1888    private final HashMap<String, OnAudioFocusChangeListener> mAudioFocusIdListenerMap =
1889            new HashMap<String, OnAudioFocusChangeListener>();
1890    /**
1891     * Lock to prevent concurrent changes to the list of focus listeners for this AudioManager
1892     * instance.
1893     */
1894    private final Object mFocusListenerLock = new Object();
1895
1896    private OnAudioFocusChangeListener findFocusListener(String id) {
1897        return mAudioFocusIdListenerMap.get(id);
1898    }
1899
1900    /**
1901     * Handler for audio focus events coming from the audio service.
1902     */
1903    private final FocusEventHandlerDelegate mAudioFocusEventHandlerDelegate =
1904            new FocusEventHandlerDelegate();
1905
1906    /**
1907     * Helper class to handle the forwarding of audio focus events to the appropriate listener
1908     */
1909    private class FocusEventHandlerDelegate {
1910        private final Handler mHandler;
1911
1912        FocusEventHandlerDelegate() {
1913            Looper looper;
1914            if ((looper = Looper.myLooper()) == null) {
1915                looper = Looper.getMainLooper();
1916            }
1917
1918            if (looper != null) {
1919                // implement the event handler delegate to receive audio focus events
1920                mHandler = new Handler(looper) {
1921                    @Override
1922                    public void handleMessage(Message msg) {
1923                        OnAudioFocusChangeListener listener = null;
1924                        synchronized(mFocusListenerLock) {
1925                            listener = findFocusListener((String)msg.obj);
1926                        }
1927                        if (listener != null) {
1928                            listener.onAudioFocusChange(msg.what);
1929                        }
1930                    }
1931                };
1932            } else {
1933                mHandler = null;
1934            }
1935        }
1936
1937        Handler getHandler() {
1938            return mHandler;
1939        }
1940    }
1941
1942    private final IAudioFocusDispatcher mAudioFocusDispatcher = new IAudioFocusDispatcher.Stub() {
1943
1944        public void dispatchAudioFocusChange(int focusChange, String id) {
1945            Message m = mAudioFocusEventHandlerDelegate.getHandler().obtainMessage(focusChange, id);
1946            mAudioFocusEventHandlerDelegate.getHandler().sendMessage(m);
1947        }
1948
1949    };
1950
1951    private String getIdForAudioFocusListener(OnAudioFocusChangeListener l) {
1952        if (l == null) {
1953            return new String(this.toString());
1954        } else {
1955            return new String(this.toString() + l.toString());
1956        }
1957    }
1958
1959    /**
1960     * @hide
1961     * Registers a listener to be called when audio focus changes. Calling this method is optional
1962     * before calling {@link #requestAudioFocus(OnAudioFocusChangeListener, int, int)}, as it
1963     * will register the listener as well if it wasn't registered already.
1964     * @param l the listener to be notified of audio focus changes.
1965     */
1966    public void registerAudioFocusListener(OnAudioFocusChangeListener l) {
1967        synchronized(mFocusListenerLock) {
1968            if (mAudioFocusIdListenerMap.containsKey(getIdForAudioFocusListener(l))) {
1969                return;
1970            }
1971            mAudioFocusIdListenerMap.put(getIdForAudioFocusListener(l), l);
1972        }
1973    }
1974
1975    /**
1976     * @hide
1977     * Causes the specified listener to not be called anymore when focus is gained or lost.
1978     * @param l the listener to unregister.
1979     */
1980    public void unregisterAudioFocusListener(OnAudioFocusChangeListener l) {
1981
1982        // remove locally
1983        synchronized(mFocusListenerLock) {
1984            mAudioFocusIdListenerMap.remove(getIdForAudioFocusListener(l));
1985        }
1986    }
1987
1988
1989    /**
1990     * A failed focus change request.
1991     */
1992    public static final int AUDIOFOCUS_REQUEST_FAILED = 0;
1993    /**
1994     * A successful focus change request.
1995     */
1996    public static final int AUDIOFOCUS_REQUEST_GRANTED = 1;
1997
1998
1999    /**
2000     *  Request audio focus.
2001     *  Send a request to obtain the audio focus
2002     *  @param l the listener to be notified of audio focus changes
2003     *  @param streamType the main audio stream type affected by the focus request
2004     *  @param durationHint use {@link #AUDIOFOCUS_GAIN_TRANSIENT} to indicate this focus request
2005     *      is temporary, and focus will be abandonned shortly. Examples of transient requests are
2006     *      for the playback of driving directions, or notifications sounds.
2007     *      Use {@link #AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK} to indicate also that it's ok for
2008     *      the previous focus owner to keep playing if it ducks its audio output.
2009     *      Alternatively use {@link #AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE} for a temporary request
2010     *      that benefits from the system not playing disruptive sounds like notifications, for
2011     *      usecases such as voice memo recording, or speech recognition.
2012     *      Use {@link #AUDIOFOCUS_GAIN} for a focus request of unknown duration such
2013     *      as the playback of a song or a video.
2014     *  @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED}
2015     */
2016    public int requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint) {
2017        int status = AUDIOFOCUS_REQUEST_FAILED;
2018        if ((durationHint < AUDIOFOCUS_GAIN) ||
2019                (durationHint > AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)) {
2020            Log.e(TAG, "Invalid duration hint, audio focus request denied");
2021            return status;
2022        }
2023        registerAudioFocusListener(l);
2024        //TODO protect request by permission check?
2025        IAudioService service = getService();
2026        try {
2027            status = service.requestAudioFocus(streamType, durationHint, mICallBack,
2028                    mAudioFocusDispatcher, getIdForAudioFocusListener(l),
2029                    mContext.getOpPackageName() /* package name */);
2030        } catch (RemoteException e) {
2031            Log.e(TAG, "Can't call requestAudioFocus() on AudioService due to "+e);
2032        }
2033        return status;
2034    }
2035
2036    /**
2037     * @hide
2038     * Used internally by telephony package to request audio focus. Will cause the focus request
2039     * to be associated with the "voice communication" identifier only used in AudioService
2040     * to identify this use case.
2041     * @param streamType use STREAM_RING for focus requests when ringing, VOICE_CALL for
2042     *    the establishment of the call
2043     * @param durationHint the type of focus request. AUDIOFOCUS_GAIN_TRANSIENT is recommended so
2044     *    media applications resume after a call
2045     */
2046    public void requestAudioFocusForCall(int streamType, int durationHint) {
2047        IAudioService service = getService();
2048        try {
2049            service.requestAudioFocus(streamType, durationHint, mICallBack, null,
2050                    MediaFocusControl.IN_VOICE_COMM_FOCUS_ID,
2051                    mContext.getOpPackageName());
2052        } catch (RemoteException e) {
2053            Log.e(TAG, "Can't call requestAudioFocusForCall() on AudioService due to "+e);
2054        }
2055    }
2056
2057    /**
2058     * @hide
2059     * Used internally by telephony package to abandon audio focus, typically after a call or
2060     * when ringing ends and the call is rejected or not answered.
2061     * Should match one or more calls to {@link #requestAudioFocusForCall(int, int)}.
2062     */
2063    public void abandonAudioFocusForCall() {
2064        IAudioService service = getService();
2065        try {
2066            service.abandonAudioFocus(null, MediaFocusControl.IN_VOICE_COMM_FOCUS_ID);
2067        } catch (RemoteException e) {
2068            Log.e(TAG, "Can't call abandonAudioFocusForCall() on AudioService due to "+e);
2069        }
2070    }
2071
2072    /**
2073     *  Abandon audio focus. Causes the previous focus owner, if any, to receive focus.
2074     *  @param l the listener with which focus was requested.
2075     *  @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED}
2076     */
2077    public int abandonAudioFocus(OnAudioFocusChangeListener l) {
2078        int status = AUDIOFOCUS_REQUEST_FAILED;
2079        unregisterAudioFocusListener(l);
2080        IAudioService service = getService();
2081        try {
2082            status = service.abandonAudioFocus(mAudioFocusDispatcher,
2083                    getIdForAudioFocusListener(l));
2084        } catch (RemoteException e) {
2085            Log.e(TAG, "Can't call abandonAudioFocus() on AudioService due to "+e);
2086        }
2087        return status;
2088    }
2089
2090
2091    //====================================================================
2092    // Remote Control
2093    /**
2094     * Register a component to be the sole receiver of MEDIA_BUTTON intents.
2095     * @param eventReceiver identifier of a {@link android.content.BroadcastReceiver}
2096     *      that will receive the media button intent. This broadcast receiver must be declared
2097     *      in the application manifest. The package of the component must match that of
2098     *      the context you're registering from.
2099     */
2100    public void registerMediaButtonEventReceiver(ComponentName eventReceiver) {
2101        if (eventReceiver == null) {
2102            return;
2103        }
2104        if (!eventReceiver.getPackageName().equals(mContext.getPackageName())) {
2105            Log.e(TAG, "registerMediaButtonEventReceiver() error: " +
2106                    "receiver and context package names don't match");
2107            return;
2108        }
2109        // construct a PendingIntent for the media button and register it
2110        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
2111        //     the associated intent will be handled by the component being registered
2112        mediaButtonIntent.setComponent(eventReceiver);
2113        PendingIntent pi = PendingIntent.getBroadcast(mContext,
2114                0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/);
2115        registerMediaButtonIntent(pi, eventReceiver);
2116    }
2117
2118    /**
2119     * Register a component to be the sole receiver of MEDIA_BUTTON intents.  This is like
2120     * {@link #registerMediaButtonEventReceiver(android.content.ComponentName)}, but allows
2121     * the buttons to go to any PendingIntent.  Note that you should only use this form if
2122     * you know you will continue running for the full time until unregistering the
2123     * PendingIntent.
2124     * @param eventReceiver target that will receive media button intents.  The PendingIntent
2125     * will be sent as-is when a media button action occurs, with {@link Intent#EXTRA_KEY_EVENT}
2126     * added and holding the key code of the media button that was pressed.
2127     */
2128    public void registerMediaButtonEventReceiver(PendingIntent eventReceiver) {
2129        if (eventReceiver == null) {
2130            return;
2131        }
2132        registerMediaButtonIntent(eventReceiver, null);
2133    }
2134
2135    /**
2136     * @hide
2137     * no-op if (pi == null) or (eventReceiver == null)
2138     */
2139    public void registerMediaButtonIntent(PendingIntent pi, ComponentName eventReceiver) {
2140        if (pi == null) {
2141            Log.e(TAG, "Cannot call registerMediaButtonIntent() with a null parameter");
2142            return;
2143        }
2144        IAudioService service = getService();
2145        try {
2146            // pi != null
2147            service.registerMediaButtonIntent(pi, eventReceiver,
2148                    eventReceiver == null ? mToken : null);
2149        } catch (RemoteException e) {
2150            Log.e(TAG, "Dead object in registerMediaButtonIntent"+e);
2151        }
2152    }
2153
2154    /**
2155     * @hide
2156     * Used internally by telephony package to register an intent receiver for ACTION_MEDIA_BUTTON.
2157     * @param eventReceiver the component that will receive the media button key events,
2158     *          no-op if eventReceiver is null
2159     */
2160    public void registerMediaButtonEventReceiverForCalls(ComponentName eventReceiver) {
2161        if (eventReceiver == null) {
2162            return;
2163        }
2164        IAudioService service = getService();
2165        try {
2166            // eventReceiver != null
2167            service.registerMediaButtonEventReceiverForCalls(eventReceiver);
2168        } catch (RemoteException e) {
2169            Log.e(TAG, "Dead object in registerMediaButtonEventReceiverForCalls", e);
2170        }
2171    }
2172
2173    /**
2174     * @hide
2175     */
2176    public void unregisterMediaButtonEventReceiverForCalls() {
2177        IAudioService service = getService();
2178        try {
2179            service.unregisterMediaButtonEventReceiverForCalls();
2180        } catch (RemoteException e) {
2181            Log.e(TAG, "Dead object in unregisterMediaButtonEventReceiverForCalls", e);
2182        }
2183    }
2184
2185    /**
2186     * Unregister the receiver of MEDIA_BUTTON intents.
2187     * @param eventReceiver identifier of a {@link android.content.BroadcastReceiver}
2188     *      that was registered with {@link #registerMediaButtonEventReceiver(ComponentName)}.
2189     */
2190    public void unregisterMediaButtonEventReceiver(ComponentName eventReceiver) {
2191        if (eventReceiver == null) {
2192            return;
2193        }
2194        // construct a PendingIntent for the media button and unregister it
2195        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
2196        //     the associated intent will be handled by the component being registered
2197        mediaButtonIntent.setComponent(eventReceiver);
2198        PendingIntent pi = PendingIntent.getBroadcast(mContext,
2199                0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/);
2200        unregisterMediaButtonIntent(pi);
2201    }
2202
2203    /**
2204     * Unregister the receiver of MEDIA_BUTTON intents.
2205     * @param eventReceiver same PendingIntent that was registed with
2206     *      {@link #registerMediaButtonEventReceiver(PendingIntent)}.
2207     */
2208    public void unregisterMediaButtonEventReceiver(PendingIntent eventReceiver) {
2209        if (eventReceiver == null) {
2210            return;
2211        }
2212        unregisterMediaButtonIntent(eventReceiver);
2213    }
2214
2215    /**
2216     * @hide
2217     */
2218    public void unregisterMediaButtonIntent(PendingIntent pi) {
2219        IAudioService service = getService();
2220        try {
2221            service.unregisterMediaButtonIntent(pi);
2222        } catch (RemoteException e) {
2223            Log.e(TAG, "Dead object in unregisterMediaButtonIntent"+e);
2224        }
2225    }
2226
2227    /**
2228     * Registers the remote control client for providing information to display on the remote
2229     * controls.
2230     * @param rcClient The remote control client from which remote controls will receive
2231     *      information to display.
2232     * @see RemoteControlClient
2233     */
2234    public void registerRemoteControlClient(RemoteControlClient rcClient) {
2235        if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) {
2236            return;
2237        }
2238        IAudioService service = getService();
2239        try {
2240            int rcseId = service.registerRemoteControlClient(
2241                    rcClient.getRcMediaIntent(),       /* mediaIntent   */
2242                    rcClient.getIRemoteControlClient(),/* rcClient      */
2243                    // used to match media button event receiver and audio focus
2244                    mContext.getPackageName());        /* packageName   */
2245            rcClient.setRcseId(rcseId);
2246        } catch (RemoteException e) {
2247            Log.e(TAG, "Dead object in registerRemoteControlClient"+e);
2248        }
2249    }
2250
2251    /**
2252     * Unregisters the remote control client that was providing information to display on the
2253     * remote controls.
2254     * @param rcClient The remote control client to unregister.
2255     * @see #registerRemoteControlClient(RemoteControlClient)
2256     */
2257    public void unregisterRemoteControlClient(RemoteControlClient rcClient) {
2258        if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) {
2259            return;
2260        }
2261        IAudioService service = getService();
2262        try {
2263            service.unregisterRemoteControlClient(rcClient.getRcMediaIntent(), /* mediaIntent   */
2264                    rcClient.getIRemoteControlClient());                       /* rcClient      */
2265        } catch (RemoteException e) {
2266            Log.e(TAG, "Dead object in unregisterRemoteControlClient"+e);
2267        }
2268    }
2269
2270    /**
2271     * Registers a {@link RemoteController} instance for it to receive media metadata updates
2272     * and playback state information from applications using {@link RemoteControlClient}, and
2273     * control their playback.
2274     * <p>Registration requires the {@link OnClientUpdateListener} listener to be one of the
2275     * enabled notification listeners (see
2276     * {@link android.service.notification.NotificationListenerService}).
2277     * @param rctlr the object to register.
2278     * @return true if the {@link RemoteController} was successfully registered, false if an
2279     *     error occurred, due to an internal system error, or insufficient permissions.
2280     */
2281    public boolean registerRemoteController(RemoteController rctlr) {
2282        if (rctlr == null) {
2283            return false;
2284        }
2285        IAudioService service = getService();
2286        final RemoteController.OnClientUpdateListener l = rctlr.getUpdateListener();
2287        final ComponentName listenerComponent = new ComponentName(mContext, l.getClass());
2288        try {
2289            int[] artworkDimensions = rctlr.getArtworkSize();
2290            boolean reg = service.registerRemoteController(rctlr.getRcDisplay(),
2291                    artworkDimensions[0]/*w*/, artworkDimensions[1]/*h*/,
2292                    listenerComponent);
2293            rctlr.setIsRegistered(reg);
2294            return reg;
2295        } catch (RemoteException e) {
2296            Log.e(TAG, "Dead object in registerRemoteController " + e);
2297            return false;
2298        }
2299    }
2300
2301    /**
2302     * Unregisters a {@link RemoteController}, causing it to no longer receive media metadata and
2303     * playback state information, and no longer be capable of controlling playback.
2304     * @param rctlr the object to unregister.
2305     */
2306    public void unregisterRemoteController(RemoteController rctlr) {
2307        if (rctlr == null) {
2308            return;
2309        }
2310        IAudioService service = getService();
2311        try {
2312            service.unregisterRemoteControlDisplay(rctlr.getRcDisplay());
2313            rctlr.setIsRegistered(false);
2314        } catch (RemoteException e) {
2315            Log.e(TAG, "Dead object in unregisterRemoteControlDisplay " + e);
2316        }
2317    }
2318
2319    /**
2320     * @hide
2321     * Registers a remote control display that will be sent information by remote control clients.
2322     * Use this method if your IRemoteControlDisplay is not going to display artwork, otherwise
2323     * use {@link #registerRemoteControlDisplay(IRemoteControlDisplay, int, int)} to pass the
2324     * artwork size directly, or
2325     * {@link #remoteControlDisplayUsesBitmapSize(IRemoteControlDisplay, int, int)} later if artwork
2326     * is not yet needed.
2327     * <p>Registration requires the {@link Manifest.permission#MEDIA_CONTENT_CONTROL} permission.
2328     * @param rcd the IRemoteControlDisplay
2329     */
2330    public void registerRemoteControlDisplay(IRemoteControlDisplay rcd) {
2331        // passing a negative value for art work width and height as they are unknown at this stage
2332        registerRemoteControlDisplay(rcd, /*w*/-1, /*h*/ -1);
2333    }
2334
2335    /**
2336     * @hide
2337     * Registers a remote control display that will be sent information by remote control clients.
2338     * <p>Registration requires the {@link Manifest.permission#MEDIA_CONTENT_CONTROL} permission.
2339     * @param rcd
2340     * @param w the maximum width of the expected bitmap. Negative values indicate it is
2341     *   useless to send artwork.
2342     * @param h the maximum height of the expected bitmap. Negative values indicate it is
2343     *   useless to send artwork.
2344     */
2345    public void registerRemoteControlDisplay(IRemoteControlDisplay rcd, int w, int h) {
2346        if (rcd == null) {
2347            return;
2348        }
2349        IAudioService service = getService();
2350        try {
2351            service.registerRemoteControlDisplay(rcd, w, h);
2352        } catch (RemoteException e) {
2353            Log.e(TAG, "Dead object in registerRemoteControlDisplay " + e);
2354        }
2355    }
2356
2357    /**
2358     * @hide
2359     * Unregisters a remote control display that was sent information by remote control clients.
2360     * @param rcd
2361     */
2362    public void unregisterRemoteControlDisplay(IRemoteControlDisplay rcd) {
2363        if (rcd == null) {
2364            return;
2365        }
2366        IAudioService service = getService();
2367        try {
2368            service.unregisterRemoteControlDisplay(rcd);
2369        } catch (RemoteException e) {
2370            Log.e(TAG, "Dead object in unregisterRemoteControlDisplay " + e);
2371        }
2372    }
2373
2374    /**
2375     * @hide
2376     * Sets the artwork size a remote control display expects when receiving bitmaps.
2377     * @param rcd
2378     * @param w the maximum width of the expected bitmap. Negative values indicate it is
2379     *   useless to send artwork.
2380     * @param h the maximum height of the expected bitmap. Negative values indicate it is
2381     *   useless to send artwork.
2382     */
2383    public void remoteControlDisplayUsesBitmapSize(IRemoteControlDisplay rcd, int w, int h) {
2384        if (rcd == null) {
2385            return;
2386        }
2387        IAudioService service = getService();
2388        try {
2389            service.remoteControlDisplayUsesBitmapSize(rcd, w, h);
2390        } catch (RemoteException e) {
2391            Log.e(TAG, "Dead object in remoteControlDisplayUsesBitmapSize " + e);
2392        }
2393    }
2394
2395    /**
2396     * @hide
2397     * Controls whether a remote control display needs periodic checks of the RemoteControlClient
2398     * playback position to verify that the estimated position has not drifted from the actual
2399     * position. By default the check is not performed.
2400     * The IRemoteControlDisplay must have been previously registered for this to have any effect.
2401     * @param rcd the IRemoteControlDisplay for which the anti-drift mechanism will be enabled
2402     *     or disabled. No effect is null.
2403     * @param wantsSync if true, RemoteControlClient instances which expose their playback position
2404     *     to the framework will regularly compare the estimated playback position with the actual
2405     *     position, and will update the IRemoteControlDisplay implementation whenever a drift is
2406     *     detected.
2407     */
2408    public void remoteControlDisplayWantsPlaybackPositionSync(IRemoteControlDisplay rcd,
2409            boolean wantsSync) {
2410        if (rcd == null) {
2411            return;
2412        }
2413        IAudioService service = getService();
2414        try {
2415            service.remoteControlDisplayWantsPlaybackPositionSync(rcd, wantsSync);
2416        } catch (RemoteException e) {
2417            Log.e(TAG, "Dead object in remoteControlDisplayWantsPlaybackPositionSync " + e);
2418        }
2419    }
2420
2421    /**
2422     * @hide
2423     * Request the user of a RemoteControlClient to seek to the given playback position.
2424     * @param generationId the RemoteControlClient generation counter for which this request is
2425     *         issued. Requests for an older generation than current one will be ignored.
2426     * @param timeMs the time in ms to seek to, must be positive.
2427     */
2428    public void setRemoteControlClientPlaybackPosition(int generationId, long timeMs) {
2429        if (timeMs < 0) {
2430            return;
2431        }
2432        IAudioService service = getService();
2433        try {
2434            service.setRemoteControlClientPlaybackPosition(generationId, timeMs);
2435        } catch (RemoteException e) {
2436            Log.e(TAG, "Dead object in setRccPlaybackPosition("+ generationId + ", "
2437                    + timeMs + ")", e);
2438        }
2439    }
2440
2441    /**
2442     * @hide
2443     * Notify the user of a RemoteControlClient that it should update its metadata with the
2444     * new value for the given key.
2445     * @param generationId the RemoteControlClient generation counter for which this request is
2446     *         issued. Requests for an older generation than current one will be ignored.
2447     * @param key the metadata key for which a new value exists
2448     * @param value the new metadata value
2449     */
2450    public void updateRemoteControlClientMetadata(int generationId, int key,
2451            Rating value) {
2452        IAudioService service = getService();
2453        try {
2454            service.updateRemoteControlClientMetadata(generationId, key, value);
2455        } catch (RemoteException e) {
2456            Log.e(TAG, "Dead object in updateRemoteControlClientMetadata("+ generationId + ", "
2457                    + key +", " + value + ")", e);
2458        }
2459    }
2460
2461    /**
2462     *  @hide
2463     *  Reload audio settings. This method is called by Settings backup
2464     *  agent when audio settings are restored and causes the AudioService
2465     *  to read and apply restored settings.
2466     */
2467    public void reloadAudioSettings() {
2468        IAudioService service = getService();
2469        try {
2470            service.reloadAudioSettings();
2471        } catch (RemoteException e) {
2472            Log.e(TAG, "Dead object in reloadAudioSettings"+e);
2473        }
2474    }
2475
2476    /**
2477     * @hide
2478     * Notifies AudioService that it is connected to an A2DP device that supports absolute volume,
2479     * so that AudioService can send volume change events to the A2DP device, rather than handling
2480     * them.
2481     */
2482    public void avrcpSupportsAbsoluteVolume(String address, boolean support) {
2483        IAudioService service = getService();
2484        try {
2485            service.avrcpSupportsAbsoluteVolume(address, support);
2486        } catch (RemoteException e) {
2487            Log.e(TAG, "Dead object in avrcpSupportsAbsoluteVolume", e);
2488        }
2489    }
2490
2491     /**
2492      * {@hide}
2493      */
2494     private final IBinder mICallBack = new Binder();
2495
2496    /**
2497     * Checks whether the phone is in silent mode, with or without vibrate.
2498     *
2499     * @return true if phone is in silent mode, with or without vibrate.
2500     *
2501     * @see #getRingerMode()
2502     *
2503     * @hide pending API Council approval
2504     */
2505    public boolean isSilentMode() {
2506        int ringerMode = getRingerMode();
2507        boolean silentMode =
2508            (ringerMode == RINGER_MODE_SILENT) ||
2509            (ringerMode == RINGER_MODE_VIBRATE);
2510        return silentMode;
2511    }
2512
2513    // This section re-defines new output device constants from AudioSystem, because the AudioSystem
2514    // class is not used by other parts of the framework, which instead use definitions and methods
2515    // from AudioManager. AudioSystem is an internal class used by AudioManager and AudioService.
2516
2517    /** {@hide} The audio output device code for the small speaker at the front of the device used
2518     *  when placing calls.  Does not refer to an in-ear headphone without attached microphone,
2519     *  such as earbuds, earphones, or in-ear monitors (IEM). Those would be handled as a
2520     *  {@link #DEVICE_OUT_WIRED_HEADPHONE}.
2521     */
2522    public static final int DEVICE_OUT_EARPIECE = AudioSystem.DEVICE_OUT_EARPIECE;
2523    /** {@hide} The audio output device code for the built-in speaker */
2524    public static final int DEVICE_OUT_SPEAKER = AudioSystem.DEVICE_OUT_SPEAKER;
2525    /** {@hide} The audio output device code for a wired headset with attached microphone */
2526    public static final int DEVICE_OUT_WIRED_HEADSET = AudioSystem.DEVICE_OUT_WIRED_HEADSET;
2527    /** {@hide} The audio output device code for a wired headphone without attached microphone */
2528    public static final int DEVICE_OUT_WIRED_HEADPHONE = AudioSystem.DEVICE_OUT_WIRED_HEADPHONE;
2529    /** {@hide} The audio output device code for generic Bluetooth SCO, for voice */
2530    public static final int DEVICE_OUT_BLUETOOTH_SCO = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO;
2531    /** {@hide} The audio output device code for Bluetooth SCO Headset Profile (HSP) and
2532     *  Hands-Free Profile (HFP), for voice
2533     */
2534    public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET =
2535            AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2536    /** {@hide} The audio output device code for Bluetooth SCO car audio, for voice */
2537    public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT =
2538            AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2539    /** {@hide} The audio output device code for generic Bluetooth A2DP, for music */
2540    public static final int DEVICE_OUT_BLUETOOTH_A2DP = AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP;
2541    /** {@hide} The audio output device code for Bluetooth A2DP headphones, for music */
2542    public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES =
2543            AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2544    /** {@hide} The audio output device code for Bluetooth A2DP external speaker, for music */
2545    public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER =
2546            AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2547    /** {@hide} The audio output device code for S/PDIF or HDMI */
2548    public static final int DEVICE_OUT_AUX_DIGITAL = AudioSystem.DEVICE_OUT_AUX_DIGITAL;
2549    /** {@hide} The audio output device code for an analog wired headset attached via a
2550     *  docking station
2551     */
2552    public static final int DEVICE_OUT_ANLG_DOCK_HEADSET = AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET;
2553    /** {@hide} The audio output device code for a digital wired headset attached via a
2554     *  docking station
2555     */
2556    public static final int DEVICE_OUT_DGTL_DOCK_HEADSET = AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET;
2557    /** {@hide} The audio output device code for a USB audio accessory. The accessory is in USB host
2558     * mode and the Android device in USB device mode
2559     */
2560    public static final int DEVICE_OUT_USB_ACCESSORY = AudioSystem.DEVICE_OUT_USB_ACCESSORY;
2561    /** {@hide} The audio output device code for a USB audio device. The device is in USB device
2562     * mode and the Android device in USB host mode
2563     */
2564    public static final int DEVICE_OUT_USB_DEVICE = AudioSystem.DEVICE_OUT_USB_DEVICE;
2565    /** {@hide} This is not used as a returned value from {@link #getDevicesForStream}, but could be
2566     *  used in the future in a set method to select whatever default device is chosen by the
2567     *  platform-specific implementation.
2568     */
2569    public static final int DEVICE_OUT_DEFAULT = AudioSystem.DEVICE_OUT_DEFAULT;
2570
2571    /**
2572     * Return the enabled devices for the specified output stream type.
2573     *
2574     * @param streamType The stream type to query. One of
2575     *            {@link #STREAM_VOICE_CALL},
2576     *            {@link #STREAM_SYSTEM},
2577     *            {@link #STREAM_RING},
2578     *            {@link #STREAM_MUSIC},
2579     *            {@link #STREAM_ALARM},
2580     *            {@link #STREAM_NOTIFICATION},
2581     *            {@link #STREAM_DTMF}.
2582     *
2583     * @return The bit-mask "or" of audio output device codes for all enabled devices on this
2584     *         stream. Zero or more of
2585     *            {@link #DEVICE_OUT_EARPIECE},
2586     *            {@link #DEVICE_OUT_SPEAKER},
2587     *            {@link #DEVICE_OUT_WIRED_HEADSET},
2588     *            {@link #DEVICE_OUT_WIRED_HEADPHONE},
2589     *            {@link #DEVICE_OUT_BLUETOOTH_SCO},
2590     *            {@link #DEVICE_OUT_BLUETOOTH_SCO_HEADSET},
2591     *            {@link #DEVICE_OUT_BLUETOOTH_SCO_CARKIT},
2592     *            {@link #DEVICE_OUT_BLUETOOTH_A2DP},
2593     *            {@link #DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES},
2594     *            {@link #DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER},
2595     *            {@link #DEVICE_OUT_AUX_DIGITAL},
2596     *            {@link #DEVICE_OUT_ANLG_DOCK_HEADSET},
2597     *            {@link #DEVICE_OUT_DGTL_DOCK_HEADSET}.
2598     *            {@link #DEVICE_OUT_DEFAULT} is not used here.
2599     *
2600     * The implementation may support additional device codes beyond those listed, so
2601     * the application should ignore any bits which it does not recognize.
2602     * Note that the information may be imprecise when the implementation
2603     * cannot distinguish whether a particular device is enabled.
2604     *
2605     * {@hide}
2606     */
2607    public int getDevicesForStream(int streamType) {
2608        switch (streamType) {
2609        case STREAM_VOICE_CALL:
2610        case STREAM_SYSTEM:
2611        case STREAM_RING:
2612        case STREAM_MUSIC:
2613        case STREAM_ALARM:
2614        case STREAM_NOTIFICATION:
2615        case STREAM_DTMF:
2616            return AudioSystem.getDevicesForStream(streamType);
2617        default:
2618            return 0;
2619        }
2620    }
2621
2622     /**
2623     * Indicate wired accessory connection state change.
2624     * @param device type of device connected/disconnected (AudioManager.DEVICE_OUT_xxx)
2625     * @param state  new connection state: 1 connected, 0 disconnected
2626     * @param name   device name
2627     * {@hide}
2628     */
2629    public void setWiredDeviceConnectionState(int device, int state, String name) {
2630        IAudioService service = getService();
2631        try {
2632            service.setWiredDeviceConnectionState(device, state, name);
2633        } catch (RemoteException e) {
2634            Log.e(TAG, "Dead object in setWiredDeviceConnectionState "+e);
2635        }
2636    }
2637
2638     /**
2639     * Indicate A2DP sink connection state change.
2640     * @param device Bluetooth device connected/disconnected
2641     * @param state  new connection state (BluetoothProfile.STATE_xxx)
2642     * @return a delay in ms that the caller should wait before broadcasting
2643     * BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED intent.
2644     * {@hide}
2645     */
2646    public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state) {
2647        IAudioService service = getService();
2648        int delay = 0;
2649        try {
2650            delay = service.setBluetoothA2dpDeviceConnectionState(device, state);
2651        } catch (RemoteException e) {
2652            Log.e(TAG, "Dead object in setBluetoothA2dpDeviceConnectionState "+e);
2653        } finally {
2654            return delay;
2655        }
2656    }
2657
2658    /** {@hide} */
2659    public IRingtonePlayer getRingtonePlayer() {
2660        try {
2661            return getService().getRingtonePlayer();
2662        } catch (RemoteException e) {
2663            return null;
2664        }
2665    }
2666
2667    /**
2668     * Used as a key for {@link #getProperty} to request the native or optimal output sample rate
2669     * for this device's primary output stream, in decimal Hz.
2670     */
2671    public static final String PROPERTY_OUTPUT_SAMPLE_RATE =
2672            "android.media.property.OUTPUT_SAMPLE_RATE";
2673
2674    /**
2675     * Used as a key for {@link #getProperty} to request the native or optimal output buffer size
2676     * for this device's primary output stream, in decimal PCM frames.
2677     */
2678    public static final String PROPERTY_OUTPUT_FRAMES_PER_BUFFER =
2679            "android.media.property.OUTPUT_FRAMES_PER_BUFFER";
2680
2681    /**
2682     * Returns the value of the property with the specified key.
2683     * @param key One of the strings corresponding to a property key: either
2684     *            {@link #PROPERTY_OUTPUT_SAMPLE_RATE} or
2685     *            {@link #PROPERTY_OUTPUT_FRAMES_PER_BUFFER}
2686     * @return A string representing the associated value for that property key,
2687     *         or null if there is no value for that key.
2688     */
2689    public String getProperty(String key) {
2690        if (PROPERTY_OUTPUT_SAMPLE_RATE.equals(key)) {
2691            int outputSampleRate = AudioSystem.getPrimaryOutputSamplingRate();
2692            return outputSampleRate > 0 ? Integer.toString(outputSampleRate) : null;
2693        } else if (PROPERTY_OUTPUT_FRAMES_PER_BUFFER.equals(key)) {
2694            int outputFramesPerBuffer = AudioSystem.getPrimaryOutputFrameCount();
2695            return outputFramesPerBuffer > 0 ? Integer.toString(outputFramesPerBuffer) : null;
2696        } else {
2697            // null or unknown key
2698            return null;
2699        }
2700    }
2701
2702    /**
2703     * Returns the estimated latency for the given stream type in milliseconds.
2704     *
2705     * DO NOT UNHIDE. The existing approach for doing A/V sync has too many problems. We need
2706     * a better solution.
2707     * @hide
2708     */
2709    public int getOutputLatency(int streamType) {
2710        return AudioSystem.getOutputLatency(streamType);
2711    }
2712
2713}
2714