The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.media; |
| 18 | |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 19 | import android.annotation.CallbackExecutor; |
Jean-Michel Trivi | 8e7aca3 | 2017-11-28 18:12:38 -0800 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Hayden Gomes | 695f802 | 2019-04-11 10:44:18 -0700 | [diff] [blame] | 21 | import android.annotation.IntRange; |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 24 | import android.annotation.RequiresPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.annotation.SdkConstant; |
| 26 | import android.annotation.SdkConstant.SdkConstantType; |
Jean-Michel Trivi | 26ae15f | 2017-08-25 19:20:49 -0700 | [diff] [blame] | 27 | import android.annotation.SuppressLint; |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 28 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 29 | import android.annotation.SystemService; |
Jean-Michel Trivi | ec97732 | 2019-04-12 11:20:35 -0700 | [diff] [blame] | 30 | import android.annotation.TestApi; |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 31 | import android.app.NotificationManager; |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 32 | import android.app.PendingIntent; |
Eric Laurent | 1c3408f | 2021-11-09 12:09:54 +0100 | [diff] [blame] | 33 | import android.app.compat.CompatChanges; |
Arun Mirpuri | cb102fa | 2019-01-11 18:39:21 -0800 | [diff] [blame] | 34 | import android.bluetooth.BluetoothCodecConfig; |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 35 | import android.bluetooth.BluetoothDevice; |
Eric Laurent | 1c3408f | 2021-11-09 12:09:54 +0100 | [diff] [blame] | 36 | import android.compat.annotation.ChangeId; |
| 37 | import android.compat.annotation.EnabledSince; |
Artur Satayev | 53fe966 | 2019-12-10 17:47:55 +0000 | [diff] [blame] | 38 | import android.compat.annotation.UnsupportedAppUsage; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 39 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.content.Context; |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 41 | import android.content.Intent; |
Hayden Gomes | 62812aa | 2019-12-23 11:40:27 -0800 | [diff] [blame] | 42 | import android.media.AudioAttributes.AttributeSystemUsage; |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 43 | import android.media.CallbackUtil.ListenerInfo; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 44 | import android.media.audiopolicy.AudioPolicy; |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 45 | import android.media.audiopolicy.AudioPolicy.AudioPolicyFocusListener; |
Hayden Gomes | 6d69bde | 2019-04-04 13:10:13 -0700 | [diff] [blame] | 46 | import android.media.audiopolicy.AudioProductStrategy; |
Hayden Gomes | ebd6aaa | 2019-04-04 13:14:21 -0700 | [diff] [blame] | 47 | import android.media.audiopolicy.AudioVolumeGroup; |
François Gaffie | adcd00a | 2018-09-18 17:06:26 +0200 | [diff] [blame] | 48 | import android.media.audiopolicy.AudioVolumeGroupChangeHandler; |
Kevin Rocard | 2fe8b8b | 2019-02-20 11:25:23 -0800 | [diff] [blame] | 49 | import android.media.projection.MediaProjection; |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 50 | import android.media.session.MediaController; |
| 51 | import android.media.session.MediaSession; |
RoboErik | f137242 | 2014-04-23 14:38:17 -0700 | [diff] [blame] | 52 | import android.media.session.MediaSessionLegacyHelper; |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 53 | import android.media.session.MediaSessionManager; |
jiabin | ad22520 | 2019-03-20 15:22:50 -0700 | [diff] [blame] | 54 | import android.net.Uri; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.os.Binder; |
Jean-Michel Trivi | 461922f | 2017-04-25 15:23:17 -0700 | [diff] [blame] | 56 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.os.Handler; |
| 58 | import android.os.IBinder; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 59 | import android.os.Looper; |
| 60 | import android.os.Message; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | import android.os.RemoteException; |
| 62 | import android.os.ServiceManager; |
Beverly | e2d9a23 | 2017-11-08 18:14:59 -0500 | [diff] [blame] | 63 | import android.os.SystemClock; |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 64 | import android.os.UserHandle; |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 65 | import android.text.TextUtils; |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 66 | import android.util.ArrayMap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | import android.util.Log; |
jiabin | 589a236 | 2018-02-22 16:21:53 -0800 | [diff] [blame] | 68 | import android.util.Pair; |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 69 | import android.view.KeyEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 71 | import com.android.internal.annotations.GuardedBy; |
François Gaffie | adcd00a | 2018-09-18 17:06:26 +0200 | [diff] [blame] | 72 | import com.android.internal.util.Preconditions; |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 73 | |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 74 | import java.io.IOException; |
Jean-Michel Trivi | 8e7aca3 | 2017-11-28 18:12:38 -0800 | [diff] [blame] | 75 | import java.lang.annotation.Retention; |
| 76 | import java.lang.annotation.RetentionPolicy; |
jiabin | 0f3339c | 2021-07-09 11:50:07 -0700 | [diff] [blame] | 77 | import java.lang.ref.WeakReference; |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 78 | import java.util.ArrayList; |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 79 | import java.util.Arrays; |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 80 | import java.util.HashMap; |
jiabin | d0be5b2 | 2018-04-10 14:10:04 -0700 | [diff] [blame] | 81 | import java.util.HashSet; |
Wonsik Kim | b561cce | 2015-01-30 17:48:51 +0900 | [diff] [blame] | 82 | import java.util.Iterator; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 83 | import java.util.List; |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 84 | import java.util.Map; |
Jean-Michel Trivi | 21cd541 | 2019-10-30 08:16:51 -0700 | [diff] [blame] | 85 | import java.util.Objects; |
Hyundo Moon | ca0080d | 2018-12-26 16:16:55 +0900 | [diff] [blame] | 86 | import java.util.TreeMap; |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 87 | import java.util.concurrent.ConcurrentHashMap; |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 88 | import java.util.concurrent.Executor; |
| 89 | |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 90 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | /** |
| 92 | * AudioManager provides access to volume and ringer mode control. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 94 | @SystemService(Context.AUDIO_SERVICE) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | public class AudioManager { |
| 96 | |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 97 | private Context mOriginalContext; |
| 98 | private Context mApplicationContext; |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 99 | private long mVolumeKeyUpTime; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 100 | private static final String TAG = "AudioManager"; |
| 101 | private static final boolean DEBUG = false; |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 102 | private static final AudioPortEventHandler sAudioPortEventHandler = new AudioPortEventHandler(); |
François Gaffie | adcd00a | 2018-09-18 17:06:26 +0200 | [diff] [blame] | 103 | private static final AudioVolumeGroupChangeHandler sAudioAudioVolumeGroupChangedHandler = |
| 104 | new AudioVolumeGroupChangeHandler(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | |
jiabin | 0f3339c | 2021-07-09 11:50:07 -0700 | [diff] [blame] | 106 | private static WeakReference<Context> sContext; |
jiabin | cfcf103 | 2021-07-01 16:30:50 -0700 | [diff] [blame] | 107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | /** |
| 109 | * Broadcast intent, a hint for applications that audio is about to become |
| 110 | * 'noisy' due to a change in audio outputs. For example, this intent may |
| 111 | * be sent when a wired headset is unplugged, or when an A2DP audio |
| 112 | * sink is disconnected, and the audio system is about to automatically |
| 113 | * switch audio route to the speaker. Applications that are controlling |
| 114 | * audio streams may consider pausing, reducing volume or some other action |
| 115 | * on receipt of this intent so as not to surprise the user with audio |
| 116 | * from the speaker. |
| 117 | */ |
| 118 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 119 | public static final String ACTION_AUDIO_BECOMING_NOISY = "android.media.AUDIO_BECOMING_NOISY"; |
| 120 | |
| 121 | /** |
| 122 | * Sticky broadcast intent action indicating that the ringer mode has |
| 123 | * changed. Includes the new ringer mode. |
| 124 | * |
| 125 | * @see #EXTRA_RINGER_MODE |
| 126 | */ |
| 127 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 128 | public static final String RINGER_MODE_CHANGED_ACTION = "android.media.RINGER_MODE_CHANGED"; |
| 129 | |
| 130 | /** |
John Spurlock | bcc1087 | 2014-11-28 15:29:21 -0500 | [diff] [blame] | 131 | * @hide |
| 132 | * Sticky broadcast intent action indicating that the internal ringer mode has |
| 133 | * changed. Includes the new ringer mode. |
| 134 | * |
| 135 | * @see #EXTRA_RINGER_MODE |
| 136 | */ |
| 137 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 138 | public static final String INTERNAL_RINGER_MODE_CHANGED_ACTION = |
| 139 | "android.media.INTERNAL_RINGER_MODE_CHANGED_ACTION"; |
| 140 | |
| 141 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | * The new ringer mode. |
| 143 | * |
| 144 | * @see #RINGER_MODE_CHANGED_ACTION |
| 145 | * @see #RINGER_MODE_NORMAL |
| 146 | * @see #RINGER_MODE_SILENT |
| 147 | * @see #RINGER_MODE_VIBRATE |
| 148 | */ |
| 149 | public static final String EXTRA_RINGER_MODE = "android.media.EXTRA_RINGER_MODE"; |
| 150 | |
| 151 | /** |
| 152 | * Broadcast intent action indicating that the vibrate setting has |
| 153 | * changed. Includes the vibrate type and its new setting. |
| 154 | * |
| 155 | * @see #EXTRA_VIBRATE_TYPE |
| 156 | * @see #EXTRA_VIBRATE_SETTING |
Eric Laurent | cd1cd73 | 2012-05-01 11:23:07 -0700 | [diff] [blame] | 157 | * @deprecated Applications should maintain their own vibrate policy based on |
| 158 | * current ringer mode and listen to {@link #RINGER_MODE_CHANGED_ACTION} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | */ |
| 160 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 161 | public static final String VIBRATE_SETTING_CHANGED_ACTION = |
| 162 | "android.media.VIBRATE_SETTING_CHANGED"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * @hide Broadcast intent when the volume for a particular stream type changes. |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 166 | * Includes the stream, the new volume and previous volumes. |
| 167 | * Notes: |
| 168 | * - for internal platform use only, do not make public, |
| 169 | * - never used for "remote" volume changes |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | * |
| 171 | * @see #EXTRA_VOLUME_STREAM_TYPE |
| 172 | * @see #EXTRA_VOLUME_STREAM_VALUE |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 173 | * @see #EXTRA_PREV_VOLUME_STREAM_VALUE |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | */ |
| 175 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 176 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | public static final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION"; |
| 178 | |
| 179 | /** |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 180 | * @hide Broadcast intent when the devices for a particular stream type changes. |
| 181 | * Includes the stream, the new devices and previous devices. |
| 182 | * Notes: |
| 183 | * - for internal platform use only, do not make public, |
| 184 | * - never used for "remote" volume changes |
| 185 | * |
| 186 | * @see #EXTRA_VOLUME_STREAM_TYPE |
| 187 | * @see #EXTRA_VOLUME_STREAM_DEVICES |
| 188 | * @see #EXTRA_PREV_VOLUME_STREAM_DEVICES |
| 189 | * @see #getDevicesForStream |
| 190 | */ |
| 191 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 192 | public static final String STREAM_DEVICES_CHANGED_ACTION = |
| 193 | "android.media.STREAM_DEVICES_CHANGED_ACTION"; |
| 194 | |
| 195 | /** |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 196 | * @hide Broadcast intent when a stream mute state changes. |
| 197 | * Includes the stream that changed and the new mute state |
| 198 | * |
| 199 | * @see #EXTRA_VOLUME_STREAM_TYPE |
| 200 | * @see #EXTRA_STREAM_VOLUME_MUTED |
| 201 | */ |
| 202 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 203 | public static final String STREAM_MUTE_CHANGED_ACTION = |
| 204 | "android.media.STREAM_MUTE_CHANGED_ACTION"; |
| 205 | |
| 206 | /** |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 207 | * @hide Broadcast intent when the master mute state changes. |
| 208 | * Includes the the new volume |
| 209 | * |
| 210 | * @see #EXTRA_MASTER_VOLUME_MUTED |
| 211 | */ |
| 212 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 213 | public static final String MASTER_MUTE_CHANGED_ACTION = |
| 214 | "android.media.MASTER_MUTE_CHANGED_ACTION"; |
| 215 | |
| 216 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | * The new vibrate setting for a particular type. |
| 218 | * |
| 219 | * @see #VIBRATE_SETTING_CHANGED_ACTION |
| 220 | * @see #EXTRA_VIBRATE_TYPE |
| 221 | * @see #VIBRATE_SETTING_ON |
| 222 | * @see #VIBRATE_SETTING_OFF |
| 223 | * @see #VIBRATE_SETTING_ONLY_SILENT |
Eric Laurent | cd1cd73 | 2012-05-01 11:23:07 -0700 | [diff] [blame] | 224 | * @deprecated Applications should maintain their own vibrate policy based on |
| 225 | * current ringer mode and listen to {@link #RINGER_MODE_CHANGED_ACTION} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | */ |
| 227 | public static final String EXTRA_VIBRATE_SETTING = "android.media.EXTRA_VIBRATE_SETTING"; |
| 228 | |
| 229 | /** |
| 230 | * The vibrate type whose setting has changed. |
| 231 | * |
| 232 | * @see #VIBRATE_SETTING_CHANGED_ACTION |
| 233 | * @see #VIBRATE_TYPE_NOTIFICATION |
| 234 | * @see #VIBRATE_TYPE_RINGER |
Eric Laurent | cd1cd73 | 2012-05-01 11:23:07 -0700 | [diff] [blame] | 235 | * @deprecated Applications should maintain their own vibrate policy based on |
| 236 | * current ringer mode and listen to {@link #RINGER_MODE_CHANGED_ACTION} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | */ |
| 238 | public static final String EXTRA_VIBRATE_TYPE = "android.media.EXTRA_VIBRATE_TYPE"; |
| 239 | |
| 240 | /** |
| 241 | * @hide The stream type for the volume changed intent. |
| 242 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 243 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | public static final String EXTRA_VOLUME_STREAM_TYPE = "android.media.EXTRA_VOLUME_STREAM_TYPE"; |
| 245 | |
| 246 | /** |
Jean-Michel Trivi | 560877d | 2015-06-25 17:38:35 -0700 | [diff] [blame] | 247 | * @hide |
| 248 | * The stream type alias for the volume changed intent. |
| 249 | * For instance the intent may indicate a change of the {@link #STREAM_NOTIFICATION} stream |
| 250 | * type (as indicated by the {@link #EXTRA_VOLUME_STREAM_TYPE} extra), but this is also |
| 251 | * reflected by a change of the volume of its alias, {@link #STREAM_RING} on some devices, |
| 252 | * {@link #STREAM_MUSIC} on others (e.g. a television). |
| 253 | */ |
| 254 | public static final String EXTRA_VOLUME_STREAM_TYPE_ALIAS = |
| 255 | "android.media.EXTRA_VOLUME_STREAM_TYPE_ALIAS"; |
| 256 | |
| 257 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | * @hide The volume associated with the stream for the volume changed intent. |
| 259 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 260 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | public static final String EXTRA_VOLUME_STREAM_VALUE = |
| 262 | "android.media.EXTRA_VOLUME_STREAM_VALUE"; |
| 263 | |
Eric Laurent | 9ce379a | 2010-02-16 06:00:26 -0800 | [diff] [blame] | 264 | /** |
| 265 | * @hide The previous volume associated with the stream for the volume changed intent. |
| 266 | */ |
| 267 | public static final String EXTRA_PREV_VOLUME_STREAM_VALUE = |
| 268 | "android.media.EXTRA_PREV_VOLUME_STREAM_VALUE"; |
| 269 | |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 270 | /** |
John Spurlock | 8a52c44 | 2015-03-26 14:23:58 -0400 | [diff] [blame] | 271 | * @hide The devices associated with the stream for the stream devices changed intent. |
| 272 | */ |
| 273 | public static final String EXTRA_VOLUME_STREAM_DEVICES = |
| 274 | "android.media.EXTRA_VOLUME_STREAM_DEVICES"; |
| 275 | |
| 276 | /** |
| 277 | * @hide The previous devices associated with the stream for the stream devices changed intent. |
| 278 | */ |
| 279 | public static final String EXTRA_PREV_VOLUME_STREAM_DEVICES = |
| 280 | "android.media.EXTRA_PREV_VOLUME_STREAM_DEVICES"; |
| 281 | |
| 282 | /** |
Mike Lockwood | 0dc37cc | 2011-12-01 16:14:19 -0500 | [diff] [blame] | 283 | * @hide The new master volume mute state for the master mute changed intent. |
| 284 | * Value is boolean |
| 285 | */ |
| 286 | public static final String EXTRA_MASTER_VOLUME_MUTED = |
| 287 | "android.media.EXTRA_MASTER_VOLUME_MUTED"; |
| 288 | |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 289 | /** |
RoboErik | 7c82ced | 2014-12-04 17:39:08 -0800 | [diff] [blame] | 290 | * @hide The new stream volume mute state for the stream mute changed intent. |
| 291 | * Value is boolean |
| 292 | */ |
| 293 | public static final String EXTRA_STREAM_VOLUME_MUTED = |
| 294 | "android.media.EXTRA_STREAM_VOLUME_MUTED"; |
| 295 | |
| 296 | /** |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 297 | * Broadcast Action: Wired Headset plugged in or unplugged. |
| 298 | * |
| 299 | * You <em>cannot</em> receive this through components declared |
| 300 | * in manifests, only by explicitly registering for it with |
| 301 | * {@link Context#registerReceiver(BroadcastReceiver, IntentFilter) |
| 302 | * Context.registerReceiver()}. |
| 303 | * |
| 304 | * <p>The intent will have the following extra values: |
| 305 | * <ul> |
| 306 | * <li><em>state</em> - 0 for unplugged, 1 for plugged. </li> |
| 307 | * <li><em>name</em> - Headset type, human readable string </li> |
| 308 | * <li><em>microphone</em> - 1 if headset has a microphone, 0 otherwise </li> |
| 309 | * </ul> |
| 310 | * </ul> |
| 311 | */ |
| 312 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 313 | public static final String ACTION_HEADSET_PLUG = |
| 314 | "android.intent.action.HEADSET_PLUG"; |
| 315 | |
| 316 | /** |
Eemi Haukkala | bc68256 | 2015-03-06 23:03:30 +0200 | [diff] [blame] | 317 | * Broadcast Action: A sticky broadcast indicating an HDMI cable was plugged or unplugged. |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 318 | * |
| 319 | * The intent will have the following extra values: {@link #EXTRA_AUDIO_PLUG_STATE}, |
| 320 | * {@link #EXTRA_MAX_CHANNEL_COUNT}, {@link #EXTRA_ENCODINGS}. |
| 321 | * <p>It can only be received by explicitly registering for it with |
| 322 | * {@link Context#registerReceiver(BroadcastReceiver, IntentFilter)}. |
| 323 | */ |
| 324 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 325 | public static final String ACTION_HDMI_AUDIO_PLUG = |
| 326 | "android.media.action.HDMI_AUDIO_PLUG"; |
| 327 | |
| 328 | /** |
| 329 | * Extra used in {@link #ACTION_HDMI_AUDIO_PLUG} to communicate whether HDMI is plugged in |
| 330 | * or unplugged. |
| 331 | * An integer value of 1 indicates a plugged-in state, 0 is unplugged. |
| 332 | */ |
Jean-Michel Trivi | deddda5 | 2014-09-05 14:38:47 -0700 | [diff] [blame] | 333 | public static final String EXTRA_AUDIO_PLUG_STATE = "android.media.extra.AUDIO_PLUG_STATE"; |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 334 | |
| 335 | /** |
| 336 | * Extra used in {@link #ACTION_HDMI_AUDIO_PLUG} to define the maximum number of channels |
| 337 | * supported by the HDMI device. |
| 338 | * The corresponding integer value is only available when the device is plugged in (as expressed |
| 339 | * by {@link #EXTRA_AUDIO_PLUG_STATE}). |
| 340 | */ |
Jean-Michel Trivi | deddda5 | 2014-09-05 14:38:47 -0700 | [diff] [blame] | 341 | public static final String EXTRA_MAX_CHANNEL_COUNT = "android.media.extra.MAX_CHANNEL_COUNT"; |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 342 | |
| 343 | /** |
| 344 | * Extra used in {@link #ACTION_HDMI_AUDIO_PLUG} to define the audio encodings supported by |
| 345 | * the connected HDMI device. |
| 346 | * The corresponding array of encoding values is only available when the device is plugged in |
| 347 | * (as expressed by {@link #EXTRA_AUDIO_PLUG_STATE}). Encoding values are defined in |
| 348 | * {@link AudioFormat} (for instance see {@link AudioFormat#ENCODING_PCM_16BIT}). Use |
| 349 | * {@link android.content.Intent#getIntArrayExtra(String)} to retrieve the encoding values. |
| 350 | */ |
Jean-Michel Trivi | deddda5 | 2014-09-05 14:38:47 -0700 | [diff] [blame] | 351 | public static final String EXTRA_ENCODINGS = "android.media.extra.ENCODINGS"; |
Jean-Michel Trivi | c525843 | 2014-08-27 15:46:54 -0700 | [diff] [blame] | 352 | |
Jean-Michel Trivi | 46d5b82 | 2016-10-04 12:37:53 -0700 | [diff] [blame] | 353 | /** Used to identify the volume of audio streams for phone calls */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 354 | public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL; |
Jean-Michel Trivi | 46d5b82 | 2016-10-04 12:37:53 -0700 | [diff] [blame] | 355 | /** Used to identify the volume of audio streams for system sounds */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 | public static final int STREAM_SYSTEM = AudioSystem.STREAM_SYSTEM; |
Jean-Michel Trivi | 46d5b82 | 2016-10-04 12:37:53 -0700 | [diff] [blame] | 357 | /** Used to identify the volume of audio streams for the phone ring */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | public static final int STREAM_RING = AudioSystem.STREAM_RING; |
Jean-Michel Trivi | 46d5b82 | 2016-10-04 12:37:53 -0700 | [diff] [blame] | 359 | /** Used to identify the volume of audio streams for music playback */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | public static final int STREAM_MUSIC = AudioSystem.STREAM_MUSIC; |
Jean-Michel Trivi | 46d5b82 | 2016-10-04 12:37:53 -0700 | [diff] [blame] | 361 | /** Used to identify the volume of audio streams for alarms */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | public static final int STREAM_ALARM = AudioSystem.STREAM_ALARM; |
Jean-Michel Trivi | 46d5b82 | 2016-10-04 12:37:53 -0700 | [diff] [blame] | 363 | /** Used to identify the volume of audio streams for notifications */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | public static final int STREAM_NOTIFICATION = AudioSystem.STREAM_NOTIFICATION; |
Jean-Michel Trivi | 46d5b82 | 2016-10-04 12:37:53 -0700 | [diff] [blame] | 365 | /** @hide Used to identify the volume of audio streams for phone calls when connected |
| 366 | * to bluetooth */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 367 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | public static final int STREAM_BLUETOOTH_SCO = AudioSystem.STREAM_BLUETOOTH_SCO; |
Jean-Michel Trivi | 46d5b82 | 2016-10-04 12:37:53 -0700 | [diff] [blame] | 369 | /** @hide Used to identify the volume of audio streams for enforced system sounds |
| 370 | * in certain countries (e.g camera in Japan) */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 371 | @UnsupportedAppUsage |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 372 | public static final int STREAM_SYSTEM_ENFORCED = AudioSystem.STREAM_SYSTEM_ENFORCED; |
Jean-Michel Trivi | 46d5b82 | 2016-10-04 12:37:53 -0700 | [diff] [blame] | 373 | /** Used to identify the volume of audio streams for DTMF Tones */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 374 | public static final int STREAM_DTMF = AudioSystem.STREAM_DTMF; |
Jean-Michel Trivi | 46d5b82 | 2016-10-04 12:37:53 -0700 | [diff] [blame] | 375 | /** @hide Used to identify the volume of audio streams exclusively transmitted through the |
| 376 | * speaker (TTS) of the device */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 377 | @UnsupportedAppUsage |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 378 | public static final int STREAM_TTS = AudioSystem.STREAM_TTS; |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 379 | /** Used to identify the volume of audio streams for accessibility prompts */ |
| 380 | public static final int STREAM_ACCESSIBILITY = AudioSystem.STREAM_ACCESSIBILITY; |
Kim Baekgyeong | b64fac7 | 2019-12-09 10:35:58 +0000 | [diff] [blame] | 381 | /** @hide Used to identify the volume of audio streams for virtual assistant */ |
| 382 | @SystemApi |
| 383 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 384 | public static final int STREAM_ASSISTANT = AudioSystem.STREAM_ASSISTANT; |
Jean-Michel Trivi | 3f0945a | 2016-11-11 10:05:18 -0800 | [diff] [blame] | 385 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 386 | /** Number of audio streams */ |
| 387 | /** |
Jean-Michel Trivi | 46d5b82 | 2016-10-04 12:37:53 -0700 | [diff] [blame] | 388 | * @deprecated Do not iterate on volume stream type values. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 390 | @Deprecated public static final int NUM_STREAMS = AudioSystem.NUM_STREAMS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | |
Paul McLean | d6f87c8 | 2021-03-31 13:02:41 -0600 | [diff] [blame] | 392 | /** @hide */ |
| 393 | private static final int[] PUBLIC_STREAM_TYPES = { AudioManager.STREAM_VOICE_CALL, |
| 394 | AudioManager.STREAM_SYSTEM, AudioManager.STREAM_RING, AudioManager.STREAM_MUSIC, |
| 395 | AudioManager.STREAM_ALARM, AudioManager.STREAM_NOTIFICATION, |
| 396 | AudioManager.STREAM_DTMF, AudioManager.STREAM_ACCESSIBILITY }; |
| 397 | |
| 398 | /** @hide */ |
| 399 | @TestApi |
| 400 | public static final int[] getPublicStreamTypes() { |
| 401 | return PUBLIC_STREAM_TYPES; |
| 402 | } |
| 403 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | /** |
| 405 | * Increase the ringer volume. |
| 406 | * |
| 407 | * @see #adjustVolume(int, int) |
| 408 | * @see #adjustStreamVolume(int, int, int) |
| 409 | */ |
| 410 | public static final int ADJUST_RAISE = 1; |
| 411 | |
| 412 | /** |
| 413 | * Decrease the ringer volume. |
| 414 | * |
| 415 | * @see #adjustVolume(int, int) |
| 416 | * @see #adjustStreamVolume(int, int, int) |
| 417 | */ |
| 418 | public static final int ADJUST_LOWER = -1; |
| 419 | |
| 420 | /** |
| 421 | * Maintain the previous ringer volume. This may be useful when needing to |
| 422 | * show the volume toast without actually modifying the volume. |
| 423 | * |
| 424 | * @see #adjustVolume(int, int) |
| 425 | * @see #adjustStreamVolume(int, int, int) |
| 426 | */ |
| 427 | public static final int ADJUST_SAME = 0; |
| 428 | |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 429 | /** |
| 430 | * Mute the volume. Has no effect if the stream is already muted. |
| 431 | * |
| 432 | * @see #adjustVolume(int, int) |
| 433 | * @see #adjustStreamVolume(int, int, int) |
| 434 | */ |
| 435 | public static final int ADJUST_MUTE = -100; |
| 436 | |
| 437 | /** |
| 438 | * Unmute the volume. Has no effect if the stream is not muted. |
| 439 | * |
| 440 | * @see #adjustVolume(int, int) |
| 441 | * @see #adjustStreamVolume(int, int, int) |
| 442 | */ |
| 443 | public static final int ADJUST_UNMUTE = 100; |
| 444 | |
| 445 | /** |
| 446 | * Toggle the mute state. If muted the stream will be unmuted. If not muted |
| 447 | * the stream will be muted. |
| 448 | * |
| 449 | * @see #adjustVolume(int, int) |
| 450 | * @see #adjustStreamVolume(int, int, int) |
| 451 | */ |
| 452 | public static final int ADJUST_TOGGLE_MUTE = 101; |
| 453 | |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 454 | /** @hide */ |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 455 | @IntDef(flag = false, prefix = "ADJUST", value = { |
| 456 | ADJUST_RAISE, |
| 457 | ADJUST_LOWER, |
| 458 | ADJUST_SAME, |
| 459 | ADJUST_MUTE, |
| 460 | ADJUST_UNMUTE, |
| 461 | ADJUST_TOGGLE_MUTE } |
| 462 | ) |
| 463 | @Retention(RetentionPolicy.SOURCE) |
Jean-Michel Trivi | 1b926e7 | 2018-01-29 16:06:51 -0800 | [diff] [blame] | 464 | public @interface VolumeAdjustment {} |
Jean-Michel Trivi | 610aa9b | 2018-01-27 15:42:42 -0800 | [diff] [blame] | 465 | |
| 466 | /** @hide */ |
Jean-Michel Trivi | cf17036 | 2017-08-24 17:24:57 -0700 | [diff] [blame] | 467 | public static final String adjustToString(int adj) { |
| 468 | switch (adj) { |
| 469 | case ADJUST_RAISE: return "ADJUST_RAISE"; |
| 470 | case ADJUST_LOWER: return "ADJUST_LOWER"; |
| 471 | case ADJUST_SAME: return "ADJUST_SAME"; |
| 472 | case ADJUST_MUTE: return "ADJUST_MUTE"; |
| 473 | case ADJUST_UNMUTE: return "ADJUST_UNMUTE"; |
| 474 | case ADJUST_TOGGLE_MUTE: return "ADJUST_TOGGLE_MUTE"; |
| 475 | default: return new StringBuilder("unknown adjust mode ").append(adj).toString(); |
| 476 | } |
| 477 | } |
| 478 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | // Flags should be powers of 2! |
| 480 | |
| 481 | /** |
| 482 | * Show a toast containing the current volume. |
| 483 | * |
| 484 | * @see #adjustStreamVolume(int, int, int) |
| 485 | * @see #adjustVolume(int, int) |
| 486 | * @see #setStreamVolume(int, int, int) |
| 487 | * @see #setRingerMode(int) |
| 488 | */ |
| 489 | public static final int FLAG_SHOW_UI = 1 << 0; |
| 490 | |
| 491 | /** |
| 492 | * Whether to include ringer modes as possible options when changing volume. |
| 493 | * For example, if true and volume level is 0 and the volume is adjusted |
| 494 | * with {@link #ADJUST_LOWER}, then the ringer mode may switch the silent or |
| 495 | * vibrate mode. |
| 496 | * <p> |
| 497 | * By default this is on for the ring stream. If this flag is included, |
| 498 | * this behavior will be present regardless of the stream type being |
| 499 | * affected by the ringer mode. |
The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 500 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 501 | * @see #adjustVolume(int, int) |
| 502 | * @see #adjustStreamVolume(int, int, int) |
| 503 | */ |
| 504 | public static final int FLAG_ALLOW_RINGER_MODES = 1 << 1; |
| 505 | |
| 506 | /** |
| 507 | * Whether to play a sound when changing the volume. |
| 508 | * <p> |
| 509 | * If this is given to {@link #adjustVolume(int, int)} or |
| 510 | * {@link #adjustSuggestedStreamVolume(int, int, int)}, it may be ignored |
| 511 | * in some cases (for example, the decided stream type is not |
| 512 | * {@link AudioManager#STREAM_RING}, or the volume is being adjusted |
| 513 | * downward). |
| 514 | * |
| 515 | * @see #adjustStreamVolume(int, int, int) |
| 516 | * @see #adjustVolume(int, int) |
| 517 | * @see #setStreamVolume(int, int, int) |
| 518 | */ |
| 519 | public static final int FLAG_PLAY_SOUND = 1 << 2; |
| 520 | |
| 521 | /** |
| 522 | * Removes any sounds/vibrate that may be in the queue, or are playing (related to |
| 523 | * changing volume). |
| 524 | */ |
| 525 | public static final int FLAG_REMOVE_SOUND_AND_VIBRATE = 1 << 3; |
| 526 | |
| 527 | /** |
| 528 | * Whether to vibrate if going into the vibrate ringer mode. |
| 529 | */ |
| 530 | public static final int FLAG_VIBRATE = 1 << 4; |
| 531 | |
| 532 | /** |
Eric Laurent | 4bbcc65 | 2012-09-24 14:26:30 -0700 | [diff] [blame] | 533 | * Indicates to VolumePanel that the volume slider should be disabled as user |
| 534 | * cannot change the stream volume |
| 535 | * @hide |
| 536 | */ |
| 537 | public static final int FLAG_FIXED_VOLUME = 1 << 5; |
| 538 | |
| 539 | /** |
Matthew Xie | c9d1d5f | 2013-09-12 00:32:22 -0700 | [diff] [blame] | 540 | * Indicates the volume set/adjust call is for Bluetooth absolute volume |
| 541 | * @hide |
| 542 | */ |
| 543 | public static final int FLAG_BLUETOOTH_ABS_VOLUME = 1 << 6; |
| 544 | |
| 545 | /** |
John Spurlock | a11b4af | 2014-06-01 11:52:23 -0400 | [diff] [blame] | 546 | * Adjusting the volume was prevented due to silent mode, display a hint in the UI. |
| 547 | * @hide |
| 548 | */ |
| 549 | public static final int FLAG_SHOW_SILENT_HINT = 1 << 7; |
| 550 | |
| 551 | /** |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 552 | * Indicates the volume call is for Hdmi Cec system audio volume |
| 553 | * @hide |
| 554 | */ |
| 555 | public static final int FLAG_HDMI_SYSTEM_AUDIO_VOLUME = 1 << 8; |
| 556 | |
| 557 | /** |
RoboErik | 3c45c29 | 2014-07-08 16:47:31 -0700 | [diff] [blame] | 558 | * Indicates that this should only be handled if media is actively playing. |
| 559 | * @hide |
| 560 | */ |
| 561 | public static final int FLAG_ACTIVE_MEDIA_ONLY = 1 << 9; |
| 562 | |
| 563 | /** |
John Spurlock | 3513460 | 2014-07-24 18:10:48 -0400 | [diff] [blame] | 564 | * Like FLAG_SHOW_UI, but only dialog warnings and confirmations, no sliders. |
| 565 | * @hide |
| 566 | */ |
| 567 | public static final int FLAG_SHOW_UI_WARNINGS = 1 << 10; |
| 568 | |
| 569 | /** |
John Spurlock | 661f2cf4 | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 570 | * Adjusting the volume down from vibrated was prevented, display a hint in the UI. |
| 571 | * @hide |
| 572 | */ |
| 573 | public static final int FLAG_SHOW_VIBRATE_HINT = 1 << 11; |
| 574 | |
John Spurlock | b94f2d6 | 2015-03-17 14:11:57 -0400 | [diff] [blame] | 575 | /** |
| 576 | * Adjusting the volume due to a hardware key press. |
Hyundo Moon | ca0080d | 2018-12-26 16:16:55 +0900 | [diff] [blame] | 577 | * This flag can be used in the places in order to denote (or check) that a volume adjustment |
| 578 | * request is from a hardware key press. (e.g. {@link MediaController}). |
John Spurlock | b94f2d6 | 2015-03-17 14:11:57 -0400 | [diff] [blame] | 579 | * @hide |
| 580 | */ |
Jin Seok Park | 4abc23e | 2020-07-30 22:28:50 +0900 | [diff] [blame] | 581 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
Hyundo Moon | c3ce09e | 2019-03-11 20:00:00 +0900 | [diff] [blame] | 582 | public static final int FLAG_FROM_KEY = 1 << 12; |
John Spurlock | b94f2d6 | 2015-03-17 14:11:57 -0400 | [diff] [blame] | 583 | |
Jin Seok Park | 5ccb6cc | 2021-02-05 01:47:13 +0900 | [diff] [blame] | 584 | /** @hide */ |
Kriti Dang | 527e66c | 2021-03-04 10:37:22 +0100 | [diff] [blame] | 585 | @IntDef(prefix = {"ENCODED_SURROUND_OUTPUT_"}, value = { |
Kriti Dang | 98fdb26 | 2021-04-01 13:26:00 +0200 | [diff] [blame] | 586 | ENCODED_SURROUND_OUTPUT_UNKNOWN, |
Kriti Dang | 527e66c | 2021-03-04 10:37:22 +0100 | [diff] [blame] | 587 | ENCODED_SURROUND_OUTPUT_AUTO, |
| 588 | ENCODED_SURROUND_OUTPUT_NEVER, |
| 589 | ENCODED_SURROUND_OUTPUT_ALWAYS, |
| 590 | ENCODED_SURROUND_OUTPUT_MANUAL |
| 591 | }) |
| 592 | @Retention(RetentionPolicy.SOURCE) |
| 593 | public @interface EncodedSurroundOutputMode {} |
| 594 | |
| 595 | /** |
Kriti Dang | 98fdb26 | 2021-04-01 13:26:00 +0200 | [diff] [blame] | 596 | * The mode for surround sound formats is unknown. |
| 597 | */ |
| 598 | public static final int ENCODED_SURROUND_OUTPUT_UNKNOWN = -1; |
| 599 | |
| 600 | /** |
Kriti Dang | 527e66c | 2021-03-04 10:37:22 +0100 | [diff] [blame] | 601 | * The surround sound formats are available for use if they are detected. This is the default |
| 602 | * mode. |
| 603 | */ |
| 604 | public static final int ENCODED_SURROUND_OUTPUT_AUTO = 0; |
| 605 | |
| 606 | /** |
| 607 | * The surround sound formats are NEVER available, even if they are detected by the hardware. |
| 608 | * Those formats will not be reported. |
| 609 | */ |
| 610 | public static final int ENCODED_SURROUND_OUTPUT_NEVER = 1; |
| 611 | |
| 612 | /** |
| 613 | * The surround sound formats are ALWAYS available, even if they are not detected by the |
| 614 | * hardware. Those formats will be reported as part of the HDMI output capability. |
| 615 | * Applications are then free to use either PCM or encoded output. |
| 616 | */ |
| 617 | public static final int ENCODED_SURROUND_OUTPUT_ALWAYS = 2; |
| 618 | |
| 619 | /** |
| 620 | * Surround sound formats are available according to the choice of user, even if they are not |
| 621 | * detected by the hardware. Those formats will be reported as part of the HDMI output |
| 622 | * capability. Applications are then free to use either PCM or encoded output. |
| 623 | */ |
| 624 | public static final int ENCODED_SURROUND_OUTPUT_MANUAL = 3; |
| 625 | |
| 626 | /** @hide */ |
Jin Seok Park | fc9db6c | 2021-02-26 02:37:20 +0900 | [diff] [blame] | 627 | @IntDef(flag = true, prefix = "FLAG", value = { |
Jin Seok Park | 5ccb6cc | 2021-02-05 01:47:13 +0900 | [diff] [blame] | 628 | FLAG_SHOW_UI, |
| 629 | FLAG_ALLOW_RINGER_MODES, |
| 630 | FLAG_PLAY_SOUND, |
| 631 | FLAG_REMOVE_SOUND_AND_VIBRATE, |
| 632 | FLAG_VIBRATE, |
| 633 | FLAG_FIXED_VOLUME, |
| 634 | FLAG_BLUETOOTH_ABS_VOLUME, |
| 635 | FLAG_SHOW_SILENT_HINT, |
| 636 | FLAG_HDMI_SYSTEM_AUDIO_VOLUME, |
| 637 | FLAG_ACTIVE_MEDIA_ONLY, |
| 638 | FLAG_SHOW_UI_WARNINGS, |
| 639 | FLAG_SHOW_VIBRATE_HINT, |
| 640 | FLAG_FROM_KEY, |
| 641 | }) |
| 642 | @Retention(RetentionPolicy.SOURCE) |
| 643 | public @interface Flags {} |
| 644 | |
Hyundo Moon | ca0080d | 2018-12-26 16:16:55 +0900 | [diff] [blame] | 645 | // The iterator of TreeMap#entrySet() returns the entries in ascending key order. |
| 646 | private static final TreeMap<Integer, String> FLAG_NAMES = new TreeMap<>(); |
| 647 | |
| 648 | static { |
| 649 | FLAG_NAMES.put(FLAG_SHOW_UI, "FLAG_SHOW_UI"); |
| 650 | FLAG_NAMES.put(FLAG_ALLOW_RINGER_MODES, "FLAG_ALLOW_RINGER_MODES"); |
| 651 | FLAG_NAMES.put(FLAG_PLAY_SOUND, "FLAG_PLAY_SOUND"); |
| 652 | FLAG_NAMES.put(FLAG_REMOVE_SOUND_AND_VIBRATE, "FLAG_REMOVE_SOUND_AND_VIBRATE"); |
| 653 | FLAG_NAMES.put(FLAG_VIBRATE, "FLAG_VIBRATE"); |
| 654 | FLAG_NAMES.put(FLAG_FIXED_VOLUME, "FLAG_FIXED_VOLUME"); |
| 655 | FLAG_NAMES.put(FLAG_BLUETOOTH_ABS_VOLUME, "FLAG_BLUETOOTH_ABS_VOLUME"); |
| 656 | FLAG_NAMES.put(FLAG_SHOW_SILENT_HINT, "FLAG_SHOW_SILENT_HINT"); |
| 657 | FLAG_NAMES.put(FLAG_HDMI_SYSTEM_AUDIO_VOLUME, "FLAG_HDMI_SYSTEM_AUDIO_VOLUME"); |
| 658 | FLAG_NAMES.put(FLAG_ACTIVE_MEDIA_ONLY, "FLAG_ACTIVE_MEDIA_ONLY"); |
| 659 | FLAG_NAMES.put(FLAG_SHOW_UI_WARNINGS, "FLAG_SHOW_UI_WARNINGS"); |
| 660 | FLAG_NAMES.put(FLAG_SHOW_VIBRATE_HINT, "FLAG_SHOW_VIBRATE_HINT"); |
| 661 | FLAG_NAMES.put(FLAG_FROM_KEY, "FLAG_FROM_KEY"); |
| 662 | } |
John Spurlock | 661f2cf4 | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 663 | |
| 664 | /** @hide */ |
| 665 | public static String flagsToString(int flags) { |
| 666 | final StringBuilder sb = new StringBuilder(); |
Hyundo Moon | ca0080d | 2018-12-26 16:16:55 +0900 | [diff] [blame] | 667 | for (Map.Entry<Integer, String> entry : FLAG_NAMES.entrySet()) { |
| 668 | final int flag = entry.getKey(); |
John Spurlock | 661f2cf4 | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 669 | if ((flags & flag) != 0) { |
| 670 | if (sb.length() > 0) { |
| 671 | sb.append(','); |
| 672 | } |
Hyundo Moon | ca0080d | 2018-12-26 16:16:55 +0900 | [diff] [blame] | 673 | sb.append(entry.getValue()); |
John Spurlock | 661f2cf4 | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 674 | flags &= ~flag; |
| 675 | } |
| 676 | } |
| 677 | if (flags != 0) { |
| 678 | if (sb.length() > 0) { |
| 679 | sb.append(','); |
| 680 | } |
| 681 | sb.append(flags); |
| 682 | } |
| 683 | return sb.toString(); |
| 684 | } |
| 685 | |
| 686 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | * Ringer mode that will be silent and will not vibrate. (This overrides the |
| 688 | * vibrate setting.) |
| 689 | * |
| 690 | * @see #setRingerMode(int) |
| 691 | * @see #getRingerMode() |
| 692 | */ |
| 693 | public static final int RINGER_MODE_SILENT = 0; |
| 694 | |
| 695 | /** |
| 696 | * Ringer mode that will be silent and will vibrate. (This will cause the |
| 697 | * phone ringer to always vibrate, but the notification vibrate to only |
| 698 | * vibrate if set.) |
| 699 | * |
| 700 | * @see #setRingerMode(int) |
| 701 | * @see #getRingerMode() |
| 702 | */ |
| 703 | public static final int RINGER_MODE_VIBRATE = 1; |
| 704 | |
| 705 | /** |
| 706 | * Ringer mode that may be audible and may vibrate. It will be audible if |
| 707 | * the volume before changing out of this mode was audible. It will vibrate |
| 708 | * if the vibrate setting is on. |
| 709 | * |
| 710 | * @see #setRingerMode(int) |
| 711 | * @see #getRingerMode() |
| 712 | */ |
| 713 | public static final int RINGER_MODE_NORMAL = 2; |
| 714 | |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 715 | /** |
| 716 | * Maximum valid ringer mode value. Values must start from 0 and be contiguous. |
| 717 | * @hide |
| 718 | */ |
| 719 | public static final int RINGER_MODE_MAX = RINGER_MODE_NORMAL; |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 720 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 721 | /** |
| 722 | * Vibrate type that corresponds to the ringer. |
| 723 | * |
| 724 | * @see #setVibrateSetting(int, int) |
| 725 | * @see #getVibrateSetting(int) |
| 726 | * @see #shouldVibrate(int) |
Eric Laurent | cd1cd73 | 2012-05-01 11:23:07 -0700 | [diff] [blame] | 727 | * @deprecated Applications should maintain their own vibrate policy based on |
| 728 | * current ringer mode that can be queried via {@link #getRingerMode()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | */ |
| 730 | public static final int VIBRATE_TYPE_RINGER = 0; |
| 731 | |
| 732 | /** |
| 733 | * Vibrate type that corresponds to notifications. |
| 734 | * |
| 735 | * @see #setVibrateSetting(int, int) |
| 736 | * @see #getVibrateSetting(int) |
| 737 | * @see #shouldVibrate(int) |
Eric Laurent | cd1cd73 | 2012-05-01 11:23:07 -0700 | [diff] [blame] | 738 | * @deprecated Applications should maintain their own vibrate policy based on |
| 739 | * current ringer mode that can be queried via {@link #getRingerMode()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 740 | */ |
| 741 | public static final int VIBRATE_TYPE_NOTIFICATION = 1; |
| 742 | |
| 743 | /** |
| 744 | * Vibrate setting that suggests to never vibrate. |
| 745 | * |
| 746 | * @see #setVibrateSetting(int, int) |
| 747 | * @see #getVibrateSetting(int) |
Eric Laurent | cd1cd73 | 2012-05-01 11:23:07 -0700 | [diff] [blame] | 748 | * @deprecated Applications should maintain their own vibrate policy based on |
| 749 | * current ringer mode that can be queried via {@link #getRingerMode()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | */ |
| 751 | public static final int VIBRATE_SETTING_OFF = 0; |
| 752 | |
| 753 | /** |
| 754 | * Vibrate setting that suggests to vibrate when possible. |
| 755 | * |
| 756 | * @see #setVibrateSetting(int, int) |
| 757 | * @see #getVibrateSetting(int) |
Eric Laurent | cd1cd73 | 2012-05-01 11:23:07 -0700 | [diff] [blame] | 758 | * @deprecated Applications should maintain their own vibrate policy based on |
| 759 | * current ringer mode that can be queried via {@link #getRingerMode()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | */ |
| 761 | public static final int VIBRATE_SETTING_ON = 1; |
| 762 | |
| 763 | /** |
| 764 | * Vibrate setting that suggests to only vibrate when in the vibrate ringer |
| 765 | * mode. |
| 766 | * |
| 767 | * @see #setVibrateSetting(int, int) |
| 768 | * @see #getVibrateSetting(int) |
Eric Laurent | cd1cd73 | 2012-05-01 11:23:07 -0700 | [diff] [blame] | 769 | * @deprecated Applications should maintain their own vibrate policy based on |
| 770 | * current ringer mode that can be queried via {@link #getRingerMode()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 771 | */ |
| 772 | public static final int VIBRATE_SETTING_ONLY_SILENT = 2; |
| 773 | |
| 774 | /** |
| 775 | * Suggests using the default stream type. This may not be used in all |
| 776 | * places a stream type is needed. |
| 777 | */ |
| 778 | public static final int USE_DEFAULT_STREAM_TYPE = Integer.MIN_VALUE; |
| 779 | |
| 780 | private static IAudioService sService; |
| 781 | |
| 782 | /** |
| 783 | * @hide |
Jean-Michel Trivi | 9dc22c2 | 2017-01-05 18:06:03 -0800 | [diff] [blame] | 784 | * For test purposes only, will throw NPE with some methods that require a Context. |
| 785 | */ |
Mathew Inwood | 8e742f9 | 2020-10-27 11:47:29 +0000 | [diff] [blame] | 786 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Jean-Michel Trivi | 9dc22c2 | 2017-01-05 18:06:03 -0800 | [diff] [blame] | 787 | public AudioManager() { |
Jean-Michel Trivi | 9dc22c2 | 2017-01-05 18:06:03 -0800 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | /** |
| 791 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 792 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 793 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 794 | public AudioManager(Context context) { |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 795 | setContext(context); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | } |
| 797 | |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 798 | private Context getContext() { |
| 799 | if (mApplicationContext == null) { |
| 800 | setContext(mOriginalContext); |
| 801 | } |
| 802 | if (mApplicationContext != null) { |
| 803 | return mApplicationContext; |
| 804 | } |
| 805 | return mOriginalContext; |
| 806 | } |
| 807 | |
| 808 | private void setContext(Context context) { |
| 809 | mApplicationContext = context.getApplicationContext(); |
| 810 | if (mApplicationContext != null) { |
| 811 | mOriginalContext = null; |
| 812 | } else { |
| 813 | mOriginalContext = context; |
| 814 | } |
jiabin | 0f3339c | 2021-07-09 11:50:07 -0700 | [diff] [blame] | 815 | sContext = new WeakReference<>(context); |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 818 | @UnsupportedAppUsage |
Jean-Michel Trivi | e9dd14e | 2021-07-12 11:10:18 -0700 | [diff] [blame] | 819 | static IAudioService getService() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | { |
| 821 | if (sService != null) { |
| 822 | return sService; |
| 823 | } |
| 824 | IBinder b = ServiceManager.getService(Context.AUDIO_SERVICE); |
| 825 | sService = IAudioService.Stub.asInterface(b); |
| 826 | return sService; |
| 827 | } |
| 828 | |
| 829 | /** |
Jean-Michel Trivi | a83487e | 2013-09-17 21:19:30 -0700 | [diff] [blame] | 830 | * Sends a simulated key event for a media button. |
| 831 | * To simulate a key press, you must first send a KeyEvent built with a |
| 832 | * {@link KeyEvent#ACTION_DOWN} action, then another event with the {@link KeyEvent#ACTION_UP} |
| 833 | * action. |
| 834 | * <p>The key event will be sent to the current media key event consumer which registered with |
| 835 | * {@link AudioManager#registerMediaButtonEventReceiver(PendingIntent)}. |
| 836 | * @param keyEvent a {@link KeyEvent} instance whose key code is one of |
| 837 | * {@link KeyEvent#KEYCODE_MUTE}, |
| 838 | * {@link KeyEvent#KEYCODE_HEADSETHOOK}, |
| 839 | * {@link KeyEvent#KEYCODE_MEDIA_PLAY}, |
| 840 | * {@link KeyEvent#KEYCODE_MEDIA_PAUSE}, |
| 841 | * {@link KeyEvent#KEYCODE_MEDIA_PLAY_PAUSE}, |
| 842 | * {@link KeyEvent#KEYCODE_MEDIA_STOP}, |
| 843 | * {@link KeyEvent#KEYCODE_MEDIA_NEXT}, |
| 844 | * {@link KeyEvent#KEYCODE_MEDIA_PREVIOUS}, |
| 845 | * {@link KeyEvent#KEYCODE_MEDIA_REWIND}, |
| 846 | * {@link KeyEvent#KEYCODE_MEDIA_RECORD}, |
| 847 | * {@link KeyEvent#KEYCODE_MEDIA_FAST_FORWARD}, |
| 848 | * {@link KeyEvent#KEYCODE_MEDIA_CLOSE}, |
| 849 | * {@link KeyEvent#KEYCODE_MEDIA_EJECT}, |
| 850 | * or {@link KeyEvent#KEYCODE_MEDIA_AUDIO_TRACK}. |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 851 | */ |
Jean-Michel Trivi | a83487e | 2013-09-17 21:19:30 -0700 | [diff] [blame] | 852 | public void dispatchMediaKeyEvent(KeyEvent keyEvent) { |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 853 | MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext()); |
RoboErik | 430fc48 | 2014-06-12 15:49:20 -0700 | [diff] [blame] | 854 | helper.sendMediaButtonEvent(keyEvent, false); |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | /** |
| 858 | * @hide |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 859 | */ |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 860 | public void preDispatchKeyEvent(KeyEvent event, int stream) { |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 861 | /* |
| 862 | * If the user hits another key within the play sound delay, then |
| 863 | * cancel the sound |
| 864 | */ |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 865 | int keyCode = event.getKeyCode(); |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 866 | if (keyCode != KeyEvent.KEYCODE_VOLUME_DOWN && keyCode != KeyEvent.KEYCODE_VOLUME_UP |
| 867 | && keyCode != KeyEvent.KEYCODE_VOLUME_MUTE |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 868 | && mVolumeKeyUpTime + AudioSystem.PLAY_SOUND_DELAY > SystemClock.uptimeMillis()) { |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 869 | /* |
| 870 | * The user has hit another key during the delay (e.g., 300ms) |
| 871 | * since the last volume key up, so cancel any sounds. |
| 872 | */ |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 873 | adjustSuggestedStreamVolume(ADJUST_SAME, |
| 874 | stream, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE); |
Joe Onorato | 86f6786 | 2010-11-05 18:57:34 -0700 | [diff] [blame] | 875 | } |
| 876 | } |
| 877 | |
| 878 | /** |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 879 | * Indicates if the device implements a fixed volume policy. |
| 880 | * <p>Some devices may not have volume control and may operate at a fixed volume, |
| 881 | * and may not enable muting or changing the volume of audio streams. |
| 882 | * This method will return true on such devices. |
| 883 | * <p>The following APIs have no effect when volume is fixed: |
| 884 | * <ul> |
| 885 | * <li> {@link #adjustVolume(int, int)} |
| 886 | * <li> {@link #adjustSuggestedStreamVolume(int, int, int)} |
| 887 | * <li> {@link #adjustStreamVolume(int, int, int)} |
| 888 | * <li> {@link #setStreamVolume(int, int, int)} |
| 889 | * <li> {@link #setRingerMode(int)} |
| 890 | * <li> {@link #setStreamSolo(int, boolean)} |
| 891 | * <li> {@link #setStreamMute(int, boolean)} |
| 892 | * </ul> |
| 893 | */ |
| 894 | public boolean isVolumeFixed() { |
Jean-Michel Trivi | 547d263 | 2021-10-26 15:40:58 -0700 | [diff] [blame] | 895 | boolean res = false; |
| 896 | try { |
| 897 | res = getService().isVolumeFixed(); |
| 898 | } catch (RemoteException e) { |
| 899 | Log.e(TAG, "Error querying isVolumeFixed", e); |
Jean-Michel Trivi | a051308 | 2020-09-22 18:43:53 -0700 | [diff] [blame] | 900 | } |
Jean-Michel Trivi | 547d263 | 2021-10-26 15:40:58 -0700 | [diff] [blame] | 901 | return res; |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | * Adjusts the volume of a particular stream by one step in a direction. |
Jean-Michel Trivi | fccb25d | 2009-09-15 16:06:10 -0700 | [diff] [blame] | 906 | * <p> |
| 907 | * This method should only be used by applications that replace the platform-wide |
| 908 | * management of audio settings or the main telephony application. |
Jean-Michel Trivi | 5977362 | 2018-06-19 17:17:57 -0700 | [diff] [blame] | 909 | * <p>This method has no effect if the device implements a fixed volume policy |
| 910 | * as indicated by {@link #isVolumeFixed()}. |
| 911 | * <p>From N onward, ringer mode adjustments that would toggle Do Not Disturb are not allowed |
| 912 | * unless the app has been granted Do Not Disturb Access. |
| 913 | * See {@link NotificationManager#isNotificationPolicyAccessGranted()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 914 | * |
| 915 | * @param streamType The stream type to adjust. One of {@link #STREAM_VOICE_CALL}, |
Jean-Michel Trivi | 8e0bc78 | 2016-11-25 10:22:23 -0800 | [diff] [blame] | 916 | * {@link #STREAM_SYSTEM}, {@link #STREAM_RING}, {@link #STREAM_MUSIC}, |
| 917 | * {@link #STREAM_ALARM} or {@link #STREAM_ACCESSIBILITY}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 918 | * @param direction The direction to adjust the volume. One of |
| 919 | * {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or |
| 920 | * {@link #ADJUST_SAME}. |
| 921 | * @param flags One or more flags. |
| 922 | * @see #adjustVolume(int, int) |
| 923 | * @see #setStreamVolume(int, int, int) |
Jean-Michel Trivi | 5977362 | 2018-06-19 17:17:57 -0700 | [diff] [blame] | 924 | * @throws SecurityException if the adjustment triggers a Do Not Disturb change |
| 925 | * and the caller is not granted notification policy access. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 926 | */ |
| 927 | public void adjustStreamVolume(int streamType, int direction, int flags) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 928 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | try { |
John Wu | 4f7e510 | 2021-06-22 17:29:11 +0000 | [diff] [blame] | 930 | service.adjustStreamVolumeWithAttribution(streamType, direction, flags, |
| 931 | getContext().getOpPackageName(), getContext().getAttributionTag()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 933 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | } |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * Adjusts the volume of the most relevant stream. For example, if a call is |
| 939 | * active, it will have the highest priority regardless of if the in-call |
| 940 | * screen is showing. Another example, if music is playing in the background |
| 941 | * and a call is not active, the music stream will be adjusted. |
Jean-Michel Trivi | fccb25d | 2009-09-15 16:06:10 -0700 | [diff] [blame] | 942 | * <p> |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 943 | * This method should only be used by applications that replace the |
| 944 | * platform-wide management of audio settings or the main telephony |
| 945 | * application. |
| 946 | * <p> |
| 947 | * This method has no effect if the device implements a fixed volume policy |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 948 | * as indicated by {@link #isVolumeFixed()}. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 949 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 950 | * @param direction The direction to adjust the volume. One of |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 951 | * {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, |
| 952 | * {@link #ADJUST_SAME}, {@link #ADJUST_MUTE}, |
| 953 | * {@link #ADJUST_UNMUTE}, or {@link #ADJUST_TOGGLE_MUTE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | * @param flags One or more flags. |
| 955 | * @see #adjustSuggestedStreamVolume(int, int, int) |
| 956 | * @see #adjustStreamVolume(int, int, int) |
| 957 | * @see #setStreamVolume(int, int, int) |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 958 | * @see #isVolumeFixed() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 959 | */ |
| 960 | public void adjustVolume(int direction, int flags) { |
Marco Nelissen | 8dc5041 | 2015-04-28 09:42:54 -0700 | [diff] [blame] | 961 | MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext()); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 962 | helper.sendAdjustVolumeBy(USE_DEFAULT_STREAM_TYPE, direction, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | /** |
| 966 | * Adjusts the volume of the most relevant stream, or the given fallback |
| 967 | * stream. |
Jean-Michel Trivi | fccb25d | 2009-09-15 16:06:10 -0700 | [diff] [blame] | 968 | * <p> |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 969 | * This method should only be used by applications that replace the |
| 970 | * platform-wide management of audio settings or the main telephony |
| 971 | * application. |
| 972 | * <p> |
| 973 | * This method has no effect if the device implements a fixed volume policy |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 974 | * as indicated by {@link #isVolumeFixed()}. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 975 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | * @param direction The direction to adjust the volume. One of |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 977 | * {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, |
| 978 | * {@link #ADJUST_SAME}, {@link #ADJUST_MUTE}, |
| 979 | * {@link #ADJUST_UNMUTE}, or {@link #ADJUST_TOGGLE_MUTE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | * @param suggestedStreamType The stream type that will be used if there |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 981 | * isn't a relevant stream. {@link #USE_DEFAULT_STREAM_TYPE} is |
| 982 | * valid here. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | * @param flags One or more flags. |
| 984 | * @see #adjustVolume(int, int) |
| 985 | * @see #adjustStreamVolume(int, int, int) |
| 986 | * @see #setStreamVolume(int, int, int) |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 987 | * @see #isVolumeFixed() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | */ |
| 989 | public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags) { |
Marco Nelissen | 8dc5041 | 2015-04-28 09:42:54 -0700 | [diff] [blame] | 990 | MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext()); |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 991 | helper.sendAdjustVolumeBy(suggestedStreamType, direction, flags); |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 992 | } |
| 993 | |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 994 | /** @hide */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 995 | @UnsupportedAppUsage |
Jean-Michel Trivi | 582ccf6 | 2019-11-01 11:07:09 -0700 | [diff] [blame] | 996 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 997 | public void setMasterMute(boolean mute, int flags) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 998 | final IAudioService service = getService(); |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 999 | try { |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 1000 | service.setMasterMute(mute, flags, getContext().getOpPackageName(), |
John Wu | 4f7e510 | 2021-06-22 17:29:11 +0000 | [diff] [blame] | 1001 | UserHandle.getCallingUserId(), getContext().getAttributionTag()); |
Mike Lockwood | cbdb49d | 2011-10-20 12:54:05 -0400 | [diff] [blame] | 1002 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1003 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * Returns the current ringtone mode. |
| 1009 | * |
| 1010 | * @return The current ringtone mode, one of {@link #RINGER_MODE_NORMAL}, |
| 1011 | * {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}. |
| 1012 | * @see #setRingerMode(int) |
| 1013 | */ |
| 1014 | public int getRingerMode() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1015 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1016 | try { |
John Spurlock | 661f2cf4 | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 1017 | return service.getRingerModeExternal(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1018 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1019 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | /** |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 1024 | * Checks valid ringer mode values. |
| 1025 | * |
| 1026 | * @return true if the ringer mode indicated is valid, false otherwise. |
| 1027 | * |
| 1028 | * @see #setRingerMode(int) |
| 1029 | * @hide |
| 1030 | */ |
Mathew Inwood | 8e742f9 | 2020-10-27 11:47:29 +0000 | [diff] [blame] | 1031 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 1032 | public static boolean isValidRingerMode(int ringerMode) { |
| 1033 | if (ringerMode < 0 || ringerMode > RINGER_MODE_MAX) { |
| 1034 | return false; |
| 1035 | } |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1036 | final IAudioService service = getService(); |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1037 | try { |
| 1038 | return service.isValidRingerMode(ringerMode); |
| 1039 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1040 | throw e.rethrowFromSystemServer(); |
John Spurlock | 9755937 | 2014-10-24 16:27:36 -0400 | [diff] [blame] | 1041 | } |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1045 | * Returns the maximum volume index for a particular stream. |
| 1046 | * |
| 1047 | * @param streamType The stream type whose maximum volume index is returned. |
| 1048 | * @return The maximum valid volume index for the stream. |
| 1049 | * @see #getStreamVolume(int) |
| 1050 | */ |
| 1051 | public int getStreamMaxVolume(int streamType) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1052 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | try { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1054 | return service.getStreamMaxVolume(streamType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1056 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | /** |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1061 | * Returns the minimum volume index for a particular stream. |
Jean-Michel Trivi | 8e7aca3 | 2017-11-28 18:12:38 -0800 | [diff] [blame] | 1062 | * @param streamType The stream type whose minimum volume index is returned. Must be one of |
| 1063 | * {@link #STREAM_VOICE_CALL}, {@link #STREAM_SYSTEM}, |
| 1064 | * {@link #STREAM_RING}, {@link #STREAM_MUSIC}, {@link #STREAM_ALARM}, |
| 1065 | * {@link #STREAM_NOTIFICATION}, {@link #STREAM_DTMF} or {@link #STREAM_ACCESSIBILITY}. |
| 1066 | * @return The minimum valid volume index for the stream. |
| 1067 | * @see #getStreamVolume(int) |
| 1068 | */ |
| 1069 | public int getStreamMinVolume(int streamType) { |
| 1070 | if (!isPublicStreamType(streamType)) { |
| 1071 | throw new IllegalArgumentException("Invalid stream type " + streamType); |
| 1072 | } |
| 1073 | return getStreamMinVolumeInt(streamType); |
| 1074 | } |
| 1075 | |
| 1076 | /** |
| 1077 | * @hide |
| 1078 | * Same as {@link #getStreamMinVolume(int)} but without the check on the public stream type. |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1079 | * @param streamType The stream type whose minimum volume index is returned. |
| 1080 | * @return The minimum valid volume index for the stream. |
| 1081 | * @see #getStreamVolume(int) |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1082 | */ |
Paul McLean | d6f87c8 | 2021-03-31 13:02:41 -0600 | [diff] [blame] | 1083 | @TestApi |
Jean-Michel Trivi | 8e7aca3 | 2017-11-28 18:12:38 -0800 | [diff] [blame] | 1084 | public int getStreamMinVolumeInt(int streamType) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1085 | final IAudioService service = getService(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1086 | try { |
| 1087 | return service.getStreamMinVolume(streamType); |
| 1088 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1089 | throw e.rethrowFromSystemServer(); |
John Spurlock | b6e19e3 | 2015-03-10 21:33:44 -0400 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | * Returns the current volume index for a particular stream. |
| 1095 | * |
| 1096 | * @param streamType The stream type whose volume index is returned. |
| 1097 | * @return The current volume index for the stream. |
| 1098 | * @see #getStreamMaxVolume(int) |
| 1099 | * @see #setStreamVolume(int, int, int) |
| 1100 | */ |
| 1101 | public int getStreamVolume(int streamType) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1102 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1103 | try { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1104 | return service.getStreamVolume(streamType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1106 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | |
Jean-Michel Trivi | 8e7aca3 | 2017-11-28 18:12:38 -0800 | [diff] [blame] | 1110 | // keep in sync with frameworks/av/services/audiopolicy/common/include/Volume.h |
| 1111 | private static final float VOLUME_MIN_DB = -758.0f; |
| 1112 | |
| 1113 | /** @hide */ |
| 1114 | @IntDef(flag = false, prefix = "STREAM", value = { |
| 1115 | STREAM_VOICE_CALL, |
| 1116 | STREAM_SYSTEM, |
| 1117 | STREAM_RING, |
| 1118 | STREAM_MUSIC, |
| 1119 | STREAM_ALARM, |
| 1120 | STREAM_NOTIFICATION, |
| 1121 | STREAM_DTMF, |
| 1122 | STREAM_ACCESSIBILITY } |
| 1123 | ) |
| 1124 | @Retention(RetentionPolicy.SOURCE) |
| 1125 | public @interface PublicStreamTypes {} |
| 1126 | |
| 1127 | /** |
| 1128 | * Returns the volume in dB (decibel) for the given stream type at the given volume index, on |
| 1129 | * the given type of audio output device. |
| 1130 | * @param streamType stream type for which the volume is queried. |
| 1131 | * @param index the volume index for which the volume is queried. The index value must be |
| 1132 | * between the minimum and maximum index values for the given stream type (see |
| 1133 | * {@link #getStreamMinVolume(int)} and {@link #getStreamMaxVolume(int)}). |
| 1134 | * @param deviceType the type of audio output device for which volume is queried. |
| 1135 | * @return a volume expressed in dB. |
| 1136 | * A negative value indicates the audio signal is attenuated. A typical maximum value |
| 1137 | * at the maximum volume index is 0 dB (no attenuation nor amplification). Muting is |
| 1138 | * reflected by a value of {@link Float#NEGATIVE_INFINITY}. |
| 1139 | */ |
| 1140 | public float getStreamVolumeDb(@PublicStreamTypes int streamType, int index, |
| 1141 | @AudioDeviceInfo.AudioDeviceTypeOut int deviceType) { |
| 1142 | if (!isPublicStreamType(streamType)) { |
| 1143 | throw new IllegalArgumentException("Invalid stream type " + streamType); |
| 1144 | } |
| 1145 | if (index > getStreamMaxVolume(streamType) || index < getStreamMinVolume(streamType)) { |
| 1146 | throw new IllegalArgumentException("Invalid stream volume index " + index); |
| 1147 | } |
| 1148 | if (!AudioDeviceInfo.isValidAudioDeviceTypeOut(deviceType)) { |
| 1149 | throw new IllegalArgumentException("Invalid audio output device type " + deviceType); |
| 1150 | } |
| 1151 | final float gain = AudioSystem.getStreamVolumeDB(streamType, index, |
| 1152 | AudioDeviceInfo.convertDeviceTypeToInternalDevice(deviceType)); |
| 1153 | if (gain <= VOLUME_MIN_DB) { |
| 1154 | return Float.NEGATIVE_INFINITY; |
| 1155 | } else { |
| 1156 | return gain; |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | private static boolean isPublicStreamType(int streamType) { |
| 1161 | switch (streamType) { |
| 1162 | case STREAM_VOICE_CALL: |
| 1163 | case STREAM_SYSTEM: |
| 1164 | case STREAM_RING: |
| 1165 | case STREAM_MUSIC: |
| 1166 | case STREAM_ALARM: |
| 1167 | case STREAM_NOTIFICATION: |
| 1168 | case STREAM_DTMF: |
| 1169 | case STREAM_ACCESSIBILITY: |
| 1170 | return true; |
| 1171 | default: |
| 1172 | return false; |
| 1173 | } |
| 1174 | } |
| 1175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1176 | /** |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1177 | * Get last audible volume before stream was muted. |
| 1178 | * |
| 1179 | * @hide |
| 1180 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 1181 | @UnsupportedAppUsage |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1182 | public int getLastAudibleStreamVolume(int streamType) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1183 | final IAudioService service = getService(); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1184 | try { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1185 | return service.getLastAudibleStreamVolume(streamType); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1186 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1187 | throw e.rethrowFromSystemServer(); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
| 1191 | /** |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1192 | * Get the stream type whose volume is driving the UI sounds volume. |
| 1193 | * UI sounds are screen lock/unlock, camera shutter, key clicks... |
John Spurlock | 4f0f120 | 2014-08-05 13:28:33 -0400 | [diff] [blame] | 1194 | * It is assumed that this stream type is also tied to ringer mode changes. |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1195 | * @hide |
| 1196 | */ |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1197 | public int getUiSoundsStreamType() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1198 | final IAudioService service = getService(); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1199 | try { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1200 | return service.getUiSoundsStreamType(); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1201 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1202 | throw e.rethrowFromSystemServer(); |
Eric Laurent | 6d51766 | 2012-04-23 18:42:39 -0700 | [diff] [blame] | 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1207 | * Sets the ringer mode. |
| 1208 | * <p> |
| 1209 | * Silent mode will mute the volume and will not vibrate. Vibrate mode will |
| 1210 | * mute the volume and vibrate. Normal mode will be audible and may vibrate |
| 1211 | * according to user settings. |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 1212 | * <p>This method has no effect if the device implements a fixed volume policy |
| 1213 | * as indicated by {@link #isVolumeFixed()}. |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1214 | * * <p>From N onward, ringer mode adjustments that would toggle Do Not Disturb are not allowed |
| 1215 | * unless the app has been granted Do Not Disturb Access. |
| 1216 | * See {@link NotificationManager#isNotificationPolicyAccessGranted()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | * @param ringerMode The ringer mode, one of {@link #RINGER_MODE_NORMAL}, |
| 1218 | * {@link #RINGER_MODE_SILENT}, or {@link #RINGER_MODE_VIBRATE}. |
| 1219 | * @see #getRingerMode() |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 1220 | * @see #isVolumeFixed() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1221 | */ |
| 1222 | public void setRingerMode(int ringerMode) { |
Eric Laurent | 72668b2 | 2011-07-19 16:04:27 -0700 | [diff] [blame] | 1223 | if (!isValidRingerMode(ringerMode)) { |
| 1224 | return; |
| 1225 | } |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1226 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1227 | try { |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 1228 | service.setRingerModeExternal(ringerMode, getContext().getOpPackageName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1229 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1230 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /** |
| 1235 | * Sets the volume index for a particular stream. |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 1236 | * <p>This method has no effect if the device implements a fixed volume policy |
| 1237 | * as indicated by {@link #isVolumeFixed()}. |
Julia Reynolds | 48034f8 | 2016-03-09 10:15:16 -0500 | [diff] [blame] | 1238 | * <p>From N onward, volume adjustments that would toggle Do Not Disturb are not allowed unless |
| 1239 | * the app has been granted Do Not Disturb Access. |
| 1240 | * See {@link NotificationManager#isNotificationPolicyAccessGranted()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1241 | * @param streamType The stream whose volume index should be set. |
| 1242 | * @param index The volume index to set. See |
| 1243 | * {@link #getStreamMaxVolume(int)} for the largest valid value. |
| 1244 | * @param flags One or more flags. |
| 1245 | * @see #getStreamMaxVolume(int) |
| 1246 | * @see #getStreamVolume(int) |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 1247 | * @see #isVolumeFixed() |
Jean-Michel Trivi | 5977362 | 2018-06-19 17:17:57 -0700 | [diff] [blame] | 1248 | * @throws SecurityException if the volume change triggers a Do Not Disturb change |
| 1249 | * and the caller is not granted notification policy access. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | */ |
| 1251 | public void setStreamVolume(int streamType, int index, int flags) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1252 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1253 | try { |
John Wu | 4f7e510 | 2021-06-22 17:29:11 +0000 | [diff] [blame] | 1254 | service.setStreamVolumeWithAttribution(streamType, index, flags, |
| 1255 | getContext().getOpPackageName(), getContext().getAttributionTag()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1256 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1257 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | /** |
François Gaffie | 9c36210 | 2018-09-21 17:43:52 +0200 | [diff] [blame] | 1262 | * Sets the volume index for a particular {@link AudioAttributes}. |
| 1263 | * @param attr The {@link AudioAttributes} whose volume index should be set. |
| 1264 | * @param index The volume index to set. See |
| 1265 | * {@link #getMaxVolumeIndexForAttributes(AudioAttributes)} for the largest valid value |
| 1266 | * {@link #getMinVolumeIndexForAttributes(AudioAttributes)} for the lowest valid value. |
| 1267 | * @param flags One or more flags. |
| 1268 | * @see #getMaxVolumeIndexForAttributes(AudioAttributes) |
| 1269 | * @see #getMinVolumeIndexForAttributes(AudioAttributes) |
| 1270 | * @see #isVolumeFixed() |
| 1271 | * @hide |
| 1272 | */ |
| 1273 | @SystemApi |
| 1274 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1275 | public void setVolumeIndexForAttributes(@NonNull AudioAttributes attr, int index, int flags) { |
| 1276 | Preconditions.checkNotNull(attr, "attr must not be null"); |
| 1277 | final IAudioService service = getService(); |
| 1278 | try { |
| 1279 | service.setVolumeIndexForAttributes(attr, index, flags, |
John Wu | 4f7e510 | 2021-06-22 17:29:11 +0000 | [diff] [blame] | 1280 | getContext().getOpPackageName(), getContext().getAttributionTag()); |
François Gaffie | 9c36210 | 2018-09-21 17:43:52 +0200 | [diff] [blame] | 1281 | } catch (RemoteException e) { |
| 1282 | throw e.rethrowFromSystemServer(); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | /** |
| 1287 | * Returns the current volume index for a particular {@link AudioAttributes}. |
| 1288 | * |
| 1289 | * @param attr The {@link AudioAttributes} whose volume index is returned. |
| 1290 | * @return The current volume index for the stream. |
| 1291 | * @see #getMaxVolumeIndexForAttributes(AudioAttributes) |
| 1292 | * @see #getMinVolumeIndexForAttributes(AudioAttributes) |
| 1293 | * @see #setVolumeForAttributes(AudioAttributes, int, int) |
| 1294 | * @hide |
| 1295 | */ |
| 1296 | @SystemApi |
Hayden Gomes | 695f802 | 2019-04-11 10:44:18 -0700 | [diff] [blame] | 1297 | @IntRange(from = 0) |
François Gaffie | 9c36210 | 2018-09-21 17:43:52 +0200 | [diff] [blame] | 1298 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1299 | public int getVolumeIndexForAttributes(@NonNull AudioAttributes attr) { |
| 1300 | Preconditions.checkNotNull(attr, "attr must not be null"); |
| 1301 | final IAudioService service = getService(); |
| 1302 | try { |
| 1303 | return service.getVolumeIndexForAttributes(attr); |
| 1304 | } catch (RemoteException e) { |
| 1305 | throw e.rethrowFromSystemServer(); |
| 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | /** |
| 1310 | * Returns the maximum volume index for a particular {@link AudioAttributes}. |
| 1311 | * |
| 1312 | * @param attr The {@link AudioAttributes} whose maximum volume index is returned. |
| 1313 | * @return The maximum valid volume index for the {@link AudioAttributes}. |
| 1314 | * @see #getVolumeIndexForAttributes(AudioAttributes) |
| 1315 | * @hide |
| 1316 | */ |
| 1317 | @SystemApi |
Hayden Gomes | 695f802 | 2019-04-11 10:44:18 -0700 | [diff] [blame] | 1318 | @IntRange(from = 0) |
François Gaffie | 9c36210 | 2018-09-21 17:43:52 +0200 | [diff] [blame] | 1319 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1320 | public int getMaxVolumeIndexForAttributes(@NonNull AudioAttributes attr) { |
| 1321 | Preconditions.checkNotNull(attr, "attr must not be null"); |
| 1322 | final IAudioService service = getService(); |
| 1323 | try { |
| 1324 | return service.getMaxVolumeIndexForAttributes(attr); |
| 1325 | } catch (RemoteException e) { |
| 1326 | throw e.rethrowFromSystemServer(); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | /** |
| 1331 | * Returns the minimum volume index for a particular {@link AudioAttributes}. |
| 1332 | * |
| 1333 | * @param attr The {@link AudioAttributes} whose minimum volume index is returned. |
| 1334 | * @return The minimum valid volume index for the {@link AudioAttributes}. |
| 1335 | * @see #getVolumeIndexForAttributes(AudioAttributes) |
| 1336 | * @hide |
| 1337 | */ |
| 1338 | @SystemApi |
Hayden Gomes | 695f802 | 2019-04-11 10:44:18 -0700 | [diff] [blame] | 1339 | @IntRange(from = 0) |
François Gaffie | 9c36210 | 2018-09-21 17:43:52 +0200 | [diff] [blame] | 1340 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1341 | public int getMinVolumeIndexForAttributes(@NonNull AudioAttributes attr) { |
| 1342 | Preconditions.checkNotNull(attr, "attr must not be null"); |
| 1343 | final IAudioService service = getService(); |
| 1344 | try { |
| 1345 | return service.getMinVolumeIndexForAttributes(attr); |
| 1346 | } catch (RemoteException e) { |
| 1347 | throw e.rethrowFromSystemServer(); |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | /** |
Hayden Gomes | 62812aa | 2019-12-23 11:40:27 -0800 | [diff] [blame] | 1352 | * Set the system usages to be supported on this device. |
| 1353 | * @param systemUsages array of system usages to support {@link AttributeSystemUsage} |
| 1354 | * @hide |
| 1355 | */ |
| 1356 | @SystemApi |
| 1357 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1358 | public void setSupportedSystemUsages(@NonNull @AttributeSystemUsage int[] systemUsages) { |
| 1359 | Objects.requireNonNull(systemUsages, "systemUsages must not be null"); |
| 1360 | final IAudioService service = getService(); |
| 1361 | try { |
| 1362 | service.setSupportedSystemUsages(systemUsages); |
| 1363 | } catch (RemoteException e) { |
| 1364 | throw e.rethrowFromSystemServer(); |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | /** |
| 1369 | * Get the system usages supported on this device. |
| 1370 | * @return array of supported system usages {@link AttributeSystemUsage} |
| 1371 | * @hide |
| 1372 | */ |
| 1373 | @SystemApi |
| 1374 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1375 | public @NonNull @AttributeSystemUsage int[] getSupportedSystemUsages() { |
| 1376 | final IAudioService service = getService(); |
| 1377 | try { |
| 1378 | return service.getSupportedSystemUsages(); |
| 1379 | } catch (RemoteException e) { |
| 1380 | throw e.rethrowFromSystemServer(); |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | /** |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1385 | * Solo or unsolo a particular stream. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1386 | * <p> |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1387 | * Do not use. This method has been deprecated and is now a no-op. |
| 1388 | * {@link #requestAudioFocus} should be used for exclusive audio playback. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | * |
| 1390 | * @param streamType The stream to be soloed/unsoloed. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1391 | * @param state The required solo state: true for solo ON, false for solo |
| 1392 | * OFF |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 1393 | * @see #isVolumeFixed() |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1394 | * @deprecated Do not use. If you need exclusive audio playback use |
| 1395 | * {@link #requestAudioFocus}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1396 | */ |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1397 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | public void setStreamSolo(int streamType, boolean state) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1399 | Log.w(TAG, "setStreamSolo has been deprecated. Do not use."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | /** |
| 1403 | * Mute or unmute an audio stream. |
| 1404 | * <p> |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1405 | * This method should only be used by applications that replace the |
| 1406 | * platform-wide management of audio settings or the main telephony |
| 1407 | * application. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1408 | * <p> |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1409 | * This method has no effect if the device implements a fixed volume policy |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 1410 | * as indicated by {@link #isVolumeFixed()}. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1411 | * <p> |
| 1412 | * This method was deprecated in API level 22. Prior to API level 22 this |
| 1413 | * method had significantly different behavior and should be used carefully. |
| 1414 | * The following applies only to pre-22 platforms: |
| 1415 | * <ul> |
| 1416 | * <li>The mute command is protected against client process death: if a |
| 1417 | * process with an active mute request on a stream dies, this stream will be |
| 1418 | * unmuted automatically.</li> |
| 1419 | * <li>The mute requests for a given stream are cumulative: the AudioManager |
| 1420 | * can receive several mute requests from one or more clients and the stream |
| 1421 | * will be unmuted only when the same number of unmute requests are |
| 1422 | * received.</li> |
| 1423 | * <li>For a better user experience, applications MUST unmute a muted stream |
| 1424 | * in onPause() and mute is again in onResume() if appropriate.</li> |
| 1425 | * </ul> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1426 | * |
| 1427 | * @param streamType The stream to be muted/unmuted. |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1428 | * @param state The required mute state: true for mute ON, false for mute |
| 1429 | * OFF |
Eric Laurent | ba207e7 | 2014-05-15 17:08:16 -0700 | [diff] [blame] | 1430 | * @see #isVolumeFixed() |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1431 | * @deprecated Use {@link #adjustStreamVolume(int, int, int)} with |
| 1432 | * {@link #ADJUST_MUTE} or {@link #ADJUST_UNMUTE} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1433 | */ |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1434 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1435 | public void setStreamMute(int streamType, boolean state) { |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1436 | Log.w(TAG, "setStreamMute is deprecated. adjustStreamVolume should be used instead."); |
| 1437 | int direction = state ? ADJUST_MUTE : ADJUST_UNMUTE; |
| 1438 | if (streamType == AudioManager.USE_DEFAULT_STREAM_TYPE) { |
| 1439 | adjustSuggestedStreamVolume(direction, streamType, 0); |
| 1440 | } else { |
| 1441 | adjustStreamVolume(streamType, direction, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | /** |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1446 | * Returns the current mute state for a particular stream. |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1447 | * |
RoboErik | 4197cb6 | 2015-01-21 15:45:32 -0800 | [diff] [blame] | 1448 | * @param streamType The stream to get mute state for. |
| 1449 | * @return The mute state for the given stream. |
| 1450 | * @see #adjustStreamVolume(int, int, int) |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1451 | */ |
| 1452 | public boolean isStreamMute(int streamType) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1453 | final IAudioService service = getService(); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1454 | try { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 1455 | return service.isStreamMute(streamType); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1456 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1457 | throw e.rethrowFromSystemServer(); |
Eric Laurent | 25101b0 | 2011-02-02 09:33:30 -0800 | [diff] [blame] | 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | /** |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1462 | * get master mute state. |
| 1463 | * |
| 1464 | * @hide |
| 1465 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 1466 | @UnsupportedAppUsage |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1467 | public boolean isMasterMute() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1468 | final IAudioService service = getService(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1469 | try { |
| 1470 | return service.isMasterMute(); |
| 1471 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1472 | throw e.rethrowFromSystemServer(); |
Mike Lockwood | ce952c8 | 2011-11-14 10:47:42 -0800 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | /** |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1477 | * forces the stream controlled by hard volume keys |
| 1478 | * specifying streamType == -1 releases control to the |
| 1479 | * logic. |
| 1480 | * |
| 1481 | * @hide |
| 1482 | */ |
Jean-Michel Trivi | 60eddfd | 2018-03-09 15:31:12 -0800 | [diff] [blame] | 1483 | @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 1484 | @UnsupportedAppUsage |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1485 | public void forceVolumeControlStream(int streamType) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1486 | final IAudioService service = getService(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1487 | try { |
| 1488 | service.forceVolumeControlStream(streamType, mICallBack); |
| 1489 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1490 | throw e.rethrowFromSystemServer(); |
Eric Laurent | 45c90ce | 2012-04-24 18:44:22 -0700 | [diff] [blame] | 1491 | } |
Eric Laurent | 402f7f2 | 2011-02-04 12:30:32 -0800 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | * Returns whether a particular type should vibrate according to user |
| 1496 | * settings and the current ringer mode. |
| 1497 | * <p> |
| 1498 | * This shouldn't be needed by most clients that use notifications to |
| 1499 | * vibrate. The notification manager will not vibrate if the policy doesn't |
| 1500 | * allow it, so the client should always set a vibrate pattern and let the |
| 1501 | * notification manager control whether or not to actually vibrate. |
| 1502 | * |
| 1503 | * @param vibrateType The type of vibrate. One of |
| 1504 | * {@link #VIBRATE_TYPE_NOTIFICATION} or |
| 1505 | * {@link #VIBRATE_TYPE_RINGER}. |
| 1506 | * @return Whether the type should vibrate at the instant this method is |
| 1507 | * called. |
| 1508 | * @see #setVibrateSetting(int, int) |
| 1509 | * @see #getVibrateSetting(int) |
Eric Laurent | cd1cd73 | 2012-05-01 11:23:07 -0700 | [diff] [blame] | 1510 | * @deprecated Applications should maintain their own vibrate policy based on |
| 1511 | * current ringer mode that can be queried via {@link #getRingerMode()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1512 | */ |
| 1513 | public boolean shouldVibrate(int vibrateType) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1514 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1515 | try { |
| 1516 | return service.shouldVibrate(vibrateType); |
| 1517 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1518 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | /** |
| 1523 | * Returns whether the user's vibrate setting for a vibrate type. |
| 1524 | * <p> |
| 1525 | * This shouldn't be needed by most clients that want to vibrate, instead |
| 1526 | * see {@link #shouldVibrate(int)}. |
| 1527 | * |
| 1528 | * @param vibrateType The type of vibrate. One of |
| 1529 | * {@link #VIBRATE_TYPE_NOTIFICATION} or |
| 1530 | * {@link #VIBRATE_TYPE_RINGER}. |
| 1531 | * @return The vibrate setting, one of {@link #VIBRATE_SETTING_ON}, |
| 1532 | * {@link #VIBRATE_SETTING_OFF}, or |
| 1533 | * {@link #VIBRATE_SETTING_ONLY_SILENT}. |
| 1534 | * @see #setVibrateSetting(int, int) |
| 1535 | * @see #shouldVibrate(int) |
Eric Laurent | cd1cd73 | 2012-05-01 11:23:07 -0700 | [diff] [blame] | 1536 | * @deprecated Applications should maintain their own vibrate policy based on |
| 1537 | * current ringer mode that can be queried via {@link #getRingerMode()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1538 | */ |
| 1539 | public int getVibrateSetting(int vibrateType) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1540 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1541 | try { |
| 1542 | return service.getVibrateSetting(vibrateType); |
| 1543 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1544 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | /** |
| 1549 | * Sets the setting for when the vibrate type should vibrate. |
Jean-Michel Trivi | fccb25d | 2009-09-15 16:06:10 -0700 | [diff] [blame] | 1550 | * <p> |
| 1551 | * This method should only be used by applications that replace the platform-wide |
| 1552 | * management of audio settings or the main telephony application. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1553 | * |
| 1554 | * @param vibrateType The type of vibrate. One of |
| 1555 | * {@link #VIBRATE_TYPE_NOTIFICATION} or |
| 1556 | * {@link #VIBRATE_TYPE_RINGER}. |
| 1557 | * @param vibrateSetting The vibrate setting, one of |
| 1558 | * {@link #VIBRATE_SETTING_ON}, |
| 1559 | * {@link #VIBRATE_SETTING_OFF}, or |
| 1560 | * {@link #VIBRATE_SETTING_ONLY_SILENT}. |
| 1561 | * @see #getVibrateSetting(int) |
| 1562 | * @see #shouldVibrate(int) |
Eric Laurent | cd1cd73 | 2012-05-01 11:23:07 -0700 | [diff] [blame] | 1563 | * @deprecated Applications should maintain their own vibrate policy based on |
| 1564 | * current ringer mode that can be queried via {@link #getRingerMode()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1565 | */ |
| 1566 | public void setVibrateSetting(int vibrateType, int vibrateSetting) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1567 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1568 | try { |
| 1569 | service.setVibrateSetting(vibrateType, vibrateSetting); |
| 1570 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1571 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | /** |
| 1576 | * Sets the speakerphone on or off. |
Jean-Michel Trivi | fccb25d | 2009-09-15 16:06:10 -0700 | [diff] [blame] | 1577 | * <p> |
| 1578 | * This method should only be used by applications that replace the platform-wide |
| 1579 | * management of audio settings or the main telephony application. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1580 | * |
| 1581 | * @param on set <var>true</var> to turn on speakerphone; |
| 1582 | * <var>false</var> to turn it off |
| 1583 | */ |
| 1584 | public void setSpeakerphoneOn(boolean on){ |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1585 | final IAudioService service = getService(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 1586 | try { |
Eric Laurent | 3aad0ad | 2020-05-14 12:45:18 -0700 | [diff] [blame] | 1587 | service.setSpeakerphoneOn(mICallBack, on); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 1588 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1589 | throw e.rethrowFromSystemServer(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1590 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | /** |
| 1594 | * Checks whether the speakerphone is on or off. |
| 1595 | * |
| 1596 | * @return true if speakerphone is on, false if it's off |
| 1597 | */ |
| 1598 | public boolean isSpeakerphoneOn() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 1599 | final IAudioService service = getService(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 1600 | try { |
| 1601 | return service.isSpeakerphoneOn(); |
| 1602 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 1603 | throw e.rethrowFromSystemServer(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 1604 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1605 | } |
| 1606 | |
Kevin Rocard | 7919c2a | 2019-03-21 10:28:01 -0700 | [diff] [blame] | 1607 | /** |
Kevin Rocard | db0ff41 | 2019-05-02 17:55:35 -0700 | [diff] [blame] | 1608 | * Specifies whether the audio played by this app may or may not be captured by other apps or |
Kevin Rocard | 654b3cb | 2019-04-03 16:51:11 -0700 | [diff] [blame] | 1609 | * the system. |
Kevin Rocard | 7919c2a | 2019-03-21 10:28:01 -0700 | [diff] [blame] | 1610 | * |
| 1611 | * The default is {@link AudioAttributes#ALLOW_CAPTURE_BY_ALL}. |
| 1612 | * |
Kevin Rocard | 654b3cb | 2019-04-03 16:51:11 -0700 | [diff] [blame] | 1613 | * There are multiple ways to set this policy: |
Kevin Rocard | db0ff41 | 2019-05-02 17:55:35 -0700 | [diff] [blame] | 1614 | * <ul> |
| 1615 | * <li> for each track independently, see |
| 1616 | * {@link AudioAttributes.Builder#setAllowedCapturePolicy(int)} </li> |
| 1617 | * <li> application-wide at runtime, with this method </li> |
| 1618 | * <li> application-wide at build time, see {@code allowAudioPlaybackCapture} in the application |
| 1619 | * manifest. </li> |
| 1620 | * </ul> |
Kevin Rocard | 654b3cb | 2019-04-03 16:51:11 -0700 | [diff] [blame] | 1621 | * The most restrictive policy is always applied. |
| 1622 | * |
Kevin Rocard | db0ff41 | 2019-05-02 17:55:35 -0700 | [diff] [blame] | 1623 | * See {@link AudioPlaybackCaptureConfiguration} for more details on |
Kevin Rocard | 654b3cb | 2019-04-03 16:51:11 -0700 | [diff] [blame] | 1624 | * which audio signals can be captured. |
Kevin Rocard | 7919c2a | 2019-03-21 10:28:01 -0700 | [diff] [blame] | 1625 | * |
| 1626 | * @param capturePolicy one of |
| 1627 | * {@link AudioAttributes#ALLOW_CAPTURE_BY_ALL}, |
| 1628 | * {@link AudioAttributes#ALLOW_CAPTURE_BY_SYSTEM}, |
| 1629 | * {@link AudioAttributes#ALLOW_CAPTURE_BY_NONE}. |
jiabin | b33f369 | 2019-12-23 13:09:58 -0800 | [diff] [blame] | 1630 | * @throws RuntimeException if the argument is not a valid value. |
Kevin Rocard | 7919c2a | 2019-03-21 10:28:01 -0700 | [diff] [blame] | 1631 | */ |
| 1632 | public void setAllowedCapturePolicy(@AudioAttributes.CapturePolicy int capturePolicy) { |
Kevin Rocard | 7919c2a | 2019-03-21 10:28:01 -0700 | [diff] [blame] | 1633 | // TODO: also pass the package in case multiple packages have the same UID |
jiabin | b33f369 | 2019-12-23 13:09:58 -0800 | [diff] [blame] | 1634 | final IAudioService service = getService(); |
| 1635 | try { |
| 1636 | int result = service.setAllowedCapturePolicy(capturePolicy); |
| 1637 | if (result != AudioSystem.AUDIO_STATUS_OK) { |
| 1638 | Log.e(TAG, "Could not setAllowedCapturePolicy: " + result); |
| 1639 | return; |
| 1640 | } |
| 1641 | } catch (RemoteException e) { |
| 1642 | throw e.rethrowFromSystemServer(); |
Kevin Rocard | 7919c2a | 2019-03-21 10:28:01 -0700 | [diff] [blame] | 1643 | } |
Kevin Rocard | 019f60d | 2019-04-09 16:25:26 -0700 | [diff] [blame] | 1644 | } |
| 1645 | |
Kevin Rocard | 019f60d | 2019-04-09 16:25:26 -0700 | [diff] [blame] | 1646 | /** |
| 1647 | * Return the capture policy. |
| 1648 | * @return the capture policy set by {@link #setAllowedCapturePolicy(int)} or |
| 1649 | * the default if it was not called. |
| 1650 | */ |
| 1651 | @AudioAttributes.CapturePolicy |
| 1652 | public int getAllowedCapturePolicy() { |
jiabin | b33f369 | 2019-12-23 13:09:58 -0800 | [diff] [blame] | 1653 | int result = AudioAttributes.ALLOW_CAPTURE_BY_ALL; |
| 1654 | try { |
| 1655 | result = getService().getAllowedCapturePolicy(); |
| 1656 | } catch (RemoteException e) { |
| 1657 | Log.e(TAG, "Failed to query allowed capture policy: " + e); |
| 1658 | } |
| 1659 | return result; |
Kevin Rocard | 7919c2a | 2019-03-21 10:28:01 -0700 | [diff] [blame] | 1660 | } |
| 1661 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 1662 | //==================================================================== |
Jean-Michel Trivi | 21cd541 | 2019-10-30 08:16:51 -0700 | [diff] [blame] | 1663 | // Audio Product Strategy routing |
| 1664 | |
| 1665 | /** |
| 1666 | * @hide |
| 1667 | * Set the preferred device for a given strategy, i.e. the audio routing to be used by |
| 1668 | * this audio strategy. Note that the device may not be available at the time the preferred |
| 1669 | * device is set, but it will be used once made available. |
| 1670 | * <p>Use {@link #removePreferredDeviceForStrategy(AudioProductStrategy)} to cancel setting |
| 1671 | * this preference for this strategy.</p> |
| 1672 | * @param strategy the audio strategy whose routing will be affected |
| 1673 | * @param device the audio device to route to when available |
| 1674 | * @return true if the operation was successful, false otherwise |
| 1675 | */ |
| 1676 | @SystemApi |
| 1677 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1678 | public boolean setPreferredDeviceForStrategy(@NonNull AudioProductStrategy strategy, |
Jean-Michel Trivi | 0e87bca | 2020-02-18 14:30:29 -0800 | [diff] [blame] | 1679 | @NonNull AudioDeviceAttributes device) { |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1680 | return setPreferredDevicesForStrategy(strategy, Arrays.asList(device)); |
Jean-Michel Trivi | 21cd541 | 2019-10-30 08:16:51 -0700 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /** |
| 1684 | * @hide |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1685 | * Removes the preferred audio device(s) previously set with |
| 1686 | * {@link #setPreferredDeviceForStrategy(AudioProductStrategy, AudioDeviceAttributes)} or |
| 1687 | * {@link #setPreferredDevicesForStrategy(AudioProductStrategy, List<AudioDeviceAttributes>)}. |
Jean-Michel Trivi | 21cd541 | 2019-10-30 08:16:51 -0700 | [diff] [blame] | 1688 | * @param strategy the audio strategy whose routing will be affected |
| 1689 | * @return true if the operation was successful, false otherwise (invalid strategy, or no |
| 1690 | * device set for example) |
| 1691 | */ |
| 1692 | @SystemApi |
| 1693 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1694 | public boolean removePreferredDeviceForStrategy(@NonNull AudioProductStrategy strategy) { |
| 1695 | Objects.requireNonNull(strategy); |
| 1696 | try { |
| 1697 | final int status = |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1698 | getService().removePreferredDevicesForStrategy(strategy.getId()); |
Jean-Michel Trivi | 21cd541 | 2019-10-30 08:16:51 -0700 | [diff] [blame] | 1699 | return status == AudioSystem.SUCCESS; |
| 1700 | } catch (RemoteException e) { |
| 1701 | throw e.rethrowFromSystemServer(); |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | /** |
| 1706 | * @hide |
| 1707 | * Return the preferred device for an audio strategy, previously set with |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1708 | * {@link #setPreferredDeviceForStrategy(AudioProductStrategy, AudioDeviceAttributes)} or |
| 1709 | * {@link #setPreferredDevicesForStrategy(AudioProductStrategy, List<AudioDeviceAttributes>)} |
| 1710 | * @param strategy the strategy to query |
| 1711 | * @return the preferred device for that strategy, if multiple devices are set as preferred |
| 1712 | * devices, the first one in the list will be returned. Null will be returned if none was |
| 1713 | * ever set or if the strategy is invalid |
| 1714 | */ |
| 1715 | @SystemApi |
| 1716 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1717 | @Nullable |
| 1718 | public AudioDeviceAttributes getPreferredDeviceForStrategy( |
| 1719 | @NonNull AudioProductStrategy strategy) { |
| 1720 | List<AudioDeviceAttributes> devices = getPreferredDevicesForStrategy(strategy); |
| 1721 | return devices.isEmpty() ? null : devices.get(0); |
| 1722 | } |
| 1723 | |
| 1724 | /** |
| 1725 | * @hide |
| 1726 | * Set the preferred devices for a given strategy, i.e. the audio routing to be used by |
| 1727 | * this audio strategy. Note that the devices may not be available at the time the preferred |
| 1728 | * devices is set, but it will be used once made available. |
| 1729 | * <p>Use {@link #removePreferredDeviceForStrategy(AudioProductStrategy)} to cancel setting |
| 1730 | * this preference for this strategy.</p> |
| 1731 | * Note that the list of devices is not a list ranked by preference, but a list of one or more |
| 1732 | * devices used simultaneously to output the same audio signal. |
| 1733 | * @param strategy the audio strategy whose routing will be affected |
| 1734 | * @param devices a non-empty list of the audio devices to route to when available |
| 1735 | * @return true if the operation was successful, false otherwise |
| 1736 | */ |
| 1737 | @SystemApi |
| 1738 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1739 | public boolean setPreferredDevicesForStrategy(@NonNull AudioProductStrategy strategy, |
| 1740 | @NonNull List<AudioDeviceAttributes> devices) { |
| 1741 | Objects.requireNonNull(strategy); |
| 1742 | Objects.requireNonNull(devices); |
| 1743 | if (devices.isEmpty()) { |
| 1744 | throw new IllegalArgumentException( |
| 1745 | "Tried to set preferred devices for strategy with a empty list"); |
| 1746 | } |
| 1747 | for (AudioDeviceAttributes device : devices) { |
| 1748 | Objects.requireNonNull(device); |
| 1749 | } |
| 1750 | try { |
| 1751 | final int status = |
| 1752 | getService().setPreferredDevicesForStrategy(strategy.getId(), devices); |
| 1753 | return status == AudioSystem.SUCCESS; |
| 1754 | } catch (RemoteException e) { |
| 1755 | throw e.rethrowFromSystemServer(); |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | /** |
| 1760 | * @hide |
| 1761 | * Return the preferred devices for an audio strategy, previously set with |
Jean-Michel Trivi | 0e87bca | 2020-02-18 14:30:29 -0800 | [diff] [blame] | 1762 | * {@link #setPreferredDeviceForStrategy(AudioProductStrategy, AudioDeviceAttributes)} |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1763 | * {@link #setPreferredDevicesForStrategy(AudioProductStrategy, List<AudioDeviceAttributes>)} |
Jean-Michel Trivi | 21cd541 | 2019-10-30 08:16:51 -0700 | [diff] [blame] | 1764 | * @param strategy the strategy to query |
| 1765 | * @return the preferred device for that strategy, or null if none was ever set or if the |
| 1766 | * strategy is invalid |
| 1767 | */ |
| 1768 | @SystemApi |
| 1769 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1770 | @NonNull |
| 1771 | public List<AudioDeviceAttributes> getPreferredDevicesForStrategy( |
Jean-Michel Trivi | 21cd541 | 2019-10-30 08:16:51 -0700 | [diff] [blame] | 1772 | @NonNull AudioProductStrategy strategy) { |
| 1773 | Objects.requireNonNull(strategy); |
| 1774 | try { |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1775 | return getService().getPreferredDevicesForStrategy(strategy.getId()); |
Jean-Michel Trivi | 21cd541 | 2019-10-30 08:16:51 -0700 | [diff] [blame] | 1776 | } catch (RemoteException e) { |
| 1777 | throw e.rethrowFromSystemServer(); |
| 1778 | } |
| 1779 | } |
| 1780 | |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1781 | /** |
| 1782 | * @hide |
| 1783 | * Interface to be notified of changes in the preferred audio device set for a given audio |
| 1784 | * strategy. |
Jean-Michel Trivi | cf9c5d3 | 2020-02-20 10:45:35 -0800 | [diff] [blame] | 1785 | * <p>Note that this listener will only be invoked whenever |
| 1786 | * {@link #setPreferredDeviceForStrategy(AudioProductStrategy, AudioDeviceAttributes)} or |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1787 | * {@link #setPreferredDevicesForStrategy(AudioProductStrategy, List<AudioDeviceAttributes>)} |
Jean-Michel Trivi | cf9c5d3 | 2020-02-20 10:45:35 -0800 | [diff] [blame] | 1788 | * {@link #removePreferredDeviceForStrategy(AudioProductStrategy)} causes a change in |
| 1789 | * preferred device. It will not be invoked directly after registration with |
| 1790 | * {@link #addOnPreferredDeviceForStrategyChangedListener(Executor, OnPreferredDeviceForStrategyChangedListener)} |
| 1791 | * to indicate which strategies had preferred devices at the time of registration.</p> |
Jean-Michel Trivi | 0e87bca | 2020-02-18 14:30:29 -0800 | [diff] [blame] | 1792 | * @see #setPreferredDeviceForStrategy(AudioProductStrategy, AudioDeviceAttributes) |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1793 | * @see #removePreferredDeviceForStrategy(AudioProductStrategy) |
| 1794 | * @see #getPreferredDeviceForStrategy(AudioProductStrategy) |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1795 | * @deprecated use #OnPreferredDevicesForStrategyChangedListener |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1796 | */ |
| 1797 | @SystemApi |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1798 | @Deprecated |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1799 | public interface OnPreferredDeviceForStrategyChangedListener { |
| 1800 | /** |
| 1801 | * Called on the listener to indicate that the preferred audio device for the given |
| 1802 | * strategy has changed. |
| 1803 | * @param strategy the {@link AudioProductStrategy} whose preferred device changed |
| 1804 | * @param device <code>null</code> if the preferred device was removed, or the newly set |
| 1805 | * preferred audio device |
| 1806 | */ |
| 1807 | void onPreferredDeviceForStrategyChanged(@NonNull AudioProductStrategy strategy, |
Jean-Michel Trivi | 0e87bca | 2020-02-18 14:30:29 -0800 | [diff] [blame] | 1808 | @Nullable AudioDeviceAttributes device); |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | /** |
| 1812 | * @hide |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1813 | * Interface to be notified of changes in the preferred audio devices set for a given audio |
| 1814 | * strategy. |
| 1815 | * <p>Note that this listener will only be invoked whenever |
| 1816 | * {@link #setPreferredDeviceForStrategy(AudioProductStrategy, AudioDeviceAttributes)} or |
| 1817 | * {@link #setPreferredDevicesForStrategy(AudioProductStrategy, List<AudioDeviceAttributes>)} |
| 1818 | * {@link #removePreferredDeviceForStrategy(AudioProductStrategy)} causes a change in |
| 1819 | * preferred device(s). It will not be invoked directly after registration with |
| 1820 | * {@link #addOnPreferredDevicesForStrategyChangedListener( |
| 1821 | * Executor, OnPreferredDevicesForStrategyChangedListener)} |
| 1822 | * to indicate which strategies had preferred devices at the time of registration.</p> |
| 1823 | * @see #setPreferredDeviceForStrategy(AudioProductStrategy, AudioDeviceAttributes) |
| 1824 | * @see #setPreferredDevicesForStrategy(AudioProductStrategy, List) |
| 1825 | * @see #removePreferredDeviceForStrategy(AudioProductStrategy) |
| 1826 | * @see #getPreferredDeviceForStrategy(AudioProductStrategy) |
| 1827 | * @see #getPreferredDevicesForStrategy(AudioProductStrategy) |
| 1828 | */ |
| 1829 | @SystemApi |
| 1830 | public interface OnPreferredDevicesForStrategyChangedListener { |
| 1831 | /** |
| 1832 | * Called on the listener to indicate that the preferred audio devices for the given |
| 1833 | * strategy has changed. |
| 1834 | * @param strategy the {@link AudioProductStrategy} whose preferred device changed |
| 1835 | * @param devices a list of newly set preferred audio devices |
| 1836 | */ |
| 1837 | void onPreferredDevicesForStrategyChanged(@NonNull AudioProductStrategy strategy, |
| 1838 | @NonNull List<AudioDeviceAttributes> devices); |
| 1839 | } |
| 1840 | |
| 1841 | /** |
| 1842 | * @hide |
| 1843 | * Adds a listener for being notified of changes to the strategy-preferred audio device. |
| 1844 | * @param executor |
| 1845 | * @param listener |
| 1846 | * @throws SecurityException if the caller doesn't hold the required permission |
| 1847 | * @deprecated use {@link #addOnPreferredDevicesForStrategyChangedListener( |
| 1848 | * Executor, AudioManager.OnPreferredDevicesForStrategyChangedListener)} instead |
| 1849 | */ |
| 1850 | @SystemApi |
| 1851 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1852 | @Deprecated |
| 1853 | public void addOnPreferredDeviceForStrategyChangedListener( |
| 1854 | @NonNull @CallbackExecutor Executor executor, |
| 1855 | @NonNull OnPreferredDeviceForStrategyChangedListener listener) |
| 1856 | throws SecurityException { |
| 1857 | // No-op, the method is deprecated. |
| 1858 | } |
| 1859 | |
| 1860 | /** |
| 1861 | * @hide |
| 1862 | * Removes a previously added listener of changes to the strategy-preferred audio device. |
| 1863 | * @param listener |
| 1864 | * @deprecated use {@link #removeOnPreferredDevicesForStrategyChangedListener( |
| 1865 | * AudioManager.OnPreferredDevicesForStrategyChangedListener)} instead |
| 1866 | */ |
| 1867 | @SystemApi |
| 1868 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 1869 | @Deprecated |
| 1870 | public void removeOnPreferredDeviceForStrategyChangedListener( |
| 1871 | @NonNull OnPreferredDeviceForStrategyChangedListener listener) { |
| 1872 | // No-op, the method is deprecated. |
| 1873 | } |
| 1874 | |
| 1875 | /** |
| 1876 | * @hide |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1877 | * Adds a listener for being notified of changes to the strategy-preferred audio device. |
| 1878 | * @param executor |
| 1879 | * @param listener |
| 1880 | * @throws SecurityException if the caller doesn't hold the required permission |
| 1881 | */ |
| 1882 | @SystemApi |
| 1883 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1884 | public void addOnPreferredDevicesForStrategyChangedListener( |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1885 | @NonNull @CallbackExecutor Executor executor, |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1886 | @NonNull OnPreferredDevicesForStrategyChangedListener listener) |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1887 | throws SecurityException { |
| 1888 | Objects.requireNonNull(executor); |
| 1889 | Objects.requireNonNull(listener); |
| 1890 | synchronized (mPrefDevListenerLock) { |
| 1891 | if (hasPrefDevListener(listener)) { |
| 1892 | throw new IllegalArgumentException( |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1893 | "attempt to call addOnPreferredDevicesForStrategyChangedListener() " |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1894 | + "on a previously registered listener"); |
| 1895 | } |
| 1896 | // lazy initialization of the list of strategy-preferred device listener |
| 1897 | if (mPrefDevListeners == null) { |
| 1898 | mPrefDevListeners = new ArrayList<>(); |
| 1899 | } |
| 1900 | final int oldCbCount = mPrefDevListeners.size(); |
| 1901 | mPrefDevListeners.add(new PrefDevListenerInfo(listener, executor)); |
| 1902 | if (oldCbCount == 0 && mPrefDevListeners.size() > 0) { |
| 1903 | // register binder for callbacks |
| 1904 | if (mPrefDevDispatcherStub == null) { |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1905 | mPrefDevDispatcherStub = new StrategyPreferredDevicesDispatcherStub(); |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1906 | } |
| 1907 | try { |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1908 | getService().registerStrategyPreferredDevicesDispatcher(mPrefDevDispatcherStub); |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1909 | } catch (RemoteException e) { |
| 1910 | throw e.rethrowFromSystemServer(); |
| 1911 | } |
| 1912 | } |
| 1913 | } |
| 1914 | } |
| 1915 | |
| 1916 | /** |
| 1917 | * @hide |
| 1918 | * Removes a previously added listener of changes to the strategy-preferred audio device. |
| 1919 | * @param listener |
| 1920 | */ |
| 1921 | @SystemApi |
| 1922 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1923 | public void removeOnPreferredDevicesForStrategyChangedListener( |
| 1924 | @NonNull OnPreferredDevicesForStrategyChangedListener listener) { |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1925 | Objects.requireNonNull(listener); |
| 1926 | synchronized (mPrefDevListenerLock) { |
| 1927 | if (!removePrefDevListener(listener)) { |
| 1928 | throw new IllegalArgumentException( |
| 1929 | "attempt to call removeOnPreferredDeviceForStrategyChangedListener() " |
| 1930 | + "on an unregistered listener"); |
| 1931 | } |
| 1932 | if (mPrefDevListeners.size() == 0) { |
| 1933 | // unregister binder for callbacks |
| 1934 | try { |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1935 | getService().unregisterStrategyPreferredDevicesDispatcher( |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1936 | mPrefDevDispatcherStub); |
| 1937 | } catch (RemoteException e) { |
| 1938 | throw e.rethrowFromSystemServer(); |
| 1939 | } finally { |
| 1940 | mPrefDevDispatcherStub = null; |
| 1941 | mPrefDevListeners = null; |
| 1942 | } |
| 1943 | } |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | |
| 1948 | private final Object mPrefDevListenerLock = new Object(); |
| 1949 | /** |
| 1950 | * List of listeners for preferred device for strategy and their associated Executor. |
| 1951 | * List is lazy-initialized on first registration |
| 1952 | */ |
| 1953 | @GuardedBy("mPrefDevListenerLock") |
| 1954 | private @Nullable ArrayList<PrefDevListenerInfo> mPrefDevListeners; |
| 1955 | |
| 1956 | private static class PrefDevListenerInfo { |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1957 | final @NonNull OnPreferredDevicesForStrategyChangedListener mListener; |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1958 | final @NonNull Executor mExecutor; |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1959 | PrefDevListenerInfo(OnPreferredDevicesForStrategyChangedListener listener, Executor exe) { |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1960 | mListener = listener; |
| 1961 | mExecutor = exe; |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | @GuardedBy("mPrefDevListenerLock") |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1966 | private StrategyPreferredDevicesDispatcherStub mPrefDevDispatcherStub; |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1967 | |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1968 | private final class StrategyPreferredDevicesDispatcherStub |
| 1969 | extends IStrategyPreferredDevicesDispatcher.Stub { |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1970 | |
| 1971 | @Override |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1972 | public void dispatchPrefDevicesChanged(int strategyId, |
| 1973 | @NonNull List<AudioDeviceAttributes> devices) { |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1974 | // make a shallow copy of listeners so callback is not executed under lock |
| 1975 | final ArrayList<PrefDevListenerInfo> prefDevListeners; |
| 1976 | synchronized (mPrefDevListenerLock) { |
| 1977 | if (mPrefDevListeners == null || mPrefDevListeners.size() == 0) { |
| 1978 | return; |
| 1979 | } |
| 1980 | prefDevListeners = (ArrayList<PrefDevListenerInfo>) mPrefDevListeners.clone(); |
| 1981 | } |
| 1982 | final AudioProductStrategy strategy = |
| 1983 | AudioProductStrategy.getAudioProductStrategyWithId(strategyId); |
| 1984 | final long ident = Binder.clearCallingIdentity(); |
| 1985 | try { |
| 1986 | for (PrefDevListenerInfo info : prefDevListeners) { |
| 1987 | info.mExecutor.execute(() -> |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1988 | info.mListener.onPreferredDevicesForStrategyChanged(strategy, devices)); |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1989 | } |
| 1990 | } finally { |
| 1991 | Binder.restoreCallingIdentity(ident); |
| 1992 | } |
| 1993 | } |
| 1994 | } |
| 1995 | |
| 1996 | @GuardedBy("mPrefDevListenerLock") |
| 1997 | private @Nullable PrefDevListenerInfo getPrefDevListenerInfo( |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 1998 | OnPreferredDevicesForStrategyChangedListener listener) { |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 1999 | if (mPrefDevListeners == null) { |
| 2000 | return null; |
| 2001 | } |
| 2002 | for (PrefDevListenerInfo info : mPrefDevListeners) { |
| 2003 | if (info.mListener == listener) { |
| 2004 | return info; |
| 2005 | } |
| 2006 | } |
| 2007 | return null; |
| 2008 | } |
| 2009 | |
| 2010 | @GuardedBy("mPrefDevListenerLock") |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 2011 | private boolean hasPrefDevListener(OnPreferredDevicesForStrategyChangedListener listener) { |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 2012 | return getPrefDevListenerInfo(listener) != null; |
| 2013 | } |
| 2014 | |
| 2015 | @GuardedBy("mPrefDevListenerLock") |
| 2016 | /** |
| 2017 | * @return true if the listener was removed from the list |
| 2018 | */ |
jiabin | f40141d | 2020-08-07 17:27:48 -0700 | [diff] [blame] | 2019 | private boolean removePrefDevListener(OnPreferredDevicesForStrategyChangedListener listener) { |
Jean-Michel Trivi | 8d64ebb | 2020-02-05 15:44:42 -0800 | [diff] [blame] | 2020 | final PrefDevListenerInfo infoToRemove = getPrefDevListenerInfo(listener); |
| 2021 | if (infoToRemove != null) { |
| 2022 | mPrefDevListeners.remove(infoToRemove); |
| 2023 | return true; |
| 2024 | } |
| 2025 | return false; |
| 2026 | } |
| 2027 | |
Jean-Michel Trivi | 21cd541 | 2019-10-30 08:16:51 -0700 | [diff] [blame] | 2028 | //==================================================================== |
Jiabin Huang | b55305f | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2029 | // Audio Capture Preset routing |
| 2030 | |
| 2031 | /** |
| 2032 | * @hide |
| 2033 | * Set the preferred device for a given capture preset, i.e. the audio routing to be used by |
| 2034 | * this capture preset. Note that the device may not be available at the time the preferred |
| 2035 | * device is set, but it will be used once made available. |
| 2036 | * <p>Use {@link #clearPreferredDevicesForCapturePreset(int)} to cancel setting this preference |
| 2037 | * for this capture preset.</p> |
| 2038 | * @param capturePreset the audio capture preset whose routing will be affected |
| 2039 | * @param device the audio device to route to when available |
| 2040 | * @return true if the operation was successful, false otherwise |
| 2041 | */ |
| 2042 | @SystemApi |
| 2043 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
jiabin | 958faf9 | 2021-03-12 20:00:05 +0000 | [diff] [blame] | 2044 | public boolean setPreferredDeviceForCapturePreset(@MediaRecorder.SystemSource int capturePreset, |
Jiabin Huang | b55305f | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2045 | @NonNull AudioDeviceAttributes device) { |
| 2046 | return setPreferredDevicesForCapturePreset(capturePreset, Arrays.asList(device)); |
| 2047 | } |
| 2048 | |
| 2049 | /** |
| 2050 | * @hide |
| 2051 | * Remove all the preferred audio devices previously set |
| 2052 | * @param capturePreset the audio capture preset whose routing will be affected |
| 2053 | * @return true if the operation was successful, false otherwise (invalid capture preset, or no |
| 2054 | * device set for example) |
| 2055 | */ |
| 2056 | @SystemApi |
| 2057 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
jiabin | 958faf9 | 2021-03-12 20:00:05 +0000 | [diff] [blame] | 2058 | public boolean clearPreferredDevicesForCapturePreset( |
| 2059 | @MediaRecorder.SystemSource int capturePreset) { |
Jiabin Huang | b55305f | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2060 | if (!MediaRecorder.isValidAudioSource(capturePreset)) { |
| 2061 | return false; |
| 2062 | } |
| 2063 | try { |
| 2064 | final int status = getService().clearPreferredDevicesForCapturePreset(capturePreset); |
| 2065 | return status == AudioSystem.SUCCESS; |
| 2066 | } catch (RemoteException e) { |
| 2067 | throw e.rethrowFromSystemServer(); |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | /** |
| 2072 | * @hide |
| 2073 | * Return the preferred devices for an audio capture preset, previously set with |
| 2074 | * {@link #setPreferredDeviceForCapturePreset(int, AudioDeviceAttributes)} |
| 2075 | * @param capturePreset the capture preset to query |
| 2076 | * @return a list that contains preferred devices for that capture preset. |
| 2077 | */ |
| 2078 | @NonNull |
| 2079 | @SystemApi |
| 2080 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
jiabin | 958faf9 | 2021-03-12 20:00:05 +0000 | [diff] [blame] | 2081 | public List<AudioDeviceAttributes> getPreferredDevicesForCapturePreset( |
| 2082 | @MediaRecorder.SystemSource int capturePreset) { |
Jiabin Huang | b55305f | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2083 | if (!MediaRecorder.isValidAudioSource(capturePreset)) { |
| 2084 | return new ArrayList<AudioDeviceAttributes>(); |
| 2085 | } |
| 2086 | try { |
| 2087 | return getService().getPreferredDevicesForCapturePreset(capturePreset); |
| 2088 | } catch (RemoteException e) { |
| 2089 | throw e.rethrowFromSystemServer(); |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | private boolean setPreferredDevicesForCapturePreset( |
jiabin | 958faf9 | 2021-03-12 20:00:05 +0000 | [diff] [blame] | 2094 | @MediaRecorder.SystemSource int capturePreset, |
| 2095 | @NonNull List<AudioDeviceAttributes> devices) { |
Jiabin Huang | b55305f | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2096 | Objects.requireNonNull(devices); |
| 2097 | if (!MediaRecorder.isValidAudioSource(capturePreset)) { |
| 2098 | return false; |
| 2099 | } |
| 2100 | if (devices.size() != 1) { |
| 2101 | throw new IllegalArgumentException( |
| 2102 | "Only support setting one preferred devices for capture preset"); |
| 2103 | } |
| 2104 | for (AudioDeviceAttributes device : devices) { |
| 2105 | Objects.requireNonNull(device); |
| 2106 | } |
| 2107 | try { |
| 2108 | final int status = |
| 2109 | getService().setPreferredDevicesForCapturePreset(capturePreset, devices); |
| 2110 | return status == AudioSystem.SUCCESS; |
| 2111 | } catch (RemoteException e) { |
| 2112 | throw e.rethrowFromSystemServer(); |
| 2113 | } |
| 2114 | } |
| 2115 | |
| 2116 | /** |
| 2117 | * @hide |
| 2118 | * Interface to be notified of changes in the preferred audio devices set for a given capture |
| 2119 | * preset. |
| 2120 | * <p>Note that this listener will only be invoked whenever |
| 2121 | * {@link #setPreferredDeviceForCapturePreset(int, AudioDeviceAttributes)} or |
| 2122 | * {@link #clearPreferredDevicesForCapturePreset(int)} causes a change in |
| 2123 | * preferred device. It will not be invoked directly after registration with |
| 2124 | * {@link #addOnPreferredDevicesForCapturePresetChangedListener( |
| 2125 | * Executor, OnPreferredDevicesForCapturePresetChangedListener)} |
| 2126 | * to indicate which strategies had preferred devices at the time of registration.</p> |
| 2127 | * @see #setPreferredDeviceForCapturePreset(int, AudioDeviceAttributes) |
| 2128 | * @see #clearPreferredDevicesForCapturePreset(int) |
| 2129 | * @see #getPreferredDevicesForCapturePreset(int) |
| 2130 | */ |
| 2131 | @SystemApi |
| 2132 | public interface OnPreferredDevicesForCapturePresetChangedListener { |
| 2133 | /** |
| 2134 | * Called on the listener to indicate that the preferred audio devices for the given |
| 2135 | * capture preset has changed. |
| 2136 | * @param capturePreset the capture preset whose preferred device changed |
| 2137 | * @param devices a list of newly set preferred audio devices |
| 2138 | */ |
| 2139 | void onPreferredDevicesForCapturePresetChanged( |
jiabin | 958faf9 | 2021-03-12 20:00:05 +0000 | [diff] [blame] | 2140 | @MediaRecorder.SystemSource int capturePreset, |
| 2141 | @NonNull List<AudioDeviceAttributes> devices); |
Jiabin Huang | b55305f | 2020-09-03 17:54:16 +0000 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | /** |
| 2145 | * @hide |
| 2146 | * Adds a listener for being notified of changes to the capture-preset-preferred audio device. |
| 2147 | * @param executor |
| 2148 | * @param listener |
| 2149 | * @throws SecurityException if the caller doesn't hold the required permission |
| 2150 | */ |
| 2151 | @SystemApi |
| 2152 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 2153 | public void addOnPreferredDevicesForCapturePresetChangedListener( |
| 2154 | @NonNull @CallbackExecutor Executor executor, |
| 2155 | @NonNull OnPreferredDevicesForCapturePresetChangedListener listener) |
| 2156 | throws SecurityException { |
| 2157 | Objects.requireNonNull(executor); |
| 2158 | Objects.requireNonNull(listener); |
| 2159 | int status = addOnDevRoleForCapturePresetChangedListener( |
| 2160 | executor, listener, AudioSystem.DEVICE_ROLE_PREFERRED); |
| 2161 | if (status == AudioSystem.ERROR) { |
| 2162 | // This must not happen |
| 2163 | throw new RuntimeException("Unknown error happened"); |
| 2164 | } |
| 2165 | if (status == AudioSystem.BAD_VALUE) { |
| 2166 | throw new IllegalArgumentException( |
| 2167 | "attempt to call addOnPreferredDevicesForCapturePresetChangedListener() " |
| 2168 | + "on a previously registered listener"); |
| 2169 | } |
| 2170 | } |
| 2171 | |
| 2172 | /** |
| 2173 | * @hide |
| 2174 | * Removes a previously added listener of changes to the capture-preset-preferred audio device. |
| 2175 | * @param listener |
| 2176 | */ |
| 2177 | @SystemApi |
| 2178 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 2179 | public void removeOnPreferredDevicesForCapturePresetChangedListener( |
| 2180 | @NonNull OnPreferredDevicesForCapturePresetChangedListener listener) { |
| 2181 | Objects.requireNonNull(listener); |
| 2182 | int status = removeOnDevRoleForCapturePresetChangedListener( |
| 2183 | listener, AudioSystem.DEVICE_ROLE_PREFERRED); |
| 2184 | if (status == AudioSystem.ERROR) { |
| 2185 | // This must not happen |
| 2186 | throw new RuntimeException("Unknown error happened"); |
| 2187 | } |
| 2188 | if (status == AudioSystem.BAD_VALUE) { |
| 2189 | throw new IllegalArgumentException( |
| 2190 | "attempt to call removeOnPreferredDevicesForCapturePresetChangedListener() " |
| 2191 | + "on an unregistered listener"); |
| 2192 | } |
| 2193 | } |
| 2194 | |
| 2195 | private <T> int addOnDevRoleForCapturePresetChangedListener( |
| 2196 | @NonNull @CallbackExecutor Executor executor, |
| 2197 | @NonNull T listener, int deviceRole) { |
| 2198 | Objects.requireNonNull(executor); |
| 2199 | Objects.requireNonNull(listener); |
| 2200 | DevRoleListeners<T> devRoleListeners = |
| 2201 | (DevRoleListeners<T>) mDevRoleForCapturePresetListeners.get(deviceRole); |
| 2202 | if (devRoleListeners == null) { |
| 2203 | return AudioSystem.ERROR; |
| 2204 | } |
| 2205 | synchronized (devRoleListeners.mDevRoleListenersLock) { |
| 2206 | if (devRoleListeners.hasDevRoleListener(listener)) { |
| 2207 | return AudioSystem.BAD_VALUE; |
| 2208 | } |
| 2209 | // lazy initialization of the list of device role listener |
| 2210 | if (devRoleListeners.mListenerInfos == null) { |
| 2211 | devRoleListeners.mListenerInfos = new ArrayList<>(); |
| 2212 | } |
| 2213 | final int oldCbCount = devRoleListeners.mListenerInfos.size(); |
| 2214 | devRoleListeners.mListenerInfos.add(new DevRoleListenerInfo<T>(executor, listener)); |
| 2215 | if (oldCbCount == 0 && devRoleListeners.mListenerInfos.size() > 0) { |
| 2216 | // register binder for callbacks |
| 2217 | synchronized (mDevRoleForCapturePresetListenersLock) { |
| 2218 | int deviceRoleListenerStatus = mDeviceRoleListenersStatus; |
| 2219 | mDeviceRoleListenersStatus |= (1 << deviceRole); |
| 2220 | if (deviceRoleListenerStatus != 0) { |
| 2221 | // There are already device role changed listeners active. |
| 2222 | return AudioSystem.SUCCESS; |
| 2223 | } |
| 2224 | if (mDevicesRoleForCapturePresetDispatcherStub == null) { |
| 2225 | mDevicesRoleForCapturePresetDispatcherStub = |
| 2226 | new CapturePresetDevicesRoleDispatcherStub(); |
| 2227 | } |
| 2228 | try { |
| 2229 | getService().registerCapturePresetDevicesRoleDispatcher( |
| 2230 | mDevicesRoleForCapturePresetDispatcherStub); |
| 2231 | } catch (RemoteException e) { |
| 2232 | throw e.rethrowFromSystemServer(); |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | } |
| 2237 | return AudioSystem.SUCCESS; |
| 2238 | } |
| 2239 | |
| 2240 | private <T> int removeOnDevRoleForCapturePresetChangedListener( |
| 2241 | @NonNull T listener, int deviceRole) { |
| 2242 | Objects.requireNonNull(listener); |
| 2243 | DevRoleListeners<T> devRoleListeners = |
| 2244 | (DevRoleListeners<T>) mDevRoleForCapturePresetListeners.get(deviceRole); |
| 2245 | if (devRoleListeners == null) { |
| 2246 | return AudioSystem.ERROR; |
| 2247 | } |
| 2248 | synchronized (devRoleListeners.mDevRoleListenersLock) { |
| 2249 | if (!devRoleListeners.removeDevRoleListener(listener)) { |
| 2250 | return AudioSystem.BAD_VALUE; |
| 2251 | } |
| 2252 | if (devRoleListeners.mListenerInfos.size() == 0) { |
| 2253 | // unregister binder for callbacks |
| 2254 | synchronized (mDevRoleForCapturePresetListenersLock) { |
| 2255 | mDeviceRoleListenersStatus ^= (1 << deviceRole); |
| 2256 | if (mDeviceRoleListenersStatus != 0) { |
| 2257 | // There are some other device role changed listeners active. |
| 2258 | return AudioSystem.SUCCESS; |
| 2259 | } |
| 2260 | try { |
| 2261 | getService().unregisterCapturePresetDevicesRoleDispatcher( |
| 2262 | mDevicesRoleForCapturePresetDispatcherStub); |
| 2263 | } catch (RemoteException e) { |
| 2264 | throw e.rethrowFromSystemServer(); |
| 2265 | } |
| 2266 | } |
| 2267 | } |
| 2268 | } |
| 2269 | return AudioSystem.SUCCESS; |
| 2270 | } |
| 2271 | |
| 2272 | private final Map<Integer, Object> mDevRoleForCapturePresetListeners = new HashMap<>(){{ |
| 2273 | put(AudioSystem.DEVICE_ROLE_PREFERRED, |
| 2274 | new DevRoleListeners<OnPreferredDevicesForCapturePresetChangedListener>()); |
| 2275 | }}; |
| 2276 | |
| 2277 | private class DevRoleListenerInfo<T> { |
| 2278 | final @NonNull Executor mExecutor; |
| 2279 | final @NonNull T mListener; |
| 2280 | DevRoleListenerInfo(Executor executor, T listener) { |
| 2281 | mExecutor = executor; |
| 2282 | mListener = listener; |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | private class DevRoleListeners<T> { |
| 2287 | private final Object mDevRoleListenersLock = new Object(); |
| 2288 | @GuardedBy("mDevRoleListenersLock") |
| 2289 | private @Nullable ArrayList<DevRoleListenerInfo<T>> mListenerInfos; |
| 2290 | |
| 2291 | @GuardedBy("mDevRoleListenersLock") |
| 2292 | private @Nullable DevRoleListenerInfo<T> getDevRoleListenerInfo(T listener) { |
| 2293 | if (mListenerInfos == null) { |
| 2294 | return null; |
| 2295 | } |
| 2296 | for (DevRoleListenerInfo<T> listenerInfo : mListenerInfos) { |
| 2297 | if (listenerInfo.mListener == listener) { |
| 2298 | return listenerInfo; |
| 2299 | } |
| 2300 | } |
| 2301 | return null; |
| 2302 | } |
| 2303 | |
| 2304 | @GuardedBy("mDevRoleListenersLock") |
| 2305 | private boolean hasDevRoleListener(T listener) { |
| 2306 | return getDevRoleListenerInfo(listener) != null; |
| 2307 | } |
| 2308 | |
| 2309 | @GuardedBy("mDevRoleListenersLock") |
| 2310 | private boolean removeDevRoleListener(T listener) { |
| 2311 | final DevRoleListenerInfo<T> infoToRemove = getDevRoleListenerInfo(listener); |
| 2312 | if (infoToRemove != null) { |
| 2313 | mListenerInfos.remove(infoToRemove); |
| 2314 | return true; |
| 2315 | } |
| 2316 | return false; |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | private final Object mDevRoleForCapturePresetListenersLock = new Object(); |
| 2321 | /** |
| 2322 | * Record if there is a listener added for device role change. If there is a listener added for |
| 2323 | * a specified device role change, the bit at position `1 << device_role` is set. |
| 2324 | */ |
| 2325 | @GuardedBy("mDevRoleForCapturePresetListenersLock") |
| 2326 | private int mDeviceRoleListenersStatus = 0; |
| 2327 | @GuardedBy("mDevRoleForCapturePresetListenersLock") |
| 2328 | private CapturePresetDevicesRoleDispatcherStub mDevicesRoleForCapturePresetDispatcherStub; |
| 2329 | |
| 2330 | private final class CapturePresetDevicesRoleDispatcherStub |
| 2331 | extends ICapturePresetDevicesRoleDispatcher.Stub { |
| 2332 | |
| 2333 | @Override |
| 2334 | public void dispatchDevicesRoleChanged( |
| 2335 | int capturePreset, int role, List<AudioDeviceAttributes> devices) { |
| 2336 | final Object listenersObj = mDevRoleForCapturePresetListeners.get(role); |
| 2337 | if (listenersObj == null) { |
| 2338 | return; |
| 2339 | } |
| 2340 | switch (role) { |
| 2341 | case AudioSystem.DEVICE_ROLE_PREFERRED: { |
| 2342 | final DevRoleListeners<OnPreferredDevicesForCapturePresetChangedListener> |
| 2343 | listeners = |
| 2344 | (DevRoleListeners<OnPreferredDevicesForCapturePresetChangedListener>) |
| 2345 | listenersObj; |
| 2346 | final ArrayList<DevRoleListenerInfo< |
| 2347 | OnPreferredDevicesForCapturePresetChangedListener>> prefDevListeners; |
| 2348 | synchronized (listeners.mDevRoleListenersLock) { |
| 2349 | if (listeners.mListenerInfos.isEmpty()) { |
| 2350 | return; |
| 2351 | } |
| 2352 | prefDevListeners = (ArrayList<DevRoleListenerInfo< |
| 2353 | OnPreferredDevicesForCapturePresetChangedListener>>) |
| 2354 | listeners.mListenerInfos.clone(); |
| 2355 | } |
| 2356 | final long ident = Binder.clearCallingIdentity(); |
| 2357 | try { |
| 2358 | for (DevRoleListenerInfo< |
| 2359 | OnPreferredDevicesForCapturePresetChangedListener> info : |
| 2360 | prefDevListeners) { |
| 2361 | info.mExecutor.execute(() -> |
| 2362 | info.mListener.onPreferredDevicesForCapturePresetChanged( |
| 2363 | capturePreset, devices)); |
| 2364 | } |
| 2365 | } finally { |
| 2366 | Binder.restoreCallingIdentity(ident); |
| 2367 | } |
| 2368 | } break; |
| 2369 | default: |
| 2370 | break; |
| 2371 | } |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | //==================================================================== |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 2376 | // Offload query |
| 2377 | /** |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 2378 | * Returns whether offloaded playback of an audio format is supported on the device. |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 2379 | * <p>Offloaded playback is the feature where the decoding and playback of an audio stream |
| 2380 | * is not competing with other software resources. In general, it is supported by dedicated |
| 2381 | * hardware, such as audio DSPs. |
| 2382 | * <p>Note that this query only provides information about the support of an audio format, |
| 2383 | * it does not indicate whether the resources necessary for the offloaded playback are |
| 2384 | * available at that instant. |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 2385 | * @param format the audio format (codec, sample rate, channels) being checked. |
Jean-Michel Trivi | f3fd84a | 2019-03-21 16:08:50 -0700 | [diff] [blame] | 2386 | * @param attributes the {@link AudioAttributes} to be used for playback |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 2387 | * @return true if the given audio format can be offloaded. |
| 2388 | */ |
Jean-Michel Trivi | f3fd84a | 2019-03-21 16:08:50 -0700 | [diff] [blame] | 2389 | public static boolean isOffloadedPlaybackSupported(@NonNull AudioFormat format, |
| 2390 | @NonNull AudioAttributes attributes) { |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 2391 | if (format == null) { |
Jean-Michel Trivi | f3fd84a | 2019-03-21 16:08:50 -0700 | [diff] [blame] | 2392 | throw new NullPointerException("Illegal null AudioFormat"); |
Jean-Michel Trivi | 03f5139 | 2018-08-31 15:47:13 -0700 | [diff] [blame] | 2393 | } |
Jean-Michel Trivi | f3fd84a | 2019-03-21 16:08:50 -0700 | [diff] [blame] | 2394 | if (attributes == null) { |
| 2395 | throw new NullPointerException("Illegal null AudioAttributes"); |
| 2396 | } |
Eric Laurent | ba3b3a6 | 2020-11-26 20:10:51 +0100 | [diff] [blame] | 2397 | return AudioSystem.getOffloadSupport(format, attributes) != PLAYBACK_OFFLOAD_NOT_SUPPORTED; |
| 2398 | } |
| 2399 | |
| 2400 | /** Return value for {@link #getPlaybackOffloadSupport(AudioFormat, AudioAttributes)}: |
| 2401 | offload playback not supported */ |
| 2402 | public static final int PLAYBACK_OFFLOAD_NOT_SUPPORTED = AudioSystem.OFFLOAD_NOT_SUPPORTED; |
| 2403 | /** Return value for {@link #getPlaybackOffloadSupport(AudioFormat, AudioAttributes)}: |
| 2404 | offload playback supported */ |
| 2405 | public static final int PLAYBACK_OFFLOAD_SUPPORTED = AudioSystem.OFFLOAD_SUPPORTED; |
| 2406 | /** Return value for {@link #getPlaybackOffloadSupport(AudioFormat, AudioAttributes)}: |
| 2407 | offload playback supported with gapless transitions */ |
| 2408 | public static final int PLAYBACK_OFFLOAD_GAPLESS_SUPPORTED = |
| 2409 | AudioSystem.OFFLOAD_GAPLESS_SUPPORTED; |
| 2410 | |
| 2411 | /** @hide */ |
| 2412 | @IntDef(flag = false, prefix = "PLAYBACK_OFFLOAD_", value = { |
| 2413 | PLAYBACK_OFFLOAD_NOT_SUPPORTED, |
| 2414 | PLAYBACK_OFFLOAD_SUPPORTED, |
| 2415 | PLAYBACK_OFFLOAD_GAPLESS_SUPPORTED } |
| 2416 | ) |
| 2417 | @Retention(RetentionPolicy.SOURCE) |
| 2418 | public @interface AudioOffloadMode {} |
| 2419 | |
| 2420 | /** |
| 2421 | * Returns whether offloaded playback of an audio format is supported on the device or not and |
| 2422 | * when supported whether gapless transitions are possible or not. |
| 2423 | * <p>Offloaded playback is the feature where the decoding and playback of an audio stream |
| 2424 | * is not competing with other software resources. In general, it is supported by dedicated |
| 2425 | * hardware, such as audio DSPs. |
| 2426 | * <p>Note that this query only provides information about the support of an audio format, |
| 2427 | * it does not indicate whether the resources necessary for the offloaded playback are |
| 2428 | * available at that instant. |
| 2429 | * @param format the audio format (codec, sample rate, channels) being checked. |
| 2430 | * @param attributes the {@link AudioAttributes} to be used for playback |
| 2431 | * @return {@link #PLAYBACK_OFFLOAD_NOT_SUPPORTED} if offload playback if not supported, |
| 2432 | * {@link #PLAYBACK_OFFLOAD_SUPPORTED} if offload playback is supported or |
| 2433 | * {@link #PLAYBACK_OFFLOAD_GAPLESS_SUPPORTED} if gapless transitions are |
| 2434 | * also supported. |
| 2435 | */ |
| 2436 | @AudioOffloadMode |
| 2437 | public static int getPlaybackOffloadSupport(@NonNull AudioFormat format, |
| 2438 | @NonNull AudioAttributes attributes) { |
| 2439 | if (format == null) { |
| 2440 | throw new NullPointerException("Illegal null AudioFormat"); |
| 2441 | } |
| 2442 | if (attributes == null) { |
| 2443 | throw new NullPointerException("Illegal null AudioAttributes"); |
| 2444 | } |
| 2445 | return AudioSystem.getOffloadSupport(format, attributes); |
Jean-Michel Trivi | 980d38f | 2018-01-08 15:43:35 -0800 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | //==================================================================== |
Jean-Michel Trivi | e9dd14e | 2021-07-12 11:10:18 -0700 | [diff] [blame] | 2449 | // Immersive audio |
| 2450 | |
| 2451 | /** |
| 2452 | * Return a handle to the optional platform's {@link Spatializer} |
Jean-Michel Trivi | 838913c | 2021-09-02 20:55:44 -0700 | [diff] [blame] | 2453 | * @return the {@code Spatializer} instance. |
Jean-Michel Trivi | c5f6957 | 2021-09-14 10:46:55 -0700 | [diff] [blame] | 2454 | * @see Spatializer#getImmersiveAudioLevel() to check for the level of support of the effect |
| 2455 | * on the platform |
Jean-Michel Trivi | e9dd14e | 2021-07-12 11:10:18 -0700 | [diff] [blame] | 2456 | */ |
Jean-Michel Trivi | c5f6957 | 2021-09-14 10:46:55 -0700 | [diff] [blame] | 2457 | public @NonNull Spatializer getSpatializer() { |
Jean-Michel Trivi | e9dd14e | 2021-07-12 11:10:18 -0700 | [diff] [blame] | 2458 | return new Spatializer(this); |
| 2459 | } |
| 2460 | |
| 2461 | //==================================================================== |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2462 | // Bluetooth SCO control |
| 2463 | /** |
Glenn Kasten | a24e991 | 2017-08-25 08:14:08 -0700 | [diff] [blame] | 2464 | * Sticky broadcast intent action indicating that the Bluetooth SCO audio |
Eric Laurent | 95b88fb | 2010-03-18 20:35:49 -0700 | [diff] [blame] | 2465 | * connection state has changed. The intent contains on extra {@link #EXTRA_SCO_AUDIO_STATE} |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2466 | * indicating the new state which is either {@link #SCO_AUDIO_STATE_DISCONNECTED} |
| 2467 | * or {@link #SCO_AUDIO_STATE_CONNECTED} |
| 2468 | * |
| 2469 | * @see #startBluetoothSco() |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2470 | * @deprecated Use {@link #ACTION_SCO_AUDIO_STATE_UPDATED} instead |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2471 | */ |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2472 | @Deprecated |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2473 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 2474 | public static final String ACTION_SCO_AUDIO_STATE_CHANGED = |
| 2475 | "android.media.SCO_AUDIO_STATE_CHANGED"; |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2476 | |
| 2477 | /** |
Glenn Kasten | a24e991 | 2017-08-25 08:14:08 -0700 | [diff] [blame] | 2478 | * Sticky broadcast intent action indicating that the Bluetooth SCO audio |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2479 | * connection state has been updated. |
| 2480 | * <p>This intent has two extras: |
| 2481 | * <ul> |
| 2482 | * <li> {@link #EXTRA_SCO_AUDIO_STATE} - The new SCO audio state. </li> |
| 2483 | * <li> {@link #EXTRA_SCO_AUDIO_PREVIOUS_STATE}- The previous SCO audio state. </li> |
| 2484 | * </ul> |
| 2485 | * <p> EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE can be any of: |
| 2486 | * <ul> |
| 2487 | * <li> {@link #SCO_AUDIO_STATE_DISCONNECTED}, </li> |
| 2488 | * <li> {@link #SCO_AUDIO_STATE_CONNECTING} or </li> |
| 2489 | * <li> {@link #SCO_AUDIO_STATE_CONNECTED}, </li> |
| 2490 | * </ul> |
| 2491 | * @see #startBluetoothSco() |
| 2492 | */ |
| 2493 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 2494 | public static final String ACTION_SCO_AUDIO_STATE_UPDATED = |
| 2495 | "android.media.ACTION_SCO_AUDIO_STATE_UPDATED"; |
| 2496 | |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2497 | /** |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2498 | * Extra for intent {@link #ACTION_SCO_AUDIO_STATE_CHANGED} or |
| 2499 | * {@link #ACTION_SCO_AUDIO_STATE_UPDATED} containing the new bluetooth SCO connection state. |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2500 | */ |
| 2501 | public static final String EXTRA_SCO_AUDIO_STATE = |
| 2502 | "android.media.extra.SCO_AUDIO_STATE"; |
| 2503 | |
| 2504 | /** |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2505 | * Extra for intent {@link #ACTION_SCO_AUDIO_STATE_UPDATED} containing the previous |
| 2506 | * bluetooth SCO connection state. |
| 2507 | */ |
| 2508 | public static final String EXTRA_SCO_AUDIO_PREVIOUS_STATE = |
| 2509 | "android.media.extra.SCO_AUDIO_PREVIOUS_STATE"; |
| 2510 | |
| 2511 | /** |
| 2512 | * Value for extra EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE |
| 2513 | * indicating that the SCO audio channel is not established |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2514 | */ |
| 2515 | public static final int SCO_AUDIO_STATE_DISCONNECTED = 0; |
| 2516 | /** |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2517 | * Value for extra {@link #EXTRA_SCO_AUDIO_STATE} or {@link #EXTRA_SCO_AUDIO_PREVIOUS_STATE} |
| 2518 | * indicating that the SCO audio channel is established |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2519 | */ |
| 2520 | public static final int SCO_AUDIO_STATE_CONNECTED = 1; |
| 2521 | /** |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2522 | * Value for extra EXTRA_SCO_AUDIO_STATE or EXTRA_SCO_AUDIO_PREVIOUS_STATE |
| 2523 | * indicating that the SCO audio channel is being established |
| 2524 | */ |
| 2525 | public static final int SCO_AUDIO_STATE_CONNECTING = 2; |
| 2526 | /** |
| 2527 | * Value for extra EXTRA_SCO_AUDIO_STATE indicating that |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2528 | * there was an error trying to obtain the state |
| 2529 | */ |
| 2530 | public static final int SCO_AUDIO_STATE_ERROR = -1; |
| 2531 | |
| 2532 | |
| 2533 | /** |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2534 | * Indicates if current platform supports use of SCO for off call use cases. |
| 2535 | * Application wanted to use bluetooth SCO audio when the phone is not in call |
Jean-Michel Trivi | 2ac2afe | 2012-08-21 11:16:55 -0700 | [diff] [blame] | 2536 | * must first call this method to make sure that the platform supports this |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2537 | * feature. |
| 2538 | * @return true if bluetooth SCO can be used for audio when not in call |
| 2539 | * false otherwise |
| 2540 | * @see #startBluetoothSco() |
| 2541 | */ |
| 2542 | public boolean isBluetoothScoAvailableOffCall() { |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 2543 | return getContext().getResources().getBoolean( |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2544 | com.android.internal.R.bool.config_bluetooth_sco_off_call); |
| 2545 | } |
| 2546 | |
| 2547 | /** |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2548 | * Start bluetooth SCO audio connection. |
| 2549 | * <p>Requires Permission: |
| 2550 | * {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}. |
| 2551 | * <p>This method can be used by applications wanting to send and received audio |
| 2552 | * to/from a bluetooth SCO headset while the phone is not in call. |
| 2553 | * <p>As the SCO connection establishment can take several seconds, |
| 2554 | * applications should not rely on the connection to be available when the method |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2555 | * returns but instead register to receive the intent {@link #ACTION_SCO_AUDIO_STATE_UPDATED} |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2556 | * and wait for the state to be {@link #SCO_AUDIO_STATE_CONNECTED}. |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2557 | * <p>As the ACTION_SCO_AUDIO_STATE_UPDATED intent is sticky, the application can check the SCO |
| 2558 | * audio state before calling startBluetoothSco() by reading the intent returned by the receiver |
| 2559 | * registration. If the state is already CONNECTED, no state change will be received via the |
| 2560 | * intent after calling startBluetoothSco(). It is however useful to call startBluetoothSco() |
| 2561 | * so that the connection stays active in case the current initiator stops the connection. |
| 2562 | * <p>Unless the connection is already active as described above, the state will always |
| 2563 | * transition from DISCONNECTED to CONNECTING and then either to CONNECTED if the connection |
| 2564 | * succeeds or back to DISCONNECTED if the connection fails (e.g no headset is connected). |
| 2565 | * <p>When finished with the SCO connection or if the establishment fails, the application must |
| 2566 | * call {@link #stopBluetoothSco()} to clear the request and turn down the bluetooth connection. |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2567 | * <p>Even if a SCO connection is established, the following restrictions apply on audio |
| 2568 | * output streams so that they can be routed to SCO headset: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2569 | * <ul> |
| 2570 | * <li> the stream type must be {@link #STREAM_VOICE_CALL} </li> |
| 2571 | * <li> the format must be mono </li> |
| 2572 | * <li> the sampling must be 16kHz or 8kHz </li> |
| 2573 | * </ul> |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2574 | * <p>The following restrictions apply on input streams: |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2575 | * <ul> |
| 2576 | * <li> the format must be mono </li> |
| 2577 | * <li> the sampling must be 8kHz </li> |
| 2578 | * </ul> |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2579 | * <p>Note that the phone application always has the priority on the usage of the SCO |
| 2580 | * connection for telephony. If this method is called while the phone is in call |
| 2581 | * it will be ignored. Similarly, if a call is received or sent while an application |
| 2582 | * is using the SCO connection, the connection will be lost for the application and NOT |
| 2583 | * returned automatically when the call ends. |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2584 | * <p>NOTE: up to and including API version |
| 2585 | * {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}, this method initiates a virtual |
| 2586 | * voice call to the bluetooth headset. |
| 2587 | * After API version {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2} only a raw SCO audio |
| 2588 | * connection is established. |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2589 | * @see #stopBluetoothSco() |
Eric Laurent | dc03c61 | 2011-04-01 10:59:41 -0700 | [diff] [blame] | 2590 | * @see #ACTION_SCO_AUDIO_STATE_UPDATED |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2591 | */ |
| 2592 | public void startBluetoothSco(){ |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 2593 | final IAudioService service = getService(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2594 | try { |
Marco Nelissen | 926ebb8 | 2015-03-11 09:59:49 -0700 | [diff] [blame] | 2595 | service.startBluetoothSco(mICallBack, |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 2596 | getContext().getApplicationInfo().targetSdkVersion); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2597 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2598 | throw e.rethrowFromSystemServer(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | /** |
Jean-Michel Trivi | 0daab22 | 2014-07-18 15:31:29 -0700 | [diff] [blame] | 2603 | * @hide |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2604 | * Start bluetooth SCO audio connection in virtual call mode. |
| 2605 | * <p>Requires Permission: |
| 2606 | * {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}. |
| 2607 | * <p>Similar to {@link #startBluetoothSco()} with explicit selection of virtual call mode. |
| 2608 | * Telephony and communication applications (VoIP, Video Chat) should preferably select |
| 2609 | * virtual call mode. |
| 2610 | * Applications using voice input for search or commands should first try raw audio connection |
| 2611 | * with {@link #startBluetoothSco()} and fall back to startBluetoothScoVirtualCall() in case of |
| 2612 | * failure. |
| 2613 | * @see #startBluetoothSco() |
| 2614 | * @see #stopBluetoothSco() |
| 2615 | * @see #ACTION_SCO_AUDIO_STATE_UPDATED |
| 2616 | */ |
Mathew Inwood | 8e742f9 | 2020-10-27 11:47:29 +0000 | [diff] [blame] | 2617 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2618 | public void startBluetoothScoVirtualCall() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 2619 | final IAudioService service = getService(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2620 | try { |
| 2621 | service.startBluetoothScoVirtualCall(mICallBack); |
| 2622 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2623 | throw e.rethrowFromSystemServer(); |
Eric Laurent | 8390075 | 2014-05-15 15:14:22 -0700 | [diff] [blame] | 2624 | } |
| 2625 | } |
| 2626 | |
| 2627 | /** |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2628 | * Stop bluetooth SCO audio connection. |
| 2629 | * <p>Requires Permission: |
| 2630 | * {@link android.Manifest.permission#MODIFY_AUDIO_SETTINGS}. |
| 2631 | * <p>This method must be called by applications having requested the use of |
Jean-Michel Trivi | 0daab22 | 2014-07-18 15:31:29 -0700 | [diff] [blame] | 2632 | * bluetooth SCO audio with {@link #startBluetoothSco()} when finished with the SCO |
| 2633 | * connection or if connection fails. |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2634 | * @see #startBluetoothSco() |
| 2635 | */ |
Jean-Michel Trivi | 0daab22 | 2014-07-18 15:31:29 -0700 | [diff] [blame] | 2636 | // Also used for connections started with {@link #startBluetoothScoVirtualCall()} |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2637 | public void stopBluetoothSco(){ |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 2638 | final IAudioService service = getService(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2639 | try { |
| 2640 | service.stopBluetoothSco(mICallBack); |
| 2641 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2642 | throw e.rethrowFromSystemServer(); |
Eric Laurent | 3def1ee | 2010-03-17 23:26:26 -0700 | [diff] [blame] | 2643 | } |
| 2644 | } |
| 2645 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2646 | /** |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2647 | * Request use of Bluetooth SCO headset for communications. |
Jean-Michel Trivi | fccb25d | 2009-09-15 16:06:10 -0700 | [diff] [blame] | 2648 | * <p> |
| 2649 | * This method should only be used by applications that replace the platform-wide |
| 2650 | * management of audio settings or the main telephony application. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2651 | * |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2652 | * @param on set <var>true</var> to use bluetooth SCO for communications; |
| 2653 | * <var>false</var> to not use bluetooth SCO for communications |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2654 | */ |
| 2655 | public void setBluetoothScoOn(boolean on){ |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 2656 | final IAudioService service = getService(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2657 | try { |
| 2658 | service.setBluetoothScoOn(on); |
| 2659 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2660 | throw e.rethrowFromSystemServer(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2661 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | /** |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2665 | * Checks whether communications use Bluetooth SCO. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2666 | * |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2667 | * @return true if SCO is used for communications; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2668 | * false if otherwise |
| 2669 | */ |
| 2670 | public boolean isBluetoothScoOn() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 2671 | final IAudioService service = getService(); |
Eric Laurent | c42ac9d | 2009-07-29 08:53:03 -0700 | [diff] [blame] | 2672 | try { |
| 2673 | return service.isBluetoothScoOn(); |
| 2674 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2675 | throw e.rethrowFromSystemServer(); |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2676 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | /** |
Eric Laurent | 242b338 | 2012-06-15 11:48:50 -0700 | [diff] [blame] | 2680 | * @param on set <var>true</var> to route A2DP audio to/from Bluetooth |
| 2681 | * headset; <var>false</var> disable A2DP audio |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2682 | * @deprecated Do not use. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2683 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2684 | @Deprecated public void setBluetoothA2dpOn(boolean on){ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2685 | } |
| 2686 | |
| 2687 | /** |
Eric Laurent | c117bea | 2017-02-07 11:04:18 -0800 | [diff] [blame] | 2688 | * Checks whether a Bluetooth A2DP audio peripheral is connected or not. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2689 | * |
Eric Laurent | c117bea | 2017-02-07 11:04:18 -0800 | [diff] [blame] | 2690 | * @return true if a Bluetooth A2DP peripheral is connected |
Eric Laurent | 242b338 | 2012-06-15 11:48:50 -0700 | [diff] [blame] | 2691 | * false if otherwise |
Eric Laurent | c117bea | 2017-02-07 11:04:18 -0800 | [diff] [blame] | 2692 | * @deprecated Use {@link AudioManager#getDevices(int)} instead to list available audio devices. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2693 | */ |
| 2694 | public boolean isBluetoothA2dpOn() { |
Eric Laurent | 242b338 | 2012-06-15 11:48:50 -0700 | [diff] [blame] | 2695 | if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_BLUETOOTH_A2DP,"") |
Eric Laurent | 9656df2 | 2016-04-20 16:42:28 -0700 | [diff] [blame] | 2696 | == AudioSystem.DEVICE_STATE_AVAILABLE) { |
| 2697 | return true; |
| 2698 | } else if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,"") |
| 2699 | == AudioSystem.DEVICE_STATE_AVAILABLE) { |
| 2700 | return true; |
| 2701 | } else if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER,"") |
| 2702 | == AudioSystem.DEVICE_STATE_AVAILABLE) { |
Eric Laurent | 242b338 | 2012-06-15 11:48:50 -0700 | [diff] [blame] | 2703 | return true; |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2704 | } |
Eric Laurent | 9656df2 | 2016-04-20 16:42:28 -0700 | [diff] [blame] | 2705 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2706 | } |
| 2707 | |
| 2708 | /** |
| 2709 | * Sets audio routing to the wired headset on or off. |
| 2710 | * |
| 2711 | * @param on set <var>true</var> to route audio to/from wired |
| 2712 | * headset; <var>false</var> disable wired headset audio |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2713 | * @deprecated Do not use. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2714 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2715 | @Deprecated public void setWiredHeadsetOn(boolean on){ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | /** |
Eric Laurent | 497b3fe | 2011-08-02 17:41:11 -0700 | [diff] [blame] | 2719 | * Checks whether a wired headset is connected or not. |
| 2720 | * <p>This is not a valid indication that audio playback is |
| 2721 | * actually over the wired headset as audio routing depends on other conditions. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2722 | * |
Eric Laurent | 497b3fe | 2011-08-02 17:41:11 -0700 | [diff] [blame] | 2723 | * @return true if a wired headset is connected. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2724 | * false if otherwise |
Eric Laurent | c117bea | 2017-02-07 11:04:18 -0800 | [diff] [blame] | 2725 | * @deprecated Use {@link AudioManager#getDevices(int)} instead to list available audio devices. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2726 | */ |
| 2727 | public boolean isWiredHeadsetOn() { |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 2728 | if (AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADSET,"") |
Eric Laurent | 6015a97 | 2010-02-12 07:41:14 -0800 | [diff] [blame] | 2729 | == AudioSystem.DEVICE_STATE_UNAVAILABLE && |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 2730 | AudioSystem.getDeviceConnectionState(DEVICE_OUT_WIRED_HEADPHONE,"") |
Paul McLean | 145c953 | 2017-08-04 11:12:19 -0600 | [diff] [blame] | 2731 | == AudioSystem.DEVICE_STATE_UNAVAILABLE && |
| 2732 | AudioSystem.getDeviceConnectionState(DEVICE_OUT_USB_HEADSET, "") |
| 2733 | == AudioSystem.DEVICE_STATE_UNAVAILABLE) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 2734 | return false; |
| 2735 | } else { |
| 2736 | return true; |
| 2737 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2738 | } |
| 2739 | |
| 2740 | /** |
| 2741 | * Sets the microphone mute on or off. |
Jean-Michel Trivi | fccb25d | 2009-09-15 16:06:10 -0700 | [diff] [blame] | 2742 | * <p> |
| 2743 | * This method should only be used by applications that replace the platform-wide |
| 2744 | * management of audio settings or the main telephony application. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2745 | * |
| 2746 | * @param on set <var>true</var> to mute the microphone; |
| 2747 | * <var>false</var> to turn mute off |
| 2748 | */ |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2749 | public void setMicrophoneMute(boolean on) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 2750 | final IAudioService service = getService(); |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2751 | try { |
Kenny Guy | 70e0c58 | 2015-06-30 19:18:28 +0100 | [diff] [blame] | 2752 | service.setMicrophoneMute(on, getContext().getOpPackageName(), |
John Wu | 4f7e510 | 2021-06-22 17:29:11 +0000 | [diff] [blame] | 2753 | UserHandle.getCallingUserId(), getContext().getAttributionTag()); |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2754 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2755 | throw e.rethrowFromSystemServer(); |
Emily Bernier | 22c921a | 2014-05-28 11:01:32 -0400 | [diff] [blame] | 2756 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | /** |
Dmitry Shmidt | 3d23393 | 2019-09-20 15:52:03 -0700 | [diff] [blame] | 2760 | * @hide |
| 2761 | * Sets the microphone from switch mute on or off. |
| 2762 | * <p> |
| 2763 | * This method should only be used by InputManager to notify |
| 2764 | * Audio Subsystem about Microphone Mute switch state. |
| 2765 | * |
| 2766 | * @param on set <var>true</var> to mute the microphone; |
| 2767 | * <var>false</var> to turn mute off |
| 2768 | */ |
Mathew Inwood | 8e742f9 | 2020-10-27 11:47:29 +0000 | [diff] [blame] | 2769 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Dmitry Shmidt | 3d23393 | 2019-09-20 15:52:03 -0700 | [diff] [blame] | 2770 | public void setMicrophoneMuteFromSwitch(boolean on) { |
| 2771 | final IAudioService service = getService(); |
| 2772 | try { |
| 2773 | service.setMicrophoneMuteFromSwitch(on); |
| 2774 | } catch (RemoteException e) { |
| 2775 | throw e.rethrowFromSystemServer(); |
| 2776 | } |
| 2777 | } |
| 2778 | |
| 2779 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2780 | * Checks whether the microphone mute is on or off. |
| 2781 | * |
| 2782 | * @return true if microphone is muted, false if it's not |
| 2783 | */ |
| 2784 | public boolean isMicrophoneMute() { |
Dmitry Shmidt | 3d23393 | 2019-09-20 15:52:03 -0700 | [diff] [blame] | 2785 | final IAudioService service = getService(); |
| 2786 | try { |
| 2787 | return service.isMicrophoneMuted(); |
| 2788 | } catch (RemoteException e) { |
| 2789 | throw e.rethrowFromSystemServer(); |
| 2790 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | /** |
Jean-Michel Trivi | 7be17d2 | 2017-12-21 18:09:21 -0800 | [diff] [blame] | 2794 | * Broadcast Action: microphone muting state changed. |
| 2795 | * |
| 2796 | * You <em>cannot</em> receive this through components declared |
| 2797 | * in manifests, only by explicitly registering for it with |
| 2798 | * {@link Context#registerReceiver(BroadcastReceiver, IntentFilter) |
| 2799 | * Context.registerReceiver()}. |
| 2800 | * |
| 2801 | * <p>The intent has no extra values, use {@link #isMicrophoneMute} to check whether the |
| 2802 | * microphone is muted. |
| 2803 | */ |
| 2804 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 2805 | public static final String ACTION_MICROPHONE_MUTE_CHANGED = |
| 2806 | "android.media.action.MICROPHONE_MUTE_CHANGED"; |
| 2807 | |
| 2808 | /** |
Jean-Michel Trivi | 90682ff | 2019-03-18 15:52:00 -0700 | [diff] [blame] | 2809 | * Broadcast Action: speakerphone state changed. |
| 2810 | * |
| 2811 | * You <em>cannot</em> receive this through components declared |
| 2812 | * in manifests, only by explicitly registering for it with |
| 2813 | * {@link Context#registerReceiver(BroadcastReceiver, IntentFilter) |
| 2814 | * Context.registerReceiver()}. |
| 2815 | * |
| 2816 | * <p>The intent has no extra values, use {@link #isSpeakerphoneOn} to check whether the |
| 2817 | * speakerphone functionality is enabled or not. |
| 2818 | */ |
| 2819 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 2820 | public static final String ACTION_SPEAKERPHONE_STATE_CHANGED = |
| 2821 | "android.media.action.SPEAKERPHONE_STATE_CHANGED"; |
| 2822 | |
| 2823 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2824 | * Sets the audio mode. |
Jean-Michel Trivi | fccb25d | 2009-09-15 16:06:10 -0700 | [diff] [blame] | 2825 | * <p> |
| 2826 | * The audio mode encompasses audio routing AND the behavior of |
| 2827 | * the telephony layer. Therefore this method should only be used by applications that |
| 2828 | * replace the platform-wide management of audio settings or the main telephony application. |
| 2829 | * In particular, the {@link #MODE_IN_CALL} mode should only be used by the telephony |
| 2830 | * application when it places a phone call, as it will cause signals from the radio layer |
| 2831 | * to feed the platform mixer. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2832 | * |
Eric Laurent | 2d7197fc | 2019-11-06 10:17:06 -0800 | [diff] [blame] | 2833 | * @param mode the requested audio mode. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2834 | * Informs the HAL about the current audio state so that |
| 2835 | * it can route the audio appropriately. |
| 2836 | */ |
Eric Laurent | 2d7197fc | 2019-11-06 10:17:06 -0800 | [diff] [blame] | 2837 | public void setMode(@AudioMode int mode) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 2838 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2839 | try { |
Marco Nelissen | a80ac05 | 2015-03-12 16:17:45 -0700 | [diff] [blame] | 2840 | service.setMode(mode, mICallBack, mApplicationContext.getOpPackageName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2841 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2842 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2843 | } |
| 2844 | } |
| 2845 | |
| 2846 | /** |
Eric Laurent | 1c3408f | 2021-11-09 12:09:54 +0100 | [diff] [blame] | 2847 | * This change id controls use of audio modes for call audio redirection. |
| 2848 | * @hide |
| 2849 | */ |
| 2850 | @ChangeId |
| 2851 | @EnabledSince(targetSdkVersion = Build.VERSION_CODES.TIRAMISU) |
| 2852 | public static final long CALL_REDIRECTION_AUDIO_MODES = 189472651L; // buganizer id |
| 2853 | |
| 2854 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2855 | * Returns the current audio mode. |
| 2856 | * |
Eric Laurent | 2d7197fc | 2019-11-06 10:17:06 -0800 | [diff] [blame] | 2857 | * @return the current audio mode. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2858 | */ |
Eric Laurent | 2d7197fc | 2019-11-06 10:17:06 -0800 | [diff] [blame] | 2859 | @AudioMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2860 | public int getMode() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 2861 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2862 | try { |
Eric Laurent | 2d7197fc | 2019-11-06 10:17:06 -0800 | [diff] [blame] | 2863 | int mode = service.getMode(); |
| 2864 | int sdk; |
| 2865 | try { |
| 2866 | sdk = getContext().getApplicationInfo().targetSdkVersion; |
| 2867 | } catch (NullPointerException e) { |
| 2868 | // some tests don't have a Context |
| 2869 | sdk = Build.VERSION.SDK_INT; |
| 2870 | } |
| 2871 | if (mode == MODE_CALL_SCREENING && sdk <= Build.VERSION_CODES.Q) { |
| 2872 | mode = MODE_IN_CALL; |
Eric Laurent | 1c3408f | 2021-11-09 12:09:54 +0100 | [diff] [blame] | 2873 | } else if (mode == MODE_CALL_REDIRECT |
| 2874 | && !CompatChanges.isChangeEnabled(CALL_REDIRECTION_AUDIO_MODES)) { |
| 2875 | mode = MODE_IN_CALL; |
| 2876 | } else if (mode == MODE_COMMUNICATION_REDIRECT |
| 2877 | && !CompatChanges.isChangeEnabled(CALL_REDIRECTION_AUDIO_MODES)) { |
| 2878 | mode = MODE_IN_COMMUNICATION; |
Eric Laurent | 2d7197fc | 2019-11-06 10:17:06 -0800 | [diff] [blame] | 2879 | } |
| 2880 | return mode; |
| 2881 | } catch (RemoteException e) { |
| 2882 | throw e.rethrowFromSystemServer(); |
| 2883 | } |
| 2884 | } |
| 2885 | |
| 2886 | /** |
Nate Myren | 08635fe | 2021-04-20 12:04:39 -0700 | [diff] [blame] | 2887 | * Interface definition of a callback that is notified when the audio mode changes |
| 2888 | */ |
| 2889 | public interface OnModeChangedListener { |
| 2890 | /** |
| 2891 | * Called on the listener to indicate that the audio mode has changed |
| 2892 | * |
| 2893 | * @param mode The current audio mode |
| 2894 | */ |
| 2895 | void onModeChanged(@AudioMode int mode); |
| 2896 | } |
| 2897 | |
| 2898 | private final Object mModeListenerLock = new Object(); |
| 2899 | /** |
| 2900 | * List of listeners for audio mode and their associated Executor. |
| 2901 | * List is lazy-initialized on first registration |
| 2902 | */ |
| 2903 | @GuardedBy("mModeListenerLock") |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 2904 | private @Nullable ArrayList<ListenerInfo<OnModeChangedListener>> mModeListeners; |
Nate Myren | 08635fe | 2021-04-20 12:04:39 -0700 | [diff] [blame] | 2905 | |
| 2906 | @GuardedBy("mModeListenerLock") |
| 2907 | private ModeDispatcherStub mModeDispatcherStub; |
| 2908 | |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 2909 | private final class ModeDispatcherStub extends IAudioModeDispatcher.Stub { |
| 2910 | |
| 2911 | public void register(boolean register) { |
| 2912 | try { |
| 2913 | if (register) { |
| 2914 | getService().registerModeDispatcher(this); |
| 2915 | } else { |
| 2916 | getService().unregisterModeDispatcher(this); |
| 2917 | } |
| 2918 | } catch (RemoteException e) { |
| 2919 | e.rethrowFromSystemServer(); |
| 2920 | } |
| 2921 | } |
Nate Myren | 08635fe | 2021-04-20 12:04:39 -0700 | [diff] [blame] | 2922 | |
| 2923 | @Override |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 2924 | @SuppressLint("GuardedBy") // lock applied inside callListeners method |
Nate Myren | 08635fe | 2021-04-20 12:04:39 -0700 | [diff] [blame] | 2925 | public void dispatchAudioModeChanged(int mode) { |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 2926 | CallbackUtil.callListeners(mModeListeners, mModeListenerLock, |
| 2927 | (listener) -> listener.onModeChanged(mode)); |
Nate Myren | 08635fe | 2021-04-20 12:04:39 -0700 | [diff] [blame] | 2928 | } |
| 2929 | } |
| 2930 | |
Nate Myren | 08635fe | 2021-04-20 12:04:39 -0700 | [diff] [blame] | 2931 | /** |
| 2932 | * Adds a listener to be notified of changes to the audio mode. |
| 2933 | * See {@link #getMode()} |
| 2934 | * @param executor |
| 2935 | * @param listener |
| 2936 | */ |
| 2937 | public void addOnModeChangedListener( |
| 2938 | @NonNull @CallbackExecutor Executor executor, |
| 2939 | @NonNull OnModeChangedListener listener) { |
Nate Myren | 08635fe | 2021-04-20 12:04:39 -0700 | [diff] [blame] | 2940 | synchronized (mModeListenerLock) { |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 2941 | final Pair<ArrayList<ListenerInfo<OnModeChangedListener>>, ModeDispatcherStub> res = |
| 2942 | CallbackUtil.addListener("addOnModeChangedListener", |
| 2943 | executor, listener, mModeListeners, mModeDispatcherStub, |
| 2944 | () -> new ModeDispatcherStub(), |
| 2945 | stub -> stub.register(true)); |
| 2946 | mModeListeners = res.first; |
| 2947 | mModeDispatcherStub = res.second; |
Nate Myren | 08635fe | 2021-04-20 12:04:39 -0700 | [diff] [blame] | 2948 | } |
| 2949 | } |
| 2950 | |
| 2951 | /** |
| 2952 | * Removes a previously added listener for changes to audio mode. |
| 2953 | * See {@link #getMode()} |
| 2954 | * @param listener |
| 2955 | */ |
| 2956 | public void removeOnModeChangedListener(@NonNull OnModeChangedListener listener) { |
Nate Myren | 08635fe | 2021-04-20 12:04:39 -0700 | [diff] [blame] | 2957 | synchronized (mModeListenerLock) { |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 2958 | final Pair<ArrayList<ListenerInfo<OnModeChangedListener>>, ModeDispatcherStub> res = |
| 2959 | CallbackUtil.removeListener("removeOnModeChangedListener", |
| 2960 | listener, mModeListeners, mModeDispatcherStub, |
| 2961 | stub -> stub.register(false)); |
| 2962 | mModeListeners = res.first; |
| 2963 | mModeDispatcherStub = res.second; |
Nate Myren | 08635fe | 2021-04-20 12:04:39 -0700 | [diff] [blame] | 2964 | } |
| 2965 | } |
| 2966 | |
| 2967 | /** |
Eric Laurent | 2d7197fc | 2019-11-06 10:17:06 -0800 | [diff] [blame] | 2968 | * Indicates if the platform supports a special call screening and call monitoring mode. |
| 2969 | * <p> |
| 2970 | * When this mode is supported, it is possible to perform call screening and monitoring |
| 2971 | * functions while other use cases like music or movie playback are active. |
| 2972 | * <p> |
| 2973 | * Use {@link #setMode(int)} with mode {@link #MODE_CALL_SCREENING} to place the platform in |
| 2974 | * call screening mode. |
| 2975 | * <p> |
| 2976 | * If call screening mode is not supported, setting mode to |
| 2977 | * MODE_CALL_SCREENING will be ignored and will not change current mode reported by |
| 2978 | * {@link #getMode()}. |
| 2979 | * @return true if call screening mode is supported, false otherwise. |
| 2980 | */ |
| 2981 | public boolean isCallScreeningModeSupported() { |
| 2982 | final IAudioService service = getService(); |
| 2983 | try { |
| 2984 | return service.isCallScreeningModeSupported(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2985 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 2986 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2987 | } |
| 2988 | } |
| 2989 | |
| 2990 | /* modes for setMode/getMode/setRoute/getRoute */ |
| 2991 | /** |
| 2992 | * Audio harware modes. |
| 2993 | */ |
| 2994 | /** |
| 2995 | * Invalid audio mode. |
| 2996 | */ |
| 2997 | public static final int MODE_INVALID = AudioSystem.MODE_INVALID; |
| 2998 | /** |
| 2999 | * Current audio mode. Used to apply audio routing to current mode. |
| 3000 | */ |
| 3001 | public static final int MODE_CURRENT = AudioSystem.MODE_CURRENT; |
| 3002 | /** |
| 3003 | * Normal audio mode: not ringing and no call established. |
| 3004 | */ |
| 3005 | public static final int MODE_NORMAL = AudioSystem.MODE_NORMAL; |
| 3006 | /** |
| 3007 | * Ringing audio mode. An incoming is being signaled. |
| 3008 | */ |
| 3009 | public static final int MODE_RINGTONE = AudioSystem.MODE_RINGTONE; |
| 3010 | /** |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 3011 | * In call audio mode. A telephony call is established. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3012 | */ |
| 3013 | public static final int MODE_IN_CALL = AudioSystem.MODE_IN_CALL; |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 3014 | /** |
Jean-Michel Trivi | 8f677d6 | 2010-11-15 12:11:32 -0800 | [diff] [blame] | 3015 | * In communication audio mode. An audio/video chat or VoIP call is established. |
| 3016 | */ |
| 3017 | public static final int MODE_IN_COMMUNICATION = AudioSystem.MODE_IN_COMMUNICATION; |
Eric Laurent | 2d7197fc | 2019-11-06 10:17:06 -0800 | [diff] [blame] | 3018 | /** |
| 3019 | * Call screening in progress. Call is connected and audio is accessible to call |
| 3020 | * screening applications but other audio use cases are still possible. |
| 3021 | */ |
| 3022 | public static final int MODE_CALL_SCREENING = AudioSystem.MODE_CALL_SCREENING; |
| 3023 | |
Eric Laurent | 1c3408f | 2021-11-09 12:09:54 +0100 | [diff] [blame] | 3024 | /** |
| 3025 | * A telephony call is established and its audio is being redirected to another device. |
| 3026 | */ |
| 3027 | public static final int MODE_CALL_REDIRECT = AudioSystem.MODE_CALL_REDIRECT; |
| 3028 | |
| 3029 | /** |
| 3030 | * n audio/video chat or VoIP call is established and its audio is being redirected to another |
| 3031 | * device. |
| 3032 | */ |
| 3033 | public static final int MODE_COMMUNICATION_REDIRECT = AudioSystem.MODE_COMMUNICATION_REDIRECT; |
| 3034 | |
Eric Laurent | 2d7197fc | 2019-11-06 10:17:06 -0800 | [diff] [blame] | 3035 | /** @hide */ |
| 3036 | @IntDef(flag = false, prefix = "MODE_", value = { |
| 3037 | MODE_NORMAL, |
| 3038 | MODE_RINGTONE, |
| 3039 | MODE_IN_CALL, |
| 3040 | MODE_IN_COMMUNICATION, |
Eric Laurent | 1c3408f | 2021-11-09 12:09:54 +0100 | [diff] [blame] | 3041 | MODE_CALL_SCREENING, |
| 3042 | MODE_CALL_REDIRECT, |
| 3043 | MODE_COMMUNICATION_REDIRECT} |
Eric Laurent | 2d7197fc | 2019-11-06 10:17:06 -0800 | [diff] [blame] | 3044 | ) |
| 3045 | @Retention(RetentionPolicy.SOURCE) |
| 3046 | public @interface AudioMode {} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3047 | |
| 3048 | /* Routing bits for setRouting/getRouting API */ |
| 3049 | /** |
| 3050 | * Routing audio output to earpiece |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 3051 | * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), |
| 3052 | * setBluetoothScoOn() methods instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3053 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3054 | @Deprecated public static final int ROUTE_EARPIECE = AudioSystem.ROUTE_EARPIECE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3055 | /** |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 3056 | * Routing audio output to speaker |
| 3057 | * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), |
| 3058 | * setBluetoothScoOn() methods instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3059 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3060 | @Deprecated public static final int ROUTE_SPEAKER = AudioSystem.ROUTE_SPEAKER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3061 | /** |
| 3062 | * @deprecated use {@link #ROUTE_BLUETOOTH_SCO} |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 3063 | * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), |
| 3064 | * setBluetoothScoOn() methods instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3065 | */ |
| 3066 | @Deprecated public static final int ROUTE_BLUETOOTH = AudioSystem.ROUTE_BLUETOOTH_SCO; |
| 3067 | /** |
| 3068 | * Routing audio output to bluetooth SCO |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 3069 | * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), |
| 3070 | * setBluetoothScoOn() methods instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3071 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3072 | @Deprecated public static final int ROUTE_BLUETOOTH_SCO = AudioSystem.ROUTE_BLUETOOTH_SCO; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3073 | /** |
| 3074 | * Routing audio output to headset |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 3075 | * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), |
| 3076 | * setBluetoothScoOn() methods instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3077 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3078 | @Deprecated public static final int ROUTE_HEADSET = AudioSystem.ROUTE_HEADSET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3079 | /** |
| 3080 | * Routing audio output to bluetooth A2DP |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 3081 | * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), |
| 3082 | * setBluetoothScoOn() methods instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3083 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3084 | @Deprecated public static final int ROUTE_BLUETOOTH_A2DP = AudioSystem.ROUTE_BLUETOOTH_A2DP; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3085 | /** |
| 3086 | * Used for mask parameter of {@link #setRouting(int,int,int)}. |
Dianne Hackborn | 4a51c20 | 2009-08-21 15:14:02 -0700 | [diff] [blame] | 3087 | * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), |
| 3088 | * setBluetoothScoOn() methods instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3089 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3090 | @Deprecated public static final int ROUTE_ALL = AudioSystem.ROUTE_ALL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3091 | |
| 3092 | /** |
| 3093 | * Sets the audio routing for a specified mode |
| 3094 | * |
| 3095 | * @param mode audio mode to change route. E.g., MODE_RINGTONE. |
| 3096 | * @param routes bit vector of routes requested, created from one or |
| 3097 | * more of ROUTE_xxx types. Set bits indicate that route should be on |
| 3098 | * @param mask bit vector of routes to change, created from one or more of |
| 3099 | * ROUTE_xxx types. Unset bits indicate the route should be left unchanged |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 3100 | * |
| 3101 | * @deprecated Do not set audio routing directly, use setSpeakerphoneOn(), |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3102 | * setBluetoothScoOn() methods instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3103 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3104 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3105 | public void setRouting(int mode, int routes, int mask) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | /** |
| 3109 | * Returns the current audio routing bit vector for a specified mode. |
| 3110 | * |
| 3111 | * @param mode audio mode to get route (e.g., MODE_RINGTONE) |
| 3112 | * @return an audio route bit vector that can be compared with ROUTE_xxx |
| 3113 | * bits |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 3114 | * @deprecated Do not query audio routing directly, use isSpeakerphoneOn(), |
| 3115 | * isBluetoothScoOn(), isBluetoothA2dpOn() and isWiredHeadsetOn() methods instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3116 | */ |
Eric Laurent | b9c9d26 | 2009-05-06 08:13:20 -0700 | [diff] [blame] | 3117 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3118 | public int getRouting(int mode) { |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3119 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3120 | } |
| 3121 | |
| 3122 | /** |
| 3123 | * Checks whether any music is active. |
| 3124 | * |
| 3125 | * @return true if any music tracks are active. |
| 3126 | */ |
| 3127 | public boolean isMusicActive() { |
Jean-Michel Trivi | 15e3005 | 2020-12-09 11:54:00 -0800 | [diff] [blame] | 3128 | final IAudioService service = getService(); |
| 3129 | try { |
Eric Laurent | 89c3a97 | 2020-12-16 15:57:56 +0100 | [diff] [blame] | 3130 | return service.isMusicActive(false /*remotely*/); |
Jean-Michel Trivi | 15e3005 | 2020-12-09 11:54:00 -0800 | [diff] [blame] | 3131 | } catch (RemoteException e) { |
| 3132 | throw e.rethrowFromSystemServer(); |
| 3133 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3134 | } |
| 3135 | |
Jean-Michel Trivi | 3114ce3 | 2012-06-11 15:03:52 -0700 | [diff] [blame] | 3136 | /** |
| 3137 | * @hide |
Jean-Michel Trivi | 679d504 | 2013-02-04 16:24:09 -0800 | [diff] [blame] | 3138 | * Checks whether any music or media is actively playing on a remote device (e.g. wireless |
| 3139 | * display). Note that BT audio sinks are not considered remote devices. |
| 3140 | * @return true if {@link AudioManager#STREAM_MUSIC} is active on a remote device |
| 3141 | */ |
Mathew Inwood | 8e742f9 | 2020-10-27 11:47:29 +0000 | [diff] [blame] | 3142 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Jean-Michel Trivi | 679d504 | 2013-02-04 16:24:09 -0800 | [diff] [blame] | 3143 | public boolean isMusicActiveRemotely() { |
Eric Laurent | 89c3a97 | 2020-12-16 15:57:56 +0100 | [diff] [blame] | 3144 | final IAudioService service = getService(); |
| 3145 | try { |
| 3146 | return service.isMusicActive(true /*remotely*/); |
| 3147 | } catch (RemoteException e) { |
| 3148 | throw e.rethrowFromSystemServer(); |
| 3149 | } |
Jean-Michel Trivi | 679d504 | 2013-02-04 16:24:09 -0800 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | /** |
| 3153 | * @hide |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 3154 | * Checks whether the current audio focus is exclusive. |
| 3155 | * @return true if the top of the audio focus stack requested focus |
| 3156 | * with {@link #AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE} |
| 3157 | */ |
| 3158 | public boolean isAudioFocusExclusive() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3159 | final IAudioService service = getService(); |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 3160 | try { |
| 3161 | return service.getCurrentAudioFocus() == AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE; |
| 3162 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 3163 | throw e.rethrowFromSystemServer(); |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | /** |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 3168 | * Return a new audio session identifier not associated with any player or effect. |
Jean-Michel Trivi | 9e477e4 | 2014-08-25 17:44:28 -0700 | [diff] [blame] | 3169 | * An audio session identifier is a system wide unique identifier for a set of audio streams |
| 3170 | * (one or more mixed together). |
| 3171 | * <p>The primary use of the audio session ID is to associate audio effects to audio players, |
| 3172 | * such as {@link MediaPlayer} or {@link AudioTrack}: all audio effects sharing the same audio |
| 3173 | * session ID will be applied to the mixed audio content of the players that share the same |
| 3174 | * audio session. |
| 3175 | * <p>This method can for instance be used when creating one of the |
| 3176 | * {@link android.media.audiofx.AudioEffect} objects to define the audio session of the effect, |
| 3177 | * or to specify a session for a speech synthesis utterance |
| 3178 | * in {@link android.speech.tts.TextToSpeech.Engine}. |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 3179 | * @return a new unclaimed and unused audio session identifier, or {@link #ERROR} when the |
Jean-Michel Trivi | 9e477e4 | 2014-08-25 17:44:28 -0700 | [diff] [blame] | 3180 | * system failed to generate a new session, a condition in which audio playback or recording |
| 3181 | * will subsequently fail as well. |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 3182 | */ |
Dichen Zhang | f50b236 | 2018-11-26 13:18:58 -0800 | [diff] [blame] | 3183 | public int generateAudioSessionId() { |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 3184 | int session = AudioSystem.newAudioSessionId(); |
| 3185 | if (session > 0) { |
| 3186 | return session; |
| 3187 | } else { |
Jean-Michel Trivi | 289cc8e | 2014-07-18 18:45:32 -0700 | [diff] [blame] | 3188 | Log.e(TAG, "Failure to generate a new audio session ID"); |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 3189 | return ERROR; |
| 3190 | } |
| 3191 | } |
| 3192 | |
Jean-Michel Trivi | 289cc8e | 2014-07-18 18:45:32 -0700 | [diff] [blame] | 3193 | /** |
| 3194 | * A special audio session ID to indicate that the audio session ID isn't known and the |
| 3195 | * framework should generate a new value. This can be used when building a new |
| 3196 | * {@link AudioTrack} instance with |
| 3197 | * {@link AudioTrack#AudioTrack(AudioAttributes, AudioFormat, int, int, int)}. |
| 3198 | */ |
| 3199 | public static final int AUDIO_SESSION_ID_GENERATE = AudioSystem.AUDIO_SESSION_ALLOCATE; |
| 3200 | |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 3201 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3202 | /* |
| 3203 | * Sets a generic audio configuration parameter. The use of these parameters |
| 3204 | * are platform dependant, see libaudio |
| 3205 | * |
| 3206 | * ** Temporary interface - DO NOT USE |
| 3207 | * |
| 3208 | * TODO: Replace with a more generic key:value get/set mechanism |
| 3209 | * |
| 3210 | * param key name of parameter to set. Must not be null. |
| 3211 | * param value value of parameter. Must not be null. |
| 3212 | */ |
| 3213 | /** |
| 3214 | * @hide |
Dianne Hackborn | ba50b97c | 2013-04-30 15:04:46 -0700 | [diff] [blame] | 3215 | * @deprecated Use {@link #setParameters(String)} instead |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3216 | */ |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3217 | @Deprecated public void setParameter(String key, String value) { |
| 3218 | setParameters(key+"="+value); |
| 3219 | } |
| 3220 | |
| 3221 | /** |
| 3222 | * Sets a variable number of parameter values to audio hardware. |
| 3223 | * |
| 3224 | * @param keyValuePairs list of parameters key value pairs in the form: |
| 3225 | * key1=value1;key2=value2;... |
| 3226 | * |
| 3227 | */ |
| 3228 | public void setParameters(String keyValuePairs) { |
| 3229 | AudioSystem.setParameters(keyValuePairs); |
| 3230 | } |
| 3231 | |
| 3232 | /** |
William Escande | ef429b6 | 2021-10-15 18:37:40 +0200 | [diff] [blame] | 3233 | * @hide |
| 3234 | */ |
| 3235 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 3236 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_STACK) |
| 3237 | public void setHfpEnabled(boolean enable) { |
| 3238 | AudioSystem.setParameters("hfp_enable=" + enable); |
| 3239 | } |
| 3240 | |
| 3241 | /** |
| 3242 | * @hide |
| 3243 | */ |
| 3244 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 3245 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_STACK) |
| 3246 | public void setHfpVolume(int volume) { |
| 3247 | AudioSystem.setParameters("hfp_volume=" + volume); |
| 3248 | } |
| 3249 | |
| 3250 | /** |
| 3251 | * @hide |
| 3252 | */ |
| 3253 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 3254 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_STACK) |
| 3255 | public void setHfpSamplingRate(int rate) { |
| 3256 | AudioSystem.setParameters("hfp_set_sampling_rate=" + rate); |
| 3257 | } |
| 3258 | |
| 3259 | /** |
| 3260 | * @hide |
| 3261 | */ |
| 3262 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 3263 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_STACK) |
| 3264 | public void setBluetoothHeadsetProperties(@NonNull String name, boolean hasNrecEnabled, |
| 3265 | boolean hasWbsEnabled) { |
| 3266 | AudioSystem.setParameters("bt_headset_name=" + name |
| 3267 | + ";bt_headset_nrec=" + (hasNrecEnabled ? "on" : "off") |
| 3268 | + ";bt_wbs=" + (hasWbsEnabled ? "on" : "off")); |
| 3269 | } |
| 3270 | |
| 3271 | /** |
| 3272 | * @hide |
| 3273 | */ |
| 3274 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 3275 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_STACK) |
| 3276 | public void setA2dpSuspended(boolean enable) { |
| 3277 | AudioSystem.setParameters("A2dpSuspended=" + enable); |
| 3278 | } |
| 3279 | |
| 3280 | /** |
John Spurlock | aac753d | 2013-02-22 16:33:32 -0500 | [diff] [blame] | 3281 | * Gets a variable number of parameter values from audio hardware. |
Eric Laurent | a553c25 | 2009-07-17 12:17:14 -0700 | [diff] [blame] | 3282 | * |
| 3283 | * @param keys list of parameters |
| 3284 | * @return list of parameters key value pairs in the form: |
| 3285 | * key1=value1;key2=value2;... |
| 3286 | */ |
| 3287 | public String getParameters(String keys) { |
| 3288 | return AudioSystem.getParameters(keys); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | /* Sound effect identifiers */ |
| 3292 | /** |
| 3293 | * Keyboard and direction pad click sound |
| 3294 | * @see #playSoundEffect(int) |
| 3295 | */ |
| 3296 | public static final int FX_KEY_CLICK = 0; |
| 3297 | /** |
| 3298 | * Focus has moved up |
| 3299 | * @see #playSoundEffect(int) |
| 3300 | */ |
| 3301 | public static final int FX_FOCUS_NAVIGATION_UP = 1; |
| 3302 | /** |
| 3303 | * Focus has moved down |
| 3304 | * @see #playSoundEffect(int) |
| 3305 | */ |
| 3306 | public static final int FX_FOCUS_NAVIGATION_DOWN = 2; |
| 3307 | /** |
| 3308 | * Focus has moved left |
| 3309 | * @see #playSoundEffect(int) |
| 3310 | */ |
| 3311 | public static final int FX_FOCUS_NAVIGATION_LEFT = 3; |
| 3312 | /** |
| 3313 | * Focus has moved right |
| 3314 | * @see #playSoundEffect(int) |
| 3315 | */ |
| 3316 | public static final int FX_FOCUS_NAVIGATION_RIGHT = 4; |
| 3317 | /** |
| 3318 | * IME standard keypress sound |
| 3319 | * @see #playSoundEffect(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3320 | */ |
| 3321 | public static final int FX_KEYPRESS_STANDARD = 5; |
| 3322 | /** |
| 3323 | * IME spacebar keypress sound |
| 3324 | * @see #playSoundEffect(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3325 | */ |
| 3326 | public static final int FX_KEYPRESS_SPACEBAR = 6; |
| 3327 | /** |
| 3328 | * IME delete keypress sound |
| 3329 | * @see #playSoundEffect(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3330 | */ |
| 3331 | public static final int FX_KEYPRESS_DELETE = 7; |
| 3332 | /** |
| 3333 | * IME return_keypress sound |
| 3334 | * @see #playSoundEffect(int) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3335 | */ |
| 3336 | public static final int FX_KEYPRESS_RETURN = 8; |
Justin Koh | cacfe69 | 2013-07-11 17:16:53 -0700 | [diff] [blame] | 3337 | |
| 3338 | /** |
| 3339 | * Invalid keypress sound |
| 3340 | * @see #playSoundEffect(int) |
| 3341 | */ |
| 3342 | public static final int FX_KEYPRESS_INVALID = 9; |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3343 | |
| 3344 | /** |
| 3345 | * Back sound |
| 3346 | * @see #playSoundEffect(int) |
| 3347 | */ |
| 3348 | public static final int FX_BACK = 10; |
| 3349 | |
| 3350 | /** |
| 3351 | * @hide Home sound |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3352 | * <p> |
| 3353 | * To be played by the framework when the home app becomes active if config_enableHomeSound is |
| 3354 | * set to true. This is currently only used on TV devices. |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3355 | * Note that this sound is only available if a sound file is specified in audio_assets.xml. |
| 3356 | * @see #playSoundEffect(int) |
| 3357 | */ |
| 3358 | public static final int FX_HOME = 11; |
| 3359 | |
| 3360 | /** |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3361 | * @hide Navigation repeat sound 1 |
| 3362 | * <p> |
| 3363 | * To be played by the framework when a focus navigation is repeatedly triggered |
| 3364 | * (e.g. due to long-pressing) and {@link #areNavigationRepeatSoundEffectsEnabled()} is true. |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3365 | * This is currently only used on TV devices. |
| 3366 | * Note that this sound is only available if a sound file is specified in audio_assets.xml |
| 3367 | * @see #playSoundEffect(int) |
| 3368 | */ |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3369 | public static final int FX_FOCUS_NAVIGATION_REPEAT_1 = 12; |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3370 | |
| 3371 | /** |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3372 | * @hide Navigation repeat sound 2 |
| 3373 | * <p> |
| 3374 | * To be played by the framework when a focus navigation is repeatedly triggered |
| 3375 | * (e.g. due to long-pressing) and {@link #areNavigationRepeatSoundEffectsEnabled()} is true. |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3376 | * This is currently only used on TV devices. |
| 3377 | * Note that this sound is only available if a sound file is specified in audio_assets.xml |
| 3378 | * @see #playSoundEffect(int) |
| 3379 | */ |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3380 | public static final int FX_FOCUS_NAVIGATION_REPEAT_2 = 13; |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3381 | |
| 3382 | /** |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3383 | * @hide Navigation repeat sound 3 |
| 3384 | * <p> |
| 3385 | * To be played by the framework when a focus navigation is repeatedly triggered |
| 3386 | * (e.g. due to long-pressing) and {@link #areNavigationRepeatSoundEffectsEnabled()} is true. |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3387 | * This is currently only used on TV devices. |
| 3388 | * Note that this sound is only available if a sound file is specified in audio_assets.xml |
| 3389 | * @see #playSoundEffect(int) |
| 3390 | */ |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3391 | public static final int FX_FOCUS_NAVIGATION_REPEAT_3 = 14; |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3392 | |
| 3393 | /** |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3394 | * @hide Navigation repeat sound 4 |
| 3395 | * <p> |
| 3396 | * To be played by the framework when a focus navigation is repeatedly triggered |
| 3397 | * (e.g. due to long-pressing) and {@link #areNavigationRepeatSoundEffectsEnabled()} is true. |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3398 | * This is currently only used on TV devices. |
| 3399 | * Note that this sound is only available if a sound file is specified in audio_assets.xml |
| 3400 | * @see #playSoundEffect(int) |
| 3401 | */ |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3402 | public static final int FX_FOCUS_NAVIGATION_REPEAT_4 = 15; |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3403 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3404 | /** |
| 3405 | * @hide Number of sound effects |
| 3406 | */ |
Mathew Inwood | 8e742f9 | 2020-10-27 11:47:29 +0000 | [diff] [blame] | 3407 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3408 | public static final int NUM_SOUND_EFFECTS = 16; |
| 3409 | |
Philip Junker | dfcb12b | 2021-03-17 19:38:50 +0100 | [diff] [blame] | 3410 | /** @hide */ |
| 3411 | @IntDef(prefix = { "FX_" }, value = { |
| 3412 | FX_KEY_CLICK, |
| 3413 | FX_FOCUS_NAVIGATION_UP, |
| 3414 | FX_FOCUS_NAVIGATION_DOWN, |
| 3415 | FX_FOCUS_NAVIGATION_LEFT, |
| 3416 | FX_FOCUS_NAVIGATION_RIGHT, |
| 3417 | FX_KEYPRESS_STANDARD, |
| 3418 | FX_KEYPRESS_SPACEBAR, |
| 3419 | FX_KEYPRESS_DELETE, |
| 3420 | FX_KEYPRESS_RETURN, |
| 3421 | FX_KEYPRESS_INVALID, |
| 3422 | FX_BACK |
| 3423 | }) |
| 3424 | @Retention(RetentionPolicy.SOURCE) |
| 3425 | public @interface SystemSoundEffect {} |
| 3426 | |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3427 | /** |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3428 | * @hide Number of FX_FOCUS_NAVIGATION_REPEAT_* sound effects |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3429 | */ |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3430 | public static final int NUM_NAVIGATION_REPEAT_SOUND_EFFECTS = 4; |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3431 | |
| 3432 | /** |
| 3433 | * @hide |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3434 | * @param n a value in [0, {@link #NUM_NAVIGATION_REPEAT_SOUND_EFFECTS}[ |
| 3435 | * @return The id of a navigation repeat sound effect or -1 if out of bounds |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3436 | */ |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3437 | public static int getNthNavigationRepeatSoundEffect(int n) { |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3438 | switch (n) { |
| 3439 | case 0: |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3440 | return FX_FOCUS_NAVIGATION_REPEAT_1; |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3441 | case 1: |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3442 | return FX_FOCUS_NAVIGATION_REPEAT_2; |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3443 | case 2: |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3444 | return FX_FOCUS_NAVIGATION_REPEAT_3; |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3445 | case 3: |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3446 | return FX_FOCUS_NAVIGATION_REPEAT_4; |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3447 | default: |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3448 | Log.w(TAG, "Invalid navigation repeat sound effect id: " + n); |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3449 | return -1; |
| 3450 | } |
| 3451 | } |
| 3452 | |
| 3453 | /** |
| 3454 | * @hide |
| 3455 | */ |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3456 | public void setNavigationRepeatSoundEffectsEnabled(boolean enabled) { |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3457 | try { |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3458 | getService().setNavigationRepeatSoundEffectsEnabled(enabled); |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3459 | } catch (RemoteException e) { |
| 3460 | |
| 3461 | } |
| 3462 | } |
| 3463 | |
| 3464 | /** |
| 3465 | * @hide |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3466 | * @return true if the navigation repeat sound effects are enabled |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3467 | */ |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3468 | public boolean areNavigationRepeatSoundEffectsEnabled() { |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3469 | try { |
Philip Junker | 6d9a096 | 2021-02-18 13:55:54 +0100 | [diff] [blame] | 3470 | return getService().areNavigationRepeatSoundEffectsEnabled(); |
Philip Junker | 7f1fdff | 2020-12-03 16:10:41 +0100 | [diff] [blame] | 3471 | } catch (RemoteException e) { |
| 3472 | throw e.rethrowFromSystemServer(); |
| 3473 | } |
| 3474 | } |
| 3475 | |
| 3476 | /** |
| 3477 | * @hide |
| 3478 | * @param enabled |
| 3479 | */ |
| 3480 | public void setHomeSoundEffectEnabled(boolean enabled) { |
| 3481 | try { |
| 3482 | getService().setHomeSoundEffectEnabled(enabled); |
| 3483 | } catch (RemoteException e) { |
| 3484 | |
| 3485 | } |
| 3486 | } |
| 3487 | |
| 3488 | /** |
| 3489 | * @hide |
| 3490 | * @return true if the home sound effect is enabled |
| 3491 | */ |
| 3492 | public boolean isHomeSoundEffectEnabled() { |
| 3493 | try { |
| 3494 | return getService().isHomeSoundEffectEnabled(); |
| 3495 | } catch (RemoteException e) { |
| 3496 | throw e.rethrowFromSystemServer(); |
| 3497 | } |
| 3498 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3499 | |
| 3500 | /** |
| 3501 | * Plays a sound effect (Key clicks, lid open/close...) |
Philip Junker | dfcb12b | 2021-03-17 19:38:50 +0100 | [diff] [blame] | 3502 | * @param effectType The type of sound effect. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3503 | * NOTE: This version uses the UI settings to determine |
| 3504 | * whether sounds are heard or not. |
| 3505 | */ |
Philip Junker | dfcb12b | 2021-03-17 19:38:50 +0100 | [diff] [blame] | 3506 | public void playSoundEffect(@SystemSoundEffect int effectType) { |
Oscar Azucena | 21f2c4d | 2021-10-26 18:06:54 -0700 | [diff] [blame] | 3507 | playSoundEffect(effectType, UserHandle.USER_CURRENT); |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | /** |
| 3511 | * Plays a sound effect (Key clicks, lid open/close...) |
Philip Junker | dfcb12b | 2021-03-17 19:38:50 +0100 | [diff] [blame] | 3512 | * @param effectType The type of sound effect. |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 3513 | * @param userId The current user to pull sound settings from |
| 3514 | * NOTE: This version uses the UI settings to determine |
| 3515 | * whether sounds are heard or not. |
| 3516 | * @hide |
| 3517 | */ |
Philip Junker | dfcb12b | 2021-03-17 19:38:50 +0100 | [diff] [blame] | 3518 | public void playSoundEffect(@SystemSoundEffect int effectType, int userId) { |
Jason Monk | 0c37ba3 | 2014-09-08 15:34:23 -0400 | [diff] [blame] | 3519 | if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) { |
| 3520 | return; |
| 3521 | } |
| 3522 | |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3523 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3524 | try { |
Oscar Azucena | 21f2c4d | 2021-10-26 18:06:54 -0700 | [diff] [blame] | 3525 | service.playSoundEffect(effectType, userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3526 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 3527 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3528 | } |
| 3529 | } |
| 3530 | |
| 3531 | /** |
| 3532 | * Plays a sound effect (Key clicks, lid open/close...) |
Philip Junker | dfcb12b | 2021-03-17 19:38:50 +0100 | [diff] [blame] | 3533 | * @param effectType The type of sound effect. |
Eric Laurent | a2ef57d | 2009-09-28 04:46:10 -0700 | [diff] [blame] | 3534 | * @param volume Sound effect volume. |
| 3535 | * The volume value is a raw scalar so UI controls should be scaled logarithmically. |
| 3536 | * If a volume of -1 is specified, the AudioManager.STREAM_MUSIC stream volume minus 3dB will be used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3537 | * NOTE: This version is for applications that have their own |
| 3538 | * settings panel for enabling and controlling volume. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3539 | */ |
Philip Junker | dfcb12b | 2021-03-17 19:38:50 +0100 | [diff] [blame] | 3540 | public void playSoundEffect(@SystemSoundEffect int effectType, float volume) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3541 | if (effectType < 0 || effectType >= NUM_SOUND_EFFECTS) { |
| 3542 | return; |
| 3543 | } |
| 3544 | |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3545 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3546 | try { |
| 3547 | service.playSoundEffectVolume(effectType, volume); |
| 3548 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 3549 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3554 | * Load Sound effects. |
| 3555 | * This method must be called when sound effects are enabled. |
| 3556 | */ |
| 3557 | public void loadSoundEffects() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3558 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3559 | try { |
| 3560 | service.loadSoundEffects(); |
| 3561 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 3562 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3563 | } |
| 3564 | } |
| 3565 | |
| 3566 | /** |
| 3567 | * Unload Sound effects. |
| 3568 | * This method can be called to free some memory when |
| 3569 | * sound effects are disabled. |
| 3570 | */ |
| 3571 | public void unloadSoundEffects() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3572 | final IAudioService service = getService(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3573 | try { |
| 3574 | service.unloadSoundEffects(); |
| 3575 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 3576 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3577 | } |
| 3578 | } |
| 3579 | |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 3580 | /** |
Andy Hung | 6983695 | 2020-03-26 01:00:15 -0700 | [diff] [blame] | 3581 | * @hide |
| 3582 | */ |
| 3583 | public static String audioFocusToString(int focus) { |
| 3584 | switch (focus) { |
| 3585 | case AUDIOFOCUS_NONE: |
| 3586 | return "AUDIOFOCUS_NONE"; |
| 3587 | case AUDIOFOCUS_GAIN: |
| 3588 | return "AUDIOFOCUS_GAIN"; |
| 3589 | case AUDIOFOCUS_GAIN_TRANSIENT: |
| 3590 | return "AUDIOFOCUS_GAIN_TRANSIENT"; |
| 3591 | case AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK: |
| 3592 | return "AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK"; |
| 3593 | case AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE: |
| 3594 | return "AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE"; |
| 3595 | case AUDIOFOCUS_LOSS: |
| 3596 | return "AUDIOFOCUS_LOSS"; |
| 3597 | case AUDIOFOCUS_LOSS_TRANSIENT: |
| 3598 | return "AUDIOFOCUS_LOSS_TRANSIENT"; |
| 3599 | case AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // Note CAN_DUCK not MAY_DUCK. |
| 3600 | return "AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK"; |
| 3601 | default: |
| 3602 | return "AUDIO_FOCUS_UNKNOWN(" + focus + ")"; |
| 3603 | } |
| 3604 | } |
| 3605 | |
| 3606 | /** |
Jean-Michel Trivi | 0f49f82 | 2017-02-16 14:36:43 -0800 | [diff] [blame] | 3607 | * Used to indicate no audio focus has been gained or lost, or requested. |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 3608 | */ |
| 3609 | public static final int AUDIOFOCUS_NONE = 0; |
| 3610 | |
| 3611 | /** |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3612 | * Used to indicate a gain of audio focus, or a request of audio focus, of unknown duration. |
Jean-Michel Trivi | 7f7e67f | 2010-03-31 11:30:24 -0700 | [diff] [blame] | 3613 | * @see OnAudioFocusChangeListener#onAudioFocusChange(int) |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3614 | * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int) |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3615 | */ |
| 3616 | public static final int AUDIOFOCUS_GAIN = 1; |
| 3617 | /** |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3618 | * Used to indicate a temporary gain or request of audio focus, anticipated to last a short |
| 3619 | * amount of time. Examples of temporary changes are the playback of driving directions, or an |
| 3620 | * event notification. |
Jean-Michel Trivi | 7f7e67f | 2010-03-31 11:30:24 -0700 | [diff] [blame] | 3621 | * @see OnAudioFocusChangeListener#onAudioFocusChange(int) |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3622 | * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int) |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3623 | */ |
| 3624 | public static final int AUDIOFOCUS_GAIN_TRANSIENT = 2; |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3625 | /** |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3626 | * Used to indicate a temporary request of audio focus, anticipated to last a short |
Jean-Michel Trivi | 983ac2b | 2010-03-19 12:09:25 -0700 | [diff] [blame] | 3627 | * amount of time, and where it is acceptable for other audio applications to keep playing |
| 3628 | * after having lowered their output level (also referred to as "ducking"). |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3629 | * Examples of temporary changes are the playback of driving directions where playback of music |
| 3630 | * in the background is acceptable. |
Jean-Michel Trivi | 7f7e67f | 2010-03-31 11:30:24 -0700 | [diff] [blame] | 3631 | * @see OnAudioFocusChangeListener#onAudioFocusChange(int) |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3632 | * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int) |
| 3633 | */ |
| 3634 | public static final int AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK = 3; |
| 3635 | /** |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 3636 | * Used to indicate a temporary request of audio focus, anticipated to last a short |
| 3637 | * amount of time, during which no other applications, or system components, should play |
| 3638 | * anything. Examples of exclusive and transient audio focus requests are voice |
| 3639 | * memo recording and speech recognition, during which the system shouldn't play any |
| 3640 | * notifications, and media playback should have paused. |
| 3641 | * @see #requestAudioFocus(OnAudioFocusChangeListener, int, int) |
| 3642 | */ |
| 3643 | public static final int AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE = 4; |
| 3644 | /** |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3645 | * Used to indicate a loss of audio focus of unknown duration. |
Jean-Michel Trivi | 7f7e67f | 2010-03-31 11:30:24 -0700 | [diff] [blame] | 3646 | * @see OnAudioFocusChangeListener#onAudioFocusChange(int) |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3647 | */ |
| 3648 | public static final int AUDIOFOCUS_LOSS = -1 * AUDIOFOCUS_GAIN; |
| 3649 | /** |
| 3650 | * Used to indicate a transient loss of audio focus. |
Jean-Michel Trivi | 7f7e67f | 2010-03-31 11:30:24 -0700 | [diff] [blame] | 3651 | * @see OnAudioFocusChangeListener#onAudioFocusChange(int) |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3652 | */ |
| 3653 | public static final int AUDIOFOCUS_LOSS_TRANSIENT = -1 * AUDIOFOCUS_GAIN_TRANSIENT; |
| 3654 | /** |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3655 | * Used to indicate a transient loss of audio focus where the loser of the audio focus can |
Jean-Michel Trivi | 983ac2b | 2010-03-19 12:09:25 -0700 | [diff] [blame] | 3656 | * lower its output volume if it wants to continue playing (also referred to as "ducking"), as |
| 3657 | * the new focus owner doesn't require others to be silent. |
Jean-Michel Trivi | 7f7e67f | 2010-03-31 11:30:24 -0700 | [diff] [blame] | 3658 | * @see OnAudioFocusChangeListener#onAudioFocusChange(int) |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3659 | */ |
| 3660 | public static final int AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK = |
| 3661 | -1 * AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3662 | |
| 3663 | /** |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3664 | * Interface definition for a callback to be invoked when the audio focus of the system is |
| 3665 | * updated. |
| 3666 | */ |
| 3667 | public interface OnAudioFocusChangeListener { |
| 3668 | /** |
| 3669 | * Called on the listener to notify it the audio focus for this listener has been changed. |
| 3670 | * The focusChange value indicates whether the focus was gained, |
| 3671 | * whether the focus was lost, and whether that loss is transient, or whether the new focus |
| 3672 | * holder will hold it for an unknown amount of time. |
Jean-Michel Trivi | 983ac2b | 2010-03-19 12:09:25 -0700 | [diff] [blame] | 3673 | * When losing focus, listeners can use the focus change information to decide what |
| 3674 | * behavior to adopt when losing focus. A music player could for instance elect to lower |
| 3675 | * the volume of its music stream (duck) for transient focus losses, and pause otherwise. |
| 3676 | * @param focusChange the type of focus change, one of {@link AudioManager#AUDIOFOCUS_GAIN}, |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3677 | * {@link AudioManager#AUDIOFOCUS_LOSS}, {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT} |
Jean-Michel Trivi | 983ac2b | 2010-03-19 12:09:25 -0700 | [diff] [blame] | 3678 | * and {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK}. |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3679 | */ |
Jean-Michel Trivi | 7f7e67f | 2010-03-31 11:30:24 -0700 | [diff] [blame] | 3680 | public void onAudioFocusChange(int focusChange); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3681 | } |
| 3682 | |
| 3683 | /** |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3684 | * Internal class to hold the AudioFocusRequest as well as the Handler for the callback |
| 3685 | */ |
| 3686 | private static class FocusRequestInfo { |
| 3687 | @NonNull final AudioFocusRequest mRequest; |
| 3688 | @Nullable final Handler mHandler; |
| 3689 | FocusRequestInfo(@NonNull AudioFocusRequest afr, @Nullable Handler handler) { |
| 3690 | mRequest = afr; |
| 3691 | mHandler = handler; |
| 3692 | } |
| 3693 | } |
| 3694 | |
| 3695 | /** |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3696 | * Map to convert focus event listener IDs, as used in the AudioService audio focus stack, |
| 3697 | * to actual listener objects. |
| 3698 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 3699 | @UnsupportedAppUsage |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3700 | private final ConcurrentHashMap<String, FocusRequestInfo> mAudioFocusIdListenerMap = |
| 3701 | new ConcurrentHashMap<String, FocusRequestInfo>(); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3702 | |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3703 | private FocusRequestInfo findFocusRequestInfo(String id) { |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 3704 | return mAudioFocusIdListenerMap.get(id); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3705 | } |
| 3706 | |
| 3707 | /** |
Jean-Michel Trivi | 3b61d2d | 2016-01-07 10:50:09 -0800 | [diff] [blame] | 3708 | * Handler for events (audio focus change, recording config change) coming from the |
| 3709 | * audio service. |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3710 | */ |
Jean-Michel Trivi | 3b61d2d | 2016-01-07 10:50:09 -0800 | [diff] [blame] | 3711 | private final ServiceEventHandlerDelegate mServiceEventHandlerDelegate = |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 3712 | new ServiceEventHandlerDelegate(null); |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3713 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3714 | /** |
Jean-Michel Trivi | 3b61d2d | 2016-01-07 10:50:09 -0800 | [diff] [blame] | 3715 | * Event types |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3716 | */ |
Jean-Michel Trivi | 3b61d2d | 2016-01-07 10:50:09 -0800 | [diff] [blame] | 3717 | private final static int MSSG_FOCUS_CHANGE = 0; |
| 3718 | private final static int MSSG_RECORDING_CONFIG_CHANGE = 1; |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 3719 | private final static int MSSG_PLAYBACK_CONFIG_CHANGE = 2; |
Jean-Michel Trivi | 3b61d2d | 2016-01-07 10:50:09 -0800 | [diff] [blame] | 3720 | |
| 3721 | /** |
| 3722 | * Helper class to handle the forwarding of audio service events to the appropriate listener |
| 3723 | */ |
| 3724 | private class ServiceEventHandlerDelegate { |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3725 | private final Handler mHandler; |
| 3726 | |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 3727 | ServiceEventHandlerDelegate(Handler handler) { |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3728 | Looper looper; |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 3729 | if (handler == null) { |
| 3730 | if ((looper = Looper.myLooper()) == null) { |
| 3731 | looper = Looper.getMainLooper(); |
| 3732 | } |
| 3733 | } else { |
| 3734 | looper = handler.getLooper(); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3735 | } |
| 3736 | |
| 3737 | if (looper != null) { |
Jean-Michel Trivi | 3b61d2d | 2016-01-07 10:50:09 -0800 | [diff] [blame] | 3738 | // implement the event handler delegate to receive events from audio service |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3739 | mHandler = new Handler(looper) { |
| 3740 | @Override |
| 3741 | public void handleMessage(Message msg) { |
Jean-Michel Trivi | 3b61d2d | 2016-01-07 10:50:09 -0800 | [diff] [blame] | 3742 | switch (msg.what) { |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 3743 | case MSSG_FOCUS_CHANGE: { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3744 | final FocusRequestInfo fri = findFocusRequestInfo((String)msg.obj); |
| 3745 | if (fri != null) { |
| 3746 | final OnAudioFocusChangeListener listener = |
| 3747 | fri.mRequest.getOnAudioFocusChangeListener(); |
| 3748 | if (listener != null) { |
| 3749 | Log.d(TAG, "dispatching onAudioFocusChange(" |
| 3750 | + msg.arg1 + ") to " + msg.obj); |
| 3751 | listener.onAudioFocusChange(msg.arg1); |
| 3752 | } |
Jean-Michel Trivi | 3b61d2d | 2016-01-07 10:50:09 -0800 | [diff] [blame] | 3753 | } |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 3754 | } break; |
| 3755 | case MSSG_RECORDING_CONFIG_CHANGE: { |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 3756 | final RecordConfigChangeCallbackData cbData = |
| 3757 | (RecordConfigChangeCallbackData) msg.obj; |
| 3758 | if (cbData.mCb != null) { |
Jean-Michel Trivi | e6a505b | 2016-04-01 09:56:28 -0700 | [diff] [blame] | 3759 | cbData.mCb.onRecordingConfigChanged(cbData.mConfigs); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 3760 | } |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 3761 | } break; |
| 3762 | case MSSG_PLAYBACK_CONFIG_CHANGE: { |
| 3763 | final PlaybackConfigChangeCallbackData cbData = |
| 3764 | (PlaybackConfigChangeCallbackData) msg.obj; |
| 3765 | if (cbData.mCb != null) { |
| 3766 | if (DEBUG) { |
| 3767 | Log.d(TAG, "dispatching onPlaybackConfigChanged()"); |
| 3768 | } |
| 3769 | cbData.mCb.onPlaybackConfigChanged(cbData.mConfigs); |
| 3770 | } |
| 3771 | } break; |
Jean-Michel Trivi | 3b61d2d | 2016-01-07 10:50:09 -0800 | [diff] [blame] | 3772 | default: |
| 3773 | Log.e(TAG, "Unknown event " + msg.what); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | }; |
| 3777 | } else { |
| 3778 | mHandler = null; |
| 3779 | } |
| 3780 | } |
| 3781 | |
| 3782 | Handler getHandler() { |
| 3783 | return mHandler; |
| 3784 | } |
| 3785 | } |
| 3786 | |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 3787 | private final IAudioFocusDispatcher mAudioFocusDispatcher = new IAudioFocusDispatcher.Stub() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3788 | @Override |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3789 | public void dispatchAudioFocusChange(int focusChange, String id) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3790 | final FocusRequestInfo fri = findFocusRequestInfo(id); |
| 3791 | if (fri != null) { |
| 3792 | final OnAudioFocusChangeListener listener = |
| 3793 | fri.mRequest.getOnAudioFocusChangeListener(); |
| 3794 | if (listener != null) { |
| 3795 | final Handler h = (fri.mHandler == null) ? |
| 3796 | mServiceEventHandlerDelegate.getHandler() : fri.mHandler; |
| 3797 | final Message m = h.obtainMessage( |
| 3798 | MSSG_FOCUS_CHANGE/*what*/, focusChange/*arg1*/, 0/*arg2 ignored*/, |
| 3799 | id/*obj*/); |
| 3800 | h.sendMessage(m); |
| 3801 | } |
| 3802 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3803 | } |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 3804 | |
| 3805 | @Override |
| 3806 | public void dispatchFocusResultFromExtPolicy(int requestResult, String clientId) { |
| 3807 | synchronized (mFocusRequestsLock) { |
| 3808 | // TODO use generation counter as the key instead |
| 3809 | final BlockingFocusResultReceiver focusReceiver = |
| 3810 | mFocusRequestsAwaitingResult.remove(clientId); |
| 3811 | if (focusReceiver != null) { |
| 3812 | focusReceiver.notifyResult(requestResult); |
| 3813 | } else { |
| 3814 | Log.e(TAG, "dispatchFocusResultFromExtPolicy found no result receiver"); |
| 3815 | } |
| 3816 | } |
| 3817 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3818 | }; |
| 3819 | |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 3820 | private String getIdForAudioFocusListener(OnAudioFocusChangeListener l) { |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3821 | if (l == null) { |
Jean-Michel Trivi | 308e9a5 | 2010-03-17 15:04:20 -0700 | [diff] [blame] | 3822 | return new String(this.toString()); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3823 | } else { |
| 3824 | return new String(this.toString() + l.toString()); |
| 3825 | } |
| 3826 | } |
| 3827 | |
| 3828 | /** |
Xavier Ducrohet | 5f53f08 | 2010-05-11 14:19:40 -0700 | [diff] [blame] | 3829 | * @hide |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3830 | * Registers a listener to be called when audio focus changes and keeps track of the associated |
| 3831 | * focus request (including Handler to use for the listener). |
| 3832 | * @param afr the full request parameters |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3833 | */ |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3834 | public void registerAudioFocusRequest(@NonNull AudioFocusRequest afr) { |
| 3835 | final Handler h = afr.getOnAudioFocusChangeListenerHandler(); |
| 3836 | final FocusRequestInfo fri = new FocusRequestInfo(afr, (h == null) ? null : |
| 3837 | new ServiceEventHandlerDelegate(h).getHandler()); |
| 3838 | final String key = getIdForAudioFocusListener(afr.getOnAudioFocusChangeListener()); |
| 3839 | mAudioFocusIdListenerMap.put(key, fri); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3840 | } |
| 3841 | |
| 3842 | /** |
Xavier Ducrohet | 5f53f08 | 2010-05-11 14:19:40 -0700 | [diff] [blame] | 3843 | * @hide |
Jean-Michel Trivi | 46b3a18 | 2010-05-11 11:22:54 -0700 | [diff] [blame] | 3844 | * Causes the specified listener to not be called anymore when focus is gained or lost. |
| 3845 | * @param l the listener to unregister. |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3846 | */ |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3847 | public void unregisterAudioFocusRequest(OnAudioFocusChangeListener l) { |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3848 | // remove locally |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 3849 | mAudioFocusIdListenerMap.remove(getIdForAudioFocusListener(l)); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | |
| 3853 | /** |
Jean-Michel Trivi | 46b3a18 | 2010-05-11 11:22:54 -0700 | [diff] [blame] | 3854 | * A failed focus change request. |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3855 | */ |
| 3856 | public static final int AUDIOFOCUS_REQUEST_FAILED = 0; |
| 3857 | /** |
Jean-Michel Trivi | 46b3a18 | 2010-05-11 11:22:54 -0700 | [diff] [blame] | 3858 | * A successful focus change request. |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3859 | */ |
| 3860 | public static final int AUDIOFOCUS_REQUEST_GRANTED = 1; |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 3861 | /** |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 3862 | * A focus change request whose granting is delayed: the request was successful, but the |
| 3863 | * requester will only be granted audio focus once the condition that prevented immediate |
| 3864 | * granting has ended. |
Jean-Michel Trivi | 3db31ac | 2017-02-24 11:40:37 -0800 | [diff] [blame] | 3865 | * See {@link #requestAudioFocus(AudioFocusRequest)} and |
| 3866 | * {@link AudioFocusRequest.Builder#setAcceptsDelayedFocusGain(boolean)} |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 3867 | */ |
| 3868 | public static final int AUDIOFOCUS_REQUEST_DELAYED = 2; |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3869 | |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 3870 | /** @hide */ |
| 3871 | @IntDef(flag = false, prefix = "AUDIOFOCUS_REQUEST", value = { |
| 3872 | AUDIOFOCUS_REQUEST_FAILED, |
| 3873 | AUDIOFOCUS_REQUEST_GRANTED, |
| 3874 | AUDIOFOCUS_REQUEST_DELAYED } |
| 3875 | ) |
| 3876 | @Retention(RetentionPolicy.SOURCE) |
| 3877 | public @interface FocusRequestResult {} |
| 3878 | |
| 3879 | /** |
| 3880 | * @hide |
| 3881 | * code returned when a synchronous focus request on the client-side is to be blocked |
| 3882 | * until the external audio focus policy decides on the response for the client |
| 3883 | */ |
| 3884 | public static final int AUDIOFOCUS_REQUEST_WAITING_FOR_EXT_POLICY = 100; |
| 3885 | |
| 3886 | /** |
| 3887 | * Timeout duration in ms when waiting on an external focus policy for the result for a |
| 3888 | * focus request |
| 3889 | */ |
| 3890 | private static final int EXT_FOCUS_POLICY_TIMEOUT_MS = 200; |
| 3891 | |
| 3892 | private static final String FOCUS_CLIENT_ID_STRING = "android_audio_focus_client_id"; |
| 3893 | |
| 3894 | private final Object mFocusRequestsLock = new Object(); |
| 3895 | /** |
| 3896 | * Map of all receivers of focus request results, one per unresolved focus request. |
| 3897 | * Receivers are added before sending the request to the external focus policy, |
| 3898 | * and are removed either after receiving the result, or after the timeout. |
| 3899 | * This variable is lazily initialized. |
| 3900 | */ |
| 3901 | @GuardedBy("mFocusRequestsLock") |
| 3902 | private HashMap<String, BlockingFocusResultReceiver> mFocusRequestsAwaitingResult; |
| 3903 | |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3904 | |
| 3905 | /** |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3906 | * Request audio focus. |
Jean-Michel Trivi | 46b3a18 | 2010-05-11 11:22:54 -0700 | [diff] [blame] | 3907 | * Send a request to obtain the audio focus |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3908 | * @param l the listener to be notified of audio focus changes |
| 3909 | * @param streamType the main audio stream type affected by the focus request |
| 3910 | * @param durationHint use {@link #AUDIOFOCUS_GAIN_TRANSIENT} to indicate this focus request |
| 3911 | * is temporary, and focus will be abandonned shortly. Examples of transient requests are |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3912 | * for the playback of driving directions, or notifications sounds. |
Jean-Michel Trivi | 983ac2b | 2010-03-19 12:09:25 -0700 | [diff] [blame] | 3913 | * Use {@link #AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK} to indicate also that it's ok for |
| 3914 | * the previous focus owner to keep playing if it ducks its audio output. |
Jean-Michel Trivi | 9171db2 | 2013-07-31 17:11:12 -0700 | [diff] [blame] | 3915 | * Alternatively use {@link #AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE} for a temporary request |
| 3916 | * that benefits from the system not playing disruptive sounds like notifications, for |
| 3917 | * usecases such as voice memo recording, or speech recognition. |
Jean-Michel Trivi | 078fd47 | 2010-03-18 16:51:04 -0700 | [diff] [blame] | 3918 | * Use {@link #AUDIOFOCUS_GAIN} for a focus request of unknown duration such |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3919 | * as the playback of a song or a video. |
| 3920 | * @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED} |
Jean-Michel Trivi | 36bdb66 | 2017-03-09 11:56:51 -0800 | [diff] [blame] | 3921 | * @deprecated use {@link #requestAudioFocus(AudioFocusRequest)} |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3922 | */ |
| 3923 | public int requestAudioFocus(OnAudioFocusChangeListener l, int streamType, int durationHint) { |
Jean-Michel Trivi | 36bdb66 | 2017-03-09 11:56:51 -0800 | [diff] [blame] | 3924 | PlayerBase.deprecateStreamTypeForPlayback(streamType, |
| 3925 | "AudioManager", "requestAudioFocus()"); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 3926 | int status = AUDIOFOCUS_REQUEST_FAILED; |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 3927 | |
| 3928 | try { |
| 3929 | // status is guaranteed to be either AUDIOFOCUS_REQUEST_FAILED or |
| 3930 | // AUDIOFOCUS_REQUEST_GRANTED as focus is requested without the |
| 3931 | // AUDIOFOCUS_FLAG_DELAY_OK flag |
| 3932 | status = requestAudioFocus(l, |
| 3933 | new AudioAttributes.Builder() |
| 3934 | .setInternalLegacyStreamType(streamType).build(), |
| 3935 | durationHint, |
| 3936 | 0 /* flags, legacy behavior */); |
| 3937 | } catch (IllegalArgumentException e) { |
| 3938 | Log.e(TAG, "Audio focus request denied due to ", e); |
| 3939 | } |
| 3940 | |
| 3941 | return status; |
| 3942 | } |
| 3943 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 3944 | // when adding new flags, add them to the relevant AUDIOFOCUS_FLAGS_APPS or SYSTEM masks |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 3945 | /** |
| 3946 | * @hide |
| 3947 | * Use this flag when requesting audio focus to indicate it is ok for the requester to not be |
| 3948 | * granted audio focus immediately (as indicated by {@link #AUDIOFOCUS_REQUEST_DELAYED}) when |
| 3949 | * the system is in a state where focus cannot change, but be granted focus later when |
| 3950 | * this condition ends. |
| 3951 | */ |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 3952 | @SystemApi |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 3953 | public static final int AUDIOFOCUS_FLAG_DELAY_OK = 0x1 << 0; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 3954 | /** |
| 3955 | * @hide |
| 3956 | * Use this flag when requesting audio focus to indicate that the requester |
| 3957 | * will pause its media playback (if applicable) when losing audio focus with |
| 3958 | * {@link #AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK}, rather than ducking. |
| 3959 | * <br>On some platforms, the ducking may be handled without the application being aware of it |
| 3960 | * (i.e. it will not transiently lose focus). For applications that for instance play spoken |
| 3961 | * content, such as audio book or podcast players, ducking may never be acceptable, and will |
| 3962 | * thus always pause. This flag enables them to be declared as such whenever they request focus. |
| 3963 | */ |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 3964 | @SystemApi |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 3965 | public static final int AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS = 0x1 << 1; |
| 3966 | /** |
| 3967 | * @hide |
| 3968 | * Use this flag to lock audio focus so granting is temporarily disabled. |
| 3969 | * <br>This flag can only be used by owners of a registered |
| 3970 | * {@link android.media.audiopolicy.AudioPolicy} in |
| 3971 | * {@link #requestAudioFocus(OnAudioFocusChangeListener, AudioAttributes, int, int, AudioPolicy)} |
| 3972 | */ |
| 3973 | @SystemApi |
| 3974 | public static final int AUDIOFOCUS_FLAG_LOCK = 0x1 << 2; |
Jean-Michel Trivi | 2860209 | 2021-03-24 11:14:47 -0700 | [diff] [blame] | 3975 | |
| 3976 | /** |
| 3977 | * @hide |
| 3978 | * flag set on test API calls, |
| 3979 | * see {@link #requestAudioFocusForTest(AudioFocusRequest, String, int, int)}, |
| 3980 | * note that it isn't used in conjunction with other flags, it is passed as the single |
| 3981 | * value for flags */ |
| 3982 | public static final int AUDIOFOCUS_FLAG_TEST = 0x1 << 3; |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 3983 | /** @hide */ |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 3984 | public static final int AUDIOFOCUS_FLAGS_APPS = AUDIOFOCUS_FLAG_DELAY_OK |
| 3985 | | AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS; |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 3986 | /** @hide */ |
| 3987 | public static final int AUDIOFOCUS_FLAGS_SYSTEM = AUDIOFOCUS_FLAG_DELAY_OK |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 3988 | | AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS | AUDIOFOCUS_FLAG_LOCK; |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 3989 | |
| 3990 | /** |
Jean-Michel Trivi | 3db31ac | 2017-02-24 11:40:37 -0800 | [diff] [blame] | 3991 | * Request audio focus. |
| 3992 | * See the {@link AudioFocusRequest} for information about the options available to configure |
| 3993 | * your request, and notification of focus gain and loss. |
| 3994 | * @param focusRequest a {@link AudioFocusRequest} instance used to configure how focus is |
| 3995 | * requested. |
| 3996 | * @return {@link #AUDIOFOCUS_REQUEST_FAILED}, {@link #AUDIOFOCUS_REQUEST_GRANTED} |
| 3997 | * or {@link #AUDIOFOCUS_REQUEST_DELAYED}. |
| 3998 | * <br>Note that the return value is never {@link #AUDIOFOCUS_REQUEST_DELAYED} when focus |
| 3999 | * is requested without building the {@link AudioFocusRequest} with |
| 4000 | * {@link AudioFocusRequest.Builder#setAcceptsDelayedFocusGain(boolean)} set to |
| 4001 | * {@code true}. |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4002 | * @throws NullPointerException if passed a null argument |
Jean-Michel Trivi | 3db31ac | 2017-02-24 11:40:37 -0800 | [diff] [blame] | 4003 | */ |
| 4004 | public int requestAudioFocus(@NonNull AudioFocusRequest focusRequest) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4005 | return requestAudioFocus(focusRequest, null /* no AudioPolicy*/); |
Jean-Michel Trivi | 3db31ac | 2017-02-24 11:40:37 -0800 | [diff] [blame] | 4006 | } |
| 4007 | |
| 4008 | /** |
| 4009 | * Abandon audio focus. Causes the previous focus owner, if any, to receive focus. |
| 4010 | * @param focusRequest the {@link AudioFocusRequest} that was used when requesting focus |
| 4011 | * with {@link #requestAudioFocus(AudioFocusRequest)}. |
| 4012 | * @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED} |
| 4013 | * @throws IllegalArgumentException if passed a null argument |
| 4014 | */ |
| 4015 | public int abandonAudioFocusRequest(@NonNull AudioFocusRequest focusRequest) { |
| 4016 | if (focusRequest == null) { |
| 4017 | throw new IllegalArgumentException("Illegal null AudioFocusRequest"); |
| 4018 | } |
| 4019 | return abandonAudioFocus(focusRequest.getOnAudioFocusChangeListener(), |
| 4020 | focusRequest.getAudioAttributes()); |
| 4021 | } |
| 4022 | |
| 4023 | /** |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4024 | * @hide |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4025 | * Request audio focus. |
| 4026 | * Send a request to obtain the audio focus. This method differs from |
| 4027 | * {@link #requestAudioFocus(OnAudioFocusChangeListener, int, int)} in that it can express |
| 4028 | * that the requester accepts delayed grants of audio focus. |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4029 | * @param l the listener to be notified of audio focus changes. It is not allowed to be null |
| 4030 | * when the request is flagged with {@link #AUDIOFOCUS_FLAG_DELAY_OK}. |
| 4031 | * @param requestAttributes non null {@link AudioAttributes} describing the main reason for |
| 4032 | * requesting audio focus. |
| 4033 | * @param durationHint use {@link #AUDIOFOCUS_GAIN_TRANSIENT} to indicate this focus request |
| 4034 | * is temporary, and focus will be abandonned shortly. Examples of transient requests are |
| 4035 | * for the playback of driving directions, or notifications sounds. |
| 4036 | * Use {@link #AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK} to indicate also that it's ok for |
| 4037 | * the previous focus owner to keep playing if it ducks its audio output. |
| 4038 | * Alternatively use {@link #AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE} for a temporary request |
| 4039 | * that benefits from the system not playing disruptive sounds like notifications, for |
| 4040 | * usecases such as voice memo recording, or speech recognition. |
| 4041 | * Use {@link #AUDIOFOCUS_GAIN} for a focus request of unknown duration such |
| 4042 | * as the playback of a song or a video. |
Jean-Michel Trivi | 26ae15f | 2017-08-25 19:20:49 -0700 | [diff] [blame] | 4043 | * @param flags 0 or a combination of {link #AUDIOFOCUS_FLAG_DELAY_OK}, |
| 4044 | * {@link #AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS} and {@link #AUDIOFOCUS_FLAG_LOCK}. |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4045 | * <br>Use 0 when not using any flags for the request, which behaves like |
| 4046 | * {@link #requestAudioFocus(OnAudioFocusChangeListener, int, int)}, where either audio |
| 4047 | * focus is granted immediately, or the grant request fails because the system is in a |
| 4048 | * state where focus cannot change (e.g. a phone call). |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4049 | * @return {@link #AUDIOFOCUS_REQUEST_FAILED}, {@link #AUDIOFOCUS_REQUEST_GRANTED} |
| 4050 | * or {@link #AUDIOFOCUS_REQUEST_DELAYED}. |
| 4051 | * The return value is never {@link #AUDIOFOCUS_REQUEST_DELAYED} when focus is requested |
| 4052 | * without the {@link #AUDIOFOCUS_FLAG_DELAY_OK} flag. |
| 4053 | * @throws IllegalArgumentException |
| 4054 | */ |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4055 | @SystemApi |
Jean-Michel Trivi | 26ae15f | 2017-08-25 19:20:49 -0700 | [diff] [blame] | 4056 | @RequiresPermission(android.Manifest.permission.MODIFY_PHONE_STATE) |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4057 | public int requestAudioFocus(OnAudioFocusChangeListener l, |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4058 | @NonNull AudioAttributes requestAttributes, |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4059 | int durationHint, |
| 4060 | int flags) throws IllegalArgumentException { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4061 | if (flags != (flags & AUDIOFOCUS_FLAGS_APPS)) { |
| 4062 | throw new IllegalArgumentException("Invalid flags 0x" |
| 4063 | + Integer.toHexString(flags).toUpperCase()); |
| 4064 | } |
| 4065 | return requestAudioFocus(l, requestAttributes, durationHint, |
| 4066 | flags & AUDIOFOCUS_FLAGS_APPS, |
| 4067 | null /* no AudioPolicy*/); |
| 4068 | } |
| 4069 | |
| 4070 | /** |
| 4071 | * @hide |
| 4072 | * Request or lock audio focus. |
| 4073 | * This method is to be used by system components that have registered an |
| 4074 | * {@link android.media.audiopolicy.AudioPolicy} to request audio focus, but also to "lock" it |
| 4075 | * so focus granting is temporarily disabled. |
| 4076 | * @param l see the description of the same parameter in |
| 4077 | * {@link #requestAudioFocus(OnAudioFocusChangeListener, AudioAttributes, int, int)} |
| 4078 | * @param requestAttributes non null {@link AudioAttributes} describing the main reason for |
| 4079 | * requesting audio focus. |
| 4080 | * @param durationHint see the description of the same parameter in |
| 4081 | * {@link #requestAudioFocus(OnAudioFocusChangeListener, AudioAttributes, int, int)} |
| 4082 | * @param flags 0 or a combination of {link #AUDIOFOCUS_FLAG_DELAY_OK}, |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 4083 | * {@link #AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS}, and {@link #AUDIOFOCUS_FLAG_LOCK}. |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4084 | * <br>Use 0 when not using any flags for the request, which behaves like |
| 4085 | * {@link #requestAudioFocus(OnAudioFocusChangeListener, int, int)}, where either audio |
| 4086 | * focus is granted immediately, or the grant request fails because the system is in a |
| 4087 | * state where focus cannot change (e.g. a phone call). |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4088 | * @param ap a registered {@link android.media.audiopolicy.AudioPolicy} instance when locking |
| 4089 | * focus, or null. |
| 4090 | * @return see the description of the same return value in |
| 4091 | * {@link #requestAudioFocus(OnAudioFocusChangeListener, AudioAttributes, int, int)} |
| 4092 | * @throws IllegalArgumentException |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4093 | * @deprecated use {@link #requestAudioFocus(AudioFocusRequest, AudioPolicy)} |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4094 | */ |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 4095 | @SystemApi |
Jean-Michel Trivi | 26ae15f | 2017-08-25 19:20:49 -0700 | [diff] [blame] | 4096 | @RequiresPermission(anyOf= { |
| 4097 | android.Manifest.permission.MODIFY_PHONE_STATE, |
| 4098 | android.Manifest.permission.MODIFY_AUDIO_ROUTING |
| 4099 | }) |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4100 | public int requestAudioFocus(OnAudioFocusChangeListener l, |
| 4101 | @NonNull AudioAttributes requestAttributes, |
| 4102 | int durationHint, |
| 4103 | int flags, |
| 4104 | AudioPolicy ap) throws IllegalArgumentException { |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4105 | // parameter checking |
| 4106 | if (requestAttributes == null) { |
| 4107 | throw new IllegalArgumentException("Illegal null AudioAttributes argument"); |
| 4108 | } |
Jean-Michel Trivi | 3db31ac | 2017-02-24 11:40:37 -0800 | [diff] [blame] | 4109 | if (!AudioFocusRequest.isValidFocusGain(durationHint)) { |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4110 | throw new IllegalArgumentException("Invalid duration hint"); |
Jean-Michel Trivi | 55d1bb3 | 2010-04-01 17:40:58 -0700 | [diff] [blame] | 4111 | } |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4112 | if (flags != (flags & AUDIOFOCUS_FLAGS_SYSTEM)) { |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4113 | throw new IllegalArgumentException("Illegal flags 0x" |
| 4114 | + Integer.toHexString(flags).toUpperCase()); |
| 4115 | } |
| 4116 | if (((flags & AUDIOFOCUS_FLAG_DELAY_OK) == AUDIOFOCUS_FLAG_DELAY_OK) && (l == null)) { |
| 4117 | throw new IllegalArgumentException( |
| 4118 | "Illegal null focus listener when flagged as accepting delayed focus grant"); |
| 4119 | } |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4120 | if (((flags & AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS) |
| 4121 | == AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS) && (l == null)) { |
| 4122 | throw new IllegalArgumentException( |
| 4123 | "Illegal null focus listener when flagged as pausing instead of ducking"); |
| 4124 | } |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4125 | if (((flags & AUDIOFOCUS_FLAG_LOCK) == AUDIOFOCUS_FLAG_LOCK) && (ap == null)) { |
| 4126 | throw new IllegalArgumentException( |
| 4127 | "Illegal null audio policy when locking audio focus"); |
| 4128 | } |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4129 | |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4130 | final AudioFocusRequest afr = new AudioFocusRequest.Builder(durationHint) |
Jean-Michel Trivi | 36728ce | 2017-05-01 12:33:40 -0700 | [diff] [blame] | 4131 | .setOnAudioFocusChangeListenerInt(l, null /* no Handler for this legacy API */) |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4132 | .setAudioAttributes(requestAttributes) |
| 4133 | .setAcceptsDelayedFocusGain((flags & AUDIOFOCUS_FLAG_DELAY_OK) |
| 4134 | == AUDIOFOCUS_FLAG_DELAY_OK) |
| 4135 | .setWillPauseWhenDucked((flags & AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS) |
| 4136 | == AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS) |
| 4137 | .setLocksFocus((flags & AUDIOFOCUS_FLAG_LOCK) == AUDIOFOCUS_FLAG_LOCK) |
| 4138 | .build(); |
| 4139 | return requestAudioFocus(afr, ap); |
| 4140 | } |
| 4141 | |
| 4142 | /** |
| 4143 | * @hide |
Jean-Michel Trivi | 2860209 | 2021-03-24 11:14:47 -0700 | [diff] [blame] | 4144 | * Test API to request audio focus for an arbitrary client operating from a (fake) given UID. |
| 4145 | * Used to simulate conditions of the test, not the behavior of the focus requester under test. |
| 4146 | * @param afr the parameters of the request |
| 4147 | * @param clientFakeId the identifier of the AudioManager the client would be requesting from |
| 4148 | * @param clientFakeUid the UID of the client, here an arbitrary int, |
| 4149 | * doesn't have to be a real UID |
| 4150 | * @param clientTargetSdk the target SDK used by the client |
| 4151 | * @return return code indicating status of the request |
| 4152 | */ |
| 4153 | @TestApi |
| 4154 | @RequiresPermission("android.permission.QUERY_AUDIO_STATE") |
| 4155 | public @FocusRequestResult int requestAudioFocusForTest(@NonNull AudioFocusRequest afr, |
| 4156 | @NonNull String clientFakeId, int clientFakeUid, int clientTargetSdk) { |
| 4157 | Objects.requireNonNull(afr); |
| 4158 | Objects.requireNonNull(clientFakeId); |
| 4159 | try { |
| 4160 | return getService().requestAudioFocusForTest(afr.getAudioAttributes(), |
| 4161 | afr.getFocusGain(), |
| 4162 | mICallBack, |
| 4163 | mAudioFocusDispatcher, |
| 4164 | clientFakeId, "com.android.test.fakeclient", clientFakeUid, clientTargetSdk); |
| 4165 | } catch (RemoteException e) { |
| 4166 | throw e.rethrowFromSystemServer(); |
| 4167 | } |
| 4168 | } |
| 4169 | |
| 4170 | /** |
| 4171 | * @hide |
| 4172 | * Test API to abandon audio focus for an arbitrary client. |
| 4173 | * Used to simulate conditions of the test, not the behavior of the focus requester under test. |
| 4174 | * @param afr the parameters used for the request |
| 4175 | * @param clientFakeId clientFakeId the identifier of the AudioManager from which the client |
| 4176 | * would be requesting |
| 4177 | * @return return code indicating status of the request |
| 4178 | */ |
| 4179 | @TestApi |
| 4180 | @RequiresPermission("android.permission.QUERY_AUDIO_STATE") |
| 4181 | public @FocusRequestResult int abandonAudioFocusForTest(@NonNull AudioFocusRequest afr, |
| 4182 | @NonNull String clientFakeId) { |
| 4183 | Objects.requireNonNull(afr); |
| 4184 | Objects.requireNonNull(clientFakeId); |
| 4185 | try { |
| 4186 | return getService().abandonAudioFocusForTest(mAudioFocusDispatcher, |
| 4187 | clientFakeId, afr.getAudioAttributes(), "com.android.test.fakeclient"); |
| 4188 | } catch (RemoteException e) { |
| 4189 | throw e.rethrowFromSystemServer(); |
| 4190 | } |
| 4191 | } |
| 4192 | |
| 4193 | /** |
| 4194 | * @hide |
| 4195 | * Return the duration of the fade out applied when a player of the given AudioAttributes |
| 4196 | * is losing audio focus |
| 4197 | * @param aa the AudioAttributes of the player losing focus with {@link #AUDIOFOCUS_LOSS} |
| 4198 | * @return a duration in ms, 0 indicates no fade out is applied |
| 4199 | */ |
| 4200 | @TestApi |
| 4201 | @RequiresPermission("android.permission.QUERY_AUDIO_STATE") |
| 4202 | public @IntRange(from = 0) long getFadeOutDurationOnFocusLossMillis(@NonNull AudioAttributes aa) |
| 4203 | { |
| 4204 | Objects.requireNonNull(aa); |
| 4205 | try { |
| 4206 | return getService().getFadeOutDurationOnFocusLossMillis(aa); |
| 4207 | } catch (RemoteException e) { |
| 4208 | throw e.rethrowFromSystemServer(); |
| 4209 | } |
| 4210 | } |
| 4211 | |
| 4212 | /** |
| 4213 | * @hide |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4214 | * Request or lock audio focus. |
| 4215 | * This method is to be used by system components that have registered an |
| 4216 | * {@link android.media.audiopolicy.AudioPolicy} to request audio focus, but also to "lock" it |
| 4217 | * so focus granting is temporarily disabled. |
| 4218 | * @param afr see the description of the same parameter in |
| 4219 | * {@link #requestAudioFocus(AudioFocusRequest)} |
| 4220 | * @param ap a registered {@link android.media.audiopolicy.AudioPolicy} instance when locking |
| 4221 | * focus, or null. |
| 4222 | * @return {@link #AUDIOFOCUS_REQUEST_FAILED}, {@link #AUDIOFOCUS_REQUEST_GRANTED} |
| 4223 | * or {@link #AUDIOFOCUS_REQUEST_DELAYED}. |
| 4224 | * @throws NullPointerException if the AudioFocusRequest is null |
| 4225 | * @throws IllegalArgumentException when trying to lock focus without an AudioPolicy |
| 4226 | */ |
| 4227 | @SystemApi |
Jean-Michel Trivi | 26ae15f | 2017-08-25 19:20:49 -0700 | [diff] [blame] | 4228 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4229 | public int requestAudioFocus(@NonNull AudioFocusRequest afr, @Nullable AudioPolicy ap) { |
| 4230 | if (afr == null) { |
| 4231 | throw new NullPointerException("Illegal null AudioFocusRequest"); |
| 4232 | } |
| 4233 | // this can only be checked now, not during the creation of the AudioFocusRequest instance |
| 4234 | if (afr.locksFocus() && ap == null) { |
| 4235 | throw new IllegalArgumentException( |
| 4236 | "Illegal null audio policy when locking audio focus"); |
| 4237 | } |
| 4238 | registerAudioFocusRequest(afr); |
| 4239 | final IAudioService service = getService(); |
| 4240 | final int status; |
Jean-Michel Trivi | 461922f | 2017-04-25 15:23:17 -0700 | [diff] [blame] | 4241 | int sdk; |
| 4242 | try { |
| 4243 | sdk = getContext().getApplicationInfo().targetSdkVersion; |
| 4244 | } catch (NullPointerException e) { |
| 4245 | // some tests don't have a Context |
| 4246 | sdk = Build.VERSION.SDK_INT; |
| 4247 | } |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 4248 | |
| 4249 | final String clientId = getIdForAudioFocusListener(afr.getOnAudioFocusChangeListener()); |
| 4250 | final BlockingFocusResultReceiver focusReceiver; |
| 4251 | synchronized (mFocusRequestsLock) { |
| 4252 | try { |
| 4253 | // TODO status contains result and generation counter for ext policy |
| 4254 | status = service.requestAudioFocus(afr.getAudioAttributes(), |
| 4255 | afr.getFocusGain(), mICallBack, |
| 4256 | mAudioFocusDispatcher, |
| 4257 | clientId, |
John Wu | 4f7e510 | 2021-06-22 17:29:11 +0000 | [diff] [blame] | 4258 | getContext().getOpPackageName() /* package name */, |
| 4259 | getContext().getAttributionTag(), |
| 4260 | afr.getFlags(), |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 4261 | ap != null ? ap.cb() : null, |
| 4262 | sdk); |
| 4263 | } catch (RemoteException e) { |
| 4264 | throw e.rethrowFromSystemServer(); |
| 4265 | } |
| 4266 | if (status != AudioManager.AUDIOFOCUS_REQUEST_WAITING_FOR_EXT_POLICY) { |
| 4267 | // default path with no external focus policy |
| 4268 | return status; |
| 4269 | } |
| 4270 | if (mFocusRequestsAwaitingResult == null) { |
| 4271 | mFocusRequestsAwaitingResult = |
| 4272 | new HashMap<String, BlockingFocusResultReceiver>(1); |
| 4273 | } |
| 4274 | focusReceiver = new BlockingFocusResultReceiver(clientId); |
| 4275 | mFocusRequestsAwaitingResult.put(clientId, focusReceiver); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4276 | } |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 4277 | focusReceiver.waitForResult(EXT_FOCUS_POLICY_TIMEOUT_MS); |
| 4278 | if (DEBUG && !focusReceiver.receivedResult()) { |
| 4279 | Log.e(TAG, "requestAudio response from ext policy timed out, denying request"); |
| 4280 | } |
| 4281 | synchronized (mFocusRequestsLock) { |
| 4282 | mFocusRequestsAwaitingResult.remove(clientId); |
| 4283 | } |
| 4284 | return focusReceiver.requestResult(); |
| 4285 | } |
| 4286 | |
| 4287 | // helper class that abstracts out the handling of spurious wakeups in Object.wait() |
| 4288 | private static final class SafeWaitObject { |
| 4289 | private boolean mQuit = false; |
| 4290 | |
| 4291 | public void safeNotify() { |
| 4292 | synchronized (this) { |
| 4293 | mQuit = true; |
| 4294 | this.notify(); |
| 4295 | } |
| 4296 | } |
| 4297 | |
| 4298 | public void safeWait(long millis) throws InterruptedException { |
| 4299 | final long timeOutTime = java.lang.System.currentTimeMillis() + millis; |
| 4300 | synchronized (this) { |
| 4301 | while (!mQuit) { |
| 4302 | final long timeToWait = timeOutTime - java.lang.System.currentTimeMillis(); |
| 4303 | if (timeToWait < 0) { break; } |
| 4304 | this.wait(timeToWait); |
| 4305 | } |
| 4306 | } |
| 4307 | } |
| 4308 | } |
| 4309 | |
| 4310 | private static final class BlockingFocusResultReceiver { |
| 4311 | private final SafeWaitObject mLock = new SafeWaitObject(); |
| 4312 | @GuardedBy("mLock") |
| 4313 | private boolean mResultReceived = false; |
| 4314 | // request denied by default (e.g. timeout) |
| 4315 | private int mFocusRequestResult = AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 4316 | private final String mFocusClientId; |
| 4317 | |
| 4318 | BlockingFocusResultReceiver(String clientId) { |
| 4319 | mFocusClientId = clientId; |
| 4320 | } |
| 4321 | |
| 4322 | boolean receivedResult() { return mResultReceived; } |
| 4323 | int requestResult() { return mFocusRequestResult; } |
| 4324 | |
| 4325 | void notifyResult(int requestResult) { |
| 4326 | synchronized (mLock) { |
| 4327 | mResultReceived = true; |
| 4328 | mFocusRequestResult = requestResult; |
| 4329 | mLock.safeNotify(); |
| 4330 | } |
| 4331 | } |
| 4332 | |
| 4333 | public void waitForResult(long timeOutMs) { |
| 4334 | synchronized (mLock) { |
| 4335 | if (mResultReceived) { |
| 4336 | // the result was received before waiting |
| 4337 | return; |
| 4338 | } |
| 4339 | try { |
| 4340 | mLock.safeWait(timeOutMs); |
| 4341 | } catch (InterruptedException e) { } |
| 4342 | } |
| 4343 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4344 | } |
| 4345 | |
Jean-Michel Trivi | 4dd3674 | 2012-01-24 09:52:39 -0800 | [diff] [blame] | 4346 | /** |
| 4347 | * @hide |
| 4348 | * Used internally by telephony package to request audio focus. Will cause the focus request |
| 4349 | * to be associated with the "voice communication" identifier only used in AudioService |
| 4350 | * to identify this use case. |
| 4351 | * @param streamType use STREAM_RING for focus requests when ringing, VOICE_CALL for |
| 4352 | * the establishment of the call |
| 4353 | * @param durationHint the type of focus request. AUDIOFOCUS_GAIN_TRANSIENT is recommended so |
| 4354 | * media applications resume after a call |
| 4355 | */ |
Mathew Inwood | 8e742f9 | 2020-10-27 11:47:29 +0000 | [diff] [blame] | 4356 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Jean-Michel Trivi | 4dd3674 | 2012-01-24 09:52:39 -0800 | [diff] [blame] | 4357 | public void requestAudioFocusForCall(int streamType, int durationHint) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4358 | final IAudioService service = getService(); |
Jean-Michel Trivi | 4dd3674 | 2012-01-24 09:52:39 -0800 | [diff] [blame] | 4359 | try { |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 4360 | service.requestAudioFocus(new AudioAttributes.Builder() |
| 4361 | .setInternalLegacyStreamType(streamType).build(), |
| 4362 | durationHint, mICallBack, null, |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4363 | AudioSystem.IN_VOICE_COMM_FOCUS_ID, |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 4364 | getContext().getOpPackageName(), |
John Wu | 4f7e510 | 2021-06-22 17:29:11 +0000 | [diff] [blame] | 4365 | getContext().getAttributionTag(), |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4366 | AUDIOFOCUS_FLAG_LOCK, |
Jean-Michel Trivi | 461922f | 2017-04-25 15:23:17 -0700 | [diff] [blame] | 4367 | null /* policy token */, 0 /* sdk n/a here*/); |
Jean-Michel Trivi | 4dd3674 | 2012-01-24 09:52:39 -0800 | [diff] [blame] | 4368 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 4369 | throw e.rethrowFromSystemServer(); |
Jean-Michel Trivi | 4dd3674 | 2012-01-24 09:52:39 -0800 | [diff] [blame] | 4370 | } |
| 4371 | } |
| 4372 | |
| 4373 | /** |
| 4374 | * @hide |
Jean-Michel Trivi | 99489cc | 2017-01-25 19:08:49 -0800 | [diff] [blame] | 4375 | * Return the volume ramping time for a sound to be played after the given focus request, |
| 4376 | * and to play a sound of the given attributes |
| 4377 | * @param focusGain |
| 4378 | * @param attr |
| 4379 | * @return |
| 4380 | */ |
| 4381 | public int getFocusRampTimeMs(int focusGain, AudioAttributes attr) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4382 | final IAudioService service = getService(); |
Jean-Michel Trivi | 99489cc | 2017-01-25 19:08:49 -0800 | [diff] [blame] | 4383 | try { |
| 4384 | return service.getFocusRampTimeMs(focusGain, attr); |
| 4385 | } catch (RemoteException e) { |
| 4386 | throw e.rethrowFromSystemServer(); |
| 4387 | } |
| 4388 | } |
| 4389 | |
| 4390 | /** |
| 4391 | * @hide |
Jean-Michel Trivi | e2d8aae | 2018-01-30 15:09:47 -0800 | [diff] [blame] | 4392 | * Set the result to the audio focus request received through |
| 4393 | * {@link AudioPolicyFocusListener#onAudioFocusRequest(AudioFocusInfo, int)}. |
| 4394 | * @param afi the information about the focus requester |
| 4395 | * @param requestResult the result to the focus request to be passed to the requester |
| 4396 | * @param ap a valid registered {@link AudioPolicy} configured as a focus policy. |
| 4397 | */ |
| 4398 | @SystemApi |
| 4399 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 4400 | public void setFocusRequestResult(@NonNull AudioFocusInfo afi, |
| 4401 | @FocusRequestResult int requestResult, @NonNull AudioPolicy ap) { |
| 4402 | if (afi == null) { |
| 4403 | throw new IllegalArgumentException("Illegal null AudioFocusInfo"); |
| 4404 | } |
| 4405 | if (ap == null) { |
| 4406 | throw new IllegalArgumentException("Illegal null AudioPolicy"); |
| 4407 | } |
| 4408 | final IAudioService service = getService(); |
| 4409 | try { |
| 4410 | service.setFocusRequestResultFromExtPolicy(afi, requestResult, ap.cb()); |
| 4411 | } catch (RemoteException e) { |
| 4412 | throw e.rethrowFromSystemServer(); |
| 4413 | } |
| 4414 | } |
| 4415 | |
| 4416 | /** |
| 4417 | * @hide |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 4418 | * Notifies an application with a focus listener of gain or loss of audio focus. |
| 4419 | * This method can only be used by owners of an {@link AudioPolicy} configured with |
| 4420 | * {@link AudioPolicy.Builder#setIsAudioFocusPolicy(boolean)} set to true. |
| 4421 | * @param afi the recipient of the focus change, that has previously requested audio focus, and |
| 4422 | * that was received by the {@code AudioPolicy} through |
| 4423 | * {@link AudioPolicy.AudioPolicyFocusListener#onAudioFocusRequest(AudioFocusInfo, int)}. |
| 4424 | * @param focusChange one of focus gain types ({@link #AUDIOFOCUS_GAIN}, |
| 4425 | * {@link #AUDIOFOCUS_GAIN_TRANSIENT}, {@link #AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK} or |
| 4426 | * {@link #AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE}) |
| 4427 | * or one of the focus loss types ({@link AudioManager#AUDIOFOCUS_LOSS}, |
| 4428 | * {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT}, |
| 4429 | * or {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK}). |
| 4430 | * <br>For the focus gain, the change type should be the same as the app requested. |
| 4431 | * @param ap a valid registered {@link AudioPolicy} configured as a focus policy. |
| 4432 | * @return {@link #AUDIOFOCUS_REQUEST_GRANTED} if the dispatch was successfully sent, or |
| 4433 | * {@link #AUDIOFOCUS_REQUEST_FAILED} if the focus client didn't have a listener, or |
| 4434 | * if there was an error sending the request. |
| 4435 | * @throws NullPointerException if the {@link AudioFocusInfo} or {@link AudioPolicy} are null. |
| 4436 | */ |
| 4437 | @SystemApi |
Jean-Michel Trivi | 26ae15f | 2017-08-25 19:20:49 -0700 | [diff] [blame] | 4438 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 4439 | public int dispatchAudioFocusChange(@NonNull AudioFocusInfo afi, int focusChange, |
| 4440 | @NonNull AudioPolicy ap) { |
| 4441 | if (afi == null) { |
| 4442 | throw new NullPointerException("Illegal null AudioFocusInfo"); |
| 4443 | } |
| 4444 | if (ap == null) { |
| 4445 | throw new NullPointerException("Illegal null AudioPolicy"); |
| 4446 | } |
| 4447 | final IAudioService service = getService(); |
| 4448 | try { |
| 4449 | return service.dispatchFocusChange(afi, focusChange, ap.cb()); |
| 4450 | } catch (RemoteException e) { |
| 4451 | throw e.rethrowFromSystemServer(); |
| 4452 | } |
| 4453 | } |
| 4454 | |
| 4455 | /** |
| 4456 | * @hide |
Jean-Michel Trivi | 4dd3674 | 2012-01-24 09:52:39 -0800 | [diff] [blame] | 4457 | * Used internally by telephony package to abandon audio focus, typically after a call or |
| 4458 | * when ringing ends and the call is rejected or not answered. |
| 4459 | * Should match one or more calls to {@link #requestAudioFocusForCall(int, int)}. |
| 4460 | */ |
Mathew Inwood | 8e742f9 | 2020-10-27 11:47:29 +0000 | [diff] [blame] | 4461 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Jean-Michel Trivi | 4dd3674 | 2012-01-24 09:52:39 -0800 | [diff] [blame] | 4462 | public void abandonAudioFocusForCall() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4463 | final IAudioService service = getService(); |
Jean-Michel Trivi | 4dd3674 | 2012-01-24 09:52:39 -0800 | [diff] [blame] | 4464 | try { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 4465 | service.abandonAudioFocus(null, AudioSystem.IN_VOICE_COMM_FOCUS_ID, |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 4466 | null /*AudioAttributes, legacy behavior*/, getContext().getOpPackageName()); |
Jean-Michel Trivi | 4dd3674 | 2012-01-24 09:52:39 -0800 | [diff] [blame] | 4467 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 4468 | throw e.rethrowFromSystemServer(); |
Jean-Michel Trivi | 4dd3674 | 2012-01-24 09:52:39 -0800 | [diff] [blame] | 4469 | } |
| 4470 | } |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4471 | |
| 4472 | /** |
Jean-Michel Trivi | 46b3a18 | 2010-05-11 11:22:54 -0700 | [diff] [blame] | 4473 | * Abandon audio focus. Causes the previous focus owner, if any, to receive focus. |
| 4474 | * @param l the listener with which focus was requested. |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4475 | * @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED} |
Jean-Michel Trivi | 36bdb66 | 2017-03-09 11:56:51 -0800 | [diff] [blame] | 4476 | * @deprecated use {@link #abandonAudioFocusRequest(AudioFocusRequest)} |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4477 | */ |
| 4478 | public int abandonAudioFocus(OnAudioFocusChangeListener l) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4479 | return abandonAudioFocus(l, null /*AudioAttributes, legacy behavior*/); |
| 4480 | } |
| 4481 | |
| 4482 | /** |
| 4483 | * @hide |
| 4484 | * Abandon audio focus. Causes the previous focus owner, if any, to receive focus. |
| 4485 | * @param l the listener with which focus was requested. |
| 4486 | * @param aa the {@link AudioAttributes} with which audio focus was requested |
| 4487 | * @return {@link #AUDIOFOCUS_REQUEST_FAILED} or {@link #AUDIOFOCUS_REQUEST_GRANTED} |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4488 | * @deprecated use {@link #abandonAudioFocusRequest(AudioFocusRequest)} |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4489 | */ |
| 4490 | @SystemApi |
Aurimas Liutikas | 1394a01 | 2020-11-12 18:26:09 -0800 | [diff] [blame] | 4491 | @SuppressLint("RequiresPermission") // no permission enforcement, but only "undoes" what would |
| 4492 | // have been done by a matching requestAudioFocus |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 4493 | public int abandonAudioFocus(OnAudioFocusChangeListener l, AudioAttributes aa) { |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4494 | int status = AUDIOFOCUS_REQUEST_FAILED; |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4495 | unregisterAudioFocusRequest(l); |
| 4496 | final IAudioService service = getService(); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4497 | try { |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 4498 | status = service.abandonAudioFocus(mAudioFocusDispatcher, |
Jean-Michel Trivi | 126cf03 | 2017-04-02 23:19:02 -0700 | [diff] [blame] | 4499 | getIdForAudioFocusListener(l), aa, getContext().getOpPackageName()); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4500 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 4501 | throw e.rethrowFromSystemServer(); |
Jean-Michel Trivi | d5176cf | 2010-01-28 11:56:42 -0800 | [diff] [blame] | 4502 | } |
| 4503 | return status; |
| 4504 | } |
| 4505 | |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 4506 | //==================================================================== |
| 4507 | // Remote Control |
| 4508 | /** |
Jean-Michel Trivi | 46b3a18 | 2010-05-11 11:22:54 -0700 | [diff] [blame] | 4509 | * Register a component to be the sole receiver of MEDIA_BUTTON intents. |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 4510 | * @param eventReceiver identifier of a {@link android.content.BroadcastReceiver} |
| 4511 | * that will receive the media button intent. This broadcast receiver must be declared |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 4512 | * in the application manifest. The package of the component must match that of |
| 4513 | * the context you're registering from. |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4514 | * @deprecated Use {@link MediaSession#setMediaButtonReceiver(PendingIntent)} instead. |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 4515 | */ |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4516 | @Deprecated |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 4517 | public void registerMediaButtonEventReceiver(ComponentName eventReceiver) { |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 4518 | if (eventReceiver == null) { |
| 4519 | return; |
| 4520 | } |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 4521 | if (!eventReceiver.getPackageName().equals(getContext().getPackageName())) { |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 4522 | Log.e(TAG, "registerMediaButtonEventReceiver() error: " + |
| 4523 | "receiver and context package names don't match"); |
| 4524 | return; |
| 4525 | } |
| 4526 | // construct a PendingIntent for the media button and register it |
| 4527 | Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); |
| 4528 | // the associated intent will be handled by the component being registered |
| 4529 | mediaButtonIntent.setComponent(eventReceiver); |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 4530 | PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
Jean-Michel Trivi | ec84dae | 2020-10-22 17:12:37 -0700 | [diff] [blame] | 4531 | 0/*requestCode, ignored*/, mediaButtonIntent, |
| 4532 | PendingIntent.FLAG_IMMUTABLE); |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 4533 | registerMediaButtonIntent(pi, eventReceiver); |
| 4534 | } |
| 4535 | |
| 4536 | /** |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4537 | * Register a component to be the sole receiver of MEDIA_BUTTON intents. This is like |
| 4538 | * {@link #registerMediaButtonEventReceiver(android.content.ComponentName)}, but allows |
| 4539 | * the buttons to go to any PendingIntent. Note that you should only use this form if |
| 4540 | * you know you will continue running for the full time until unregistering the |
| 4541 | * PendingIntent. |
| 4542 | * @param eventReceiver target that will receive media button intents. The PendingIntent |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4543 | * will be sent an {@link Intent#ACTION_MEDIA_BUTTON} event when a media button action |
| 4544 | * occurs, with {@link Intent#EXTRA_KEY_EVENT} added and holding the key code of the |
| 4545 | * media button that was pressed. |
| 4546 | * @deprecated Use {@link MediaSession#setMediaButtonReceiver(PendingIntent)} instead. |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4547 | */ |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4548 | @Deprecated |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4549 | public void registerMediaButtonEventReceiver(PendingIntent eventReceiver) { |
| 4550 | if (eventReceiver == null) { |
| 4551 | return; |
| 4552 | } |
| 4553 | registerMediaButtonIntent(eventReceiver, null); |
| 4554 | } |
| 4555 | |
| 4556 | /** |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 4557 | * @hide |
| 4558 | * no-op if (pi == null) or (eventReceiver == null) |
| 4559 | */ |
| 4560 | public void registerMediaButtonIntent(PendingIntent pi, ComponentName eventReceiver) { |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4561 | if (pi == null) { |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 4562 | Log.e(TAG, "Cannot call registerMediaButtonIntent() with a null parameter"); |
| 4563 | return; |
| 4564 | } |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 4565 | MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext()); |
| 4566 | helper.addMediaButtonListener(pi, eventReceiver, getContext()); |
Jean-Michel Trivi | 722b808 | 2012-05-15 15:18:33 -0700 | [diff] [blame] | 4567 | } |
| 4568 | |
| 4569 | /** |
Jean-Michel Trivi | 46b3a18 | 2010-05-11 11:22:54 -0700 | [diff] [blame] | 4570 | * Unregister the receiver of MEDIA_BUTTON intents. |
| 4571 | * @param eventReceiver identifier of a {@link android.content.BroadcastReceiver} |
| 4572 | * that was registered with {@link #registerMediaButtonEventReceiver(ComponentName)}. |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4573 | * @deprecated Use {@link MediaSession} instead. |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 4574 | */ |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4575 | @Deprecated |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 4576 | public void unregisterMediaButtonEventReceiver(ComponentName eventReceiver) { |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 4577 | if (eventReceiver == null) { |
| 4578 | return; |
| 4579 | } |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 4580 | // construct a PendingIntent for the media button and unregister it |
| 4581 | Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON); |
| 4582 | // the associated intent will be handled by the component being registered |
| 4583 | mediaButtonIntent.setComponent(eventReceiver); |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 4584 | PendingIntent pi = PendingIntent.getBroadcast(getContext(), |
Jean-Michel Trivi | ec84dae | 2020-10-22 17:12:37 -0700 | [diff] [blame] | 4585 | 0/*requestCode, ignored*/, mediaButtonIntent, |
| 4586 | PendingIntent.FLAG_IMMUTABLE); |
Jean-Michel Trivi | b839b83 | 2013-03-30 14:17:45 -0700 | [diff] [blame] | 4587 | unregisterMediaButtonIntent(pi); |
Jean-Michel Trivi | d327f21 | 2010-03-16 21:44:33 -0700 | [diff] [blame] | 4588 | } |
| 4589 | |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 4590 | /** |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4591 | * Unregister the receiver of MEDIA_BUTTON intents. |
| 4592 | * @param eventReceiver same PendingIntent that was registed with |
| 4593 | * {@link #registerMediaButtonEventReceiver(PendingIntent)}. |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4594 | * @deprecated Use {@link MediaSession} instead. |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4595 | */ |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4596 | @Deprecated |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4597 | public void unregisterMediaButtonEventReceiver(PendingIntent eventReceiver) { |
| 4598 | if (eventReceiver == null) { |
| 4599 | return; |
| 4600 | } |
Jean-Michel Trivi | b839b83 | 2013-03-30 14:17:45 -0700 | [diff] [blame] | 4601 | unregisterMediaButtonIntent(eventReceiver); |
Dianne Hackborn | 961cae9 | 2013-03-20 14:59:43 -0700 | [diff] [blame] | 4602 | } |
| 4603 | |
| 4604 | /** |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 4605 | * @hide |
| 4606 | */ |
Jean-Michel Trivi | b839b83 | 2013-03-30 14:17:45 -0700 | [diff] [blame] | 4607 | public void unregisterMediaButtonIntent(PendingIntent pi) { |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 4608 | MediaSessionLegacyHelper helper = MediaSessionLegacyHelper.getHelper(getContext()); |
RoboErik | 430fc48 | 2014-06-12 15:49:20 -0700 | [diff] [blame] | 4609 | helper.removeMediaButtonListener(pi); |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 4610 | } |
Jean-Michel Trivi | 178889e | 2011-08-15 17:17:52 -0700 | [diff] [blame] | 4611 | |
| 4612 | /** |
Jean-Michel Trivi | 178889e | 2011-08-15 17:17:52 -0700 | [diff] [blame] | 4613 | * Registers the remote control client for providing information to display on the remote |
| 4614 | * controls. |
Jean-Michel Trivi | 466ade5 | 2011-09-01 10:02:30 -0700 | [diff] [blame] | 4615 | * @param rcClient The remote control client from which remote controls will receive |
| 4616 | * information to display. |
| 4617 | * @see RemoteControlClient |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4618 | * @deprecated Use {@link MediaSession} instead. |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 4619 | */ |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4620 | @Deprecated |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4621 | public void registerRemoteControlClient(RemoteControlClient rcClient) { |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 4622 | if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) { |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 4623 | return; |
| 4624 | } |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 4625 | rcClient.registerWithSession(MediaSessionLegacyHelper.getHelper(getContext())); |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | /** |
Jean-Michel Trivi | fcd693a | 2011-08-11 13:53:55 -0700 | [diff] [blame] | 4629 | * Unregisters the remote control client that was providing information to display on the |
Jean-Michel Trivi | 466ade5 | 2011-09-01 10:02:30 -0700 | [diff] [blame] | 4630 | * remote controls. |
| 4631 | * @param rcClient The remote control client to unregister. |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4632 | * @see #registerRemoteControlClient(RemoteControlClient) |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4633 | * @deprecated Use {@link MediaSession} instead. |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 4634 | */ |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4635 | @Deprecated |
Jean-Michel Trivi | 4426e42 | 2011-08-18 19:16:47 -0700 | [diff] [blame] | 4636 | public void unregisterRemoteControlClient(RemoteControlClient rcClient) { |
Jean-Michel Trivi | f0cff04 | 2011-09-14 18:11:09 -0700 | [diff] [blame] | 4637 | if ((rcClient == null) || (rcClient.getRcMediaIntent() == null)) { |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 4638 | return; |
| 4639 | } |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 4640 | rcClient.unregisterWithSession(MediaSessionLegacyHelper.getHelper(getContext())); |
Jean-Michel Trivi | 8f61918 | 2011-07-21 15:10:10 -0700 | [diff] [blame] | 4641 | } |
| 4642 | |
Jean-Michel Trivi | 44413e5 | 2011-08-23 18:20:03 -0700 | [diff] [blame] | 4643 | /** |
RoboErik | a66c40b | 2014-08-15 15:21:41 -0700 | [diff] [blame] | 4644 | * Registers a {@link RemoteController} instance for it to receive media |
| 4645 | * metadata updates and playback state information from applications using |
| 4646 | * {@link RemoteControlClient}, and control their playback. |
| 4647 | * <p> |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 4648 | * Registration requires the {@link RemoteController.OnClientUpdateListener} listener to be |
RoboErik | a66c40b | 2014-08-15 15:21:41 -0700 | [diff] [blame] | 4649 | * one of the enabled notification listeners (see |
Jean-Michel Trivi | f108cdd9 | 2013-09-27 18:37:36 -0700 | [diff] [blame] | 4650 | * {@link android.service.notification.NotificationListenerService}). |
RoboErik | a66c40b | 2014-08-15 15:21:41 -0700 | [diff] [blame] | 4651 | * |
Jean-Michel Trivi | a83487e | 2013-09-17 21:19:30 -0700 | [diff] [blame] | 4652 | * @param rctlr the object to register. |
RoboErik | a66c40b | 2014-08-15 15:21:41 -0700 | [diff] [blame] | 4653 | * @return true if the {@link RemoteController} was successfully registered, |
| 4654 | * false if an error occurred, due to an internal system error, or |
| 4655 | * insufficient permissions. |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4656 | * @deprecated Use |
RoboErik | a66c40b | 2014-08-15 15:21:41 -0700 | [diff] [blame] | 4657 | * {@link MediaSessionManager#addOnActiveSessionsChangedListener(android.media.session.MediaSessionManager.OnActiveSessionsChangedListener, ComponentName)} |
| 4658 | * and {@link MediaController} instead. |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 4659 | */ |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4660 | @Deprecated |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 4661 | public boolean registerRemoteController(RemoteController rctlr) { |
| 4662 | if (rctlr == null) { |
| 4663 | return false; |
| 4664 | } |
RoboErik | 430fc48 | 2014-06-12 15:49:20 -0700 | [diff] [blame] | 4665 | rctlr.startListeningToSessions(); |
| 4666 | return true; |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 4667 | } |
| 4668 | |
| 4669 | /** |
RoboErik | a66c40b | 2014-08-15 15:21:41 -0700 | [diff] [blame] | 4670 | * Unregisters a {@link RemoteController}, causing it to no longer receive |
| 4671 | * media metadata and playback state information, and no longer be capable |
| 4672 | * of controlling playback. |
| 4673 | * |
Jean-Michel Trivi | a83487e | 2013-09-17 21:19:30 -0700 | [diff] [blame] | 4674 | * @param rctlr the object to unregister. |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4675 | * @deprecated Use |
RoboErik | a66c40b | 2014-08-15 15:21:41 -0700 | [diff] [blame] | 4676 | * {@link MediaSessionManager#removeOnActiveSessionsChangedListener(android.media.session.MediaSessionManager.OnActiveSessionsChangedListener)} |
| 4677 | * instead. |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 4678 | */ |
RoboErik | b214efb | 2014-07-24 13:20:30 -0700 | [diff] [blame] | 4679 | @Deprecated |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 4680 | public void unregisterRemoteController(RemoteController rctlr) { |
| 4681 | if (rctlr == null) { |
| 4682 | return; |
| 4683 | } |
RoboErik | 430fc48 | 2014-06-12 15:49:20 -0700 | [diff] [blame] | 4684 | rctlr.stopListeningToSessions(); |
Jean-Michel Trivi | 7ddd226 | 2013-09-01 18:06:45 -0700 | [diff] [blame] | 4685 | } |
| 4686 | |
Jean-Michel Trivi | c3c4bab | 2013-04-19 08:56:50 -0700 | [diff] [blame] | 4687 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 4688 | //==================================================================== |
| 4689 | // Audio policy |
Jean-Michel Trivi | c3c4bab | 2013-04-19 08:56:50 -0700 | [diff] [blame] | 4690 | /** |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4691 | * @hide |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4692 | * Register the given {@link AudioPolicy}. |
| 4693 | * This call is synchronous and blocks until the registration process successfully completed |
| 4694 | * or failed to complete. |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 4695 | * @param policy the non-null {@link AudioPolicy} to register. |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4696 | * @return {@link #ERROR} if there was an error communicating with the registration service |
| 4697 | * or if the user doesn't have the required |
| 4698 | * {@link android.Manifest.permission#MODIFY_AUDIO_ROUTING} permission, |
| 4699 | * {@link #SUCCESS} otherwise. |
| 4700 | */ |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 4701 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 4702 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 4703 | public int registerAudioPolicy(@NonNull AudioPolicy policy) { |
Robert Berry | 669be04 | 2019-01-22 18:48:35 -0500 | [diff] [blame] | 4704 | return registerAudioPolicyStatic(policy); |
| 4705 | } |
| 4706 | |
| 4707 | static int registerAudioPolicyStatic(@NonNull AudioPolicy policy) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4708 | if (policy == null) { |
| 4709 | throw new IllegalArgumentException("Illegal null AudioPolicy argument"); |
| 4710 | } |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4711 | final IAudioService service = getService(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4712 | try { |
Kevin Rocard | 2fe8b8b | 2019-02-20 11:25:23 -0800 | [diff] [blame] | 4713 | MediaProjection projection = policy.getMediaProjection(); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 4714 | String regId = service.registerAudioPolicy(policy.getConfig(), policy.cb(), |
Jean-Michel Trivi | ee7d245 | 2019-03-19 12:29:27 -0700 | [diff] [blame] | 4715 | policy.hasFocusListener(), policy.isFocusPolicy(), policy.isTestFocusPolicy(), |
| 4716 | policy.isVolumeController(), |
Kevin Rocard | 2fe8b8b | 2019-02-20 11:25:23 -0800 | [diff] [blame] | 4717 | projection == null ? null : projection.getProjection()); |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 4718 | if (regId == null) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4719 | return ERROR; |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 4720 | } else { |
| 4721 | policy.setRegistration(regId); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4722 | } |
Jean-Michel Trivi | 8fdb0d4 | 2014-07-16 19:08:37 -0700 | [diff] [blame] | 4723 | // successful registration |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4724 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 4725 | throw e.rethrowFromSystemServer(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4726 | } |
| 4727 | return SUCCESS; |
| 4728 | } |
| 4729 | |
| 4730 | /** |
| 4731 | * @hide |
Jean-Michel Trivi | cada372 | 2019-03-02 16:14:12 -0800 | [diff] [blame] | 4732 | * Unregisters an {@link AudioPolicy} asynchronously. |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 4733 | * @param policy the non-null {@link AudioPolicy} to unregister. |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4734 | */ |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 4735 | @SystemApi |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 4736 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
Jean-Michel Trivi | 1b3541d | 2014-11-25 12:53:41 -0800 | [diff] [blame] | 4737 | public void unregisterAudioPolicyAsync(@NonNull AudioPolicy policy) { |
Robert Berry | 669be04 | 2019-01-22 18:48:35 -0500 | [diff] [blame] | 4738 | unregisterAudioPolicyAsyncStatic(policy); |
| 4739 | } |
| 4740 | |
| 4741 | static void unregisterAudioPolicyAsyncStatic(@NonNull AudioPolicy policy) { |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4742 | if (policy == null) { |
| 4743 | throw new IllegalArgumentException("Illegal null AudioPolicy argument"); |
| 4744 | } |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4745 | final IAudioService service = getService(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4746 | try { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 4747 | service.unregisterAudioPolicyAsync(policy.cb()); |
Jean-Michel Trivi | f5f3e68 | 2020-07-29 15:45:48 -0700 | [diff] [blame] | 4748 | policy.reset(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4749 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 4750 | throw e.rethrowFromSystemServer(); |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4751 | } |
| 4752 | } |
| 4753 | |
Jean-Michel Trivi | cada372 | 2019-03-02 16:14:12 -0800 | [diff] [blame] | 4754 | /** |
| 4755 | * @hide |
| 4756 | * Unregisters an {@link AudioPolicy} synchronously. |
| 4757 | * This method also invalidates all {@link AudioRecord} and {@link AudioTrack} objects |
| 4758 | * associated with mixes of this policy. |
| 4759 | * @param policy the non-null {@link AudioPolicy} to unregister. |
| 4760 | */ |
| 4761 | @SystemApi |
| 4762 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 4763 | public void unregisterAudioPolicy(@NonNull AudioPolicy policy) { |
| 4764 | Preconditions.checkNotNull(policy, "Illegal null AudioPolicy argument"); |
| 4765 | final IAudioService service = getService(); |
| 4766 | try { |
| 4767 | policy.invalidateCaptorsAndInjectors(); |
| 4768 | service.unregisterAudioPolicy(policy.cb()); |
Jean-Michel Trivi | f5f3e68 | 2020-07-29 15:45:48 -0700 | [diff] [blame] | 4769 | policy.reset(); |
Jean-Michel Trivi | cada372 | 2019-03-02 16:14:12 -0800 | [diff] [blame] | 4770 | } catch (RemoteException e) { |
| 4771 | throw e.rethrowFromSystemServer(); |
| 4772 | } |
| 4773 | } |
| 4774 | |
Jean-Michel Trivi | 0c88f49 | 2019-04-12 15:43:56 -0700 | [diff] [blame] | 4775 | /** |
| 4776 | * @hide |
| 4777 | * @return true if an AudioPolicy was previously registered |
| 4778 | */ |
| 4779 | @TestApi |
| 4780 | public boolean hasRegisteredDynamicPolicy() { |
| 4781 | final IAudioService service = getService(); |
| 4782 | try { |
| 4783 | return service.hasRegisteredDynamicPolicy(); |
| 4784 | } catch (RemoteException e) { |
| 4785 | throw e.rethrowFromSystemServer(); |
| 4786 | } |
| 4787 | } |
| 4788 | |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 4789 | //==================================================================== |
| 4790 | // Notification of playback activity & playback configuration |
| 4791 | /** |
| 4792 | * Interface for receiving update notifications about the playback activity on the system. |
| 4793 | * Extend this abstract class and register it with |
| 4794 | * {@link AudioManager#registerAudioPlaybackCallback(AudioPlaybackCallback, Handler)} |
| 4795 | * to be notified. |
| 4796 | * Use {@link AudioManager#getActivePlaybackConfigurations()} to query the current |
| 4797 | * configuration. |
| 4798 | * @see AudioPlaybackConfiguration |
| 4799 | */ |
| 4800 | public static abstract class AudioPlaybackCallback { |
| 4801 | /** |
| 4802 | * Called whenever the playback activity and configuration has changed. |
| 4803 | * @param configs list containing the results of |
| 4804 | * {@link AudioManager#getActivePlaybackConfigurations()}. |
| 4805 | */ |
| 4806 | public void onPlaybackConfigChanged(List<AudioPlaybackConfiguration> configs) {} |
| 4807 | } |
| 4808 | |
| 4809 | private static class AudioPlaybackCallbackInfo { |
| 4810 | final AudioPlaybackCallback mCb; |
| 4811 | final Handler mHandler; |
| 4812 | AudioPlaybackCallbackInfo(AudioPlaybackCallback cb, Handler handler) { |
| 4813 | mCb = cb; |
| 4814 | mHandler = handler; |
| 4815 | } |
| 4816 | } |
| 4817 | |
| 4818 | private final static class PlaybackConfigChangeCallbackData { |
| 4819 | final AudioPlaybackCallback mCb; |
| 4820 | final List<AudioPlaybackConfiguration> mConfigs; |
| 4821 | |
| 4822 | PlaybackConfigChangeCallbackData(AudioPlaybackCallback cb, |
| 4823 | List<AudioPlaybackConfiguration> configs) { |
| 4824 | mCb = cb; |
| 4825 | mConfigs = configs; |
| 4826 | } |
| 4827 | } |
| 4828 | |
| 4829 | /** |
| 4830 | * Register a callback to be notified of audio playback changes through |
| 4831 | * {@link AudioPlaybackCallback} |
| 4832 | * @param cb non-null callback to register |
| 4833 | * @param handler the {@link Handler} object for the thread on which to execute |
| 4834 | * the callback. If <code>null</code>, the {@link Handler} associated with the main |
| 4835 | * {@link Looper} will be used. |
| 4836 | */ |
Jean-Michel Trivi | 1ec6618 | 2019-12-30 09:53:04 -0800 | [diff] [blame] | 4837 | public void registerAudioPlaybackCallback(@NonNull AudioPlaybackCallback cb, |
| 4838 | @Nullable Handler handler) |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 4839 | { |
| 4840 | if (cb == null) { |
| 4841 | throw new IllegalArgumentException("Illegal null AudioPlaybackCallback argument"); |
| 4842 | } |
| 4843 | |
| 4844 | synchronized(mPlaybackCallbackLock) { |
| 4845 | // lazy initialization of the list of playback callbacks |
| 4846 | if (mPlaybackCallbackList == null) { |
| 4847 | mPlaybackCallbackList = new ArrayList<AudioPlaybackCallbackInfo>(); |
| 4848 | } |
| 4849 | final int oldCbCount = mPlaybackCallbackList.size(); |
| 4850 | if (!hasPlaybackCallback_sync(cb)) { |
| 4851 | mPlaybackCallbackList.add(new AudioPlaybackCallbackInfo(cb, |
| 4852 | new ServiceEventHandlerDelegate(handler).getHandler())); |
| 4853 | final int newCbCount = mPlaybackCallbackList.size(); |
| 4854 | if ((oldCbCount == 0) && (newCbCount > 0)) { |
| 4855 | // register binder for callbacks |
| 4856 | try { |
| 4857 | getService().registerPlaybackCallback(mPlayCb); |
| 4858 | } catch (RemoteException e) { |
| 4859 | throw e.rethrowFromSystemServer(); |
| 4860 | } |
| 4861 | } |
| 4862 | } else { |
| 4863 | Log.w(TAG, "attempt to call registerAudioPlaybackCallback() on a previously" |
| 4864 | + "registered callback"); |
| 4865 | } |
| 4866 | } |
| 4867 | } |
| 4868 | |
| 4869 | /** |
| 4870 | * Unregister an audio playback callback previously registered with |
| 4871 | * {@link #registerAudioPlaybackCallback(AudioPlaybackCallback, Handler)}. |
| 4872 | * @param cb non-null callback to unregister |
| 4873 | */ |
| 4874 | public void unregisterAudioPlaybackCallback(@NonNull AudioPlaybackCallback cb) { |
| 4875 | if (cb == null) { |
| 4876 | throw new IllegalArgumentException("Illegal null AudioPlaybackCallback argument"); |
| 4877 | } |
| 4878 | synchronized(mPlaybackCallbackLock) { |
| 4879 | if (mPlaybackCallbackList == null) { |
| 4880 | Log.w(TAG, "attempt to call unregisterAudioPlaybackCallback() on a callback" |
| 4881 | + " that was never registered"); |
| 4882 | return; |
| 4883 | } |
| 4884 | final int oldCbCount = mPlaybackCallbackList.size(); |
| 4885 | if (removePlaybackCallback_sync(cb)) { |
| 4886 | final int newCbCount = mPlaybackCallbackList.size(); |
| 4887 | if ((oldCbCount > 0) && (newCbCount == 0)) { |
| 4888 | // unregister binder for callbacks |
| 4889 | try { |
| 4890 | getService().unregisterPlaybackCallback(mPlayCb); |
| 4891 | } catch (RemoteException e) { |
| 4892 | throw e.rethrowFromSystemServer(); |
| 4893 | } |
| 4894 | } |
| 4895 | } else { |
| 4896 | Log.w(TAG, "attempt to call unregisterAudioPlaybackCallback() on a callback" |
| 4897 | + " already unregistered or never registered"); |
| 4898 | } |
| 4899 | } |
| 4900 | } |
| 4901 | |
| 4902 | /** |
| 4903 | * Returns the current active audio playback configurations of the device |
| 4904 | * @return a non-null list of playback configurations. An empty list indicates there is no |
| 4905 | * playback active when queried. |
| 4906 | * @see AudioPlaybackConfiguration |
| 4907 | */ |
| 4908 | public @NonNull List<AudioPlaybackConfiguration> getActivePlaybackConfigurations() { |
| 4909 | final IAudioService service = getService(); |
| 4910 | try { |
| 4911 | return service.getActivePlaybackConfigurations(); |
| 4912 | } catch (RemoteException e) { |
| 4913 | throw e.rethrowFromSystemServer(); |
| 4914 | } |
| 4915 | } |
| 4916 | |
| 4917 | /** |
| 4918 | * All operations on this list are sync'd on mPlaybackCallbackLock. |
| 4919 | * List is lazy-initialized in |
| 4920 | * {@link #registerAudioPlaybackCallback(AudioPlaybackCallback, Handler)}. |
| 4921 | * List can be null. |
| 4922 | */ |
| 4923 | private List<AudioPlaybackCallbackInfo> mPlaybackCallbackList; |
| 4924 | private final Object mPlaybackCallbackLock = new Object(); |
| 4925 | |
| 4926 | /** |
| 4927 | * Must be called synchronized on mPlaybackCallbackLock |
| 4928 | */ |
| 4929 | private boolean hasPlaybackCallback_sync(@NonNull AudioPlaybackCallback cb) { |
| 4930 | if (mPlaybackCallbackList != null) { |
| 4931 | for (int i=0 ; i < mPlaybackCallbackList.size() ; i++) { |
| 4932 | if (cb.equals(mPlaybackCallbackList.get(i).mCb)) { |
| 4933 | return true; |
| 4934 | } |
| 4935 | } |
| 4936 | } |
| 4937 | return false; |
| 4938 | } |
| 4939 | |
| 4940 | /** |
| 4941 | * Must be called synchronized on mPlaybackCallbackLock |
| 4942 | */ |
| 4943 | private boolean removePlaybackCallback_sync(@NonNull AudioPlaybackCallback cb) { |
| 4944 | if (mPlaybackCallbackList != null) { |
| 4945 | for (int i=0 ; i < mPlaybackCallbackList.size() ; i++) { |
| 4946 | if (cb.equals(mPlaybackCallbackList.get(i).mCb)) { |
| 4947 | mPlaybackCallbackList.remove(i); |
| 4948 | return true; |
| 4949 | } |
| 4950 | } |
| 4951 | } |
| 4952 | return false; |
| 4953 | } |
| 4954 | |
| 4955 | private final IPlaybackConfigDispatcher mPlayCb = new IPlaybackConfigDispatcher.Stub() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 4956 | @Override |
Jean-Michel Trivi | 776a399 | 2017-09-12 16:45:34 -0700 | [diff] [blame] | 4957 | public void dispatchPlaybackConfigChange(List<AudioPlaybackConfiguration> configs, |
| 4958 | boolean flush) { |
| 4959 | if (flush) { |
| 4960 | Binder.flushPendingCommands(); |
| 4961 | } |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 4962 | synchronized(mPlaybackCallbackLock) { |
| 4963 | if (mPlaybackCallbackList != null) { |
| 4964 | for (int i=0 ; i < mPlaybackCallbackList.size() ; i++) { |
| 4965 | final AudioPlaybackCallbackInfo arci = mPlaybackCallbackList.get(i); |
| 4966 | if (arci.mHandler != null) { |
| 4967 | final Message m = arci.mHandler.obtainMessage( |
| 4968 | MSSG_PLAYBACK_CONFIG_CHANGE/*what*/, |
| 4969 | new PlaybackConfigChangeCallbackData(arci.mCb, configs)/*obj*/); |
| 4970 | arci.mHandler.sendMessage(m); |
| 4971 | } |
| 4972 | } |
| 4973 | } |
| 4974 | } |
| 4975 | } |
| 4976 | |
| 4977 | }; |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 4978 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 4979 | //==================================================================== |
Jean-Michel Trivi | 292a6a4 | 2016-12-01 08:32:15 -0800 | [diff] [blame] | 4980 | // Notification of recording activity & recording configuration |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 4981 | /** |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 4982 | * Interface for receiving update notifications about the recording configuration. Extend |
| 4983 | * this abstract class and register it with |
| 4984 | * {@link AudioManager#registerAudioRecordingCallback(AudioRecordingCallback, Handler)} |
| 4985 | * to be notified. |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame] | 4986 | * Use {@link AudioManager#getActiveRecordingConfigurations()} to query the current |
| 4987 | * configuration. |
Jean-Michel Trivi | 7872aff | 2016-07-06 14:34:52 -0700 | [diff] [blame] | 4988 | * @see AudioRecordingConfiguration |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 4989 | */ |
| 4990 | public static abstract class AudioRecordingCallback { |
| 4991 | /** |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 4992 | * Called whenever the device recording configuration has changed. |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 4993 | * @param configs list containing the results of |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame] | 4994 | * {@link AudioManager#getActiveRecordingConfigurations()}. |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 4995 | */ |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 4996 | public void onRecordingConfigChanged(List<AudioRecordingConfiguration> configs) {} |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 4997 | } |
| 4998 | |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 4999 | private static class AudioRecordingCallbackInfo { |
| 5000 | final AudioRecordingCallback mCb; |
| 5001 | final Handler mHandler; |
| 5002 | AudioRecordingCallbackInfo(AudioRecordingCallback cb, Handler handler) { |
| 5003 | mCb = cb; |
| 5004 | mHandler = handler; |
| 5005 | } |
| 5006 | } |
| 5007 | |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 5008 | private final static class RecordConfigChangeCallbackData { |
| 5009 | final AudioRecordingCallback mCb; |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 5010 | final List<AudioRecordingConfiguration> mConfigs; |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 5011 | |
| 5012 | RecordConfigChangeCallbackData(AudioRecordingCallback cb, |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 5013 | List<AudioRecordingConfiguration> configs) { |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 5014 | mCb = cb; |
| 5015 | mConfigs = configs; |
| 5016 | } |
| 5017 | } |
| 5018 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5019 | /** |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5020 | * Register a callback to be notified of audio recording changes through |
| 5021 | * {@link AudioRecordingCallback} |
| 5022 | * @param cb non-null callback to register |
| 5023 | * @param handler the {@link Handler} object for the thread on which to execute |
| 5024 | * the callback. If <code>null</code>, the {@link Handler} associated with the main |
| 5025 | * {@link Looper} will be used. |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5026 | */ |
Jean-Michel Trivi | 1ec6618 | 2019-12-30 09:53:04 -0800 | [diff] [blame] | 5027 | public void registerAudioRecordingCallback(@NonNull AudioRecordingCallback cb, |
| 5028 | @Nullable Handler handler) |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5029 | { |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5030 | if (cb == null) { |
| 5031 | throw new IllegalArgumentException("Illegal null AudioRecordingCallback argument"); |
| 5032 | } |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5033 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5034 | synchronized(mRecordCallbackLock) { |
| 5035 | // lazy initialization of the list of recording callbacks |
| 5036 | if (mRecordCallbackList == null) { |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5037 | mRecordCallbackList = new ArrayList<AudioRecordingCallbackInfo>(); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5038 | } |
| 5039 | final int oldCbCount = mRecordCallbackList.size(); |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5040 | if (!hasRecordCallback_sync(cb)) { |
| 5041 | mRecordCallbackList.add(new AudioRecordingCallbackInfo(cb, |
| 5042 | new ServiceEventHandlerDelegate(handler).getHandler())); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5043 | final int newCbCount = mRecordCallbackList.size(); |
| 5044 | if ((oldCbCount == 0) && (newCbCount > 0)) { |
| 5045 | // register binder for callbacks |
| 5046 | final IAudioService service = getService(); |
| 5047 | try { |
| 5048 | service.registerRecordingCallback(mRecCb); |
| 5049 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 5050 | throw e.rethrowFromSystemServer(); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5051 | } |
| 5052 | } |
| 5053 | } else { |
| 5054 | Log.w(TAG, "attempt to call registerAudioRecordingCallback() on a previously" |
| 5055 | + "registered callback"); |
| 5056 | } |
| 5057 | } |
| 5058 | } |
| 5059 | |
| 5060 | /** |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5061 | * Unregister an audio recording callback previously registered with |
| 5062 | * {@link #registerAudioRecordingCallback(AudioRecordingCallback, Handler)}. |
| 5063 | * @param cb non-null callback to unregister |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5064 | */ |
| 5065 | public void unregisterAudioRecordingCallback(@NonNull AudioRecordingCallback cb) { |
| 5066 | if (cb == null) { |
| 5067 | throw new IllegalArgumentException("Illegal null AudioRecordingCallback argument"); |
| 5068 | } |
| 5069 | synchronized(mRecordCallbackLock) { |
| 5070 | if (mRecordCallbackList == null) { |
| 5071 | return; |
| 5072 | } |
| 5073 | final int oldCbCount = mRecordCallbackList.size(); |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5074 | if (removeRecordCallback_sync(cb)) { |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5075 | final int newCbCount = mRecordCallbackList.size(); |
| 5076 | if ((oldCbCount > 0) && (newCbCount == 0)) { |
| 5077 | // unregister binder for callbacks |
| 5078 | final IAudioService service = getService(); |
| 5079 | try { |
| 5080 | service.unregisterRecordingCallback(mRecCb); |
| 5081 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 5082 | throw e.rethrowFromSystemServer(); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5083 | } |
| 5084 | } |
| 5085 | } else { |
| 5086 | Log.w(TAG, "attempt to call unregisterAudioRecordingCallback() on a callback" |
| 5087 | + " already unregistered or never registered"); |
| 5088 | } |
| 5089 | } |
| 5090 | } |
| 5091 | |
| 5092 | /** |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5093 | * Returns the current active audio recording configurations of the device. |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 5094 | * @return a non-null list of recording configurations. An empty list indicates there is |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5095 | * no recording active when queried. |
Jean-Michel Trivi | 7872aff | 2016-07-06 14:34:52 -0700 | [diff] [blame] | 5096 | * @see AudioRecordingConfiguration |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5097 | */ |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 5098 | public @NonNull List<AudioRecordingConfiguration> getActiveRecordingConfigurations() { |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5099 | final IAudioService service = getService(); |
| 5100 | try { |
Jean-Michel Trivi | 598c0c9 | 2016-03-07 14:13:03 -0800 | [diff] [blame] | 5101 | return service.getActiveRecordingConfigurations(); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5102 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 5103 | throw e.rethrowFromSystemServer(); |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5104 | } |
| 5105 | } |
| 5106 | |
| 5107 | /** |
| 5108 | * constants for the recording events, to keep in sync |
| 5109 | * with frameworks/av/include/media/AudioPolicy.h |
| 5110 | */ |
| 5111 | /** @hide */ |
Mikhail Naganov | a00883d | 2019-04-18 12:36:27 -0700 | [diff] [blame] | 5112 | public static final int RECORD_CONFIG_EVENT_NONE = -1; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5113 | /** @hide */ |
Mikhail Naganov | a00883d | 2019-04-18 12:36:27 -0700 | [diff] [blame] | 5114 | public static final int RECORD_CONFIG_EVENT_START = 0; |
| 5115 | /** @hide */ |
| 5116 | public static final int RECORD_CONFIG_EVENT_STOP = 1; |
| 5117 | /** @hide */ |
| 5118 | public static final int RECORD_CONFIG_EVENT_UPDATE = 2; |
| 5119 | /** @hide */ |
Mikhail Naganov | cfe4c26 | 2019-05-09 09:02:47 -0700 | [diff] [blame] | 5120 | public static final int RECORD_CONFIG_EVENT_RELEASE = 3; |
Mikhail Naganov | a00883d | 2019-04-18 12:36:27 -0700 | [diff] [blame] | 5121 | /** |
| 5122 | * keep in sync with frameworks/native/include/audiomanager/AudioManager.h |
| 5123 | */ |
| 5124 | /** @hide */ |
| 5125 | public static final int RECORD_RIID_INVALID = -1; |
| 5126 | /** @hide */ |
| 5127 | public static final int RECORDER_STATE_STARTED = 0; |
| 5128 | /** @hide */ |
| 5129 | public static final int RECORDER_STATE_STOPPED = 1; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5130 | |
| 5131 | /** |
| 5132 | * All operations on this list are sync'd on mRecordCallbackLock. |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5133 | * List is lazy-initialized in |
| 5134 | * {@link #registerAudioRecordingCallback(AudioRecordingCallback, Handler)}. |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5135 | * List can be null. |
| 5136 | */ |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5137 | private List<AudioRecordingCallbackInfo> mRecordCallbackList; |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5138 | private final Object mRecordCallbackLock = new Object(); |
| 5139 | |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5140 | /** |
| 5141 | * Must be called synchronized on mRecordCallbackLock |
| 5142 | */ |
| 5143 | private boolean hasRecordCallback_sync(@NonNull AudioRecordingCallback cb) { |
| 5144 | if (mRecordCallbackList != null) { |
| 5145 | for (int i=0 ; i < mRecordCallbackList.size() ; i++) { |
| 5146 | if (cb.equals(mRecordCallbackList.get(i).mCb)) { |
| 5147 | return true; |
| 5148 | } |
| 5149 | } |
| 5150 | } |
| 5151 | return false; |
| 5152 | } |
| 5153 | |
| 5154 | /** |
| 5155 | * Must be called synchronized on mRecordCallbackLock |
| 5156 | */ |
| 5157 | private boolean removeRecordCallback_sync(@NonNull AudioRecordingCallback cb) { |
| 5158 | if (mRecordCallbackList != null) { |
| 5159 | for (int i=0 ; i < mRecordCallbackList.size() ; i++) { |
| 5160 | if (cb.equals(mRecordCallbackList.get(i).mCb)) { |
| 5161 | mRecordCallbackList.remove(i); |
| 5162 | return true; |
| 5163 | } |
| 5164 | } |
| 5165 | } |
| 5166 | return false; |
| 5167 | } |
| 5168 | |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5169 | private final IRecordingConfigDispatcher mRecCb = new IRecordingConfigDispatcher.Stub() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 5170 | @Override |
Jean-Michel Trivi | f04fab1 | 2016-05-19 10:42:35 -0700 | [diff] [blame] | 5171 | public void dispatchRecordingConfigChange(List<AudioRecordingConfiguration> configs) { |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5172 | synchronized(mRecordCallbackLock) { |
| 5173 | if (mRecordCallbackList != null) { |
| 5174 | for (int i=0 ; i < mRecordCallbackList.size() ; i++) { |
| 5175 | final AudioRecordingCallbackInfo arci = mRecordCallbackList.get(i); |
| 5176 | if (arci.mHandler != null) { |
| 5177 | final Message m = arci.mHandler.obtainMessage( |
Jean-Michel Trivi | 28ff76b | 2016-03-02 09:36:30 -0800 | [diff] [blame] | 5178 | MSSG_RECORDING_CONFIG_CHANGE/*what*/, |
| 5179 | new RecordConfigChangeCallbackData(arci.mCb, configs)/*obj*/); |
Jean-Michel Trivi | b919f6f | 2016-01-12 18:27:17 -0800 | [diff] [blame] | 5180 | arci.mHandler.sendMessage(m); |
| 5181 | } |
| 5182 | } |
| 5183 | } |
| 5184 | } |
Jean-Michel Trivi | d3c71f0 | 2015-12-07 11:59:31 -0800 | [diff] [blame] | 5185 | } |
| 5186 | |
| 5187 | }; |
| 5188 | |
| 5189 | //===================================================================== |
| 5190 | |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 5191 | /** |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 5192 | * @hide |
| 5193 | * Reload audio settings. This method is called by Settings backup |
| 5194 | * agent when audio settings are restored and causes the AudioService |
| 5195 | * to read and apply restored settings. |
| 5196 | */ |
Mathew Inwood | 8e742f9 | 2020-10-27 11:47:29 +0000 | [diff] [blame] | 5197 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 5198 | public void reloadAudioSettings() { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 5199 | final IAudioService service = getService(); |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 5200 | try { |
| 5201 | service.reloadAudioSettings(); |
| 5202 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 5203 | throw e.rethrowFromSystemServer(); |
Eric Laurent | 4050c93 | 2009-07-08 02:52:14 -0700 | [diff] [blame] | 5204 | } |
| 5205 | } |
| 5206 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5207 | /** |
| 5208 | * {@hide} |
| 5209 | */ |
Glenn Kasten | 30c918c | 2011-11-10 17:56:41 -0800 | [diff] [blame] | 5210 | private final IBinder mICallBack = new Binder(); |
Glenn Kasten | 7f9b1fa | 2010-03-31 11:11:36 -0700 | [diff] [blame] | 5211 | |
| 5212 | /** |
| 5213 | * Checks whether the phone is in silent mode, with or without vibrate. |
| 5214 | * |
| 5215 | * @return true if phone is in silent mode, with or without vibrate. |
| 5216 | * |
| 5217 | * @see #getRingerMode() |
| 5218 | * |
| 5219 | * @hide pending API Council approval |
| 5220 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5221 | @UnsupportedAppUsage |
Glenn Kasten | 7f9b1fa | 2010-03-31 11:11:36 -0700 | [diff] [blame] | 5222 | public boolean isSilentMode() { |
| 5223 | int ringerMode = getRingerMode(); |
| 5224 | boolean silentMode = |
| 5225 | (ringerMode == RINGER_MODE_SILENT) || |
| 5226 | (ringerMode == RINGER_MODE_VIBRATE); |
| 5227 | return silentMode; |
| 5228 | } |
| 5229 | |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5230 | // This section re-defines new output device constants from AudioSystem, because the AudioSystem |
| 5231 | // class is not used by other parts of the framework, which instead use definitions and methods |
| 5232 | // from AudioManager. AudioSystem is an internal class used by AudioManager and AudioService. |
| 5233 | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5234 | /** @hide |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 5235 | * The audio device code for representing "no device." */ |
| 5236 | public static final int DEVICE_NONE = AudioSystem.DEVICE_NONE; |
| 5237 | /** @hide |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5238 | * The audio output device code for the small speaker at the front of the device used |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5239 | * when placing calls. Does not refer to an in-ear headphone without attached microphone, |
| 5240 | * such as earbuds, earphones, or in-ear monitors (IEM). Those would be handled as a |
| 5241 | * {@link #DEVICE_OUT_WIRED_HEADPHONE}. |
| 5242 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5243 | @UnsupportedAppUsage |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5244 | public static final int DEVICE_OUT_EARPIECE = AudioSystem.DEVICE_OUT_EARPIECE; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5245 | /** @hide |
| 5246 | * The audio output device code for the built-in speaker */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5247 | @UnsupportedAppUsage |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5248 | public static final int DEVICE_OUT_SPEAKER = AudioSystem.DEVICE_OUT_SPEAKER; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5249 | /** @hide |
| 5250 | * The audio output device code for a wired headset with attached microphone */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5251 | @UnsupportedAppUsage |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5252 | public static final int DEVICE_OUT_WIRED_HEADSET = AudioSystem.DEVICE_OUT_WIRED_HEADSET; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5253 | /** @hide |
| 5254 | * The audio output device code for a wired headphone without attached microphone */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5255 | @UnsupportedAppUsage |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5256 | public static final int DEVICE_OUT_WIRED_HEADPHONE = AudioSystem.DEVICE_OUT_WIRED_HEADPHONE; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5257 | /** @hide |
Paul McLean | 145c953 | 2017-08-04 11:12:19 -0600 | [diff] [blame] | 5258 | * The audio output device code for a USB headphone with attached microphone */ |
| 5259 | public static final int DEVICE_OUT_USB_HEADSET = AudioSystem.DEVICE_OUT_USB_HEADSET; |
| 5260 | /** @hide |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5261 | * The audio output device code for generic Bluetooth SCO, for voice */ |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5262 | public static final int DEVICE_OUT_BLUETOOTH_SCO = AudioSystem.DEVICE_OUT_BLUETOOTH_SCO; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5263 | /** @hide |
| 5264 | * The audio output device code for Bluetooth SCO Headset Profile (HSP) and |
| 5265 | * Hands-Free Profile (HFP), for voice |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5266 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5267 | @UnsupportedAppUsage |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5268 | public static final int DEVICE_OUT_BLUETOOTH_SCO_HEADSET = |
| 5269 | AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5270 | /** @hide |
| 5271 | * The audio output device code for Bluetooth SCO car audio, for voice */ |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5272 | public static final int DEVICE_OUT_BLUETOOTH_SCO_CARKIT = |
| 5273 | AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5274 | /** @hide |
| 5275 | * The audio output device code for generic Bluetooth A2DP, for music */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5276 | @UnsupportedAppUsage |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5277 | public static final int DEVICE_OUT_BLUETOOTH_A2DP = AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5278 | /** @hide |
| 5279 | * The audio output device code for Bluetooth A2DP headphones, for music */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5280 | @UnsupportedAppUsage |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5281 | public static final int DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES = |
| 5282 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5283 | /** @hide |
| 5284 | * The audio output device code for Bluetooth A2DP external speaker, for music */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5285 | @UnsupportedAppUsage |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5286 | public static final int DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER = |
| 5287 | AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5288 | /** @hide |
| 5289 | * The audio output device code for S/PDIF (legacy) or HDMI |
| 5290 | * Deprecated: replaced by {@link #DEVICE_OUT_HDMI} */ |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5291 | public static final int DEVICE_OUT_AUX_DIGITAL = AudioSystem.DEVICE_OUT_AUX_DIGITAL; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5292 | /** @hide |
| 5293 | * The audio output device code for HDMI */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5294 | @UnsupportedAppUsage |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5295 | public static final int DEVICE_OUT_HDMI = AudioSystem.DEVICE_OUT_HDMI; |
| 5296 | /** @hide |
| 5297 | * The audio output device code for an analog wired headset attached via a |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5298 | * docking station |
| 5299 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5300 | @UnsupportedAppUsage |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5301 | public static final int DEVICE_OUT_ANLG_DOCK_HEADSET = AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5302 | /** @hide |
| 5303 | * The audio output device code for a digital wired headset attached via a |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5304 | * docking station |
| 5305 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5306 | @UnsupportedAppUsage |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5307 | public static final int DEVICE_OUT_DGTL_DOCK_HEADSET = AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5308 | /** @hide |
| 5309 | * The audio output device code for a USB audio accessory. The accessory is in USB host |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5310 | * mode and the Android device in USB device mode |
| 5311 | */ |
| 5312 | public static final int DEVICE_OUT_USB_ACCESSORY = AudioSystem.DEVICE_OUT_USB_ACCESSORY; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5313 | /** @hide |
| 5314 | * The audio output device code for a USB audio device. The device is in USB device |
Eric Laurent | 59f4827 | 2012-04-05 19:42:21 -0700 | [diff] [blame] | 5315 | * mode and the Android device in USB host mode |
| 5316 | */ |
| 5317 | public static final int DEVICE_OUT_USB_DEVICE = AudioSystem.DEVICE_OUT_USB_DEVICE; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5318 | /** @hide |
| 5319 | * The audio output device code for projection output. |
| 5320 | */ |
| 5321 | public static final int DEVICE_OUT_REMOTE_SUBMIX = AudioSystem.DEVICE_OUT_REMOTE_SUBMIX; |
| 5322 | /** @hide |
| 5323 | * The audio output device code the telephony voice TX path. |
| 5324 | */ |
| 5325 | public static final int DEVICE_OUT_TELEPHONY_TX = AudioSystem.DEVICE_OUT_TELEPHONY_TX; |
| 5326 | /** @hide |
| 5327 | * The audio output device code for an analog jack with line impedance detected. |
| 5328 | */ |
| 5329 | public static final int DEVICE_OUT_LINE = AudioSystem.DEVICE_OUT_LINE; |
| 5330 | /** @hide |
| 5331 | * The audio output device code for HDMI Audio Return Channel. |
| 5332 | */ |
| 5333 | public static final int DEVICE_OUT_HDMI_ARC = AudioSystem.DEVICE_OUT_HDMI_ARC; |
| 5334 | /** @hide |
Kuowei Li | f898eae | 2020-10-27 16:41:18 +0800 | [diff] [blame] | 5335 | * The audio output device code for HDMI enhanced Audio Return Channel. |
| 5336 | */ |
| 5337 | public static final int DEVICE_OUT_HDMI_EARC = AudioSystem.DEVICE_OUT_HDMI_EARC; |
| 5338 | /** @hide |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5339 | * The audio output device code for S/PDIF digital connection. |
| 5340 | */ |
| 5341 | public static final int DEVICE_OUT_SPDIF = AudioSystem.DEVICE_OUT_SPDIF; |
| 5342 | /** @hide |
| 5343 | * The audio output device code for built-in FM transmitter. |
| 5344 | */ |
| 5345 | public static final int DEVICE_OUT_FM = AudioSystem.DEVICE_OUT_FM; |
| 5346 | /** @hide |
Eric Laurent | ca94af0 | 2020-08-07 12:30:44 -0700 | [diff] [blame] | 5347 | * The audio output device code for echo reference injection point. |
| 5348 | */ |
| 5349 | public static final int DEVICE_OUT_ECHO_CANCELLER = AudioSystem.DEVICE_OUT_ECHO_CANCELLER; |
| 5350 | /** @hide |
Eric Laurent | 6239d7e | 2020-08-07 10:58:14 -0700 | [diff] [blame] | 5351 | * The audio output device code for a BLE audio headset. |
| 5352 | */ |
| 5353 | public static final int DEVICE_OUT_BLE_HEADSET = AudioSystem.DEVICE_OUT_BLE_HEADSET; |
| 5354 | /** @hide |
| 5355 | * The audio output device code for a BLE audio speaker. |
| 5356 | */ |
| 5357 | public static final int DEVICE_OUT_BLE_SPEAKER = AudioSystem.DEVICE_OUT_BLE_SPEAKER; |
| 5358 | /** @hide |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5359 | * This is not used as a returned value from {@link #getDevicesForStream}, but could be |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5360 | * used in the future in a set method to select whatever default device is chosen by the |
| 5361 | * platform-specific implementation. |
| 5362 | */ |
| 5363 | public static final int DEVICE_OUT_DEFAULT = AudioSystem.DEVICE_OUT_DEFAULT; |
| 5364 | |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5365 | /** @hide |
| 5366 | * The audio input device code for default built-in microphone |
| 5367 | */ |
| 5368 | public static final int DEVICE_IN_BUILTIN_MIC = AudioSystem.DEVICE_IN_BUILTIN_MIC; |
| 5369 | /** @hide |
| 5370 | * The audio input device code for a Bluetooth SCO headset |
| 5371 | */ |
| 5372 | public static final int DEVICE_IN_BLUETOOTH_SCO_HEADSET = |
| 5373 | AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET; |
| 5374 | /** @hide |
| 5375 | * The audio input device code for wired headset microphone |
| 5376 | */ |
| 5377 | public static final int DEVICE_IN_WIRED_HEADSET = |
| 5378 | AudioSystem.DEVICE_IN_WIRED_HEADSET; |
| 5379 | /** @hide |
| 5380 | * The audio input device code for HDMI |
| 5381 | */ |
| 5382 | public static final int DEVICE_IN_HDMI = |
| 5383 | AudioSystem.DEVICE_IN_HDMI; |
| 5384 | /** @hide |
Nick Chalko | 2e1f76a | 2018-10-25 10:19:10 -0700 | [diff] [blame] | 5385 | * The audio input device code for HDMI ARC |
| 5386 | */ |
| 5387 | public static final int DEVICE_IN_HDMI_ARC = |
| 5388 | AudioSystem.DEVICE_IN_HDMI_ARC; |
| 5389 | |
| 5390 | /** @hide |
Kuowei Li | f898eae | 2020-10-27 16:41:18 +0800 | [diff] [blame] | 5391 | * The audio input device code for HDMI EARC |
| 5392 | */ |
| 5393 | public static final int DEVICE_IN_HDMI_EARC = |
| 5394 | AudioSystem.DEVICE_IN_HDMI_EARC; |
| 5395 | |
| 5396 | /** @hide |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5397 | * The audio input device code for telephony voice RX path |
| 5398 | */ |
| 5399 | public static final int DEVICE_IN_TELEPHONY_RX = |
| 5400 | AudioSystem.DEVICE_IN_TELEPHONY_RX; |
| 5401 | /** @hide |
| 5402 | * The audio input device code for built-in microphone pointing to the back |
| 5403 | */ |
| 5404 | public static final int DEVICE_IN_BACK_MIC = |
| 5405 | AudioSystem.DEVICE_IN_BACK_MIC; |
| 5406 | /** @hide |
| 5407 | * The audio input device code for analog from a docking station |
| 5408 | */ |
| 5409 | public static final int DEVICE_IN_ANLG_DOCK_HEADSET = |
| 5410 | AudioSystem.DEVICE_IN_ANLG_DOCK_HEADSET; |
| 5411 | /** @hide |
| 5412 | * The audio input device code for digital from a docking station |
| 5413 | */ |
| 5414 | public static final int DEVICE_IN_DGTL_DOCK_HEADSET = |
| 5415 | AudioSystem.DEVICE_IN_DGTL_DOCK_HEADSET; |
| 5416 | /** @hide |
| 5417 | * The audio input device code for a USB audio accessory. The accessory is in USB host |
| 5418 | * mode and the Android device in USB device mode |
| 5419 | */ |
| 5420 | public static final int DEVICE_IN_USB_ACCESSORY = |
| 5421 | AudioSystem.DEVICE_IN_USB_ACCESSORY; |
| 5422 | /** @hide |
| 5423 | * The audio input device code for a USB audio device. The device is in USB device |
| 5424 | * mode and the Android device in USB host mode |
| 5425 | */ |
| 5426 | public static final int DEVICE_IN_USB_DEVICE = |
| 5427 | AudioSystem.DEVICE_IN_USB_DEVICE; |
| 5428 | /** @hide |
| 5429 | * The audio input device code for a FM radio tuner |
| 5430 | */ |
| 5431 | public static final int DEVICE_IN_FM_TUNER = AudioSystem.DEVICE_IN_FM_TUNER; |
| 5432 | /** @hide |
| 5433 | * The audio input device code for a TV tuner |
| 5434 | */ |
| 5435 | public static final int DEVICE_IN_TV_TUNER = AudioSystem.DEVICE_IN_TV_TUNER; |
| 5436 | /** @hide |
| 5437 | * The audio input device code for an analog jack with line impedance detected |
| 5438 | */ |
| 5439 | public static final int DEVICE_IN_LINE = AudioSystem.DEVICE_IN_LINE; |
| 5440 | /** @hide |
| 5441 | * The audio input device code for a S/PDIF digital connection |
| 5442 | */ |
| 5443 | public static final int DEVICE_IN_SPDIF = AudioSystem.DEVICE_IN_SPDIF; |
Terry Heo | 112c19e | 2014-07-07 10:25:38 +0900 | [diff] [blame] | 5444 | /** @hide |
| 5445 | * The audio input device code for audio loopback |
| 5446 | */ |
| 5447 | public static final int DEVICE_IN_LOOPBACK = AudioSystem.DEVICE_IN_LOOPBACK; |
Eric Laurent | 6239d7e | 2020-08-07 10:58:14 -0700 | [diff] [blame] | 5448 | /** @hide |
Eric Laurent | ca94af0 | 2020-08-07 12:30:44 -0700 | [diff] [blame] | 5449 | * The audio input device code for an echo reference capture point. |
| 5450 | */ |
| 5451 | public static final int DEVICE_IN_ECHO_REFERENCE = AudioSystem.DEVICE_IN_ECHO_REFERENCE; |
| 5452 | /** @hide |
Eric Laurent | 6239d7e | 2020-08-07 10:58:14 -0700 | [diff] [blame] | 5453 | * The audio input device code for a BLE audio headset. |
| 5454 | */ |
| 5455 | public static final int DEVICE_IN_BLE_HEADSET = AudioSystem.DEVICE_IN_BLE_HEADSET; |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5456 | |
| 5457 | /** |
| 5458 | * Return true if the device code corresponds to an output device. |
| 5459 | * @hide |
| 5460 | */ |
| 5461 | public static boolean isOutputDevice(int device) |
| 5462 | { |
| 5463 | return (device & AudioSystem.DEVICE_BIT_IN) == 0; |
| 5464 | } |
| 5465 | |
| 5466 | /** |
| 5467 | * Return true if the device code corresponds to an input device. |
| 5468 | * @hide |
| 5469 | */ |
| 5470 | public static boolean isInputDevice(int device) |
| 5471 | { |
| 5472 | return (device & AudioSystem.DEVICE_BIT_IN) == AudioSystem.DEVICE_BIT_IN; |
| 5473 | } |
| 5474 | |
| 5475 | |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5476 | /** |
| 5477 | * Return the enabled devices for the specified output stream type. |
| 5478 | * |
| 5479 | * @param streamType The stream type to query. One of |
| 5480 | * {@link #STREAM_VOICE_CALL}, |
| 5481 | * {@link #STREAM_SYSTEM}, |
| 5482 | * {@link #STREAM_RING}, |
| 5483 | * {@link #STREAM_MUSIC}, |
| 5484 | * {@link #STREAM_ALARM}, |
| 5485 | * {@link #STREAM_NOTIFICATION}, |
Jean-Michel Trivi | 8e0bc78 | 2016-11-25 10:22:23 -0800 | [diff] [blame] | 5486 | * {@link #STREAM_DTMF}, |
| 5487 | * {@link #STREAM_ACCESSIBILITY}. |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5488 | * |
| 5489 | * @return The bit-mask "or" of audio output device codes for all enabled devices on this |
| 5490 | * stream. Zero or more of |
| 5491 | * {@link #DEVICE_OUT_EARPIECE}, |
| 5492 | * {@link #DEVICE_OUT_SPEAKER}, |
| 5493 | * {@link #DEVICE_OUT_WIRED_HEADSET}, |
| 5494 | * {@link #DEVICE_OUT_WIRED_HEADPHONE}, |
| 5495 | * {@link #DEVICE_OUT_BLUETOOTH_SCO}, |
| 5496 | * {@link #DEVICE_OUT_BLUETOOTH_SCO_HEADSET}, |
| 5497 | * {@link #DEVICE_OUT_BLUETOOTH_SCO_CARKIT}, |
| 5498 | * {@link #DEVICE_OUT_BLUETOOTH_A2DP}, |
| 5499 | * {@link #DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES}, |
| 5500 | * {@link #DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER}, |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5501 | * {@link #DEVICE_OUT_HDMI}, |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5502 | * {@link #DEVICE_OUT_ANLG_DOCK_HEADSET}, |
| 5503 | * {@link #DEVICE_OUT_DGTL_DOCK_HEADSET}. |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5504 | * {@link #DEVICE_OUT_USB_ACCESSORY}. |
| 5505 | * {@link #DEVICE_OUT_USB_DEVICE}. |
| 5506 | * {@link #DEVICE_OUT_REMOTE_SUBMIX}. |
| 5507 | * {@link #DEVICE_OUT_TELEPHONY_TX}. |
| 5508 | * {@link #DEVICE_OUT_LINE}. |
| 5509 | * {@link #DEVICE_OUT_HDMI_ARC}. |
Kuowei Li | f898eae | 2020-10-27 16:41:18 +0800 | [diff] [blame] | 5510 | * {@link #DEVICE_OUT_HDMI_EARC}. |
Eric Laurent | 948d327 | 2014-05-16 15:18:45 -0700 | [diff] [blame] | 5511 | * {@link #DEVICE_OUT_SPDIF}. |
| 5512 | * {@link #DEVICE_OUT_FM}. |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5513 | * {@link #DEVICE_OUT_DEFAULT} is not used here. |
| 5514 | * |
| 5515 | * The implementation may support additional device codes beyond those listed, so |
| 5516 | * the application should ignore any bits which it does not recognize. |
| 5517 | * Note that the information may be imprecise when the implementation |
| 5518 | * cannot distinguish whether a particular device is enabled. |
| 5519 | * |
| 5520 | * {@hide} |
| 5521 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5522 | @UnsupportedAppUsage |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5523 | public int getDevicesForStream(int streamType) { |
| 5524 | switch (streamType) { |
| 5525 | case STREAM_VOICE_CALL: |
| 5526 | case STREAM_SYSTEM: |
| 5527 | case STREAM_RING: |
| 5528 | case STREAM_MUSIC: |
| 5529 | case STREAM_ALARM: |
| 5530 | case STREAM_NOTIFICATION: |
| 5531 | case STREAM_DTMF: |
Jean-Michel Trivi | 8e0bc78 | 2016-11-25 10:22:23 -0800 | [diff] [blame] | 5532 | case STREAM_ACCESSIBILITY: |
Eric Laurent | 89c3a97 | 2020-12-16 15:57:56 +0100 | [diff] [blame] | 5533 | final IAudioService service = getService(); |
| 5534 | try { |
| 5535 | return service.getDevicesForStream(streamType); |
| 5536 | } catch (RemoteException e) { |
| 5537 | throw e.rethrowFromSystemServer(); |
| 5538 | } |
Glenn Kasten | 8b4b97a | 2011-02-04 13:54:26 -0800 | [diff] [blame] | 5539 | default: |
| 5540 | return 0; |
| 5541 | } |
| 5542 | } |
| 5543 | |
Jean-Michel Trivi | 56b97b7 | 2020-01-07 14:18:05 -0800 | [diff] [blame] | 5544 | /** |
| 5545 | * @hide |
| 5546 | * Get the audio devices that would be used for the routing of the given audio attributes. |
| 5547 | * @param attributes the {@link AudioAttributes} for which the routing is being queried |
| 5548 | * @return an empty list if there was an issue with the request, a list of audio devices |
| 5549 | * otherwise (typically one device, except for duplicated paths). |
| 5550 | */ |
| 5551 | @SystemApi |
kholoud mohamed | 3783921 | 2021-03-15 16:49:06 +0000 | [diff] [blame] | 5552 | @RequiresPermission(anyOf = { |
| 5553 | android.Manifest.permission.MODIFY_AUDIO_ROUTING, |
| 5554 | android.Manifest.permission.QUERY_AUDIO_STATE |
| 5555 | }) |
Jean-Michel Trivi | 0e87bca | 2020-02-18 14:30:29 -0800 | [diff] [blame] | 5556 | public @NonNull List<AudioDeviceAttributes> getDevicesForAttributes( |
Jean-Michel Trivi | 56b97b7 | 2020-01-07 14:18:05 -0800 | [diff] [blame] | 5557 | @NonNull AudioAttributes attributes) { |
| 5558 | Objects.requireNonNull(attributes); |
| 5559 | final IAudioService service = getService(); |
| 5560 | try { |
| 5561 | return service.getDevicesForAttributes(attributes); |
| 5562 | } catch (RemoteException e) { |
| 5563 | throw e.rethrowFromSystemServer(); |
| 5564 | } |
| 5565 | } |
| 5566 | |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5567 | /** |
| 5568 | * @hide |
Madhava Srinivasan | b5ed600 | 2020-06-11 21:49:24 +0000 | [diff] [blame] | 5569 | * Volume behavior for an audio device that has no particular volume behavior set. Invalid as |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5570 | * an argument to {@link #setDeviceVolumeBehavior(AudioDeviceAttributes, int)} and should not |
| 5571 | * be returned by {@link #getDeviceVolumeBehavior(AudioDeviceAttributes)}. |
Madhava Srinivasan | b5ed600 | 2020-06-11 21:49:24 +0000 | [diff] [blame] | 5572 | */ |
| 5573 | public static final int DEVICE_VOLUME_BEHAVIOR_UNSET = -1; |
| 5574 | /** |
| 5575 | * @hide |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5576 | * Volume behavior for an audio device where a software attenuation is applied |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5577 | * @see #setDeviceVolumeBehavior(AudioDeviceAttributes, int) |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5578 | */ |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5579 | @SystemApi |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5580 | public static final int DEVICE_VOLUME_BEHAVIOR_VARIABLE = 0; |
| 5581 | /** |
| 5582 | * @hide |
| 5583 | * Volume behavior for an audio device where the volume is always set to provide no attenuation |
| 5584 | * nor gain (e.g. unit gain). |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5585 | * @see #setDeviceVolumeBehavior(AudioDeviceAttributes, int) |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5586 | */ |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5587 | @SystemApi |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5588 | public static final int DEVICE_VOLUME_BEHAVIOR_FULL = 1; |
| 5589 | /** |
| 5590 | * @hide |
| 5591 | * Volume behavior for an audio device where the volume is either set to muted, or to provide |
| 5592 | * no attenuation nor gain (e.g. unit gain). |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5593 | * @see #setDeviceVolumeBehavior(AudioDeviceAttributes, int) |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5594 | */ |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5595 | @SystemApi |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5596 | public static final int DEVICE_VOLUME_BEHAVIOR_FIXED = 2; |
| 5597 | /** |
| 5598 | * @hide |
| 5599 | * Volume behavior for an audio device where no software attenuation is applied, and |
| 5600 | * the volume is kept synchronized between the host and the device itself through a |
| 5601 | * device-specific protocol such as BT AVRCP. |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5602 | * @see #setDeviceVolumeBehavior(AudioDeviceAttributes, int) |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5603 | */ |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5604 | @SystemApi |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5605 | public static final int DEVICE_VOLUME_BEHAVIOR_ABSOLUTE = 3; |
| 5606 | /** |
| 5607 | * @hide |
| 5608 | * Volume behavior for an audio device where no software attenuation is applied, and |
| 5609 | * the volume is kept synchronized between the host and the device itself through a |
| 5610 | * device-specific protocol (such as for hearing aids), based on the audio mode (e.g. |
| 5611 | * normal vs in phone call). |
| 5612 | * @see #setMode(int) |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5613 | * @see #setDeviceVolumeBehavior(AudioDeviceAttributes, int) |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5614 | */ |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5615 | @SystemApi |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5616 | public static final int DEVICE_VOLUME_BEHAVIOR_ABSOLUTE_MULTI_MODE = 4; |
| 5617 | |
| 5618 | /** @hide */ |
| 5619 | @IntDef({ |
| 5620 | DEVICE_VOLUME_BEHAVIOR_VARIABLE, |
| 5621 | DEVICE_VOLUME_BEHAVIOR_FULL, |
| 5622 | DEVICE_VOLUME_BEHAVIOR_FIXED, |
| 5623 | DEVICE_VOLUME_BEHAVIOR_ABSOLUTE, |
| 5624 | DEVICE_VOLUME_BEHAVIOR_ABSOLUTE_MULTI_MODE, |
| 5625 | }) |
| 5626 | @Retention(RetentionPolicy.SOURCE) |
| 5627 | public @interface DeviceVolumeBehavior {} |
| 5628 | |
Madhava Srinivasan | b5ed600 | 2020-06-11 21:49:24 +0000 | [diff] [blame] | 5629 | /** @hide */ |
| 5630 | @IntDef({ |
| 5631 | DEVICE_VOLUME_BEHAVIOR_UNSET, |
| 5632 | DEVICE_VOLUME_BEHAVIOR_VARIABLE, |
| 5633 | DEVICE_VOLUME_BEHAVIOR_FULL, |
| 5634 | DEVICE_VOLUME_BEHAVIOR_FIXED, |
| 5635 | DEVICE_VOLUME_BEHAVIOR_ABSOLUTE, |
| 5636 | DEVICE_VOLUME_BEHAVIOR_ABSOLUTE_MULTI_MODE, |
| 5637 | }) |
| 5638 | @Retention(RetentionPolicy.SOURCE) |
| 5639 | public @interface DeviceVolumeBehaviorState {} |
| 5640 | |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5641 | /** |
| 5642 | * @hide |
| 5643 | * Throws IAE on an invalid volume behavior value |
| 5644 | * @param volumeBehavior behavior value to check |
| 5645 | */ |
| 5646 | public static void enforceValidVolumeBehavior(int volumeBehavior) { |
| 5647 | switch (volumeBehavior) { |
| 5648 | case DEVICE_VOLUME_BEHAVIOR_VARIABLE: |
| 5649 | case DEVICE_VOLUME_BEHAVIOR_FULL: |
| 5650 | case DEVICE_VOLUME_BEHAVIOR_FIXED: |
| 5651 | case DEVICE_VOLUME_BEHAVIOR_ABSOLUTE: |
| 5652 | case DEVICE_VOLUME_BEHAVIOR_ABSOLUTE_MULTI_MODE: |
| 5653 | return; |
| 5654 | default: |
| 5655 | throw new IllegalArgumentException("Illegal volume behavior " + volumeBehavior); |
| 5656 | } |
| 5657 | } |
| 5658 | |
| 5659 | /** |
| 5660 | * @hide |
| 5661 | * Sets the volume behavior for an audio output device. |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5662 | * @see #DEVICE_VOLUME_BEHAVIOR_VARIABLE |
| 5663 | * @see #DEVICE_VOLUME_BEHAVIOR_FULL |
| 5664 | * @see #DEVICE_VOLUME_BEHAVIOR_FIXED |
| 5665 | * @see #DEVICE_VOLUME_BEHAVIOR_ABSOLUTE |
| 5666 | * @see #DEVICE_VOLUME_BEHAVIOR_ABSOLUTE_MULTI_MODE |
| 5667 | * @param device the device to be affected |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5668 | * @param deviceVolumeBehavior one of the device behaviors |
| 5669 | */ |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5670 | @SystemApi |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5671 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 5672 | public void setDeviceVolumeBehavior(@NonNull AudioDeviceAttributes device, |
| 5673 | @DeviceVolumeBehavior int deviceVolumeBehavior) { |
| 5674 | // verify arguments (validity of device type is enforced in server) |
| 5675 | Objects.requireNonNull(device); |
| 5676 | enforceValidVolumeBehavior(deviceVolumeBehavior); |
| 5677 | // communicate with service |
| 5678 | final IAudioService service = getService(); |
| 5679 | try { |
| 5680 | service.setDeviceVolumeBehavior(device, deviceVolumeBehavior, |
| 5681 | mApplicationContext.getOpPackageName()); |
| 5682 | } catch (RemoteException e) { |
| 5683 | throw e.rethrowFromSystemServer(); |
| 5684 | } |
| 5685 | } |
| 5686 | |
| 5687 | /** |
| 5688 | * @hide |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5689 | * Returns the volume device behavior for the given audio device |
| 5690 | * @param device the audio device |
| 5691 | * @return the volume behavior for the device |
| 5692 | */ |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5693 | @SystemApi |
kholoud mohamed | 3783921 | 2021-03-15 16:49:06 +0000 | [diff] [blame] | 5694 | @RequiresPermission(anyOf = { |
| 5695 | android.Manifest.permission.MODIFY_AUDIO_ROUTING, |
| 5696 | android.Manifest.permission.QUERY_AUDIO_STATE |
| 5697 | }) |
Marvin Ramin | 5495cc9 | 2020-07-23 11:58:33 +0200 | [diff] [blame] | 5698 | public @DeviceVolumeBehavior |
| 5699 | int getDeviceVolumeBehavior(@NonNull AudioDeviceAttributes device) { |
Jean-Michel Trivi | 005c601 | 2020-03-15 20:33:15 -0700 | [diff] [blame] | 5700 | // verify arguments (validity of device type is enforced in server) |
| 5701 | Objects.requireNonNull(device); |
| 5702 | // communicate with service |
| 5703 | final IAudioService service = getService(); |
| 5704 | try { |
| 5705 | return service.getDeviceVolumeBehavior(device); |
| 5706 | } catch (RemoteException e) { |
| 5707 | throw e.rethrowFromSystemServer(); |
| 5708 | } |
| 5709 | } |
| 5710 | |
kholoud mohamed | 3783921 | 2021-03-15 16:49:06 +0000 | [diff] [blame] | 5711 | /** |
| 5712 | * @hide |
| 5713 | * Returns {@code true} if the volume device behavior is {@link #DEVICE_VOLUME_BEHAVIOR_FULL}. |
| 5714 | */ |
| 5715 | @TestApi |
| 5716 | @RequiresPermission(anyOf = { |
| 5717 | android.Manifest.permission.MODIFY_AUDIO_ROUTING, |
| 5718 | android.Manifest.permission.QUERY_AUDIO_STATE |
| 5719 | }) |
| 5720 | public boolean isFullVolumeDevice() { |
| 5721 | final AudioAttributes attributes = new AudioAttributes.Builder() |
| 5722 | .setUsage(AudioAttributes.USAGE_MEDIA) |
| 5723 | .build(); |
| 5724 | final List<AudioDeviceAttributes> devices = getDevicesForAttributes(attributes); |
| 5725 | for (AudioDeviceAttributes device : devices) { |
| 5726 | if (getDeviceVolumeBehavior(device) == DEVICE_VOLUME_BEHAVIOR_FULL) { |
| 5727 | return true; |
| 5728 | } |
| 5729 | } |
| 5730 | return false; |
| 5731 | } |
| 5732 | |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5733 | /** |
| 5734 | * Indicate wired accessory connection state change. |
| 5735 | * @param device type of device connected/disconnected (AudioManager.DEVICE_OUT_xxx) |
| 5736 | * @param state new connection state: 1 connected, 0 disconnected |
| 5737 | * @param name device name |
| 5738 | * {@hide} |
| 5739 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5740 | @UnsupportedAppUsage |
Jean-Michel Trivi | f049197 | 2020-03-24 09:20:50 -0700 | [diff] [blame] | 5741 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
Paul McLean | 10804eb | 2015-01-28 11:16:35 -0800 | [diff] [blame] | 5742 | public void setWiredDeviceConnectionState(int type, int state, String address, String name) { |
Jean-Michel Trivi | 50750ff | 2017-03-21 11:18:24 -0700 | [diff] [blame] | 5743 | final IAudioService service = getService(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5744 | try { |
John Spurlock | 9087433 | 2015-03-10 16:00:54 -0400 | [diff] [blame] | 5745 | service.setWiredDeviceConnectionState(type, state, address, name, |
Marco Nelissen | a80ac05 | 2015-03-12 16:17:45 -0700 | [diff] [blame] | 5746 | mApplicationContext.getOpPackageName()); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5747 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 5748 | throw e.rethrowFromSystemServer(); |
Eric Laurent | b1fbaac | 2012-05-29 09:24:28 -0700 | [diff] [blame] | 5749 | } |
| 5750 | } |
| 5751 | |
Grzegorz Kołodziejczyk | 59b2baa | 2021-05-14 12:19:07 +0000 | [diff] [blame] | 5752 | /** |
wescande | 7c17ba0c | 2021-07-30 16:46:14 +0200 | [diff] [blame] | 5753 | * Indicate Bluetooth profile connection state change. |
| 5754 | * Configuration changes for A2DP are indicated by having the same <code>newDevice</code> and |
| 5755 | * <code>previousDevice</code> |
| 5756 | * This operation is asynchronous. |
| 5757 | * |
| 5758 | * @param newDevice Bluetooth device connected or null if there is no new devices |
| 5759 | * @param previousDevice Bluetooth device disconnected or null if there is no disconnected |
| 5760 | * devices |
| 5761 | * @param info contain all info related to the device. {@link BtProfileConnectionInfo} |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 5762 | * {@hide} |
| 5763 | */ |
wescande | 7c17ba0c | 2021-07-30 16:46:14 +0200 | [diff] [blame] | 5764 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 5765 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_STACK) |
| 5766 | public void handleBluetoothActiveDeviceChanged(@Nullable BluetoothDevice newDevice, |
| 5767 | @Nullable BluetoothDevice previousDevice, @NonNull BtProfileConnectionInfo info) { |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 5768 | final IAudioService service = getService(); |
Pavlin Radoslavov | 82e0646 | 2018-01-19 18:20:04 -0800 | [diff] [blame] | 5769 | try { |
wescande | 7c17ba0c | 2021-07-30 16:46:14 +0200 | [diff] [blame] | 5770 | service.handleBluetoothActiveDeviceChanged(newDevice, previousDevice, info); |
Pavlin Radoslavov | 44a4ef0 | 2016-12-21 12:05:51 -0800 | [diff] [blame] | 5771 | } catch (RemoteException e) { |
| 5772 | throw e.rethrowFromSystemServer(); |
| 5773 | } |
| 5774 | } |
| 5775 | |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5776 | /** {@hide} */ |
| 5777 | public IRingtonePlayer getRingtonePlayer() { |
| 5778 | try { |
| 5779 | return getService().getRingtonePlayer(); |
| 5780 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 5781 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 5782 | } |
| 5783 | } |
Glenn Kasten | 228c984 | 2012-09-14 08:48:47 -0700 | [diff] [blame] | 5784 | |
| 5785 | /** |
Glenn Kasten | 62796aa | 2012-09-20 08:08:07 -0700 | [diff] [blame] | 5786 | * Used as a key for {@link #getProperty} to request the native or optimal output sample rate |
Glenn Kasten | c3de514 | 2016-07-15 12:14:24 -0700 | [diff] [blame] | 5787 | * for this device's low latency output stream, in decimal Hz. Latency-sensitive apps |
| 5788 | * should use this value as a default, and offer the user the option to override it. |
| 5789 | * The low latency output stream is typically either the device's primary output stream, |
| 5790 | * or another output stream with smaller buffers. |
Glenn Kasten | 228c984 | 2012-09-14 08:48:47 -0700 | [diff] [blame] | 5791 | */ |
Glenn Kasten | 1cbf9b3 | 2016-02-02 12:04:09 -0800 | [diff] [blame] | 5792 | // FIXME Deprecate |
Glenn Kasten | 228c984 | 2012-09-14 08:48:47 -0700 | [diff] [blame] | 5793 | public static final String PROPERTY_OUTPUT_SAMPLE_RATE = |
| 5794 | "android.media.property.OUTPUT_SAMPLE_RATE"; |
| 5795 | |
| 5796 | /** |
Glenn Kasten | 62796aa | 2012-09-20 08:08:07 -0700 | [diff] [blame] | 5797 | * Used as a key for {@link #getProperty} to request the native or optimal output buffer size |
Glenn Kasten | c3de514 | 2016-07-15 12:14:24 -0700 | [diff] [blame] | 5798 | * for this device's low latency output stream, in decimal PCM frames. Latency-sensitive apps |
| 5799 | * should use this value as a minimum, and offer the user the option to override it. |
| 5800 | * The low latency output stream is typically either the device's primary output stream, |
| 5801 | * or another output stream with smaller buffers. |
Glenn Kasten | 228c984 | 2012-09-14 08:48:47 -0700 | [diff] [blame] | 5802 | */ |
Glenn Kasten | 1cbf9b3 | 2016-02-02 12:04:09 -0800 | [diff] [blame] | 5803 | // FIXME Deprecate |
Glenn Kasten | 228c984 | 2012-09-14 08:48:47 -0700 | [diff] [blame] | 5804 | public static final String PROPERTY_OUTPUT_FRAMES_PER_BUFFER = |
| 5805 | "android.media.property.OUTPUT_FRAMES_PER_BUFFER"; |
| 5806 | |
| 5807 | /** |
Arunesh Mishra | d08715e5 | 2015-04-23 22:39:40 -0700 | [diff] [blame] | 5808 | * Used as a key for {@link #getProperty} to determine if the default microphone audio source |
| 5809 | * supports near-ultrasound frequencies (range of 18 - 21 kHz). |
| 5810 | */ |
| 5811 | public static final String PROPERTY_SUPPORT_MIC_NEAR_ULTRASOUND = |
| 5812 | "android.media.property.SUPPORT_MIC_NEAR_ULTRASOUND"; |
| 5813 | |
| 5814 | /** |
| 5815 | * Used as a key for {@link #getProperty} to determine if the default speaker audio path |
| 5816 | * supports near-ultrasound frequencies (range of 18 - 21 kHz). |
| 5817 | */ |
| 5818 | public static final String PROPERTY_SUPPORT_SPEAKER_NEAR_ULTRASOUND = |
| 5819 | "android.media.property.SUPPORT_SPEAKER_NEAR_ULTRASOUND"; |
| 5820 | |
| 5821 | /** |
rago | a7cc59c | 2015-12-02 11:31:15 -0800 | [diff] [blame] | 5822 | * Used as a key for {@link #getProperty} to determine if the unprocessed audio source is |
| 5823 | * available and supported with the expected frequency range and level response. |
| 5824 | */ |
| 5825 | public static final String PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED = |
| 5826 | "android.media.property.SUPPORT_AUDIO_SOURCE_UNPROCESSED"; |
| 5827 | /** |
Glenn Kasten | 62796aa | 2012-09-20 08:08:07 -0700 | [diff] [blame] | 5828 | * Returns the value of the property with the specified key. |
Glenn Kasten | 228c984 | 2012-09-14 08:48:47 -0700 | [diff] [blame] | 5829 | * @param key One of the strings corresponding to a property key: either |
Glenn Kasten | 0b986af | 2015-10-30 18:24:04 -0700 | [diff] [blame] | 5830 | * {@link #PROPERTY_OUTPUT_SAMPLE_RATE}, |
| 5831 | * {@link #PROPERTY_OUTPUT_FRAMES_PER_BUFFER}, |
rago | a7cc59c | 2015-12-02 11:31:15 -0800 | [diff] [blame] | 5832 | * {@link #PROPERTY_SUPPORT_MIC_NEAR_ULTRASOUND}, |
| 5833 | * {@link #PROPERTY_SUPPORT_SPEAKER_NEAR_ULTRASOUND}, or |
| 5834 | * {@link #PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED}. |
Glenn Kasten | 228c984 | 2012-09-14 08:48:47 -0700 | [diff] [blame] | 5835 | * @return A string representing the associated value for that property key, |
| 5836 | * or null if there is no value for that key. |
Glenn Kasten | 228c984 | 2012-09-14 08:48:47 -0700 | [diff] [blame] | 5837 | */ |
| 5838 | public String getProperty(String key) { |
Glenn Kasten | c6c4365 | 2012-09-24 17:32:30 -0700 | [diff] [blame] | 5839 | if (PROPERTY_OUTPUT_SAMPLE_RATE.equals(key)) { |
| 5840 | int outputSampleRate = AudioSystem.getPrimaryOutputSamplingRate(); |
| 5841 | return outputSampleRate > 0 ? Integer.toString(outputSampleRate) : null; |
| 5842 | } else if (PROPERTY_OUTPUT_FRAMES_PER_BUFFER.equals(key)) { |
| 5843 | int outputFramesPerBuffer = AudioSystem.getPrimaryOutputFrameCount(); |
| 5844 | return outputFramesPerBuffer > 0 ? Integer.toString(outputFramesPerBuffer) : null; |
Arunesh Mishra | bc92227 | 2015-04-27 09:39:00 -0700 | [diff] [blame] | 5845 | } else if (PROPERTY_SUPPORT_MIC_NEAR_ULTRASOUND.equals(key)) { |
Arunesh Mishra | 6e4fb34 | 2015-06-02 13:23:41 -0700 | [diff] [blame] | 5846 | // Will throw a RuntimeException Resources.NotFoundException if this config value is |
| 5847 | // not found. |
| 5848 | return String.valueOf(getContext().getResources().getBoolean( |
| 5849 | com.android.internal.R.bool.config_supportMicNearUltrasound)); |
Arunesh Mishra | bc92227 | 2015-04-27 09:39:00 -0700 | [diff] [blame] | 5850 | } else if (PROPERTY_SUPPORT_SPEAKER_NEAR_ULTRASOUND.equals(key)) { |
Arunesh Mishra | 6e4fb34 | 2015-06-02 13:23:41 -0700 | [diff] [blame] | 5851 | return String.valueOf(getContext().getResources().getBoolean( |
| 5852 | com.android.internal.R.bool.config_supportSpeakerNearUltrasound)); |
rago | a7cc59c | 2015-12-02 11:31:15 -0800 | [diff] [blame] | 5853 | } else if (PROPERTY_SUPPORT_AUDIO_SOURCE_UNPROCESSED.equals(key)) { |
| 5854 | return String.valueOf(getContext().getResources().getBoolean( |
| 5855 | com.android.internal.R.bool.config_supportAudioSourceUnprocessed)); |
Glenn Kasten | c6c4365 | 2012-09-24 17:32:30 -0700 | [diff] [blame] | 5856 | } else { |
| 5857 | // null or unknown key |
| 5858 | return null; |
| 5859 | } |
Glenn Kasten | 228c984 | 2012-09-14 08:48:47 -0700 | [diff] [blame] | 5860 | } |
| 5861 | |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 5862 | /** |
Andy Hung | 97aa07f8 | 2020-01-17 14:05:06 -0800 | [diff] [blame] | 5863 | * @hide |
| 5864 | * Sets an additional audio output device delay in milliseconds. |
| 5865 | * |
| 5866 | * The additional output delay is a request to the output device to |
| 5867 | * delay audio presentation (generally with respect to video presentation for better |
| 5868 | * synchronization). |
| 5869 | * It may not be supported by all output devices, |
| 5870 | * and typically increases the audio latency by the amount of additional |
| 5871 | * audio delay requested. |
| 5872 | * |
| 5873 | * If additional audio delay is supported by an audio output device, |
| 5874 | * it is expected to be supported for all output streams (and configurations) |
| 5875 | * opened on that device. |
| 5876 | * |
| 5877 | * @param device an instance of {@link AudioDeviceInfo} returned from {@link getDevices()}. |
Andy Hung | 120c1c4 | 2020-03-26 12:01:33 -0700 | [diff] [blame] | 5878 | * @param delayMillis delay in milliseconds desired. This should be in range of {@code 0} |
Andy Hung | 97aa07f8 | 2020-01-17 14:05:06 -0800 | [diff] [blame] | 5879 | * to the value returned by {@link #getMaxAdditionalOutputDeviceDelay()}. |
| 5880 | * @return true if successful, false if the device does not support output device delay |
| 5881 | * or the delay is not in range of {@link #getMaxAdditionalOutputDeviceDelay()}. |
| 5882 | */ |
| 5883 | @SystemApi |
| 5884 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 5885 | public boolean setAdditionalOutputDeviceDelay( |
Andy Hung | 120c1c4 | 2020-03-26 12:01:33 -0700 | [diff] [blame] | 5886 | @NonNull AudioDeviceInfo device, @IntRange(from = 0) long delayMillis) { |
Andy Hung | 97aa07f8 | 2020-01-17 14:05:06 -0800 | [diff] [blame] | 5887 | Objects.requireNonNull(device); |
Kuowei Li | f50ac2e3 | 2020-08-13 14:34:17 +0800 | [diff] [blame] | 5888 | try { |
| 5889 | return getService().setAdditionalOutputDeviceDelay( |
| 5890 | new AudioDeviceAttributes(device), delayMillis); |
| 5891 | } catch (RemoteException e) { |
| 5892 | throw e.rethrowFromSystemServer(); |
| 5893 | } |
Andy Hung | 97aa07f8 | 2020-01-17 14:05:06 -0800 | [diff] [blame] | 5894 | } |
| 5895 | |
| 5896 | /** |
| 5897 | * @hide |
| 5898 | * Returns the current additional audio output device delay in milliseconds. |
| 5899 | * |
| 5900 | * @param device an instance of {@link AudioDeviceInfo} returned from {@link getDevices()}. |
| 5901 | * @return the additional output device delay. This is a non-negative number. |
| 5902 | * {@code 0} is returned if unsupported. |
| 5903 | */ |
| 5904 | @SystemApi |
| 5905 | @IntRange(from = 0) |
Andy Hung | 120c1c4 | 2020-03-26 12:01:33 -0700 | [diff] [blame] | 5906 | public long getAdditionalOutputDeviceDelay(@NonNull AudioDeviceInfo device) { |
Andy Hung | 97aa07f8 | 2020-01-17 14:05:06 -0800 | [diff] [blame] | 5907 | Objects.requireNonNull(device); |
Kuowei Li | f50ac2e3 | 2020-08-13 14:34:17 +0800 | [diff] [blame] | 5908 | try { |
| 5909 | return getService().getAdditionalOutputDeviceDelay(new AudioDeviceAttributes(device)); |
| 5910 | } catch (RemoteException e) { |
| 5911 | throw e.rethrowFromSystemServer(); |
| 5912 | } |
Andy Hung | 97aa07f8 | 2020-01-17 14:05:06 -0800 | [diff] [blame] | 5913 | } |
| 5914 | |
| 5915 | /** |
| 5916 | * @hide |
| 5917 | * Returns the maximum additional audio output device delay in milliseconds. |
| 5918 | * |
| 5919 | * @param device an instance of {@link AudioDeviceInfo} returned from {@link getDevices()}. |
| 5920 | * @return the maximum output device delay in milliseconds that can be set. |
| 5921 | * This is a non-negative number |
| 5922 | * representing the additional audio delay supported for the device. |
| 5923 | * {@code 0} is returned if unsupported. |
| 5924 | */ |
| 5925 | @SystemApi |
| 5926 | @IntRange(from = 0) |
Andy Hung | 120c1c4 | 2020-03-26 12:01:33 -0700 | [diff] [blame] | 5927 | public long getMaxAdditionalOutputDeviceDelay(@NonNull AudioDeviceInfo device) { |
Andy Hung | 97aa07f8 | 2020-01-17 14:05:06 -0800 | [diff] [blame] | 5928 | Objects.requireNonNull(device); |
Kuowei Li | f50ac2e3 | 2020-08-13 14:34:17 +0800 | [diff] [blame] | 5929 | try { |
| 5930 | return getService().getMaxAdditionalOutputDeviceDelay( |
| 5931 | new AudioDeviceAttributes(device)); |
| 5932 | } catch (RemoteException e) { |
| 5933 | throw e.rethrowFromSystemServer(); |
| 5934 | } |
Andy Hung | 97aa07f8 | 2020-01-17 14:05:06 -0800 | [diff] [blame] | 5935 | } |
| 5936 | |
| 5937 | /** |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 5938 | * Returns the estimated latency for the given stream type in milliseconds. |
| 5939 | * |
| 5940 | * DO NOT UNHIDE. The existing approach for doing A/V sync has too many problems. We need |
| 5941 | * a better solution. |
| 5942 | * @hide |
| 5943 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 5944 | @UnsupportedAppUsage |
Oliver Woodman | 61dcdf3 | 2013-06-26 12:43:36 +0100 | [diff] [blame] | 5945 | public int getOutputLatency(int streamType) { |
| 5946 | return AudioSystem.getOutputLatency(streamType); |
| 5947 | } |
| 5948 | |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5949 | /** |
| 5950 | * Registers a global volume controller interface. Currently limited to SystemUI. |
| 5951 | * |
| 5952 | * @hide |
| 5953 | */ |
| 5954 | public void setVolumeController(IVolumeController controller) { |
| 5955 | try { |
| 5956 | getService().setVolumeController(controller); |
| 5957 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 5958 | throw e.rethrowFromSystemServer(); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5959 | } |
| 5960 | } |
| 5961 | |
| 5962 | /** |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5963 | * Notify audio manager about volume controller visibility changes. |
| 5964 | * Currently limited to SystemUI. |
| 5965 | * |
| 5966 | * @hide |
| 5967 | */ |
| 5968 | public void notifyVolumeControllerVisible(IVolumeController controller, boolean visible) { |
| 5969 | try { |
| 5970 | getService().notifyVolumeControllerVisible(controller, visible); |
| 5971 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 5972 | throw e.rethrowFromSystemServer(); |
John Spurlock | 33f4e04 | 2014-07-11 13:10:58 -0400 | [diff] [blame] | 5973 | } |
| 5974 | } |
| 5975 | |
| 5976 | /** |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5977 | * Only useful for volume controllers. |
| 5978 | * @hide |
| 5979 | */ |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5980 | public boolean isStreamAffectedByRingerMode(int streamType) { |
| 5981 | try { |
| 5982 | return getService().isStreamAffectedByRingerMode(streamType); |
| 5983 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 5984 | throw e.rethrowFromSystemServer(); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 5985 | } |
| 5986 | } |
| 5987 | |
| 5988 | /** |
| 5989 | * Only useful for volume controllers. |
| 5990 | * @hide |
| 5991 | */ |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 5992 | public boolean isStreamAffectedByMute(int streamType) { |
| 5993 | try { |
| 5994 | return getService().isStreamAffectedByMute(streamType); |
| 5995 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 5996 | throw e.rethrowFromSystemServer(); |
John Spurlock | a9dfbe8b | 2015-02-17 11:01:51 -0500 | [diff] [blame] | 5997 | } |
| 5998 | } |
| 5999 | |
| 6000 | /** |
| 6001 | * Only useful for volume controllers. |
| 6002 | * @hide |
| 6003 | */ |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6004 | public void disableSafeMediaVolume() { |
| 6005 | try { |
Marco Nelissen | a80ac05 | 2015-03-12 16:17:45 -0700 | [diff] [blame] | 6006 | getService().disableSafeMediaVolume(mApplicationContext.getOpPackageName()); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6007 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 6008 | throw e.rethrowFromSystemServer(); |
John Spurlock | 3346a80 | 2014-05-20 16:25:37 -0400 | [diff] [blame] | 6009 | } |
| 6010 | } |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6011 | |
| 6012 | /** |
John Spurlock | 661f2cf4 | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6013 | * Only useful for volume controllers. |
| 6014 | * @hide |
| 6015 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 6016 | @UnsupportedAppUsage |
John Spurlock | 661f2cf4 | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6017 | public void setRingerModeInternal(int ringerMode) { |
| 6018 | try { |
Marco Nelissen | 29f1693 | 2015-04-17 09:50:56 -0700 | [diff] [blame] | 6019 | getService().setRingerModeInternal(ringerMode, getContext().getOpPackageName()); |
John Spurlock | 661f2cf4 | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6020 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 6021 | throw e.rethrowFromSystemServer(); |
John Spurlock | 661f2cf4 | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6022 | } |
| 6023 | } |
| 6024 | |
| 6025 | /** |
| 6026 | * Only useful for volume controllers. |
| 6027 | * @hide |
| 6028 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 6029 | @UnsupportedAppUsage |
John Spurlock | 661f2cf4 | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6030 | public int getRingerModeInternal() { |
| 6031 | try { |
| 6032 | return getService().getRingerModeInternal(); |
| 6033 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 6034 | throw e.rethrowFromSystemServer(); |
John Spurlock | 661f2cf4 | 2014-11-17 10:29:10 -0500 | [diff] [blame] | 6035 | } |
| 6036 | } |
| 6037 | |
| 6038 | /** |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 6039 | * Only useful for volume controllers. |
| 6040 | * @hide |
| 6041 | */ |
| 6042 | public void setVolumePolicy(VolumePolicy policy) { |
| 6043 | try { |
| 6044 | getService().setVolumePolicy(policy); |
| 6045 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 6046 | throw e.rethrowFromSystemServer(); |
John Spurlock | a48d779 | 2015-03-03 17:35:57 -0500 | [diff] [blame] | 6047 | } |
| 6048 | } |
| 6049 | |
| 6050 | /** |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6051 | * Set Hdmi Cec system audio mode. |
| 6052 | * |
| 6053 | * @param on whether to be on system audio mode |
Jungshik Jang | 6f34f5a | 2014-07-08 21:17:29 +0900 | [diff] [blame] | 6054 | * @return output device type. 0 (DEVICE_NONE) if failed to set device. |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6055 | * @hide |
| 6056 | */ |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 6057 | public int setHdmiSystemAudioSupported(boolean on) { |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6058 | try { |
Jungshik Jang | 12307ca | 2014-07-15 19:27:56 +0900 | [diff] [blame] | 6059 | return getService().setHdmiSystemAudioSupported(on); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6060 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 6061 | throw e.rethrowFromSystemServer(); |
Jungshik Jang | 41d9746 | 2014-06-30 22:26:29 +0900 | [diff] [blame] | 6062 | } |
| 6063 | } |
| 6064 | |
| 6065 | /** |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 6066 | * Returns true if Hdmi Cec system audio mode is supported. |
| 6067 | * |
| 6068 | * @hide |
| 6069 | */ |
| 6070 | @SystemApi |
Aurimas Liutikas | 1394a01 | 2020-11-12 18:26:09 -0800 | [diff] [blame] | 6071 | @SuppressLint("RequiresPermission") // FIXME is this still used? |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 6072 | public boolean isHdmiSystemAudioSupported() { |
| 6073 | try { |
| 6074 | return getService().isHdmiSystemAudioSupported(); |
| 6075 | } catch (RemoteException e) { |
Jeff Sharkey | f888056 | 2016-02-26 13:03:01 -0700 | [diff] [blame] | 6076 | throw e.rethrowFromSystemServer(); |
Terry Heo | e7d6d97 | 2014-09-04 21:05:28 +0900 | [diff] [blame] | 6077 | } |
| 6078 | } |
| 6079 | |
| 6080 | /** |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6081 | * Return codes for listAudioPorts(), createAudioPatch() ... |
| 6082 | */ |
| 6083 | |
Jean-Michel Trivi | d6f65de | 2018-12-18 18:49:14 -0800 | [diff] [blame] | 6084 | /** @hide */ |
| 6085 | @SystemApi |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6086 | public static final int SUCCESS = AudioSystem.SUCCESS; |
Jean-Michel Trivi | 8a21f5d | 2014-06-05 15:03:52 -0700 | [diff] [blame] | 6087 | /** |
| 6088 | * A default error code. |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6089 | */ |
| 6090 | public static final int ERROR = AudioSystem.ERROR; |
| 6091 | /** @hide |
Jean-Michel Trivi | a8b6bd8 | 2014-07-01 09:48:46 -0700 | [diff] [blame] | 6092 | * CANDIDATE FOR PUBLIC API |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6093 | */ |
| 6094 | public static final int ERROR_BAD_VALUE = AudioSystem.BAD_VALUE; |
| 6095 | /** @hide |
| 6096 | */ |
| 6097 | public static final int ERROR_INVALID_OPERATION = AudioSystem.INVALID_OPERATION; |
| 6098 | /** @hide |
| 6099 | */ |
| 6100 | public static final int ERROR_PERMISSION_DENIED = AudioSystem.PERMISSION_DENIED; |
| 6101 | /** @hide |
| 6102 | */ |
| 6103 | public static final int ERROR_NO_INIT = AudioSystem.NO_INIT; |
Eric Laurent | ff0d9f0 | 2014-06-09 17:23:02 -0700 | [diff] [blame] | 6104 | /** |
| 6105 | * An error code indicating that the object reporting it is no longer valid and needs to |
| 6106 | * be recreated. |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6107 | */ |
| 6108 | public static final int ERROR_DEAD_OBJECT = AudioSystem.DEAD_OBJECT; |
| 6109 | |
| 6110 | /** |
| 6111 | * Returns a list of descriptors for all audio ports managed by the audio framework. |
| 6112 | * Audio ports are nodes in the audio framework or audio hardware that can be configured |
| 6113 | * or connected and disconnected with createAudioPatch() or releaseAudioPatch(). |
| 6114 | * See AudioPort for a list of attributes of each audio port. |
| 6115 | * @param ports An AudioPort ArrayList where the list will be returned. |
| 6116 | * @hide |
| 6117 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 6118 | @UnsupportedAppUsage |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 6119 | public static int listAudioPorts(ArrayList<AudioPort> ports) { |
Eric Laurent | 8a1e7a8 | 2015-05-08 11:43:05 -0700 | [diff] [blame] | 6120 | return updateAudioPortCache(ports, null, null); |
| 6121 | } |
| 6122 | |
| 6123 | /** |
| 6124 | * Returns a list of descriptors for all audio ports managed by the audio framework as |
| 6125 | * it was before the last update calback. |
| 6126 | * @param ports An AudioPort ArrayList where the list will be returned. |
| 6127 | * @hide |
| 6128 | */ |
| 6129 | public static int listPreviousAudioPorts(ArrayList<AudioPort> ports) { |
| 6130 | return updateAudioPortCache(null, null, ports); |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6131 | } |
| 6132 | |
| 6133 | /** |
| 6134 | * Specialized version of listAudioPorts() listing only audio devices (AudioDevicePort) |
| 6135 | * @see listAudioPorts(ArrayList<AudioPort>) |
| 6136 | * @hide |
| 6137 | */ |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 6138 | public static int listAudioDevicePorts(ArrayList<AudioDevicePort> devices) { |
Eric Laurent | 8a1e7a8 | 2015-05-08 11:43:05 -0700 | [diff] [blame] | 6139 | if (devices == null) { |
| 6140 | return ERROR_BAD_VALUE; |
| 6141 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6142 | ArrayList<AudioPort> ports = new ArrayList<AudioPort>(); |
Eric Laurent | 8a1e7a8 | 2015-05-08 11:43:05 -0700 | [diff] [blame] | 6143 | int status = updateAudioPortCache(ports, null, null); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6144 | if (status == SUCCESS) { |
Eric Laurent | 8a1e7a8 | 2015-05-08 11:43:05 -0700 | [diff] [blame] | 6145 | filterDevicePorts(ports, devices); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6146 | } |
| 6147 | return status; |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6148 | } |
| 6149 | |
| 6150 | /** |
Eric Laurent | 8a1e7a8 | 2015-05-08 11:43:05 -0700 | [diff] [blame] | 6151 | * Specialized version of listPreviousAudioPorts() listing only audio devices (AudioDevicePort) |
| 6152 | * @see listPreviousAudioPorts(ArrayList<AudioPort>) |
| 6153 | * @hide |
| 6154 | */ |
| 6155 | public static int listPreviousAudioDevicePorts(ArrayList<AudioDevicePort> devices) { |
| 6156 | if (devices == null) { |
| 6157 | return ERROR_BAD_VALUE; |
| 6158 | } |
| 6159 | ArrayList<AudioPort> ports = new ArrayList<AudioPort>(); |
| 6160 | int status = updateAudioPortCache(null, null, ports); |
| 6161 | if (status == SUCCESS) { |
| 6162 | filterDevicePorts(ports, devices); |
| 6163 | } |
| 6164 | return status; |
| 6165 | } |
| 6166 | |
| 6167 | private static void filterDevicePorts(ArrayList<AudioPort> ports, |
| 6168 | ArrayList<AudioDevicePort> devices) { |
| 6169 | devices.clear(); |
| 6170 | for (int i = 0; i < ports.size(); i++) { |
| 6171 | if (ports.get(i) instanceof AudioDevicePort) { |
| 6172 | devices.add((AudioDevicePort)ports.get(i)); |
| 6173 | } |
| 6174 | } |
| 6175 | } |
| 6176 | |
| 6177 | /** |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6178 | * Create a connection between two or more devices. The framework will reject the request if |
| 6179 | * device types are not compatible or the implementation does not support the requested |
| 6180 | * configuration. |
| 6181 | * NOTE: current implementation is limited to one source and one sink per patch. |
| 6182 | * @param patch AudioPatch array where the newly created patch will be returned. |
| 6183 | * As input, if patch[0] is not null, the specified patch will be replaced by the |
| 6184 | * new patch created. This avoids calling releaseAudioPatch() when modifying a |
| 6185 | * patch and allows the implementation to optimize transitions. |
| 6186 | * @param sources List of source audio ports. All must be AudioPort.ROLE_SOURCE. |
| 6187 | * @param sinks List of sink audio ports. All must be AudioPort.ROLE_SINK. |
| 6188 | * |
| 6189 | * @return - {@link #SUCCESS} if connection is successful. |
| 6190 | * - {@link #ERROR_BAD_VALUE} if incompatible device types are passed. |
| 6191 | * - {@link #ERROR_INVALID_OPERATION} if the requested connection is not supported. |
| 6192 | * - {@link #ERROR_PERMISSION_DENIED} if the client does not have permission to create |
| 6193 | * a patch. |
| 6194 | * - {@link #ERROR_DEAD_OBJECT} if the server process is dead |
| 6195 | * - {@link #ERROR} if patch cannot be connected for any other reason. |
| 6196 | * |
| 6197 | * patch[0] contains the newly created patch |
| 6198 | * @hide |
| 6199 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 6200 | @UnsupportedAppUsage |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 6201 | public static int createAudioPatch(AudioPatch[] patch, |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6202 | AudioPortConfig[] sources, |
| 6203 | AudioPortConfig[] sinks) { |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6204 | return AudioSystem.createAudioPatch(patch, sources, sinks); |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | /** |
| 6208 | * Releases an existing audio patch connection. |
| 6209 | * @param patch The audio patch to disconnect. |
| 6210 | * @return - {@link #SUCCESS} if disconnection is successful. |
| 6211 | * - {@link #ERROR_BAD_VALUE} if the specified patch does not exist. |
| 6212 | * - {@link #ERROR_PERMISSION_DENIED} if the client does not have permission to release |
| 6213 | * a patch. |
| 6214 | * - {@link #ERROR_DEAD_OBJECT} if the server process is dead |
| 6215 | * - {@link #ERROR} if patch cannot be released for any other reason. |
| 6216 | * @hide |
| 6217 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 6218 | @UnsupportedAppUsage |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 6219 | public static int releaseAudioPatch(AudioPatch patch) { |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6220 | return AudioSystem.releaseAudioPatch(patch); |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6221 | } |
| 6222 | |
| 6223 | /** |
| 6224 | * List all existing connections between audio ports. |
| 6225 | * @param patches An AudioPatch array where the list will be returned. |
| 6226 | * @hide |
| 6227 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 6228 | @UnsupportedAppUsage |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 6229 | public static int listAudioPatches(ArrayList<AudioPatch> patches) { |
Eric Laurent | 8a1e7a8 | 2015-05-08 11:43:05 -0700 | [diff] [blame] | 6230 | return updateAudioPortCache(null, patches, null); |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6231 | } |
| 6232 | |
| 6233 | /** |
| 6234 | * Set the gain on the specified AudioPort. The AudioGainConfig config is build by |
| 6235 | * AudioGain.buildConfig() |
| 6236 | * @hide |
| 6237 | */ |
Eric Laurent | 4bcdba8 | 2015-05-01 11:37:49 -0700 | [diff] [blame] | 6238 | public static int setAudioPortGain(AudioPort port, AudioGainConfig gain) { |
Eric Laurent | 3a24199 | 2014-05-19 19:33:26 -0700 | [diff] [blame] | 6239 | if (port == null || gain == null) { |
| 6240 | return ERROR_BAD_VALUE; |
| 6241 | } |
| 6242 | AudioPortConfig activeConfig = port.activeConfig(); |
| 6243 | AudioPortConfig config = new AudioPortConfig(port, activeConfig.samplingRate(), |
| 6244 | activeConfig.channelMask(), activeConfig.format(), gain); |
| 6245 | config.mConfigMask = AudioPortConfig.GAIN; |
| 6246 | return AudioSystem.setAudioPortConfig(config); |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6247 | } |
| 6248 | |
| 6249 | /** |
| 6250 | * Listener registered by client to be notified upon new audio port connections, |
| 6251 | * disconnections or attributes update. |
| 6252 | * @hide |
| 6253 | */ |
| 6254 | public interface OnAudioPortUpdateListener { |
| 6255 | /** |
| 6256 | * Callback method called upon audio port list update. |
| 6257 | * @param portList the updated list of audio ports |
| 6258 | */ |
Eric Laurent | fdaed9d | 2014-07-29 09:29:30 -0700 | [diff] [blame] | 6259 | public void onAudioPortListUpdate(AudioPort[] portList); |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6260 | |
| 6261 | /** |
| 6262 | * Callback method called upon audio patch list update. |
| 6263 | * @param patchList the updated list of audio patches |
| 6264 | */ |
Eric Laurent | fdaed9d | 2014-07-29 09:29:30 -0700 | [diff] [blame] | 6265 | public void onAudioPatchListUpdate(AudioPatch[] patchList); |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6266 | |
| 6267 | /** |
| 6268 | * Callback method called when the mediaserver dies |
| 6269 | */ |
Eric Laurent | fdaed9d | 2014-07-29 09:29:30 -0700 | [diff] [blame] | 6270 | public void onServiceDied(); |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6271 | } |
| 6272 | |
| 6273 | /** |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 6274 | * Register an audio port list update listener. |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6275 | * @hide |
| 6276 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 6277 | @UnsupportedAppUsage |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6278 | public void registerAudioPortUpdateListener(OnAudioPortUpdateListener l) { |
Eric Laurent | c573bc5 | 2015-06-26 10:01:12 -0700 | [diff] [blame] | 6279 | sAudioPortEventHandler.init(); |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6280 | sAudioPortEventHandler.registerListener(l); |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6281 | } |
| 6282 | |
| 6283 | /** |
Eric Laurent | 700e734 | 2014-05-02 18:33:15 -0700 | [diff] [blame] | 6284 | * Unregister an audio port list update listener. |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6285 | * @hide |
| 6286 | */ |
Mathew Inwood | 31a792a | 2018-08-17 08:54:26 +0100 | [diff] [blame] | 6287 | @UnsupportedAppUsage |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6288 | public void unregisterAudioPortUpdateListener(OnAudioPortUpdateListener l) { |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6289 | sAudioPortEventHandler.unregisterListener(l); |
Eric Laurent | a198a29 | 2014-02-18 16:26:17 -0800 | [diff] [blame] | 6290 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6291 | |
| 6292 | // |
| 6293 | // AudioPort implementation |
| 6294 | // |
| 6295 | |
| 6296 | static final int AUDIOPORT_GENERATION_INIT = 0; |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6297 | static Integer sAudioPortGeneration = new Integer(AUDIOPORT_GENERATION_INIT); |
| 6298 | static ArrayList<AudioPort> sAudioPortsCached = new ArrayList<AudioPort>(); |
Eric Laurent | 8a1e7a8 | 2015-05-08 11:43:05 -0700 | [diff] [blame] | 6299 | static ArrayList<AudioPort> sPreviousAudioPortsCached = new ArrayList<AudioPort>(); |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6300 | static ArrayList<AudioPatch> sAudioPatchesCached = new ArrayList<AudioPatch>(); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6301 | |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6302 | static int resetAudioPortGeneration() { |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6303 | int generation; |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6304 | synchronized (sAudioPortGeneration) { |
| 6305 | generation = sAudioPortGeneration; |
| 6306 | sAudioPortGeneration = AUDIOPORT_GENERATION_INIT; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6307 | } |
| 6308 | return generation; |
| 6309 | } |
| 6310 | |
Eric Laurent | 8a1e7a8 | 2015-05-08 11:43:05 -0700 | [diff] [blame] | 6311 | static int updateAudioPortCache(ArrayList<AudioPort> ports, ArrayList<AudioPatch> patches, |
| 6312 | ArrayList<AudioPort> previousPorts) { |
Eric Laurent | c573bc5 | 2015-06-26 10:01:12 -0700 | [diff] [blame] | 6313 | sAudioPortEventHandler.init(); |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6314 | synchronized (sAudioPortGeneration) { |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6315 | |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6316 | if (sAudioPortGeneration == AUDIOPORT_GENERATION_INIT) { |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6317 | int[] patchGeneration = new int[1]; |
| 6318 | int[] portGeneration = new int[1]; |
| 6319 | int status; |
| 6320 | ArrayList<AudioPort> newPorts = new ArrayList<AudioPort>(); |
| 6321 | ArrayList<AudioPatch> newPatches = new ArrayList<AudioPatch>(); |
| 6322 | |
| 6323 | do { |
| 6324 | newPorts.clear(); |
| 6325 | status = AudioSystem.listAudioPorts(newPorts, portGeneration); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6326 | if (status != SUCCESS) { |
Wonsik Kim | b561cce | 2015-01-30 17:48:51 +0900 | [diff] [blame] | 6327 | Log.w(TAG, "updateAudioPortCache: listAudioPorts failed"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6328 | return status; |
| 6329 | } |
| 6330 | newPatches.clear(); |
| 6331 | status = AudioSystem.listAudioPatches(newPatches, patchGeneration); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6332 | if (status != SUCCESS) { |
Wonsik Kim | b561cce | 2015-01-30 17:48:51 +0900 | [diff] [blame] | 6333 | Log.w(TAG, "updateAudioPortCache: listAudioPatches failed"); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6334 | return status; |
| 6335 | } |
jiabin | c4ecaa5 | 2017-09-26 14:28:41 -0700 | [diff] [blame] | 6336 | // Loop until patch generation is the same as port generation unless audio ports |
| 6337 | // and audio patches are not null. |
| 6338 | } while (patchGeneration[0] != portGeneration[0] |
| 6339 | && (ports == null || patches == null)); |
| 6340 | // If the patch generation doesn't equal port generation, return ERROR here in case |
| 6341 | // of mismatch between audio ports and audio patches. |
| 6342 | if (patchGeneration[0] != portGeneration[0]) { |
| 6343 | return ERROR; |
| 6344 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6345 | |
| 6346 | for (int i = 0; i < newPatches.size(); i++) { |
| 6347 | for (int j = 0; j < newPatches.get(i).sources().length; j++) { |
Eric Laurent | b4e0909 | 2014-06-02 19:18:43 -0700 | [diff] [blame] | 6348 | AudioPortConfig portCfg = updatePortConfig(newPatches.get(i).sources()[j], |
| 6349 | newPorts); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6350 | newPatches.get(i).sources()[j] = portCfg; |
| 6351 | } |
| 6352 | for (int j = 0; j < newPatches.get(i).sinks().length; j++) { |
Eric Laurent | b4e0909 | 2014-06-02 19:18:43 -0700 | [diff] [blame] | 6353 | AudioPortConfig portCfg = updatePortConfig(newPatches.get(i).sinks()[j], |
| 6354 | newPorts); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6355 | newPatches.get(i).sinks()[j] = portCfg; |
| 6356 | } |
| 6357 | } |
Wonsik Kim | b561cce | 2015-01-30 17:48:51 +0900 | [diff] [blame] | 6358 | for (Iterator<AudioPatch> i = newPatches.iterator(); i.hasNext(); ) { |
| 6359 | AudioPatch newPatch = i.next(); |
| 6360 | boolean hasInvalidPort = false; |
| 6361 | for (AudioPortConfig portCfg : newPatch.sources()) { |
| 6362 | if (portCfg == null) { |
| 6363 | hasInvalidPort = true; |
| 6364 | break; |
| 6365 | } |
| 6366 | } |
| 6367 | for (AudioPortConfig portCfg : newPatch.sinks()) { |
| 6368 | if (portCfg == null) { |
| 6369 | hasInvalidPort = true; |
| 6370 | break; |
| 6371 | } |
| 6372 | } |
| 6373 | if (hasInvalidPort) { |
| 6374 | // Temporarily remove patches with invalid ports. One who created the patch |
| 6375 | // is responsible for dealing with the port change. |
| 6376 | i.remove(); |
| 6377 | } |
| 6378 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6379 | |
Eric Laurent | 8a1e7a8 | 2015-05-08 11:43:05 -0700 | [diff] [blame] | 6380 | sPreviousAudioPortsCached = sAudioPortsCached; |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6381 | sAudioPortsCached = newPorts; |
| 6382 | sAudioPatchesCached = newPatches; |
| 6383 | sAudioPortGeneration = portGeneration[0]; |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6384 | } |
| 6385 | if (ports != null) { |
| 6386 | ports.clear(); |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6387 | ports.addAll(sAudioPortsCached); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6388 | } |
| 6389 | if (patches != null) { |
| 6390 | patches.clear(); |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6391 | patches.addAll(sAudioPatchesCached); |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6392 | } |
Eric Laurent | 8a1e7a8 | 2015-05-08 11:43:05 -0700 | [diff] [blame] | 6393 | if (previousPorts != null) { |
| 6394 | previousPorts.clear(); |
| 6395 | previousPorts.addAll(sPreviousAudioPortsCached); |
| 6396 | } |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6397 | } |
| 6398 | return SUCCESS; |
| 6399 | } |
| 6400 | |
Eric Laurent | f076db4 | 2015-01-14 13:23:27 -0800 | [diff] [blame] | 6401 | static AudioPortConfig updatePortConfig(AudioPortConfig portCfg, ArrayList<AudioPort> ports) { |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6402 | AudioPort port = portCfg.port(); |
| 6403 | int k; |
| 6404 | for (k = 0; k < ports.size(); k++) { |
| 6405 | // compare handles because the port returned by JNI is not of the correct |
| 6406 | // subclass |
| 6407 | if (ports.get(k).handle().equals(port.handle())) { |
Eric Laurent | b69681c | 2014-05-19 19:02:51 -0700 | [diff] [blame] | 6408 | port = ports.get(k); |
| 6409 | break; |
| 6410 | } |
| 6411 | } |
| 6412 | if (k == ports.size()) { |
| 6413 | // this hould never happen |
| 6414 | Log.e(TAG, "updatePortConfig port not found for handle: "+port.handle().id()); |
| 6415 | return null; |
| 6416 | } |
| 6417 | AudioGainConfig gainCfg = portCfg.gain(); |
| 6418 | if (gainCfg != null) { |
| 6419 | AudioGain gain = port.gain(gainCfg.index()); |
| 6420 | gainCfg = gain.buildConfig(gainCfg.mode(), |
| 6421 | gainCfg.channelMask(), |
| 6422 | gainCfg.values(), |
| 6423 | gainCfg.rampDurationMs()); |
| 6424 | } |
| 6425 | return port.buildConfig(portCfg.samplingRate(), |
| 6426 | portCfg.channelMask(), |
| 6427 | portCfg.format(), |
| 6428 | gainCfg); |
| 6429 | } |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6430 | |
| 6431 | private OnAmPortUpdateListener mPortListener = null; |
| 6432 | |
| 6433 | /** |
| 6434 | * The message sent to apps when the contents of the device list changes if they provide |
Andrew Solovay | 5c05ded | 2018-10-02 14:14:42 -0700 | [diff] [blame] | 6435 | * a {@link Handler} object to addOnAudioDeviceConnectionListener(). |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6436 | */ |
Paul McLean | cbeb8a2 | 2015-06-10 08:21:27 -0700 | [diff] [blame] | 6437 | private final static int MSG_DEVICES_CALLBACK_REGISTERED = 0; |
| 6438 | private final static int MSG_DEVICES_DEVICES_ADDED = 1; |
| 6439 | private final static int MSG_DEVICES_DEVICES_REMOVED = 2; |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6440 | |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6441 | /** |
| 6442 | * The list of {@link AudioDeviceCallback} objects to receive add/remove notifications. |
| 6443 | */ |
Jack He | 89f9798 | 2018-05-02 19:10:56 -0700 | [diff] [blame] | 6444 | private final ArrayMap<AudioDeviceCallback, NativeEventHandlerDelegate> mDeviceCallbacks = |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6445 | new ArrayMap<AudioDeviceCallback, NativeEventHandlerDelegate>(); |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6446 | |
| 6447 | /** |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6448 | * The following are flags to allow users of {@link AudioManager#getDevices(int)} to filter |
| 6449 | * the results list to only those device types they are interested in. |
| 6450 | */ |
| 6451 | /** |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6452 | * Specifies to the {@link AudioManager#getDevices(int)} method to include |
| 6453 | * source (i.e. input) audio devices. |
| 6454 | */ |
| 6455 | public static final int GET_DEVICES_INPUTS = 0x0001; |
| 6456 | |
| 6457 | /** |
| 6458 | * Specifies to the {@link AudioManager#getDevices(int)} method to include |
| 6459 | * sink (i.e. output) audio devices. |
| 6460 | */ |
| 6461 | public static final int GET_DEVICES_OUTPUTS = 0x0002; |
| 6462 | |
Jean-Michel Trivi | 21cd541 | 2019-10-30 08:16:51 -0700 | [diff] [blame] | 6463 | /** @hide */ |
| 6464 | @IntDef(flag = true, prefix = "GET_DEVICES", value = { |
| 6465 | GET_DEVICES_INPUTS, |
| 6466 | GET_DEVICES_OUTPUTS } |
| 6467 | ) |
| 6468 | @Retention(RetentionPolicy.SOURCE) |
| 6469 | public @interface AudioDeviceRole {} |
| 6470 | |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6471 | /** |
| 6472 | * Specifies to the {@link AudioManager#getDevices(int)} method to include both |
| 6473 | * source and sink devices. |
| 6474 | */ |
| 6475 | public static final int GET_DEVICES_ALL = GET_DEVICES_OUTPUTS | GET_DEVICES_INPUTS; |
| 6476 | |
| 6477 | /** |
| 6478 | * Determines if a given AudioDevicePort meets the specified filter criteria. |
| 6479 | * @param port The port to test. |
| 6480 | * @param flags A set of bitflags specifying the criteria to test. |
| 6481 | * @see {@link GET_DEVICES_OUTPUTS} and {@link GET_DEVICES_INPUTS} |
| 6482 | **/ |
| 6483 | private static boolean checkFlags(AudioDevicePort port, int flags) { |
| 6484 | return port.role() == AudioPort.ROLE_SINK && (flags & GET_DEVICES_OUTPUTS) != 0 || |
| 6485 | port.role() == AudioPort.ROLE_SOURCE && (flags & GET_DEVICES_INPUTS) != 0; |
| 6486 | } |
| 6487 | |
Paul McLean | 1135457 | 2015-08-07 12:50:48 -0600 | [diff] [blame] | 6488 | private static boolean checkTypes(AudioDevicePort port) { |
| 6489 | return AudioDeviceInfo.convertInternalDeviceToDeviceType(port.type()) != |
jiabin | 9697c6c | 2018-03-20 17:13:04 -0700 | [diff] [blame] | 6490 | AudioDeviceInfo.TYPE_UNKNOWN; |
Paul McLean | 1135457 | 2015-08-07 12:50:48 -0600 | [diff] [blame] | 6491 | } |
| 6492 | |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6493 | /** |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6494 | * Returns an array of {@link AudioDeviceInfo} objects corresponding to the audio devices |
| 6495 | * currently connected to the system and meeting the criteria specified in the |
| 6496 | * <code>flags</code> parameter. |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6497 | * @param flags A set of bitflags specifying the criteria to test. |
Elliot Waite | 54de7747 | 2017-01-11 15:30:35 -0800 | [diff] [blame] | 6498 | * @see #GET_DEVICES_OUTPUTS |
| 6499 | * @see #GET_DEVICES_INPUTS |
| 6500 | * @see #GET_DEVICES_ALL |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6501 | * @return A (possibly zero-length) array of AudioDeviceInfo objects. |
| 6502 | */ |
Jean-Michel Trivi | 21cd541 | 2019-10-30 08:16:51 -0700 | [diff] [blame] | 6503 | public AudioDeviceInfo[] getDevices(@AudioDeviceRole int flags) { |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6504 | return getDevicesStatic(flags); |
| 6505 | } |
| 6506 | |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6507 | /** |
| 6508 | * Does the actual computation to generate an array of (externally-visible) AudioDeviceInfo |
| 6509 | * objects from the current (internal) AudioDevicePort list. |
| 6510 | */ |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6511 | private static AudioDeviceInfo[] |
| 6512 | infoListFromPortList(ArrayList<AudioDevicePort> ports, int flags) { |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6513 | |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6514 | // figure out how many AudioDeviceInfo we need space for... |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6515 | int numRecs = 0; |
| 6516 | for (AudioDevicePort port : ports) { |
Paul McLean | 1135457 | 2015-08-07 12:50:48 -0600 | [diff] [blame] | 6517 | if (checkTypes(port) && checkFlags(port, flags)) { |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6518 | numRecs++; |
| 6519 | } |
| 6520 | } |
| 6521 | |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6522 | // Now load them up... |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6523 | AudioDeviceInfo[] deviceList = new AudioDeviceInfo[numRecs]; |
| 6524 | int slot = 0; |
| 6525 | for (AudioDevicePort port : ports) { |
Paul McLean | 1135457 | 2015-08-07 12:50:48 -0600 | [diff] [blame] | 6526 | if (checkTypes(port) && checkFlags(port, flags)) { |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6527 | deviceList[slot++] = new AudioDeviceInfo(port); |
| 6528 | } |
| 6529 | } |
| 6530 | |
| 6531 | return deviceList; |
| 6532 | } |
| 6533 | |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6534 | /* |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6535 | * Calculate the list of ports that are in ports_B, but not in ports_A. This is used by |
| 6536 | * the add/remove callback mechanism to provide a list of the newly added or removed devices |
| 6537 | * rather than the whole list and make the app figure it out. |
| 6538 | * Note that calling this method with: |
| 6539 | * ports_A == PREVIOUS_ports and ports_B == CURRENT_ports will calculated ADDED ports. |
| 6540 | * ports_A == CURRENT_ports and ports_B == PREVIOUS_ports will calculated REMOVED ports. |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6541 | */ |
| 6542 | private static AudioDeviceInfo[] calcListDeltas( |
| 6543 | ArrayList<AudioDevicePort> ports_A, ArrayList<AudioDevicePort> ports_B, int flags) { |
| 6544 | |
| 6545 | ArrayList<AudioDevicePort> delta_ports = new ArrayList<AudioDevicePort>(); |
| 6546 | |
| 6547 | AudioDevicePort cur_port = null; |
| 6548 | for (int cur_index = 0; cur_index < ports_B.size(); cur_index++) { |
| 6549 | boolean cur_port_found = false; |
| 6550 | cur_port = ports_B.get(cur_index); |
| 6551 | for (int prev_index = 0; |
| 6552 | prev_index < ports_A.size() && !cur_port_found; |
| 6553 | prev_index++) { |
| 6554 | cur_port_found = (cur_port.id() == ports_A.get(prev_index).id()); |
| 6555 | } |
| 6556 | |
| 6557 | if (!cur_port_found) { |
| 6558 | delta_ports.add(cur_port); |
| 6559 | } |
| 6560 | } |
| 6561 | |
| 6562 | return infoListFromPortList(delta_ports, flags); |
| 6563 | } |
| 6564 | |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6565 | /** |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6566 | * Generates a list of AudioDeviceInfo objects corresponding to the audio devices currently |
| 6567 | * connected to the system and meeting the criteria specified in the <code>flags</code> |
| 6568 | * parameter. |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6569 | * This is an internal function. The public API front is getDevices(int). |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6570 | * @param flags A set of bitflags specifying the criteria to test. |
Elliot Waite | 54de7747 | 2017-01-11 15:30:35 -0800 | [diff] [blame] | 6571 | * @see #GET_DEVICES_OUTPUTS |
| 6572 | * @see #GET_DEVICES_INPUTS |
| 6573 | * @see #GET_DEVICES_ALL |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6574 | * @return A (possibly zero-length) array of AudioDeviceInfo objects. |
| 6575 | * @hide |
| 6576 | */ |
| 6577 | public static AudioDeviceInfo[] getDevicesStatic(int flags) { |
| 6578 | ArrayList<AudioDevicePort> ports = new ArrayList<AudioDevicePort>(); |
| 6579 | int status = AudioManager.listAudioDevicePorts(ports); |
| 6580 | if (status != AudioManager.SUCCESS) { |
| 6581 | // fail and bail! |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6582 | return new AudioDeviceInfo[0]; // Always return an array. |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6583 | } |
| 6584 | |
| 6585 | return infoListFromPortList(ports, flags); |
| 6586 | } |
| 6587 | |
| 6588 | /** |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 6589 | * Returns an {@link AudioDeviceInfo} corresponding to the specified {@link AudioPort} ID. |
| 6590 | * @param portId The audio port ID to look up for. |
| 6591 | * @param flags A set of bitflags specifying the criteria to test. |
| 6592 | * @see #GET_DEVICES_OUTPUTS |
| 6593 | * @see #GET_DEVICES_INPUTS |
| 6594 | * @see #GET_DEVICES_ALL |
| 6595 | * @return An AudioDeviceInfo or null if no device with matching port ID is found. |
| 6596 | * @hide |
| 6597 | */ |
| 6598 | public static AudioDeviceInfo getDeviceForPortId(int portId, int flags) { |
| 6599 | if (portId == 0) { |
| 6600 | return null; |
| 6601 | } |
| 6602 | AudioDeviceInfo[] devices = getDevicesStatic(flags); |
| 6603 | for (AudioDeviceInfo device : devices) { |
| 6604 | if (device.getId() == portId) { |
| 6605 | return device; |
| 6606 | } |
| 6607 | } |
| 6608 | return null; |
| 6609 | } |
| 6610 | |
| 6611 | /** |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6612 | * Registers an {@link AudioDeviceCallback} object to receive notifications of changes |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6613 | * to the set of connected audio devices. |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6614 | * @param callback The {@link AudioDeviceCallback} object to receive connect/disconnect |
| 6615 | * notifications. |
| 6616 | * @param handler Specifies the {@link Handler} object for the thread on which to execute |
| 6617 | * the callback. If <code>null</code>, the {@link Handler} associated with the main |
| 6618 | * {@link Looper} will be used. |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6619 | */ |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6620 | public void registerAudioDeviceCallback(AudioDeviceCallback callback, |
Jean-Michel Trivi | 1ec6618 | 2019-12-30 09:53:04 -0800 | [diff] [blame] | 6621 | @Nullable Handler handler) { |
Eric Laurent | 1691f73 | 2015-07-14 16:27:54 -0700 | [diff] [blame] | 6622 | synchronized (mDeviceCallbacks) { |
| 6623 | if (callback != null && !mDeviceCallbacks.containsKey(callback)) { |
Eric Laurent | c573bc5 | 2015-06-26 10:01:12 -0700 | [diff] [blame] | 6624 | if (mDeviceCallbacks.size() == 0) { |
| 6625 | if (mPortListener == null) { |
| 6626 | mPortListener = new OnAmPortUpdateListener(); |
| 6627 | } |
| 6628 | registerAudioPortUpdateListener(mPortListener); |
| 6629 | } |
Paul McLean | cbeb8a2 | 2015-06-10 08:21:27 -0700 | [diff] [blame] | 6630 | NativeEventHandlerDelegate delegate = |
| 6631 | new NativeEventHandlerDelegate(callback, handler); |
| 6632 | mDeviceCallbacks.put(callback, delegate); |
jiabin | 8c3a767 | 2018-05-22 15:44:21 -0700 | [diff] [blame] | 6633 | broadcastDeviceListChange_sync(delegate.getHandler()); |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6634 | } |
| 6635 | } |
| 6636 | } |
| 6637 | |
| 6638 | /** |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6639 | * Unregisters an {@link AudioDeviceCallback} object which has been previously registered |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6640 | * to receive notifications of changes to the set of connected audio devices. |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6641 | * @param callback The {@link AudioDeviceCallback} object that was previously registered |
Elliot Waite | 54de7747 | 2017-01-11 15:30:35 -0800 | [diff] [blame] | 6642 | * with {@link AudioManager#registerAudioDeviceCallback} to be unregistered. |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6643 | */ |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6644 | public void unregisterAudioDeviceCallback(AudioDeviceCallback callback) { |
| 6645 | synchronized (mDeviceCallbacks) { |
| 6646 | if (mDeviceCallbacks.containsKey(callback)) { |
| 6647 | mDeviceCallbacks.remove(callback); |
Eric Laurent | c573bc5 | 2015-06-26 10:01:12 -0700 | [diff] [blame] | 6648 | if (mDeviceCallbacks.size() == 0) { |
| 6649 | unregisterAudioPortUpdateListener(mPortListener); |
| 6650 | } |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6651 | } |
| 6652 | } |
| 6653 | } |
| 6654 | |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 6655 | /** |
| 6656 | * Set port id for microphones by matching device type and address. |
| 6657 | * @hide |
| 6658 | */ |
| 6659 | public static void setPortIdForMicrophones(ArrayList<MicrophoneInfo> microphones) { |
| 6660 | AudioDeviceInfo[] devices = getDevicesStatic(AudioManager.GET_DEVICES_INPUTS); |
| 6661 | for (int i = microphones.size() - 1; i >= 0; i--) { |
| 6662 | boolean foundPortId = false; |
| 6663 | for (AudioDeviceInfo device : devices) { |
| 6664 | if (device.getPort().type() == microphones.get(i).getInternalDeviceType() |
| 6665 | && TextUtils.equals(device.getAddress(), microphones.get(i).getAddress())) { |
| 6666 | microphones.get(i).setId(device.getId()); |
| 6667 | foundPortId = true; |
| 6668 | break; |
| 6669 | } |
| 6670 | } |
| 6671 | if (!foundPortId) { |
| 6672 | Log.i(TAG, "Failed to find port id for device with type:" |
| 6673 | + microphones.get(i).getType() + " address:" |
| 6674 | + microphones.get(i).getAddress()); |
| 6675 | microphones.remove(i); |
| 6676 | } |
| 6677 | } |
| 6678 | } |
| 6679 | |
| 6680 | /** |
jiabin | 589a236 | 2018-02-22 16:21:53 -0800 | [diff] [blame] | 6681 | * Convert {@link AudioDeviceInfo} to {@link MicrophoneInfo}. |
| 6682 | * @hide |
| 6683 | */ |
| 6684 | public static MicrophoneInfo microphoneInfoFromAudioDeviceInfo(AudioDeviceInfo deviceInfo) { |
| 6685 | int deviceType = deviceInfo.getType(); |
| 6686 | int micLocation = (deviceType == AudioDeviceInfo.TYPE_BUILTIN_MIC |
| 6687 | || deviceType == AudioDeviceInfo.TYPE_TELEPHONY) ? MicrophoneInfo.LOCATION_MAINBODY |
| 6688 | : deviceType == AudioDeviceInfo.TYPE_UNKNOWN ? MicrophoneInfo.LOCATION_UNKNOWN |
| 6689 | : MicrophoneInfo.LOCATION_PERIPHERAL; |
| 6690 | MicrophoneInfo microphone = new MicrophoneInfo( |
| 6691 | deviceInfo.getPort().name() + deviceInfo.getId(), |
| 6692 | deviceInfo.getPort().type(), deviceInfo.getAddress(), micLocation, |
| 6693 | MicrophoneInfo.GROUP_UNKNOWN, MicrophoneInfo.INDEX_IN_THE_GROUP_UNKNOWN, |
| 6694 | MicrophoneInfo.POSITION_UNKNOWN, MicrophoneInfo.ORIENTATION_UNKNOWN, |
| 6695 | new ArrayList<Pair<Float, Float>>(), new ArrayList<Pair<Integer, Integer>>(), |
| 6696 | MicrophoneInfo.SENSITIVITY_UNKNOWN, MicrophoneInfo.SPL_UNKNOWN, |
| 6697 | MicrophoneInfo.SPL_UNKNOWN, MicrophoneInfo.DIRECTIONALITY_UNKNOWN); |
| 6698 | microphone.setId(deviceInfo.getId()); |
| 6699 | return microphone; |
| 6700 | } |
| 6701 | |
| 6702 | /** |
jiabin | d0be5b2 | 2018-04-10 14:10:04 -0700 | [diff] [blame] | 6703 | * Add {@link MicrophoneInfo} by device information while filtering certain types. |
| 6704 | */ |
| 6705 | private void addMicrophonesFromAudioDeviceInfo(ArrayList<MicrophoneInfo> microphones, |
| 6706 | HashSet<Integer> filterTypes) { |
| 6707 | AudioDeviceInfo[] devices = getDevicesStatic(GET_DEVICES_INPUTS); |
| 6708 | for (AudioDeviceInfo device : devices) { |
| 6709 | if (filterTypes.contains(device.getType())) { |
| 6710 | continue; |
| 6711 | } |
| 6712 | MicrophoneInfo microphone = microphoneInfoFromAudioDeviceInfo(device); |
| 6713 | microphones.add(microphone); |
| 6714 | } |
| 6715 | } |
| 6716 | |
| 6717 | /** |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 6718 | * Returns a list of {@link MicrophoneInfo} that corresponds to the characteristics |
| 6719 | * of all available microphones. The list is empty when no microphones are available |
| 6720 | * on the device. An error during the query will result in an IOException being thrown. |
| 6721 | * |
| 6722 | * @return a list that contains all microphones' characteristics |
| 6723 | * @throws IOException if an error occurs. |
| 6724 | */ |
| 6725 | public List<MicrophoneInfo> getMicrophones() throws IOException { |
| 6726 | ArrayList<MicrophoneInfo> microphones = new ArrayList<MicrophoneInfo>(); |
| 6727 | int status = AudioSystem.getMicrophones(microphones); |
jiabin | d0be5b2 | 2018-04-10 14:10:04 -0700 | [diff] [blame] | 6728 | HashSet<Integer> filterTypes = new HashSet<>(); |
| 6729 | filterTypes.add(AudioDeviceInfo.TYPE_TELEPHONY); |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 6730 | if (status != AudioManager.SUCCESS) { |
jiabin | d0be5b2 | 2018-04-10 14:10:04 -0700 | [diff] [blame] | 6731 | // fail and populate microphones with unknown characteristics by device information. |
jiabin | a26a762 | 2018-04-11 15:38:46 -0700 | [diff] [blame] | 6732 | if (status != AudioManager.ERROR_INVALID_OPERATION) { |
| 6733 | Log.e(TAG, "getMicrophones failed:" + status); |
| 6734 | } |
| 6735 | Log.i(TAG, "fallback on device info"); |
jiabin | d0be5b2 | 2018-04-10 14:10:04 -0700 | [diff] [blame] | 6736 | addMicrophonesFromAudioDeviceInfo(microphones, filterTypes); |
| 6737 | return microphones; |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 6738 | } |
| 6739 | setPortIdForMicrophones(microphones); |
jiabin | d0be5b2 | 2018-04-10 14:10:04 -0700 | [diff] [blame] | 6740 | filterTypes.add(AudioDeviceInfo.TYPE_BUILTIN_MIC); |
| 6741 | addMicrophonesFromAudioDeviceInfo(microphones, filterTypes); |
jiabin | c0f4944 | 2018-01-05 10:23:50 -0800 | [diff] [blame] | 6742 | return microphones; |
| 6743 | } |
| 6744 | |
Arun Mirpuri | cb102fa | 2019-01-11 18:39:21 -0800 | [diff] [blame] | 6745 | /** |
| 6746 | * Returns a list of audio formats that corresponds to encoding formats |
| 6747 | * supported on offload path for A2DP playback. |
| 6748 | * |
| 6749 | * @return a list of {@link BluetoothCodecConfig} objects containing encoding formats |
| 6750 | * supported for offload A2DP playback |
| 6751 | * @hide |
| 6752 | */ |
| 6753 | public List<BluetoothCodecConfig> getHwOffloadEncodingFormatsSupportedForA2DP() { |
| 6754 | ArrayList<Integer> formatsList = new ArrayList<Integer>(); |
| 6755 | ArrayList<BluetoothCodecConfig> codecConfigList = new ArrayList<BluetoothCodecConfig>(); |
| 6756 | |
| 6757 | int status = AudioSystem.getHwOffloadEncodingFormatsSupportedForA2DP(formatsList); |
| 6758 | if (status != AudioManager.SUCCESS) { |
| 6759 | Log.e(TAG, "getHwOffloadEncodingFormatsSupportedForA2DP failed:" + status); |
| 6760 | return codecConfigList; |
| 6761 | } |
| 6762 | |
| 6763 | for (Integer format : formatsList) { |
| 6764 | int btSourceCodec = AudioSystem.audioFormatToBluetoothSourceCodec(format); |
| 6765 | if (btSourceCodec |
| 6766 | != BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID) { |
| 6767 | codecConfigList.add(new BluetoothCodecConfig(btSourceCodec)); |
| 6768 | } |
| 6769 | } |
| 6770 | return codecConfigList; |
| 6771 | } |
| 6772 | |
Paul McLean | cbeb8a2 | 2015-06-10 08:21:27 -0700 | [diff] [blame] | 6773 | // Since we need to calculate the changes since THE LAST NOTIFICATION, and not since the |
| 6774 | // (unpredictable) last time updateAudioPortCache() was called by someone, keep a list |
| 6775 | // of the ports that exist at the time of the last notification. |
| 6776 | private ArrayList<AudioDevicePort> mPreviousPorts = new ArrayList<AudioDevicePort>(); |
| 6777 | |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6778 | /** |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6779 | * Internal method to compute and generate add/remove messages and then send to any |
jiabin | 8c3a767 | 2018-05-22 15:44:21 -0700 | [diff] [blame] | 6780 | * registered callbacks. Must be called synchronized on mDeviceCallbacks. |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6781 | */ |
jiabin | 8c3a767 | 2018-05-22 15:44:21 -0700 | [diff] [blame] | 6782 | private void broadcastDeviceListChange_sync(Handler handler) { |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6783 | int status; |
| 6784 | |
Paul McLean | cbeb8a2 | 2015-06-10 08:21:27 -0700 | [diff] [blame] | 6785 | // Get the new current set of ports |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6786 | ArrayList<AudioDevicePort> current_ports = new ArrayList<AudioDevicePort>(); |
| 6787 | status = AudioManager.listAudioDevicePorts(current_ports); |
| 6788 | if (status != AudioManager.SUCCESS) { |
| 6789 | return; |
| 6790 | } |
| 6791 | |
Paul McLean | cbeb8a2 | 2015-06-10 08:21:27 -0700 | [diff] [blame] | 6792 | if (handler != null) { |
| 6793 | // This is the callback for the registration, so send the current list |
| 6794 | AudioDeviceInfo[] deviceList = |
| 6795 | infoListFromPortList(current_ports, GET_DEVICES_ALL); |
| 6796 | handler.sendMessage( |
| 6797 | Message.obtain(handler, MSG_DEVICES_CALLBACK_REGISTERED, deviceList)); |
| 6798 | } else { |
| 6799 | AudioDeviceInfo[] added_devices = |
| 6800 | calcListDeltas(mPreviousPorts, current_ports, GET_DEVICES_ALL); |
| 6801 | AudioDeviceInfo[] removed_devices = |
| 6802 | calcListDeltas(current_ports, mPreviousPorts, GET_DEVICES_ALL); |
Paul McLean | cbeb8a2 | 2015-06-10 08:21:27 -0700 | [diff] [blame] | 6803 | if (added_devices.length != 0 || removed_devices.length != 0) { |
jiabin | 8c3a767 | 2018-05-22 15:44:21 -0700 | [diff] [blame] | 6804 | for (int i = 0; i < mDeviceCallbacks.size(); i++) { |
| 6805 | handler = mDeviceCallbacks.valueAt(i).getHandler(); |
| 6806 | if (handler != null) { |
| 6807 | if (removed_devices.length != 0) { |
| 6808 | handler.sendMessage(Message.obtain(handler, |
| 6809 | MSG_DEVICES_DEVICES_REMOVED, |
| 6810 | removed_devices)); |
| 6811 | } |
| 6812 | if (added_devices.length != 0) { |
| 6813 | handler.sendMessage(Message.obtain(handler, |
| 6814 | MSG_DEVICES_DEVICES_ADDED, |
| 6815 | added_devices)); |
Paul McLean | cbeb8a2 | 2015-06-10 08:21:27 -0700 | [diff] [blame] | 6816 | } |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 6817 | } |
| 6818 | } |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6819 | } |
| 6820 | } |
Paul McLean | cbeb8a2 | 2015-06-10 08:21:27 -0700 | [diff] [blame] | 6821 | |
| 6822 | mPreviousPorts = current_ports; |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6823 | } |
| 6824 | |
| 6825 | /** |
| 6826 | * Handles Port list update notifications from the AudioManager |
| 6827 | */ |
| 6828 | private class OnAmPortUpdateListener implements AudioManager.OnAudioPortUpdateListener { |
| 6829 | static final String TAG = "OnAmPortUpdateListener"; |
| 6830 | public void onAudioPortListUpdate(AudioPort[] portList) { |
jiabin | 8c3a767 | 2018-05-22 15:44:21 -0700 | [diff] [blame] | 6831 | synchronized (mDeviceCallbacks) { |
| 6832 | broadcastDeviceListChange_sync(null); |
| 6833 | } |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6834 | } |
| 6835 | |
| 6836 | /** |
| 6837 | * Callback method called upon audio patch list update. |
Paul McLean | 8e6c9f4 | 2015-05-19 11:13:41 -0700 | [diff] [blame] | 6838 | * Note: We don't do anything with Patches at this time, so ignore this notification. |
| 6839 | * @param patchList the updated list of audio patches. |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6840 | */ |
| 6841 | public void onAudioPatchListUpdate(AudioPatch[] patchList) {} |
| 6842 | |
| 6843 | /** |
| 6844 | * Callback method called when the mediaserver dies |
| 6845 | */ |
| 6846 | public void onServiceDied() { |
jiabin | 8c3a767 | 2018-05-22 15:44:21 -0700 | [diff] [blame] | 6847 | synchronized (mDeviceCallbacks) { |
| 6848 | broadcastDeviceListChange_sync(null); |
| 6849 | } |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 6850 | } |
| 6851 | } |
| 6852 | |
Eric Laurent | 1d3cdce | 2018-01-20 10:31:21 -0800 | [diff] [blame] | 6853 | |
| 6854 | /** |
| 6855 | * @hide |
| 6856 | * Abstract class to receive event notification about audioserver process state. |
| 6857 | */ |
| 6858 | @SystemApi |
| 6859 | public abstract static class AudioServerStateCallback { |
| 6860 | public void onAudioServerDown() { } |
| 6861 | public void onAudioServerUp() { } |
| 6862 | } |
| 6863 | |
| 6864 | private Executor mAudioServerStateExec; |
| 6865 | private AudioServerStateCallback mAudioServerStateCb; |
| 6866 | private final Object mAudioServerStateCbLock = new Object(); |
| 6867 | |
| 6868 | private final IAudioServerStateDispatcher mAudioServerStateDispatcher = |
| 6869 | new IAudioServerStateDispatcher.Stub() { |
| 6870 | @Override |
| 6871 | public void dispatchAudioServerStateChange(boolean state) { |
| 6872 | Executor exec; |
| 6873 | AudioServerStateCallback cb; |
| 6874 | |
| 6875 | synchronized (mAudioServerStateCbLock) { |
| 6876 | exec = mAudioServerStateExec; |
| 6877 | cb = mAudioServerStateCb; |
| 6878 | } |
| 6879 | |
| 6880 | if ((exec == null) || (cb == null)) { |
| 6881 | return; |
| 6882 | } |
| 6883 | if (state) { |
| 6884 | exec.execute(() -> cb.onAudioServerUp()); |
| 6885 | } else { |
| 6886 | exec.execute(() -> cb.onAudioServerDown()); |
| 6887 | } |
| 6888 | } |
| 6889 | }; |
| 6890 | |
| 6891 | /** |
| 6892 | * @hide |
| 6893 | * Registers a callback for notification of audio server state changes. |
| 6894 | * @param executor {@link Executor} to handle the callbacks |
| 6895 | * @param stateCallback the callback to receive the audio server state changes |
| 6896 | * To remove the callabck, pass a null reference for both executor and stateCallback. |
| 6897 | */ |
| 6898 | @SystemApi |
| 6899 | public void setAudioServerStateCallback(@NonNull Executor executor, |
| 6900 | @NonNull AudioServerStateCallback stateCallback) { |
| 6901 | if (stateCallback == null) { |
| 6902 | throw new IllegalArgumentException("Illegal null AudioServerStateCallback"); |
| 6903 | } |
| 6904 | if (executor == null) { |
| 6905 | throw new IllegalArgumentException( |
| 6906 | "Illegal null Executor for the AudioServerStateCallback"); |
| 6907 | } |
| 6908 | |
| 6909 | synchronized (mAudioServerStateCbLock) { |
| 6910 | if (mAudioServerStateCb != null) { |
| 6911 | throw new IllegalStateException( |
| 6912 | "setAudioServerStateCallback called with already registered callabck"); |
| 6913 | } |
| 6914 | final IAudioService service = getService(); |
| 6915 | try { |
| 6916 | service.registerAudioServerStateDispatcher(mAudioServerStateDispatcher); |
| 6917 | } catch (RemoteException e) { |
| 6918 | throw e.rethrowFromSystemServer(); |
| 6919 | } |
| 6920 | mAudioServerStateExec = executor; |
| 6921 | mAudioServerStateCb = stateCallback; |
| 6922 | } |
| 6923 | } |
| 6924 | |
| 6925 | /** |
| 6926 | * @hide |
| 6927 | * Unregisters the callback for notification of audio server state changes. |
| 6928 | */ |
| 6929 | @SystemApi |
| 6930 | public void clearAudioServerStateCallback() { |
| 6931 | synchronized (mAudioServerStateCbLock) { |
| 6932 | if (mAudioServerStateCb != null) { |
| 6933 | final IAudioService service = getService(); |
| 6934 | try { |
| 6935 | service.unregisterAudioServerStateDispatcher( |
| 6936 | mAudioServerStateDispatcher); |
| 6937 | } catch (RemoteException e) { |
| 6938 | throw e.rethrowFromSystemServer(); |
| 6939 | } |
| 6940 | } |
| 6941 | mAudioServerStateExec = null; |
| 6942 | mAudioServerStateCb = null; |
| 6943 | } |
| 6944 | } |
| 6945 | |
| 6946 | /** |
| 6947 | * @hide |
| 6948 | * Checks if native audioservice is running or not. |
| 6949 | * @return true if native audioservice runs, false otherwise. |
| 6950 | */ |
| 6951 | @SystemApi |
| 6952 | public boolean isAudioServerRunning() { |
| 6953 | final IAudioService service = getService(); |
| 6954 | try { |
| 6955 | return service.isAudioServerRunning(); |
| 6956 | } catch (RemoteException e) { |
| 6957 | throw e.rethrowFromSystemServer(); |
| 6958 | } |
| 6959 | } |
| 6960 | |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 6961 | /** |
Kriti Dang | 527e66c | 2021-03-04 10:37:22 +0100 | [diff] [blame] | 6962 | * Sets the surround sound mode. |
| 6963 | * |
| 6964 | * @return true if successful, otherwise false |
| 6965 | */ |
| 6966 | @RequiresPermission(android.Manifest.permission.WRITE_SETTINGS) |
| 6967 | public boolean setEncodedSurroundMode(@EncodedSurroundOutputMode int mode) { |
| 6968 | try { |
| 6969 | return getService().setEncodedSurroundMode(mode); |
| 6970 | } catch (RemoteException e) { |
| 6971 | throw e.rethrowFromSystemServer(); |
| 6972 | } |
| 6973 | } |
| 6974 | |
| 6975 | /** |
| 6976 | * Gets the surround sound mode. |
| 6977 | * |
| 6978 | * @return true if successful, otherwise false |
| 6979 | */ |
Kriti Dang | 527e66c | 2021-03-04 10:37:22 +0100 | [diff] [blame] | 6980 | public @EncodedSurroundOutputMode int getEncodedSurroundMode() { |
| 6981 | try { |
Kriti Dang | 98fdb26 | 2021-04-01 13:26:00 +0200 | [diff] [blame] | 6982 | return getService().getEncodedSurroundMode( |
| 6983 | getContext().getApplicationInfo().targetSdkVersion); |
Kriti Dang | 527e66c | 2021-03-04 10:37:22 +0100 | [diff] [blame] | 6984 | } catch (RemoteException e) { |
| 6985 | throw e.rethrowFromSystemServer(); |
| 6986 | } |
| 6987 | } |
| 6988 | |
| 6989 | /** |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 6990 | * @hide |
| 6991 | * Returns all surround formats. |
| 6992 | * @return a map where the key is a surround format and |
| 6993 | * the value indicates the surround format is enabled or not |
| 6994 | */ |
Marin Shalamanov | 49e778e | 2021-06-02 14:12:41 +0200 | [diff] [blame] | 6995 | @TestApi |
| 6996 | @NonNull |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 6997 | public Map<Integer, Boolean> getSurroundFormats() { |
Kriti Dang | 1380c0e | 2021-06-04 14:51:48 +0200 | [diff] [blame] | 6998 | try { |
| 6999 | return getService().getSurroundFormats(); |
| 7000 | } catch (RemoteException e) { |
| 7001 | throw e.rethrowFromSystemServer(); |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 7002 | } |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 7003 | } |
| 7004 | |
| 7005 | /** |
Kriti Dang | 3f296bd | 2021-05-31 15:54:44 +0200 | [diff] [blame] | 7006 | * Sets and persists a certain surround format as enabled or not. |
| 7007 | * <p> |
| 7008 | * This API is called by TvSettings surround sound menu when user enables or disables a |
| 7009 | * surround sound format. This setting is persisted as global user setting. |
| 7010 | * Applications should revert their changes to surround sound settings unless they intend to |
| 7011 | * modify the global user settings across all apps. The framework does not auto-revert an |
| 7012 | * application's settings after a lifecycle event. Audio focus is not required to apply these |
| 7013 | * settings. |
Kriti Dang | 1985c45 | 2021-05-10 17:06:44 +0200 | [diff] [blame] | 7014 | * |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 7015 | * @param enabled the required surround format state, true for enabled, false for disabled |
| 7016 | * @return true if successful, otherwise false |
| 7017 | */ |
Kriti Dang | 527e66c | 2021-03-04 10:37:22 +0100 | [diff] [blame] | 7018 | @RequiresPermission(android.Manifest.permission.WRITE_SETTINGS) |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 7019 | public boolean setSurroundFormatEnabled( |
| 7020 | @AudioFormat.SurroundSoundEncoding int audioFormat, boolean enabled) { |
Kriti Dang | 527e66c | 2021-03-04 10:37:22 +0100 | [diff] [blame] | 7021 | try { |
| 7022 | return getService().setSurroundFormatEnabled(audioFormat, enabled); |
| 7023 | } catch (RemoteException e) { |
| 7024 | throw e.rethrowFromSystemServer(); |
| 7025 | } |
| 7026 | } |
| 7027 | |
| 7028 | /** |
| 7029 | * Gets whether a certain surround format is enabled or not. |
| 7030 | * @param audioFormat a surround format |
| 7031 | * |
| 7032 | * @return whether the required surround format is enabled |
| 7033 | */ |
Kriti Dang | 527e66c | 2021-03-04 10:37:22 +0100 | [diff] [blame] | 7034 | public boolean isSurroundFormatEnabled(@AudioFormat.SurroundSoundEncoding int audioFormat) { |
| 7035 | try { |
| 7036 | return getService().isSurroundFormatEnabled(audioFormat); |
| 7037 | } catch (RemoteException e) { |
| 7038 | throw e.rethrowFromSystemServer(); |
| 7039 | } |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 7040 | } |
| 7041 | |
| 7042 | /** |
| 7043 | * @hide |
| 7044 | * Returns all surround formats that are reported by the connected HDMI device. |
Kriti Dang | 0192423 | 2021-03-02 13:51:09 +0100 | [diff] [blame] | 7045 | * The return values are not affected by calling setSurroundFormatEnabled. |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 7046 | * |
Kriti Dang | 0192423 | 2021-03-02 13:51:09 +0100 | [diff] [blame] | 7047 | * @return a list of surround formats |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 7048 | */ |
Kriti Dang | 1380c0e | 2021-06-04 14:51:48 +0200 | [diff] [blame] | 7049 | @TestApi |
| 7050 | @NonNull |
| 7051 | public List<Integer> getReportedSurroundFormats() { |
| 7052 | try { |
| 7053 | return getService().getReportedSurroundFormats(); |
| 7054 | } catch (RemoteException e) { |
| 7055 | throw e.rethrowFromSystemServer(); |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 7056 | } |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 7057 | } |
| 7058 | |
jiabin | 66f9e72 | 2018-11-02 16:20:19 -0700 | [diff] [blame] | 7059 | /** |
| 7060 | * Return if audio haptic coupled playback is supported or not. |
| 7061 | * |
| 7062 | * @return whether audio haptic playback supported. |
| 7063 | */ |
| 7064 | public static boolean isHapticPlaybackSupported() { |
| 7065 | return AudioSystem.isHapticPlaybackSupported(); |
| 7066 | } |
| 7067 | |
François Gaffie | 0699fec | 2018-07-09 14:35:10 +0200 | [diff] [blame] | 7068 | /** |
| 7069 | * @hide |
| 7070 | * Introspection API to retrieve audio product strategies. |
| 7071 | * When implementing {Car|Oem}AudioManager, use this method to retrieve the collection of |
| 7072 | * audio product strategies, which is indexed by a weakly typed index in order to be extended |
| 7073 | * by OEM without any needs of AOSP patches. |
| 7074 | * The {Car|Oem}AudioManager can expose API to build {@link AudioAttributes} for a given product |
| 7075 | * strategy refered either by its index or human readable string. It will allow clients |
| 7076 | * application to start streaming data using these {@link AudioAttributes} on the selected |
| 7077 | * device by Audio Policy Engine. |
| 7078 | * @return a (possibly zero-length) array of |
| 7079 | * {@see android.media.audiopolicy.AudioProductStrategy} objects. |
| 7080 | */ |
| 7081 | @SystemApi |
Hayden Gomes | 6d69bde | 2019-04-04 13:10:13 -0700 | [diff] [blame] | 7082 | @NonNull |
François Gaffie | 0699fec | 2018-07-09 14:35:10 +0200 | [diff] [blame] | 7083 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
Hayden Gomes | 6d69bde | 2019-04-04 13:10:13 -0700 | [diff] [blame] | 7084 | public static List<AudioProductStrategy> getAudioProductStrategies() { |
François Gaffie | 0699fec | 2018-07-09 14:35:10 +0200 | [diff] [blame] | 7085 | final IAudioService service = getService(); |
| 7086 | try { |
| 7087 | return service.getAudioProductStrategies(); |
| 7088 | } catch (RemoteException e) { |
| 7089 | throw e.rethrowFromSystemServer(); |
| 7090 | } |
| 7091 | } |
| 7092 | |
François Gaffie | adcd00a | 2018-09-18 17:06:26 +0200 | [diff] [blame] | 7093 | /** |
| 7094 | * @hide |
| 7095 | * Introspection API to retrieve audio volume groups. |
| 7096 | * When implementing {Car|Oem}AudioManager, use this method to retrieve the collection of |
| 7097 | * audio volume groups. |
Hayden Gomes | ebd6aaa | 2019-04-04 13:14:21 -0700 | [diff] [blame] | 7098 | * @return a (possibly zero-length) List of |
| 7099 | * {@see android.media.audiopolicy.AudioVolumeGroup} objects. |
François Gaffie | adcd00a | 2018-09-18 17:06:26 +0200 | [diff] [blame] | 7100 | */ |
| 7101 | @SystemApi |
Hayden Gomes | ebd6aaa | 2019-04-04 13:14:21 -0700 | [diff] [blame] | 7102 | @NonNull |
François Gaffie | adcd00a | 2018-09-18 17:06:26 +0200 | [diff] [blame] | 7103 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
Hayden Gomes | ebd6aaa | 2019-04-04 13:14:21 -0700 | [diff] [blame] | 7104 | public static List<AudioVolumeGroup> getAudioVolumeGroups() { |
François Gaffie | 9c36210 | 2018-09-21 17:43:52 +0200 | [diff] [blame] | 7105 | final IAudioService service = getService(); |
| 7106 | try { |
Hayden Gomes | ebd6aaa | 2019-04-04 13:14:21 -0700 | [diff] [blame] | 7107 | return service.getAudioVolumeGroups(); |
François Gaffie | 9c36210 | 2018-09-21 17:43:52 +0200 | [diff] [blame] | 7108 | } catch (RemoteException e) { |
| 7109 | throw e.rethrowFromSystemServer(); |
| 7110 | } |
François Gaffie | adcd00a | 2018-09-18 17:06:26 +0200 | [diff] [blame] | 7111 | } |
| 7112 | |
| 7113 | /** |
| 7114 | * @hide |
| 7115 | * Callback registered by client to be notified upon volume group change. |
| 7116 | */ |
| 7117 | @SystemApi |
| 7118 | public abstract static class VolumeGroupCallback { |
| 7119 | /** |
| 7120 | * Callback method called upon audio volume group change. |
| 7121 | * @param group the group for which the volume has changed |
| 7122 | */ |
| 7123 | public void onAudioVolumeGroupChanged(int group, int flags) {} |
| 7124 | } |
| 7125 | |
| 7126 | /** |
| 7127 | * @hide |
| 7128 | * Register an audio volume group change listener. |
| 7129 | * @param callback the {@link VolumeGroupCallback} to register |
| 7130 | */ |
| 7131 | @SystemApi |
| 7132 | public void registerVolumeGroupCallback( |
| 7133 | @NonNull Executor executor, |
| 7134 | @NonNull VolumeGroupCallback callback) { |
| 7135 | Preconditions.checkNotNull(executor, "executor must not be null"); |
| 7136 | Preconditions.checkNotNull(callback, "volume group change cb must not be null"); |
| 7137 | sAudioAudioVolumeGroupChangedHandler.init(); |
| 7138 | // TODO: make use of executor |
| 7139 | sAudioAudioVolumeGroupChangedHandler.registerListener(callback); |
| 7140 | } |
| 7141 | |
| 7142 | /** |
| 7143 | * @hide |
| 7144 | * Unregister an audio volume group change listener. |
| 7145 | * @param callback the {@link VolumeGroupCallback} to unregister |
| 7146 | */ |
| 7147 | @SystemApi |
| 7148 | public void unregisterVolumeGroupCallback( |
| 7149 | @NonNull VolumeGroupCallback callback) { |
| 7150 | Preconditions.checkNotNull(callback, "volume group change cb must not be null"); |
| 7151 | sAudioAudioVolumeGroupChangedHandler.unregisterListener(callback); |
| 7152 | } |
jiabin | 3994075 | 2018-04-02 18:18:45 -0700 | [diff] [blame] | 7153 | |
jiabin | ad22520 | 2019-03-20 15:22:50 -0700 | [diff] [blame] | 7154 | /** |
| 7155 | * Return if an asset contains haptic channels or not. |
jiabin | cfcf103 | 2021-07-01 16:30:50 -0700 | [diff] [blame] | 7156 | * |
| 7157 | * @param context the {@link Context} to resolve the uri. |
jiabin | ad22520 | 2019-03-20 15:22:50 -0700 | [diff] [blame] | 7158 | * @param uri the {@link Uri} of the asset. |
| 7159 | * @return true if the assert contains haptic channels. |
| 7160 | * @hide |
| 7161 | */ |
jiabin | cfcf103 | 2021-07-01 16:30:50 -0700 | [diff] [blame] | 7162 | public static boolean hasHapticChannelsImpl(@NonNull Context context, @NonNull Uri uri) { |
| 7163 | MediaExtractor extractor = new MediaExtractor(); |
jiabin | ad22520 | 2019-03-20 15:22:50 -0700 | [diff] [blame] | 7164 | try { |
jiabin | cfcf103 | 2021-07-01 16:30:50 -0700 | [diff] [blame] | 7165 | extractor.setDataSource(context, uri, null); |
| 7166 | for (int i = 0; i < extractor.getTrackCount(); i++) { |
| 7167 | MediaFormat format = extractor.getTrackFormat(i); |
| 7168 | if (format.containsKey(MediaFormat.KEY_HAPTIC_CHANNEL_COUNT) |
| 7169 | && format.getInteger(MediaFormat.KEY_HAPTIC_CHANNEL_COUNT) > 0) { |
| 7170 | return true; |
| 7171 | } |
| 7172 | } |
| 7173 | } catch (IOException e) { |
| 7174 | Log.e(TAG, "hasHapticChannels failure:" + e); |
| 7175 | } |
| 7176 | return false; |
| 7177 | } |
| 7178 | |
| 7179 | /** |
| 7180 | * Return if an asset contains haptic channels or not. |
| 7181 | * |
| 7182 | * @param context the {@link Context} to resolve the uri. |
| 7183 | * @param uri the {@link Uri} of the asset. |
| 7184 | * @return true if the assert contains haptic channels. |
| 7185 | * @hide |
| 7186 | */ |
| 7187 | public static boolean hasHapticChannels(@Nullable Context context, @NonNull Uri uri) { |
| 7188 | Objects.requireNonNull(uri); |
jiabin | 0f3339c | 2021-07-09 11:50:07 -0700 | [diff] [blame] | 7189 | |
jiabin | cfcf103 | 2021-07-01 16:30:50 -0700 | [diff] [blame] | 7190 | if (context != null) { |
| 7191 | return hasHapticChannelsImpl(context, uri); |
jiabin | 0f3339c | 2021-07-09 11:50:07 -0700 | [diff] [blame] | 7192 | } |
| 7193 | |
| 7194 | Context cachedContext = sContext.get(); |
| 7195 | if (cachedContext != null) { |
jiabin | cfcf103 | 2021-07-01 16:30:50 -0700 | [diff] [blame] | 7196 | if (DEBUG) { |
| 7197 | Log.d(TAG, "Try to use static context to query if having haptic channels"); |
| 7198 | } |
jiabin | 0f3339c | 2021-07-09 11:50:07 -0700 | [diff] [blame] | 7199 | return hasHapticChannelsImpl(cachedContext, uri); |
| 7200 | } |
| 7201 | |
| 7202 | // Try with audio service context, this may fail to get correct result. |
| 7203 | if (DEBUG) { |
| 7204 | Log.d(TAG, "Try to use audio service context to query if having haptic channels"); |
| 7205 | } |
| 7206 | try { |
| 7207 | return getService().hasHapticChannels(uri); |
| 7208 | } catch (RemoteException e) { |
| 7209 | throw e.rethrowFromSystemServer(); |
jiabin | ad22520 | 2019-03-20 15:22:50 -0700 | [diff] [blame] | 7210 | } |
| 7211 | } |
| 7212 | |
Kohsuke Yatoh | 900e1f1 | 2020-03-25 08:05:49 -0700 | [diff] [blame] | 7213 | /** |
| 7214 | * Set whether or not there is an active RTT call. |
| 7215 | * This method should be called by Telecom service. |
| 7216 | * @hide |
| 7217 | * TODO: make this a @SystemApi |
| 7218 | */ |
| 7219 | public static void setRttEnabled(boolean rttEnabled) { |
| 7220 | try { |
| 7221 | getService().setRttEnabled(rttEnabled); |
| 7222 | } catch (RemoteException e) { |
| 7223 | throw e.rethrowFromSystemServer(); |
| 7224 | } |
| 7225 | } |
| 7226 | |
Jin Seok Park | 16aeba38 | 2020-08-06 12:52:54 +0900 | [diff] [blame] | 7227 | /** |
| 7228 | * Adjusts the volume of the most relevant stream, or the given fallback |
| 7229 | * stream. |
| 7230 | * <p> |
| 7231 | * This method should only be used by applications that replace the |
| 7232 | * platform-wide management of audio settings or the main telephony |
| 7233 | * application. |
| 7234 | * <p> |
| 7235 | * This method has no effect if the device implements a fixed volume policy |
| 7236 | * as indicated by {@link #isVolumeFixed()}. |
| 7237 | * <p>This API checks if the caller has the necessary permissions based on the provided |
| 7238 | * component name, uid, and pid values. |
| 7239 | * See {@link #adjustSuggestedStreamVolume(int, int, int)}. |
| 7240 | * |
| 7241 | * @param suggestedStreamType The stream type that will be used if there |
| 7242 | * isn't a relevant stream. {@link #USE_DEFAULT_STREAM_TYPE} is |
| 7243 | * valid here. |
| 7244 | * @param direction The direction to adjust the volume. One of |
| 7245 | * {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, |
| 7246 | * {@link #ADJUST_SAME}, {@link #ADJUST_MUTE}, |
| 7247 | * {@link #ADJUST_UNMUTE}, or {@link #ADJUST_TOGGLE_MUTE}. |
| 7248 | * @param flags One or more flags. |
| 7249 | * @param packageName the package name of client application |
| 7250 | * @param uid the uid of client application |
| 7251 | * @param pid the pid of client application |
| 7252 | * @param targetSdkVersion the target sdk version of client application |
| 7253 | * @see #adjustVolume(int, int) |
| 7254 | * @see #adjustStreamVolume(int, int, int) |
| 7255 | * @see #setStreamVolume(int, int, int) |
| 7256 | * @see #isVolumeFixed() |
| 7257 | * |
| 7258 | * @hide |
| 7259 | */ |
| 7260 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 7261 | public void adjustSuggestedStreamVolumeForUid(int suggestedStreamType, int direction, int flags, |
| 7262 | @NonNull String packageName, int uid, int pid, int targetSdkVersion) { |
| 7263 | try { |
| 7264 | getService().adjustSuggestedStreamVolumeForUid(suggestedStreamType, direction, flags, |
| 7265 | packageName, uid, pid, UserHandle.getUserHandleForUid(uid), targetSdkVersion); |
| 7266 | } catch (RemoteException e) { |
| 7267 | throw e.rethrowFromSystemServer(); |
| 7268 | } |
| 7269 | } |
| 7270 | |
| 7271 | /** |
| 7272 | * Adjusts the volume of a particular stream by one step in a direction. |
| 7273 | * <p> |
| 7274 | * This method should only be used by applications that replace the platform-wide |
| 7275 | * management of audio settings or the main telephony application. |
| 7276 | * <p>This method has no effect if the device implements a fixed volume policy |
| 7277 | * as indicated by {@link #isVolumeFixed()}. |
| 7278 | * <p>From N onward, ringer mode adjustments that would toggle Do Not Disturb are not allowed |
| 7279 | * unless the app has been granted Do Not Disturb Access. |
| 7280 | * See {@link NotificationManager#isNotificationPolicyAccessGranted()}. |
| 7281 | * <p>This API checks if the caller has the necessary permissions based on the provided |
| 7282 | * component name, uid, and pid values. |
| 7283 | * See {@link #adjustStreamVolume(int, int, int)}. |
| 7284 | * |
| 7285 | * @param streamType The stream type to adjust. One of {@link #STREAM_VOICE_CALL}, |
| 7286 | * {@link #STREAM_SYSTEM}, {@link #STREAM_RING}, {@link #STREAM_MUSIC}, |
| 7287 | * {@link #STREAM_ALARM} or {@link #STREAM_ACCESSIBILITY}. |
| 7288 | * @param direction The direction to adjust the volume. One of |
| 7289 | * {@link #ADJUST_LOWER}, {@link #ADJUST_RAISE}, or |
| 7290 | * {@link #ADJUST_SAME}. |
| 7291 | * @param flags One or more flags. |
| 7292 | * @param packageName the package name of client application |
| 7293 | * @param uid the uid of client application |
| 7294 | * @param pid the pid of client application |
| 7295 | * @param targetSdkVersion the target sdk version of client application |
| 7296 | * @see #adjustVolume(int, int) |
| 7297 | * @see #setStreamVolume(int, int, int) |
| 7298 | * @throws SecurityException if the adjustment triggers a Do Not Disturb change |
| 7299 | * and the caller is not granted notification policy access. |
| 7300 | * |
| 7301 | * @hide |
| 7302 | */ |
| 7303 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 7304 | public void adjustStreamVolumeForUid(int streamType, int direction, int flags, |
| 7305 | @NonNull String packageName, int uid, int pid, int targetSdkVersion) { |
| 7306 | try { |
| 7307 | getService().adjustStreamVolumeForUid(streamType, direction, flags, packageName, uid, |
| 7308 | pid, UserHandle.getUserHandleForUid(uid), targetSdkVersion); |
| 7309 | } catch (RemoteException e) { |
| 7310 | throw e.rethrowFromSystemServer(); |
| 7311 | } |
| 7312 | } |
| 7313 | |
| 7314 | /** |
| 7315 | * Sets the volume index for a particular stream. |
| 7316 | * <p>This method has no effect if the device implements a fixed volume policy |
| 7317 | * as indicated by {@link #isVolumeFixed()}. |
| 7318 | * <p>From N onward, volume adjustments that would toggle Do Not Disturb are not allowed unless |
| 7319 | * the app has been granted Do Not Disturb Access. |
| 7320 | * See {@link NotificationManager#isNotificationPolicyAccessGranted()}. |
| 7321 | * <p>This API checks if the caller has the necessary permissions based on the provided |
| 7322 | * component name, uid, and pid values. |
| 7323 | * See {@link #setStreamVolume(int, int, int)}. |
| 7324 | * |
| 7325 | * @param streamType The stream whose volume index should be set. |
| 7326 | * @param index The volume index to set. See |
| 7327 | * {@link #getStreamMaxVolume(int)} for the largest valid value. |
| 7328 | * @param flags One or more flags. |
| 7329 | * @param packageName the package name of client application |
| 7330 | * @param uid the uid of client application |
| 7331 | * @param pid the pid of client application |
| 7332 | * @param targetSdkVersion the target sdk version of client application |
| 7333 | * @see #getStreamMaxVolume(int) |
| 7334 | * @see #getStreamVolume(int) |
| 7335 | * @see #isVolumeFixed() |
| 7336 | * @throws SecurityException if the volume change triggers a Do Not Disturb change |
| 7337 | * and the caller is not granted notification policy access. |
| 7338 | * |
| 7339 | * @hide |
| 7340 | */ |
| 7341 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 7342 | public void setStreamVolumeForUid(int streamType, int index, int flags, |
| 7343 | @NonNull String packageName, int uid, int pid, int targetSdkVersion) { |
| 7344 | try { |
| 7345 | getService().setStreamVolumeForUid(streamType, index, flags, packageName, uid, pid, |
| 7346 | UserHandle.getUserHandleForUid(uid), targetSdkVersion); |
| 7347 | } catch (RemoteException e) { |
| 7348 | throw e.rethrowFromSystemServer(); |
| 7349 | } |
| 7350 | } |
| 7351 | |
| 7352 | |
hjin81.lee | 4e984e5 | 2019-12-05 14:34:52 +0900 | [diff] [blame] | 7353 | /** @hide |
| 7354 | * TODO: make this a @SystemApi */ |
| 7355 | @RequiresPermission(android.Manifest.permission.MODIFY_AUDIO_ROUTING) |
| 7356 | public void setMultiAudioFocusEnabled(boolean enabled) { |
| 7357 | try { |
| 7358 | getService().setMultiAudioFocusEnabled(enabled); |
| 7359 | } catch (RemoteException e) { |
| 7360 | throw e.rethrowFromSystemServer(); |
| 7361 | } |
| 7362 | } |
| 7363 | |
Eric Laurent | 43a78de | 2020-07-24 17:11:15 -0700 | [diff] [blame] | 7364 | |
| 7365 | /** |
| 7366 | * Retrieves the Hardware A/V synchronization ID corresponding to the given audio session ID. |
| 7367 | * For more details on Hardware A/V synchronization please refer to |
| 7368 | * <a href="https://source.android.com/devices/tv/multimedia-tunneling/"> |
| 7369 | * media tunneling documentation</a>. |
| 7370 | * @param sessionId the audio session ID for which the HW A/V sync ID is retrieved. |
| 7371 | * @return the HW A/V sync ID for this audio session (an integer different from 0). |
| 7372 | * @throws UnsupportedOperationException if HW A/V synchronization is not supported. |
| 7373 | */ |
| 7374 | public int getAudioHwSyncForSession(int sessionId) { |
| 7375 | int hwSyncId = AudioSystem.getAudioHwSyncForSession(sessionId); |
| 7376 | if (hwSyncId == AudioSystem.AUDIO_HW_SYNC_INVALID) { |
| 7377 | throw new UnsupportedOperationException("HW A/V synchronization is not supported."); |
| 7378 | } |
| 7379 | return hwSyncId; |
| 7380 | } |
| 7381 | |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7382 | /** |
| 7383 | * Selects the audio device that should be used for communication use cases, for instance voice |
| 7384 | * or video calls. This method can be used by voice or video chat applications to select a |
| 7385 | * different audio device than the one selected by default by the platform. |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7386 | * <p>The device selection is expressed as an {@link AudioDeviceInfo} among devices returned by |
| 7387 | * {@link #getAvailableCommunicationDevices()}. |
| 7388 | * The selection is active as long as the requesting application process lives, until |
| 7389 | * {@link #clearCommunicationDevice} is called or until the device is disconnected. |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7390 | * It is therefore important for applications to clear the request when a call ends or the |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7391 | * the requesting activity or service is stopped or destroyed. |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7392 | * <p>In case of simultaneous requests by multiple applications the priority is given to the |
| 7393 | * application currently controlling the audio mode (see {@link #setMode(int)}). This is the |
| 7394 | * latest application having selected mode {@link #MODE_IN_COMMUNICATION} or mode |
| 7395 | * {@link #MODE_IN_CALL}. Note that <code>MODE_IN_CALL</code> can only be selected by the main |
| 7396 | * telephony application with permission |
| 7397 | * {@link android.Manifest.permission#MODIFY_PHONE_STATE}. |
| 7398 | * <p> If the requested devices is not currently available, the request will be rejected and |
| 7399 | * the method will return false. |
| 7400 | * <p>This API replaces the following deprecated APIs: |
| 7401 | * <ul> |
| 7402 | * <li> {@link #startBluetoothSco()} |
| 7403 | * <li> {@link #stopBluetoothSco()} |
| 7404 | * <li> {@link #setSpeakerphoneOn(boolean)} |
| 7405 | * </ul> |
| 7406 | * <h4>Example</h4> |
| 7407 | * <p>The example below shows how to enable and disable speakerphone mode. |
| 7408 | * <pre class="prettyprint"> |
| 7409 | * // Get an AudioManager instance |
| 7410 | * AudioManager audioManager = Context.getSystemService(AudioManager.class); |
Eric Laurent | 9a40448 | 2021-03-09 19:58:39 +0100 | [diff] [blame] | 7411 | * AudioDeviceInfo speakerDevice = null; |
| 7412 | * List<AudioDeviceInfo> devices = audioManager.getAvailableCommunicationDevices(); |
| 7413 | * for (AudioDeviceInfo device : devices) { |
| 7414 | * if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) { |
| 7415 | * speakerDevice = device; |
| 7416 | * break; |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7417 | * } |
Eric Laurent | 9a40448 | 2021-03-09 19:58:39 +0100 | [diff] [blame] | 7418 | * } |
| 7419 | * if (speakerDevice != null) { |
| 7420 | * // Turn speakerphone ON. |
| 7421 | * boolean result = audioManager.setCommunicationDevice(speakerDevice); |
| 7422 | * if (!result) { |
| 7423 | * // Handle error. |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7424 | * } |
Eric Laurent | 9a40448 | 2021-03-09 19:58:39 +0100 | [diff] [blame] | 7425 | * // Turn speakerphone OFF. |
| 7426 | * audioManager.clearCommunicationDevice(); |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7427 | * } |
| 7428 | * </pre> |
| 7429 | * @param device the requested audio device. |
| 7430 | * @return <code>true</code> if the request was accepted, <code>false</code> otherwise. |
| 7431 | * @throws IllegalArgumentException If an invalid device is specified. |
| 7432 | */ |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7433 | public boolean setCommunicationDevice(@NonNull AudioDeviceInfo device) { |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7434 | Objects.requireNonNull(device); |
| 7435 | try { |
| 7436 | if (device.getId() == 0) { |
| 7437 | throw new IllegalArgumentException("In valid device: " + device); |
| 7438 | } |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7439 | return getService().setCommunicationDevice(mICallBack, device.getId()); |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7440 | } catch (RemoteException e) { |
| 7441 | throw e.rethrowFromSystemServer(); |
| 7442 | } |
| 7443 | } |
| 7444 | |
| 7445 | /** |
| 7446 | * Cancels previous communication device selection made with |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7447 | * {@link #setCommunicationDevice(AudioDeviceInfo)}. |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7448 | */ |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7449 | public void clearCommunicationDevice() { |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7450 | try { |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7451 | getService().setCommunicationDevice(mICallBack, 0); |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7452 | } catch (RemoteException e) { |
| 7453 | throw e.rethrowFromSystemServer(); |
| 7454 | } |
| 7455 | } |
| 7456 | |
| 7457 | /** |
| 7458 | * Returns currently selected audio device for communication. |
| 7459 | * <p>This API replaces the following deprecated APIs: |
| 7460 | * <ul> |
| 7461 | * <li> {@link #isBluetoothScoOn()} |
| 7462 | * <li> {@link #isSpeakerphoneOn()} |
| 7463 | * </ul> |
| 7464 | * @return an {@link AudioDeviceInfo} indicating which audio device is |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7465 | * currently selected for communication use cases. Can be null on platforms |
| 7466 | * not supporting {@link android.content.pm.PackageManager#FEATURE_TELEPHONY}. |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7467 | * is used. |
| 7468 | */ |
| 7469 | @Nullable |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7470 | public AudioDeviceInfo getCommunicationDevice() { |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7471 | try { |
| 7472 | return getDeviceForPortId( |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7473 | getService().getCommunicationDevice(), GET_DEVICES_OUTPUTS); |
| 7474 | } catch (RemoteException e) { |
| 7475 | throw e.rethrowFromSystemServer(); |
| 7476 | } |
| 7477 | } |
| 7478 | |
| 7479 | /** |
| 7480 | * Returns a list of audio devices that can be selected for communication use cases via |
| 7481 | * {@link #setCommunicationDevice(AudioDeviceInfo)}. |
| 7482 | * @return a list of {@link AudioDeviceInfo} suitable for use with setCommunicationDevice(). |
| 7483 | */ |
| 7484 | @NonNull |
| 7485 | public List<AudioDeviceInfo> getAvailableCommunicationDevices() { |
| 7486 | try { |
| 7487 | ArrayList<AudioDeviceInfo> devices = new ArrayList<>(); |
| 7488 | int[] portIds = getService().getAvailableCommunicationDeviceIds(); |
| 7489 | for (int portId : portIds) { |
| 7490 | AudioDeviceInfo device = getDeviceForPortId(portId, GET_DEVICES_OUTPUTS); |
| 7491 | if (device == null) { |
| 7492 | continue; |
| 7493 | } |
| 7494 | devices.add(device); |
| 7495 | } |
| 7496 | return devices; |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7497 | } catch (RemoteException e) { |
| 7498 | throw e.rethrowFromSystemServer(); |
| 7499 | } |
| 7500 | } |
| 7501 | |
| 7502 | /** |
| 7503 | * @hide |
| 7504 | * Returns an {@link AudioDeviceInfo} corresponding to a connected device of the type provided. |
| 7505 | * The type must be a valid output type defined in <code>AudioDeviceInfo</code> class, |
| 7506 | * for instance {@link AudioDeviceInfo#TYPE_BUILTIN_SPEAKER}. |
| 7507 | * The method will return null if no device of the provided type is connected. |
| 7508 | * If more than one device of the provided type is connected, an object corresponding to the |
| 7509 | * first device encountered in the enumeration list will be returned. |
| 7510 | * @param deviceType The device device for which an <code>AudioDeviceInfo</code> |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7511 | * object is queried. |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7512 | * @return An AudioDeviceInfo object or null if no device with the requested type is connected. |
| 7513 | * @throws IllegalArgumentException If an invalid device type is specified. |
| 7514 | */ |
| 7515 | @TestApi |
| 7516 | @Nullable |
| 7517 | public static AudioDeviceInfo getDeviceInfoFromType( |
| 7518 | @AudioDeviceInfo.AudioDeviceTypeOut int deviceType) { |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7519 | return getDeviceInfoFromTypeAndAddress(deviceType, null); |
| 7520 | } |
| 7521 | |
| 7522 | /** |
| 7523 | * @hide |
| 7524 | * Returns an {@link AudioDeviceInfo} corresponding to a connected device of the type and |
| 7525 | * address provided. |
| 7526 | * The type must be a valid output type defined in <code>AudioDeviceInfo</code> class, |
| 7527 | * for instance {@link AudioDeviceInfo#TYPE_BUILTIN_SPEAKER}. |
| 7528 | * If a null address is provided, the matching will happen on the type only. |
| 7529 | * The method will return null if no device of the provided type and address is connected. |
| 7530 | * If more than one device of the provided type is connected, an object corresponding to the |
| 7531 | * first device encountered in the enumeration list will be returned. |
| 7532 | * @param type The device device for which an <code>AudioDeviceInfo</code> |
| 7533 | * object is queried. |
| 7534 | * @param address The device address for which an <code>AudioDeviceInfo</code> |
| 7535 | * object is queried or null if requesting match on type only. |
| 7536 | * @return An AudioDeviceInfo object or null if no matching device is connected. |
| 7537 | * @throws IllegalArgumentException If an invalid device type is specified. |
| 7538 | */ |
| 7539 | @Nullable |
| 7540 | public static AudioDeviceInfo getDeviceInfoFromTypeAndAddress( |
| 7541 | @AudioDeviceInfo.AudioDeviceTypeOut int type, @Nullable String address) { |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7542 | AudioDeviceInfo[] devices = getDevicesStatic(GET_DEVICES_OUTPUTS); |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7543 | AudioDeviceInfo deviceForType = null; |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7544 | for (AudioDeviceInfo device : devices) { |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7545 | if (device.getType() == type) { |
| 7546 | deviceForType = device; |
| 7547 | if (address == null || address.equals(device.getAddress())) { |
| 7548 | return device; |
| 7549 | } |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7550 | } |
| 7551 | } |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7552 | return deviceForType; |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7553 | } |
| 7554 | |
| 7555 | /** |
| 7556 | * Listener registered by client to be notified upon communication audio device change. |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7557 | * See {@link #setCommunicationDevice(AudioDeviceInfo)}. |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7558 | */ |
| 7559 | public interface OnCommunicationDeviceChangedListener { |
| 7560 | /** |
| 7561 | * Callback method called upon communication audio device change. |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7562 | * @param device the audio device requested for communication use cases. |
| 7563 | * Can be null on platforms not supporting |
| 7564 | * {@link android.content.pm.PackageManager#FEATURE_TELEPHONY}. |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7565 | */ |
| 7566 | void onCommunicationDeviceChanged(@Nullable AudioDeviceInfo device); |
| 7567 | } |
| 7568 | |
| 7569 | /** |
| 7570 | * Adds a listener for being notified of changes to the communication audio device. |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7571 | * See {@link #setCommunicationDevice(AudioDeviceInfo)}. |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7572 | * @param executor |
| 7573 | * @param listener |
| 7574 | */ |
| 7575 | public void addOnCommunicationDeviceChangedListener( |
| 7576 | @NonNull @CallbackExecutor Executor executor, |
| 7577 | @NonNull OnCommunicationDeviceChangedListener listener) { |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7578 | synchronized (mCommDevListenerLock) { |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 7579 | final Pair<ArrayList<ListenerInfo<OnCommunicationDeviceChangedListener>>, |
| 7580 | CommunicationDeviceDispatcherStub> res = |
| 7581 | CallbackUtil.addListener("addOnCommunicationDeviceChangedListener", |
| 7582 | executor, listener, mCommDevListeners, mCommDevDispatcherStub, |
| 7583 | () -> new CommunicationDeviceDispatcherStub(), |
| 7584 | stub -> stub.register(true)); |
| 7585 | mCommDevListeners = res.first; |
| 7586 | mCommDevDispatcherStub = res.second; |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7587 | } |
| 7588 | } |
| 7589 | |
| 7590 | /** |
| 7591 | * Removes a previously added listener of changes to the communication audio device. |
Eric Laurent | 7412f57 | 2021-02-11 15:10:31 +0100 | [diff] [blame] | 7592 | * See {@link #setCommunicationDevice(AudioDeviceInfo)}. |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7593 | * @param listener |
| 7594 | */ |
| 7595 | public void removeOnCommunicationDeviceChangedListener( |
| 7596 | @NonNull OnCommunicationDeviceChangedListener listener) { |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7597 | synchronized (mCommDevListenerLock) { |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 7598 | final Pair<ArrayList<ListenerInfo<OnCommunicationDeviceChangedListener>>, |
| 7599 | CommunicationDeviceDispatcherStub> res = |
| 7600 | CallbackUtil.removeListener("removeOnCommunicationDeviceChangedListener", |
| 7601 | listener, mCommDevListeners, mCommDevDispatcherStub, |
| 7602 | stub -> stub.register(false)); |
| 7603 | mCommDevListeners = res.first; |
| 7604 | mCommDevDispatcherStub = res.second; |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7605 | } |
| 7606 | } |
| 7607 | |
| 7608 | private final Object mCommDevListenerLock = new Object(); |
| 7609 | /** |
| 7610 | * List of listeners for preferred device for strategy and their associated Executor. |
| 7611 | * List is lazy-initialized on first registration |
| 7612 | */ |
| 7613 | @GuardedBy("mCommDevListenerLock") |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 7614 | private @Nullable |
| 7615 | ArrayList<ListenerInfo<OnCommunicationDeviceChangedListener>> mCommDevListeners; |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7616 | |
| 7617 | @GuardedBy("mCommDevListenerLock") |
| 7618 | private CommunicationDeviceDispatcherStub mCommDevDispatcherStub; |
| 7619 | |
| 7620 | private final class CommunicationDeviceDispatcherStub |
| 7621 | extends ICommunicationDeviceDispatcher.Stub { |
| 7622 | |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 7623 | public void register(boolean register) { |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7624 | try { |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 7625 | if (register) { |
| 7626 | getService().registerCommunicationDeviceDispatcher(this); |
| 7627 | } else { |
| 7628 | getService().unregisterCommunicationDeviceDispatcher(this); |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7629 | } |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 7630 | } catch (RemoteException e) { |
| 7631 | e.rethrowFromSystemServer(); |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7632 | } |
| 7633 | } |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7634 | |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 7635 | @Override |
| 7636 | @SuppressLint("GuardedBy") // lock applied inside callListeners method |
| 7637 | public void dispatchCommunicationDeviceChanged(int portId) { |
| 7638 | AudioDeviceInfo device = getDeviceForPortId(portId, GET_DEVICES_OUTPUTS); |
| 7639 | CallbackUtil.callListeners(mCommDevListeners, mCommDevListenerLock, |
| 7640 | (listener) -> listener.onCommunicationDeviceChanged(device)); |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7641 | } |
Eric Laurent | b36d4a1 | 2020-10-09 09:52:49 -0700 | [diff] [blame] | 7642 | } |
| 7643 | |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 7644 | //--------------------------------------------------------- |
| 7645 | // Inner classes |
| 7646 | //-------------------- |
| 7647 | /** |
| 7648 | * Helper class to handle the forwarding of native events to the appropriate listener |
| 7649 | * (potentially) handled in a different thread. |
| 7650 | */ |
| 7651 | private class NativeEventHandlerDelegate { |
| 7652 | private final Handler mHandler; |
| 7653 | |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 7654 | NativeEventHandlerDelegate(final AudioDeviceCallback callback, |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 7655 | Handler handler) { |
| 7656 | // find the looper for our new event handler |
| 7657 | Looper looper; |
| 7658 | if (handler != null) { |
| 7659 | looper = handler.getLooper(); |
| 7660 | } else { |
| 7661 | // no given handler, use the looper the addListener call was called in |
| 7662 | looper = Looper.getMainLooper(); |
| 7663 | } |
| 7664 | |
| 7665 | // construct the event handler with this looper |
| 7666 | if (looper != null) { |
| 7667 | // implement the event handler delegate |
| 7668 | mHandler = new Handler(looper) { |
| 7669 | @Override |
| 7670 | public void handleMessage(Message msg) { |
| 7671 | switch(msg.what) { |
Paul McLean | cbeb8a2 | 2015-06-10 08:21:27 -0700 | [diff] [blame] | 7672 | case MSG_DEVICES_CALLBACK_REGISTERED: |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 7673 | case MSG_DEVICES_DEVICES_ADDED: |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 7674 | if (callback != null) { |
| 7675 | callback.onAudioDevicesAdded((AudioDeviceInfo[])msg.obj); |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 7676 | } |
| 7677 | break; |
Paul McLean | 0334688 | 2015-05-12 15:36:56 -0700 | [diff] [blame] | 7678 | |
| 7679 | case MSG_DEVICES_DEVICES_REMOVED: |
| 7680 | if (callback != null) { |
| 7681 | callback.onAudioDevicesRemoved((AudioDeviceInfo[])msg.obj); |
| 7682 | } |
| 7683 | break; |
| 7684 | |
Paul McLean | e3383cc | 2015-05-08 11:41:20 -0700 | [diff] [blame] | 7685 | default: |
| 7686 | Log.e(TAG, "Unknown native event type: " + msg.what); |
| 7687 | break; |
| 7688 | } |
| 7689 | } |
| 7690 | }; |
| 7691 | } else { |
| 7692 | mHandler = null; |
| 7693 | } |
| 7694 | } |
| 7695 | |
| 7696 | Handler getHandler() { |
| 7697 | return mHandler; |
| 7698 | } |
| 7699 | } |
Jean-Michel Trivi | 91f0baa | 2021-11-11 16:31:32 -0800 | [diff] [blame^] | 7700 | } |