diff options
author | Nancy Chen <nancychen@google.com> | 2015-05-08 21:50:26 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2015-05-08 21:50:27 +0000 |
commit | 1905d8f34ee317da788244f2dde3e8b75c25c072 (patch) | |
tree | 0dd59ba884308f808ef4b74a20b0bfa89cecf55c /src | |
parent | 730e21c99faa82e6624d8aaca1f2e8dc9cfb8040 (diff) | |
parent | ac5a8e14871c1424eb2e41b9eca939b891574672 (diff) |
Merge "Grab audio focus when listening to voicemail." into mnc-dev
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java index 30fea1aee..6a1041140 100644 --- a/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java +++ b/src/com/android/dialer/voicemail/VoicemailPlaybackPresenter.java @@ -19,6 +19,7 @@ package com.android.dialer.voicemail; import android.content.Context; import android.database.ContentObserver; import android.media.AudioManager; +import android.media.AudioManager.OnAudioFocusChangeListener; import android.media.MediaPlayer; import android.net.Uri; import android.os.AsyncTask; @@ -59,7 +60,7 @@ import javax.annotation.concurrent.ThreadSafe; */ @NotThreadSafe @VisibleForTesting -public class VoicemailPlaybackPresenter { +public class VoicemailPlaybackPresenter implements OnAudioFocusChangeListener { /** The stream used to playback voicemail. */ private static final int PLAYBACK_STREAM = AudioManager.STREAM_VOICE_CALL; @@ -172,6 +173,7 @@ public class VoicemailPlaybackPresenter { private AsyncTask<Void, ?, ?> mPrepareTask; private int mPosition; private boolean mPlaying; + private AudioManager mAudioManager; public VoicemailPlaybackPresenter(PlaybackView view, MediaPlayerProxy player, Uri voicemailUri, ScheduledExecutorService executorService, @@ -482,6 +484,15 @@ public class VoicemailPlaybackPresenter { mPlayer.seekTo(startPosition); mView.setClipPosition(startPosition, duration); try { + // Grab audio focus here + int result = getAudioManager().requestAudioFocus( + VoicemailPlaybackPresenter.this, + PLAYBACK_STREAM, + AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); + + if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) { + throw new RejectedExecutionException("Could not capture audio focus."); + } // Can throw RejectedExecutionException mPlayer.start(); setPositionAndPlayingStatus(mPlayer.getCurrentPosition(), true); @@ -504,6 +515,29 @@ public class VoicemailPlaybackPresenter { } } + private AudioManager getAudioManager() { + if (mAudioManager == null) { + mAudioManager = (AudioManager) + mView.getDataSourceContext().getSystemService(Context.AUDIO_SERVICE); + } + return mAudioManager; + } + + @Override + public void onAudioFocusChange(int focusChange) { + boolean lostFocus = focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT || + focusChange == AudioManager.AUDIOFOCUS_LOSS; + // Note: the below logic is the same as in {@code StartStopButtonListener}. + if (mPlayer.isPlaying() && lostFocus) { + setPositionAndPlayingStatus(mPlayer.getCurrentPosition(), false); + stopPlaybackAtPosition(mPlayer.getCurrentPosition(), mDuration.get()); + } else if (!mPlayer.isPlaying() && focusChange == AudioManager.AUDIOFOCUS_GAIN) { + setPositionAndPlayingStatus(mPosition, true); + postSuccessfullyFetchedContent(); + } + } + + private void resetPrepareStartPlaying(final int clipPositionInMillis) { if (mPrepareTask != null) { mPrepareTask.cancel(false); @@ -525,6 +559,7 @@ public class VoicemailPlaybackPresenter { } private void stopPlaybackAtPosition(int clipPosition, int duration) { + getAudioManager().abandonAudioFocus(this); mPositionUpdater.stopUpdating(); mView.playbackStopped(); if (mWakeLock.isHeld()) { |