summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/app/MoviePlayer.java
diff options
context:
space:
mode:
authorOwen Lin <owenlin@google.com>2011-09-14 19:49:03 +0800
committerOwen Lin <owenlin@google.com>2011-09-20 11:40:17 +0800
commit540e4698824654232f300992ed8c03b20c9d5946 (patch)
tree050476252eb0de17f474d2f104b7bc3dbadcc9fb /src/com/android/gallery3d/app/MoviePlayer.java
parentacbab5b17f393e57f7466e5459fed41f518e2864 (diff)
downloadandroid_packages_apps_Gallery2-540e4698824654232f300992ed8c03b20c9d5946.tar.gz
android_packages_apps_Gallery2-540e4698824654232f300992ed8c03b20c9d5946.tar.bz2
android_packages_apps_Gallery2-540e4698824654232f300992ed8c03b20c9d5946.zip
Pause the playback of video if the activity is resumed after 3 mins.
fix: 5260232 Also keep the current playback position when the activity is resumed. Originally, we use the bookmark to keep the position. However, the bookmark will be ignored if 1.) The video duration is less than 2 mins, 2.) The video is just started (with 30sec), or 3.) The video is approching the end. I think resume should not limited by the above rules. And we did get some reports on this. (We mark them as WORK AS INTENTDED). Change-Id: Ib9ac359d7ccb6a0278777b0892f0821e16bbb745
Diffstat (limited to 'src/com/android/gallery3d/app/MoviePlayer.java')
-rw-r--r--src/com/android/gallery3d/app/MoviePlayer.java66
1 files changed, 50 insertions, 16 deletions
diff --git a/src/com/android/gallery3d/app/MoviePlayer.java b/src/com/android/gallery3d/app/MoviePlayer.java
index 423994485..ee76fa5ac 100644
--- a/src/com/android/gallery3d/app/MoviePlayer.java
+++ b/src/com/android/gallery3d/app/MoviePlayer.java
@@ -33,6 +33,7 @@ import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
+import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
@@ -49,11 +50,18 @@ public class MoviePlayer implements
@SuppressWarnings("unused")
private static final String TAG = "MoviePlayer";
+ private static final String KEY_VIDEO_POSITION = "video-position";
+ private static final String KEY_RESUMEABLE_TIME = "resumeable-timeout";
+
// Copied from MediaPlaybackService in the Music Player app.
private static final String SERVICECMD = "com.android.music.musicservicecommand";
private static final String CMDNAME = "command";
private static final String CMDPAUSE = "pause";
+ // If we resume the acitivty with in RESUMEABLE_TIMEOUT, we will keep playing.
+ // Otherwise, we pause the player.
+ private static final long RESUMEABLE_TIMEOUT = 3 * 60 * 1000; // 3 mins
+
private Context mContext;
private final VideoView mVideoView;
private final View mProgressView;
@@ -62,10 +70,14 @@ public class MoviePlayer implements
private final Handler mHandler = new Handler();
private final AudioBecomingNoisyReceiver mAudioBecomingNoisyReceiver;
private final ActionBar mActionBar;
+ private final MediaController mMediaController;
- private boolean mHasPaused;
+ private long mResumeableTime = Long.MAX_VALUE;
+ private int mVideoPosition = 0;
+ private boolean mHasPaused = false;
private final Runnable mPlayingChecker = new Runnable() {
+ @Override
public void run() {
if (mVideoView.isPlaying()) {
mProgressView.setVisibility(View.GONE);
@@ -75,7 +87,8 @@ public class MoviePlayer implements
}
};
- public MoviePlayer(View rootView, final MovieActivity movieActivity, Uri videoUri) {
+ public MoviePlayer(View rootView, final MovieActivity movieActivity, Uri videoUri,
+ Bundle savedInstance) {
mContext = movieActivity.getApplicationContext();
mVideoView = (VideoView) rootView.findViewById(R.id.surface_view);
mProgressView = rootView.findViewById(R.id.progress_indicator);
@@ -96,7 +109,7 @@ public class MoviePlayer implements
mVideoView.setOnCompletionListener(this);
mVideoView.setVideoURI(mUri);
- MediaController mediaController = new MediaController(movieActivity) {
+ mMediaController = new MediaController(movieActivity) {
@Override
public void show() {
super.show();
@@ -109,8 +122,8 @@ public class MoviePlayer implements
mActionBar.hide();
}
};
- mVideoView.setMediaController(mediaController);
- mediaController.setOnKeyListener(new View.OnKeyListener() {
+ mMediaController.setOnKeyListener(new View.OnKeyListener() {
+ @Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (event.getAction() == KeyEvent.ACTION_UP) {
@@ -121,6 +134,7 @@ public class MoviePlayer implements
return false;
}
});
+ mVideoView.setMediaController(mMediaController);
mAudioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver();
mAudioBecomingNoisyReceiver.register();
@@ -132,14 +146,27 @@ public class MoviePlayer implements
i.putExtra(CMDNAME, CMDPAUSE);
movieActivity.sendBroadcast(i);
- final Integer bookmark = mBookmarker.getBookmark(mUri);
- if (bookmark != null) {
- showResumeDialog(movieActivity, bookmark);
- } else {
+ if (savedInstance != null) { // this is a resumed activity
+ mVideoPosition = savedInstance.getInt(KEY_VIDEO_POSITION, 0);
+ mResumeableTime = savedInstance.getLong(KEY_RESUMEABLE_TIME, Long.MAX_VALUE);
mVideoView.start();
+ mVideoView.suspend();
+ mHasPaused = true;
+ } else {
+ final Integer bookmark = mBookmarker.getBookmark(mUri);
+ if (bookmark != null) {
+ showResumeDialog(movieActivity, bookmark);
+ } else {
+ mVideoView.start();
+ }
}
}
+ public void onSaveInstanceState(Bundle outState) {
+ outState.putInt(KEY_VIDEO_POSITION, mVideoPosition);
+ outState.putLong(KEY_RESUMEABLE_TIME, mResumeableTime);
+ }
+
private void showResumeDialog(Context context, final int bookmark) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.resume_playing_title);
@@ -147,12 +174,14 @@ public class MoviePlayer implements
context.getString(R.string.resume_playing_message),
GalleryUtils.formatDuration(context, bookmark / 1000)));
builder.setOnCancelListener(new OnCancelListener() {
+ @Override
public void onCancel(DialogInterface dialog) {
onCompletion();
}
});
builder.setPositiveButton(
R.string.resume_playing_resume, new OnClickListener() {
+ @Override
public void onClick(DialogInterface dialog, int which) {
mVideoView.seekTo(bookmark);
mVideoView.start();
@@ -160,6 +189,7 @@ public class MoviePlayer implements
});
builder.setNegativeButton(
R.string.resume_playing_restart, new OnClickListener() {
+ @Override
public void onClick(DialogInterface dialog, int which) {
mVideoView.start();
}
@@ -168,21 +198,25 @@ public class MoviePlayer implements
}
public void onPause() {
+ mHasPaused = true;
mHandler.removeCallbacksAndMessages(null);
- mBookmarker.setBookmark(mUri, mVideoView.getCurrentPosition(),
- mVideoView.getDuration());
+ mVideoPosition = mVideoView.getCurrentPosition();
+ mBookmarker.setBookmark(mUri, mVideoPosition, mVideoView.getDuration());
mVideoView.suspend();
- mHasPaused = true;
+ mResumeableTime = System.currentTimeMillis() + RESUMEABLE_TIMEOUT;
}
public void onResume() {
if (mHasPaused) {
- Integer bookmark = mBookmarker.getBookmark(mUri);
- if (bookmark != null) {
- mVideoView.seekTo(bookmark);
+ mVideoView.seekTo(mVideoPosition);
+ mVideoView.resume();
+
+ // If we have slept for too long, pause the play
+ if (System.currentTimeMillis() > mResumeableTime) {
+ mMediaController.show();
+ mVideoView.pause();
}
}
- mVideoView.resume();
}
public void onDestroy() {