AudioManager.java revision 8a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7
19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.media;
189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Triviimport android.Manifest;
20958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Triviimport android.annotation.NonNull;
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.annotation.SdkConstant;
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.annotation.SdkConstant.SdkConstantType;
23e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heoimport android.annotation.SystemApi;
24f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Triviimport android.app.PendingIntent;
25b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurentimport android.bluetooth.BluetoothDevice;
26d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Triviimport android.content.ComponentName;
279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.content.Context;
28f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Triviimport android.content.Intent;
29a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Triviimport android.media.audiopolicy.AudioPolicy;
30b214efbb9170a9f6a4991684a63ca59680074cc7RoboErikimport android.media.session.MediaController;
31b214efbb9170a9f6a4991684a63ca59680074cc7RoboErikimport android.media.session.MediaSession;
32f1372428f2df781c71c71caa2f6a4db6f847cf10RoboErikimport android.media.session.MediaSessionLegacyHelper;
33b214efbb9170a9f6a4991684a63ca59680074cc7RoboErikimport android.media.session.MediaSessionManager;
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Binder;
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.Handler;
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.IBinder;
37d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Triviimport android.os.Looper;
38d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Triviimport android.os.Message;
390c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monkimport android.os.Process;
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.RemoteException;
41bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishraimport android.os.SystemProperties;
4286f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onoratoimport android.os.SystemClock;
439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.os.ServiceManager;
449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.provider.Settings;
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectimport android.util.Log;
46d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Triviimport android.view.KeyEvent;
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
48a198a29250acb7c3e918f1566727190966bb336fEric Laurentimport java.util.ArrayList;
49b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kimimport java.util.HashMap;
50b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kimimport java.util.Iterator;
51700e73471d85348b52ecf213c36bb24b93997ec7Eric Laurent
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/**
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * AudioManager provides access to volume and ringer mode control.
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p>
559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Use <code>Context.getSystemService(Context.AUDIO_SERVICE)</code> to get
569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * an instance of this class.
579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class AudioManager {
599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6029f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen    private Context mOriginalContext;
6129f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen    private Context mApplicationContext;
6286f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato    private long mVolumeKeyUpTime;
63b89ce434993cca883818ab38f98c0b29d77e7aa2Christopher Tate    private final boolean mUseVolumeKeySounds;
64ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent    private final boolean mUseFixedVolume;
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static String TAG = "AudioManager";
66f076db407029e734703d8676f806bea94393e824Eric Laurent    private static final AudioPortEventHandler sAudioPortEventHandler = new AudioPortEventHandler();
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
69bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra     * System properties for whether the default microphone and speaker paths support
70bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra     * near-ultrasound frequencies (range of 18 - 21 kHz).
71bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra     */
72bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra    private static final String SYSTEM_PROPERTY_MIC_NEAR_ULTRASOUND =
73bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra            "persist.audio.mic.ultrasound";
74bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra    private static final String SYSTEM_PROPERTY_SPEAKER_NEAR_ULTRASOUND =
75bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra            "persist.audio.spkr.ultrasound";
76bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra    private static final String DEFAULT_RESULT_FALSE_STRING = "false";
77bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra
78bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra    /**
799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Broadcast intent, a hint for applications that audio is about to become
809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * 'noisy' due to a change in audio outputs. For example, this intent may
819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * be sent when a wired headset is unplugged, or when an A2DP audio
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * sink is disconnected, and the audio system is about to automatically
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * switch audio route to the speaker. Applications that are controlling
849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * audio streams may consider pausing, reducing volume or some other action
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * on receipt of this intent so as not to surprise the user with audio
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * from the speaker.
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String ACTION_AUDIO_BECOMING_NOISY = "android.media.AUDIO_BECOMING_NOISY";
909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sticky broadcast intent action indicating that the ringer mode has
939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * changed. Includes the new ringer mode.
949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #EXTRA_RINGER_MODE
969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String RINGER_MODE_CHANGED_ACTION = "android.media.RINGER_MODE_CHANGED";
999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
101bcc1087af40a0e1bb35dbe8a39c830ecdea8280bJohn Spurlock     * @hide
102bcc1087af40a0e1bb35dbe8a39c830ecdea8280bJohn Spurlock     * Sticky broadcast intent action indicating that the internal ringer mode has
103bcc1087af40a0e1bb35dbe8a39c830ecdea8280bJohn Spurlock     * changed. Includes the new ringer mode.
104bcc1087af40a0e1bb35dbe8a39c830ecdea8280bJohn Spurlock     *
105bcc1087af40a0e1bb35dbe8a39c830ecdea8280bJohn Spurlock     * @see #EXTRA_RINGER_MODE
106bcc1087af40a0e1bb35dbe8a39c830ecdea8280bJohn Spurlock     */
107bcc1087af40a0e1bb35dbe8a39c830ecdea8280bJohn Spurlock    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
108bcc1087af40a0e1bb35dbe8a39c830ecdea8280bJohn Spurlock    public static final String INTERNAL_RINGER_MODE_CHANGED_ACTION =
109bcc1087af40a0e1bb35dbe8a39c830ecdea8280bJohn Spurlock            "android.media.INTERNAL_RINGER_MODE_CHANGED_ACTION";
110bcc1087af40a0e1bb35dbe8a39c830ecdea8280bJohn Spurlock
111bcc1087af40a0e1bb35dbe8a39c830ecdea8280bJohn Spurlock    /**
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The new ringer mode.
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #RINGER_MODE_CHANGED_ACTION
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #RINGER_MODE_NORMAL
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #RINGER_MODE_SILENT
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #RINGER_MODE_VIBRATE
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String EXTRA_RINGER_MODE = "android.media.EXTRA_RINGER_MODE";
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Broadcast intent action indicating that the vibrate setting has
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * changed. Includes the vibrate type and its new setting.
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #EXTRA_VIBRATE_TYPE
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #EXTRA_VIBRATE_SETTING
127cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * @deprecated Applications should maintain their own vibrate policy based on
128cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * current ringer mode and listen to {@link #RINGER_MODE_CHANGED_ACTION} instead.
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
1310dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood    public static final String VIBRATE_SETTING_CHANGED_ACTION =
1320dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood        "android.media.VIBRATE_SETTING_CHANGED";
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide Broadcast intent when the volume for a particular stream type changes.
1363114ce3861f20f9a5c2c59dd2629197a1f4874a8Jean-Michel Trivi     * Includes the stream, the new volume and previous volumes.
1373114ce3861f20f9a5c2c59dd2629197a1f4874a8Jean-Michel Trivi     * Notes:
1383114ce3861f20f9a5c2c59dd2629197a1f4874a8Jean-Michel Trivi     *  - for internal platform use only, do not make public,
1393114ce3861f20f9a5c2c59dd2629197a1f4874a8Jean-Michel Trivi     *  - never used for "remote" volume changes
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #EXTRA_VOLUME_STREAM_TYPE
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #EXTRA_VOLUME_STREAM_VALUE
1439ce379aef155e0c21b5d82d8dc713c62792e4f30Eric Laurent     * @see #EXTRA_PREV_VOLUME_STREAM_VALUE
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION";
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1498a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     * @hide Broadcast intent when the devices for a particular stream type changes.
1508a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     * Includes the stream, the new devices and previous devices.
1518a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     * Notes:
1528a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     *  - for internal platform use only, do not make public,
1538a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     *  - never used for "remote" volume changes
1548a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     *
1558a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     * @see #EXTRA_VOLUME_STREAM_TYPE
1568a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     * @see #EXTRA_VOLUME_STREAM_DEVICES
1578a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     * @see #EXTRA_PREV_VOLUME_STREAM_DEVICES
1588a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     * @see #getDevicesForStream
1598a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     */
1608a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
1618a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock    public static final String STREAM_DEVICES_CHANGED_ACTION =
1628a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock        "android.media.STREAM_DEVICES_CHANGED_ACTION";
1638a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock
1648a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock    /**
1657c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik     * @hide Broadcast intent when a stream mute state changes.
1667c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik     * Includes the stream that changed and the new mute state
1677c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik     *
1687c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik     * @see #EXTRA_VOLUME_STREAM_TYPE
1697c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik     * @see #EXTRA_STREAM_VOLUME_MUTED
1707c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik     */
1717c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
1727c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik    public static final String STREAM_MUTE_CHANGED_ACTION =
1737c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik        "android.media.STREAM_MUTE_CHANGED_ACTION";
1747c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik
1757c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik    /**
1760dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood     * @hide Broadcast intent when the master mute state changes.
1770dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood     * Includes the the new volume
1780dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood     *
1790dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood     * @see #EXTRA_MASTER_VOLUME_MUTED
1800dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood     */
1810dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
1820dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood    public static final String MASTER_MUTE_CHANGED_ACTION =
1830dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood        "android.media.MASTER_MUTE_CHANGED_ACTION";
1840dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood
1850dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood    /**
1869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The new vibrate setting for a particular type.
1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #VIBRATE_SETTING_CHANGED_ACTION
1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #EXTRA_VIBRATE_TYPE
1909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #VIBRATE_SETTING_ON
1919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #VIBRATE_SETTING_OFF
1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #VIBRATE_SETTING_ONLY_SILENT
193cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * @deprecated Applications should maintain their own vibrate policy based on
194cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * current ringer mode and listen to {@link #RINGER_MODE_CHANGED_ACTION} instead.
1959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String EXTRA_VIBRATE_SETTING = "android.media.EXTRA_VIBRATE_SETTING";
1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * The vibrate type whose setting has changed.
2009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
2019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #VIBRATE_SETTING_CHANGED_ACTION
2029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #VIBRATE_TYPE_NOTIFICATION
2039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #VIBRATE_TYPE_RINGER
204cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * @deprecated Applications should maintain their own vibrate policy based on
205cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * current ringer mode and listen to {@link #RINGER_MODE_CHANGED_ACTION} instead.
2069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String EXTRA_VIBRATE_TYPE = "android.media.EXTRA_VIBRATE_TYPE";
2089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide The stream type for the volume changed intent.
2119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String EXTRA_VOLUME_STREAM_TYPE = "android.media.EXTRA_VOLUME_STREAM_TYPE";
2139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
2159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide The volume associated with the stream for the volume changed intent.
2169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final String EXTRA_VOLUME_STREAM_VALUE =
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        "android.media.EXTRA_VOLUME_STREAM_VALUE";
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2209ce379aef155e0c21b5d82d8dc713c62792e4f30Eric Laurent    /**
2219ce379aef155e0c21b5d82d8dc713c62792e4f30Eric Laurent     * @hide The previous volume associated with the stream for the volume changed intent.
2229ce379aef155e0c21b5d82d8dc713c62792e4f30Eric Laurent     */
2239ce379aef155e0c21b5d82d8dc713c62792e4f30Eric Laurent    public static final String EXTRA_PREV_VOLUME_STREAM_VALUE =
2249ce379aef155e0c21b5d82d8dc713c62792e4f30Eric Laurent        "android.media.EXTRA_PREV_VOLUME_STREAM_VALUE";
2250dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood
2260dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood    /**
2278a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     * @hide The devices associated with the stream for the stream devices changed intent.
2288a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     */
2298a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock    public static final String EXTRA_VOLUME_STREAM_DEVICES =
2308a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock        "android.media.EXTRA_VOLUME_STREAM_DEVICES";
2318a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock
2328a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock    /**
2338a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     * @hide The previous devices associated with the stream for the stream devices changed intent.
2348a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock     */
2358a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock    public static final String EXTRA_PREV_VOLUME_STREAM_DEVICES =
2368a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock        "android.media.EXTRA_PREV_VOLUME_STREAM_DEVICES";
2378a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock
2388a52c4491dfbb8354a92c0a1e6abc86848e187c6John Spurlock    /**
2390dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood     * @hide The new master volume mute state for the master mute changed intent.
2400dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood     * Value is boolean
2410dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood     */
2420dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood    public static final String EXTRA_MASTER_VOLUME_MUTED =
2430dc37cce9d564ae43883c8dc8672b9266b881e63Mike Lockwood        "android.media.EXTRA_MASTER_VOLUME_MUTED";
2449ce379aef155e0c21b5d82d8dc713c62792e4f30Eric Laurent
245c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi    /**
2467c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik     * @hide The new stream volume mute state for the stream mute changed intent.
2477c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik     * Value is boolean
2487c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik     */
2497c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik    public static final String EXTRA_STREAM_VOLUME_MUTED =
2507c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik        "android.media.EXTRA_STREAM_VOLUME_MUTED";
2517c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik
2527c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik    /**
253c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * Broadcast Action: Wired Headset plugged in or unplugged.
254c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     *
255c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * You <em>cannot</em> receive this through components declared
256c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * in manifests, only by explicitly registering for it with
257c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)
258c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * Context.registerReceiver()}.
259c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     *
260c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * <p>The intent will have the following extra values:
261c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * <ul>
262c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     *   <li><em>state</em> - 0 for unplugged, 1 for plugged. </li>
263c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     *   <li><em>name</em> - Headset type, human readable string </li>
264c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     *   <li><em>microphone</em> - 1 if headset has a microphone, 0 otherwise </li>
265c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * </ul>
266c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * </ul>
267c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     */
268c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
269c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi    public static final String ACTION_HEADSET_PLUG =
270c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi            "android.intent.action.HEADSET_PLUG";
271c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi
272c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi    /**
273bc68256fea95c25258d8faad6bb697c364ad5a90Eemi Haukkala     * Broadcast Action: A sticky broadcast indicating an HDMI cable was plugged or unplugged.
274c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     *
275c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * The intent will have the following extra values: {@link #EXTRA_AUDIO_PLUG_STATE},
276c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * {@link #EXTRA_MAX_CHANNEL_COUNT}, {@link #EXTRA_ENCODINGS}.
277c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * <p>It can only be received by explicitly registering for it with
278c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)}.
279c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     */
280c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
281c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi    public static final String ACTION_HDMI_AUDIO_PLUG =
282c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi            "android.media.action.HDMI_AUDIO_PLUG";
283c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi
284c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi    /**
285c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * Extra used in {@link #ACTION_HDMI_AUDIO_PLUG} to communicate whether HDMI is plugged in
286c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * or unplugged.
287c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * An integer value of 1 indicates a plugged-in state, 0 is unplugged.
288c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     */
289deddda5d62498bbaa9789c622be7a08be83f2f51Jean-Michel Trivi    public static final String EXTRA_AUDIO_PLUG_STATE = "android.media.extra.AUDIO_PLUG_STATE";
290c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi
291c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi    /**
292c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * Extra used in {@link #ACTION_HDMI_AUDIO_PLUG} to define the maximum number of channels
293c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * supported by the HDMI device.
294c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * The corresponding integer value is only available when the device is plugged in (as expressed
295c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * by {@link #EXTRA_AUDIO_PLUG_STATE}).
296c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     */
297deddda5d62498bbaa9789c622be7a08be83f2f51Jean-Michel Trivi    public static final String EXTRA_MAX_CHANNEL_COUNT = "android.media.extra.MAX_CHANNEL_COUNT";
298c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi
299c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi    /**
300c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * Extra used in {@link #ACTION_HDMI_AUDIO_PLUG} to define the audio encodings supported by
301c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * the connected HDMI device.
302c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * The corresponding array of encoding values is only available when the device is plugged in
303c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * (as expressed by {@link #EXTRA_AUDIO_PLUG_STATE}). Encoding values are defined in
304c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * {@link AudioFormat} (for instance see {@link AudioFormat#ENCODING_PCM_16BIT}). Use
305c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     * {@link android.content.Intent#getIntArrayExtra(String)} to retrieve the encoding values.
306c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi     */
307deddda5d62498bbaa9789c622be7a08be83f2f51Jean-Michel Trivi    public static final String EXTRA_ENCODINGS = "android.media.extra.ENCODINGS";
308c5258433dd353769ccfa2e5e769a7379378a3a0bJean-Michel Trivi
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** The audio stream for phone calls */
3109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL;
3119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** The audio stream for system sounds */
3129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM;
3139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** The audio stream for the phone ring */
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int STREAM_RING = AudioSystem.STREAM_RING;
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** The audio stream for music playback */
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC;
3179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** The audio stream for alarms */
3189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int STREAM_ALARM = AudioSystem.STREAM_ALARM;
3199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** The audio stream for notifications */
3209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION;
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** @hide The audio stream for phone calls when connected to bluetooth */
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int STREAM_BLUETOOTH_SCO = AudioSystem.STREAM_BLUETOOTH_SCO;
323a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    /** @hide The audio stream for enforced system sounds in certain countries (e.g camera in Japan) */
324a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public static final int STREAM_SYSTEM_ENFORCED = AudioSystem.STREAM_SYSTEM_ENFORCED;
325a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    /** The audio stream for DTMF Tones */
326a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public static final int STREAM_DTMF = AudioSystem.STREAM_DTMF;
327a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    /** @hide The audio stream for text to speech (TTS) */
328a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public static final int STREAM_TTS = AudioSystem.STREAM_TTS;
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /** Number of audio streams */
3309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @deprecated Use AudioSystem.getNumStreamTypes() instead
3329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
333a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated public static final int NUM_STREAMS = AudioSystem.NUM_STREAMS;
3349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Increase the ringer volume.
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustVolume(int, int)
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustStreamVolume(int, int, int)
3409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int ADJUST_RAISE = 1;
3429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Decrease the ringer volume.
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustVolume(int, int)
3479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustStreamVolume(int, int, int)
3489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int ADJUST_LOWER = -1;
3509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Maintain the previous ringer volume. This may be useful when needing to
3539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * show the volume toast without actually modifying the volume.
3549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustVolume(int, int)
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustStreamVolume(int, int, int)
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int ADJUST_SAME = 0;
3599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3604197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik    /**
3614197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * Mute the volume. Has no effect if the stream is already muted.
3624197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *
3634197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @see #adjustVolume(int, int)
3644197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @see #adjustStreamVolume(int, int, int)
3654197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     */
3664197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik    public static final int ADJUST_MUTE = -100;
3674197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik
3684197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik    /**
3694197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * Unmute the volume. Has no effect if the stream is not muted.
3704197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *
3714197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @see #adjustVolume(int, int)
3724197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @see #adjustStreamVolume(int, int, int)
3734197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     */
3744197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik    public static final int ADJUST_UNMUTE = 100;
3754197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik
3764197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik    /**
3774197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * Toggle the mute state. If muted the stream will be unmuted. If not muted
3784197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * the stream will be muted.
3794197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *
3804197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @see #adjustVolume(int, int)
3814197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @see #adjustStreamVolume(int, int, int)
3824197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     */
3834197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik    public static final int ADJUST_TOGGLE_MUTE = 101;
3844197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik
3859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // Flags should be powers of 2!
3869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Show a toast containing the current volume.
3899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
3909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustStreamVolume(int, int, int)
3919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustVolume(int, int)
3929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setStreamVolume(int, int, int)
3939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setRingerMode(int)
3949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
3959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FLAG_SHOW_UI = 1 << 0;
3969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
3989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Whether to include ringer modes as possible options when changing volume.
3999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * For example, if true and volume level is 0 and the volume is adjusted
4009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * with {@link #ADJUST_LOWER}, then the ringer mode may switch the silent or
4019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * vibrate mode.
4029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p>
4039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * By default this is on for the ring stream. If this flag is included,
4049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * this behavior will be present regardless of the stream type being
4059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * affected by the ringer mode.
406105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     *
4079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustVolume(int, int)
4089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustStreamVolume(int, int, int)
4099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FLAG_ALLOW_RINGER_MODES = 1 << 1;
4119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Whether to play a sound when changing the volume.
4149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p>
4159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * If this is given to {@link #adjustVolume(int, int)} or
4169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #adjustSuggestedStreamVolume(int, int, int)}, it may be ignored
4179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * in some cases (for example, the decided stream type is not
4189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link AudioManager#STREAM_RING}, or the volume is being adjusted
4199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * downward).
4209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
4219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustStreamVolume(int, int, int)
4229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustVolume(int, int)
4239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setStreamVolume(int, int, int)
4249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FLAG_PLAY_SOUND = 1 << 2;
4269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Removes any sounds/vibrate that may be in the queue, or are playing (related to
4299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * changing volume).
4309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FLAG_REMOVE_SOUND_AND_VIBRATE = 1 << 3;
4329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Whether to vibrate if going into the vibrate ringer mode.
4359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
4369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FLAG_VIBRATE = 1 << 4;
4379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
4389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
4394bbcc6549738f3d69831b2bd9eb4accec3e9920eEric Laurent     * Indicates to VolumePanel that the volume slider should be disabled as user
4404bbcc6549738f3d69831b2bd9eb4accec3e9920eEric Laurent     * cannot change the stream volume
4414bbcc6549738f3d69831b2bd9eb4accec3e9920eEric Laurent     * @hide
4424bbcc6549738f3d69831b2bd9eb4accec3e9920eEric Laurent     */
4434bbcc6549738f3d69831b2bd9eb4accec3e9920eEric Laurent    public static final int FLAG_FIXED_VOLUME = 1 << 5;
4444bbcc6549738f3d69831b2bd9eb4accec3e9920eEric Laurent
4454bbcc6549738f3d69831b2bd9eb4accec3e9920eEric Laurent    /**
446c9d1d5f35091226e96fcfa91817480f589eb36e2Matthew Xie     * Indicates the volume set/adjust call is for Bluetooth absolute volume
447c9d1d5f35091226e96fcfa91817480f589eb36e2Matthew Xie     * @hide
448c9d1d5f35091226e96fcfa91817480f589eb36e2Matthew Xie     */
449c9d1d5f35091226e96fcfa91817480f589eb36e2Matthew Xie    public static final int FLAG_BLUETOOTH_ABS_VOLUME = 1 << 6;
450c9d1d5f35091226e96fcfa91817480f589eb36e2Matthew Xie
451c9d1d5f35091226e96fcfa91817480f589eb36e2Matthew Xie    /**
452a11b4affcad3d255aa723a89b768ea222506f2e8John Spurlock     * Adjusting the volume was prevented due to silent mode, display a hint in the UI.
453a11b4affcad3d255aa723a89b768ea222506f2e8John Spurlock     * @hide
454a11b4affcad3d255aa723a89b768ea222506f2e8John Spurlock     */
455a11b4affcad3d255aa723a89b768ea222506f2e8John Spurlock    public static final int FLAG_SHOW_SILENT_HINT = 1 << 7;
456a11b4affcad3d255aa723a89b768ea222506f2e8John Spurlock
457a11b4affcad3d255aa723a89b768ea222506f2e8John Spurlock    /**
45841d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang     * Indicates the volume call is for Hdmi Cec system audio volume
45941d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang     * @hide
46041d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang     */
46141d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang    public static final int FLAG_HDMI_SYSTEM_AUDIO_VOLUME = 1 << 8;
46241d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang
46341d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang    /**
4643c45c29109d23981d8b707c809b3b43ce2e20fc3RoboErik     * Indicates that this should only be handled if media is actively playing.
4653c45c29109d23981d8b707c809b3b43ce2e20fc3RoboErik     * @hide
4663c45c29109d23981d8b707c809b3b43ce2e20fc3RoboErik     */
4673c45c29109d23981d8b707c809b3b43ce2e20fc3RoboErik    public static final int FLAG_ACTIVE_MEDIA_ONLY = 1 << 9;
4683c45c29109d23981d8b707c809b3b43ce2e20fc3RoboErik
4693c45c29109d23981d8b707c809b3b43ce2e20fc3RoboErik    /**
470351346092acdfbfcc1d9ebf98d539d2a1196c5e8John Spurlock     * Like FLAG_SHOW_UI, but only dialog warnings and confirmations, no sliders.
471351346092acdfbfcc1d9ebf98d539d2a1196c5e8John Spurlock     * @hide
472351346092acdfbfcc1d9ebf98d539d2a1196c5e8John Spurlock     */
473351346092acdfbfcc1d9ebf98d539d2a1196c5e8John Spurlock    public static final int FLAG_SHOW_UI_WARNINGS = 1 << 10;
474351346092acdfbfcc1d9ebf98d539d2a1196c5e8John Spurlock
475351346092acdfbfcc1d9ebf98d539d2a1196c5e8John Spurlock    /**
476661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock     * Adjusting the volume down from vibrated was prevented, display a hint in the UI.
477661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock     * @hide
478661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock     */
479661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    public static final int FLAG_SHOW_VIBRATE_HINT = 1 << 11;
480661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock
481b94f2d6cad92c4f6ad184920e06a260d87eb3b6aJohn Spurlock    /**
482b94f2d6cad92c4f6ad184920e06a260d87eb3b6aJohn Spurlock     * Adjusting the volume due to a hardware key press.
483b94f2d6cad92c4f6ad184920e06a260d87eb3b6aJohn Spurlock     * @hide
484b94f2d6cad92c4f6ad184920e06a260d87eb3b6aJohn Spurlock     */
485b94f2d6cad92c4f6ad184920e06a260d87eb3b6aJohn Spurlock    public static final int FLAG_FROM_KEY = 1 << 12;
486b94f2d6cad92c4f6ad184920e06a260d87eb3b6aJohn Spurlock
487661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    private static final String[] FLAG_NAMES = {
488661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_SHOW_UI",
489661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_ALLOW_RINGER_MODES",
490661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_PLAY_SOUND",
491661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_REMOVE_SOUND_AND_VIBRATE",
492661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_VIBRATE",
493661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_FIXED_VOLUME",
494661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_BLUETOOTH_ABS_VOLUME",
495661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_SHOW_SILENT_HINT",
496661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_HDMI_SYSTEM_AUDIO_VOLUME",
497661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_ACTIVE_MEDIA_ONLY",
498661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_SHOW_UI_WARNINGS",
499661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        "FLAG_SHOW_VIBRATE_HINT",
500b94f2d6cad92c4f6ad184920e06a260d87eb3b6aJohn Spurlock        "FLAG_FROM_KEY",
501661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    };
502661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock
503661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    /** @hide */
504661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    public static String flagsToString(int flags) {
505661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        final StringBuilder sb = new StringBuilder();
506661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        for (int i = 0; i < FLAG_NAMES.length; i++) {
507661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock            final int flag = 1 << i;
508661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock            if ((flags & flag) != 0) {
509661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock                if (sb.length() > 0) {
510661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock                    sb.append(',');
511661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock                }
512661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock                sb.append(FLAG_NAMES[i]);
513661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock                flags &= ~flag;
514661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock            }
515661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        }
516661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        if (flags != 0) {
517661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock            if (sb.length() > 0) {
518661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock                sb.append(',');
519661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock            }
520661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock            sb.append(flags);
521661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        }
522661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        return sb.toString();
523661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    }
524661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock
525661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    /**
5269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Ringer mode that will be silent and will not vibrate. (This overrides the
5279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * vibrate setting.)
5289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setRingerMode(int)
5309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getRingerMode()
5319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int RINGER_MODE_SILENT = 0;
5339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Ringer mode that will be silent and will vibrate. (This will cause the
5369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * phone ringer to always vibrate, but the notification vibrate to only
5379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * vibrate if set.)
5389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setRingerMode(int)
5409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getRingerMode()
5419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int RINGER_MODE_VIBRATE = 1;
5439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Ringer mode that may be audible and may vibrate. It will be audible if
5469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * the volume before changing out of this mode was audible. It will vibrate
5479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * if the vibrate setting is on.
5489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setRingerMode(int)
5509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getRingerMode()
5519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int RINGER_MODE_NORMAL = 2;
5539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5549755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock    /**
5559755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock     * Maximum valid ringer mode value. Values must start from 0 and be contiguous.
5569755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock     * @hide
5579755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock     */
5589755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock    public static final int RINGER_MODE_MAX = RINGER_MODE_NORMAL;
55972668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent
5609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Vibrate type that corresponds to the ringer.
5629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setVibrateSetting(int, int)
5649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getVibrateSetting(int)
5659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #shouldVibrate(int)
566cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * @deprecated Applications should maintain their own vibrate policy based on
567cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * current ringer mode that can be queried via {@link #getRingerMode()}.
5689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int VIBRATE_TYPE_RINGER = 0;
5709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Vibrate type that corresponds to notifications.
5739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setVibrateSetting(int, int)
5759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getVibrateSetting(int)
5769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #shouldVibrate(int)
577cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * @deprecated Applications should maintain their own vibrate policy based on
578cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * current ringer mode that can be queried via {@link #getRingerMode()}.
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int VIBRATE_TYPE_NOTIFICATION = 1;
5819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Vibrate setting that suggests to never vibrate.
5849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setVibrateSetting(int, int)
5869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getVibrateSetting(int)
587cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * @deprecated Applications should maintain their own vibrate policy based on
588cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * current ringer mode that can be queried via {@link #getRingerMode()}.
5899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
5909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int VIBRATE_SETTING_OFF = 0;
5919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
5939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Vibrate setting that suggests to vibrate when possible.
5949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
5959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setVibrateSetting(int, int)
5969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getVibrateSetting(int)
597cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * @deprecated Applications should maintain their own vibrate policy based on
598cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * current ringer mode that can be queried via {@link #getRingerMode()}.
5999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int VIBRATE_SETTING_ON = 1;
6019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Vibrate setting that suggests to only vibrate when in the vibrate ringer
6049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * mode.
6059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
6069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setVibrateSetting(int, int)
6079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getVibrateSetting(int)
608cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * @deprecated Applications should maintain their own vibrate policy based on
609cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * current ringer mode that can be queried via {@link #getRingerMode()}.
6109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int VIBRATE_SETTING_ONLY_SILENT = 2;
6129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Suggests using the default stream type. This may not be used in all
6159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * places a stream type is needed.
6169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int USE_DEFAULT_STREAM_TYPE = Integer.MIN_VALUE;
6189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static IAudioService sService;
6209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
6229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
6239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
6249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public AudioManager(Context context) {
62529f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        setContext(context);
62629f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        mUseVolumeKeySounds = getContext().getResources().getBoolean(
627b89ce434993cca883818ab38f98c0b29d77e7aa2Christopher Tate                com.android.internal.R.bool.config_useVolumeKeySounds);
62829f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        mUseFixedVolume = getContext().getResources().getBoolean(
629ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent                com.android.internal.R.bool.config_useFixedVolume);
630f076db407029e734703d8676f806bea94393e824Eric Laurent        sAudioPortEventHandler.init();
6319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
63329f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen    private Context getContext() {
63429f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        if (mApplicationContext == null) {
63529f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen            setContext(mOriginalContext);
63629f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        }
63729f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        if (mApplicationContext != null) {
63829f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen            return mApplicationContext;
63929f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        }
64029f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        return mOriginalContext;
64129f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen    }
64229f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen
64329f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen    private void setContext(Context context) {
64429f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        mApplicationContext = context.getApplicationContext();
64529f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        if (mApplicationContext != null) {
64629f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen            mOriginalContext = null;
64729f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        } else {
64829f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen            mOriginalContext = context;
64929f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        }
65029f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen    }
65129f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen
6529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    private static IAudioService getService()
6539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    {
6549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (sService != null) {
6559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return sService;
6569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
6579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE);
6589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        sService = IAudioService.Stub.asInterface(b);
6599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return sService;
6609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
6619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
6629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
663a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     * Sends a simulated key event for a media button.
664a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     * To simulate a key press, you must first send a KeyEvent built with a
665a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     * {@link KeyEvent#ACTION_DOWN} action, then another event with the {@link KeyEvent#ACTION_UP}
666a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     * action.
667a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     * <p>The key event will be sent to the current media key event consumer which registered with
668a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     * {@link AudioManager#registerMediaButtonEventReceiver(PendingIntent)}.
669a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     * @param keyEvent a {@link KeyEvent} instance whose key code is one of
670a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MUTE},
671a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_HEADSETHOOK},
672a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MEDIA_PLAY},
673a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MEDIA_PAUSE},
674a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MEDIA_PLAY_PAUSE},
675a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MEDIA_STOP},
676a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MEDIA_NEXT},
677a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MEDIA_PREVIOUS},
678a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MEDIA_REWIND},
679a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MEDIA_RECORD},
680a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MEDIA_FAST_FORWARD},
681a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MEDIA_CLOSE},
682a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     {@link KeyEvent#KEYCODE_MEDIA_EJECT},
683a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     *     or {@link KeyEvent#KEYCODE_MEDIA_AUDIO_TRACK}.
684a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     */
685a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi    public void dispatchMediaKeyEvent(KeyEvent keyEvent) {
68629f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext());
687430fc48865e5a371b08f180390946b96d73848feRoboErik        helper.sendMediaButtonEvent(keyEvent, false);
6887ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi    }
6897ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi
6907ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi    /**
6917ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi     * @hide
69286f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato     */
693ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood    public void preDispatchKeyEvent(KeyEvent event, int stream) {
69486f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato        /*
69586f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato         * If the user hits another key within the play sound delay, then
69686f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato         * cancel the sound
69786f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato         */
698ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood        int keyCode = event.getKeyCode();
69986f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato        if (keyCode != KeyEvent.KEYCODE_VOLUME_DOWN && keyCode != KeyEvent.KEYCODE_VOLUME_UP
70086f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                && keyCode != KeyEvent.KEYCODE_VOLUME_MUTE
7016156017c2217d0fbbbb03434986250ec6bbd69d8John Spurlock                && mVolumeKeyUpTime + AudioSystem.PLAY_SOUND_DELAY > SystemClock.uptimeMillis()) {
70286f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato            /*
70386f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato             * The user has hit another key during the delay (e.g., 300ms)
70486f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato             * since the last volume key up, so cancel any sounds.
70586f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato             */
706ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock            adjustSuggestedStreamVolume(ADJUST_SAME,
707ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock                    stream, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
70886f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato        }
70986f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato    }
71086f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato
71186f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato    /**
71286f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato     * @hide
71386f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato     */
714ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood    public void handleKeyDown(KeyEvent event, int stream) {
715ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood        int keyCode = event.getKeyCode();
71686f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato        switch (keyCode) {
71786f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato            case KeyEvent.KEYCODE_VOLUME_UP:
71886f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato            case KeyEvent.KEYCODE_VOLUME_DOWN:
71986f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                /*
72086f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                 * Adjust the volume in on key down since it is more
72186f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                 * responsive to the user.
72286f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                 */
723ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock                adjustSuggestedStreamVolume(
724ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock                        keyCode == KeyEvent.KEYCODE_VOLUME_UP
725ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock                                ? ADJUST_RAISE
726ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock                                : ADJUST_LOWER,
727ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock                        stream,
728ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock                        FLAG_SHOW_UI | FLAG_VIBRATE);
72986f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                break;
73086f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato            case KeyEvent.KEYCODE_VOLUME_MUTE:
731ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood                if (event.getRepeatCount() == 0) {
73229f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen                    MediaSessionLegacyHelper.getHelper(getContext())
733926ebb860a8b5cbdec17f07404272be7c484fd79Marco Nelissen                            .sendVolumeKeyEvent(event, false);
734ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood                }
73586f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                break;
73686f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato        }
73786f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato    }
73886f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato
73986f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato    /**
74086f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato     * @hide
74186f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato     */
742ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood    public void handleKeyUp(KeyEvent event, int stream) {
743ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood        int keyCode = event.getKeyCode();
74486f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato        switch (keyCode) {
74586f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato            case KeyEvent.KEYCODE_VOLUME_UP:
74686f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato            case KeyEvent.KEYCODE_VOLUME_DOWN:
74786f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                /*
74886f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                 * Play a sound. This is done on key up since we don't want the
74986f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                 * sound to play when a user holds down volume down to mute.
75086f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                 */
751b89ce434993cca883818ab38f98c0b29d77e7aa2Christopher Tate                if (mUseVolumeKeySounds) {
752ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock                    adjustSuggestedStreamVolume(
753ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock                            ADJUST_SAME,
754ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock                            stream,
755ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock                            FLAG_PLAY_SOUND);
756402f7f29634a9f68e7929be828a927a3e2f5efe9Eric Laurent                }
75786f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                mVolumeKeyUpTime = SystemClock.uptimeMillis();
75886f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato                break;
7597c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik            case KeyEvent.KEYCODE_VOLUME_MUTE:
76029f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen                MediaSessionLegacyHelper.getHelper(getContext())
761926ebb860a8b5cbdec17f07404272be7c484fd79Marco Nelissen                        .sendVolumeKeyEvent(event, false);
7627c82ced4fc5b66c09a19eed9a5499039530142fbRoboErik                break;
76386f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato        }
76486f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato    }
76586f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato
76686f6786032b1a0380cf089aeeceef7e9d8982ef8Joe Onorato    /**
767ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * Indicates if the device implements a fixed volume policy.
768ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * <p>Some devices may not have volume control and may operate at a fixed volume,
769ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * and may not enable muting or changing the volume of audio streams.
770ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * This method will return true on such devices.
771ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * <p>The following APIs have no effect when volume is fixed:
772ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * <ul>
773ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     *   <li> {@link #adjustVolume(int, int)}
774ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     *   <li> {@link #adjustSuggestedStreamVolume(int, int, int)}
775ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     *   <li> {@link #adjustStreamVolume(int, int, int)}
776ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     *   <li> {@link #setStreamVolume(int, int, int)}
777ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     *   <li> {@link #setRingerMode(int)}
778ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     *   <li> {@link #setStreamSolo(int, boolean)}
779ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     *   <li> {@link #setStreamMute(int, boolean)}
780ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * </ul>
781ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     */
782ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent    public boolean isVolumeFixed() {
783ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent        return mUseFixedVolume;
784ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent    }
785ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent
786ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent    /**
7879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Adjusts the volume of a particular stream by one step in a direction.
788fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * <p>
789fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * This method should only be used by applications that replace the platform-wide
790fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * management of audio settings or the main telephony application.
7919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
7929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param streamType The stream type to adjust. One of {@link #STREAM_VOICE_CALL},
7939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #STREAM_SYSTEM}, {@link #STREAM_RING}, {@link #STREAM_MUSIC} or
7949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * {@link #STREAM_ALARM}
7959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param direction The direction to adjust the volume. One of
7969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or
7979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #ADJUST_SAME}.
7989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param flags One or more flags.
7999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustVolume(int, int)
8009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setStreamVolume(int, int, int)
8019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void adjustStreamVolume(int streamType, int direction, int flags) {
8039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
8049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
805ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock            service.adjustStreamVolume(streamType, direction, flags,
8068dc50416a209e76bca38694d59d5779ffcf08d92Marco Nelissen                    getContext().getOpPackageName());
8079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
8089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in adjustStreamVolume", e);
8099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Adjusts the volume of the most relevant stream. For example, if a call is
8149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * active, it will have the highest priority regardless of if the in-call
8159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * screen is showing. Another example, if music is playing in the background
8169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * and a call is not active, the music stream will be adjusted.
817fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * <p>
8184197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * This method should only be used by applications that replace the
8194197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * platform-wide management of audio settings or the main telephony
8204197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * application.
8214197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <p>
8224197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * This method has no effect if the device implements a fixed volume policy
823ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * as indicated by {@link #isVolumeFixed()}.
8244197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *
8259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param direction The direction to adjust the volume. One of
8264197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *            {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE},
8274197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *            {@link #ADJUST_SAME}, {@link #ADJUST_MUTE},
8284197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *            {@link #ADJUST_UNMUTE}, or {@link #ADJUST_TOGGLE_MUTE}.
8299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param flags One or more flags.
8309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustSuggestedStreamVolume(int, int, int)
8319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustStreamVolume(int, int, int)
8329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setStreamVolume(int, int, int)
833ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * @see #isVolumeFixed()
8349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void adjustVolume(int direction, int flags) {
8368dc50416a209e76bca38694d59d5779ffcf08d92Marco Nelissen        MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext());
837ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock        helper.sendAdjustVolumeBy(USE_DEFAULT_STREAM_TYPE, direction, flags);
8389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Adjusts the volume of the most relevant stream, or the given fallback
8429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * stream.
843fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * <p>
8444197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * This method should only be used by applications that replace the
8454197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * platform-wide management of audio settings or the main telephony
8464197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * application.
8474197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <p>
8484197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * This method has no effect if the device implements a fixed volume policy
849ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * as indicated by {@link #isVolumeFixed()}.
8504197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *
8519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param direction The direction to adjust the volume. One of
8524197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *            {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE},
8534197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *            {@link #ADJUST_SAME}, {@link #ADJUST_MUTE},
8544197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *            {@link #ADJUST_UNMUTE}, or {@link #ADJUST_TOGGLE_MUTE}.
8559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param suggestedStreamType The stream type that will be used if there
8564197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *            isn't a relevant stream. {@link #USE_DEFAULT_STREAM_TYPE} is
8574197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *            valid here.
8589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param flags One or more flags.
8599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustVolume(int, int)
8609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #adjustStreamVolume(int, int, int)
8619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setStreamVolume(int, int, int)
862ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * @see #isVolumeFixed()
8639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags) {
8658dc50416a209e76bca38694d59d5779ffcf08d92Marco Nelissen        MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext());
866ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock        helper.sendAdjustVolumeBy(suggestedStreamType, direction, flags);
867cbdb49dc5e1b993a0bc5c68dbfb9486bfa0cd762Mike Lockwood    }
868cbdb49dc5e1b993a0bc5c68dbfb9486bfa0cd762Mike Lockwood
869ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock    /** @hide */
870ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock    public void setMasterMute(boolean mute, int flags) {
871cbdb49dc5e1b993a0bc5c68dbfb9486bfa0cd762Mike Lockwood        IAudioService service = getService();
872cbdb49dc5e1b993a0bc5c68dbfb9486bfa0cd762Mike Lockwood        try {
8738dc50416a209e76bca38694d59d5779ffcf08d92Marco Nelissen            service.setMasterMute(mute, flags, getContext().getOpPackageName());
874cbdb49dc5e1b993a0bc5c68dbfb9486bfa0cd762Mike Lockwood        } catch (RemoteException e) {
875ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock            Log.e(TAG, "Dead object in setMasterMute", e);
8769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
8809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the current ringtone mode.
8819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
8829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The current ringtone mode, one of {@link #RINGER_MODE_NORMAL},
8839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}.
8849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setRingerMode(int)
8859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
8869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getRingerMode() {
8879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
8889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
889661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock            return service.getRingerModeExternal();
8909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
8919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in getRingerMode", e);
8929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return RINGER_MODE_NORMAL;
8939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
8949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
8959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
8969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
89772668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent     * Checks valid ringer mode values.
89872668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent     *
89972668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent     * @return true if the ringer mode indicated is valid, false otherwise.
90072668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent     *
90172668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent     * @see #setRingerMode(int)
90272668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent     * @hide
90372668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent     */
90472668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent    public static boolean isValidRingerMode(int ringerMode) {
90572668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent        if (ringerMode < 0 || ringerMode > RINGER_MODE_MAX) {
90672668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent            return false;
90772668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent        }
9089755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock        IAudioService service = getService();
9099755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock        try {
9109755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock            return service.isValidRingerMode(ringerMode);
9119755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock        } catch (RemoteException e) {
9129755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock            Log.e(TAG, "Dead object in isValidRingerMode", e);
9139755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock            return false;
9149755937ed90f06db45ff5fe4510950ae1516f8f9John Spurlock        }
91572668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent    }
91672668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent
91772668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent    /**
9189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the maximum volume index for a particular stream.
9199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
9209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param streamType The stream type whose maximum volume index is returned.
9219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The maximum valid volume index for the stream.
9229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getStreamVolume(int)
9239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
9249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getStreamMaxVolume(int streamType) {
9259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
9269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
927ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock            return service.getStreamMaxVolume(streamType);
9289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
9299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in getStreamMaxVolume", e);
9309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return 0;
9319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
935b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock     * Returns the minimum volume index for a particular stream.
936b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock     *
937b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock     * @param streamType The stream type whose minimum volume index is returned.
938b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock     * @return The minimum valid volume index for the stream.
939b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock     * @see #getStreamVolume(int)
940b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock     * @hide
941b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock     */
942b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock    public int getStreamMinVolume(int streamType) {
943b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock        IAudioService service = getService();
944b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock        try {
945b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock            return service.getStreamMinVolume(streamType);
946b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock        } catch (RemoteException e) {
947b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock            Log.e(TAG, "Dead object in getStreamMinVolume", e);
948b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock            return 0;
949b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock        }
950b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock    }
951b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock
952b6e19e3e0f0170d058636101e23f964196f6f4b2John Spurlock    /**
9539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the current volume index for a particular stream.
9549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
9559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param streamType The stream type whose volume index is returned.
9569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The current volume index for the stream.
9579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getStreamMaxVolume(int)
9589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setStreamVolume(int, int, int)
9599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
9609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getStreamVolume(int streamType) {
9619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
9629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
963ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock            return service.getStreamVolume(streamType);
9649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
9659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in getStreamVolume", e);
9669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return 0;
9679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
9689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
9699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
9709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
97125101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent     * Get last audible volume before stream was muted.
97225101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent     *
97325101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent     * @hide
97425101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent     */
97525101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent    public int getLastAudibleStreamVolume(int streamType) {
97625101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent        IAudioService service = getService();
97725101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent        try {
978ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock            return service.getLastAudibleStreamVolume(streamType);
97925101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent        } catch (RemoteException e) {
98025101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent            Log.e(TAG, "Dead object in getLastAudibleStreamVolume", e);
98125101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent            return 0;
98225101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent        }
98325101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent    }
98425101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent
98525101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent    /**
9866d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent     * Get the stream type whose volume is driving the UI sounds volume.
9876d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent     * UI sounds are screen lock/unlock, camera shutter, key clicks...
9884f0f120316cfcee5880191264885772677fff921John Spurlock     * It is assumed that this stream type is also tied to ringer mode changes.
9896d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent     * @hide
9906d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent     */
991ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock    public int getUiSoundsStreamType() {
9926d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent        IAudioService service = getService();
9936d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent        try {
994ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock            return service.getUiSoundsStreamType();
9956d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent        } catch (RemoteException e) {
996ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock            Log.e(TAG, "Dead object in getUiSoundsStreamType", e);
9976d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent            return STREAM_RING;
9986d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent        }
9996d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent    }
10006d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent
10016d5176638c2189595cede38fb92c3e7e8700e221Eric Laurent    /**
10029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the ringer mode.
10039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p>
10049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Silent mode will mute the volume and will not vibrate. Vibrate mode will
10059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * mute the volume and vibrate. Normal mode will be audible and may vibrate
10069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * according to user settings.
1007ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * <p>This method has no effect if the device implements a fixed volume policy
1008ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * as indicated by {@link #isVolumeFixed()}.
10099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param ringerMode The ringer mode, one of {@link #RINGER_MODE_NORMAL},
10109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}.
10119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getRingerMode()
1012ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * @see #isVolumeFixed()
10139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setRingerMode(int ringerMode) {
101572668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent        if (!isValidRingerMode(ringerMode)) {
101672668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent            return;
101772668b2c040b581b298b069f3b5af5ed7f212d89Eric Laurent        }
10189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
10199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
102029f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen            service.setRingerModeExternal(ringerMode, getContext().getOpPackageName());
10219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
10229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in setRingerMode", e);
10239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the volume index for a particular stream.
1028ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * <p>This method has no effect if the device implements a fixed volume policy
1029ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * as indicated by {@link #isVolumeFixed()}.
10309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param streamType The stream whose volume index should be set.
10319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param index The volume index to set. See
10329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #getStreamMaxVolume(int)} for the largest valid value.
10339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param flags One or more flags.
10349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getStreamMaxVolume(int)
10359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getStreamVolume(int)
1036ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * @see #isVolumeFixed()
10379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setStreamVolume(int streamType, int index, int flags) {
10399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
10409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
10418dc50416a209e76bca38694d59d5779ffcf08d92Marco Nelissen            service.setStreamVolume(streamType, index, flags, getContext().getOpPackageName());
10429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
10439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in setStreamVolume", e);
10449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
10459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10484197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * Solo or unsolo a particular stream.
10499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p>
10504197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * Do not use. This method has been deprecated and is now a no-op.
10514197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * {@link #requestAudioFocus} should be used for exclusive audio playback.
10529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param streamType The stream to be soloed/unsoloed.
10544197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @param state The required solo state: true for solo ON, false for solo
10554197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *            OFF
1056ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * @see #isVolumeFixed()
10574197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @deprecated Do not use. If you need exclusive audio playback use
10584197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *             {@link #requestAudioFocus}.
10599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10604197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik    @Deprecated
10619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setStreamSolo(int streamType, boolean state) {
10624197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik        Log.w(TAG, "setStreamSolo has been deprecated. Do not use.");
10639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
10649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
10659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
10669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Mute or unmute an audio stream.
10679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p>
10684197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * This method should only be used by applications that replace the
10694197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * platform-wide management of audio settings or the main telephony
10704197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * application.
10719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p>
10724197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * This method has no effect if the device implements a fixed volume policy
1073ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * as indicated by {@link #isVolumeFixed()}.
10744197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <p>
10754197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * This method was deprecated in API level 22. Prior to API level 22 this
10764197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * method had significantly different behavior and should be used carefully.
10774197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * The following applies only to pre-22 platforms:
10784197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <ul>
10794197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <li>The mute command is protected against client process death: if a
10804197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * process with an active mute request on a stream dies, this stream will be
10814197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * unmuted automatically.</li>
10824197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <li>The mute requests for a given stream are cumulative: the AudioManager
10834197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * can receive several mute requests from one or more clients and the stream
10844197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * will be unmuted only when the same number of unmute requests are
10854197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * received.</li>
10864197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * <li>For a better user experience, applications MUST unmute a muted stream
10874197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * in onPause() and mute is again in onResume() if appropriate.</li>
10884197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * </ul>
10899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
10909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param streamType The stream to be muted/unmuted.
10914197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @param state The required mute state: true for mute ON, false for mute
10924197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *            OFF
1093ba207e76b2c5ebc01ececff351107d19a3134f3aEric Laurent     * @see #isVolumeFixed()
10944197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @deprecated Use {@link #adjustStreamVolume(int, int, int)} with
10954197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     *             {@link #ADJUST_MUTE} or {@link #ADJUST_UNMUTE} instead.
10969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
10974197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik    @Deprecated
10989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setStreamMute(int streamType, boolean state) {
10994197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik        Log.w(TAG, "setStreamMute is deprecated. adjustStreamVolume should be used instead.");
11004197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik        int direction = state ? ADJUST_MUTE : ADJUST_UNMUTE;
11014197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik        if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) {
11024197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik            adjustSuggestedStreamVolume(direction, streamType, 0);
11034197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik        } else {
11044197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik            adjustStreamVolume(streamType, direction, 0);
11059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
11094197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * Returns the current mute state for a particular stream.
111025101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent     *
11114197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @param streamType The stream to get mute state for.
11124197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @return The mute state for the given stream.
11134197cb60bc74629fe4c04ab10cb3b1c9a7427d24RoboErik     * @see #adjustStreamVolume(int, int, int)
111425101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent     */
111525101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent    public boolean isStreamMute(int streamType) {
111625101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent        IAudioService service = getService();
111725101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent        try {
1118ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock            return service.isStreamMute(streamType);
111925101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent        } catch (RemoteException e) {
112025101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent            Log.e(TAG, "Dead object in isStreamMute", e);
112125101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent            return false;
112225101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent        }
112325101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent    }
112425101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent
112525101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent    /**
1126ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood     * get master mute state.
1127ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood     *
1128ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood     * @hide
1129ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood     */
1130ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood    public boolean isMasterMute() {
1131ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood        IAudioService service = getService();
1132ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood        try {
1133ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood            return service.isMasterMute();
1134ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood        } catch (RemoteException e) {
1135ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood            Log.e(TAG, "Dead object in isMasterMute", e);
1136ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood            return false;
1137ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood        }
1138ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood    }
1139ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood
1140ce952c8e13c535bedde77bcdb94dfcc7508475aaMike Lockwood    /**
1141402f7f29634a9f68e7929be828a927a3e2f5efe9Eric Laurent     * forces the stream controlled by hard volume keys
1142402f7f29634a9f68e7929be828a927a3e2f5efe9Eric Laurent     * specifying streamType == -1 releases control to the
1143402f7f29634a9f68e7929be828a927a3e2f5efe9Eric Laurent     * logic.
1144402f7f29634a9f68e7929be828a927a3e2f5efe9Eric Laurent     *
1145402f7f29634a9f68e7929be828a927a3e2f5efe9Eric Laurent     * @hide
1146402f7f29634a9f68e7929be828a927a3e2f5efe9Eric Laurent     */
1147402f7f29634a9f68e7929be828a927a3e2f5efe9Eric Laurent    public void forceVolumeControlStream(int streamType) {
114845c90cefd13a03b852bb4b8da4be218876cbbb32Eric Laurent        IAudioService service = getService();
114945c90cefd13a03b852bb4b8da4be218876cbbb32Eric Laurent        try {
115045c90cefd13a03b852bb4b8da4be218876cbbb32Eric Laurent            service.forceVolumeControlStream(streamType, mICallBack);
115145c90cefd13a03b852bb4b8da4be218876cbbb32Eric Laurent        } catch (RemoteException e) {
115245c90cefd13a03b852bb4b8da4be218876cbbb32Eric Laurent            Log.e(TAG, "Dead object in forceVolumeControlStream", e);
115345c90cefd13a03b852bb4b8da4be218876cbbb32Eric Laurent        }
1154402f7f29634a9f68e7929be828a927a3e2f5efe9Eric Laurent    }
1155402f7f29634a9f68e7929be828a927a3e2f5efe9Eric Laurent
1156402f7f29634a9f68e7929be828a927a3e2f5efe9Eric Laurent    /**
11579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns whether a particular type should vibrate according to user
11589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * settings and the current ringer mode.
11599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p>
11609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This shouldn't be needed by most clients that use notifications to
11619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * vibrate. The notification manager will not vibrate if the policy doesn't
11629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * allow it, so the client should always set a vibrate pattern and let the
11639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * notification manager control whether or not to actually vibrate.
11649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
11659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param vibrateType The type of vibrate. One of
11669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #VIBRATE_TYPE_NOTIFICATION} or
11679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #VIBRATE_TYPE_RINGER}.
11689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return Whether the type should vibrate at the instant this method is
11699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         called.
11709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setVibrateSetting(int, int)
11719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getVibrateSetting(int)
1172cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * @deprecated Applications should maintain their own vibrate policy based on
1173cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * current ringer mode that can be queried via {@link #getRingerMode()}.
11749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
11759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean shouldVibrate(int vibrateType) {
11769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
11779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
11789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return service.shouldVibrate(vibrateType);
11799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
11809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in shouldVibrate", e);
11819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return false;
11829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
11839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
11849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
11859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
11869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns whether the user's vibrate setting for a vibrate type.
11879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * <p>
11889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * This shouldn't be needed by most clients that want to vibrate, instead
11899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * see {@link #shouldVibrate(int)}.
11909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
11919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param vibrateType The type of vibrate. One of
11929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #VIBRATE_TYPE_NOTIFICATION} or
11939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #VIBRATE_TYPE_RINGER}.
11949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return The vibrate setting, one of {@link #VIBRATE_SETTING_ON},
11959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         {@link #VIBRATE_SETTING_OFF}, or
11969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         {@link #VIBRATE_SETTING_ONLY_SILENT}.
11979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #setVibrateSetting(int, int)
11989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #shouldVibrate(int)
1199cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * @deprecated Applications should maintain their own vibrate policy based on
1200cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * current ringer mode that can be queried via {@link #getRingerMode()}.
12019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getVibrateSetting(int vibrateType) {
12039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
12049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
12059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return service.getVibrateSetting(vibrateType);
12069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
12079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in getVibrateSetting", e);
12089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return VIBRATE_SETTING_OFF;
12099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
12139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the setting for when the vibrate type should vibrate.
1214fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * <p>
1215fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * This method should only be used by applications that replace the platform-wide
1216fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * management of audio settings or the main telephony application.
12179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
12189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param vibrateType The type of vibrate. One of
12199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #VIBRATE_TYPE_NOTIFICATION} or
12209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #VIBRATE_TYPE_RINGER}.
12219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param vibrateSetting The vibrate setting, one of
12229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #VIBRATE_SETTING_ON},
12239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #VIBRATE_SETTING_OFF}, or
12249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #VIBRATE_SETTING_ONLY_SILENT}.
12259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #getVibrateSetting(int)
12269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #shouldVibrate(int)
1227cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * @deprecated Applications should maintain their own vibrate policy based on
1228cd1cd73baec1f93d21f67fdded5466e507431ebaEric Laurent     * current ringer mode that can be queried via {@link #getRingerMode()}.
12299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setVibrateSetting(int vibrateType, int vibrateSetting) {
12319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
12329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
12339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            service.setVibrateSetting(vibrateType, vibrateSetting);
12349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
12359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in setVibrateSetting", e);
12369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
12379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
12409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the speakerphone on or off.
1241fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * <p>
1242fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * This method should only be used by applications that replace the platform-wide
1243fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * management of audio settings or the main telephony application.
12449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
12459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param on set <var>true</var> to turn on speakerphone;
12469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *           <var>false</var> to turn it off
12479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setSpeakerphoneOn(boolean on){
1249c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        IAudioService service = getService();
1250c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        try {
1251c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent            service.setSpeakerphoneOn(on);
1252c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        } catch (RemoteException e) {
1253c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent            Log.e(TAG, "Dead object in setSpeakerphoneOn", e);
1254a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        }
12559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
12569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
12589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Checks whether the speakerphone is on or off.
12599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
12609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if speakerphone is on, false if it's off
12619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
12629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean isSpeakerphoneOn() {
1263c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        IAudioService service = getService();
1264c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        try {
1265c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent            return service.isSpeakerphoneOn();
1266c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        } catch (RemoteException e) {
1267c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent            Log.e(TAG, "Dead object in isSpeakerphoneOn", e);
1268a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent            return false;
1269a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        }
12709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     }
12719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
12723def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    //====================================================================
12733def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    // Bluetooth SCO control
12743def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    /**
12753def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * Sticky broadcast intent action indicating that the bluetoooth SCO audio
127695b88fbe744d8ce53b5f54f6fcd90c55094a8d14Eric Laurent     * connection state has changed. The intent contains on extra {@link #EXTRA_SCO_AUDIO_STATE}
12773def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * indicating the new state which is either {@link #SCO_AUDIO_STATE_DISCONNECTED}
12783def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * or {@link #SCO_AUDIO_STATE_CONNECTED}
12793def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     *
12803def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * @see #startBluetoothSco()
1281dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * @deprecated Use  {@link #ACTION_SCO_AUDIO_STATE_UPDATED} instead
12823def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     */
1283dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent    @Deprecated
12843def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
12853def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    public static final String ACTION_SCO_AUDIO_STATE_CHANGED =
12863def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent            "android.media.SCO_AUDIO_STATE_CHANGED";
1287dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent
1288dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     /**
1289dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * Sticky broadcast intent action indicating that the bluetoooth SCO audio
1290dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * connection state has been updated.
1291dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * <p>This intent has two extras:
1292dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * <ul>
1293dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     *   <li> {@link #EXTRA_SCO_AUDIO_STATE} - The new SCO audio state. </li>
1294dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     *   <li> {@link #EXTRA_SCO_AUDIO_PREVIOUS_STATE}- The previous SCO audio state. </li>
1295dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * </ul>
1296dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * <p> EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE can be any of:
1297dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * <ul>
1298dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     *   <li> {@link #SCO_AUDIO_STATE_DISCONNECTED}, </li>
1299dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     *   <li> {@link #SCO_AUDIO_STATE_CONNECTING} or </li>
1300dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     *   <li> {@link #SCO_AUDIO_STATE_CONNECTED}, </li>
1301dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * </ul>
1302dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * @see #startBluetoothSco()
1303dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     */
1304dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
1305dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent    public static final String ACTION_SCO_AUDIO_STATE_UPDATED =
1306dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent            "android.media.ACTION_SCO_AUDIO_STATE_UPDATED";
1307dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent
13083def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    /**
1309dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * Extra for intent {@link #ACTION_SCO_AUDIO_STATE_CHANGED} or
1310dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * {@link #ACTION_SCO_AUDIO_STATE_UPDATED} containing the new bluetooth SCO connection state.
13113def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     */
13123def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    public static final String EXTRA_SCO_AUDIO_STATE =
13133def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent            "android.media.extra.SCO_AUDIO_STATE";
13143def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent
13153def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    /**
1316dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * Extra for intent {@link #ACTION_SCO_AUDIO_STATE_UPDATED} containing the previous
1317dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * bluetooth SCO connection state.
1318dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     */
1319dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent    public static final String EXTRA_SCO_AUDIO_PREVIOUS_STATE =
1320dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent            "android.media.extra.SCO_AUDIO_PREVIOUS_STATE";
1321dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent
1322dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent    /**
1323dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * Value for extra EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE
1324dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * indicating that the SCO audio channel is not established
13253def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     */
13263def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    public static final int SCO_AUDIO_STATE_DISCONNECTED = 0;
13273def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    /**
1328dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * Value for extra {@link #EXTRA_SCO_AUDIO_STATE} or {@link #EXTRA_SCO_AUDIO_PREVIOUS_STATE}
1329dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * indicating that the SCO audio channel is established
13303def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     */
13313def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    public static final int SCO_AUDIO_STATE_CONNECTED = 1;
13323def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    /**
1333dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * Value for extra EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE
1334dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * indicating that the SCO audio channel is being established
1335dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     */
1336dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent    public static final int SCO_AUDIO_STATE_CONNECTING = 2;
1337dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent    /**
1338dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * Value for extra EXTRA_SCO_AUDIO_STATE indicating that
13393def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * there was an error trying to obtain the state
13403def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     */
13413def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    public static final int SCO_AUDIO_STATE_ERROR = -1;
13423def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent
13433def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent
13443def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    /**
13453def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * Indicates if current platform supports use of SCO for off call use cases.
13463def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * Application wanted to use bluetooth SCO audio when the phone is not in call
13472ac2afeac989ea1dc326b0db996d6c6c8e00cc29Jean-Michel Trivi     * must first call this method to make sure that the platform supports this
13483def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * feature.
13493def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * @return true if bluetooth SCO can be used for audio when not in call
13503def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     *         false otherwise
13513def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * @see #startBluetoothSco()
13523def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    */
13533def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    public boolean isBluetoothScoAvailableOffCall() {
135429f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        return getContext().getResources().getBoolean(
13553def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent               com.android.internal.R.bool.config_bluetooth_sco_off_call);
13563def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    }
13573def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent
13583def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    /**
13593def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * Start bluetooth SCO audio connection.
13603def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * <p>Requires Permission:
13613def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     *   {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}.
13623def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * <p>This method can be used by applications wanting to send and received audio
13633def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * to/from a bluetooth SCO headset while the phone is not in call.
13643def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * <p>As the SCO connection establishment can take several seconds,
13653def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * applications should not rely on the connection to be available when the method
1366dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * returns but instead register to receive the intent {@link #ACTION_SCO_AUDIO_STATE_UPDATED}
13673def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * and wait for the state to be {@link #SCO_AUDIO_STATE_CONNECTED}.
1368dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * <p>As the ACTION_SCO_AUDIO_STATE_UPDATED intent is sticky, the application can check the SCO
1369dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * audio state before calling startBluetoothSco() by reading the intent returned by the receiver
1370dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * registration. If the state is already CONNECTED, no state change will be received via the
1371dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * intent after calling startBluetoothSco(). It is however useful to call startBluetoothSco()
1372dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * so that the connection stays active in case the current initiator stops the connection.
1373dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * <p>Unless the connection is already active as described above, the state will always
1374dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * transition from DISCONNECTED to CONNECTING and then either to CONNECTED if the connection
1375dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * succeeds or back to DISCONNECTED if the connection fails (e.g no headset is connected).
1376dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * <p>When finished with the SCO connection or if the establishment fails, the application must
1377dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * call {@link #stopBluetoothSco()} to clear the request and turn down the bluetooth connection.
13783def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * <p>Even if a SCO connection is established, the following restrictions apply on audio
13793def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * output streams so that they can be routed to SCO headset:
1380dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * <ul>
1381dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     *   <li> the stream type must be {@link #STREAM_VOICE_CALL} </li>
1382dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     *   <li> the format must be mono </li>
1383dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     *   <li> the sampling must be 16kHz or 8kHz </li>
1384dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * </ul>
13853def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * <p>The following restrictions apply on input streams:
1386dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * <ul>
1387dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     *   <li> the format must be mono </li>
1388dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     *   <li> the sampling must be 8kHz </li>
1389dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * </ul>
13903def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * <p>Note that the phone application always has the priority on the usage of the SCO
13913def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * connection for telephony. If this method is called while the phone is in call
13923def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * it will be ignored. Similarly, if a call is received or sent while an application
13933def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * is using the SCO connection, the connection will be lost for the application and NOT
13943def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * returned automatically when the call ends.
139583900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * <p>NOTE: up to and including API version
139683900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}, this method initiates a virtual
139783900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * voice call to the bluetooth headset.
139883900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * After API version {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2} only a raw SCO audio
139983900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * connection is established.
14003def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * @see #stopBluetoothSco()
1401dc03c61fe3cd8d0805480e48a974986439977a60Eric Laurent     * @see #ACTION_SCO_AUDIO_STATE_UPDATED
14023def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     */
14033def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    public void startBluetoothSco(){
14043def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent        IAudioService service = getService();
14053def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent        try {
1406926ebb860a8b5cbdec17f07404272be7c484fd79Marco Nelissen            service.startBluetoothSco(mICallBack,
140729f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen                    getContext().getApplicationInfo().targetSdkVersion);
14083def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent        } catch (RemoteException e) {
14093def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent            Log.e(TAG, "Dead object in startBluetoothSco", e);
14103def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent        }
14113def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    }
14123def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent
14133def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    /**
14140daab220b2379aad33321364e8a39d2e3d4c3551Jean-Michel Trivi     * @hide
141583900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * Start bluetooth SCO audio connection in virtual call mode.
141683900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * <p>Requires Permission:
141783900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     *   {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}.
141883900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * <p>Similar to {@link #startBluetoothSco()} with explicit selection of virtual call mode.
141983900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * Telephony and communication applications (VoIP, Video Chat) should preferably select
142083900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * virtual call mode.
142183900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * Applications using voice input for search or commands should first try raw audio connection
142283900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * with {@link #startBluetoothSco()} and fall back to startBluetoothScoVirtualCall() in case of
142383900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * failure.
142483900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * @see #startBluetoothSco()
142583900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * @see #stopBluetoothSco()
142683900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     * @see #ACTION_SCO_AUDIO_STATE_UPDATED
142783900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent     */
142883900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent    public void startBluetoothScoVirtualCall() {
142983900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent        IAudioService service = getService();
143083900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent        try {
143183900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent            service.startBluetoothScoVirtualCall(mICallBack);
143283900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent        } catch (RemoteException e) {
143383900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent            Log.e(TAG, "Dead object in startBluetoothScoVirtualCall", e);
143483900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent        }
143583900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent    }
143683900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent
143783900754f357616b9e56eaf7fc85f49b8906e987Eric Laurent    /**
14383def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * Stop bluetooth SCO audio connection.
14393def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * <p>Requires Permission:
14403def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     *   {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}.
14413def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * <p>This method must be called by applications having requested the use of
14420daab220b2379aad33321364e8a39d2e3d4c3551Jean-Michel Trivi     * bluetooth SCO audio with {@link #startBluetoothSco()} when finished with the SCO
14430daab220b2379aad33321364e8a39d2e3d4c3551Jean-Michel Trivi     * connection or if connection fails.
14443def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     * @see #startBluetoothSco()
14453def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent     */
14460daab220b2379aad33321364e8a39d2e3d4c3551Jean-Michel Trivi    // Also used for connections started with {@link #startBluetoothScoVirtualCall()}
14473def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    public void stopBluetoothSco(){
14483def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent        IAudioService service = getService();
14493def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent        try {
14503def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent            service.stopBluetoothSco(mICallBack);
14513def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent        } catch (RemoteException e) {
14523def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent            Log.e(TAG, "Dead object in stopBluetoothSco", e);
14533def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent        }
14543def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent    }
14553def1eec2baed0b8845ec32c871e249dc533a9d9Eric Laurent
14569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1457a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * Request use of Bluetooth SCO headset for communications.
1458fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * <p>
1459fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * This method should only be used by applications that replace the platform-wide
1460fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * management of audio settings or the main telephony application.
14619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1462a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * @param on set <var>true</var> to use bluetooth SCO for communications;
1463a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     *               <var>false</var> to not use bluetooth SCO for communications
14649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
14659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setBluetoothScoOn(boolean on){
1466c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        IAudioService service = getService();
1467c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        try {
1468c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent            service.setBluetoothScoOn(on);
1469c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        } catch (RemoteException e) {
1470c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent            Log.e(TAG, "Dead object in setBluetoothScoOn", e);
1471a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        }
14729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1475a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * Checks whether communications use Bluetooth SCO.
14769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1477a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * @return true if SCO is used for communications;
14789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         false if otherwise
14799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
14809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean isBluetoothScoOn() {
1481c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        IAudioService service = getService();
1482c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        try {
1483c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent            return service.isBluetoothScoOn();
1484c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent        } catch (RemoteException e) {
1485c42ac9d4d03f62c3a1ba197a28a81fda44bd8b7fEric Laurent            Log.e(TAG, "Dead object in isBluetoothScoOn", e);
1486a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent            return false;
1487a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        }
14889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1491242b33870b51ea962a1d5c077d71c648eccc2b7aEric Laurent     * @param on set <var>true</var> to route A2DP audio to/from Bluetooth
1492242b33870b51ea962a1d5c077d71c648eccc2b7aEric Laurent     *           headset; <var>false</var> disable A2DP audio
1493a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * @deprecated Do not use.
14949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1495a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated public void setBluetoothA2dpOn(boolean on){
14969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
14979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
14989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1499242b33870b51ea962a1d5c077d71c648eccc2b7aEric Laurent     * Checks whether A2DP audio routing to the Bluetooth headset is on or off.
15009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1501242b33870b51ea962a1d5c077d71c648eccc2b7aEric Laurent     * @return true if A2DP audio is being routed to/from Bluetooth headset;
1502242b33870b51ea962a1d5c077d71c648eccc2b7aEric Laurent     *         false if otherwise
15039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
15049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean isBluetoothA2dpOn() {
1505242b33870b51ea962a1d5c077d71c648eccc2b7aEric Laurent        if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_BLUETOOTH_A2DP,"")
1506242b33870b51ea962a1d5c077d71c648eccc2b7aEric Laurent            == AudioSystem.DEVICE_STATE_UNAVAILABLE) {
1507a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent            return false;
1508242b33870b51ea962a1d5c077d71c648eccc2b7aEric Laurent        } else {
1509242b33870b51ea962a1d5c077d71c648eccc2b7aEric Laurent            return true;
1510a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        }
15119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets audio routing to the wired headset on or off.
15159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param on set <var>true</var> to route audio to/from wired
15179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *           headset; <var>false</var> disable wired headset audio
1518a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * @deprecated Do not use.
15199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1520a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated public void setWiredHeadsetOn(boolean on){
15219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
1524497b3fe7257d5bbee7762455c6988915bde21601Eric Laurent     * Checks whether a wired headset is connected or not.
1525497b3fe7257d5bbee7762455c6988915bde21601Eric Laurent     * <p>This is not a valid indication that audio playback is
1526497b3fe7257d5bbee7762455c6988915bde21601Eric Laurent     * actually over the wired headset as audio routing depends on other conditions.
15279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
1528497b3fe7257d5bbee7762455c6988915bde21601Eric Laurent     * @return true if a wired headset is connected.
15299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *         false if otherwise
1530497b3fe7257d5bbee7762455c6988915bde21601Eric Laurent     * @deprecated Use only to check is a headset is connected or not.
15319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
15329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean isWiredHeadsetOn() {
15338b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten        if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADSET,"")
15346015a9715421a339c2b7bb63f9166504e3488179Eric Laurent                == AudioSystem.DEVICE_STATE_UNAVAILABLE &&
15358b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten            AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADPHONE,"")
1536a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent                == AudioSystem.DEVICE_STATE_UNAVAILABLE) {
1537a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent            return false;
1538a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        } else {
1539a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent            return true;
1540a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        }
15419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the microphone mute on or off.
1545fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * <p>
1546fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * This method should only be used by applications that replace the platform-wide
1547fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * management of audio settings or the main telephony application.
15489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param on set <var>true</var> to mute the microphone;
15509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *           <var>false</var> to turn mute off
15519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
15529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setMicrophoneMute(boolean on){
155322c921a910d236abf3a1705a02541a49fdaf3a14Emily Bernier        IAudioService service = getService();
155422c921a910d236abf3a1705a02541a49fdaf3a14Emily Bernier        try {
155529f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen            service.setMicrophoneMute(on, getContext().getOpPackageName());
155622c921a910d236abf3a1705a02541a49fdaf3a14Emily Bernier        } catch (RemoteException e) {
155722c921a910d236abf3a1705a02541a49fdaf3a14Emily Bernier            Log.e(TAG, "Dead object in setMicrophoneMute", e);
155822c921a910d236abf3a1705a02541a49fdaf3a14Emily Bernier        }
15599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Checks whether the microphone mute is on or off.
15639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if microphone is muted, false if it's not
15659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
15669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean isMicrophoneMute() {
1567a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        return AudioSystem.isMicrophoneMuted();
15689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the audio mode.
1572fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * <p>
1573fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * The audio mode encompasses audio routing AND the behavior of
1574fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * the telephony layer. Therefore this method should only be used by applications that
1575fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * replace the platform-wide management of audio settings or the main telephony application.
1576fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * In particular, the {@link #MODE_IN_CALL} mode should only be used by the telephony
1577fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * application when it places a phone call, as it will cause signals from the radio layer
1578fccb25d2074358c54d4c60eb321aeaa08a5de59aJean-Michel Trivi     * to feed the platform mixer.
15799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15808aa798b244e32e227bad97d40ddfd7d3557030a3Jean-Michel Trivi     * @param mode  the requested audio mode ({@link #MODE_NORMAL}, {@link #MODE_RINGTONE},
15818aa798b244e32e227bad97d40ddfd7d3557030a3Jean-Michel Trivi     *              {@link #MODE_IN_CALL} or {@link #MODE_IN_COMMUNICATION}).
15829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              Informs the HAL about the current audio state so that
15839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              it can route the audio appropriately.
15849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
15859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setMode(int mode) {
15869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
15879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
1588a80ac05f85962949d5072831200d86a7023575fbMarco Nelissen            service.setMode(mode, mICallBack, mApplicationContext.getOpPackageName());
15899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
15909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in setMode", e);
15919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
15929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
15939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
15949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
15959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the current audio mode.
15969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
15978aa798b244e32e227bad97d40ddfd7d3557030a3Jean-Michel Trivi     * @return      the current audio mode ({@link #MODE_NORMAL}, {@link #MODE_RINGTONE},
15988aa798b244e32e227bad97d40ddfd7d3557030a3Jean-Michel Trivi     *              {@link #MODE_IN_CALL} or {@link #MODE_IN_COMMUNICATION}).
15999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *              Returns the current current audio state from the HAL.
16009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getMode() {
16029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
16039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
16049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return service.getMode();
16059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
16069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in getMode", e);
16079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return MODE_INVALID;
16089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
16099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* modes for setMode/getMode/setRoute/getRoute */
16129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Audio harware modes.
16149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Invalid audio mode.
16179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int MODE_INVALID            = AudioSystem.MODE_INVALID;
16199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Current audio mode. Used to apply audio routing to current mode.
16219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int MODE_CURRENT            = AudioSystem.MODE_CURRENT;
16239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Normal audio mode: not ringing and no call established.
16259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int MODE_NORMAL             = AudioSystem.MODE_NORMAL;
16279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Ringing audio mode. An incoming is being signaled.
16299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int MODE_RINGTONE           = AudioSystem.MODE_RINGTONE;
16319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16328f677d66d9c3ba34c97e69b2bb9e161f129af0eeJean-Michel Trivi     * In call audio mode. A telephony call is established.
16339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int MODE_IN_CALL            = AudioSystem.MODE_IN_CALL;
16358f677d66d9c3ba34c97e69b2bb9e161f129af0eeJean-Michel Trivi    /**
16368f677d66d9c3ba34c97e69b2bb9e161f129af0eeJean-Michel Trivi     * In communication audio mode. An audio/video chat or VoIP call is established.
16378f677d66d9c3ba34c97e69b2bb9e161f129af0eeJean-Michel Trivi     */
16388f677d66d9c3ba34c97e69b2bb9e161f129af0eeJean-Michel Trivi    public static final int MODE_IN_COMMUNICATION   = AudioSystem.MODE_IN_COMMUNICATION;
16399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Routing bits for setRouting/getRouting API */
16419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Routing audio output to earpiece
16434a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
16444a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * setBluetoothScoOn() methods instead.
16459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1646a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated public static final int ROUTE_EARPIECE          = AudioSystem.ROUTE_EARPIECE;
16479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16484a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * Routing audio output to speaker
16494a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
16504a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * setBluetoothScoOn() methods instead.
16519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1652a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated public static final int ROUTE_SPEAKER           = AudioSystem.ROUTE_SPEAKER;
16539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @deprecated use {@link #ROUTE_BLUETOOTH_SCO}
16554a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
16564a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * setBluetoothScoOn() methods instead.
16579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
16589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    @Deprecated public static final int ROUTE_BLUETOOTH = AudioSystem.ROUTE_BLUETOOTH_SCO;
16599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Routing audio output to bluetooth SCO
16614a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
16624a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * setBluetoothScoOn() methods instead.
16639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1664a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated public static final int ROUTE_BLUETOOTH_SCO     = AudioSystem.ROUTE_BLUETOOTH_SCO;
16659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Routing audio output to headset
16674a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
16684a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * setBluetoothScoOn() methods instead.
16699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1670a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated public static final int ROUTE_HEADSET           = AudioSystem.ROUTE_HEADSET;
16719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Routing audio output to bluetooth A2DP
16734a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
16744a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * setBluetoothScoOn() methods instead.
16759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1676a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated public static final int ROUTE_BLUETOOTH_A2DP    = AudioSystem.ROUTE_BLUETOOTH_A2DP;
16779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Used for mask parameter of {@link #setRouting(int,int,int)}.
16794a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
16804a51c20ce607c74914f90fd897f04080121ac13bDianne Hackborn     * setBluetoothScoOn() methods instead.
16819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1682a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated public static final int ROUTE_ALL               = AudioSystem.ROUTE_ALL;
16839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
16849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
16859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets the audio routing for a specified mode
16869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
16879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param mode   audio mode to change route. E.g., MODE_RINGTONE.
16889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param routes bit vector of routes requested, created from one or
16899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *               more of ROUTE_xxx types. Set bits indicate that route should be on
16909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param mask   bit vector of routes to change, created from one or more of
16919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * ROUTE_xxx types. Unset bits indicate the route should be left unchanged
1692b9c9d260f21b321527c4622a123af9767630d94dEric Laurent     *
1693b9c9d260f21b321527c4622a123af9767630d94dEric Laurent     * @deprecated   Do not set audio routing directly, use setSpeakerphoneOn(),
1694a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * setBluetoothScoOn() methods instead.
16959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1696a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated
16979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void setRouting(int mode, int routes, int mask) {
16989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
16999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
17019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Returns the current audio routing bit vector for a specified mode.
17029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param mode audio mode to get route (e.g., MODE_RINGTONE)
17049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return an audio route bit vector that can be compared with ROUTE_xxx
17059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * bits
1706b9c9d260f21b321527c4622a123af9767630d94dEric Laurent     * @deprecated   Do not query audio routing directly, use isSpeakerphoneOn(),
1707b9c9d260f21b321527c4622a123af9767630d94dEric Laurent     * isBluetoothScoOn(), isBluetoothA2dpOn() and isWiredHeadsetOn() methods instead.
17089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1709b9c9d260f21b321527c4622a123af9767630d94dEric Laurent    @Deprecated
17109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public int getRouting(int mode) {
1711a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        return -1;
17129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
17139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
17159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Checks whether any music is active.
17169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @return true if any music tracks are active.
17189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
17199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public boolean isMusicActive() {
172025101b0b9a84571ead15b26e9f4cd9c4298d7823Eric Laurent        return AudioSystem.isStreamActive(STREAM_MUSIC, 0);
17219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
17229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17233114ce3861f20f9a5c2c59dd2629197a1f4874a8Jean-Michel Trivi    /**
17243114ce3861f20f9a5c2c59dd2629197a1f4874a8Jean-Michel Trivi     * @hide
1725679d5046f245ea8b39f9b5596195f3468808dd54Jean-Michel Trivi     * Checks whether any music or media is actively playing on a remote device (e.g. wireless
1726679d5046f245ea8b39f9b5596195f3468808dd54Jean-Michel Trivi     *   display). Note that BT audio sinks are not considered remote devices.
1727679d5046f245ea8b39f9b5596195f3468808dd54Jean-Michel Trivi     * @return true if {@link AudioManager#STREAM_MUSIC} is active on a remote device
1728679d5046f245ea8b39f9b5596195f3468808dd54Jean-Michel Trivi     */
1729679d5046f245ea8b39f9b5596195f3468808dd54Jean-Michel Trivi    public boolean isMusicActiveRemotely() {
1730679d5046f245ea8b39f9b5596195f3468808dd54Jean-Michel Trivi        return AudioSystem.isStreamActiveRemotely(STREAM_MUSIC, 0);
1731679d5046f245ea8b39f9b5596195f3468808dd54Jean-Michel Trivi    }
1732679d5046f245ea8b39f9b5596195f3468808dd54Jean-Michel Trivi
1733679d5046f245ea8b39f9b5596195f3468808dd54Jean-Michel Trivi    /**
1734679d5046f245ea8b39f9b5596195f3468808dd54Jean-Michel Trivi     * @hide
17352380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     * Checks whether the current audio focus is exclusive.
17362380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     * @return true if the top of the audio focus stack requested focus
17372380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     *     with {@link #AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE}
17382380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     */
17392380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi    public boolean isAudioFocusExclusive() {
17402380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi        IAudioService service = getService();
17412380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi        try {
17422380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi            return service.getCurrentAudioFocus() == AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE;
17432380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi        } catch (RemoteException e) {
17442380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi            Log.e(TAG, "Dead object in isAudioFocusExclusive()", e);
17452380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi            return false;
17462380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi        }
17472380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi    }
17482380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi
17492380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi    /**
17508a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi     * Return a new audio session identifier not associated with any player or effect.
17519e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     * An audio session identifier is a system wide unique identifier for a set of audio streams
17529e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     * (one or more mixed together).
17539e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     * <p>The primary use of the audio session ID is to associate audio effects to audio players,
17549e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     * such as {@link MediaPlayer} or {@link AudioTrack}: all audio effects sharing the same audio
17559e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     * session ID will be applied to the mixed audio content of the players that share the same
17569e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     * audio session.
17579e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     * <p>This method can for instance be used when creating one of the
17589e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     * {@link android.media.audiofx.AudioEffect} objects to define the audio session of the effect,
17599e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     * or to specify a session for a speech synthesis utterance
17609e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     * in {@link android.speech.tts.TextToSpeech.Engine}.
17618a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi     * @return a new unclaimed and unused audio session identifier, or {@link #ERROR} when the
17629e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     *   system failed to generate a new session, a condition in which audio playback or recording
17639e477e408433fe1bd0c695b1e7a0738a76009cfdJean-Michel Trivi     *   will subsequently fail as well.
17648a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi     */
1765289cc8e887f786f557faab226a1e01abb9a632a6Jean-Michel Trivi    public int generateAudioSessionId() {
17668a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi        int session = AudioSystem.newAudioSessionId();
17678a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi        if (session > 0) {
17688a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi            return session;
17698a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi        } else {
1770289cc8e887f786f557faab226a1e01abb9a632a6Jean-Michel Trivi            Log.e(TAG, "Failure to generate a new audio session ID");
17718a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi            return ERROR;
17728a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi        }
17738a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi    }
17748a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi
1775289cc8e887f786f557faab226a1e01abb9a632a6Jean-Michel Trivi    /**
1776289cc8e887f786f557faab226a1e01abb9a632a6Jean-Michel Trivi     * A special audio session ID to indicate that the audio session ID isn't known and the
1777289cc8e887f786f557faab226a1e01abb9a632a6Jean-Michel Trivi     * framework should generate a new value. This can be used when building a new
1778289cc8e887f786f557faab226a1e01abb9a632a6Jean-Michel Trivi     * {@link AudioTrack} instance with
1779289cc8e887f786f557faab226a1e01abb9a632a6Jean-Michel Trivi     * {@link AudioTrack#AudioTrack(AudioAttributes, AudioFormat, int, int, int)}.
1780289cc8e887f786f557faab226a1e01abb9a632a6Jean-Michel Trivi     */
1781289cc8e887f786f557faab226a1e01abb9a632a6Jean-Michel Trivi    public static final int AUDIO_SESSION_ID_GENERATE = AudioSystem.AUDIO_SESSION_ALLOCATE;
1782289cc8e887f786f557faab226a1e01abb9a632a6Jean-Michel Trivi
17838a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi
17849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /*
17859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Sets a generic audio configuration parameter. The use of these parameters
17869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * are platform dependant, see libaudio
17879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * ** Temporary interface - DO NOT USE
17899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * TODO: Replace with a more generic key:value get/set mechanism
17919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *
17929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * param key   name of parameter to set. Must not be null.
17939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * param value value of parameter. Must not be null.
17949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
17959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
17969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide
1797ba50b97cff80e73620a0e3d13cae169e095974a7Dianne Hackborn     * @deprecated Use {@link #setParameters(String)} instead
17989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1799a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    @Deprecated public void setParameter(String key, String value) {
1800a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        setParameters(key+"="+value);
1801a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    }
1802a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent
1803a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    /**
1804a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * Sets a variable number of parameter values to audio hardware.
1805a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     *
1806a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * @param keyValuePairs list of parameters key value pairs in the form:
1807a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     *    key1=value1;key2=value2;...
1808a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     *
1809a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     */
1810a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public void setParameters(String keyValuePairs) {
1811a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        AudioSystem.setParameters(keyValuePairs);
1812a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    }
1813a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent
1814a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    /**
1815aac753dcf8862b416773eabaabb9d938a30f5896John Spurlock     * Gets a variable number of parameter values from audio hardware.
1816a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     *
1817a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * @param keys list of parameters
1818a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     * @return list of parameters key value pairs in the form:
1819a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     *    key1=value1;key2=value2;...
1820a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent     */
1821a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent    public String getParameters(String keys) {
1822a553c25b33c99b345cf1c8688f8df0ed8df14e5aEric Laurent        return AudioSystem.getParameters(keys);
18239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
18249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
18259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /* Sound effect identifiers */
18269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Keyboard and direction pad click sound
18289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #playSoundEffect(int)
18299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
18309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FX_KEY_CLICK = 0;
18319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Focus has moved up
18339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #playSoundEffect(int)
18349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
18359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FX_FOCUS_NAVIGATION_UP = 1;
18369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Focus has moved down
18389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #playSoundEffect(int)
18399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
18409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FX_FOCUS_NAVIGATION_DOWN = 2;
18419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Focus has moved left
18439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #playSoundEffect(int)
18449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
18459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FX_FOCUS_NAVIGATION_LEFT = 3;
18469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Focus has moved right
18489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #playSoundEffect(int)
18499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
18509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FX_FOCUS_NAVIGATION_RIGHT = 4;
18519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * IME standard keypress sound
18539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #playSoundEffect(int)
18549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
18559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FX_KEYPRESS_STANDARD = 5;
18569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * IME spacebar keypress sound
18589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #playSoundEffect(int)
18599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
18609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FX_KEYPRESS_SPACEBAR = 6;
18619066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18629066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * IME delete keypress sound
18639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #playSoundEffect(int)
18649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
18659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FX_KEYPRESS_DELETE = 7;
18669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * IME return_keypress sound
18689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @see #playSoundEffect(int)
18699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
18709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public static final int FX_KEYPRESS_RETURN = 8;
1871cacfe69b6845dd1c1c8682f67c3c52ef35214b2cJustin Koh
1872cacfe69b6845dd1c1c8682f67c3c52ef35214b2cJustin Koh    /**
1873cacfe69b6845dd1c1c8682f67c3c52ef35214b2cJustin Koh     * Invalid keypress sound
1874cacfe69b6845dd1c1c8682f67c3c52ef35214b2cJustin Koh     * @see #playSoundEffect(int)
1875cacfe69b6845dd1c1c8682f67c3c52ef35214b2cJustin Koh     */
1876cacfe69b6845dd1c1c8682f67c3c52ef35214b2cJustin Koh    public static final int FX_KEYPRESS_INVALID = 9;
18779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @hide Number of sound effects
18799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
1880cacfe69b6845dd1c1c8682f67c3c52ef35214b2cJustin Koh    public static final int NUM_SOUND_EFFECTS = 10;
18819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
18829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
18839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Plays a sound effect (Key clicks, lid open/close...)
18849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param effectType The type of sound effect. One of
18859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #FX_KEY_CLICK},
18869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #FX_FOCUS_NAVIGATION_UP},
18879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #FX_FOCUS_NAVIGATION_DOWN},
18889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #FX_FOCUS_NAVIGATION_LEFT},
18899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #FX_FOCUS_NAVIGATION_RIGHT},
1890105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     *            {@link #FX_KEYPRESS_STANDARD},
1891105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     *            {@link #FX_KEYPRESS_SPACEBAR},
1892105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     *            {@link #FX_KEYPRESS_DELETE},
1893105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     *            {@link #FX_KEYPRESS_RETURN},
1894cacfe69b6845dd1c1c8682f67c3c52ef35214b2cJustin Koh     *            {@link #FX_KEYPRESS_INVALID},
18959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * NOTE: This version uses the UI settings to determine
18969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * whether sounds are heard or not.
18979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
18989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void  playSoundEffect(int effectType) {
18999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) {
19009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
19019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
19029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19030c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk        if (!querySoundEffectsEnabled(Process.myUserHandle().getIdentifier())) {
19040c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk            return;
19050c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk        }
19060c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk
19070c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk        IAudioService service = getService();
19080c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk        try {
19090c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk            service.playSoundEffect(effectType);
19100c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk        } catch (RemoteException e) {
19110c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk            Log.e(TAG, "Dead object in playSoundEffect"+e);
19120c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk        }
19130c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk    }
19140c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk
19150c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk    /**
19160c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     * Plays a sound effect (Key clicks, lid open/close...)
19170c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     * @param effectType The type of sound effect. One of
19180c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     *            {@link #FX_KEY_CLICK},
19190c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     *            {@link #FX_FOCUS_NAVIGATION_UP},
19200c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     *            {@link #FX_FOCUS_NAVIGATION_DOWN},
19210c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     *            {@link #FX_FOCUS_NAVIGATION_LEFT},
19220c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     *            {@link #FX_FOCUS_NAVIGATION_RIGHT},
19230c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     *            {@link #FX_KEYPRESS_STANDARD},
19240c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     *            {@link #FX_KEYPRESS_SPACEBAR},
19250c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     *            {@link #FX_KEYPRESS_DELETE},
19260c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     *            {@link #FX_KEYPRESS_RETURN},
19270c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     *            {@link #FX_KEYPRESS_INVALID},
19280c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     * @param userId The current user to pull sound settings from
19290c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     * NOTE: This version uses the UI settings to determine
19300c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     * whether sounds are heard or not.
19310c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     * @hide
19320c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk     */
19330c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk    public void  playSoundEffect(int effectType, int userId) {
19340c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk        if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) {
19350c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk            return;
19360c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk        }
19370c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk
19380c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk        if (!querySoundEffectsEnabled(userId)) {
19399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
19409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
19419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
19439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
19449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            service.playSoundEffect(effectType);
19459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
19469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in playSoundEffect"+e);
19479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
19489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
19499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
19519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Plays a sound effect (Key clicks, lid open/close...)
19529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * @param effectType The type of sound effect. One of
19539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #FX_KEY_CLICK},
19549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #FX_FOCUS_NAVIGATION_UP},
19559066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #FX_FOCUS_NAVIGATION_DOWN},
19569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #FX_FOCUS_NAVIGATION_LEFT},
19579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *            {@link #FX_FOCUS_NAVIGATION_RIGHT},
1958105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     *            {@link #FX_KEYPRESS_STANDARD},
1959105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     *            {@link #FX_KEYPRESS_SPACEBAR},
1960105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     *            {@link #FX_KEYPRESS_DELETE},
1961105925376f8d0f6b318c9938c7b83ef7fef094daThe Android Open Source Project     *            {@link #FX_KEYPRESS_RETURN},
1962cacfe69b6845dd1c1c8682f67c3c52ef35214b2cJustin Koh     *            {@link #FX_KEYPRESS_INVALID},
1963a2ef57dba9ac77d8eccacd646b2b8a8d99fe9d8bEric Laurent     * @param volume Sound effect volume.
1964a2ef57dba9ac77d8eccacd646b2b8a8d99fe9d8bEric Laurent     * The volume value is a raw scalar so UI controls should be scaled logarithmically.
1965a2ef57dba9ac77d8eccacd646b2b8a8d99fe9d8bEric Laurent     * If a volume of -1 is specified, the AudioManager.STREAM_MUSIC stream volume minus 3dB will be used.
19669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * NOTE: This version is for applications that have their own
19679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * settings panel for enabling and controlling volume.
19689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
19699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void  playSoundEffect(int effectType, float volume) {
19709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) {
19719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            return;
19729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
19739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
19759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
19769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            service.playSoundEffectVolume(effectType, volume);
19779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
19789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in playSoundEffect"+e);
19799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
19809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
19819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
19839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     * Settings has an in memory cache, so this is fast.
19849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
19850c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk    private boolean querySoundEffectsEnabled(int user) {
198629f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        return Settings.System.getIntForUser(getContext().getContentResolver(),
19870c37ba318847a44cb9256e55eca1160dc1fffd4dJason Monk                Settings.System.SOUND_EFFECTS_ENABLED, 0, user) != 0;
19889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
19899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
19919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
19929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *  Load Sound effects.
19939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *  This method must be called when sound effects are enabled.
19949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
19959066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void loadSoundEffects() {
19969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
19979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
19989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            service.loadSoundEffects();
19999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
20009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in loadSoundEffects"+e);
20019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
20029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
20039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
20049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    /**
20059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *  Unload Sound effects.
20069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *  This method can be called to free some memory when
20079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     *  sound effects are disabled.
20089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     */
20099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    public void unloadSoundEffects() {
20109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        IAudioService service = getService();
20119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        try {
20129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            service.unloadSoundEffects();
20139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        } catch (RemoteException e) {
20149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            Log.e(TAG, "Dead object in unloadSoundEffects"+e);
20159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        }
20169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
20179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
20184050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent    /**
20192380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     * @hide
20202380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     * Used to indicate no audio focus has been gained or lost.
20212380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     */
20222380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi    public static final int AUDIOFOCUS_NONE = 0;
20232380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi
20242380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi    /**
2025d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration.
20267f7e67f1b60b7840011236097baad9df6dd74c20Jean-Michel Trivi     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
2027d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
2028d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
2029d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    public static final int AUDIOFOCUS_GAIN = 1;
2030d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
2031d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * Used to indicate a temporary gain or request of audio focus, anticipated to last a short
2032d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * amount of time. Examples of temporary changes are the playback of driving directions, or an
2033d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * event notification.
20347f7e67f1b60b7840011236097baad9df6dd74c20Jean-Michel Trivi     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
2035d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
2036d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
2037d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    public static final int AUDIOFOCUS_GAIN_TRANSIENT = 2;
2038078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi    /**
2039078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     * Used to indicate a temporary request of audio focus, anticipated to last a short
2040983ac2b8cff85dda1f55b0d7779a33248b3fa7cfJean-Michel Trivi     * amount of time, and where it is acceptable for other audio applications to keep playing
2041983ac2b8cff85dda1f55b0d7779a33248b3fa7cfJean-Michel Trivi     * after having lowered their output level (also referred to as "ducking").
2042078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     * Examples of temporary changes are the playback of driving directions where playback of music
2043078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     * in the background is acceptable.
20447f7e67f1b60b7840011236097baad9df6dd74c20Jean-Michel Trivi     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
2045078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
2046078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     */
2047078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi    public static final int AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = 3;
2048078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi    /**
20492380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     * Used to indicate a temporary request of audio focus, anticipated to last a short
20502380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     * amount of time, during which no other applications, or system components, should play
20512380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     * anything. Examples of exclusive and transient audio focus requests are voice
20522380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     * memo recording and speech recognition, during which the system shouldn't play any
20532380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     * notifications, and media playback should have paused.
20542380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int)
20552380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi     */
20562380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi    public static final int AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = 4;
20572380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi    /**
2058078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     * Used to indicate a loss of audio focus of unknown duration.
20597f7e67f1b60b7840011236097baad9df6dd74c20Jean-Michel Trivi     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
2060078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     */
2061078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi    public static final int AUDIOFOCUS_LOSS = -1 * AUDIOFOCUS_GAIN;
2062078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi    /**
2063078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     * Used to indicate a transient loss of audio focus.
20647f7e67f1b60b7840011236097baad9df6dd74c20Jean-Michel Trivi     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
2065078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     */
2066078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi    public static final int AUDIOFOCUS_LOSS_TRANSIENT = -1 * AUDIOFOCUS_GAIN_TRANSIENT;
2067078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi    /**
2068078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     * Used to indicate a transient loss of audio focus where the loser of the audio focus can
2069983ac2b8cff85dda1f55b0d7779a33248b3fa7cfJean-Michel Trivi     * lower its output volume if it wants to continue playing (also referred to as "ducking"), as
2070983ac2b8cff85dda1f55b0d7779a33248b3fa7cfJean-Michel Trivi     * the new focus owner doesn't require others to be silent.
20717f7e67f1b60b7840011236097baad9df6dd74c20Jean-Michel Trivi     * @see OnAudioFocusChangeListener#onAudioFocusChange(int)
2072078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     */
2073078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi    public static final int AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK =
2074078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi            -1 * AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK;
2075d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2076d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
2077d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * Interface definition for a callback to be invoked when the audio focus of the system is
2078d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * updated.
2079d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
2080d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    public interface OnAudioFocusChangeListener {
2081d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        /**
2082d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi         * Called on the listener to notify it the audio focus for this listener has been changed.
2083d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi         * The focusChange value indicates whether the focus was gained,
2084d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi         * whether the focus was lost, and whether that loss is transient, or whether the new focus
2085d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi         * holder will hold it for an unknown amount of time.
2086983ac2b8cff85dda1f55b0d7779a33248b3fa7cfJean-Michel Trivi         * When losing focus, listeners can use the focus change information to decide what
2087983ac2b8cff85dda1f55b0d7779a33248b3fa7cfJean-Michel Trivi         * behavior to adopt when losing focus. A music player could for instance elect to lower
2088983ac2b8cff85dda1f55b0d7779a33248b3fa7cfJean-Michel Trivi         * the volume of its music stream (duck) for transient focus losses, and pause otherwise.
2089983ac2b8cff85dda1f55b0d7779a33248b3fa7cfJean-Michel Trivi         * @param focusChange the type of focus change, one of {@link AudioManager#AUDIOFOCUS_GAIN},
2090078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi         *   {@link AudioManager#AUDIOFOCUS_LOSS}, {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT}
2091983ac2b8cff85dda1f55b0d7779a33248b3fa7cfJean-Michel Trivi         *   and {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK}.
2092d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi         */
20937f7e67f1b60b7840011236097baad9df6dd74c20Jean-Michel Trivi        public void onAudioFocusChange(int focusChange);
2094d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    }
2095d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2096d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
2097d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * Map to convert focus event listener IDs, as used in the AudioService audio focus stack,
2098d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * to actual listener objects.
2099d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
210030c918ce7fbe171944b28fc91b3f22b3d631872dGlenn Kasten    private final HashMap<String, OnAudioFocusChangeListener> mAudioFocusIdListenerMap =
2101d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi            new HashMap<String, OnAudioFocusChangeListener>();
2102d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
2103d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * Lock to prevent concurrent changes to the list of focus listeners for this AudioManager
2104d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * instance.
2105d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
2106d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    private final Object mFocusListenerLock = new Object();
2107d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2108d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    private OnAudioFocusChangeListener findFocusListener(String id) {
2109d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi        return mAudioFocusIdListenerMap.get(id);
2110d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    }
2111d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2112d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
2113d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * Handler for audio focus events coming from the audio service.
2114d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
211530c918ce7fbe171944b28fc91b3f22b3d631872dGlenn Kasten    private final FocusEventHandlerDelegate mAudioFocusEventHandlerDelegate =
2116d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi            new FocusEventHandlerDelegate();
2117078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi
2118d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
2119d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     * Helper class to handle the forwarding of audio focus events to the appropriate listener
2120d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
2121d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    private class FocusEventHandlerDelegate {
2122d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        private final Handler mHandler;
2123d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2124d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        FocusEventHandlerDelegate() {
2125d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi            Looper looper;
2126d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi            if ((looper = Looper.myLooper()) == null) {
2127d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                looper = Looper.getMainLooper();
2128d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi            }
2129d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2130d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi            if (looper != null) {
2131d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                // implement the event handler delegate to receive audio focus events
2132d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                mHandler = new Handler(looper) {
2133d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                    @Override
2134d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                    public void handleMessage(Message msg) {
2135d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                        OnAudioFocusChangeListener listener = null;
2136d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                        synchronized(mFocusListenerLock) {
2137d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                            listener = findFocusListener((String)msg.obj);
2138d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                        }
2139d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                        if (listener != null) {
2140fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi                            Log.d(TAG, "AudioManager dispatching onAudioFocusChange("
2141fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi                                    + msg.what + ") for " + msg.obj);
21427f7e67f1b60b7840011236097baad9df6dd74c20Jean-Michel Trivi                            listener.onAudioFocusChange(msg.what);
2143d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                        }
2144d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                    }
2145d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                };
2146d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi            } else {
2147d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                mHandler = null;
2148d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi            }
2149d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        }
2150d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2151d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        Handler getHandler() {
2152d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi            return mHandler;
2153d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        }
2154d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    }
2155d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
215630c918ce7fbe171944b28fc91b3f22b3d631872dGlenn Kasten    private final IAudioFocusDispatcher mAudioFocusDispatcher = new IAudioFocusDispatcher.Stub() {
2157d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2158d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        public void dispatchAudioFocusChange(int focusChange, String id) {
2159d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi            Message m = mAudioFocusEventHandlerDelegate.getHandler().obtainMessage(focusChange, id);
2160d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi            mAudioFocusEventHandlerDelegate.getHandler().sendMessage(m);
2161d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        }
2162d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2163d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    };
2164d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2165d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi    private String getIdForAudioFocusListener(OnAudioFocusChangeListener l) {
2166d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        if (l == null) {
2167308e9a5d0975489982f93fd1f3728e6ed7a778d0Jean-Michel Trivi            return new String(this.toString());
2168d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        } else {
2169d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi            return new String(this.toString() + l.toString());
2170d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        }
2171d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    }
2172d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2173d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
21745f53f0883a507b9d4b9b9891af560a64f746d149Xavier Ducrohet     * @hide
217546b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     * Registers a listener to be called when audio focus changes. Calling this method is optional
217646b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     * before calling {@link #requestAudioFocus(OnAudioFocusChangeListener, int, int)}, as it
217746b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     * will register the listener as well if it wasn't registered already.
217846b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     * @param l the listener to be notified of audio focus changes.
2179d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
2180d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    public void registerAudioFocusListener(OnAudioFocusChangeListener l) {
2181d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        synchronized(mFocusListenerLock) {
2182d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi            if (mAudioFocusIdListenerMap.containsKey(getIdForAudioFocusListener(l))) {
2183d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi                return;
2184d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi            }
2185d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi            mAudioFocusIdListenerMap.put(getIdForAudioFocusListener(l), l);
2186d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        }
2187d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    }
2188d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2189d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
21905f53f0883a507b9d4b9b9891af560a64f746d149Xavier Ducrohet     * @hide
219146b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     * Causes the specified listener to not be called anymore when focus is gained or lost.
219246b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     * @param l the listener to unregister.
2193d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
2194d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    public void unregisterAudioFocusListener(OnAudioFocusChangeListener l) {
2195392a2bbb52688ebd25768a7784d9edca7f498110Jean-Michel Trivi
2196d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        // remove locally
2197d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        synchronized(mFocusListenerLock) {
2198d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi            mAudioFocusIdListenerMap.remove(getIdForAudioFocusListener(l));
2199d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        }
2200d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    }
2201d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2202d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2203d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
220446b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     * A failed focus change request.
2205d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
2206d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    public static final int AUDIOFOCUS_REQUEST_FAILED = 0;
2207d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
220846b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     * A successful focus change request.
2209d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
2210d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    public static final int AUDIOFOCUS_REQUEST_GRANTED = 1;
2211fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     /**
2212fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi      * @hide
2213fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi      * A focus change request whose granting is delayed: the request was successful, but the
2214fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi      * requester will only be granted audio focus once the condition that prevented immediate
2215fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi      * granting has ended.
2216fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi      * See {@link #requestAudioFocus(OnAudioFocusChangeListener, AudioAttributes, int, int)}
2217fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi      */
2218fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi    public static final int AUDIOFOCUS_REQUEST_DELAYED = 2;
2219d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2220d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2221d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
2222d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     *  Request audio focus.
222346b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     *  Send a request to obtain the audio focus
2224d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     *  @param l the listener to be notified of audio focus changes
2225d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     *  @param streamType the main audio stream type affected by the focus request
2226d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     *  @param durationHint use {@link #AUDIOFOCUS_GAIN_TRANSIENT} to indicate this focus request
2227d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     *      is temporary, and focus will be abandonned shortly. Examples of transient requests are
2228078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     *      for the playback of driving directions, or notifications sounds.
2229983ac2b8cff85dda1f55b0d7779a33248b3fa7cfJean-Michel Trivi     *      Use {@link #AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK} to indicate also that it's ok for
2230983ac2b8cff85dda1f55b0d7779a33248b3fa7cfJean-Michel Trivi     *      the previous focus owner to keep playing if it ducks its audio output.
22319171db279826ee91d64eb9538a504ffed5e53b38Jean-Michel Trivi     *      Alternatively use {@link #AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE} for a temporary request
22329171db279826ee91d64eb9538a504ffed5e53b38Jean-Michel Trivi     *      that benefits from the system not playing disruptive sounds like notifications, for
22339171db279826ee91d64eb9538a504ffed5e53b38Jean-Michel Trivi     *      usecases such as voice memo recording, or speech recognition.
2234078fd47e91d495175927d1a4a8b9aad039a7ba4eJean-Michel Trivi     *      Use {@link #AUDIOFOCUS_GAIN} for a focus request of unknown duration such
2235d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     *      as the playback of a song or a video.
2236d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     *  @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED}
2237d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
2238d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    public int requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint) {
2239d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        int status = AUDIOFOCUS_REQUEST_FAILED;
2240fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi
2241fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi        try {
2242fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            // status is guaranteed to be either AUDIOFOCUS_REQUEST_FAILED or
2243fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            // AUDIOFOCUS_REQUEST_GRANTED as focus is requested without the
2244fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            // AUDIOFOCUS_FLAG_DELAY_OK flag
2245fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            status = requestAudioFocus(l,
2246fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi                    new AudioAttributes.Builder()
2247fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi                            .setInternalLegacyStreamType(streamType).build(),
2248fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi                    durationHint,
2249fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi                    0 /* flags, legacy behavior */);
2250fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi        } catch (IllegalArgumentException e) {
2251fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            Log.e(TAG, "Audio focus request denied due to ", e);
2252fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi        }
2253fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi
2254fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi        return status;
2255fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi    }
2256fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi
2257958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    // when adding new flags, add them to the relevant AUDIOFOCUS_FLAGS_APPS or SYSTEM masks
22580212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi    /**
22590212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * @hide
22600212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * Use this flag when requesting audio focus to indicate it is ok for the requester to not be
22610212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * granted audio focus immediately (as indicated by {@link #AUDIOFOCUS_REQUEST_DELAYED}) when
22620212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * the system is in a state where focus cannot change, but be granted focus later when
22630212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * this condition ends.
22640212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     */
2265958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    @SystemApi
2266fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi    public static final int AUDIOFOCUS_FLAG_DELAY_OK = 0x1 << 0;
22670212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi    /**
22680212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * @hide
22690212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * Use this flag when requesting audio focus to indicate that the requester
22700212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * will pause its media playback (if applicable) when losing audio focus with
22710212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * {@link #AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK}, rather than ducking.
22720212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * <br>On some platforms, the ducking may be handled without the application being aware of it
22730212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * (i.e. it will not transiently lose focus). For applications that for instance play spoken
22740212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * content, such as audio book or podcast players, ducking may never be acceptable, and will
22750212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * thus always pause. This flag enables them to be declared as such whenever they request focus.
22760212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     */
22770212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi    @SystemApi
22780212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi    public static final int AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS = 0x1 << 1;
22790212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi    /**
22800212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * @hide
22810212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * Use this flag to lock audio focus so granting is temporarily disabled.
22820212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * <br>This flag can only be used by owners of a registered
22830212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * {@link android.media.audiopolicy.AudioPolicy} in
22840212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * {@link #requestAudioFocus(OnAudioFocusChangeListener, AudioAttributes, int, int, AudioPolicy)}
22850212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     */
2286958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    @SystemApi
22870212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi    public static final int AUDIOFOCUS_FLAG_LOCK     = 0x1 << 2;
2288958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    /** @hide */
22890212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi    public static final int AUDIOFOCUS_FLAGS_APPS = AUDIOFOCUS_FLAG_DELAY_OK
22900212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi            | AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS;
2291958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    /** @hide */
2292958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    public static final int AUDIOFOCUS_FLAGS_SYSTEM = AUDIOFOCUS_FLAG_DELAY_OK
22930212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi            | AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS | AUDIOFOCUS_FLAG_LOCK;
2294fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi
2295fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi    /**
2296fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     * @hide
2297958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * Request audio focus.
2298958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * Send a request to obtain the audio focus. This method differs from
2299958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * {@link #requestAudioFocus(OnAudioFocusChangeListener, int, int)} in that it can express
2300958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * that the requester accepts delayed grants of audio focus.
2301fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     * @param l the listener to be notified of audio focus changes. It is not allowed to be null
2302fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *     when the request is flagged with {@link #AUDIOFOCUS_FLAG_DELAY_OK}.
2303fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     * @param requestAttributes non null {@link AudioAttributes} describing the main reason for
2304fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *     requesting audio focus.
2305fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     * @param durationHint use {@link #AUDIOFOCUS_GAIN_TRANSIENT} to indicate this focus request
2306fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *      is temporary, and focus will be abandonned shortly. Examples of transient requests are
2307fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *      for the playback of driving directions, or notifications sounds.
2308fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *      Use {@link #AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK} to indicate also that it's ok for
2309fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *      the previous focus owner to keep playing if it ducks its audio output.
2310fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *      Alternatively use {@link #AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE} for a temporary request
2311fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *      that benefits from the system not playing disruptive sounds like notifications, for
2312fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *      usecases such as voice memo recording, or speech recognition.
2313fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *      Use {@link #AUDIOFOCUS_GAIN} for a focus request of unknown duration such
2314fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *      as the playback of a song or a video.
23150212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     * @param flags 0 or a combination of {link #AUDIOFOCUS_FLAG_DELAY_OK}
23160212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     *     and {@link #AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS}.
2317958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     <br>Use 0 when not using any flags for the request, which behaves like
2318958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     {@link #requestAudioFocus(OnAudioFocusChangeListener, int, int)}, where either audio
2319958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     focus is granted immediately, or the grant request fails because the system is in a
2320958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     state where focus cannot change (e.g. a phone call).
2321fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     * @return {@link #AUDIOFOCUS_REQUEST_FAILED}, {@link #AUDIOFOCUS_REQUEST_GRANTED}
2322fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *     or {@link #AUDIOFOCUS_REQUEST_DELAYED}.
2323fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *     The return value is never {@link #AUDIOFOCUS_REQUEST_DELAYED} when focus is requested
2324fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     *     without the {@link #AUDIOFOCUS_FLAG_DELAY_OK} flag.
2325fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     * @throws IllegalArgumentException
2326fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi     */
2327958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    @SystemApi
2328fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi    public int requestAudioFocus(OnAudioFocusChangeListener l,
2329958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi            @NonNull AudioAttributes requestAttributes,
2330fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            int durationHint,
2331fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            int flags) throws IllegalArgumentException {
2332958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi        if (flags != (flags & AUDIOFOCUS_FLAGS_APPS)) {
2333958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi            throw new IllegalArgumentException("Invalid flags 0x"
2334958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi                    + Integer.toHexString(flags).toUpperCase());
2335958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi        }
2336958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi        return requestAudioFocus(l, requestAttributes, durationHint,
2337958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi                flags & AUDIOFOCUS_FLAGS_APPS,
2338958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi                null /* no AudioPolicy*/);
2339958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    }
2340958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi
2341958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    /**
2342958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * @hide
2343958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * Request or lock audio focus.
2344958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * This method is to be used by system components that have registered an
2345958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * {@link android.media.audiopolicy.AudioPolicy} to request audio focus, but also to "lock" it
2346958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * so focus granting is temporarily disabled.
2347958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * @param l see the description of the same parameter in
2348958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     {@link #requestAudioFocus(OnAudioFocusChangeListener, AudioAttributes, int, int)}
2349958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * @param requestAttributes non null {@link AudioAttributes} describing the main reason for
2350958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     requesting audio focus.
2351958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * @param durationHint see the description of the same parameter in
2352958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     {@link #requestAudioFocus(OnAudioFocusChangeListener, AudioAttributes, int, int)}
2353958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * @param flags 0 or a combination of {link #AUDIOFOCUS_FLAG_DELAY_OK},
23540212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi     *     {@link #AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS}, and {@link #AUDIOFOCUS_FLAG_LOCK}.
2355958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     <br>Use 0 when not using any flags for the request, which behaves like
2356958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     {@link #requestAudioFocus(OnAudioFocusChangeListener, int, int)}, where either audio
2357958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     focus is granted immediately, or the grant request fails because the system is in a
2358958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     state where focus cannot change (e.g. a phone call).
2359958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * @param ap a registered {@link android.media.audiopolicy.AudioPolicy} instance when locking
2360958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     focus, or null.
2361958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * @return see the description of the same return value in
2362958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *     {@link #requestAudioFocus(OnAudioFocusChangeListener, AudioAttributes, int, int)}
2363958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * @throws IllegalArgumentException
2364958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     */
23651b3541d5eedb332ea01066b4a78a2d06d5304044Jean-Michel Trivi    @SystemApi
2366958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    public int requestAudioFocus(OnAudioFocusChangeListener l,
2367958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi            @NonNull AudioAttributes requestAttributes,
2368958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi            int durationHint,
2369958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi            int flags,
2370958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi            AudioPolicy ap) throws IllegalArgumentException {
2371fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi        // parameter checking
2372fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi        if (requestAttributes == null) {
2373fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            throw new IllegalArgumentException("Illegal null AudioAttributes argument");
2374fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi        }
23752380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi        if ((durationHint < AUDIOFOCUS_GAIN) ||
23762380566debfc57eb1cc07db1306ccee23b84ddd4Jean-Michel Trivi                (durationHint > AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)) {
2377fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            throw new IllegalArgumentException("Invalid duration hint");
2378fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi        }
2379958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi        if (flags != (flags & AUDIOFOCUS_FLAGS_SYSTEM)) {
2380fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            throw new IllegalArgumentException("Illegal flags 0x"
2381fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi                + Integer.toHexString(flags).toUpperCase());
238255d1bb3483e17a11d122e68044e552d96ab55ff4Jean-Michel Trivi        }
2383fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi        if (((flags & AUDIOFOCUS_FLAG_DELAY_OK) == AUDIOFOCUS_FLAG_DELAY_OK) && (l == null)) {
2384fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            throw new IllegalArgumentException(
2385fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi                    "Illegal null focus listener when flagged as accepting delayed focus grant");
2386fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi        }
2387958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi        if (((flags & AUDIOFOCUS_FLAG_LOCK) == AUDIOFOCUS_FLAG_LOCK) && (ap == null)) {
2388958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi            throw new IllegalArgumentException(
2389958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi                    "Illegal null audio policy when locking audio focus");
2390958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi        }
2391fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi
2392fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi        int status = AUDIOFOCUS_REQUEST_FAILED;
2393d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        registerAudioFocusListener(l);
2394d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        IAudioService service = getService();
2395d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        try {
2396fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            status = service.requestAudioFocus(requestAttributes, durationHint, mICallBack,
23978f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi                    mAudioFocusDispatcher, getIdForAudioFocusListener(l),
239829f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen                    getContext().getOpPackageName() /* package name */, flags,
23990212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi                    ap != null ? ap.cb() : null);
2400d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        } catch (RemoteException e) {
2401958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi            Log.e(TAG, "Can't call requestAudioFocus() on AudioService:", e);
2402d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        }
2403d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        return status;
2404d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    }
2405d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
24064dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi    /**
24074dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     * @hide
24084dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     * Used internally by telephony package to request audio focus. Will cause the focus request
24094dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     * to be associated with the "voice communication" identifier only used in AudioService
24104dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     * to identify this use case.
24114dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     * @param streamType use STREAM_RING for focus requests when ringing, VOICE_CALL for
24124dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     *    the establishment of the call
24134dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     * @param durationHint the type of focus request. AUDIOFOCUS_GAIN_TRANSIENT is recommended so
24144dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     *    media applications resume after a call
24154dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     */
24164dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi    public void requestAudioFocusForCall(int streamType, int durationHint) {
24174dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi        IAudioService service = getService();
24184dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi        try {
2419fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi            service.requestAudioFocus(new AudioAttributes.Builder()
2420fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi                        .setInternalLegacyStreamType(streamType).build(),
2421fd6ad747e6c268753d0edf7a5a59b6815b190854Jean-Michel Trivi                    durationHint, mICallBack, null,
24226156017c2217d0fbbbb03434986250ec6bbd69d8John Spurlock                    AudioSystem.IN_VOICE_COMM_FOCUS_ID,
242329f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen                    getContext().getOpPackageName(),
2424958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi                    AUDIOFOCUS_FLAG_LOCK,
2425958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi                    null /* policy token */);
24264dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi        } catch (RemoteException e) {
2427958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi            Log.e(TAG, "Can't call requestAudioFocusForCall() on AudioService:", e);
24284dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi        }
24294dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi    }
24304dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi
24314dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi    /**
24324dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     * @hide
24334dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     * Used internally by telephony package to abandon audio focus, typically after a call or
24344dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     * when ringing ends and the call is rejected or not answered.
24354dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     * Should match one or more calls to {@link #requestAudioFocusForCall(int, int)}.
24364dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi     */
24374dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi    public void abandonAudioFocusForCall() {
24384dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi        IAudioService service = getService();
24394dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi        try {
24406156017c2217d0fbbbb03434986250ec6bbd69d8John Spurlock            service.abandonAudioFocus(null, AudioSystem.IN_VOICE_COMM_FOCUS_ID,
2441958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi                    null /*AudioAttributes, legacy behavior*/);
24424dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi        } catch (RemoteException e) {
2443958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi            Log.e(TAG, "Can't call abandonAudioFocusForCall() on AudioService:", e);
24444dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi        }
24454dd3674e517051f130fef36d2eb201c68ff61094Jean-Michel Trivi    }
2446d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2447d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    /**
244846b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     *  Abandon audio focus. Causes the previous focus owner, if any, to receive focus.
244946b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     *  @param l the listener with which focus was requested.
2450d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     *  @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED}
2451d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi     */
2452d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    public int abandonAudioFocus(OnAudioFocusChangeListener l) {
2453958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi        return abandonAudioFocus(l, null /*AudioAttributes, legacy behavior*/);
2454958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    }
2455958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi
2456958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    /**
2457958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * @hide
2458958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * Abandon audio focus. Causes the previous focus owner, if any, to receive focus.
2459958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     *  @param l the listener with which focus was requested.
2460958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * @param aa the {@link AudioAttributes} with which audio focus was requested
2461958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     * @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED}
2462958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi     */
2463958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    @SystemApi
2464958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi    public int abandonAudioFocus(OnAudioFocusChangeListener l, AudioAttributes aa) {
2465d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        int status = AUDIOFOCUS_REQUEST_FAILED;
2466392a2bbb52688ebd25768a7784d9edca7f498110Jean-Michel Trivi        unregisterAudioFocusListener(l);
2467d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        IAudioService service = getService();
2468d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        try {
2469d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi            status = service.abandonAudioFocus(mAudioFocusDispatcher,
2470958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi                    getIdForAudioFocusListener(l), aa);
2471d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        } catch (RemoteException e) {
2472958876fe55ea0fdeb73c72240a2f2bab32833443Jean-Michel Trivi            Log.e(TAG, "Can't call abandonAudioFocus() on AudioService:", e);
2473d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        }
2474d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi        return status;
2475d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi    }
2476d5176cfe6eae954e9cef1e2ec17859a5089e1330Jean-Michel Trivi
2477d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi    //====================================================================
2478d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi    // Remote Control
2479d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi    /**
248046b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     * Register a component to be the sole receiver of MEDIA_BUTTON intents.
2481d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi     * @param eventReceiver identifier of a {@link android.content.BroadcastReceiver}
2482d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi     *      that will receive the media button intent. This broadcast receiver must be declared
2483f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi     *      in the application manifest. The package of the component must match that of
2484f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi     *      the context you're registering from.
2485b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik     * @deprecated Use {@link MediaSession#setMediaButtonReceiver(PendingIntent)} instead.
2486d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi     */
2487b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik    @Deprecated
2488d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi    public void registerMediaButtonEventReceiver(ComponentName eventReceiver) {
24898f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi        if (eventReceiver == null) {
24908f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi            return;
24918f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi        }
249229f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        if (!eventReceiver.getPackageName().equals(getContext().getPackageName())) {
2493f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi            Log.e(TAG, "registerMediaButtonEventReceiver() error: " +
2494f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi                    "receiver and context package names don't match");
2495f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi            return;
2496f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        }
2497f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        // construct a PendingIntent for the media button and register it
2498f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
2499f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        //     the associated intent will be handled by the component being registered
2500f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        mediaButtonIntent.setComponent(eventReceiver);
250129f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        PendingIntent pi = PendingIntent.getBroadcast(getContext(),
2502f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi                0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/);
2503f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        registerMediaButtonIntent(pi, eventReceiver);
2504f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi    }
2505f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi
2506f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi    /**
2507961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn     * Register a component to be the sole receiver of MEDIA_BUTTON intents.  This is like
2508961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn     * {@link #registerMediaButtonEventReceiver(android.content.ComponentName)}, but allows
2509961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn     * the buttons to go to any PendingIntent.  Note that you should only use this form if
2510961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn     * you know you will continue running for the full time until unregistering the
2511961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn     * PendingIntent.
2512961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn     * @param eventReceiver target that will receive media button intents.  The PendingIntent
2513b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik     * will be sent an {@link Intent#ACTION_MEDIA_BUTTON} event when a media button action
2514b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik     * occurs, with {@link Intent#EXTRA_KEY_EVENT} added and holding the key code of the
2515b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik     * media button that was pressed.
2516b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik     * @deprecated Use {@link MediaSession#setMediaButtonReceiver(PendingIntent)} instead.
2517961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn     */
2518b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik    @Deprecated
2519961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn    public void registerMediaButtonEventReceiver(PendingIntent eventReceiver) {
2520961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn        if (eventReceiver == null) {
2521961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn            return;
2522961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn        }
2523961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn        registerMediaButtonIntent(eventReceiver, null);
2524961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn    }
2525961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn
2526961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn    /**
2527f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi     * @hide
2528f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi     * no-op if (pi == null) or (eventReceiver == null)
2529f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi     */
2530f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi    public void registerMediaButtonIntent(PendingIntent pi, ComponentName eventReceiver) {
2531961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn        if (pi == null) {
2532f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi            Log.e(TAG, "Cannot call registerMediaButtonIntent() with a null parameter");
2533f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi            return;
2534f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        }
253529f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext());
253629f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        helper.addMediaButtonListener(pi, eventReceiver, getContext());
2537722b808662eb20fa91151f1e3aa05fd911d1d226Jean-Michel Trivi    }
2538722b808662eb20fa91151f1e3aa05fd911d1d226Jean-Michel Trivi
2539722b808662eb20fa91151f1e3aa05fd911d1d226Jean-Michel Trivi    /**
254046b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     * Unregister the receiver of MEDIA_BUTTON intents.
254146b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     * @param eventReceiver identifier of a {@link android.content.BroadcastReceiver}
254246b3a1808a8e6fc60079af3c2292fd784a7bb0fcJean-Michel Trivi     *      that was registered with {@link #registerMediaButtonEventReceiver(ComponentName)}.
2543b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik     * @deprecated Use {@link MediaSession} instead.
2544d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi     */
2545b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik    @Deprecated
2546d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi    public void unregisterMediaButtonEventReceiver(ComponentName eventReceiver) {
25478f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi        if (eventReceiver == null) {
25488f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi            return;
25498f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi        }
2550f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        // construct a PendingIntent for the media button and unregister it
2551f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
2552f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        //     the associated intent will be handled by the component being registered
2553f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        mediaButtonIntent.setComponent(eventReceiver);
255429f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        PendingIntent pi = PendingIntent.getBroadcast(getContext(),
2555f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi                0/*requestCode, ignored*/, mediaButtonIntent, 0/*flags*/);
2556b839b83c7349533b4cb7278fd3d792b47199ba36Jean-Michel Trivi        unregisterMediaButtonIntent(pi);
2557f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi    }
2558f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi
2559f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi    /**
2560961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn     * Unregister the receiver of MEDIA_BUTTON intents.
2561961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn     * @param eventReceiver same PendingIntent that was registed with
2562961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn     *      {@link #registerMediaButtonEventReceiver(PendingIntent)}.
2563b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik     * @deprecated Use {@link MediaSession} instead.
2564961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn     */
2565b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik    @Deprecated
2566961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn    public void unregisterMediaButtonEventReceiver(PendingIntent eventReceiver) {
2567961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn        if (eventReceiver == null) {
2568961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn            return;
2569961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn        }
2570b839b83c7349533b4cb7278fd3d792b47199ba36Jean-Michel Trivi        unregisterMediaButtonIntent(eventReceiver);
2571961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn    }
2572961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn
2573961cae92540763226648813d111c5b5c3b0f1597Dianne Hackborn    /**
2574f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi     * @hide
2575f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi     */
2576b839b83c7349533b4cb7278fd3d792b47199ba36Jean-Michel Trivi    public void unregisterMediaButtonIntent(PendingIntent pi) {
257729f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext());
2578430fc48865e5a371b08f180390946b96d73848feRoboErik        helper.removeMediaButtonListener(pi);
2579d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi    }
2580d327f21626217aa3c9c0cdb7a84a742c531e59a3Jean-Michel Trivi
2581178889eff7fa3361a5cb08d6d43846a1baf5216bJean-Michel Trivi    /**
2582178889eff7fa3361a5cb08d6d43846a1baf5216bJean-Michel Trivi     * Registers the remote control client for providing information to display on the remote
2583178889eff7fa3361a5cb08d6d43846a1baf5216bJean-Michel Trivi     * controls.
2584466ade5ad66e7bfb1814d5e5ac76a17f8a0bcd3aJean-Michel Trivi     * @param rcClient The remote control client from which remote controls will receive
2585466ade5ad66e7bfb1814d5e5ac76a17f8a0bcd3aJean-Michel Trivi     *      information to display.
2586466ade5ad66e7bfb1814d5e5ac76a17f8a0bcd3aJean-Michel Trivi     * @see RemoteControlClient
2587b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik     * @deprecated Use {@link MediaSession} instead.
25888f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi     */
2589b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik    @Deprecated
25904426e42ac6107bf6b09f7c4cdad39eb161d8b9caJean-Michel Trivi    public void registerRemoteControlClient(RemoteControlClient rcClient) {
2591f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) {
25928f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi            return;
25938f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi        }
259429f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        rcClient.registerWithSession(MediaSessionLegacyHelper.getHelper(getContext()));
25958f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi    }
25968f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi
25978f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi    /**
2598fcd693a21d862ea765006f8987b8dd4b125b28c5Jean-Michel Trivi     * Unregisters the remote control client that was providing information to display on the
2599466ade5ad66e7bfb1814d5e5ac76a17f8a0bcd3aJean-Michel Trivi     * remote controls.
2600466ade5ad66e7bfb1814d5e5ac76a17f8a0bcd3aJean-Michel Trivi     * @param rcClient The remote control client to unregister.
26014426e42ac6107bf6b09f7c4cdad39eb161d8b9caJean-Michel Trivi     * @see #registerRemoteControlClient(RemoteControlClient)
2602b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik     * @deprecated Use {@link MediaSession} instead.
26038f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi     */
2604b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik    @Deprecated
26054426e42ac6107bf6b09f7c4cdad39eb161d8b9caJean-Michel Trivi    public void unregisterRemoteControlClient(RemoteControlClient rcClient) {
2606f0cff0456258478ba768097f73d4367ab67fd7a3Jean-Michel Trivi        if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) {
26078f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi            return;
26088f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi        }
260929f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen        rcClient.unregisterWithSession(MediaSessionLegacyHelper.getHelper(getContext()));
26108f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi    }
26118f619182cb759718f64ab95fd6d61c16138f6952Jean-Michel Trivi
261244413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi    /**
2613a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     * Registers a {@link RemoteController} instance for it to receive media
2614a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     * metadata updates and playback state information from applications using
2615a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     * {@link RemoteControlClient}, and control their playback.
2616a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     * <p>
2617ee5ad729b90deff435f9875337cbc434be4f8fe7John Spurlock     * Registration requires the {@link RemoteController.OnClientUpdateListener} listener to be
2618a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     * one of the enabled notification listeners (see
2619f108cdd9ee5efe354d87edd02a07b323298c116cJean-Michel Trivi     * {@link android.service.notification.NotificationListenerService}).
2620a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     *
2621a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     * @param rctlr the object to register.
2622a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     * @return true if the {@link RemoteController} was successfully registered,
2623a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     *         false if an error occurred, due to an internal system error, or
2624a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     *         insufficient permissions.
2625b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik     * @deprecated Use
2626a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     *             {@link MediaSessionManager#addOnActiveSessionsChangedListener(android.media.session.MediaSessionManager.OnActiveSessionsChangedListener, ComponentName)}
2627a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     *             and {@link MediaController} instead.
26287ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi     */
2629b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik    @Deprecated
26307ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi    public boolean registerRemoteController(RemoteController rctlr) {
26317ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi        if (rctlr == null) {
26327ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi            return false;
26337ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi        }
2634430fc48865e5a371b08f180390946b96d73848feRoboErik        rctlr.startListeningToSessions();
2635430fc48865e5a371b08f180390946b96d73848feRoboErik        return true;
26367ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi    }
26377ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi
26387ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi    /**
2639a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     * Unregisters a {@link RemoteController}, causing it to no longer receive
2640a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     * media metadata and playback state information, and no longer be capable
2641a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     * of controlling playback.
2642a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     *
2643a83487e8c618f3c267c3fe3a72d4eb9f1388d07eJean-Michel Trivi     * @param rctlr the object to unregister.
2644b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik     * @deprecated Use
2645a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     *             {@link MediaSessionManager#removeOnActiveSessionsChangedListener(android.media.session.MediaSessionManager.OnActiveSessionsChangedListener)}
2646a66c40bf6e0fb79ead6d8a9fc29c5671fa7b1206RoboErik     *             instead.
26477ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi     */
2648b214efbb9170a9f6a4991684a63ca59680074cc7RoboErik    @Deprecated
26497ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi    public void unregisterRemoteController(RemoteController rctlr) {
26507ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi        if (rctlr == null) {
26517ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi            return;
26527ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi        }
2653430fc48865e5a371b08f180390946b96d73848feRoboErik        rctlr.stopListeningToSessions();
26547ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi    }
26557ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi
26567ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi    /**
26577ddd226e7c6e759feaf2747a90be1cc06acf37a3Jean-Michel Trivi     * @hide
265844413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     * Registers a remote control display that will be sent information by remote control clients.
26594a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     * Use this method if your IRemoteControlDisplay is not going to display artwork, otherwise
26604a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     * use {@link #registerRemoteControlDisplay(IRemoteControlDisplay, int, int)} to pass the
26614a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     * artwork size directly, or
26624a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     * {@link #remoteControlDisplayUsesBitmapSize(IRemoteControlDisplay, int, int)} later if artwork
26634a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     * is not yet needed.
2664f108cdd9ee5efe354d87edd02a07b323298c116cJean-Michel Trivi     * <p>Registration requires the {@link Manifest.permission#MEDIA_CONTENT_CONTROL} permission.
26654a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     * @param rcd the IRemoteControlDisplay
266644413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     */
266744413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi    public void registerRemoteControlDisplay(IRemoteControlDisplay rcd) {
26684a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi        // passing a negative value for art work width and height as they are unknown at this stage
26694a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi        registerRemoteControlDisplay(rcd, /*w*/-1, /*h*/ -1);
26704a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi    }
26714a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi
26724a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi    /**
26734a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     * @hide
26744a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     * Registers a remote control display that will be sent information by remote control clients.
2675f108cdd9ee5efe354d87edd02a07b323298c116cJean-Michel Trivi     * <p>Registration requires the {@link Manifest.permission#MEDIA_CONTENT_CONTROL} permission.
26764a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     * @param rcd
26774a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     * @param w the maximum width of the expected bitmap. Negative values indicate it is
26784a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     *   useless to send artwork.
26794a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     * @param h the maximum height of the expected bitmap. Negative values indicate it is
26804a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     *   useless to send artwork.
26814a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi     */
26824a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi    public void registerRemoteControlDisplay(IRemoteControlDisplay rcd, int w, int h) {
268344413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        if (rcd == null) {
268444413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi            return;
268544413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        }
268644413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        IAudioService service = getService();
268744413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        try {
26884a5700556191c835116ec2a6997a4f16f464ac9dJean-Michel Trivi            service.registerRemoteControlDisplay(rcd, w, h);
268944413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        } catch (RemoteException e) {
269044413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi            Log.e(TAG, "Dead object in registerRemoteControlDisplay " + e);
269144413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        }
269244413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi    }
269344413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi
269444413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi    /**
269544413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     * @hide
269644413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     * Unregisters a remote control display that was sent information by remote control clients.
269744413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     * @param rcd
269844413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     */
269944413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi    public void unregisterRemoteControlDisplay(IRemoteControlDisplay rcd) {
270044413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        if (rcd == null) {
270144413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi            return;
270244413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        }
270344413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        IAudioService service = getService();
270444413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        try {
270544413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi            service.unregisterRemoteControlDisplay(rcd);
270644413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        } catch (RemoteException e) {
270744413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi            Log.e(TAG, "Dead object in unregisterRemoteControlDisplay " + e);
270844413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        }
270944413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi    }
271044413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi
271144413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi    /**
271244413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     * @hide
271344413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     * Sets the artwork size a remote control display expects when receiving bitmaps.
271444413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     * @param rcd
271544413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     * @param w the maximum width of the expected bitmap. Negative values indicate it is
271644413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     *   useless to send artwork.
271744413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     * @param h the maximum height of the expected bitmap. Negative values indicate it is
271844413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     *   useless to send artwork.
271944413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi     */
272044413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi    public void remoteControlDisplayUsesBitmapSize(IRemoteControlDisplay rcd, int w, int h) {
272144413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        if (rcd == null) {
272244413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi            return;
272344413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        }
272444413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        IAudioService service = getService();
272544413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        try {
272644413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi            service.remoteControlDisplayUsesBitmapSize(rcd, w, h);
272744413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        } catch (RemoteException e) {
272844413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi            Log.e(TAG, "Dead object in remoteControlDisplayUsesBitmapSize " + e);
272944413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi        }
273044413e5b514a91806a4bb4c7780029ea43be6f81Jean-Michel Trivi    }
2731ced110ec5970c632f523dfc0350b6071461ea9bcNeel Parekh
2732b5f04d6dbf5106140df367ae07f0e7708e1b650eJean-Michel Trivi    /**
27333261b537c5fdec824575a1f6ad6d8942715e82e2Jean-Michel Trivi     * @hide
2734c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi     * Controls whether a remote control display needs periodic checks of the RemoteControlClient
2735c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi     * playback position to verify that the estimated position has not drifted from the actual
2736c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi     * position. By default the check is not performed.
2737c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi     * The IRemoteControlDisplay must have been previously registered for this to have any effect.
2738c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi     * @param rcd the IRemoteControlDisplay for which the anti-drift mechanism will be enabled
2739c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi     *     or disabled. No effect is null.
2740c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi     * @param wantsSync if true, RemoteControlClient instances which expose their playback position
2741c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi     *     to the framework will regularly compare the estimated playback position with the actual
2742c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi     *     position, and will update the IRemoteControlDisplay implementation whenever a drift is
2743c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi     *     detected.
2744c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi     */
2745c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi    public void remoteControlDisplayWantsPlaybackPositionSync(IRemoteControlDisplay rcd,
2746c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi            boolean wantsSync) {
2747c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi        if (rcd == null) {
2748c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi            return;
2749c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi        }
2750c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi        IAudioService service = getService();
2751c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi        try {
2752c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi            service.remoteControlDisplayWantsPlaybackPositionSync(rcd, wantsSync);
2753c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi        } catch (RemoteException e) {
2754c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi            Log.e(TAG, "Dead object in remoteControlDisplayWantsPlaybackPositionSync " + e);
2755c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi        }
2756c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi    }
2757c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi
2758c3c4babf8424f65b3d3d2700f60fae6e94e9cd00Jean-Michel Trivi    /**
2759a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     * @hide
2760a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     * Register the given {@link AudioPolicy}.
2761a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     * This call is synchronous and blocks until the registration process successfully completed
2762a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     * or failed to complete.
27631b3541d5eedb332ea01066b4a78a2d06d5304044Jean-Michel Trivi     * @param policy the non-null {@link AudioPolicy} to register.
2764a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     * @return {@link #ERROR} if there was an error communicating with the registration service
2765a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     *    or if the user doesn't have the required
2766a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     *    {@link android.Manifest.permission#MODIFY_AUDIO_ROUTING} permission,
2767a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     *    {@link #SUCCESS} otherwise.
2768a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     */
27691b3541d5eedb332ea01066b4a78a2d06d5304044Jean-Michel Trivi    @SystemApi
27701b3541d5eedb332ea01066b4a78a2d06d5304044Jean-Michel Trivi    public int registerAudioPolicy(@NonNull AudioPolicy policy) {
2771a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        if (policy == null) {
2772a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi            throw new IllegalArgumentException("Illegal null AudioPolicy argument");
2773a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        }
2774a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        IAudioService service = getService();
2775a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        try {
27760212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi            String regId = service.registerAudioPolicy(policy.getConfig(), policy.cb(),
27770212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi                    policy.hasFocusListener());
27788fdb0d4defb6ee2ca8057d3442ead36b408b6c17Jean-Michel Trivi            if (regId == null) {
2779a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi                return ERROR;
27808fdb0d4defb6ee2ca8057d3442ead36b408b6c17Jean-Michel Trivi            } else {
27818fdb0d4defb6ee2ca8057d3442ead36b408b6c17Jean-Michel Trivi                policy.setRegistration(regId);
2782a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi            }
27838fdb0d4defb6ee2ca8057d3442ead36b408b6c17Jean-Michel Trivi            // successful registration
2784a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        } catch (RemoteException e) {
2785a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi            Log.e(TAG, "Dead object in registerAudioPolicyAsync()", e);
2786a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi            return ERROR;
2787a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        }
2788a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        return SUCCESS;
2789a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi    }
2790a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi
2791a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi    /**
2792a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     * @hide
27931b3541d5eedb332ea01066b4a78a2d06d5304044Jean-Michel Trivi     * @param policy the non-null {@link AudioPolicy} to unregister.
2794a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     */
27951b3541d5eedb332ea01066b4a78a2d06d5304044Jean-Michel Trivi    @SystemApi
27961b3541d5eedb332ea01066b4a78a2d06d5304044Jean-Michel Trivi    public void unregisterAudioPolicyAsync(@NonNull AudioPolicy policy) {
2797a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        if (policy == null) {
2798a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi            throw new IllegalArgumentException("Illegal null AudioPolicy argument");
2799a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        }
2800a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        IAudioService service = getService();
2801a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        try {
28020212be5150fb9fb3c340f3c7e51f6126372cc6f9Jean-Michel Trivi            service.unregisterAudioPolicyAsync(policy.cb());
28031b3541d5eedb332ea01066b4a78a2d06d5304044Jean-Michel Trivi            policy.setRegistration(null);
2804a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        } catch (RemoteException e) {
2805a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi            Log.e(TAG, "Dead object in unregisterAudioPolicyAsync()", e);
2806a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi        }
2807a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi    }
2808a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi
2809a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi
2810a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi    /**
28114050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent     *  @hide
28124050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent     *  Reload audio settings. This method is called by Settings backup
28134050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent     *  agent when audio settings are restored and causes the AudioService
28144050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent     *  to read and apply restored settings.
28154050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent     */
28164050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent    public void reloadAudioSettings() {
28174050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent        IAudioService service = getService();
28184050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent        try {
28194050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent            service.reloadAudioSettings();
28204050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent        } catch (RemoteException e) {
28214050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent            Log.e(TAG, "Dead object in reloadAudioSettings"+e);
28224050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent        }
28234050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent    }
28244050c93601b3c3609b21cc1e18a29b64747e7fa9Eric Laurent
28255a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    /**
28265a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * @hide
28275a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * Notifies AudioService that it is connected to an A2DP device that supports absolute volume,
28285a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * so that AudioService can send volume change events to the A2DP device, rather than handling
28295a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     * them.
28305a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du     */
28315a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    public void avrcpSupportsAbsoluteVolume(String address, boolean support) {
28325a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        IAudioService service = getService();
28335a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        try {
28345a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            service.avrcpSupportsAbsoluteVolume(address, support);
28355a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        } catch (RemoteException e) {
28365a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du            Log.e(TAG, "Dead object in avrcpSupportsAbsoluteVolume", e);
28375a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du        }
28385a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du    }
28395a0cf7a27f3953a1266af48543ccd9024f4cd89fJohn Du
28409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project     /**
28419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project      * {@hide}
28429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project      */
284330c918ce7fbe171944b28fc91b3f22b3d631872dGlenn Kasten     private final IBinder mICallBack = new Binder();
28447f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten
28457f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten    /**
28467f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten     * Checks whether the phone is in silent mode, with or without vibrate.
28477f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten     *
28487f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten     * @return true if phone is in silent mode, with or without vibrate.
28497f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten     *
28507f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten     * @see #getRingerMode()
28517f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten     *
28527f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten     * @hide pending API Council approval
28537f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten     */
28547f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten    public boolean isSilentMode() {
28557f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten        int ringerMode = getRingerMode();
28567f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten        boolean silentMode =
28577f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten            (ringerMode == RINGER_MODE_SILENT) ||
28587f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten            (ringerMode == RINGER_MODE_VIBRATE);
28597f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten        return silentMode;
28607f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten    }
28617f9b1fa6719dde575f8ea9858313358733fbd6c9Glenn Kasten
28628b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    // This section re-defines new output device constants from AudioSystem, because the AudioSystem
28638b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    // class is not used by other parts of the framework, which instead use definitions and methods
28648b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    // from AudioManager. AudioSystem is an internal class used by AudioManager and AudioService.
28658b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten
2866948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2867d7c29189aa639bfac1e6efcd222e65c2c8ecf3f1Wonsik Kim     * The audio device code for representing "no device." */
2868d7c29189aa639bfac1e6efcd222e65c2c8ecf3f1Wonsik Kim    public static final int DEVICE_NONE = AudioSystem.DEVICE_NONE;
2869d7c29189aa639bfac1e6efcd222e65c2c8ecf3f1Wonsik Kim    /** @hide
2870948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     *  The audio output device code for the small speaker at the front of the device used
28718b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *  when placing calls.  Does not refer to an in-ear headphone without attached microphone,
28728b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *  such as earbuds, earphones, or in-ear monitors (IEM). Those would be handled as a
28738b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *  {@link #DEVICE_OUT_WIRED_HEADPHONE}.
28748b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     */
28758b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_EARPIECE = AudioSystem.DEVICE_OUT_EARPIECE;
2876948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2877948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     *  The audio output device code for the built-in speaker */
28788b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_SPEAKER = AudioSystem.DEVICE_OUT_SPEAKER;
2879948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2880948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for a wired headset with attached microphone */
28818b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_WIRED_HEADSET = AudioSystem.DEVICE_OUT_WIRED_HEADSET;
2882948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2883948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for a wired headphone without attached microphone */
28848b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_WIRED_HEADPHONE = AudioSystem.DEVICE_OUT_WIRED_HEADPHONE;
2885948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2886948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for generic Bluetooth SCO, for voice */
28878b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_BLUETOOTH_SCO = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO;
2888948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2889948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for Bluetooth SCO Headset Profile (HSP) and
2890948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * Hands-Free Profile (HFP), for voice
28918b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     */
28928b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET =
28938b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten            AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2894948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2895948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for Bluetooth SCO car audio, for voice */
28968b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT =
28978b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten            AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2898948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2899948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for generic Bluetooth A2DP, for music */
29008b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_BLUETOOTH_A2DP = AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP;
2901948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2902948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for Bluetooth A2DP headphones, for music */
29038b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES =
29048b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten            AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2905948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2906948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for Bluetooth A2DP external speaker, for music */
29078b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER =
29088b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten            AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2909948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2910948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for S/PDIF (legacy) or HDMI
2911948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * Deprecated: replaced by {@link #DEVICE_OUT_HDMI} */
29128b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_AUX_DIGITAL = AudioSystem.DEVICE_OUT_AUX_DIGITAL;
2913948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2914948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for HDMI */
2915948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_OUT_HDMI = AudioSystem.DEVICE_OUT_HDMI;
2916948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2917948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for an analog wired headset attached via a
29188b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *  docking station
29198b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     */
29208b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_ANLG_DOCK_HEADSET = AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET;
2921948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2922948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for a digital wired headset attached via a
29238b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *  docking station
29248b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     */
29258b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_DGTL_DOCK_HEADSET = AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET;
2926948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2927948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for a USB audio accessory. The accessory is in USB host
292859f482764e346a5c5ac118ee1f7b24da645c2559Eric Laurent     * mode and the Android device in USB device mode
292959f482764e346a5c5ac118ee1f7b24da645c2559Eric Laurent     */
293059f482764e346a5c5ac118ee1f7b24da645c2559Eric Laurent    public static final int DEVICE_OUT_USB_ACCESSORY = AudioSystem.DEVICE_OUT_USB_ACCESSORY;
2931948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2932948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for a USB audio device. The device is in USB device
293359f482764e346a5c5ac118ee1f7b24da645c2559Eric Laurent     * mode and the Android device in USB host mode
293459f482764e346a5c5ac118ee1f7b24da645c2559Eric Laurent     */
293559f482764e346a5c5ac118ee1f7b24da645c2559Eric Laurent    public static final int DEVICE_OUT_USB_DEVICE = AudioSystem.DEVICE_OUT_USB_DEVICE;
2936948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2937948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for projection output.
2938948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2939948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_OUT_REMOTE_SUBMIX = AudioSystem.DEVICE_OUT_REMOTE_SUBMIX;
2940948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2941948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code the telephony voice TX path.
2942948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2943948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_OUT_TELEPHONY_TX = AudioSystem.DEVICE_OUT_TELEPHONY_TX;
2944948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2945948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for an analog jack with line impedance detected.
2946948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2947948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_OUT_LINE = AudioSystem.DEVICE_OUT_LINE;
2948948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2949948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for HDMI Audio Return Channel.
2950948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2951948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_OUT_HDMI_ARC = AudioSystem.DEVICE_OUT_HDMI_ARC;
2952948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2953948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for S/PDIF digital connection.
2954948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2955948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_OUT_SPDIF = AudioSystem.DEVICE_OUT_SPDIF;
2956948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2957948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio output device code for built-in FM transmitter.
2958948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2959948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_OUT_FM = AudioSystem.DEVICE_OUT_FM;
2960948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2961948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * This is not used as a returned value from {@link #getDevicesForStream}, but could be
29628b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *  used in the future in a set method to select whatever default device is chosen by the
29638b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *  platform-specific implementation.
29648b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     */
29658b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public static final int DEVICE_OUT_DEFAULT = AudioSystem.DEVICE_OUT_DEFAULT;
29668b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten
2967948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2968948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for default built-in microphone
2969948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2970948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_BUILTIN_MIC = AudioSystem.DEVICE_IN_BUILTIN_MIC;
2971948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2972948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for a Bluetooth SCO headset
2973948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2974948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET =
2975948d32748caaac5be06c991ebf00f74265a7849fEric Laurent                                    AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2976948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2977948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for wired headset microphone
2978948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2979948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_WIRED_HEADSET =
2980948d32748caaac5be06c991ebf00f74265a7849fEric Laurent                                    AudioSystem.DEVICE_IN_WIRED_HEADSET;
2981948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2982948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for HDMI
2983948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2984948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_HDMI =
2985948d32748caaac5be06c991ebf00f74265a7849fEric Laurent                                    AudioSystem.DEVICE_IN_HDMI;
2986948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2987948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for telephony voice RX path
2988948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2989948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_TELEPHONY_RX =
2990948d32748caaac5be06c991ebf00f74265a7849fEric Laurent                                    AudioSystem.DEVICE_IN_TELEPHONY_RX;
2991948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2992948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for built-in microphone pointing to the back
2993948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2994948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_BACK_MIC =
2995948d32748caaac5be06c991ebf00f74265a7849fEric Laurent                                    AudioSystem.DEVICE_IN_BACK_MIC;
2996948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
2997948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for analog from a docking station
2998948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
2999948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_ANLG_DOCK_HEADSET =
3000948d32748caaac5be06c991ebf00f74265a7849fEric Laurent                                    AudioSystem.DEVICE_IN_ANLG_DOCK_HEADSET;
3001948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
3002948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for digital from a docking station
3003948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
3004948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_DGTL_DOCK_HEADSET =
3005948d32748caaac5be06c991ebf00f74265a7849fEric Laurent                                    AudioSystem.DEVICE_IN_DGTL_DOCK_HEADSET;
3006948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
3007948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for a USB audio accessory. The accessory is in USB host
3008948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * mode and the Android device in USB device mode
3009948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
3010948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_USB_ACCESSORY =
3011948d32748caaac5be06c991ebf00f74265a7849fEric Laurent                                    AudioSystem.DEVICE_IN_USB_ACCESSORY;
3012948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
3013948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for a USB audio device. The device is in USB device
3014948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * mode and the Android device in USB host mode
3015948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
3016948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_USB_DEVICE =
3017948d32748caaac5be06c991ebf00f74265a7849fEric Laurent                                    AudioSystem.DEVICE_IN_USB_DEVICE;
3018948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
3019948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for a FM radio tuner
3020948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
3021948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_FM_TUNER = AudioSystem.DEVICE_IN_FM_TUNER;
3022948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
3023948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for a TV tuner
3024948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
3025948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_TV_TUNER = AudioSystem.DEVICE_IN_TV_TUNER;
3026948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
3027948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for an analog jack with line impedance detected
3028948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
3029948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_LINE = AudioSystem.DEVICE_IN_LINE;
3030948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /** @hide
3031948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * The audio input device code for a S/PDIF digital connection
3032948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
3033948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static final int DEVICE_IN_SPDIF = AudioSystem.DEVICE_IN_SPDIF;
3034112c19ee8b459439c3462767199832e5394b37a8Terry Heo    /** @hide
3035112c19ee8b459439c3462767199832e5394b37a8Terry Heo     * The audio input device code for audio loopback
3036112c19ee8b459439c3462767199832e5394b37a8Terry Heo     */
3037112c19ee8b459439c3462767199832e5394b37a8Terry Heo    public static final int DEVICE_IN_LOOPBACK = AudioSystem.DEVICE_IN_LOOPBACK;
3038948d32748caaac5be06c991ebf00f74265a7849fEric Laurent
3039948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /**
3040948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * Return true if the device code corresponds to an output device.
3041948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * @hide
3042948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
3043948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static boolean isOutputDevice(int device)
3044948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    {
3045948d32748caaac5be06c991ebf00f74265a7849fEric Laurent        return (device & AudioSystem.DEVICE_BIT_IN) == 0;
3046948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    }
3047948d32748caaac5be06c991ebf00f74265a7849fEric Laurent
3048948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    /**
3049948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * Return true if the device code corresponds to an input device.
3050948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     * @hide
3051948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     */
3052948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    public static boolean isInputDevice(int device)
3053948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    {
3054948d32748caaac5be06c991ebf00f74265a7849fEric Laurent        return (device & AudioSystem.DEVICE_BIT_IN) == AudioSystem.DEVICE_BIT_IN;
3055948d32748caaac5be06c991ebf00f74265a7849fEric Laurent    }
3056948d32748caaac5be06c991ebf00f74265a7849fEric Laurent
3057948d32748caaac5be06c991ebf00f74265a7849fEric Laurent
30588b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    /**
30598b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     * Return the enabled devices for the specified output stream type.
30608b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *
30618b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     * @param streamType The stream type to query. One of
30628b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #STREAM_VOICE_CALL},
30638b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #STREAM_SYSTEM},
30648b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #STREAM_RING},
30658b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #STREAM_MUSIC},
30668b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #STREAM_ALARM},
30678b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #STREAM_NOTIFICATION},
30688b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #STREAM_DTMF}.
30698b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *
30708b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     * @return The bit-mask "or" of audio output device codes for all enabled devices on this
30718b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *         stream. Zero or more of
30728b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_EARPIECE},
30738b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_SPEAKER},
30748b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_WIRED_HEADSET},
30758b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_WIRED_HEADPHONE},
30768b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_BLUETOOTH_SCO},
30778b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_BLUETOOTH_SCO_HEADSET},
30788b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_BLUETOOTH_SCO_CARKIT},
30798b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_BLUETOOTH_A2DP},
30808b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES},
30818b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER},
3082948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     *            {@link #DEVICE_OUT_HDMI},
30838b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_ANLG_DOCK_HEADSET},
30848b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_DGTL_DOCK_HEADSET}.
3085948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     *            {@link #DEVICE_OUT_USB_ACCESSORY}.
3086948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     *            {@link #DEVICE_OUT_USB_DEVICE}.
3087948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     *            {@link #DEVICE_OUT_REMOTE_SUBMIX}.
3088948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     *            {@link #DEVICE_OUT_TELEPHONY_TX}.
3089948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     *            {@link #DEVICE_OUT_LINE}.
3090948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     *            {@link #DEVICE_OUT_HDMI_ARC}.
3091948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     *            {@link #DEVICE_OUT_SPDIF}.
3092948d32748caaac5be06c991ebf00f74265a7849fEric Laurent     *            {@link #DEVICE_OUT_FM}.
30938b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *            {@link #DEVICE_OUT_DEFAULT} is not used here.
30948b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *
30958b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     * The implementation may support additional device codes beyond those listed, so
30968b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     * the application should ignore any bits which it does not recognize.
30978b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     * Note that the information may be imprecise when the implementation
30988b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     * cannot distinguish whether a particular device is enabled.
30998b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     *
31008b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     * {@hide}
31018b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten     */
31028b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    public int getDevicesForStream(int streamType) {
31038b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten        switch (streamType) {
31048b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten        case STREAM_VOICE_CALL:
31058b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten        case STREAM_SYSTEM:
31068b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten        case STREAM_RING:
31078b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten        case STREAM_MUSIC:
31088b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten        case STREAM_ALARM:
31098b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten        case STREAM_NOTIFICATION:
31108b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten        case STREAM_DTMF:
31118b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten            return AudioSystem.getDevicesForStream(streamType);
31128b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten        default:
31138b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten            return 0;
31148b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten        }
31158b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten    }
31168b4b97a14ad9b5b982d8fe92755efabec8ad0076Glenn Kasten
3117b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     /**
3118b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     * Indicate wired accessory connection state change.
3119b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     * @param device type of device connected/disconnected (AudioManager.DEVICE_OUT_xxx)
3120b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     * @param state  new connection state: 1 connected, 0 disconnected
3121b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     * @param name   device name
3122b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     * {@hide}
3123b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     */
312410804eb2818ab59b763a37b4f6151693c2ebba7bPaul McLean    public void setWiredDeviceConnectionState(int type, int state, String address, String name) {
3125b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent        IAudioService service = getService();
3126b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent        try {
31279087433753a5068819dcd3e86bdc2cce82bb8040John Spurlock            service.setWiredDeviceConnectionState(type, state, address, name,
3128a80ac05f85962949d5072831200d86a7023575fbMarco Nelissen                    mApplicationContext.getOpPackageName());
3129b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent        } catch (RemoteException e) {
3130b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent            Log.e(TAG, "Dead object in setWiredDeviceConnectionState "+e);
3131b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent        }
3132b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent    }
3133b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent
3134b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     /**
31350a40ec2192e4836b2fcb6ba51a7688aa6bd4ee98Mike Lockwood     * Indicate A2DP source or sink connection state change.
3136b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     * @param device Bluetooth device connected/disconnected
3137b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     * @param state  new connection state (BluetoothProfile.STATE_xxx)
31380a40ec2192e4836b2fcb6ba51a7688aa6bd4ee98Mike Lockwood     * @param profile profile for the A2DP device
31390a40ec2192e4836b2fcb6ba51a7688aa6bd4ee98Mike Lockwood     * (either {@link android.bluetooth.BluetoothProfile.A2DP} or
31400a40ec2192e4836b2fcb6ba51a7688aa6bd4ee98Mike Lockwood     * {@link android.bluetooth.BluetoothProfile.A2DP_SINK})
3141b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     * @return a delay in ms that the caller should wait before broadcasting
3142b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     * BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED intent.
3143b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     * {@hide}
3144b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent     */
31450a40ec2192e4836b2fcb6ba51a7688aa6bd4ee98Mike Lockwood    public int setBluetoothA2dpDeviceConnectionState(BluetoothDevice device, int state,
31460a40ec2192e4836b2fcb6ba51a7688aa6bd4ee98Mike Lockwood            int profile) {
3147b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent        IAudioService service = getService();
3148b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent        int delay = 0;
3149b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent        try {
31500a40ec2192e4836b2fcb6ba51a7688aa6bd4ee98Mike Lockwood            delay = service.setBluetoothA2dpDeviceConnectionState(device, state, profile);
3151b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent        } catch (RemoteException e) {
3152b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent            Log.e(TAG, "Dead object in setBluetoothA2dpDeviceConnectionState "+e);
3153b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent        }
3154b304ba5bc8fbc6034cf232cbbeeb3a34a5ce5b9fEric Laurent        return delay;
3155b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent    }
3156b1fbaaccb656ef09a8770c28df15e3e91a452e64Eric Laurent
3157098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey    /** {@hide} */
3158098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey    public IRingtonePlayer getRingtonePlayer() {
3159098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey        try {
3160098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey            return getService().getRingtonePlayer();
3161098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey        } catch (RemoteException e) {
3162098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey            return null;
3163098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey        }
3164098d580cc2bb6c0891c756a4e5230c6c6b0d2376Jeff Sharkey    }
3165228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten
3166228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten    /**
316762796aac596f556a1871358bb8ce93af9d55c5a5Glenn Kasten     * Used as a key for {@link #getProperty} to request the native or optimal output sample rate
316862796aac596f556a1871358bb8ce93af9d55c5a5Glenn Kasten     * for this device's primary output stream, in decimal Hz.
3169228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten     */
3170228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten    public static final String PROPERTY_OUTPUT_SAMPLE_RATE =
3171228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten            "android.media.property.OUTPUT_SAMPLE_RATE";
3172228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten
3173228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten    /**
317462796aac596f556a1871358bb8ce93af9d55c5a5Glenn Kasten     * Used as a key for {@link #getProperty} to request the native or optimal output buffer size
317562796aac596f556a1871358bb8ce93af9d55c5a5Glenn Kasten     * for this device's primary output stream, in decimal PCM frames.
3176228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten     */
3177228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten    public static final String PROPERTY_OUTPUT_FRAMES_PER_BUFFER =
3178228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten            "android.media.property.OUTPUT_FRAMES_PER_BUFFER";
3179228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten
3180228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten    /**
3181d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra     * Used as a key for {@link #getProperty} to determine if the default microphone audio source
3182d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra     * supports near-ultrasound frequencies (range of 18 - 21 kHz).
3183d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra     */
3184d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra    public static final String PROPERTY_SUPPORT_MIC_NEAR_ULTRASOUND =
3185d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra            "android.media.property.SUPPORT_MIC_NEAR_ULTRASOUND";
3186d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra
3187d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra    /**
3188d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra     * Used as a key for {@link #getProperty} to determine if the default speaker audio path
3189d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra     * supports near-ultrasound frequencies (range of 18 - 21 kHz).
3190d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra     */
3191d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra    public static final String PROPERTY_SUPPORT_SPEAKER_NEAR_ULTRASOUND =
3192d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra            "android.media.property.SUPPORT_SPEAKER_NEAR_ULTRASOUND";
3193d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra
3194d08715e50342af02751a93735cca9d1bf76798dfArunesh Mishra    /**
319562796aac596f556a1871358bb8ce93af9d55c5a5Glenn Kasten     * Returns the value of the property with the specified key.
3196228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten     * @param key One of the strings corresponding to a property key: either
319762796aac596f556a1871358bb8ce93af9d55c5a5Glenn Kasten     *            {@link #PROPERTY_OUTPUT_SAMPLE_RATE} or
319862796aac596f556a1871358bb8ce93af9d55c5a5Glenn Kasten     *            {@link #PROPERTY_OUTPUT_FRAMES_PER_BUFFER}
3199228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten     * @return A string representing the associated value for that property key,
3200228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten     *         or null if there is no value for that key.
3201228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten     */
3202228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten    public String getProperty(String key) {
3203c6c4365ddb0213c548b055f257dc8077389224fbGlenn Kasten        if (PROPERTY_OUTPUT_SAMPLE_RATE.equals(key)) {
3204c6c4365ddb0213c548b055f257dc8077389224fbGlenn Kasten            int outputSampleRate = AudioSystem.getPrimaryOutputSamplingRate();
3205c6c4365ddb0213c548b055f257dc8077389224fbGlenn Kasten            return outputSampleRate > 0 ? Integer.toString(outputSampleRate) : null;
3206c6c4365ddb0213c548b055f257dc8077389224fbGlenn Kasten        } else if (PROPERTY_OUTPUT_FRAMES_PER_BUFFER.equals(key)) {
3207c6c4365ddb0213c548b055f257dc8077389224fbGlenn Kasten            int outputFramesPerBuffer = AudioSystem.getPrimaryOutputFrameCount();
3208c6c4365ddb0213c548b055f257dc8077389224fbGlenn Kasten            return outputFramesPerBuffer > 0 ? Integer.toString(outputFramesPerBuffer) : null;
3209bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra        } else if (PROPERTY_SUPPORT_MIC_NEAR_ULTRASOUND.equals(key)) {
3210bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra            return SystemProperties.get(SYSTEM_PROPERTY_MIC_NEAR_ULTRASOUND,
3211bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra                    DEFAULT_RESULT_FALSE_STRING);
3212bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra        } else if (PROPERTY_SUPPORT_SPEAKER_NEAR_ULTRASOUND.equals(key)) {
3213bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra            return SystemProperties.get(SYSTEM_PROPERTY_SPEAKER_NEAR_ULTRASOUND,
3214bc92227d6cd8380ae49815ac08220de4997d5e50Arunesh Mishra                    DEFAULT_RESULT_FALSE_STRING);
3215c6c4365ddb0213c548b055f257dc8077389224fbGlenn Kasten        } else {
3216c6c4365ddb0213c548b055f257dc8077389224fbGlenn Kasten            // null or unknown key
3217c6c4365ddb0213c548b055f257dc8077389224fbGlenn Kasten            return null;
3218c6c4365ddb0213c548b055f257dc8077389224fbGlenn Kasten        }
3219228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten    }
3220228c9847a31b79f8baa80302731cdbf6a2b3c0d4Glenn Kasten
322161dcdf3764b614c6f01e77cb4342d192e3101f8aOliver Woodman    /**
322261dcdf3764b614c6f01e77cb4342d192e3101f8aOliver Woodman     * Returns the estimated latency for the given stream type in milliseconds.
322361dcdf3764b614c6f01e77cb4342d192e3101f8aOliver Woodman     *
322461dcdf3764b614c6f01e77cb4342d192e3101f8aOliver Woodman     * DO NOT UNHIDE. The existing approach for doing A/V sync has too many problems. We need
322561dcdf3764b614c6f01e77cb4342d192e3101f8aOliver Woodman     * a better solution.
322661dcdf3764b614c6f01e77cb4342d192e3101f8aOliver Woodman     * @hide
322761dcdf3764b614c6f01e77cb4342d192e3101f8aOliver Woodman     */
322861dcdf3764b614c6f01e77cb4342d192e3101f8aOliver Woodman    public int getOutputLatency(int streamType) {
322961dcdf3764b614c6f01e77cb4342d192e3101f8aOliver Woodman        return AudioSystem.getOutputLatency(streamType);
323061dcdf3764b614c6f01e77cb4342d192e3101f8aOliver Woodman    }
323161dcdf3764b614c6f01e77cb4342d192e3101f8aOliver Woodman
32323346a802087f621c6441bc512dfcc17b07143fc6John Spurlock    /**
32333346a802087f621c6441bc512dfcc17b07143fc6John Spurlock     * Registers a global volume controller interface.  Currently limited to SystemUI.
32343346a802087f621c6441bc512dfcc17b07143fc6John Spurlock     *
32353346a802087f621c6441bc512dfcc17b07143fc6John Spurlock     * @hide
32363346a802087f621c6441bc512dfcc17b07143fc6John Spurlock     */
32373346a802087f621c6441bc512dfcc17b07143fc6John Spurlock    public void setVolumeController(IVolumeController controller) {
32383346a802087f621c6441bc512dfcc17b07143fc6John Spurlock        try {
32393346a802087f621c6441bc512dfcc17b07143fc6John Spurlock            getService().setVolumeController(controller);
32403346a802087f621c6441bc512dfcc17b07143fc6John Spurlock        } catch (RemoteException e) {
32413346a802087f621c6441bc512dfcc17b07143fc6John Spurlock            Log.w(TAG, "Error setting volume controller", e);
32423346a802087f621c6441bc512dfcc17b07143fc6John Spurlock        }
32433346a802087f621c6441bc512dfcc17b07143fc6John Spurlock    }
32443346a802087f621c6441bc512dfcc17b07143fc6John Spurlock
32453346a802087f621c6441bc512dfcc17b07143fc6John Spurlock    /**
324633f4e04e32fac42f158733d6a731e50490fa9951John Spurlock     * Notify audio manager about volume controller visibility changes.
324733f4e04e32fac42f158733d6a731e50490fa9951John Spurlock     * Currently limited to SystemUI.
324833f4e04e32fac42f158733d6a731e50490fa9951John Spurlock     *
324933f4e04e32fac42f158733d6a731e50490fa9951John Spurlock     * @hide
325033f4e04e32fac42f158733d6a731e50490fa9951John Spurlock     */
325133f4e04e32fac42f158733d6a731e50490fa9951John Spurlock    public void notifyVolumeControllerVisible(IVolumeController controller, boolean visible) {
325233f4e04e32fac42f158733d6a731e50490fa9951John Spurlock        try {
325333f4e04e32fac42f158733d6a731e50490fa9951John Spurlock            getService().notifyVolumeControllerVisible(controller, visible);
325433f4e04e32fac42f158733d6a731e50490fa9951John Spurlock        } catch (RemoteException e) {
325533f4e04e32fac42f158733d6a731e50490fa9951John Spurlock            Log.w(TAG, "Error notifying about volume controller visibility", e);
325633f4e04e32fac42f158733d6a731e50490fa9951John Spurlock        }
325733f4e04e32fac42f158733d6a731e50490fa9951John Spurlock    }
325833f4e04e32fac42f158733d6a731e50490fa9951John Spurlock
325933f4e04e32fac42f158733d6a731e50490fa9951John Spurlock    /**
32603346a802087f621c6441bc512dfcc17b07143fc6John Spurlock     * Only useful for volume controllers.
32613346a802087f621c6441bc512dfcc17b07143fc6John Spurlock     * @hide
32623346a802087f621c6441bc512dfcc17b07143fc6John Spurlock     */
32633346a802087f621c6441bc512dfcc17b07143fc6John Spurlock    public boolean isStreamAffectedByRingerMode(int streamType) {
32643346a802087f621c6441bc512dfcc17b07143fc6John Spurlock        try {
32653346a802087f621c6441bc512dfcc17b07143fc6John Spurlock            return getService().isStreamAffectedByRingerMode(streamType);
32663346a802087f621c6441bc512dfcc17b07143fc6John Spurlock        } catch (RemoteException e) {
32673346a802087f621c6441bc512dfcc17b07143fc6John Spurlock            Log.w(TAG, "Error calling isStreamAffectedByRingerMode", e);
32683346a802087f621c6441bc512dfcc17b07143fc6John Spurlock            return false;
32693346a802087f621c6441bc512dfcc17b07143fc6John Spurlock        }
32703346a802087f621c6441bc512dfcc17b07143fc6John Spurlock    }
32713346a802087f621c6441bc512dfcc17b07143fc6John Spurlock
32723346a802087f621c6441bc512dfcc17b07143fc6John Spurlock    /**
32733346a802087f621c6441bc512dfcc17b07143fc6John Spurlock     * Only useful for volume controllers.
32743346a802087f621c6441bc512dfcc17b07143fc6John Spurlock     * @hide
32753346a802087f621c6441bc512dfcc17b07143fc6John Spurlock     */
3276a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock    public boolean isStreamAffectedByMute(int streamType) {
3277a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock        try {
3278a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock            return getService().isStreamAffectedByMute(streamType);
3279a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock        } catch (RemoteException e) {
3280a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock            Log.w(TAG, "Error calling isStreamAffectedByMute", e);
3281a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock            return false;
3282a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock        }
3283a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock    }
3284a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock
3285a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock    /**
3286a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock     * Only useful for volume controllers.
3287a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock     * @hide
3288a9dfbe8b122d746cf8841fe1b13d31e7e0fc9c52John Spurlock     */
32893346a802087f621c6441bc512dfcc17b07143fc6John Spurlock    public void disableSafeMediaVolume() {
32903346a802087f621c6441bc512dfcc17b07143fc6John Spurlock        try {
3291a80ac05f85962949d5072831200d86a7023575fbMarco Nelissen            getService().disableSafeMediaVolume(mApplicationContext.getOpPackageName());
32923346a802087f621c6441bc512dfcc17b07143fc6John Spurlock        } catch (RemoteException e) {
32933346a802087f621c6441bc512dfcc17b07143fc6John Spurlock            Log.w(TAG, "Error disabling safe media volume", e);
32943346a802087f621c6441bc512dfcc17b07143fc6John Spurlock        }
32953346a802087f621c6441bc512dfcc17b07143fc6John Spurlock    }
3296a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3297a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /**
3298661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock     * Only useful for volume controllers.
3299661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock     * @hide
3300661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock     */
3301661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    public void setRingerModeInternal(int ringerMode) {
3302661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        try {
330329f16933a8a93753c83bb5f1e0b5a410ea3a29f2Marco Nelissen            getService().setRingerModeInternal(ringerMode, getContext().getOpPackageName());
3304661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        } catch (RemoteException e) {
3305661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock            Log.w(TAG, "Error calling setRingerModeInternal", e);
3306661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        }
3307661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    }
3308661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock
3309661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    /**
3310661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock     * Only useful for volume controllers.
3311661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock     * @hide
3312661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock     */
3313661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    public int getRingerModeInternal() {
3314661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        try {
3315661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock            return getService().getRingerModeInternal();
3316661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        } catch (RemoteException e) {
3317661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock            Log.w(TAG, "Error calling getRingerModeInternal", e);
3318661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock            return RINGER_MODE_NORMAL;
3319661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock        }
3320661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    }
3321661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock
3322661f2cf45860d2e10924e6b69966a9afe255f28bJohn Spurlock    /**
3323a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock     * Only useful for volume controllers.
3324a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock     * @hide
3325a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock     */
3326a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock    public void setVolumePolicy(VolumePolicy policy) {
3327a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock        try {
3328a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock            getService().setVolumePolicy(policy);
3329a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock        } catch (RemoteException e) {
3330a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock            Log.w(TAG, "Error calling setVolumePolicy", e);
3331a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock        }
3332a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock    }
3333a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock
3334a48d779dad4c48aa2f97ad021ae3a5649c058312John Spurlock    /**
333541d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang     * Set Hdmi Cec system audio mode.
333641d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang     *
333741d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang     * @param on whether to be on system audio mode
33386f34f5ab8ab1b1db7887e5405d8b0031e105ab05Jungshik Jang     * @return output device type. 0 (DEVICE_NONE) if failed to set device.
333941d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang     * @hide
334041d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang     */
334112307ca810e8100981b2b60e3f2c6a7e451b9774Jungshik Jang    public int setHdmiSystemAudioSupported(boolean on) {
334241d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang        try {
334312307ca810e8100981b2b60e3f2c6a7e451b9774Jungshik Jang            return getService().setHdmiSystemAudioSupported(on);
334441d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang        } catch (RemoteException e) {
334541d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang            Log.w(TAG, "Error setting system audio mode", e);
33466f34f5ab8ab1b1db7887e5405d8b0031e105ab05Jungshik Jang            return AudioSystem.DEVICE_NONE;
334741d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang        }
334841d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang    }
334941d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang
335041d974631c5f525da49c88d34cecedd5a4cfeda8Jungshik Jang    /**
3351e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo     * Returns true if Hdmi Cec system audio mode is supported.
3352e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo     *
3353e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo     * @hide
3354e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo     */
3355e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo    @SystemApi
3356e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo    public boolean isHdmiSystemAudioSupported() {
3357e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo        try {
3358e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo            return getService().isHdmiSystemAudioSupported();
3359e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo        } catch (RemoteException e) {
3360e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo            Log.w(TAG, "Error querying system audio mode", e);
3361e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo            return false;
3362e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo        }
3363e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo    }
3364e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo
3365e7d6d97f0d95b9e8982b0d4d9f5e8ce688291940Terry Heo    /**
3366a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * Return codes for listAudioPorts(), createAudioPatch() ...
3367a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
3368a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3369a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /** @hide
3370a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     * CANDIDATE FOR PUBLIC API
3371a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
3372a198a29250acb7c3e918f1566727190966bb336fEric Laurent    public static final int SUCCESS = AudioSystem.SUCCESS;
33738a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi    /**
33748a21f5dd79e93aa4e4b08ab4f33b9255d7c06961Jean-Michel Trivi     * A default error code.
3375a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
3376a198a29250acb7c3e918f1566727190966bb336fEric Laurent    public static final int ERROR = AudioSystem.ERROR;
3377a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /** @hide
3378a8b6bd88cfb010c9e9aa1339e504fd593919e1e0Jean-Michel Trivi     * CANDIDATE FOR PUBLIC API
3379a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
3380a198a29250acb7c3e918f1566727190966bb336fEric Laurent    public static final int ERROR_BAD_VALUE = AudioSystem.BAD_VALUE;
3381a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /** @hide
3382a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
3383a198a29250acb7c3e918f1566727190966bb336fEric Laurent    public static final int ERROR_INVALID_OPERATION = AudioSystem.INVALID_OPERATION;
3384a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /** @hide
3385a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
3386a198a29250acb7c3e918f1566727190966bb336fEric Laurent    public static final int ERROR_PERMISSION_DENIED = AudioSystem.PERMISSION_DENIED;
3387a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /** @hide
3388a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
3389a198a29250acb7c3e918f1566727190966bb336fEric Laurent    public static final int ERROR_NO_INIT = AudioSystem.NO_INIT;
3390ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent    /**
3391ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent     * An error code indicating that the object reporting it is no longer valid and needs to
3392ff0d9f098e51c54e1a030ed21fd980680cb7b405Eric Laurent     * be recreated.
3393a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
3394a198a29250acb7c3e918f1566727190966bb336fEric Laurent    public static final int ERROR_DEAD_OBJECT = AudioSystem.DEAD_OBJECT;
3395a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3396a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /**
3397a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * Returns a list of descriptors for all audio ports managed by the audio framework.
3398a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * Audio ports are nodes in the audio framework or audio hardware that can be configured
3399a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * or connected and disconnected with createAudioPatch() or releaseAudioPatch().
3400a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * See AudioPort for a list of attributes of each audio port.
3401a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @param ports An AudioPort ArrayList where the list will be returned.
3402a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @hide
3403a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
34044bcdba848449b33d7022de527c526943aff1f5fdEric Laurent    public static int listAudioPorts(ArrayList<AudioPort> ports) {
34058a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        return updateAudioPortCache(ports, null, null);
34068a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent    }
34078a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent
34088a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent    /**
34098a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent     * Returns a list of descriptors for all audio ports managed by the audio framework as
34108a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent     * it was before the last update calback.
34118a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent     * @param ports An AudioPort ArrayList where the list will be returned.
34128a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent     * @hide
34138a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent     */
34148a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent    public static int listPreviousAudioPorts(ArrayList<AudioPort> ports) {
34158a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        return updateAudioPortCache(null, null, ports);
3416a198a29250acb7c3e918f1566727190966bb336fEric Laurent    }
3417a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3418a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /**
3419a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * Specialized version of listAudioPorts() listing only audio devices (AudioDevicePort)
3420a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @see listAudioPorts(ArrayList<AudioPort>)
3421a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @hide
3422a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
34234bcdba848449b33d7022de527c526943aff1f5fdEric Laurent    public static int listAudioDevicePorts(ArrayList<AudioDevicePort> devices) {
34248a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        if (devices == null) {
34258a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent            return ERROR_BAD_VALUE;
34268a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        }
3427b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        ArrayList<AudioPort> ports = new ArrayList<AudioPort>();
34288a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        int status = updateAudioPortCache(ports, null, null);
3429b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        if (status == SUCCESS) {
34308a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent            filterDevicePorts(ports, devices);
34318a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        }
34328a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        return status;
34338a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent    }
34348a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent
34358a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent    /**
34368a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent     * Specialized version of listPreviousAudioPorts() listing only audio devices (AudioDevicePort)
34378a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent     * @see listPreviousAudioPorts(ArrayList<AudioPort>)
34388a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent     * @hide
34398a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent     */
34408a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent    public static int listPreviousAudioDevicePorts(ArrayList<AudioDevicePort> devices) {
34418a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        if (devices == null) {
34428a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent            return ERROR_BAD_VALUE;
34438a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        }
34448a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        ArrayList<AudioPort> ports = new ArrayList<AudioPort>();
34458a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        int status = updateAudioPortCache(null, null, ports);
34468a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        if (status == SUCCESS) {
34478a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent            filterDevicePorts(ports, devices);
3448b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        }
3449b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        return status;
3450a198a29250acb7c3e918f1566727190966bb336fEric Laurent    }
3451a198a29250acb7c3e918f1566727190966bb336fEric Laurent
34528a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent    private static void filterDevicePorts(ArrayList<AudioPort> ports,
34538a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent                                          ArrayList<AudioDevicePort> devices) {
34548a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        devices.clear();
34558a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        for (int i = 0; i < ports.size(); i++) {
34568a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent            if (ports.get(i) instanceof AudioDevicePort) {
34578a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent                devices.add((AudioDevicePort)ports.get(i));
34588a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent            }
34598a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        }
34608a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent    }
34618a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent
3462a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /**
3463a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * Create a connection between two or more devices. The framework will reject the request if
3464a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * device types are not compatible or the implementation does not support the requested
3465a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * configuration.
3466a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * NOTE: current implementation is limited to one source and one sink per patch.
3467a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @param patch AudioPatch array where the newly created patch will be returned.
3468a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *              As input, if patch[0] is not null, the specified patch will be replaced by the
3469a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *              new patch created. This avoids calling releaseAudioPatch() when modifying a
3470a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *              patch and allows the implementation to optimize transitions.
3471a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @param sources List of source audio ports. All must be AudioPort.ROLE_SOURCE.
3472a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @param sinks   List of sink audio ports. All must be AudioPort.ROLE_SINK.
3473a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *
3474a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @return - {@link #SUCCESS} if connection is successful.
3475a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         - {@link #ERROR_BAD_VALUE} if incompatible device types are passed.
3476a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         - {@link #ERROR_INVALID_OPERATION} if the requested connection is not supported.
3477a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         - {@link #ERROR_PERMISSION_DENIED} if the client does not have permission to create
3478a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         a patch.
3479a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         - {@link #ERROR_DEAD_OBJECT} if the server process is dead
3480a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         - {@link #ERROR} if patch cannot be connected for any other reason.
3481a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *
3482a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         patch[0] contains the newly created patch
3483a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @hide
3484a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
34854bcdba848449b33d7022de527c526943aff1f5fdEric Laurent    public static int createAudioPatch(AudioPatch[] patch,
3486a198a29250acb7c3e918f1566727190966bb336fEric Laurent                                 AudioPortConfig[] sources,
3487a198a29250acb7c3e918f1566727190966bb336fEric Laurent                                 AudioPortConfig[] sinks) {
3488b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        return AudioSystem.createAudioPatch(patch, sources, sinks);
3489a198a29250acb7c3e918f1566727190966bb336fEric Laurent    }
3490a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3491a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /**
3492a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * Releases an existing audio patch connection.
3493a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @param patch The audio patch to disconnect.
3494a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @return - {@link #SUCCESS} if disconnection is successful.
3495a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         - {@link #ERROR_BAD_VALUE} if the specified patch does not exist.
3496a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         - {@link #ERROR_PERMISSION_DENIED} if the client does not have permission to release
3497a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         a patch.
3498a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         - {@link #ERROR_DEAD_OBJECT} if the server process is dead
3499a198a29250acb7c3e918f1566727190966bb336fEric Laurent     *         - {@link #ERROR} if patch cannot be released for any other reason.
3500a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @hide
3501a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
35024bcdba848449b33d7022de527c526943aff1f5fdEric Laurent    public static int releaseAudioPatch(AudioPatch patch) {
3503b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        return AudioSystem.releaseAudioPatch(patch);
3504a198a29250acb7c3e918f1566727190966bb336fEric Laurent    }
3505a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3506a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /**
3507a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * List all existing connections between audio ports.
3508a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @param patches An AudioPatch array where the list will be returned.
3509a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @hide
3510a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
35114bcdba848449b33d7022de527c526943aff1f5fdEric Laurent    public static int listAudioPatches(ArrayList<AudioPatch> patches) {
35128a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent        return updateAudioPortCache(null, patches, null);
3513a198a29250acb7c3e918f1566727190966bb336fEric Laurent    }
3514a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3515a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /**
3516a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * Set the gain on the specified AudioPort. The AudioGainConfig config is build by
3517a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * AudioGain.buildConfig()
3518a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @hide
3519a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
35204bcdba848449b33d7022de527c526943aff1f5fdEric Laurent    public static int setAudioPortGain(AudioPort port, AudioGainConfig gain) {
35213a24199a0fe9479802ff6096d82cb3745efa1f1fEric Laurent        if (port == null || gain == null) {
35223a24199a0fe9479802ff6096d82cb3745efa1f1fEric Laurent            return ERROR_BAD_VALUE;
35233a24199a0fe9479802ff6096d82cb3745efa1f1fEric Laurent        }
35243a24199a0fe9479802ff6096d82cb3745efa1f1fEric Laurent        AudioPortConfig activeConfig = port.activeConfig();
35253a24199a0fe9479802ff6096d82cb3745efa1f1fEric Laurent        AudioPortConfig config = new AudioPortConfig(port, activeConfig.samplingRate(),
35263a24199a0fe9479802ff6096d82cb3745efa1f1fEric Laurent                                        activeConfig.channelMask(), activeConfig.format(), gain);
35273a24199a0fe9479802ff6096d82cb3745efa1f1fEric Laurent        config.mConfigMask = AudioPortConfig.GAIN;
35283a24199a0fe9479802ff6096d82cb3745efa1f1fEric Laurent        return AudioSystem.setAudioPortConfig(config);
3529a198a29250acb7c3e918f1566727190966bb336fEric Laurent    }
3530a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3531a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /**
3532a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * Listener registered by client to be notified upon new audio port connections,
3533a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * disconnections or attributes update.
3534a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @hide
3535a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
3536a198a29250acb7c3e918f1566727190966bb336fEric Laurent    public interface OnAudioPortUpdateListener {
3537a198a29250acb7c3e918f1566727190966bb336fEric Laurent        /**
3538a198a29250acb7c3e918f1566727190966bb336fEric Laurent         * Callback method called upon audio port list update.
3539a198a29250acb7c3e918f1566727190966bb336fEric Laurent         * @param portList the updated list of audio ports
3540a198a29250acb7c3e918f1566727190966bb336fEric Laurent         */
3541fdaed9dcb63171379fd7c5a6208014bac505dbc6Eric Laurent        public void onAudioPortListUpdate(AudioPort[] portList);
3542a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3543a198a29250acb7c3e918f1566727190966bb336fEric Laurent        /**
3544a198a29250acb7c3e918f1566727190966bb336fEric Laurent         * Callback method called upon audio patch list update.
3545a198a29250acb7c3e918f1566727190966bb336fEric Laurent         * @param patchList the updated list of audio patches
3546a198a29250acb7c3e918f1566727190966bb336fEric Laurent         */
3547fdaed9dcb63171379fd7c5a6208014bac505dbc6Eric Laurent        public void onAudioPatchListUpdate(AudioPatch[] patchList);
3548a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3549a198a29250acb7c3e918f1566727190966bb336fEric Laurent        /**
3550a198a29250acb7c3e918f1566727190966bb336fEric Laurent         * Callback method called when the mediaserver dies
3551a198a29250acb7c3e918f1566727190966bb336fEric Laurent         */
3552fdaed9dcb63171379fd7c5a6208014bac505dbc6Eric Laurent        public void onServiceDied();
3553a198a29250acb7c3e918f1566727190966bb336fEric Laurent    }
3554a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3555a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /**
3556700e73471d85348b52ecf213c36bb24b93997ec7Eric Laurent     * Register an audio port list update listener.
3557a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @hide
3558a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
3559a198a29250acb7c3e918f1566727190966bb336fEric Laurent    public void registerAudioPortUpdateListener(OnAudioPortUpdateListener l) {
3560f076db407029e734703d8676f806bea94393e824Eric Laurent        sAudioPortEventHandler.registerListener(l);
3561a198a29250acb7c3e918f1566727190966bb336fEric Laurent    }
3562a198a29250acb7c3e918f1566727190966bb336fEric Laurent
3563a198a29250acb7c3e918f1566727190966bb336fEric Laurent    /**
3564700e73471d85348b52ecf213c36bb24b93997ec7Eric Laurent     * Unregister an audio port list update listener.
3565a198a29250acb7c3e918f1566727190966bb336fEric Laurent     * @hide
3566a198a29250acb7c3e918f1566727190966bb336fEric Laurent     */
3567a198a29250acb7c3e918f1566727190966bb336fEric Laurent    public void unregisterAudioPortUpdateListener(OnAudioPortUpdateListener l) {
3568f076db407029e734703d8676f806bea94393e824Eric Laurent        sAudioPortEventHandler.unregisterListener(l);
3569a198a29250acb7c3e918f1566727190966bb336fEric Laurent    }
3570b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent
3571b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent    //
3572b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent    // AudioPort implementation
3573b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent    //
3574b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent
3575b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent    static final int AUDIOPORT_GENERATION_INIT = 0;
3576f076db407029e734703d8676f806bea94393e824Eric Laurent    static Integer sAudioPortGeneration = new Integer(AUDIOPORT_GENERATION_INIT);
3577f076db407029e734703d8676f806bea94393e824Eric Laurent    static ArrayList<AudioPort> sAudioPortsCached = new ArrayList<AudioPort>();
35788a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent    static ArrayList<AudioPort> sPreviousAudioPortsCached = new ArrayList<AudioPort>();
3579f076db407029e734703d8676f806bea94393e824Eric Laurent    static ArrayList<AudioPatch> sAudioPatchesCached = new ArrayList<AudioPatch>();
3580b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent
3581f076db407029e734703d8676f806bea94393e824Eric Laurent    static int resetAudioPortGeneration() {
3582b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        int generation;
3583f076db407029e734703d8676f806bea94393e824Eric Laurent        synchronized (sAudioPortGeneration) {
3584f076db407029e734703d8676f806bea94393e824Eric Laurent            generation = sAudioPortGeneration;
3585f076db407029e734703d8676f806bea94393e824Eric Laurent            sAudioPortGeneration = AUDIOPORT_GENERATION_INIT;
3586b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        }
3587b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        return generation;
3588b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent    }
3589b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent
35908a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent    static int updateAudioPortCache(ArrayList<AudioPort> ports, ArrayList<AudioPatch> patches,
35918a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent                                    ArrayList<AudioPort> previousPorts) {
3592f076db407029e734703d8676f806bea94393e824Eric Laurent        synchronized (sAudioPortGeneration) {
3593b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent
3594f076db407029e734703d8676f806bea94393e824Eric Laurent            if (sAudioPortGeneration == AUDIOPORT_GENERATION_INIT) {
3595b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                int[] patchGeneration = new int[1];
3596b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                int[] portGeneration = new int[1];
3597b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                int status;
3598b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                ArrayList<AudioPort> newPorts = new ArrayList<AudioPort>();
3599b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                ArrayList<AudioPatch> newPatches = new ArrayList<AudioPatch>();
3600b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent
3601b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                do {
3602b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    newPorts.clear();
3603b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    status = AudioSystem.listAudioPorts(newPorts, portGeneration);
3604b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    if (status != SUCCESS) {
3605b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                        Log.w(TAG, "updateAudioPortCache: listAudioPorts failed");
3606b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                        return status;
3607b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    }
3608b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    newPatches.clear();
3609b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    status = AudioSystem.listAudioPatches(newPatches, patchGeneration);
3610b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    if (status != SUCCESS) {
3611b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                        Log.w(TAG, "updateAudioPortCache: listAudioPatches failed");
3612b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                        return status;
3613b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    }
3614b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                } while (patchGeneration[0] != portGeneration[0]);
3615b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent
3616b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                for (int i = 0; i < newPatches.size(); i++) {
3617b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    for (int j = 0; j < newPatches.get(i).sources().length; j++) {
3618b4e0909fc4fb08f513cb2b39a919b189621be87cEric Laurent                        AudioPortConfig portCfg = updatePortConfig(newPatches.get(i).sources()[j],
3619b4e0909fc4fb08f513cb2b39a919b189621be87cEric Laurent                                                                   newPorts);
3620b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                        newPatches.get(i).sources()[j] = portCfg;
3621b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    }
3622b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    for (int j = 0; j < newPatches.get(i).sinks().length; j++) {
3623b4e0909fc4fb08f513cb2b39a919b189621be87cEric Laurent                        AudioPortConfig portCfg = updatePortConfig(newPatches.get(i).sinks()[j],
3624b4e0909fc4fb08f513cb2b39a919b189621be87cEric Laurent                                                                   newPorts);
3625b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                        newPatches.get(i).sinks()[j] = portCfg;
3626b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                    }
3627b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                }
3628b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                for (Iterator<AudioPatch> i = newPatches.iterator(); i.hasNext(); ) {
3629b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                    AudioPatch newPatch = i.next();
3630b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                    boolean hasInvalidPort = false;
3631b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                    for (AudioPortConfig portCfg : newPatch.sources()) {
3632b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                        if (portCfg == null) {
3633b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                            hasInvalidPort = true;
3634b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                            break;
3635b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                        }
3636b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                    }
3637b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                    for (AudioPortConfig portCfg : newPatch.sinks()) {
3638b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                        if (portCfg == null) {
3639b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                            hasInvalidPort = true;
3640b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                            break;
3641b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                        }
3642b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                    }
3643b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                    if (hasInvalidPort) {
3644b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                        // Temporarily remove patches with invalid ports. One who created the patch
3645b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                        // is responsible for dealing with the port change.
3646b561ccedd89f92b249c06522e960b135a02107d8Wonsik Kim                        i.remove();
3647b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                    }
3648b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                }
3649b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent
36508a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent                sPreviousAudioPortsCached = sAudioPortsCached;
3651f076db407029e734703d8676f806bea94393e824Eric Laurent                sAudioPortsCached = newPorts;
3652f076db407029e734703d8676f806bea94393e824Eric Laurent                sAudioPatchesCached = newPatches;
3653f076db407029e734703d8676f806bea94393e824Eric Laurent                sAudioPortGeneration = portGeneration[0];
3654b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            }
3655b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            if (ports != null) {
3656b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                ports.clear();
3657f076db407029e734703d8676f806bea94393e824Eric Laurent                ports.addAll(sAudioPortsCached);
3658b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            }
3659b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            if (patches != null) {
3660b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                patches.clear();
3661f076db407029e734703d8676f806bea94393e824Eric Laurent                patches.addAll(sAudioPatchesCached);
3662b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            }
36638a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent            if (previousPorts != null) {
36648a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent                previousPorts.clear();
36658a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent                previousPorts.addAll(sPreviousAudioPortsCached);
36668a1e7a881ee73b9b1e492e7014c3eaefe81cfbd7Eric Laurent            }
3667b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        }
3668b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        return SUCCESS;
3669b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent    }
3670b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent
3671f076db407029e734703d8676f806bea94393e824Eric Laurent    static AudioPortConfig updatePortConfig(AudioPortConfig portCfg, ArrayList<AudioPort> ports) {
3672b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        AudioPort port = portCfg.port();
3673b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        int k;
3674b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        for (k = 0; k < ports.size(); k++) {
3675b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            // compare handles because the port returned by JNI is not of the correct
3676b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            // subclass
3677b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            if (ports.get(k).handle().equals(port.handle())) {
3678b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                port = ports.get(k);
3679b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                break;
3680b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            }
3681b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        }
3682b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        if (k == ports.size()) {
3683b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            // this hould never happen
3684b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            Log.e(TAG, "updatePortConfig port not found for handle: "+port.handle().id());
3685b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            return null;
3686b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        }
3687b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        AudioGainConfig gainCfg = portCfg.gain();
3688b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        if (gainCfg != null) {
3689b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            AudioGain gain = port.gain(gainCfg.index());
3690b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent            gainCfg = gain.buildConfig(gainCfg.mode(),
3691b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                                       gainCfg.channelMask(),
3692b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                                       gainCfg.values(),
3693b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                                       gainCfg.rampDurationMs());
3694b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        }
3695b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent        return port.buildConfig(portCfg.samplingRate(),
3696b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                                                 portCfg.channelMask(),
3697b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                                                 portCfg.format(),
3698b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent                                                 gainCfg);
3699b69681c894c663e84f2826d9b0c832ceb9b45047Eric Laurent    }
37009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3701