From 926ebb860a8b5cbdec17f07404272be7c484fd79 Mon Sep 17 00:00:00 2001 From: Marco Nelissen Date: Wed, 11 Mar 2015 09:59:49 -0700 Subject: Fix context leak Using an activity context with AudioManager could cause that context to be held on to longer than desired, for example if the caller acquired audio focus but never abandoned it. Fix acquire/abandon in VideoView, and use the application context in AudioManager to mitigate the issue for other misbehaving code. Bug: https://code.google.com/p/android/issues/detail?id=152173 Change-Id: I0fb8390207422c784800dda25b1f2c03d4574bcd --- core/java/android/widget/VideoView.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'core/java/android/widget/VideoView.java') diff --git a/core/java/android/widget/VideoView.java b/core/java/android/widget/VideoView.java index 572cca2..47644f9 100644 --- a/core/java/android/widget/VideoView.java +++ b/core/java/android/widget/VideoView.java @@ -311,6 +311,8 @@ public class VideoView extends SurfaceView mMediaPlayer = null; mCurrentState = STATE_IDLE; mTargetState = STATE_IDLE; + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); + am.abandonAudioFocus(null); } } @@ -319,12 +321,13 @@ public class VideoView extends SurfaceView // not ready for playback just yet, will try again later return; } - AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); - am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); - // we shouldn't clear the target state, because somebody might have // called start() previously release(false); + + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); + am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); + try { mMediaPlayer = new MediaPlayer(); // TODO: create SubtitleController in MediaPlayer, but we need @@ -650,6 +653,8 @@ public class VideoView extends SurfaceView if (cleartargetstate) { mTargetState = STATE_IDLE; } + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); + am.abandonAudioFocus(null); } } -- cgit v1.1