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

Commit 1f4c6d0d authored by Ahmad Khalil's avatar Ahmad Khalil Committed by Android (Google) Code Review
Browse files

Merge "Fix NotificationPlayer wakelock being held for too long." into main

parents 7a2d97f0 eed37509
Loading
Loading
Loading
Loading
+72 −54
Original line number Diff line number Diff line
@@ -216,66 +216,83 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener
        }
    }

    private final class CmdThread extends java.lang.Thread {
        CmdThread() {
            super("NotificationPlayer-" + mTag);
        }

        public void run() {
            while (true) {
                Command cmd = null;

                synchronized (mCmdQueue) {
                    if (DEBUG) Log.d(mTag, "RemoveFirst");
                    cmd = mCmdQueue.removeFirst();
                }

                switch (cmd.code) {
                case PLAY:
                    if (DEBUG) Log.d(mTag, "PLAY");
                    startSound(cmd);
                    break;
                case STOP:
                    if (DEBUG) Log.d(mTag, "STOP");
    private void stopSound(Command cmd) {
        final MediaPlayer mp;
        synchronized (mPlayerLock) {
            mp = mPlayer;
            mPlayer = null;
        }
                    if (mp != null) {
        if (mp == null) {
            Log.w(mTag, "STOP command without a player");
            return;
        }

        long delay = SystemClock.uptimeMillis() - cmd.requestTime;
        if (delay > 1000) {
            Log.w(mTag, "Notification stop delayed by " + delay + "msecs");
        }
        try {
            mp.stop();
                        } catch (Exception e) { }
        } catch (Exception e) {
            Log.w(mTag, "Failed to stop MediaPlayer", e);
        }
        if (DEBUG) {
            Log.i(mTag, "About to release MediaPlayer piid:"
                    + mp.getPlayerIId() + " due to notif cancelled");
        }
        try {
            mp.release();
        } catch (Exception e) {
            Log.w(mTag, "Failed to release MediaPlayer", e);
        }
        synchronized (mQueueAudioFocusLock) {
            if (mAudioManagerWithAudioFocus != null) {
                                if (DEBUG) { Log.d(mTag, "in STOP: abandonning AudioFocus"); }
                if (DEBUG) {
                    Log.d(mTag, "in STOP: abandonning AudioFocus");
                }
                try {
                    mAudioManagerWithAudioFocus.abandonAudioFocus(null);
                } catch (Exception e) {
                    Log.w(mTag, "Failed to abandon audio focus", e);
                }
                mAudioManagerWithAudioFocus = null;
            }
        }
        synchronized (mCompletionHandlingLock) {
                            if ((mLooper != null) &&
                                    (mLooper.getThread().getState() != Thread.State.TERMINATED))
                            {
                                if (DEBUG) { Log.d(mTag, "in STOP: quitting looper "+ mLooper); }
            if ((mLooper != null) && (mLooper.getThread().getState() != Thread.State.TERMINATED)) {
                if (DEBUG) {
                    Log.d(mTag, "in STOP: quitting looper " + mLooper);
                }
                mLooper.quit();
            }
        }
                    } else {
                        Log.w(mTag, "STOP command without a player");
    }
                    break;

    private final class CmdThread extends java.lang.Thread {
        CmdThread() {
            super("NotificationPlayer-" + mTag);
        }

        public void run() {
            while (true) {
                Command cmd = null;

                synchronized (mCmdQueue) {
                    if (DEBUG) Log.d(mTag, "RemoveFirst");
                    cmd = mCmdQueue.removeFirst();
                }
                try {
                    switch (cmd.code) {
                        case PLAY:
                            if (DEBUG) Log.d(mTag, "PLAY");
                            startSound(cmd);
                            break;
                        case STOP:
                            if (DEBUG) Log.d(mTag, "STOP");
                            stopSound(cmd);
                            break;
                    }
                } finally {
                    synchronized (mCmdQueue) {
                        if (mCmdQueue.size() == 0) {
                            // nothing left to do, quit
@@ -290,6 +307,7 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener
                }
            }
        }
    }

    public void onCompletion(MediaPlayer mp) {
        synchronized(mQueueAudioFocusLock) {