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

Commit cd9213eb authored by Lais Andrade's avatar Lais Andrade
Browse files

Always stop Ringtone instance in AsyncRingtonePlayer

Make sure that any instace of Ringtone created by AsyncRingtonePlayer
will be stopped, which will cause the MediaPlayer to be released, before
replacing it with a new one.

The ringtone created by this class is always a looping one, so if the
ringtone is not stopped it will keep playing on the background.

Bug: 201440544
Fix: 203875109
Test: manual
Change-Id: I716264babe328c817b1a452436a2e1761b27c71d
parent a443d2b2
Loading
Loading
Loading
Loading
+15 −8
Original line number Diff line number Diff line
@@ -172,13 +172,13 @@ public class AsyncRingtonePlayer {
            // Use haptic-only ringtone or do not play anything.
            if (!isRingerAudible || Uri.EMPTY.equals(incomingCall.getRingtone())) {
                if (isVibrationEnabled) {
                    mRingtone = factory.getHapticOnlyRingtone();
                    setRingtone(factory.getHapticOnlyRingtone());
                    if (mRingtone == null) {
                        completeHapticFuture(false /* ringtoneHasHaptics */);
                        return;
                    }
                } else {
                    mRingtone = null;
                    setRingtone(null);
                    completeHapticFuture(false /* ringtoneHasHaptics */);
                    return;
                }
@@ -188,7 +188,7 @@ public class AsyncRingtonePlayer {
            Log.i(this, "handlePlay: Play ringtone.");

            if (mRingtone == null) {
                mRingtone = factory.getRingtone(incomingCall, volumeShaperConfig);
                setRingtone(factory.getRingtone(incomingCall, volumeShaperConfig));
                if (mRingtone == null) {
                    Uri ringtoneUri = incomingCall.getRingtone();
                    String ringtoneUriString = (ringtoneUri == null) ? "null" :
@@ -240,11 +240,7 @@ public class AsyncRingtonePlayer {
        ThreadUtil.checkNotOnMainThread();
        Log.i(this, "Stop ringtone.");

        if (mRingtone != null) {
            Log.d(this, "Ringtone.stop() invoked.");
            mRingtone.stop();
            mRingtone = null;
        }
        setRingtone(null);

        synchronized(this) {
            if (mHandler.hasMessages(EVENT_PLAY)) {
@@ -262,6 +258,17 @@ public class AsyncRingtonePlayer {
        return mRingtone != null;
    }

    private void setRingtone(@Nullable Ringtone ringtone) {
        // Make sure that any previously created instance of Ringtone is stopped so the MediaPlayer
        // can be released, before replacing mRingtone with a new instance. This is always created
        // as a looping Ringtone, so if not stopped it will keep playing on the background.
        if (mRingtone != null) {
            Log.d(this, "Ringtone.stop() invoked.");
            mRingtone.stop();
        }
        mRingtone = ringtone;
    }

    private void completeHapticFuture(boolean ringtoneHasHaptics) {
        if (mHapticsFuture != null) {
            mHapticsFuture.complete(ringtoneHasHaptics);