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

Commit 9369087f authored by tonihei's avatar tonihei
Browse files

Add MediaSession.finalize to ensure the session is unregistered

Apps may forget to call release(), which risks leaking the
session state in the system which keeps the reference to it for
as long as the app is alive.

This can be fixed by adding a finalize() implementation that
triggers the release if not already done.

Bug: 261882243
Flag: EXEMPT bugfix
Test: new test in same topic
Change-Id: I41afa5bef3b1b90501851ee7875217ef8f268930
parent 34558a2c
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -146,6 +146,7 @@ public final class MediaSession {
    private CallbackMessageHandler mCallback;
    private VolumeProvider mVolumeProvider;
    private PlaybackState mPlaybackState;
    private boolean mIsReleased;

    private boolean mActive = false;

@@ -451,11 +452,26 @@ public final class MediaSession {
     * but it must be released if your activity or service is being destroyed.
     */
    public void release() {
        if (mIsReleased) {
            return;
        }
        setCallback(null);
        try {
            mBinder.destroySession();
        } catch (RemoteException e) {
            Log.wtf(TAG, "Error releasing session: ", e);
        } finally {
            mIsReleased = true;
        }
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            // Fallback if release() hasn't been called already.
            release();
        } finally {
            super.finalize();
        }
    }