Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | */ |
Scott Randolph | 12edb60 | 2018-11-28 18:57:09 -0800 | [diff] [blame] | 16 | package com.android.car.audio; |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 17 | |
| 18 | import android.hardware.automotive.audiocontrol.V1_0.ContextNumber; |
| 19 | import android.media.AudioAttributes; |
| 20 | import android.media.AudioFocusInfo; |
| 21 | import android.media.AudioManager; |
| 22 | import android.media.audiopolicy.AudioPolicy; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | import java.io.PrintWriter; |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.HashMap; |
| 28 | import java.util.Iterator; |
| 29 | |
| 30 | |
| 31 | public class CarAudioFocus extends AudioPolicy.AudioPolicyFocusListener { |
| 32 | |
| 33 | private static final String TAG = "CarAudioFocus"; |
| 34 | |
| 35 | private final AudioManager mAudioManager; |
| 36 | private CarAudioService mCarAudioService; // Dynamically assigned just after construction |
| 37 | private AudioPolicy mAudioPolicy; // Dynamically assigned just after construction |
| 38 | |
| 39 | |
| 40 | // Values for the internal interaction matrix we use to make focus decisions |
| 41 | private static final int INTERACTION_REJECT = 0; // Focus not granted |
| 42 | private static final int INTERACTION_EXCLUSIVE = 1; // Focus granted, others loose focus |
| 43 | private static final int INTERACTION_CONCURRENT = 2; // Focus granted, others keep focus |
| 44 | |
| 45 | // TODO: Make this an overlayable resource... |
| 46 | // MUSIC = 1, // Music playback |
| 47 | // NAVIGATION = 2, // Navigation directions |
| 48 | // VOICE_COMMAND = 3, // Voice command session |
| 49 | // CALL_RING = 4, // Voice call ringing |
| 50 | // CALL = 5, // Voice call |
| 51 | // ALARM = 6, // Alarm sound from Android |
| 52 | // NOTIFICATION = 7, // Notifications |
| 53 | // SYSTEM_SOUND = 8, // User interaction sounds (button clicks, etc) |
| 54 | private static int sInteractionMatrix[][] = { |
| 55 | // Row selected by playing sound (labels along the right) |
| 56 | // Column selected by incoming request (labels along the top) |
| 57 | // Cell value is one of INTERACTION_REJECT, INTERACTION_EXCLUSIVE, INTERACTION_CONCURRENT |
| 58 | // Invalid, Music, Nav, Voice, Ring, Call, Alarm, Notification, System |
| 59 | { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // Invalid |
| 60 | { 0, 1, 2, 1, 1, 1, 1, 2, 2 }, // Music |
| 61 | { 0, 2, 2, 1, 2, 1, 2, 2, 2 }, // Nav |
| 62 | { 0, 2, 0, 2, 1, 1, 0, 0, 0 }, // Voice |
| 63 | { 0, 0, 2, 2, 2, 2, 0, 0, 2 }, // Ring |
| 64 | { 0, 0, 2, 0, 2, 2, 2, 2, 0 }, // Context |
| 65 | { 0, 2, 2, 1, 1, 1, 2, 2, 2 }, // Alarm |
| 66 | { 0, 2, 2, 1, 1, 1, 2, 2, 2 }, // Notification |
| 67 | { 0, 2, 2, 1, 1, 1, 2, 2, 2 }, // System |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | private class FocusEntry { |
| 72 | // Requester info |
| 73 | final AudioFocusInfo mAfi; // never null |
| 74 | |
| 75 | final int mAudioContext; // Which HAL level context does this affect |
| 76 | final ArrayList<FocusEntry> mBlockers; // List of requests that block ours |
| 77 | |
| 78 | FocusEntry(AudioFocusInfo afi, |
| 79 | int context) { |
| 80 | mAfi = afi; |
| 81 | mAudioContext = context; |
| 82 | mBlockers = new ArrayList<FocusEntry>(); |
| 83 | } |
| 84 | |
| 85 | public String getClientId() { |
| 86 | return mAfi.getClientId(); |
| 87 | } |
| 88 | |
| 89 | public boolean wantsPauseInsteadOfDucking() { |
| 90 | return (mAfi.getFlags() & AudioManager.AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS) != 0; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | // We keep track of all the focus requesters in this map, with their clientId as the key. |
| 96 | // This is used both for focus dispatch and death handling |
| 97 | // Note that the clientId reflects the AudioManager instance and listener object (if any) |
| 98 | // so that one app can have more than one unique clientId by setting up distinct listeners. |
Scott Randolph | 8eb4955 | 2018-10-17 00:56:39 -0700 | [diff] [blame] | 99 | // Because the listener gets only LOSS/GAIN messages, this is important for an app to do if |
| 100 | // it expects to request focus concurrently for different USAGEs so it knows which USAGE |
| 101 | // gained or lost focus at any given moment. If the SAME listener is used for requests of |
| 102 | // different USAGE while the earlier request is still in the focus stack (whether holding |
| 103 | // focus or pending), the new request will be REJECTED so as to avoid any confusion about |
| 104 | // the meaning of subsequent GAIN/LOSS events (which would continue to apply to the focus |
| 105 | // request that was already active or pending). |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 106 | private HashMap<String, FocusEntry> mFocusHolders = new HashMap<String, FocusEntry>(); |
| 107 | private HashMap<String, FocusEntry> mFocusLosers = new HashMap<String, FocusEntry>(); |
| 108 | |
| 109 | |
| 110 | CarAudioFocus(AudioManager audioManager) { |
| 111 | mAudioManager = audioManager; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | // This has to happen after the construction to avoid a chicken and egg problem when setting up |
| 116 | // the AudioPolicy which must depend on this object. |
| 117 | public void setOwningPolicy(CarAudioService audioService, AudioPolicy parentPolicy) { |
| 118 | mCarAudioService = audioService; |
| 119 | mAudioPolicy = parentPolicy; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | // This sends a focus loss message to the targeted requester. |
| 124 | private void sendFocusLoss(FocusEntry loser, boolean permanent) { |
| 125 | int lossType = (permanent ? AudioManager.AUDIOFOCUS_LOSS : |
| 126 | AudioManager.AUDIOFOCUS_LOSS_TRANSIENT); |
| 127 | Log.i(TAG, "sendFocusLoss to " + loser.getClientId()); |
| 128 | int result = mAudioManager.dispatchAudioFocusChange(loser.mAfi, lossType, mAudioPolicy); |
| 129 | if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { |
| 130 | // TODO: Is this actually an error, or is it okay for an entry in the focus stack |
| 131 | // to NOT have a listener? If that's the case, should we even keep it in the focus |
| 132 | // stack? |
| 133 | Log.e(TAG, "Failure to signal loss of audio focus with error: " + result); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /** @see AudioManager#requestAudioFocus(AudioManager.OnAudioFocusChangeListener, int, int, int) */ |
Scott Randolph | 8eb4955 | 2018-10-17 00:56:39 -0700 | [diff] [blame] | 139 | // Note that we replicate most, but not all of the behaviors of the default MediaFocusControl |
| 140 | // engine as of Android P. |
| 141 | // Besides the interaction matrix which allows concurrent focus for multiple requestors, which |
| 142 | // is the reason for this module, we also treat repeated requests from the same clientId |
| 143 | // slightly differently. |
| 144 | // If a focus request for the same listener (clientId) is received while that listener is |
| 145 | // already in the focus stack, we REJECT it outright unless it is for the same USAGE. |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 146 | // The default audio framework's behavior is to remove the previous entry in the stack (no-op |
| 147 | // if the requester is already holding focus). |
| 148 | int evaluateFocusRequest(AudioFocusInfo afi) { |
| 149 | Log.i(TAG, "Evaluating focus request for client " + afi.getClientId()); |
| 150 | |
| 151 | // Is this a request for premanant focus? |
| 152 | // AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE -- Means Notifications should be denied |
| 153 | // AUDIOFOCUS_GAIN_TRANSIENT -- Means current focus holders should get transient loss |
| 154 | // AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK -- Means other can duck (no loss message from us) |
| 155 | // NOTE: We expect that in practice it will be permanent for all media requests and |
| 156 | // transient for everything else, but that isn't currently an enforced requirement. |
| 157 | final boolean permanent = |
| 158 | (afi.getGainRequest() == AudioManager.AUDIOFOCUS_GAIN); |
| 159 | final boolean allowDucking = |
| 160 | (afi.getGainRequest() == AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK); |
| 161 | |
| 162 | |
| 163 | // Convert from audio attributes "usage" to HAL level "context" |
| 164 | final int requestedContext = mCarAudioService.getContextForUsage( |
| 165 | afi.getAttributes().getUsage()); |
| 166 | |
Scott Randolph | 8eb4955 | 2018-10-17 00:56:39 -0700 | [diff] [blame] | 167 | // If we happen find an entry that this new request should replace, we'll store it here. |
| 168 | FocusEntry deprecatedBlockedEntry = null; |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 169 | |
| 170 | // Scan all active and pending focus requests. If any should cause rejection of |
| 171 | // this new request, then we're done. Keep a list of those against whom we're exclusive |
| 172 | // so we can update the relationships if/when we are sure we won't get rejected. |
Scott Randolph | 8eb4955 | 2018-10-17 00:56:39 -0700 | [diff] [blame] | 173 | Log.i(TAG, "Scanning focus holders..."); |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 174 | final ArrayList<FocusEntry> losers = new ArrayList<FocusEntry>(); |
| 175 | for (FocusEntry entry : mFocusHolders.values()) { |
Scott Randolph | 8eb4955 | 2018-10-17 00:56:39 -0700 | [diff] [blame] | 176 | Log.i(TAG, entry.mAfi.getClientId()); |
| 177 | |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 178 | // If this request is for Notifications and a current focus holder has specified |
| 179 | // AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE, then reject the request. |
| 180 | // This matches the hardwired behavior in the default audio policy engine which apps |
| 181 | // might expect (The interaction matrix doesn't have any provision for dealing with |
| 182 | // override flags like this). |
| 183 | if ((requestedContext == ContextNumber.NOTIFICATION) && |
| 184 | (entry.mAfi.getGainRequest() == |
| 185 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)) { |
| 186 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 187 | } |
| 188 | |
Scott Randolph | 8eb4955 | 2018-10-17 00:56:39 -0700 | [diff] [blame] | 189 | // We don't allow sharing listeners (client IDs) between two concurrent requests |
| 190 | // (because the app would have no way to know to which request a later event applied) |
| 191 | if (afi.getClientId().equals(entry.mAfi.getClientId())) { |
| 192 | if (entry.mAudioContext == requestedContext) { |
| 193 | // Trivially accept if this request is a duplicate |
| 194 | Log.i(TAG, "Duplicate request from focus holder is accepted"); |
| 195 | return AudioManager.AUDIOFOCUS_REQUEST_GRANTED; |
| 196 | } else { |
| 197 | // Trivially reject a request for a different USAGE |
| 198 | Log.i(TAG, "Different request from focus holder is rejected"); |
| 199 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 200 | } |
| 201 | } |
| 202 | |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 203 | // Check the interaction matrix for the relationship between this entry and the request |
| 204 | switch (sInteractionMatrix[entry.mAudioContext][requestedContext]) { |
| 205 | case INTERACTION_REJECT: |
| 206 | // This request is rejected, so nothing further to do |
| 207 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 208 | case INTERACTION_EXCLUSIVE: |
| 209 | // The new request will cause this existing entry to lose focus |
| 210 | losers.add(entry); |
| 211 | break; |
| 212 | default: |
| 213 | // If ducking isn't allowed by the focus requestor, then everybody else |
| 214 | // must get a LOSS. |
| 215 | // If a focus holder has set the AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS flag, |
| 216 | // they must get a LOSS message even if ducking would otherwise be allowed. |
| 217 | if ((!allowDucking) || |
| 218 | (entry.mAfi.getFlags() & |
| 219 | AudioManager.AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS) != 0) { |
| 220 | // The new request will cause audio book to lose focus and pause |
| 221 | losers.add(entry); |
| 222 | } |
| 223 | } |
| 224 | } |
Scott Randolph | 8eb4955 | 2018-10-17 00:56:39 -0700 | [diff] [blame] | 225 | Log.i(TAG, "Scanning those who've already lost focus..."); |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 226 | final ArrayList<FocusEntry> blocked = new ArrayList<FocusEntry>(); |
| 227 | for (FocusEntry entry : mFocusLosers.values()) { |
Scott Randolph | 8eb4955 | 2018-10-17 00:56:39 -0700 | [diff] [blame] | 228 | Log.i(TAG, entry.mAfi.getClientId()); |
| 229 | |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 230 | // If this request is for Notifications and a pending focus holder has specified |
| 231 | // AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE, then reject the request |
| 232 | if ((requestedContext == ContextNumber.NOTIFICATION) && |
| 233 | (entry.mAfi.getGainRequest() == |
| 234 | AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE)) { |
| 235 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 236 | } |
| 237 | |
Scott Randolph | 8eb4955 | 2018-10-17 00:56:39 -0700 | [diff] [blame] | 238 | // We don't allow sharing listeners (client IDs) between two concurrent requests |
| 239 | // (because the app would have no way to know to which request a later event applied) |
| 240 | if (afi.getClientId().equals(entry.mAfi.getClientId())) { |
| 241 | if (entry.mAudioContext == requestedContext) { |
| 242 | // This is a repeat of a request that is currently blocked. |
| 243 | // Evaluate it as if it were a new request, but note that we should remove |
| 244 | // the old pending request, and move it. |
| 245 | // We do not want to evaluate the new request against itself. |
| 246 | Log.i(TAG, "Duplicate request while waiting is being evaluated"); |
| 247 | deprecatedBlockedEntry = entry; |
| 248 | continue; |
| 249 | } else { |
| 250 | // Trivially reject a request for a different USAGE |
| 251 | Log.i(TAG, "Different request while waiting is rejected"); |
| 252 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 253 | } |
| 254 | } |
| 255 | |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 256 | // Check the interaction matrix for the relationship between this entry and the request |
| 257 | switch (sInteractionMatrix[entry.mAudioContext][requestedContext]) { |
| 258 | case INTERACTION_REJECT: |
| 259 | // Even though this entry has currently lost focus, the fact that it is |
| 260 | // waiting to play means we'll reject this new conflicting request. |
| 261 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 262 | case INTERACTION_EXCLUSIVE: |
| 263 | // The new request is yet another reason this entry cannot regain focus (yet) |
| 264 | blocked.add(entry); |
| 265 | break; |
| 266 | default: |
| 267 | // If ducking is not allowed by the requester, or the pending focus holder had |
| 268 | // set the AUDIOFOCUS_FLAG_PAUSES_ON_DUCKABLE_LOSS flag, |
| 269 | // then the pending holder must stay "lost" until this requester goes away. |
| 270 | if ((!allowDucking) || entry.wantsPauseInsteadOfDucking()) { |
| 271 | // The new request is yet another reason this entry cannot regain focus yet |
| 272 | blocked.add(entry); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | |
| 278 | // Now that we've decided we'll grant focus, construct our new FocusEntry |
| 279 | FocusEntry newEntry = new FocusEntry(afi, requestedContext); |
| 280 | |
| 281 | |
| 282 | // Now that we're sure we'll accept this request, update any requests which we would |
| 283 | // block but are already out of focus but waiting to come back |
| 284 | for (FocusEntry entry : blocked) { |
| 285 | // If we're out of focus it must be because somebody is blocking us |
| 286 | assert !entry.mBlockers.isEmpty(); |
| 287 | |
| 288 | if (permanent) { |
| 289 | // This entry has now lost focus forever |
| 290 | sendFocusLoss(entry, permanent); |
| 291 | final FocusEntry deadEntry = mFocusLosers.remove(entry.mAfi.getClientId()); |
| 292 | assert deadEntry != null; |
| 293 | } else { |
| 294 | // Note that this new request is yet one more reason we can't (yet) have focus |
| 295 | entry.mBlockers.add(newEntry); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // Notify and update any requests which are now losing focus as a result of the new request |
| 300 | for (FocusEntry entry : losers) { |
| 301 | // If we have focus (but are about to loose it), nobody should be blocking us yet |
| 302 | assert entry.mBlockers.isEmpty(); |
| 303 | |
| 304 | sendFocusLoss(entry, permanent); |
| 305 | |
| 306 | // The entry no longer holds focus, so take it out of the holders list |
| 307 | mFocusHolders.remove(entry.mAfi.getClientId()); |
| 308 | |
| 309 | if (!permanent) { |
| 310 | // Add ourselves to the list of requests waiting to get focus back and |
| 311 | // note why we lost focus so we can tell when it's time to get it back |
| 312 | mFocusLosers.put(entry.mAfi.getClientId(), entry); |
| 313 | entry.mBlockers.add(newEntry); |
| 314 | } |
| 315 | } |
| 316 | |
Scott Randolph | 8eb4955 | 2018-10-17 00:56:39 -0700 | [diff] [blame] | 317 | // If we encountered a duplicate of this request that was pending, but now we're going to |
| 318 | // grant focus, we need to remove the old pending request (without sending a LOSS message). |
| 319 | if (deprecatedBlockedEntry != null) { |
| 320 | mFocusLosers.remove(deprecatedBlockedEntry.mAfi.getClientId()); |
| 321 | } |
| 322 | |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 323 | // Finally, add the request we're granting to the focus holders' list |
| 324 | mFocusHolders.put(afi.getClientId(), newEntry); |
| 325 | |
Scott Randolph | 8eb4955 | 2018-10-17 00:56:39 -0700 | [diff] [blame] | 326 | Log.i(TAG, "AUDIOFOCUS_REQUEST_GRANTED"); |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 327 | return AudioManager.AUDIOFOCUS_REQUEST_GRANTED; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | @Override |
| 332 | public synchronized void onAudioFocusRequest(AudioFocusInfo afi, int requestResult) { |
| 333 | Log.i(TAG, "onAudioFocusRequest " + afi); |
| 334 | |
| 335 | int response = evaluateFocusRequest(afi); |
| 336 | |
| 337 | // Post our reply for delivery to the original focus requester |
| 338 | mAudioManager.setFocusRequestResult(afi, response, mAudioPolicy); |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /** |
| 343 | * @see AudioManager#abandonAudioFocus(AudioManager.OnAudioFocusChangeListener, AudioAttributes) |
Roberto Perez | 0a15c81 | 2018-10-31 12:53:38 -0700 | [diff] [blame] | 344 | * Note that we'll get this call for a focus holder that dies while in the focus stack, so |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 345 | * we don't need to watch for death notifications directly. |
| 346 | * */ |
| 347 | @Override |
| 348 | public synchronized void onAudioFocusAbandon(AudioFocusInfo afi) { |
| 349 | Log.i(TAG, "onAudioFocusAbandon " + afi); |
| 350 | |
| 351 | // Remove this entry from our active or pending list |
| 352 | FocusEntry deadEntry = mFocusHolders.remove(afi.getClientId()); |
| 353 | if (deadEntry == null) { |
| 354 | deadEntry = mFocusLosers.remove(afi.getClientId()); |
| 355 | if (deadEntry == null) { |
| 356 | // Caller is providing an unrecognzied clientId!? |
Scott Randolph | dc41061 | 2018-10-18 18:43:26 -0700 | [diff] [blame] | 357 | Log.w(TAG, "Audio focus abandoned by unrecognized client id: " + afi.getClientId()); |
| 358 | // This probably means an app double released focused for some reason. One |
| 359 | // harmless possibility is a race between an app being told it lost focus and the |
| 360 | // app voluntarily abandoning focus. More likely the app is just sloppy. :) |
| 361 | // The more nefarious possibility is that the clientId is actually corrupted |
| 362 | // somehow, in which case we might have a real focus entry that we're going to fail |
| 363 | // to remove. If that were to happen, I'd expect either the app to swallow it |
| 364 | // silently, or else take unexpected action (eg: resume playing spontaneously), or |
| 365 | // else to see "Failure to signal ..." gain/loss error messages in the log from |
| 366 | // this module when a focus change tries to take action on a truly zombie entry. |
Roberto Perez | 0a15c81 | 2018-10-31 12:53:38 -0700 | [diff] [blame] | 367 | return; |
Scott Randolph | 54892f8 | 2018-03-02 13:24:55 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | // Remove this entry from the blocking list of any pending requests |
| 372 | Iterator<FocusEntry> it = mFocusLosers.values().iterator(); |
| 373 | while (it.hasNext()) { |
| 374 | FocusEntry entry = it.next(); |
| 375 | |
| 376 | // Remove the retiring entry from all blocker lists |
| 377 | entry.mBlockers.remove(deadEntry); |
| 378 | |
| 379 | // Any entry whose blocking list becomes empty should regain focus |
| 380 | if (entry.mBlockers.isEmpty()) { |
| 381 | // Pull this entry out of the focus losers list |
| 382 | it.remove(); |
| 383 | |
| 384 | // Add it back into the focus holders list |
| 385 | mFocusHolders.put(entry.getClientId(), entry); |
| 386 | |
| 387 | // Send the focus (re)gain notification |
| 388 | int result = mAudioManager.dispatchAudioFocusChange( |
| 389 | entry.mAfi, |
| 390 | entry.mAfi.getGainRequest(), |
| 391 | mAudioPolicy); |
| 392 | if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { |
| 393 | // TODO: Is this actually an error, or is it okay for an entry in the focus |
| 394 | // stack to NOT have a listener? If that's the case, should we even keep |
| 395 | // it in the focus stack? |
| 396 | Log.e(TAG, "Failure to signal gain of audio focus with error: " + result); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | |
| 403 | public synchronized void dump(PrintWriter writer) { |
| 404 | writer.println("*CarAudioFocus*"); |
| 405 | |
| 406 | writer.println(" Current Focus Holders:"); |
| 407 | for (String clientId : mFocusHolders.keySet()) { |
| 408 | System.out.println(clientId); |
| 409 | } |
| 410 | |
| 411 | writer.println(" Transient Focus Losers:"); |
| 412 | for (String clientId : mFocusLosers.keySet()) { |
| 413 | System.out.println(clientId); |
| 414 | } |
| 415 | } |
| 416 | } |