Skip to content

Commit a6ec356

Browse files
jmtriviAndroid (Google) Code Review
authored andcommitted
Merge "Fix 5243349 RemoteControlDisplay incorrectly updated"
2 parents 185a251 + b716f0b commit a6ec356

File tree

1 file changed

+47
-43
lines changed

1 file changed

+47
-43
lines changed

media/java/android/media/AudioService.java

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,7 +2638,7 @@ private void removeFocusStackEntry(String clientToRemove, boolean signal) {
26382638
notifyTopOfAudioFocusStack();
26392639
// there's a new top of the stack, let the remote control know
26402640
synchronized(mRCStack) {
2641-
checkUpdateRemoteControlDisplay_syncRcs(RC_INFO_ALL);
2641+
checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL);
26422642
}
26432643
}
26442644
} else {
@@ -2681,7 +2681,7 @@ private void removeFocusStackEntryForClient(IBinder cb) {
26812681
notifyTopOfAudioFocusStack();
26822682
// there's a new top of the stack, let the remote control know
26832683
synchronized(mRCStack) {
2684-
checkUpdateRemoteControlDisplay_syncRcs(RC_INFO_ALL);
2684+
checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL);
26852685
}
26862686
}
26872687
}
@@ -2785,7 +2785,7 @@ public int requestAudioFocus(int mainStreamType, int focusChangeHint, IBinder cb
27852785

27862786
// there's a new top of the stack, let the remote control know
27872787
synchronized(mRCStack) {
2788-
checkUpdateRemoteControlDisplay_syncRcs(RC_INFO_ALL);
2788+
checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL);
27892789
}
27902790
}//synchronized(mAudioFocusLock)
27912791

