Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 17 | package com.android.server.audio; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 18 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 19 | import android.app.AppOpsManager; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 20 | import android.content.Context; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 21 | import android.media.AudioAttributes; |
| 22 | import android.media.AudioFocusInfo; |
| 23 | import android.media.AudioManager; |
| 24 | import android.media.AudioSystem; |
| 25 | import android.media.IAudioFocusDispatcher; |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 26 | import android.media.audiopolicy.IAudioPolicyCallback; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 27 | import android.os.Binder; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 28 | import android.os.IBinder; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 29 | import android.os.RemoteException; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 30 | import android.util.Log; |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 31 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 32 | import java.io.PrintWriter; |
| 33 | import java.util.ArrayList; |
Jean-Michel Trivi | 545fcf8 | 2015-04-07 09:45:35 -0700 | [diff] [blame] | 34 | import java.util.Date; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 35 | import java.util.Iterator; |
| 36 | import java.util.Stack; |
Jean-Michel Trivi | 545fcf8 | 2015-04-07 09:45:35 -0700 | [diff] [blame] | 37 | import java.text.DateFormat; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * @hide |
| 41 | * |
| 42 | */ |
Jean-Michel Trivi | d4de20d | 2015-11-04 14:45:54 -0800 | [diff] [blame] | 43 | public class MediaFocusControl { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 44 | |
| 45 | private static final String TAG = "MediaFocusControl"; |
| 46 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 47 | private final Context mContext; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 48 | private final AppOpsManager mAppOps; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 49 | |
Jean-Michel Trivi | d4de20d | 2015-11-04 14:45:54 -0800 | [diff] [blame] | 50 | protected MediaFocusControl(Context cntxt) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 51 | mContext = cntxt; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 52 | mAppOps = (AppOpsManager)mContext.getSystemService(Context.APP_OPS_SERVICE); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jean-Michel Trivi | 73673ab | 2013-08-06 10:42:45 -0700 | [diff] [blame] | 55 | protected void dump(PrintWriter pw) { |
Jean-Michel Trivi | 545fcf8 | 2015-04-07 09:45:35 -0700 | [diff] [blame] | 56 | pw.println("\nMediaFocusControl dump time: " |
| 57 | + DateFormat.getTimeInstance().format(new Date())); |
Jean-Michel Trivi | 73673ab | 2013-08-06 10:42:45 -0700 | [diff] [blame] | 58 | dumpFocusStack(pw); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 61 | |
| 62 | //========================================================================================== |
| 63 | // AudioFocus |
| 64 | //========================================================================================== |
| 65 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 66 | private final static Object mAudioFocusLock = new Object(); |
| 67 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 68 | /** |
| 69 | * Discard the current audio focus owner. |
| 70 | * Notify top of audio focus stack that it lost focus (regardless of possibility to reassign |
| 71 | * focus), remove it from the stack, and clear the remote control display. |
| 72 | */ |
| 73 | protected void discardAudioFocusOwner() { |
| 74 | synchronized(mAudioFocusLock) { |
Jean-Michel Trivi | cbb212f | 2013-07-30 15:09:33 -0700 | [diff] [blame] | 75 | if (!mFocusStack.empty()) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 76 | // notify the current focus owner it lost focus after removing it from stack |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 77 | final FocusRequester exFocusOwner = mFocusStack.pop(); |
Jean-Michel Trivi | 00bf4b1 | 2013-07-26 17:19:32 -0700 | [diff] [blame] | 78 | exFocusOwner.handleFocusLoss(AudioManager.AUDIOFOCUS_LOSS); |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 79 | exFocusOwner.release(); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 84 | /** |
| 85 | * Called synchronized on mAudioFocusLock |
| 86 | */ |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 87 | private void notifyTopOfAudioFocusStack() { |
| 88 | // notify the top of the stack it gained focus |
Jean-Michel Trivi | cbb212f | 2013-07-30 15:09:33 -0700 | [diff] [blame] | 89 | if (!mFocusStack.empty()) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 90 | if (canReassignAudioFocus()) { |
Jean-Michel Trivi | 00bf4b1 | 2013-07-26 17:19:32 -0700 | [diff] [blame] | 91 | mFocusStack.peek().handleFocusGain(AudioManager.AUDIOFOCUS_GAIN); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Jean-Michel Trivi | cbb212f | 2013-07-30 15:09:33 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Focus is requested, propagate the associated loss throughout the stack. |
| 98 | * @param focusGain the new focus gain that will later be added at the top of the stack |
| 99 | */ |
| 100 | private void propagateFocusLossFromGain_syncAf(int focusGain) { |
| 101 | // going through the audio focus stack to signal new focus, traversing order doesn't |
| 102 | // matter as all entries respond to the same external focus gain |
| 103 | Iterator<FocusRequester> stackIterator = mFocusStack.iterator(); |
| 104 | while(stackIterator.hasNext()) { |
| 105 | stackIterator.next().handleExternalFocusGain(focusGain); |
| 106 | } |
| 107 | } |
| 108 | |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 109 | private final Stack<FocusRequester> mFocusStack = new Stack<FocusRequester>(); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Helper function: |
| 113 | * Display in the log the current entries in the audio focus stack |
| 114 | */ |
| 115 | private void dumpFocusStack(PrintWriter pw) { |
| 116 | pw.println("\nAudio Focus stack entries (last is top of stack):"); |
| 117 | synchronized(mAudioFocusLock) { |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 118 | Iterator<FocusRequester> stackIterator = mFocusStack.iterator(); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 119 | while(stackIterator.hasNext()) { |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 120 | stackIterator.next().dump(pw); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 121 | } |
| 122 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 123 | pw.println("\n Notify on duck: " + mNotifyFocusOwnerOnDuck +"\n"); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Helper function: |
| 128 | * Called synchronized on mAudioFocusLock |
| 129 | * Remove a focus listener from the focus stack. |
| 130 | * @param clientToRemove the focus listener |
| 131 | * @param signal if true and the listener was at the top of the focus stack, i.e. it was holding |
| 132 | * focus, notify the next item in the stack it gained focus. |
| 133 | */ |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 134 | private void removeFocusStackEntry(String clientToRemove, boolean signal, |
| 135 | boolean notifyFocusFollowers) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 136 | // is the current top of the focus stack abandoning focus? (because of request, not death) |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 137 | if (!mFocusStack.empty() && mFocusStack.peek().hasSameClient(clientToRemove)) |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 138 | { |
| 139 | //Log.i(TAG, " removeFocusStackEntry() removing top of stack"); |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 140 | FocusRequester fr = mFocusStack.pop(); |
| 141 | fr.release(); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 142 | if (notifyFocusFollowers) { |
| 143 | final AudioFocusInfo afi = fr.toAudioFocusInfo(); |
| 144 | afi.clearLossReceived(); |
| 145 | notifyExtPolicyFocusLoss_syncAf(afi, false); |
| 146 | } |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 147 | if (signal) { |
| 148 | // notify the new top of the stack it gained focus |
| 149 | notifyTopOfAudioFocusStack(); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 150 | } |
| 151 | } else { |
| 152 | // focus is abandoned by a client that's not at the top of the stack, |
| 153 | // no need to update focus. |
| 154 | // (using an iterator on the stack so we can safely remove an entry after having |
| 155 | // evaluated it, traversal order doesn't matter here) |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 156 | Iterator<FocusRequester> stackIterator = mFocusStack.iterator(); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 157 | while(stackIterator.hasNext()) { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 158 | FocusRequester fr = stackIterator.next(); |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 159 | if(fr.hasSameClient(clientToRemove)) { |
Jean-Michel Trivi | 00bf4b1 | 2013-07-26 17:19:32 -0700 | [diff] [blame] | 160 | Log.i(TAG, "AudioFocus removeFocusStackEntry(): removing entry for " |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 161 | + clientToRemove); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 162 | stackIterator.remove(); |
Jean-Michel Trivi | e898772 | 2016-07-07 15:38:32 -0700 | [diff] [blame] | 163 | // stack entry not used anymore, clear references |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 164 | fr.release(); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Helper function: |
| 172 | * Called synchronized on mAudioFocusLock |
| 173 | * Remove focus listeners from the focus stack for a particular client when it has died. |
| 174 | */ |
Jean-Michel Trivi | e898772 | 2016-07-07 15:38:32 -0700 | [diff] [blame] | 175 | private void removeFocusStackEntryOnDeath(IBinder cb) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 176 | // is the owner of the audio focus part of the client to remove? |
| 177 | boolean isTopOfStackForClientToRemove = !mFocusStack.isEmpty() && |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 178 | mFocusStack.peek().hasSameBinder(cb); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 179 | // (using an iterator on the stack so we can safely remove an entry after having |
| 180 | // evaluated it, traversal order doesn't matter here) |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 181 | Iterator<FocusRequester> stackIterator = mFocusStack.iterator(); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 182 | while(stackIterator.hasNext()) { |
RoboErik | 01fe661 | 2014-02-13 14:19:04 -0800 | [diff] [blame] | 183 | FocusRequester fr = stackIterator.next(); |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 184 | if(fr.hasSameBinder(cb)) { |
Jean-Michel Trivi | e898772 | 2016-07-07 15:38:32 -0700 | [diff] [blame] | 185 | Log.i(TAG, "AudioFocus removeFocusStackEntryOnDeath(): removing entry for " + cb); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 186 | stackIterator.remove(); |
Jean-Michel Trivi | e898772 | 2016-07-07 15:38:32 -0700 | [diff] [blame] | 187 | // stack entry not used anymore, clear references |
| 188 | fr.release(); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | if (isTopOfStackForClientToRemove) { |
| 192 | // we removed an entry at the top of the stack: |
| 193 | // notify the new top of the stack it gained focus. |
| 194 | notifyTopOfAudioFocusStack(); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Helper function: |
| 200 | * Returns true if the system is in a state where the focus can be reevaluated, false otherwise. |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 201 | * The implementation guarantees that a state where focus cannot be immediately reassigned |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 202 | * implies that an "locked" focus owner is at the top of the focus stack. |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 203 | * Modifications to the implementation that break this assumption will cause focus requests to |
| 204 | * misbehave when honoring the AudioManager.AUDIOFOCUS_FLAG_DELAY_OK flag. |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 205 | */ |
| 206 | private boolean canReassignAudioFocus() { |
| 207 | // focus requests are rejected during a phone call or when the phone is ringing |
| 208 | // this is equivalent to IN_VOICE_COMM_FOCUS_ID having the focus |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 209 | if (!mFocusStack.isEmpty() && isLockedFocusOwner(mFocusStack.peek())) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 210 | return false; |
| 211 | } |
| 212 | return true; |
| 213 | } |
| 214 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 215 | private boolean isLockedFocusOwner(FocusRequester fr) { |
John Spurlock | 6156017 | 2015-02-06 19:46:04 -0500 | [diff] [blame] | 216 | return (fr.hasSameClient(AudioSystem.IN_VOICE_COMM_FOCUS_ID) || fr.isLockedFocusOwner()); |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Helper function |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 221 | * Pre-conditions: focus stack is not empty, there is one or more locked focus owner |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 222 | * at the top of the focus stack |
| 223 | * Push the focus requester onto the audio focus stack at the first position immediately |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 224 | * following the locked focus owners. |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 225 | * @return {@link AudioManager#AUDIOFOCUS_REQUEST_GRANTED} or |
| 226 | * {@link AudioManager#AUDIOFOCUS_REQUEST_DELAYED} |
| 227 | */ |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 228 | private int pushBelowLockedFocusOwners(FocusRequester nfr) { |
| 229 | int lastLockedFocusOwnerIndex = mFocusStack.size(); |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 230 | for (int index = mFocusStack.size()-1; index >= 0; index--) { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 231 | if (isLockedFocusOwner(mFocusStack.elementAt(index))) { |
| 232 | lastLockedFocusOwnerIndex = index; |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 235 | if (lastLockedFocusOwnerIndex == mFocusStack.size()) { |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 236 | // this should not happen, but handle it and log an error |
| 237 | Log.e(TAG, "No exclusive focus owner found in propagateFocusLossFromGain_syncAf()", |
| 238 | new Exception()); |
| 239 | // no exclusive owner, push at top of stack, focus is granted, propagate change |
| 240 | propagateFocusLossFromGain_syncAf(nfr.getGainRequest()); |
| 241 | mFocusStack.push(nfr); |
| 242 | return AudioManager.AUDIOFOCUS_REQUEST_GRANTED; |
| 243 | } else { |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 244 | mFocusStack.insertElementAt(nfr, lastLockedFocusOwnerIndex); |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 245 | return AudioManager.AUDIOFOCUS_REQUEST_DELAYED; |
| 246 | } |
| 247 | } |
| 248 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 249 | /** |
| 250 | * Inner class to monitor audio focus client deaths, and remove them from the audio focus |
| 251 | * stack if necessary. |
| 252 | */ |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 253 | protected class AudioFocusDeathHandler implements IBinder.DeathRecipient { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 254 | private IBinder mCb; // To be notified of client's death |
| 255 | |
| 256 | AudioFocusDeathHandler(IBinder cb) { |
| 257 | mCb = cb; |
| 258 | } |
| 259 | |
| 260 | public void binderDied() { |
| 261 | synchronized(mAudioFocusLock) { |
Jean-Michel Trivi | e898772 | 2016-07-07 15:38:32 -0700 | [diff] [blame] | 262 | removeFocusStackEntryOnDeath(mCb); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 267 | /** |
| 268 | * Indicates whether to notify an audio focus owner when it loses focus |
| 269 | * with {@link AudioManager#AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK} if it will only duck. |
| 270 | * This variable being false indicates an AudioPolicy has been registered and has signaled |
| 271 | * it will handle audio ducking. |
| 272 | */ |
| 273 | private boolean mNotifyFocusOwnerOnDuck = true; |
| 274 | |
| 275 | protected void setDuckingInExtPolicyAvailable(boolean available) { |
| 276 | mNotifyFocusOwnerOnDuck = !available; |
| 277 | } |
| 278 | |
| 279 | boolean mustNotifyFocusOwnerOnDuck() { return mNotifyFocusOwnerOnDuck; } |
| 280 | |
| 281 | private ArrayList<IAudioPolicyCallback> mFocusFollowers = new ArrayList<IAudioPolicyCallback>(); |
| 282 | |
| 283 | void addFocusFollower(IAudioPolicyCallback ff) { |
| 284 | if (ff == null) { |
| 285 | return; |
| 286 | } |
| 287 | synchronized(mAudioFocusLock) { |
| 288 | boolean found = false; |
| 289 | for (IAudioPolicyCallback pcb : mFocusFollowers) { |
| 290 | if (pcb.asBinder().equals(ff.asBinder())) { |
| 291 | found = true; |
| 292 | break; |
| 293 | } |
| 294 | } |
| 295 | if (found) { |
| 296 | return; |
| 297 | } else { |
| 298 | mFocusFollowers.add(ff); |
Jean-Michel Trivi | 60fd084 | 2015-03-13 10:11:28 -0700 | [diff] [blame] | 299 | notifyExtPolicyCurrentFocusAsync(ff); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | void removeFocusFollower(IAudioPolicyCallback ff) { |
| 305 | if (ff == null) { |
| 306 | return; |
| 307 | } |
| 308 | synchronized(mAudioFocusLock) { |
| 309 | for (IAudioPolicyCallback pcb : mFocusFollowers) { |
| 310 | if (pcb.asBinder().equals(ff.asBinder())) { |
| 311 | mFocusFollowers.remove(pcb); |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
Jean-Michel Trivi | 60fd084 | 2015-03-13 10:11:28 -0700 | [diff] [blame] | 319 | * @param pcb non null |
| 320 | */ |
| 321 | void notifyExtPolicyCurrentFocusAsync(IAudioPolicyCallback pcb) { |
| 322 | final IAudioPolicyCallback pcb2 = pcb; |
| 323 | final Thread thread = new Thread() { |
| 324 | @Override |
| 325 | public void run() { |
| 326 | synchronized(mAudioFocusLock) { |
| 327 | if (mFocusStack.isEmpty()) { |
| 328 | return; |
| 329 | } |
| 330 | try { |
| 331 | pcb2.notifyAudioFocusGrant(mFocusStack.peek().toAudioFocusInfo(), |
| 332 | // top of focus stack always has focus |
| 333 | AudioManager.AUDIOFOCUS_REQUEST_GRANTED); |
| 334 | } catch (RemoteException e) { |
| 335 | Log.e(TAG, "Can't call notifyAudioFocusGrant() on IAudioPolicyCallback " |
| 336 | + pcb2.asBinder(), e); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | }; |
| 341 | thread.start(); |
| 342 | } |
| 343 | |
| 344 | /** |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 345 | * Called synchronized on mAudioFocusLock |
| 346 | */ |
| 347 | void notifyExtPolicyFocusGrant_syncAf(AudioFocusInfo afi, int requestResult) { |
| 348 | for (IAudioPolicyCallback pcb : mFocusFollowers) { |
| 349 | try { |
| 350 | // oneway |
| 351 | pcb.notifyAudioFocusGrant(afi, requestResult); |
| 352 | } catch (RemoteException e) { |
Jean-Michel Trivi | 60fd084 | 2015-03-13 10:11:28 -0700 | [diff] [blame] | 353 | Log.e(TAG, "Can't call notifyAudioFocusGrant() on IAudioPolicyCallback " |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 354 | + pcb.asBinder(), e); |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * Called synchronized on mAudioFocusLock |
| 361 | */ |
| 362 | void notifyExtPolicyFocusLoss_syncAf(AudioFocusInfo afi, boolean wasDispatched) { |
| 363 | for (IAudioPolicyCallback pcb : mFocusFollowers) { |
| 364 | try { |
| 365 | // oneway |
| 366 | pcb.notifyAudioFocusLoss(afi, wasDispatched); |
| 367 | } catch (RemoteException e) { |
Jean-Michel Trivi | 60fd084 | 2015-03-13 10:11:28 -0700 | [diff] [blame] | 368 | Log.e(TAG, "Can't call notifyAudioFocusLoss() on IAudioPolicyCallback " |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 369 | + pcb.asBinder(), e); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | |
Jean-Michel Trivi | 2380566 | 2013-07-31 14:19:18 -0700 | [diff] [blame] | 374 | protected int getCurrentAudioFocus() { |
| 375 | synchronized(mAudioFocusLock) { |
| 376 | if (mFocusStack.empty()) { |
| 377 | return AudioManager.AUDIOFOCUS_NONE; |
| 378 | } else { |
| 379 | return mFocusStack.peek().getGainRequest(); |
| 380 | } |
| 381 | } |
| 382 | } |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 383 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 384 | /** @see AudioManager#requestAudioFocus(AudioManager.OnAudioFocusChangeListener, int, int, int) */ |
| 385 | protected int requestAudioFocus(AudioAttributes aa, int focusChangeHint, IBinder cb, |
| 386 | IAudioFocusDispatcher fd, String clientId, String callingPackageName, int flags) { |
Jean-Michel Trivi | 4039905 | 2015-11-23 15:17:33 -0800 | [diff] [blame] | 387 | Log.i(TAG, " AudioFocus requestAudioFocus() from uid/pid " + Binder.getCallingUid() |
| 388 | + "/" + Binder.getCallingPid() |
| 389 | + " clientId=" + clientId |
| 390 | + " req=" + focusChangeHint |
| 391 | + " flags=0x" + Integer.toHexString(flags)); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 392 | // we need a valid binder callback for clients |
| 393 | if (!cb.pingBinder()) { |
| 394 | Log.e(TAG, " AudioFocus DOA client for requestAudioFocus(), aborting."); |
| 395 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 396 | } |
| 397 | |
| 398 | if (mAppOps.noteOp(AppOpsManager.OP_TAKE_AUDIO_FOCUS, Binder.getCallingUid(), |
| 399 | callingPackageName) != AppOpsManager.MODE_ALLOWED) { |
| 400 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 401 | } |
| 402 | |
| 403 | synchronized(mAudioFocusLock) { |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 404 | boolean focusGrantDelayed = false; |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 405 | if (!canReassignAudioFocus()) { |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 406 | if ((flags & AudioManager.AUDIOFOCUS_FLAG_DELAY_OK) == 0) { |
| 407 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 408 | } else { |
| 409 | // request has AUDIOFOCUS_FLAG_DELAY_OK: focus can't be |
| 410 | // granted right now, so the requester will be inserted in the focus stack |
| 411 | // to receive focus later |
| 412 | focusGrantDelayed = true; |
| 413 | } |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | // handle the potential premature death of the new holder of the focus |
| 417 | // (premature death == death before abandoning focus) |
| 418 | // Register for client death notification |
| 419 | AudioFocusDeathHandler afdh = new AudioFocusDeathHandler(cb); |
Jean-Michel Trivi | e898772 | 2016-07-07 15:38:32 -0700 | [diff] [blame] | 420 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 421 | try { |
| 422 | cb.linkToDeath(afdh, 0); |
| 423 | } catch (RemoteException e) { |
| 424 | // client has already died! |
| 425 | Log.w(TAG, "AudioFocus requestAudioFocus() could not link to "+cb+" binder death"); |
| 426 | return AudioManager.AUDIOFOCUS_REQUEST_FAILED; |
| 427 | } |
| 428 | |
Jean-Michel Trivi | 83283f2 | 2013-07-29 18:09:41 -0700 | [diff] [blame] | 429 | if (!mFocusStack.empty() && mFocusStack.peek().hasSameClient(clientId)) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 430 | // if focus is already owned by this client and the reason for acquiring the focus |
| 431 | // hasn't changed, don't do anything |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 432 | final FocusRequester fr = mFocusStack.peek(); |
| 433 | if (fr.getGainRequest() == focusChangeHint && fr.getGrantFlags() == flags) { |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 434 | // unlink death handler so it can be gc'ed. |
| 435 | // linkToDeath() creates a JNI global reference preventing collection. |
| 436 | cb.unlinkToDeath(afdh, 0); |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 437 | notifyExtPolicyFocusGrant_syncAf(fr.toAudioFocusInfo(), |
| 438 | AudioManager.AUDIOFOCUS_REQUEST_GRANTED); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 439 | return AudioManager.AUDIOFOCUS_REQUEST_GRANTED; |
| 440 | } |
| 441 | // the reason for the audio focus request has changed: remove the current top of |
| 442 | // stack and respond as if we had a new focus owner |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 443 | if (!focusGrantDelayed) { |
| 444 | mFocusStack.pop(); |
| 445 | // the entry that was "popped" is the same that was "peeked" above |
| 446 | fr.release(); |
| 447 | } |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 450 | // focus requester might already be somewhere below in the stack, remove it |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 451 | removeFocusStackEntry(clientId, false /* signal */, false /*notifyFocusFollowers*/); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 452 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 453 | final FocusRequester nfr = new FocusRequester(aa, focusChangeHint, flags, fd, cb, |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 454 | clientId, afdh, callingPackageName, Binder.getCallingUid(), this); |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 455 | if (focusGrantDelayed) { |
| 456 | // focusGrantDelayed being true implies we can't reassign focus right now |
| 457 | // which implies the focus stack is not empty. |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 458 | final int requestResult = pushBelowLockedFocusOwners(nfr); |
| 459 | if (requestResult != AudioManager.AUDIOFOCUS_REQUEST_FAILED) { |
| 460 | notifyExtPolicyFocusGrant_syncAf(nfr.toAudioFocusInfo(), requestResult); |
| 461 | } |
| 462 | return requestResult; |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 463 | } else { |
| 464 | // propagate the focus change through the stack |
| 465 | if (!mFocusStack.empty()) { |
| 466 | propagateFocusLossFromGain_syncAf(focusChangeHint); |
| 467 | } |
Jean-Michel Trivi | cbb212f | 2013-07-30 15:09:33 -0700 | [diff] [blame] | 468 | |
Jean-Michel Trivi | fd6ad74 | 2014-11-10 14:38:30 -0800 | [diff] [blame] | 469 | // push focus requester at the top of the audio focus stack |
| 470 | mFocusStack.push(nfr); |
| 471 | } |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 472 | notifyExtPolicyFocusGrant_syncAf(nfr.toAudioFocusInfo(), |
| 473 | AudioManager.AUDIOFOCUS_REQUEST_GRANTED); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 474 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 475 | }//synchronized(mAudioFocusLock) |
| 476 | |
| 477 | return AudioManager.AUDIOFOCUS_REQUEST_GRANTED; |
| 478 | } |
| 479 | |
Jean-Michel Trivi | 958876f | 2014-11-16 15:40:22 -0800 | [diff] [blame] | 480 | /** |
| 481 | * @see AudioManager#abandonAudioFocus(AudioManager.OnAudioFocusChangeListener, AudioAttributes) |
| 482 | * */ |
| 483 | protected int abandonAudioFocus(IAudioFocusDispatcher fl, String clientId, AudioAttributes aa) { |
| 484 | // AudioAttributes are currently ignored, to be used for zones |
Jean-Michel Trivi | 4039905 | 2015-11-23 15:17:33 -0800 | [diff] [blame] | 485 | Log.i(TAG, " AudioFocus abandonAudioFocus() from uid/pid " + Binder.getCallingUid() |
| 486 | + "/" + Binder.getCallingPid() |
| 487 | + " clientId=" + clientId); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 488 | try { |
| 489 | // this will take care of notifying the new focus owner if needed |
| 490 | synchronized(mAudioFocusLock) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 491 | removeFocusStackEntry(clientId, true /*signal*/, true /*notifyFocusFollowers*/); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 492 | } |
| 493 | } catch (java.util.ConcurrentModificationException cme) { |
| 494 | // Catching this exception here is temporary. It is here just to prevent |
| 495 | // a crash seen when the "Silent" notification is played. This is believed to be fixed |
| 496 | // but this try catch block is left just to be safe. |
| 497 | Log.e(TAG, "FATAL EXCEPTION AudioFocus abandonAudioFocus() caused " + cme); |
| 498 | cme.printStackTrace(); |
| 499 | } |
| 500 | |
| 501 | return AudioManager.AUDIOFOCUS_REQUEST_GRANTED; |
| 502 | } |
| 503 | |
| 504 | |
| 505 | protected void unregisterAudioFocusClient(String clientId) { |
| 506 | synchronized(mAudioFocusLock) { |
Jean-Michel Trivi | 0212be5 | 2014-11-24 14:43:10 -0800 | [diff] [blame] | 507 | removeFocusStackEntry(clientId, false, true /*notifyFocusFollowers*/); |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
Jean-Michel Trivi | fa9a698 | 2013-06-27 16:22:58 -0700 | [diff] [blame] | 511 | } |