Merge "Audio focus: clean up FocusRequester early" into nyc-mr1-dev

This commit is contained in:
Jean-Michel Trivi
2016-09-13 00:44:54 +00:00
committed by Android (Google) Code Review
2 changed files with 17 additions and 16 deletions

View File

@@ -40,9 +40,9 @@ public class FocusRequester {
private static final String TAG = "MediaFocusControl";
private static final boolean DEBUG = false;
private AudioFocusDeathHandler mDeathHandler;
private final IAudioFocusDispatcher mFocusDispatcher; // may be null
private final IBinder mSourceRef;
private AudioFocusDeathHandler mDeathHandler; // may be null
private IAudioFocusDispatcher mFocusDispatcher; // may be null
private final IBinder mSourceRef; // may be null
private final String mClientId;
private final String mPackageName;
private final int mCallingUid;
@@ -205,6 +205,7 @@ public class FocusRequester {
if (mSourceRef != null && mDeathHandler != null) {
mSourceRef.unlinkToDeath(mDeathHandler, 0);
mDeathHandler = null;
mFocusDispatcher = null;
}
} catch (java.util.NoSuchElementException e) {
Log.e(TAG, "FocusRequester.release() hit ", e);
@@ -275,12 +276,13 @@ public class FocusRequester {
mFocusLossReceived = AudioManager.AUDIOFOCUS_NONE;
mFocusController.notifyExtPolicyFocusGrant_syncAf(toAudioFocusInfo(),
AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
if (mFocusDispatcher != null) {
final IAudioFocusDispatcher fd = mFocusDispatcher;
if (fd != null) {
if (DEBUG) {
Log.v(TAG, "dispatching " + focusChangeToString(focusGain) + " to "
+ mClientId);
}
mFocusDispatcher.dispatchAudioFocusChange(focusGain, mClientId);
fd.dispatchAudioFocusChange(focusGain, mClientId);
}
} catch (android.os.RemoteException e) {
Log.e(TAG, "Failure to signal gain of audio focus due to: ", e);
@@ -311,14 +313,15 @@ public class FocusRequester {
toAudioFocusInfo(), false /* wasDispatched */);
return;
}
if (mFocusDispatcher != null) {
final IAudioFocusDispatcher fd = mFocusDispatcher;
if (fd != null) {
if (DEBUG) {
Log.v(TAG, "dispatching " + focusChangeToString(mFocusLossReceived) + " to "
+ mClientId);
}
mFocusController.notifyExtPolicyFocusLoss_syncAf(
toAudioFocusInfo(), true /* wasDispatched */);
mFocusDispatcher.dispatchAudioFocusChange(mFocusLossReceived, mClientId);
fd.dispatchAudioFocusChange(mFocusLossReceived, mClientId);
}
}
} catch (android.os.RemoteException e) {

View File

@@ -160,6 +160,7 @@ public class MediaFocusControl {
Log.i(TAG, "AudioFocus removeFocusStackEntry(): removing entry for "
+ clientToRemove);
stackIterator.remove();
// stack entry not used anymore, clear references
fr.release();
}
}
@@ -171,7 +172,7 @@ public class MediaFocusControl {
* Called synchronized on mAudioFocusLock
* Remove focus listeners from the focus stack for a particular client when it has died.
*/
private void removeFocusStackEntryForClient(IBinder cb) {
private void removeFocusStackEntryOnDeath(IBinder cb) {
// is the owner of the audio focus part of the client to remove?
boolean isTopOfStackForClientToRemove = !mFocusStack.isEmpty() &&
mFocusStack.peek().hasSameBinder(cb);
@@ -181,9 +182,10 @@ public class MediaFocusControl {
while(stackIterator.hasNext()) {
FocusRequester fr = stackIterator.next();
if(fr.hasSameBinder(cb)) {
Log.i(TAG, "AudioFocus removeFocusStackEntry(): removing entry for " + cb);
Log.i(TAG, "AudioFocus removeFocusStackEntryOnDeath(): removing entry for " + cb);
stackIterator.remove();
// the client just died, no need to unlink to its death
// stack entry not used anymore, clear references
fr.release();
}
}
if (isTopOfStackForClientToRemove) {
@@ -257,14 +259,9 @@ public class MediaFocusControl {
public void binderDied() {
synchronized(mAudioFocusLock) {
Log.w(TAG, " AudioFocus audio focus client died");
removeFocusStackEntryForClient(mCb);
removeFocusStackEntryOnDeath(mCb);
}
}
public IBinder getBinder() {
return mCb;
}
}
/**
@@ -420,6 +417,7 @@ public class MediaFocusControl {
// (premature death == death before abandoning focus)
// Register for client death notification
AudioFocusDeathHandler afdh = new AudioFocusDeathHandler(cb);
try {
cb.linkToDeath(afdh, 0);
} catch (RemoteException e) {