Skip to content
Snippets Groups Projects
Commit 115eead2 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "MediaSessionManager: Use Executor instead of Handler"

parents e185ac2b 95c783e2
No related branches found
No related tags found
No related merge requests found
......@@ -3693,7 +3693,7 @@ package android.media.audiopolicy {
package android.media.session {
 
public final class MediaSessionManager {
method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void registerCallback(@NonNull android.media.session.MediaSessionManager.Callback, @Nullable android.os.Handler);
method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void registerCallback(@NonNull java.util.concurrent.Executor, @NonNull android.media.session.MediaSessionManager.Callback);
method @RequiresPermission(android.Manifest.permission.SET_MEDIA_KEY_LISTENER) public void setOnMediaKeyListener(android.media.session.MediaSessionManager.OnMediaKeyListener, @Nullable android.os.Handler);
method @RequiresPermission(android.Manifest.permission.SET_VOLUME_KEY_LONG_PRESS_LISTENER) public void setOnVolumeKeyLongPressListener(android.media.session.MediaSessionManager.OnVolumeKeyLongPressListener, @Nullable android.os.Handler);
method @RequiresPermission(android.Manifest.permission.MEDIA_CONTENT_CONTROL) public void unregisterCallback(@NonNull android.media.session.MediaSessionManager.Callback);
......
......@@ -16,6 +16,7 @@
package android.media.session;
import android.annotation.CallbackExecutor;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
......@@ -31,6 +32,7 @@ import android.media.MediaSession2;
import android.media.Session2Token;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerExecutor;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ResultReceiver;
......@@ -50,6 +52,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
/**
* Provides support for interacting with {@link MediaSession media sessions}
......@@ -86,7 +89,7 @@ public final class MediaSessionManager {
@GuardedBy("mLock")
private final CallbackStub mCbStub = new CallbackStub();
@GuardedBy("mLock")
private final Map<Callback, Handler> mCallbacks = new HashMap<>();
private final Map<Callback, Executor> mCallbacks = new HashMap<>();
@GuardedBy("mLock")
private MediaSession.Token mCurMediaButtonSession;
@GuardedBy("mLock")
......@@ -765,13 +768,16 @@ public final class MediaSessionManager {
*/
// TODO: Remove this method once Bluetooth app stop calling it.
public void setCallback(@Nullable Callback callback, @Nullable Handler handler) {
if (handler == null) {
handler = new Handler();
}
synchronized (mLock) {
if (mLegacyCallback != null) {
unregisterCallback(mLegacyCallback);
}
mLegacyCallback = callback;
if (callback != null) {
registerCallback(callback, handler);
registerCallback(new HandlerExecutor(handler), callback);
}
}
}
......@@ -779,27 +785,29 @@ public final class MediaSessionManager {
/**
* Register a {@link Callback}.
*
* @param executor The executor on which the callback should be invoked
* @param callback A {@link Callback}.
* @param handler The handler on which the callback should be invoked, or {@code null}
* if the callback should be invoked on the calling thread's looper.
* @hide
*/
@SystemApi
@RequiresPermission(value = android.Manifest.permission.MEDIA_CONTENT_CONTROL)
public void registerCallback(@NonNull Callback callback, @Nullable Handler handler) {
public void registerCallback(@NonNull @CallbackExecutor Executor executor,
@NonNull Callback callback) {
if (executor == null) {
throw new NullPointerException("executor shouldn't be null");
}
if (callback == null) {
throw new NullPointerException("callback shouldn't be null");
}
synchronized (mLock) {
try {
if (handler == null) {
handler = new Handler();
}
mCallbacks.put(callback, handler);
mCallbacks.put(callback, executor);
if (mCurMediaButtonSession != null) {
handler.post(() -> callback.onAddressedPlayerChanged(mCurMediaButtonSession));
executor.execute(
() -> callback.onAddressedPlayerChanged(mCurMediaButtonSession));
} else if (mCurMediaButtonReceiver != null) {
handler.post(() -> callback.onAddressedPlayerChanged(mCurMediaButtonReceiver));
executor.execute(
() -> callback.onAddressedPlayerChanged(mCurMediaButtonReceiver));
}
if (mCallbacks.size() == 1) {
......@@ -1147,8 +1155,8 @@ public final class MediaSessionManager {
public void onMediaKeyEventDispatchedToMediaSession(KeyEvent event,
MediaSession.Token sessionToken) {
synchronized (mLock) {
for (Map.Entry<Callback, Handler> e : mCallbacks.entrySet()) {
e.getValue().post(
for (Map.Entry<Callback, Executor> e : mCallbacks.entrySet()) {
e.getValue().execute(
() -> e.getKey().onMediaKeyEventDispatched(event, sessionToken));
}
}
......@@ -1158,8 +1166,8 @@ public final class MediaSessionManager {
public void onMediaKeyEventDispatchedToMediaButtonReceiver(KeyEvent event,
ComponentName mediaButtonReceiver) {
synchronized (mLock) {
for (Map.Entry<Callback, Handler> e : mCallbacks.entrySet()) {
e.getValue().post(
for (Map.Entry<Callback, Executor> e : mCallbacks.entrySet()) {
e.getValue().execute(
() -> e.getKey().onMediaKeyEventDispatched(event, mediaButtonReceiver));
}
}
......@@ -1170,8 +1178,8 @@ public final class MediaSessionManager {
synchronized (mLock) {
mCurMediaButtonSession = sessionToken;
mCurMediaButtonReceiver = null;
for (Map.Entry<Callback, Handler> e : mCallbacks.entrySet()) {
e.getValue().post(() -> e.getKey().onAddressedPlayerChanged(sessionToken));
for (Map.Entry<Callback, Executor> e : mCallbacks.entrySet()) {
e.getValue().execute(() -> e.getKey().onAddressedPlayerChanged(sessionToken));
}
}
}
......@@ -1182,8 +1190,8 @@ public final class MediaSessionManager {
synchronized (mLock) {
mCurMediaButtonSession = null;
mCurMediaButtonReceiver = mediaButtonReceiver;
for (Map.Entry<Callback, Handler> e : mCallbacks.entrySet()) {
e.getValue().post(() -> e.getKey().onAddressedPlayerChanged(
for (Map.Entry<Callback, Executor> e : mCallbacks.entrySet()) {
e.getValue().execute(() -> e.getKey().onAddressedPlayerChanged(
mediaButtonReceiver));
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment