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

Commit 2c2e5453 authored by Tyler Gunn's avatar Tyler Gunn
Browse files

Ensure null media player does not crash Telecom.

If the media player could not be initialized, telecom can throw an NPE.
Defensively catching this prevents a crash.
In the bug there appears to be some issues with the audio subsystem.
However, we shouldn't crash the device just because the end call tone
could not play.

Test: Manual.
Bug: 122144736
Change-Id: Ifbfa72b0e4d103201a945d2776f5ca912a04fa1c
parent 2d087215
Loading
Loading
Loading
Loading
+27 −6
Original line number Diff line number Diff line
@@ -16,10 +16,13 @@

package com.android.server.telecom;

import android.annotation.Nullable;
import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.ToneGenerator;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.telecom.Log;
@@ -87,34 +90,52 @@ public class InCallTonePlayer extends Thread {
    public static class MediaPlayerAdapterImpl implements MediaPlayerAdapter {
        private MediaPlayer mMediaPlayer;

        public MediaPlayerAdapterImpl(MediaPlayer mediaPlayer) {
        /**
         * Create new media player adapter backed by a real mediaplayer.
         * Note: Its possible for the mediaplayer to be null if
         * {@link MediaPlayer#create(Context, Uri)} fails for some reason; in this case we can
         * continue but not bother playing the audio.
         * @param mediaPlayer The media player.
         */
        public MediaPlayerAdapterImpl(@Nullable MediaPlayer mediaPlayer) {
            mMediaPlayer = mediaPlayer;
        }

        @Override
        public void setLooping(boolean isLooping) {
            if (mMediaPlayer != null) {
                mMediaPlayer.setLooping(isLooping);
            }
        }

        @Override
        public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
            if (mMediaPlayer != null) {
                mMediaPlayer.setOnCompletionListener(listener);
            }
        }

        @Override
        public void start() {
            if (mMediaPlayer != null) {
                mMediaPlayer.start();
            }
        }

        @Override
        public void release() {
            if (mMediaPlayer != null) {
                mMediaPlayer.release();
            }
        }

        @Override
        public int getDuration() {
            if (mMediaPlayer != null) {
                return mMediaPlayer.getDuration();
            }
            return 0;
        }
    }

    public interface MediaPlayerFactory {