@@ -3183,7 +3183,7 @@ private void onRcDisplayUpdate(RemoteControlStackEntry rcse, int flags /* USED ?
31833183
* Helper function:
31843184
* Called synchronized on mRCStack
31853185
*/
3186-
private void clearRemoteControlDisplay_syncRcs() {
3186+
private void clearRemoteControlDisplay_syncAfRcs() {
31873187
synchronized(mCurrentRcLock) {
31883188
mCurrentRcClient = null;
31893189
}
@@ -3192,18 +3192,21 @@ private void clearRemoteControlDisplay_syncRcs() {
31923192
}
31933193

31943194
/**
3195-
* Helper function:
3196-
* Called synchronized on mRCStack
3197-
* mRCStack.isEmpty() is false
3195+
* Helper function for code readability: only to be called from
3196+
* checkUpdateRemoteControlDisplay_syncAfRcs() which checks the preconditions for
3197+
* this method.
3198+
* Preconditions:
3199+
* - called synchronized mAudioFocusLock then on mRCStack
3200+
* - mRCStack.isEmpty() is false
31983201
*/
3199-
private void updateRemoteControlDisplay_syncRcs(int infoChangedFlags) {
3202+
private void updateRemoteControlDisplay_syncAfRcs(int infoChangedFlags) {
32003203
RemoteControlStackEntry rcse = mRCStack.peek();
32013204
int infoFlagsAboutToBeUsed = infoChangedFlags;
32023205
// this is where we enforce opt-in for information display on the remote controls
32033206
// with the new AudioManager.registerRemoteControlClient() API
32043207
if (rcse.mRcClient == null) {
32053208
//Log.w(TAG, "Can't update remote control display with null remote control client");
3206-
clearRemoteControlDisplay_syncRcs();
3209+
clearRemoteControlDisplay_syncAfRcs();
32073210
return;
32083211
}
32093212
synchronized(mCurrentRcLock) {
@@ -3220,36 +3223,36 @@ private void updateRemoteControlDisplay_syncRcs(int infoChangedFlags) {
32203223

32213224
/**
32223225
* Helper function:
3223-
* Called synchronized on mFocusLock, then mRCStack
3226+
* Called synchronized on mAudioFocusLock, then mRCStack
32243227
* Check whether the remote control display should be updated, triggers the update if required
32253228
* @param infoChangedFlags the flags corresponding to the remote control client information
32263229
* that has changed, if applicable (checking for the update conditions might trigger a
32273230
* clear, rather than an update event).
32283231
*/
3229-
private void checkUpdateRemoteControlDisplay_syncRcs(int infoChangedFlags) {
3232+
private void checkUpdateRemoteControlDisplay_syncAfRcs(int infoChangedFlags) {
32303233
// determine whether the remote control display should be refreshed
32313234
// if either stack is empty, there is a mismatch, so clear the RC display
32323235
if (mRCStack.isEmpty() || mFocusStack.isEmpty()) {
3233-
clearRemoteControlDisplay_syncRcs();
3236+
clearRemoteControlDisplay_syncAfRcs();
32343237
return;
32353238
}
32363239
// if the top of the two stacks belong to different packages, there is a mismatch, clear
32373240
if ((mRCStack.peek().mCallingPackageName != null)
32383241
&& (mFocusStack.peek().mPackageName != null)
32393242
&& !(mRCStack.peek().mCallingPackageName.compareTo(
32403243
mFocusStack.peek().mPackageName) == 0)) {
3241-
clearRemoteControlDisplay_syncRcs();
3244+
clearRemoteControlDisplay_syncAfRcs();
32423245
return;
32433246
}
32443247
// if the audio focus didn't originate from the same Uid as the one in which the remote
32453248
// control information will be retrieved, clear
32463249
if (mRCStack.peek().mCallingUid != mFocusStack.peek().mCallingUid) {
3247-
clearRemoteControlDisplay_syncRcs();
3250+
clearRemoteControlDisplay_syncAfRcs();
32483251
return;
32493252
}
32503253
// refresh conditions were verified: update the remote controls
3251-
// ok to call, mRCStack is not empty
3252-
updateRemoteControlDisplay_syncRcs(infoChangedFlags);
3254+
// ok to call: synchronized mAudioFocusLock then on mRCStack, mRCStack is not empty
3255+
updateRemoteControlDisplay_syncAfRcs(infoChangedFlags);
32533256
}
32543257

32553258
/** see AudioManager.registerMediaButtonEventReceiver(ComponentName eventReceiver) */
@@ -3260,7 +3263,7 @@ public void registerMediaButtonEventReceiver(ComponentName eventReceiver) {
32603263
synchronized(mRCStack) {
32613264
pushMediaButtonReceiver(eventReceiver);
32623265
// new RC client, assume every type of information shall be queried
3263-
checkUpdateRemoteControlDisplay_syncRcs(RC_INFO_ALL);
3266+
checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL);
32643267
}
32653268
}
32663269
}
@@ -3275,7 +3278,7 @@ public void unregisterMediaButtonEventReceiver(ComponentName eventReceiver) {
32753278
removeMediaButtonReceiver(eventReceiver);
32763279
if (topOfStackWillChange) {
32773280
// current RC client will change, assume every type of info needs to be queried
3278-
checkUpdateRemoteControlDisplay_syncRcs(RC_INFO_ALL);
3281+
checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL);
32793282
}
32803283
}
32813284
}
@@ -3284,6 +3287,7 @@ public void unregisterMediaButtonEventReceiver(ComponentName eventReceiver) {
32843287
/** see AudioManager.registerRemoteControlClient(ComponentName eventReceiver, ...) */
32853288
public void registerRemoteControlClient(ComponentName eventReceiver,
32863289
IRemoteControlClient rcClient, String clientName, String callingPackageName) {
3290+
if (DEBUG_RC) Log.i(TAG, "Register remote control client rcClient="+rcClient);
32873291
synchronized(mAudioFocusLock) {
32883292
synchronized(mRCStack) {
32893293
// store the new display information
@@ -3331,7 +3335,7 @@ public void registerRemoteControlClient(ComponentName eventReceiver,
33313335
// if the eventReceiver is at the top of the stack
33323336
// then check for potential refresh of the remote controls
33333337
if (isCurrentRcController(eventReceiver)) {
3334-
checkUpdateRemoteControlDisplay_syncRcs(RC_INFO_ALL);
3338+
checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL);
33353339
}
33363340
}
33373341
}
@@ -3436,35 +3440,35 @@ private void rcDisplay_startDeathMonitor_syncRcStack() {
34363440
*/
34373441
public void registerRemoteControlDisplay(IRemoteControlDisplay rcd) {
34383442
if (DEBUG_RC) Log.d(TAG, ">>> registerRemoteControlDisplay("+rcd+")");
3439-
synchronized(mRCStack) {
3440-
if ((mRcDisplay == rcd) || (rcd == null)) {
3441-
return;
3442-
}
3443-
// if we had a display before, stop monitoring its death
3444-
rcDisplay_stopDeathMonitor_syncRcStack();
3445-
mRcDisplay = rcd;
3446-
// new display, start monitoring its death
3447-
rcDisplay_startDeathMonitor_syncRcStack();
3443+
synchronized(mAudioFocusLock) {
3444+
synchronized(mRCStack) {
3445+
if ((mRcDisplay == rcd) || (rcd == null)) {
3446+
return;
3447+
}
3448+
// if we had a display before, stop monitoring its death
3449+
rcDisplay_stopDeathMonitor_syncRcStack();
3450+
mRcDisplay = rcd;
3451+
// new display, start monitoring its death
3452+
rcDisplay_startDeathMonitor_syncRcStack();
34483453

3449-
// let all the remote control clients there is a new display
3450-
// no need to unplug the previous because we only support one display
3451-
// and the clients don't track the death of the display
3452-
Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator();
3453-
while(stackIterator.hasNext()) {
3454-
RemoteControlStackEntry rcse = stackIterator.next();
3455-
if(rcse.mRcClient != null) {
3456-
try {
3457-
rcse.mRcClient.plugRemoteControlDisplay(mRcDisplay);
3458-
} catch (RemoteException e) {
3459-
Log.e(TAG, "Error connecting remote control display to client: " + e);
3460-
e.printStackTrace();
3454+
// let all the remote control clients there is a new display
3455+
// no need to unplug the previous because we only support one display
3456+
// and the clients don't track the death of the display
3457+
Iterator<RemoteControlStackEntry> stackIterator = mRCStack.iterator();
3458+
while(stackIterator.hasNext()) {
3459+
RemoteControlStackEntry rcse = stackIterator.next();
3460+
if(rcse.mRcClient != null) {
3461+
try {
3462+
rcse.mRcClient.plugRemoteControlDisplay(mRcDisplay);
3463+
} catch (RemoteException e) {
3464+
Log.e(TAG, "Error connecting remote control display to client: " + e);
3465+
e.printStackTrace();
3466+
}
34613467
}
34623468
}
3463-
}
34643469

3465-
if (!mRCStack.isEmpty()) {
34663470
// we have a new display, of which all the clients are now aware: have it be updated
3467-
updateRemoteControlDisplay_syncRcs(RC_INFO_ALL);
3471+
checkUpdateRemoteControlDisplay_syncAfRcs(RC_INFO_ALL);
34683472
}
34693473
}
34703474
}

0 commit comments

Comments
 (0)