blob: f2dc9ad2021bdc25831ca07b390ea04102297755 [file] [log] [blame]
Owen Linf9a0a432011-08-17 22:07:43 +08001/*
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
17package com.android.gallery3d.app;
18
Owen Lin7a5e1e72012-06-20 16:54:24 +080019import android.annotation.TargetApi;
Owen Linf9a0a432011-08-17 22:07:43 +080020import android.app.AlertDialog;
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.DialogInterface.OnCancelListener;
25import android.content.DialogInterface.OnClickListener;
26import android.content.Intent;
27import android.content.IntentFilter;
Owen Lin00bb8e42012-05-23 15:47:36 -070028import android.graphics.Color;
Owen Linf9a0a432011-08-17 22:07:43 +080029import android.media.AudioManager;
30import android.media.MediaPlayer;
31import android.net.Uri;
Owen Lindf4763c2012-06-28 17:12:20 +080032import android.os.Build;
Owen Lin540e4692011-09-14 19:49:03 +080033import android.os.Bundle;
Owen Linf9a0a432011-08-17 22:07:43 +080034import android.os.Handler;
Chih-Chung Changfc8c5032011-11-29 14:21:11 +080035import android.view.KeyEvent;
Chih-Chung Chang209a9162011-10-14 16:09:09 +080036import android.view.MotionEvent;
Owen Linf9a0a432011-08-17 22:07:43 +080037import android.view.View;
Chih-Chung Chang209a9162011-10-14 16:09:09 +080038import android.view.ViewGroup;
Owen Linf9a0a432011-08-17 22:07:43 +080039import android.widget.VideoView;
40
Owen Linaea077a2011-11-11 12:01:09 +080041import com.android.gallery3d.R;
Owen Lin7a5e1e72012-06-20 16:54:24 +080042import com.android.gallery3d.common.ApiHelper;
Owen Linaea077a2011-11-11 12:01:09 +080043import com.android.gallery3d.common.BlobCache;
44import com.android.gallery3d.util.CacheManager;
45import com.android.gallery3d.util.GalleryUtils;
46
Owen Linf9a0a432011-08-17 22:07:43 +080047import java.io.ByteArrayInputStream;
48import java.io.ByteArrayOutputStream;
49import java.io.DataInputStream;
50import java.io.DataOutputStream;
51
52public class MoviePlayer implements
Chih-Chung Chang209a9162011-10-14 16:09:09 +080053 MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener,
54 ControllerOverlay.Listener {
Owen Linf9a0a432011-08-17 22:07:43 +080055 @SuppressWarnings("unused")
56 private static final String TAG = "MoviePlayer";
57
Owen Lin540e4692011-09-14 19:49:03 +080058 private static final String KEY_VIDEO_POSITION = "video-position";
59 private static final String KEY_RESUMEABLE_TIME = "resumeable-timeout";
60
Chih-Chung Chang67721732012-07-03 12:49:45 +080061 // 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 Linf9a0a432011-08-17 22:07:43 +080065 // 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 Lin00bb8e42012-05-23 15:47:36 -070070 private static final long BLACK_TIMEOUT = 500;
71
Owen Lin540e4692011-09-14 19:49:03 +080072 // 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 Linf9a0a432011-08-17 22:07:43 +080076 private Context mContext;
77 private final VideoView mVideoView;
Owen Lin00bb8e42012-05-23 15:47:36 -070078 private final View mRootView;
Owen Linf9a0a432011-08-17 22:07:43 +080079 private final Bookmarker mBookmarker;
80 private final Uri mUri;
81 private final Handler mHandler = new Handler();
82 private final AudioBecomingNoisyReceiver mAudioBecomingNoisyReceiver;
Owen Lin00bb8e42012-05-23 15:47:36 -070083 private final MovieControllerOverlay mController;
Owen Linf9a0a432011-08-17 22:07:43 +080084
Owen Lin540e4692011-09-14 19:49:03 +080085 private long mResumeableTime = Long.MAX_VALUE;
86 private int mVideoPosition = 0;
87 private boolean mHasPaused = false;
Ray Chen24525c22012-05-22 17:34:29 +080088 private int mLastSystemUiVis = 0;
Owen Linf9a0a432011-08-17 22:07:43 +080089
Chih-Chung Chang209a9162011-10-14 16:09:09 +080090 // 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 Linf9a0a432011-08-17 22:07:43 +080096 private final Runnable mPlayingChecker = new Runnable() {
Owen Lin540e4692011-09-14 19:49:03 +080097 @Override
Owen Linf9a0a432011-08-17 22:07:43 +080098 public void run() {
99 if (mVideoView.isPlaying()) {
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800100 mController.showPlaying();
Owen Linf9a0a432011-08-17 22:07:43 +0800101 } else {
102 mHandler.postDelayed(mPlayingChecker, 250);
103 }
104 }
105 };
106
Owen Lin00bb8e42012-05-23 15:47:36 -0700107 private final Runnable mRemoveBackground = new Runnable() {
Wu-cheng Li22a36332012-06-21 16:45:55 +0800108 @SuppressWarnings("deprecation")
Owen Lin00bb8e42012-05-23 15:47:36 -0700109 @Override
110 public void run() {
Wu-cheng Li22a36332012-06-21 16:45:55 +0800111 mRootView.setBackgroundDrawable(null);
Owen Lin00bb8e42012-05-23 15:47:36 -0700112 }
113 };
114
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800115 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 Lin00bb8e42012-05-23 15:47:36 -0700123 public MoviePlayer(View rootView, final MovieActivity movieActivity,
124 Uri videoUri, Bundle savedInstance, boolean canReplay) {
Owen Linf9a0a432011-08-17 22:07:43 +0800125 mContext = movieActivity.getApplicationContext();
Owen Lin00bb8e42012-05-23 15:47:36 -0700126 mRootView = rootView;
Owen Linf9a0a432011-08-17 22:07:43 +0800127 mVideoView = (VideoView) rootView.findViewById(R.id.surface_view);
Owen Linf9a0a432011-08-17 22:07:43 +0800128 mBookmarker = new Bookmarker(movieActivity);
Owen Linf9a0a432011-08-17 22:07:43 +0800129 mUri = videoUri;
130
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800131 mController = new MovieControllerOverlay(mContext);
132 ((ViewGroup)rootView).addView(mController.getView());
133 mController.setListener(this);
134 mController.setCanReplay(canReplay);
Owen Linf9a0a432011-08-17 22:07:43 +0800135
136 mVideoView.setOnErrorListener(this);
137 mVideoView.setOnCompletionListener(this);
138 mVideoView.setVideoURI(mUri);
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800139 mVideoView.setOnTouchListener(new View.OnTouchListener() {
Owen Lin00bb8e42012-05-23 15:47:36 -0700140 @Override
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800141 public boolean onTouch(View v, MotionEvent event) {
142 mController.show();
143 return true;
Owen Linf9a0a432011-08-17 22:07:43 +0800144 }
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800145 });
Chih-Chung Chang81760122011-09-27 16:50:16 +0800146
Owen Lin00bb8e42012-05-23 15:47:36 -0700147 // 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 Lin7a5e1e72012-06-20 16:54:24 +0800159 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 Lindf4763c2012-06-28 17:12:20 +0800186 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Owen Lin7a5e1e72012-06-20 16:54:24 +0800187 private void setOnSystemUiVisibilityChangeListener() {
188 if (!ApiHelper.HAS_VIEW_SYSTEM_UI_FLAG_HIDE_NAVIGATION) return;
189
Chih-Chung Chang81760122011-09-27 16:50:16 +0800190 // 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 Chen392a2922012-05-14 17:19:56 +0800192 // the media control and enable system UI (e.g. ActionBar) to be visible at this point
Chih-Chung Chang81760122011-09-27 16:50:16 +0800193 mVideoView.setOnSystemUiVisibilityChangeListener(
194 new View.OnSystemUiVisibilityChangeListener() {
Owen Lin00bb8e42012-05-23 15:47:36 -0700195 @Override
Chih-Chung Chang81760122011-09-27 16:50:16 +0800196 public void onSystemUiVisibilityChange(int visibility) {
Ray Chen24525c22012-05-22 17:34:29 +0800197 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 Chang209a9162011-10-14 16:09:09 +0800201 mController.show();
Owen Lin00bb8e42012-05-23 15:47:36 -0700202
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 Chang81760122011-09-27 16:50:16 +0800215 }
216 }
217 });
Owen Linf9a0a432011-08-17 22:07:43 +0800218 }
219
Owen Lindf4763c2012-06-28 17:12:20 +0800220 @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Chih-Chung Chang81760122011-09-27 16:50:16 +0800221 private void showSystemUi(boolean visible) {
Owen Lin7a5e1e72012-06-20 16:54:24 +0800222 if (!ApiHelper.HAS_VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE) return;
223
Ray Chen24525c22012-05-22 17:34:29 +0800224 int flag = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
225 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
Owen Lin7a5e1e72012-06-20 16:54:24 +0800226 | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
Ray Chen24525c22012-05-22 17:34:29 +0800227 if (!visible) {
The Android Open Source Projectca7d9bf2012-06-29 08:19:39 -0700228 flag |= View.STATUS_BAR_HIDDEN | View.SYSTEM_UI_FLAG_FULLSCREEN
Ray Chen24525c22012-05-22 17:34:29 +0800229 | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
Ray Chen392a2922012-05-14 17:19:56 +0800230 }
Ray Chen24525c22012-05-22 17:34:29 +0800231 mVideoView.setSystemUiVisibility(flag);
Chih-Chung Chang81760122011-09-27 16:50:16 +0800232 }
233
Owen Lin540e4692011-09-14 19:49:03 +0800234 public void onSaveInstanceState(Bundle outState) {
235 outState.putInt(KEY_VIDEO_POSITION, mVideoPosition);
236 outState.putLong(KEY_RESUMEABLE_TIME, mResumeableTime);
237 }
238
Owen Linf9a0a432011-08-17 22:07:43 +0800239 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 Lin540e4692011-09-14 19:49:03 +0800246 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800247 public void onCancel(DialogInterface dialog) {
248 onCompletion();
249 }
250 });
251 builder.setPositiveButton(
252 R.string.resume_playing_resume, new OnClickListener() {
Owen Lin540e4692011-09-14 19:49:03 +0800253 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800254 public void onClick(DialogInterface dialog, int which) {
255 mVideoView.seekTo(bookmark);
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800256 startVideo();
Owen Linf9a0a432011-08-17 22:07:43 +0800257 }
258 });
259 builder.setNegativeButton(
260 R.string.resume_playing_restart, new OnClickListener() {
Owen Lin540e4692011-09-14 19:49:03 +0800261 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800262 public void onClick(DialogInterface dialog, int which) {
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800263 startVideo();
Owen Linf9a0a432011-08-17 22:07:43 +0800264 }
265 });
266 builder.show();
267 }
268
269 public void onPause() {
Owen Linf9a0a432011-08-17 22:07:43 +0800270 mHasPaused = true;
Owen Lin540e4692011-09-14 19:49:03 +0800271 mHandler.removeCallbacksAndMessages(null);
272 mVideoPosition = mVideoView.getCurrentPosition();
273 mBookmarker.setBookmark(mUri, mVideoPosition, mVideoView.getDuration());
274 mVideoView.suspend();
275 mResumeableTime = System.currentTimeMillis() + RESUMEABLE_TIMEOUT;
Owen Linf9a0a432011-08-17 22:07:43 +0800276 }
277
278 public void onResume() {
279 if (mHasPaused) {
Owen Lin540e4692011-09-14 19:49:03 +0800280 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 Chang209a9162011-10-14 16:09:09 +0800285 pauseVideo();
Owen Linf9a0a432011-08-17 22:07:43 +0800286 }
287 }
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800288 mHandler.post(mProgressChecker);
Owen Linf9a0a432011-08-17 22:07:43 +0800289 }
290
291 public void onDestroy() {
292 mVideoView.stopPlayback();
293 mAudioBecomingNoisyReceiver.unregister();
294 }
295
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800296 // 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 Lin00bb8e42012-05-23 15:47:36 -0700319 mController.hide();
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800320 }
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 Linf9a0a432011-08-17 22:07:43 +0800339 public boolean onError(MediaPlayer player, int arg1, int arg2) {
340 mHandler.removeCallbacksAndMessages(null);
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800341 // VideoView will show an error dialog if we return false, so no need
342 // to show more message.
343 mController.showErrorMessage("");
Owen Linf9a0a432011-08-17 22:07:43 +0800344 return false;
345 }
346
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800347 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800348 public void onCompletion(MediaPlayer mp) {
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800349 mController.showEnded();
Owen Linf9a0a432011-08-17 22:07:43 +0800350 onCompletion();
351 }
352
353 public void onCompletion() {
354 }
355
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800356 // 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 Chang209a9162011-10-14 16:09:09 +0800386 setProgress();
Owen Lin0b4e9092012-06-19 14:35:06 +0800387 showSystemUi(true);
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800388 }
389
390 @Override
391 public void onHidden() {
392 mShowing = false;
Chih-Chung Chang209a9162011-10-14 16:09:09 +0800393 showSystemUi(false);
394 }
395
396 @Override
397 public void onReplay() {
398 startVideo();
399 }
400
Chih-Chung Changfc8c5032011-11-29 14:21:11 +0800401 // 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 Chang67721732012-07-03 12:49:45 +0800418 case KEYCODE_MEDIA_PAUSE:
Chih-Chung Changfc8c5032011-11-29 14:21:11 +0800419 if (mVideoView.isPlaying()) {
420 pauseVideo();
421 }
422 return true;
Chih-Chung Chang67721732012-07-03 12:49:45 +0800423 case KEYCODE_MEDIA_PLAY:
Chih-Chung Changfc8c5032011-11-29 14:21:11 +0800424 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 Chang209a9162011-10-14 16:09:09 +0800450 // We want to pause when the headset is unplugged.
Owen Linf9a0a432011-08-17 22:07:43 +0800451 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 Linaea077a2011-11-11 12:01:09 +0800464 if (mVideoView.isPlaying()) pauseVideo();
Owen Linf9a0a432011-08-17 22:07:43 +0800465 }
466 }
467}
468
469class 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 Lin7a5e1e72012-06-20 16:54:24 +0800516 String uriString = DataInputStream.readUTF(dis);
Owen Linf9a0a432011-08-17 22:07:43 +0800517 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}