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