diff options
| author | Likai Ding <likaid@codeaurora.org> | 2016-04-27 13:06:56 +0800 |
|---|---|---|
| committer | Likai Ding <likaid@codeaurora.org> | 2016-04-27 13:06:56 +0800 |
| commit | 37aab725ac2a52fee49c386fc61dcbb53b142ad0 (patch) | |
| tree | 226fcf8e9842e02fd700d7158f577637a4e1b7a9 /src | |
| parent | 9166741f9eb6d7b337e2257b701fcf5556c88231 (diff) | |
| download | android_packages_apps_Gallery2-37aab725ac2a52fee49c386fc61dcbb53b142ad0.tar.gz android_packages_apps_Gallery2-37aab725ac2a52fee49c386fc61dcbb53b142ad0.tar.bz2 android_packages_apps_Gallery2-37aab725ac2a52fee49c386fc61dcbb53b142ad0.zip | |
Gallery: fix a memory leak
The requestAudioFocus() call does not provide the listener previously
registered, so it remains in AudioManager and eventually leaks
MovieActivity.
The regression is introduced by
Id8e3e4b0afbc72892301a08fb8a122c285fbfe97.
Change-Id: I032cfb9df1973420a51476bba139294badee1366
CRs-Fixed: 999702
Diffstat (limited to 'src')
| -rw-r--r-- | src/com/android/gallery3d/app/MovieActivity.java | 9 | ||||
| -rw-r--r--[-rwxr-xr-x] | src/com/android/gallery3d/app/MoviePlayer.java | 82 | ||||
| -rw-r--r-- | src/org/codeaurora/gallery3d/video/CodeauroraVideoView.java | 37 |
3 files changed, 51 insertions, 77 deletions
diff --git a/src/com/android/gallery3d/app/MovieActivity.java b/src/com/android/gallery3d/app/MovieActivity.java index 8767248ff..cc6c72966 100644 --- a/src/com/android/gallery3d/app/MovieActivity.java +++ b/src/com/android/gallery3d/app/MovieActivity.java @@ -529,7 +529,7 @@ public class MovieActivity extends Activity { public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { - // If click back up button, we will always finish current activity and + // If click back up button, we will always finish current activity and // back to previous one. finish(); return true; @@ -550,9 +550,7 @@ public class MovieActivity extends Activity { @Override public void onStart() { - ((AudioManager) getSystemService(AUDIO_SERVICE)) - .requestAudioFocus(null, AudioManager.STREAM_MUSIC, - AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); + mPlayer.requestAudioFocus(); super.onStart(); mMovieHooker.onStart(); registerScreenReceiver(); @@ -560,8 +558,7 @@ public class MovieActivity extends Activity { @Override protected void onStop() { - ((AudioManager) getSystemService(AUDIO_SERVICE)) - .abandonAudioFocus(null); + mPlayer.abandonAudioFocus(); super.onStop(); if (mControlResumed && mPlayer != null) { mPlayer.onStop(); diff --git a/src/com/android/gallery3d/app/MoviePlayer.java b/src/com/android/gallery3d/app/MoviePlayer.java index f97df7d19..720c50428 100755..100644 --- a/src/com/android/gallery3d/app/MoviePlayer.java +++ b/src/com/android/gallery3d/app/MoviePlayer.java @@ -32,7 +32,6 @@ import android.graphics.Color; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.Metadata; -import android.media.audiofx.AudioEffect; import android.media.audiofx.Virtualizer; import android.net.Uri; import android.os.Build; @@ -43,7 +42,6 @@ import android.view.MotionEvent; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; -import android.widget.VideoView; import android.widget.Toast; import com.android.gallery3d.R; @@ -51,17 +49,17 @@ import com.android.gallery3d.common.ApiHelper; import com.android.gallery3d.common.BlobCache; import com.android.gallery3d.util.CacheManager; import com.android.gallery3d.util.GalleryUtils; + import org.codeaurora.gallery3d.ext.IContrllerOverlayExt; -import org.codeaurora.gallery3d.ext.IMoviePlayer; import org.codeaurora.gallery3d.ext.IMovieItem; +import org.codeaurora.gallery3d.ext.IMoviePlayer; import org.codeaurora.gallery3d.ext.MovieUtils; import org.codeaurora.gallery3d.video.BookmarkEnhance; -import org.codeaurora.gallery3d.video.ExtensionHelper; +import org.codeaurora.gallery3d.video.CodeauroraVideoView; import org.codeaurora.gallery3d.video.IControllerRewindAndForward; import org.codeaurora.gallery3d.video.IControllerRewindAndForward.IRewindAndForwardListener; import org.codeaurora.gallery3d.video.ScreenModeManager; import org.codeaurora.gallery3d.video.ScreenModeManager.ScreenModeListener; -import org.codeaurora.gallery3d.video.CodeauroraVideoView; import org.codeaurora.gallery3d.video.VideoSnapshotExt; import java.io.ByteArrayInputStream; @@ -225,40 +223,6 @@ public class MoviePlayer implements } }; - private CodeauroraVideoView.AudioFocusChangeListener mAudioFocusListener = - new CodeauroraVideoView.AudioFocusChangeListener() { - @Override - public void onAudioFocusRequestFailed() { - Log.d(TAG, "pause video because onAudioFocusRequestFailed."); - onPauseVideo(); - } - - @Override - public void onAudioFocusChange(int focusChange) { - switch (focusChange) { - case AudioManager.AUDIOFOCUS_GAIN: - boolean isTargetPlaying = (mVideoView != null && - mVideoView.isTargetPlaying()); - Log.d(TAG, "AUDIOFOCUS_GAIN isTargetPlaying : " + isTargetPlaying); - if (mPausedByTransientLossOfFocus || isTargetPlaying) { - onPlayVideo(); - mPausedByTransientLossOfFocus = false; - } - break; - case AudioManager.AUDIOFOCUS_LOSS: - case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: - boolean isPlaying = (mVideoView != null && mVideoView.isPlaying()); - Log.d(TAG, "AUDIOFOCUS_LOSS isPlaying : " + isPlaying); - if (isPlaying) { - onPauseVideo(); - mPausedByTransientLossOfFocus = true; - } - break; - default: - } - } - }; - public MoviePlayer(View rootView, final MovieActivity movieActivity, IMovieItem info, Bundle savedInstance, boolean canReplay) { mContext = movieActivity.getApplicationContext(); @@ -280,7 +244,6 @@ public class MoviePlayer implements mVideoView.setOnErrorListener(this); mVideoView.setOnCompletionListener(this); - mVideoView.setOnAudioFocusChangeListener(mAudioFocusListener); if (mVirtualizer != null) { mVirtualizer.release(); @@ -1651,6 +1614,45 @@ public class MoviePlayer implements mController.cancelHiding(); } } + + private AudioManager.OnAudioFocusChangeListener mAudioFocusChangeListener = + new AudioManager.OnAudioFocusChangeListener() { + @Override + public void onAudioFocusChange(int focusChange) { + switch (focusChange) { + case AudioManager.AUDIOFOCUS_GAIN: + boolean isTargetPlaying = (mVideoView != null && + mVideoView.isTargetPlaying()); + Log.d(TAG, "AUDIOFOCUS_GAIN isTargetPlaying : " + isTargetPlaying); + if (mPausedByTransientLossOfFocus || isTargetPlaying) { + onPlayVideo(); + mPausedByTransientLossOfFocus = false; + } + break; + case AudioManager.AUDIOFOCUS_LOSS: + case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: + boolean isPlaying = (mVideoView != null && mVideoView.isPlaying()); + Log.d(TAG, "AUDIOFOCUS_LOSS isPlaying : " + isPlaying); + if (isPlaying) { + onPauseVideo(); + mPausedByTransientLossOfFocus = true; + } + break; + default: + } + } + }; + + public int requestAudioFocus() { + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); + return am.requestAudioFocus(mAudioFocusChangeListener, AudioManager.STREAM_MUSIC, + AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); + } + + public int abandonAudioFocus() { + AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); + return am.abandonAudioFocus(mAudioFocusChangeListener); + } } class Bookmarker { diff --git a/src/org/codeaurora/gallery3d/video/CodeauroraVideoView.java b/src/org/codeaurora/gallery3d/video/CodeauroraVideoView.java index b3c5622e2..d787d8daf 100644 --- a/src/org/codeaurora/gallery3d/video/CodeauroraVideoView.java +++ b/src/org/codeaurora/gallery3d/video/CodeauroraVideoView.java @@ -98,11 +98,6 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr private boolean mIsShowDialog = false; private boolean mErrorDialogShowing = false; private KeyguardManager mKeyguardManager; - private AudioFocusChangeListener mAudioFocusListener; - - public interface AudioFocusChangeListener extends AudioManager.OnAudioFocusChangeListener { - public void onAudioFocusRequestFailed(); - } private final Handler mHandler = new Handler() { public void handleMessage(final Message msg) { @@ -356,14 +351,14 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr getHolder().removeCallback(mSHCallback); mSHCallback = new SurfaceHolder.Callback() { - public void surfaceChanged(final SurfaceHolder holder, final int format, + public void surfaceChanged(final SurfaceHolder holder, final int format, final int w, final int h) { if (LOG) { Log.v(TAG, "surfaceChanged(" + holder + ", " + format + ", " + w + ", " + h + ")"); Log.v(TAG, "surfaceChanged() mMediaPlayer=" + mMediaPlayer + ", mTargetState=" + mTargetState - + ", mVideoWidth=" + mVideoWidth + + ", mVideoWidth=" + mVideoWidth + ", mVideoHeight=" + mVideoHeight); } mSurfaceWidth = w; @@ -454,8 +449,6 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr mMediaPlayer = null; mCurrentState = STATE_IDLE; mTargetState = STATE_IDLE; - AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); - am.abandonAudioFocus(null); } } @@ -474,8 +467,6 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); return; } - AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); - am.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); try { mMediaPlayer = new MediaPlayer(); @@ -626,10 +617,6 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr mOnInfoListener = l; } - public void setOnAudioFocusChangeListener(AudioFocusChangeListener l) { - mAudioFocusListener = l; - } - SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback() { public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { @@ -695,8 +682,6 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr if (cleartargetstate) { mTargetState = STATE_IDLE; } - AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); - am.abandonAudioFocus(null); } } @@ -782,18 +767,8 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr public void start() { if (mIsShowDialog) return; if (isInPlaybackState()) { - AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); - int result = am.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, - AudioManager.AUDIOFOCUS_GAIN); - if (result != AudioManager.AUDIOFOCUS_REQUEST_FAILED) { - mMediaPlayer.start(); - mCurrentState = STATE_PLAYING; - } else { - Log.w(TAG, "requestAudioFocus failed."); - if (mAudioFocusListener != null) { - mAudioFocusListener.onAudioFocusRequestFailed(); - } - } + mMediaPlayer.start(); + mCurrentState = STATE_PLAYING; } mTargetState = STATE_PLAYING; } @@ -826,7 +801,7 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr } public void resume() { - // HTTP streaming (with suspended status) will call mMediaPlayer->resume(), + // HTTP streaming (with suspended status) will call mMediaPlayer->resume(), // others will call openVideo() /* if (mCurrentState == STATE_SUSPENDED) { @@ -846,7 +821,7 @@ public class CodeauroraVideoView extends SurfaceView implements MediaPlayerContr release(false); } } else { - // The surface has been destroyed, resume operation will be done + // The surface has been destroyed, resume operation will be done // after surface created return; } |
