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

Commit eed37509 authored by Ahmad Khalil's avatar Ahmad Khalil
Browse files

Fix NotificationPlayer wakelock being held for too long.

We added a try/finally to the run() method, to make sure the wakelock gets released even if there was a crash. We also added a couple of try/catch blocks to log any issues releasing the MediaPlayer and abandoning audio focus.

Fix: 293371948
Test: N/A
Change-Id: Idfa4049b70ceb1bbdb5afce8f1cc8381796d6bbe
parent 8c5aa74b
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) {