Post volume requests to the handler thread in MediaSession

Volume requests should be getting sent on the same thread as other
callbacks, not on the binder thread.

bug:18156755
Change-Id: I35bceb3f8c74ccba35f3d08ff000a254fcbd04bb
This commit is contained in:
RoboErik
2014-10-28 13:26:26 -07:00
parent ac7d8c5cc8
commit 563acea666

View File

@@ -286,7 +286,9 @@ public final class MediaSession {
if (volumeProvider == null) {
throw new IllegalArgumentException("volumeProvider may not be null!");
}
mVolumeProvider = volumeProvider;
synchronized (mLock) {
mVolumeProvider = volumeProvider;
}
volumeProvider.setCallback(new VolumeProvider.Callback() {
@Override
public void onVolumeChanged(VolumeProvider volumeProvider) {
@@ -470,9 +472,11 @@ public final class MediaSession {
* @hide
*/
public void notifyRemoteVolumeChanged(VolumeProvider provider) {
if (provider == null || provider != mVolumeProvider) {
Log.w(TAG, "Received update from stale volume provider");
return;
synchronized (mLock) {
if (provider == null || provider != mVolumeProvider) {
Log.w(TAG, "Received update from stale volume provider");
return;
}
}
try {
mBinder.setCurrentVolume(provider.getCurrentVolume());
@@ -537,6 +541,14 @@ public final class MediaSession {
postToCallback(CallbackMessageHandler.MSG_MEDIA_BUTTON, mediaButtonIntent);
}
private void dispatchAdjustVolume(int direction) {
postToCallback(CallbackMessageHandler.MSG_ADJUST_VOLUME, direction);
}
private void dispatchSetVolumeTo(int volume) {
postToCallback(CallbackMessageHandler.MSG_SET_VOLUME, volume);
}
private void postToCallback(int what) {
postToCallback(what, null);
}
@@ -988,9 +1000,7 @@ public final class MediaSession {
public void onAdjustVolume(int direction) {
MediaSession session = mMediaSession.get();
if (session != null) {
if (session.mVolumeProvider != null) {
session.mVolumeProvider.onAdjustVolume(direction);
}
session.dispatchAdjustVolume(direction);
}
}
@@ -998,9 +1008,7 @@ public final class MediaSession {
public void onSetVolumeTo(int value) {
MediaSession session = mMediaSession.get();
if (session != null) {
if (session.mVolumeProvider != null) {
session.mVolumeProvider.onSetVolumeTo(value);
}
session.dispatchSetVolumeTo(value);
}
}
@@ -1117,6 +1125,8 @@ public final class MediaSession {
private static final int MSG_CUSTOM_ACTION = 13;
private static final int MSG_MEDIA_BUTTON = 14;
private static final int MSG_COMMAND = 15;
private static final int MSG_ADJUST_VOLUME = 16;
private static final int MSG_SET_VOLUME = 17;
private MediaSession.Callback mCallback;
@@ -1145,6 +1155,7 @@ public final class MediaSession {
@Override
public void handleMessage(Message msg) {
VolumeProvider vp;
switch (msg.what) {
case MSG_PLAY:
mCallback.onPlay();
@@ -1192,6 +1203,22 @@ public final class MediaSession {
Command cmd = (Command) msg.obj;
mCallback.onCommand(cmd.command, cmd.extras, cmd.stub);
break;
case MSG_ADJUST_VOLUME:
synchronized (mLock) {
vp = mVolumeProvider;
}
if (vp != null) {
vp.onAdjustVolume((int) msg.obj);
}
break;
case MSG_SET_VOLUME:
synchronized (mLock) {
vp = mVolumeProvider;
}
if (vp != null) {
vp.onSetVolumeTo((int) msg.obj);
}
break;
}
}
}