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

Commit d8cb4196 authored by Jean-Michel Trivi's avatar Jean-Michel Trivi
Browse files

NotificationPlayer: fix focus not abandoned on playback error

If an exception is fired when trying to start the MediaPlayer
  after audio focus was requested, it will not be abandoned
  on error or completion.

Bug: 70727414
Test: see bug
Change-Id: Ib2a07499829890a858d5a38d46c3575d2d7b6629
parent cb37da8b
Loading
Loading
Loading
Loading
+21 −2
Original line number Diff line number Diff line
@@ -133,12 +133,19 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener
                            + mNotificationRampTimeMs + "ms"); }
                    try {
                        Thread.sleep(mNotificationRampTimeMs);
                        player.start();
                    } catch (InterruptedException e) {
                        Log.e(mTag, "Exception while sleeping to sync notification playback"
                                + " with ducking", e);
                    }
                    try {
                        player.start();
                        if (DEBUG) { Log.d(mTag, "player.start"); }
                    } catch (Exception e) {
                        player.release();
                        player = null;
                        // playing the notification didn't work, revert the focus request
                        abandonAudioFocusAfterError();
                    }
                    if (mPlayer != null) {
                        if (DEBUG) { Log.d(mTag, "mPlayer.release"); }
                        mPlayer.release();
@@ -147,6 +154,8 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener
                }
                catch (Exception e) {
                    Log.w(mTag, "error loading sound for " + mCmd.uri, e);
                    // playing the notification didn't work, revert the focus request
                    abandonAudioFocusAfterError();
                }
                this.notify();
            }
@@ -154,6 +163,16 @@ public class NotificationPlayer implements OnCompletionListener, OnErrorListener
        }
    };

    private void abandonAudioFocusAfterError() {
        synchronized (mQueueAudioFocusLock) {
            if (mAudioManagerWithAudioFocus != null) {
                if (DEBUG) Log.d(mTag, "abandoning focus after playback error");
                mAudioManagerWithAudioFocus.abandonAudioFocus(null);
                mAudioManagerWithAudioFocus = null;
            }
        }
    }

    private void startSound(Command cmd) {
        // Preparing can be slow, so if there is something else
        // is playing, let it continue until we're done, so there