Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.gallery3d.app; |
| 18 | |
Owen Lin | 7a5e1e7 | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 19 | import android.annotation.TargetApi; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 20 | import android.app.AlertDialog; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.DialogInterface; |
| 24 | import android.content.DialogInterface.OnCancelListener; |
| 25 | import android.content.DialogInterface.OnClickListener; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 28 | import android.graphics.Color; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 29 | import android.media.AudioManager; |
| 30 | import android.media.MediaPlayer; |
| 31 | import android.net.Uri; |
Owen Lin | df4763c | 2012-06-28 17:12:20 +0800 | [diff] [blame] | 32 | import android.os.Build; |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 33 | import android.os.Bundle; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 34 | import android.os.Handler; |
Chih-Chung Chang | fc8c503 | 2011-11-29 14:21:11 +0800 | [diff] [blame] | 35 | import android.view.KeyEvent; |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 36 | import android.view.MotionEvent; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 37 | import android.view.View; |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 38 | import android.view.ViewGroup; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 39 | import android.widget.VideoView; |
| 40 | |
Owen Lin | aea077a | 2011-11-11 12:01:09 +0800 | [diff] [blame] | 41 | import com.android.gallery3d.R; |
Owen Lin | 7a5e1e7 | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 42 | import com.android.gallery3d.common.ApiHelper; |
Owen Lin | aea077a | 2011-11-11 12:01:09 +0800 | [diff] [blame] | 43 | import com.android.gallery3d.common.BlobCache; |
| 44 | import com.android.gallery3d.util.CacheManager; |
| 45 | import com.android.gallery3d.util.GalleryUtils; |
| 46 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 47 | import java.io.ByteArrayInputStream; |
| 48 | import java.io.ByteArrayOutputStream; |
| 49 | import java.io.DataInputStream; |
| 50 | import java.io.DataOutputStream; |
| 51 | |
| 52 | public class MoviePlayer implements |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 53 | MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener, |
| 54 | ControllerOverlay.Listener { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 55 | @SuppressWarnings("unused") |
| 56 | private static final String TAG = "MoviePlayer"; |
| 57 | |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 58 | private static final String KEY_VIDEO_POSITION = "video-position"; |
| 59 | private static final String KEY_RESUMEABLE_TIME = "resumeable-timeout"; |
| 60 | |
Chih-Chung Chang | 6772173 | 2012-07-03 12:49:45 +0800 | [diff] [blame^] | 61 | // These are constants in KeyEvent, appearing on API level 11. |
| 62 | private static final int KEYCODE_MEDIA_PLAY = 126; |
| 63 | private static final int KEYCODE_MEDIA_PAUSE = 127; |
| 64 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 65 | // Copied from MediaPlaybackService in the Music Player app. |
| 66 | private static final String SERVICECMD = "com.android.music.musicservicecommand"; |
| 67 | private static final String CMDNAME = "command"; |
| 68 | private static final String CMDPAUSE = "pause"; |
| 69 | |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 70 | private static final long BLACK_TIMEOUT = 500; |
| 71 | |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 72 | // If we resume the acitivty with in RESUMEABLE_TIMEOUT, we will keep playing. |
| 73 | // Otherwise, we pause the player. |
| 74 | private static final long RESUMEABLE_TIMEOUT = 3 * 60 * 1000; // 3 mins |
| 75 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 76 | private Context mContext; |
| 77 | private final VideoView mVideoView; |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 78 | private final View mRootView; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 79 | private final Bookmarker mBookmarker; |
| 80 | private final Uri mUri; |
| 81 | private final Handler mHandler = new Handler(); |
| 82 | private final AudioBecomingNoisyReceiver mAudioBecomingNoisyReceiver; |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 83 | private final MovieControllerOverlay mController; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 84 | |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 85 | private long mResumeableTime = Long.MAX_VALUE; |
| 86 | private int mVideoPosition = 0; |
| 87 | private boolean mHasPaused = false; |
Ray Chen | 24525c2 | 2012-05-22 17:34:29 +0800 | [diff] [blame] | 88 | private int mLastSystemUiVis = 0; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 89 | |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 90 | // If the time bar is being dragged. |
| 91 | private boolean mDragging; |
| 92 | |
| 93 | // If the time bar is visible. |
| 94 | private boolean mShowing; |
| 95 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 96 | private final Runnable mPlayingChecker = new Runnable() { |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 97 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 98 | public void run() { |
| 99 | if (mVideoView.isPlaying()) { |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 100 | mController.showPlaying(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 101 | } else { |
| 102 | mHandler.postDelayed(mPlayingChecker, 250); |
| 103 | } |
| 104 | } |
| 105 | }; |
| 106 | |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 107 | private final Runnable mRemoveBackground = new Runnable() { |
Wu-cheng Li | 22a3633 | 2012-06-21 16:45:55 +0800 | [diff] [blame] | 108 | @SuppressWarnings("deprecation") |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 109 | @Override |
| 110 | public void run() { |
Wu-cheng Li | 22a3633 | 2012-06-21 16:45:55 +0800 | [diff] [blame] | 111 | mRootView.setBackgroundDrawable(null); |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 112 | } |
| 113 | }; |
| 114 | |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 115 | private final Runnable mProgressChecker = new Runnable() { |
| 116 | @Override |
| 117 | public void run() { |
| 118 | int pos = setProgress(); |
| 119 | mHandler.postDelayed(mProgressChecker, 1000 - (pos % 1000)); |
| 120 | } |
| 121 | }; |
| 122 | |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 123 | public MoviePlayer(View rootView, final MovieActivity movieActivity, |
| 124 | Uri videoUri, Bundle savedInstance, boolean canReplay) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 125 | mContext = movieActivity.getApplicationContext(); |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 126 | mRootView = rootView; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 127 | mVideoView = (VideoView) rootView.findViewById(R.id.surface_view); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 128 | mBookmarker = new Bookmarker(movieActivity); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 129 | mUri = videoUri; |
| 130 | |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 131 | mController = new MovieControllerOverlay(mContext); |
| 132 | ((ViewGroup)rootView).addView(mController.getView()); |
| 133 | mController.setListener(this); |
| 134 | mController.setCanReplay(canReplay); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 135 | |
| 136 | mVideoView.setOnErrorListener(this); |
| 137 | mVideoView.setOnCompletionListener(this); |
| 138 | mVideoView.setVideoURI(mUri); |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 139 | mVideoView.setOnTouchListener(new View.OnTouchListener() { |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 140 | @Override |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 141 | public boolean onTouch(View v, MotionEvent event) { |
| 142 | mController.show(); |
| 143 | return true; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 144 | } |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 145 | }); |
Chih-Chung Chang | 8176012 | 2011-09-27 16:50:16 +0800 | [diff] [blame] | 146 | |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 147 | // The SurfaceView is transparent before drawing the first frame. |
| 148 | // This makes the UI flashing when open a video. (black -> old screen |
| 149 | // -> video) However, we have no way to know the timing of the first |
| 150 | // frame. So, we hide the VideoView for a while to make sure the |
| 151 | // video has been drawn on it. |
| 152 | mVideoView.postDelayed(new Runnable() { |
| 153 | @Override |
| 154 | public void run() { |
| 155 | mVideoView.setVisibility(View.VISIBLE); |
| 156 | } |
| 157 | }, BLACK_TIMEOUT); |
| 158 | |
Owen Lin | 7a5e1e7 | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 159 | setOnSystemUiVisibilityChangeListener(); |
| 160 | // Hide system UI by default |
| 161 | showSystemUi(false); |
| 162 | |
| 163 | mAudioBecomingNoisyReceiver = new AudioBecomingNoisyReceiver(); |
| 164 | mAudioBecomingNoisyReceiver.register(); |
| 165 | |
| 166 | Intent i = new Intent(SERVICECMD); |
| 167 | i.putExtra(CMDNAME, CMDPAUSE); |
| 168 | movieActivity.sendBroadcast(i); |
| 169 | |
| 170 | if (savedInstance != null) { // this is a resumed activity |
| 171 | mVideoPosition = savedInstance.getInt(KEY_VIDEO_POSITION, 0); |
| 172 | mResumeableTime = savedInstance.getLong(KEY_RESUMEABLE_TIME, Long.MAX_VALUE); |
| 173 | mVideoView.start(); |
| 174 | mVideoView.suspend(); |
| 175 | mHasPaused = true; |
| 176 | } else { |
| 177 | final Integer bookmark = mBookmarker.getBookmark(mUri); |
| 178 | if (bookmark != null) { |
| 179 | showResumeDialog(movieActivity, bookmark); |
| 180 | } else { |
| 181 | startVideo(); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Owen Lin | df4763c | 2012-06-28 17:12:20 +0800 | [diff] [blame] | 186 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
Owen Lin | 7a5e1e7 | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 187 | private void setOnSystemUiVisibilityChangeListener() { |
| 188 | if (!ApiHelper.HAS_VIEW_SYSTEM_UI_FLAG_HIDE_NAVIGATION) return; |
| 189 | |
Chih-Chung Chang | 8176012 | 2011-09-27 16:50:16 +0800 | [diff] [blame] | 190 | // When the user touches the screen or uses some hard key, the framework |
| 191 | // will change system ui visibility from invisible to visible. We show |
Ray Chen | 392a292 | 2012-05-14 17:19:56 +0800 | [diff] [blame] | 192 | // the media control and enable system UI (e.g. ActionBar) to be visible at this point |
Chih-Chung Chang | 8176012 | 2011-09-27 16:50:16 +0800 | [diff] [blame] | 193 | mVideoView.setOnSystemUiVisibilityChangeListener( |
| 194 | new View.OnSystemUiVisibilityChangeListener() { |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 195 | @Override |
Chih-Chung Chang | 8176012 | 2011-09-27 16:50:16 +0800 | [diff] [blame] | 196 | public void onSystemUiVisibilityChange(int visibility) { |
Ray Chen | 24525c2 | 2012-05-22 17:34:29 +0800 | [diff] [blame] | 197 | int diff = mLastSystemUiVis ^ visibility; |
| 198 | mLastSystemUiVis = visibility; |
| 199 | if ((diff & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) != 0 |
| 200 | && (visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) { |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 201 | mController.show(); |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 202 | |
| 203 | // We need to set the background to clear ghosting images |
| 204 | // when ActionBar slides in. However, if we keep the background, |
| 205 | // there will be one additional layer in HW composer, which is bad |
| 206 | // to battery. As a solution, we remove the background when we |
| 207 | // hide the action bar |
| 208 | mHandler.removeCallbacks(mRemoveBackground); |
| 209 | mRootView.setBackgroundColor(Color.BLACK); |
| 210 | } else { |
| 211 | mHandler.removeCallbacks(mRemoveBackground); |
| 212 | |
| 213 | // Wait for the slide out animation, one second should be enough |
| 214 | mHandler.postDelayed(mRemoveBackground, 1000); |
Chih-Chung Chang | 8176012 | 2011-09-27 16:50:16 +0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | }); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Owen Lin | df4763c | 2012-06-28 17:12:20 +0800 | [diff] [blame] | 220 | @TargetApi(Build.VERSION_CODES.JELLY_BEAN) |
Chih-Chung Chang | 8176012 | 2011-09-27 16:50:16 +0800 | [diff] [blame] | 221 | private void showSystemUi(boolean visible) { |
Owen Lin | 7a5e1e7 | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 222 | if (!ApiHelper.HAS_VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE) return; |
| 223 | |
Ray Chen | 24525c2 | 2012-05-22 17:34:29 +0800 | [diff] [blame] | 224 | int flag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
| 225 | | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
Owen Lin | 7a5e1e7 | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 226 | | View.SYSTEM_UI_FLAG_LAYOUT_STABLE; |
Ray Chen | 24525c2 | 2012-05-22 17:34:29 +0800 | [diff] [blame] | 227 | if (!visible) { |
The Android Open Source Project | ca7d9bf | 2012-06-29 08:19:39 -0700 | [diff] [blame] | 228 | flag |= View.STATUS_BAR_HIDDEN | View.SYSTEM_UI_FLAG_FULLSCREEN |
Ray Chen | 24525c2 | 2012-05-22 17:34:29 +0800 | [diff] [blame] | 229 | | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; |
Ray Chen | 392a292 | 2012-05-14 17:19:56 +0800 | [diff] [blame] | 230 | } |
Ray Chen | 24525c2 | 2012-05-22 17:34:29 +0800 | [diff] [blame] | 231 | mVideoView.setSystemUiVisibility(flag); |
Chih-Chung Chang | 8176012 | 2011-09-27 16:50:16 +0800 | [diff] [blame] | 232 | } |
| 233 | |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 234 | public void onSaveInstanceState(Bundle outState) { |
| 235 | outState.putInt(KEY_VIDEO_POSITION, mVideoPosition); |
| 236 | outState.putLong(KEY_RESUMEABLE_TIME, mResumeableTime); |
| 237 | } |
| 238 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 239 | private void showResumeDialog(Context context, final int bookmark) { |
| 240 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 241 | builder.setTitle(R.string.resume_playing_title); |
| 242 | builder.setMessage(String.format( |
| 243 | context.getString(R.string.resume_playing_message), |
| 244 | GalleryUtils.formatDuration(context, bookmark / 1000))); |
| 245 | builder.setOnCancelListener(new OnCancelListener() { |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 246 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 247 | public void onCancel(DialogInterface dialog) { |
| 248 | onCompletion(); |
| 249 | } |
| 250 | }); |
| 251 | builder.setPositiveButton( |
| 252 | R.string.resume_playing_resume, new OnClickListener() { |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 253 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 254 | public void onClick(DialogInterface dialog, int which) { |
| 255 | mVideoView.seekTo(bookmark); |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 256 | startVideo(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 257 | } |
| 258 | }); |
| 259 | builder.setNegativeButton( |
| 260 | R.string.resume_playing_restart, new OnClickListener() { |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 261 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 262 | public void onClick(DialogInterface dialog, int which) { |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 263 | startVideo(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 264 | } |
| 265 | }); |
| 266 | builder.show(); |
| 267 | } |
| 268 | |
| 269 | public void onPause() { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 270 | mHasPaused = true; |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 271 | mHandler.removeCallbacksAndMessages(null); |
| 272 | mVideoPosition = mVideoView.getCurrentPosition(); |
| 273 | mBookmarker.setBookmark(mUri, mVideoPosition, mVideoView.getDuration()); |
| 274 | mVideoView.suspend(); |
| 275 | mResumeableTime = System.currentTimeMillis() + RESUMEABLE_TIMEOUT; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | public void onResume() { |
| 279 | if (mHasPaused) { |
Owen Lin | 540e469 | 2011-09-14 19:49:03 +0800 | [diff] [blame] | 280 | mVideoView.seekTo(mVideoPosition); |
| 281 | mVideoView.resume(); |
| 282 | |
| 283 | // If we have slept for too long, pause the play |
| 284 | if (System.currentTimeMillis() > mResumeableTime) { |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 285 | pauseVideo(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 286 | } |
| 287 | } |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 288 | mHandler.post(mProgressChecker); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | public void onDestroy() { |
| 292 | mVideoView.stopPlayback(); |
| 293 | mAudioBecomingNoisyReceiver.unregister(); |
| 294 | } |
| 295 | |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 296 | // This updates the time bar display (if necessary). It is called every |
| 297 | // second by mProgressChecker and also from places where the time bar needs |
| 298 | // to be updated immediately. |
| 299 | private int setProgress() { |
| 300 | if (mDragging || !mShowing) { |
| 301 | return 0; |
| 302 | } |
| 303 | int position = mVideoView.getCurrentPosition(); |
| 304 | int duration = mVideoView.getDuration(); |
| 305 | mController.setTimes(position, duration); |
| 306 | return position; |
| 307 | } |
| 308 | |
| 309 | private void startVideo() { |
| 310 | // For streams that we expect to be slow to start up, show a |
| 311 | // progress spinner until playback starts. |
| 312 | String scheme = mUri.getScheme(); |
| 313 | if ("http".equalsIgnoreCase(scheme) || "rtsp".equalsIgnoreCase(scheme)) { |
| 314 | mController.showLoading(); |
| 315 | mHandler.removeCallbacks(mPlayingChecker); |
| 316 | mHandler.postDelayed(mPlayingChecker, 250); |
| 317 | } else { |
| 318 | mController.showPlaying(); |
Owen Lin | 00bb8e4 | 2012-05-23 15:47:36 -0700 | [diff] [blame] | 319 | mController.hide(); |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | mVideoView.start(); |
| 323 | setProgress(); |
| 324 | } |
| 325 | |
| 326 | private void playVideo() { |
| 327 | mVideoView.start(); |
| 328 | mController.showPlaying(); |
| 329 | setProgress(); |
| 330 | } |
| 331 | |
| 332 | private void pauseVideo() { |
| 333 | mVideoView.pause(); |
| 334 | mController.showPaused(); |
| 335 | } |
| 336 | |
| 337 | // Below are notifications from VideoView |
| 338 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 339 | public boolean onError(MediaPlayer player, int arg1, int arg2) { |
| 340 | mHandler.removeCallbacksAndMessages(null); |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 341 | // VideoView will show an error dialog if we return false, so no need |
| 342 | // to show more message. |
| 343 | mController.showErrorMessage(""); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 344 | return false; |
| 345 | } |
| 346 | |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 347 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 348 | public void onCompletion(MediaPlayer mp) { |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 349 | mController.showEnded(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 350 | onCompletion(); |
| 351 | } |
| 352 | |
| 353 | public void onCompletion() { |
| 354 | } |
| 355 | |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 356 | // Below are notifications from ControllerOverlay |
| 357 | @Override |
| 358 | public void onPlayPause() { |
| 359 | if (mVideoView.isPlaying()) { |
| 360 | pauseVideo(); |
| 361 | } else { |
| 362 | playVideo(); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | @Override |
| 367 | public void onSeekStart() { |
| 368 | mDragging = true; |
| 369 | } |
| 370 | |
| 371 | @Override |
| 372 | public void onSeekMove(int time) { |
| 373 | mVideoView.seekTo(time); |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public void onSeekEnd(int time) { |
| 378 | mDragging = false; |
| 379 | mVideoView.seekTo(time); |
| 380 | setProgress(); |
| 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public void onShown() { |
| 385 | mShowing = true; |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 386 | setProgress(); |
Owen Lin | 0b4e909 | 2012-06-19 14:35:06 +0800 | [diff] [blame] | 387 | showSystemUi(true); |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public void onHidden() { |
| 392 | mShowing = false; |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 393 | showSystemUi(false); |
| 394 | } |
| 395 | |
| 396 | @Override |
| 397 | public void onReplay() { |
| 398 | startVideo(); |
| 399 | } |
| 400 | |
Chih-Chung Chang | fc8c503 | 2011-11-29 14:21:11 +0800 | [diff] [blame] | 401 | // Below are key events passed from MovieActivity. |
| 402 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 403 | |
| 404 | // Some headsets will fire off 7-10 events on a single click |
| 405 | if (event.getRepeatCount() > 0) { |
| 406 | return isMediaKey(keyCode); |
| 407 | } |
| 408 | |
| 409 | switch (keyCode) { |
| 410 | case KeyEvent.KEYCODE_HEADSETHOOK: |
| 411 | case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: |
| 412 | if (mVideoView.isPlaying()) { |
| 413 | pauseVideo(); |
| 414 | } else { |
| 415 | playVideo(); |
| 416 | } |
| 417 | return true; |
Chih-Chung Chang | 6772173 | 2012-07-03 12:49:45 +0800 | [diff] [blame^] | 418 | case KEYCODE_MEDIA_PAUSE: |
Chih-Chung Chang | fc8c503 | 2011-11-29 14:21:11 +0800 | [diff] [blame] | 419 | if (mVideoView.isPlaying()) { |
| 420 | pauseVideo(); |
| 421 | } |
| 422 | return true; |
Chih-Chung Chang | 6772173 | 2012-07-03 12:49:45 +0800 | [diff] [blame^] | 423 | case KEYCODE_MEDIA_PLAY: |
Chih-Chung Chang | fc8c503 | 2011-11-29 14:21:11 +0800 | [diff] [blame] | 424 | if (!mVideoView.isPlaying()) { |
| 425 | playVideo(); |
| 426 | } |
| 427 | return true; |
| 428 | case KeyEvent.KEYCODE_MEDIA_PREVIOUS: |
| 429 | case KeyEvent.KEYCODE_MEDIA_NEXT: |
| 430 | // TODO: Handle next / previous accordingly, for now we're |
| 431 | // just consuming the events. |
| 432 | return true; |
| 433 | } |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 438 | return isMediaKey(keyCode); |
| 439 | } |
| 440 | |
| 441 | private static boolean isMediaKey(int keyCode) { |
| 442 | return keyCode == KeyEvent.KEYCODE_HEADSETHOOK |
| 443 | || keyCode == KeyEvent.KEYCODE_MEDIA_PREVIOUS |
| 444 | || keyCode == KeyEvent.KEYCODE_MEDIA_NEXT |
| 445 | || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE |
| 446 | || keyCode == KeyEvent.KEYCODE_MEDIA_PLAY |
| 447 | || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE; |
| 448 | } |
| 449 | |
Chih-Chung Chang | 209a916 | 2011-10-14 16:09:09 +0800 | [diff] [blame] | 450 | // We want to pause when the headset is unplugged. |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 451 | private class AudioBecomingNoisyReceiver extends BroadcastReceiver { |
| 452 | |
| 453 | public void register() { |
| 454 | mContext.registerReceiver(this, |
| 455 | new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY)); |
| 456 | } |
| 457 | |
| 458 | public void unregister() { |
| 459 | mContext.unregisterReceiver(this); |
| 460 | } |
| 461 | |
| 462 | @Override |
| 463 | public void onReceive(Context context, Intent intent) { |
Owen Lin | aea077a | 2011-11-11 12:01:09 +0800 | [diff] [blame] | 464 | if (mVideoView.isPlaying()) pauseVideo(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | class Bookmarker { |
| 470 | private static final String TAG = "Bookmarker"; |
| 471 | |
| 472 | private static final String BOOKMARK_CACHE_FILE = "bookmark"; |
| 473 | private static final int BOOKMARK_CACHE_MAX_ENTRIES = 100; |
| 474 | private static final int BOOKMARK_CACHE_MAX_BYTES = 10 * 1024; |
| 475 | private static final int BOOKMARK_CACHE_VERSION = 1; |
| 476 | |
| 477 | private static final int HALF_MINUTE = 30 * 1000; |
| 478 | private static final int TWO_MINUTES = 4 * HALF_MINUTE; |
| 479 | |
| 480 | private final Context mContext; |
| 481 | |
| 482 | public Bookmarker(Context context) { |
| 483 | mContext = context; |
| 484 | } |
| 485 | |
| 486 | public void setBookmark(Uri uri, int bookmark, int duration) { |
| 487 | try { |
| 488 | BlobCache cache = CacheManager.getCache(mContext, |
| 489 | BOOKMARK_CACHE_FILE, BOOKMARK_CACHE_MAX_ENTRIES, |
| 490 | BOOKMARK_CACHE_MAX_BYTES, BOOKMARK_CACHE_VERSION); |
| 491 | |
| 492 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 493 | DataOutputStream dos = new DataOutputStream(bos); |
| 494 | dos.writeUTF(uri.toString()); |
| 495 | dos.writeInt(bookmark); |
| 496 | dos.writeInt(duration); |
| 497 | dos.flush(); |
| 498 | cache.insert(uri.hashCode(), bos.toByteArray()); |
| 499 | } catch (Throwable t) { |
| 500 | Log.w(TAG, "setBookmark failed", t); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | public Integer getBookmark(Uri uri) { |
| 505 | try { |
| 506 | BlobCache cache = CacheManager.getCache(mContext, |
| 507 | BOOKMARK_CACHE_FILE, BOOKMARK_CACHE_MAX_ENTRIES, |
| 508 | BOOKMARK_CACHE_MAX_BYTES, BOOKMARK_CACHE_VERSION); |
| 509 | |
| 510 | byte[] data = cache.lookup(uri.hashCode()); |
| 511 | if (data == null) return null; |
| 512 | |
| 513 | DataInputStream dis = new DataInputStream( |
| 514 | new ByteArrayInputStream(data)); |
| 515 | |
Owen Lin | 7a5e1e7 | 2012-06-20 16:54:24 +0800 | [diff] [blame] | 516 | String uriString = DataInputStream.readUTF(dis); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 517 | int bookmark = dis.readInt(); |
| 518 | int duration = dis.readInt(); |
| 519 | |
| 520 | if (!uriString.equals(uri.toString())) { |
| 521 | return null; |
| 522 | } |
| 523 | |
| 524 | if ((bookmark < HALF_MINUTE) || (duration < TWO_MINUTES) |
| 525 | || (bookmark > (duration - HALF_MINUTE))) { |
| 526 | return null; |
| 527 | } |
| 528 | return Integer.valueOf(bookmark); |
| 529 | } catch (Throwable t) { |
| 530 | Log.w(TAG, "getBookmark failed", t); |
| 531 | } |
| 532 | return null; |
| 533 | } |
| 534 | } |