Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 62cef488 authored by Insun Kang's avatar Insun Kang
Browse files

MediaSession: Remove pending callback messages when setCallback(null) is called.

setCallback(null) is supposed to clear registered callbacks.
Previously, setCallback(null) doesn't remove pending callback messages
and so belated callbacks are called quite frequently after
setCallback(null) is done. This CL reduces major number of such cases by
removing pending callback messages.

Bug: 63446360
Test: Ran cts test
adb shell am instrument -w -e class \
android.media.cts.MediaSessionTest#testSetCallbackWithNull \
android.media.cts/android.support.test.runner.AndroidJUnitRunner

Change-Id: I7192b0a61a2114390f10734bbb48aeac56a19fe3
parent 1d4be040
Loading
Loading
Loading
Loading
+10 −12
Original line number Diff line number Diff line
@@ -118,7 +118,7 @@ public final class MediaSession {
    private final ISession mBinder;
    private final CallbackStub mCbStub;

    private CallbackMessageHandler mCallback;
    private CallbackMessageHandler mCallbackHandler;
    private VolumeProvider mVolumeProvider;
    private PlaybackState mPlaybackState;

@@ -193,24 +193,22 @@ public final class MediaSession {
     */
    public void setCallback(@Nullable Callback callback, @Nullable Handler handler) {
        synchronized (mLock) {
            if (callback == null) {
                if (mCallback != null) {
                    mCallback.mCallback.mSession = null;
            if (mCallbackHandler != null) {
                // We're updating the callback, clear the session from the old one.
                mCallbackHandler.mCallback.mSession = null;
                mCallbackHandler.removeCallbacksAndMessages(null);
            }
                mCallback = null;
            if (callback == null) {
                mCallbackHandler = null;
                return;
            }
            if (mCallback != null) {
                // We're updating the callback, clear the session from the old one.
                mCallback.mCallback.mSession = null;
            }
            if (handler == null) {
                handler = new Handler();
            }
            callback.mSession = this;
            CallbackMessageHandler msgHandler = new CallbackMessageHandler(handler.getLooper(),
                    callback);
            mCallback = msgHandler;
            mCallbackHandler = msgHandler;
        }
    }

@@ -635,8 +633,8 @@ public final class MediaSession {

    private void postToCallback(int what, Object obj, Bundle extras) {
        synchronized (mLock) {
            if (mCallback != null) {
                mCallback.post(what, obj, extras);
            if (mCallbackHandler != null) {
                mCallbackHandler.post(what, obj, extras);
            }
        }
    }