b0578c2b5e30bff758c230376c43b63e4ffa0ef9
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewMediaFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20 package com.owncloud.android.ui.preview;
21
22 import android.accounts.Account;
23 import android.app.Activity;
24 import android.content.ContentResolver;
25 import android.graphics.Bitmap;
26 import android.graphics.BitmapFactory;
27 import android.media.MediaMetadataRetriever;
28 import android.support.v7.app.AlertDialog;
29 import android.content.ComponentName;
30 import android.content.Context;
31 import android.content.DialogInterface;
32 import android.content.Intent;
33 import android.content.ServiceConnection;
34 import android.content.res.Configuration;
35 import android.content.res.Resources;
36 import android.media.MediaPlayer;
37 import android.media.MediaPlayer.OnCompletionListener;
38 import android.media.MediaPlayer.OnErrorListener;
39 import android.media.MediaPlayer.OnPreparedListener;
40 import android.net.Uri;
41 import android.os.Build;
42 import android.os.Bundle;
43 import android.os.IBinder;
44 import android.view.LayoutInflater;
45 import android.view.Menu;
46 import android.view.MenuInflater;
47 import android.view.MenuItem;
48 import android.view.MotionEvent;
49 import android.view.View;
50 import android.view.View.OnTouchListener;
51 import android.view.ViewGroup;
52 import android.widget.ImageView;
53 import android.widget.Toast;
54 import android.widget.VideoView;
55
56 import com.owncloud.android.R;
57 import com.owncloud.android.datamodel.OCFile;
58 import com.owncloud.android.files.FileMenuFilter;
59 import com.owncloud.android.lib.common.utils.Log_OC;
60 import com.owncloud.android.media.MediaControlView;
61 import com.owncloud.android.media.MediaService;
62 import com.owncloud.android.media.MediaServiceBinder;
63 import com.owncloud.android.ui.activity.FileActivity;
64 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment;
65 import com.owncloud.android.ui.dialog.RemoveFileDialogFragment;
66 import com.owncloud.android.ui.fragment.FileFragment;
67
68
69 /**
70 * This fragment shows a preview of a downloaded media file (audio or video).
71 *
72 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will
73 * produce an {@link IllegalStateException}.
74 *
75 * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is
76 * generated on instantiation too.
77 */
78 public class PreviewMediaFragment extends FileFragment implements
79 OnTouchListener {
80
81 public static final String EXTRA_FILE = "FILE";
82 public static final String EXTRA_ACCOUNT = "ACCOUNT";
83 private static final String EXTRA_PLAY_POSITION = "PLAY_POSITION";
84 private static final String EXTRA_PLAYING = "PLAYING";
85
86 private View mView;
87 private Account mAccount;
88 private ImageView mImagePreview;
89 private VideoView mVideoPreview;
90 private int mSavedPlaybackPosition;
91
92 private MediaServiceBinder mMediaServiceBinder = null;
93 private MediaControlView mMediaController = null;
94 private MediaServiceConnection mMediaServiceConnection = null;
95 private VideoHelper mVideoHelper;
96 private boolean mAutoplay;
97 public boolean mPrepared;
98
99 private static final String TAG = PreviewMediaFragment.class.getSimpleName();
100
101
102 /**
103 * Creates a fragment to preview a file.
104 * <p/>
105 * When 'fileToDetail' or 'ocAccount' are null
106 *
107 * @param fileToDetail An {@link OCFile} to preview in the fragment
108 * @param ocAccount An ownCloud account; needed to start downloads
109 */
110 public PreviewMediaFragment(
111 OCFile fileToDetail,
112 Account ocAccount,
113 int startPlaybackPosition,
114 boolean autoplay) {
115
116 super(fileToDetail);
117 mAccount = ocAccount;
118 mSavedPlaybackPosition = startPlaybackPosition;
119 mAutoplay = autoplay;
120 }
121
122
123 /**
124 * Creates an empty fragment for previews.
125 * <p/>
126 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
127 * (for instance, when the device is turned a aside).
128 * <p/>
129 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
130 * construction
131 */
132 public PreviewMediaFragment() {
133 super();
134 mAccount = null;
135 mSavedPlaybackPosition = 0;
136 mAutoplay = true;
137 }
138
139
140 /**
141 * {@inheritDoc}
142 */
143 @Override
144 public void onCreate(Bundle savedInstanceState) {
145 super.onCreate(savedInstanceState);
146 setHasOptionsMenu(true);
147 }
148
149
150 /**
151 * {@inheritDoc}
152 */
153 @Override
154 public View onCreateView(LayoutInflater inflater, ViewGroup container,
155 Bundle savedInstanceState) {
156 super.onCreateView(inflater, container, savedInstanceState);
157 Log_OC.e(TAG, "onCreateView");
158
159
160 mView = inflater.inflate(R.layout.file_preview, container, false);
161
162 mImagePreview = (ImageView) mView.findViewById(R.id.image_preview);
163 mVideoPreview = (VideoView) mView.findViewById(R.id.video_preview);
164 mVideoPreview.setOnTouchListener(this);
165
166 mMediaController = (MediaControlView) mView.findViewById(R.id.media_controller);
167
168 return mView;
169 }
170
171
172 /**
173 * {@inheritDoc}
174 */
175 @Override
176 public void onActivityCreated(Bundle savedInstanceState) {
177 super.onActivityCreated(savedInstanceState);
178 Log_OC.e(TAG, "onActivityCreated");
179
180 OCFile file = getFile();
181 if (savedInstanceState == null) {
182 if (file == null) {
183 throw new IllegalStateException("Instanced with a NULL OCFile");
184 }
185 if (mAccount == null) {
186 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
187 }
188 if (!file.isDown()) {
189 throw new IllegalStateException("There is no local file to preview");
190 }
191
192 }
193 else {
194 file = (OCFile) savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE);
195 setFile(file);
196 mAccount = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_ACCOUNT);
197 mSavedPlaybackPosition =
198 savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION);
199 mAutoplay = savedInstanceState.getBoolean(PreviewMediaFragment.EXTRA_PLAYING);
200
201 }
202 if (file != null && file.isDown()) {
203 if (file.isVideo()) {
204 mVideoPreview.setVisibility(View.VISIBLE);
205 mImagePreview.setVisibility(View.GONE);
206 prepareVideo();
207
208 }
209 else {
210 mVideoPreview.setVisibility(View.GONE);
211 mImagePreview.setVisibility(View.VISIBLE);
212 extractAndSetCoverArt(file);
213 }
214 }
215
216 }
217
218 /**
219 * tries to read the cover art from the audio file and sets it as cover art.
220 *
221 * @param file audio file with potential cover art
222 */
223 private void extractAndSetCoverArt(OCFile file) {
224 if (file.isAudio()) {
225 try {
226 MediaMetadataRetriever mmr = new MediaMetadataRetriever();
227 mmr.setDataSource(file.getStoragePath());
228 byte[] data = mmr.getEmbeddedPicture();
229 if (data != null) {
230 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
231 mImagePreview.setImageBitmap(bitmap); //associated cover art in bitmap
232 } else {
233 mImagePreview.setImageResource(R.drawable.logo);
234 }
235 } catch (Throwable t) {
236 mImagePreview.setImageResource(R.drawable.logo);
237 }
238 }
239 }
240
241
242 /**
243 * {@inheritDoc}
244 */
245 @Override
246 public void onSaveInstanceState(Bundle outState) {
247 super.onSaveInstanceState(outState);
248 Log_OC.e(TAG, "onSaveInstanceState");
249
250 outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, getFile());
251 outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount);
252
253 if (getFile().isVideo()) {
254 mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
255 mAutoplay = mVideoPreview.isPlaying();
256 outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION, mSavedPlaybackPosition);
257 outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING, mAutoplay);
258 }
259 else {
260 outState.putInt(
261 PreviewMediaFragment.EXTRA_PLAY_POSITION,
262 mMediaServiceBinder.getCurrentPosition());
263 outState.putBoolean(
264 PreviewMediaFragment.EXTRA_PLAYING, mMediaServiceBinder.isPlaying());
265 }
266 }
267
268
269 @Override
270 public void onStart() {
271 super.onStart();
272 Log_OC.e(TAG, "onStart");
273
274 OCFile file = getFile();
275 if (file != null && file.isDown()) {
276 if (file.isAudio()) {
277 bindMediaService();
278
279 }
280 else {
281 if (file.isVideo()) {
282 stopAudio();
283 playVideo();
284 }
285 }
286 }
287 }
288
289
290 private void stopAudio() {
291 Intent i = new Intent(getActivity(), MediaService.class);
292 i.setAction(MediaService.ACTION_STOP_ALL);
293 getActivity().startService(i);
294 }
295
296
297 /**
298 * {@inheritDoc}
299 */
300 @Override
301 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
302 super.onCreateOptionsMenu(menu, inflater);
303 inflater.inflate(R.menu.file_actions_menu, menu);
304 }
305
306
307 /**
308 * {@inheritDoc}
309 */
310 @Override
311 public void onPrepareOptionsMenu(Menu menu) {
312 super.onPrepareOptionsMenu(menu);
313
314 if (mContainerActivity.getStorageManager() != null) {
315 FileMenuFilter mf = new FileMenuFilter(
316 getFile(),
317 mContainerActivity.getStorageManager().getAccount(),
318 mContainerActivity,
319 getActivity()
320 );
321 mf.filter(menu);
322 }
323
324 // additional restriction for this fragment
325 // TODO allow renaming in PreviewImageFragment
326 MenuItem item = menu.findItem(R.id.action_rename_file);
327 if (item != null) {
328 item.setVisible(false);
329 item.setEnabled(false);
330 }
331
332 // additional restriction for this fragment
333 item = menu.findItem(R.id.action_move);
334 if (item != null) {
335 item.setVisible(false);
336 item.setEnabled(false);
337 }
338
339 // additional restriction for this fragment
340 item = menu.findItem(R.id.action_copy);
341 if (item != null) {
342 item.setVisible(false);
343 item.setEnabled(false);
344 }
345 }
346
347
348 /**
349 * {@inheritDoc}
350 */
351 @Override
352 public boolean onOptionsItemSelected(MenuItem item) {
353 switch (item.getItemId()) {
354 case R.id.action_share_file: {
355 stopPreview(false);
356 mContainerActivity.getFileOperationsHelper().shareFileWithLink(getFile());
357 return true;
358 }
359 case R.id.action_share_with_users: {
360 seeShareFile();
361 return true;
362 }
363 case R.id.action_unshare_file: {
364 stopPreview(false);
365 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
366 return true;
367 }
368 case R.id.action_open_file_with: {
369 openFile();
370 return true;
371 }
372 case R.id.action_remove_file: {
373 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
374 dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
375 return true;
376 }
377 case R.id.action_see_details: {
378 seeDetails();
379 return true;
380 }
381 case R.id.action_send_file: {
382 sendFile();
383 return true;
384 }
385 case R.id.action_sync_file: {
386 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
387 return true;
388 }
389 case R.id.action_favorite_file:{
390 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), true);
391 return true;
392 }
393 case R.id.action_unfavorite_file:{
394 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), false);
395 return true;
396 }
397 default:
398 return false;
399 }
400 }
401
402
403 /**
404 * Update the file of the fragment with file value
405 *
406 * @param file
407 */
408 public void updateFile(OCFile file) {
409 setFile(file);
410 }
411
412 private void sendFile() {
413 stopPreview(false);
414 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
415
416 }
417
418 private void seeDetails() {
419 stopPreview(false);
420 mContainerActivity.showDetails(getFile());
421 }
422
423 private void seeShareFile() {
424 stopPreview(false);
425 mContainerActivity.getFileOperationsHelper().showShareFile(getFile());
426 }
427
428 private void prepareVideo() {
429 // create helper to get more control on the playback
430 mVideoHelper = new VideoHelper();
431 mVideoPreview.setOnPreparedListener(mVideoHelper);
432 mVideoPreview.setOnCompletionListener(mVideoHelper);
433 mVideoPreview.setOnErrorListener(mVideoHelper);
434 }
435
436 @SuppressWarnings("static-access")
437 private void playVideo() {
438 // create and prepare control panel for the user
439 mMediaController.setMediaPlayer(mVideoPreview);
440
441 // load the video file in the video player ;
442 // when done, VideoHelper#onPrepared() will be called
443 mVideoPreview.setVideoURI(getFile().getStorageUri());
444 }
445
446
447 private class VideoHelper implements OnCompletionListener, OnPreparedListener, OnErrorListener {
448
449 /**
450 * Called when the file is ready to be played.
451 * <p/>
452 * Just starts the playback.
453 *
454 * @param vp {@link MediaPlayer} instance performing the playback.
455 */
456 @Override
457 public void onPrepared(MediaPlayer vp) {
458 Log_OC.e(TAG, "onPrepared");
459 mVideoPreview.seekTo(mSavedPlaybackPosition);
460 if (mAutoplay) {
461 mVideoPreview.start();
462 }
463 mMediaController.setEnabled(true);
464 mMediaController.updatePausePlay();
465 mPrepared = true;
466 }
467
468
469 /**
470 * Called when the file is finished playing.
471 * <p/>
472 * Finishes the activity.
473 *
474 * @param mp {@link MediaPlayer} instance performing the playback.
475 */
476 @Override
477 public void onCompletion(MediaPlayer mp) {
478 Log_OC.e(TAG, "completed");
479 if (mp != null) {
480 mVideoPreview.seekTo(0);
481 // next lines are necessary to work around undesired video loops
482 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD) {
483 mVideoPreview.pause();
484
485 }
486 else {
487 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD_MR1) {
488 // mVideePreview.pause() is not enough
489
490 mMediaController.setEnabled(false);
491 mVideoPreview.stopPlayback();
492 mAutoplay = false;
493 mSavedPlaybackPosition = 0;
494 mVideoPreview.setVideoURI(getFile().getStorageUri());
495 }
496 }
497 } // else : called from onError()
498 mMediaController.updatePausePlay();
499 }
500
501
502 /**
503 * Called when an error in playback occurs.
504 *
505 * @param mp {@link MediaPlayer} instance performing the playback.
506 * @param what Type of error
507 * @param extra Extra code specific to the error
508 */
509 @Override
510 public boolean onError(MediaPlayer mp, int what, int extra) {
511 if (mVideoPreview.getWindowToken() != null) {
512 String message = MediaService.getMessageForMediaError(
513 getActivity(), what, extra);
514 new AlertDialog.Builder(getActivity())
515 .setMessage(message)
516 .setPositiveButton(android.R.string.VideoView_error_button,
517 new DialogInterface.OnClickListener() {
518 public void onClick(DialogInterface dialog, int whichButton) {
519 dialog.dismiss();
520 VideoHelper.this.onCompletion(null);
521 }
522 })
523 .setCancelable(false)
524 .show();
525 }
526 return true;
527 }
528
529 }
530
531
532 @Override
533 public void onPause() {
534 Log_OC.e(TAG, "onPause");
535 super.onPause();
536 }
537
538 @Override
539 public void onResume() {
540 super.onResume();
541 Log_OC.e(TAG, "onResume");
542 }
543
544 @Override
545 public void onDestroy() {
546 Log_OC.e(TAG, "onDestroy");
547 super.onDestroy();
548 }
549
550 @Override
551 public void onStop() {
552 Log_OC.e(TAG, "onStop");
553
554 mPrepared = false;
555 if (mMediaServiceConnection != null) {
556 Log_OC.d(TAG, "Unbinding from MediaService ...");
557 if (mMediaServiceBinder != null && mMediaController != null) {
558 mMediaServiceBinder.unregisterMediaController(mMediaController);
559 }
560 getActivity().unbindService(mMediaServiceConnection);
561 mMediaServiceConnection = null;
562 mMediaServiceBinder = null;
563 }
564
565 super.onStop();
566 }
567
568 @Override
569 public boolean onTouch(View v, MotionEvent event) {
570 if (event.getAction() == MotionEvent.ACTION_DOWN && v == mVideoPreview) {
571 // added a margin on the left to avoid interfering with gesture to open navigation drawer
572 if (event.getX() / Resources.getSystem().getDisplayMetrics().density > 24.0) {
573 startFullScreenVideo();
574 }
575 return true;
576 }
577 return false;
578 }
579
580
581 private void startFullScreenVideo() {
582 Intent i = new Intent(getActivity(), PreviewVideoActivity.class);
583 i.putExtra(FileActivity.EXTRA_ACCOUNT, mAccount);
584 i.putExtra(FileActivity.EXTRA_FILE, getFile());
585 i.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, mVideoPreview.isPlaying());
586 mVideoPreview.pause();
587 i.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPreview.getCurrentPosition());
588 startActivityForResult(i, 0);
589 }
590
591 @Override
592 public void onConfigurationChanged(Configuration newConfig) {
593 Log_OC.e(TAG, "onConfigurationChanged " + this);
594 }
595
596 @Override
597 public void onActivityResult(int requestCode, int resultCode, Intent data) {
598 Log_OC.e(TAG, "onActivityResult " + this);
599 super.onActivityResult(requestCode, resultCode, data);
600 if (resultCode == Activity.RESULT_OK) {
601 mSavedPlaybackPosition = data.getExtras().getInt(
602 PreviewVideoActivity.EXTRA_START_POSITION);
603 mAutoplay = data.getExtras().getBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY);
604 }
605 }
606
607
608 private void playAudio() {
609 OCFile file = getFile();
610 if (!mMediaServiceBinder.isPlaying(file)) {
611 Log_OC.d(TAG, "starting playback of " + file.getStoragePath());
612 mMediaServiceBinder.start(mAccount, file, mAutoplay, mSavedPlaybackPosition);
613
614 }
615 else {
616 if (!mMediaServiceBinder.isPlaying() && mAutoplay) {
617 mMediaServiceBinder.start();
618 mMediaController.updatePausePlay();
619 }
620 }
621 }
622
623
624 private void bindMediaService() {
625 Log_OC.d(TAG, "Binding to MediaService...");
626 if (mMediaServiceConnection == null) {
627 mMediaServiceConnection = new MediaServiceConnection();
628 }
629 getActivity().bindService( new Intent(getActivity(),
630 MediaService.class),
631 mMediaServiceConnection,
632 Context.BIND_AUTO_CREATE);
633 // follow the flow in MediaServiceConnection#onServiceConnected(...)
634 }
635
636 /** Defines callbacks for service binding, passed to bindService() */
637 private class MediaServiceConnection implements ServiceConnection {
638
639 @Override
640 public void onServiceConnected(ComponentName component, IBinder service) {
641 if (getActivity() != null) {
642 if (component.equals(
643 new ComponentName(getActivity(), MediaService.class))) {
644 Log_OC.d(TAG, "Media service connected");
645 mMediaServiceBinder = (MediaServiceBinder) service;
646 if (mMediaServiceBinder != null) {
647 prepareMediaController();
648 playAudio(); // do not wait for the touch of nobody to play audio
649
650 Log_OC.d(TAG, "Successfully bound to MediaService, MediaController ready");
651
652 }
653 else {
654 Log_OC.e(TAG, "Unexpected response from MediaService while binding");
655 }
656 }
657 }
658 }
659
660 private void prepareMediaController() {
661 mMediaServiceBinder.registerMediaController(mMediaController);
662 if (mMediaController != null) {
663 mMediaController.setMediaPlayer(mMediaServiceBinder);
664 mMediaController.setEnabled(true);
665 mMediaController.updatePausePlay();
666 }
667 }
668
669 @Override
670 public void onServiceDisconnected(ComponentName component) {
671 if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
672 Log_OC.e(TAG, "Media service suddenly disconnected");
673 if (mMediaController != null) {
674 mMediaController.setMediaPlayer(null);
675 }
676 else {
677 Toast.makeText(
678 getActivity(),
679 "No media controller to release when disconnected from media service",
680 Toast.LENGTH_SHORT).show();
681 }
682 mMediaServiceBinder = null;
683 mMediaServiceConnection = null;
684 }
685 }
686 }
687
688
689 /**
690 * Opens the previewed file with an external application.
691 */
692 private void openFile() {
693 stopPreview(true);
694 mContainerActivity.getFileOperationsHelper().openFile(getFile());
695 finish();
696 }
697
698 /**
699 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
700 * to be previewed.
701 *
702 * @param file File to test if can be previewed.
703 * @return 'True' if the file can be handled by the fragment.
704 */
705 public static boolean canBePreviewed(OCFile file) {
706 return (file != null && (file.isAudio() || file.isVideo()));
707 }
708
709
710 public void stopPreview(boolean stopAudio) {
711 OCFile file = getFile();
712 if (file.isAudio() && stopAudio) {
713 mMediaServiceBinder.pause();
714
715 }
716 else {
717 if (file.isVideo()) {
718 mVideoPreview.stopPlayback();
719 }
720 }
721 }
722
723
724 /**
725 * Finishes the preview
726 */
727 private void finish() {
728 getActivity().onBackPressed();
729 }
730
731
732 public int getPosition() {
733 if (mPrepared) {
734 mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
735 }
736 Log_OC.e(TAG, "getting position: " + mSavedPlaybackPosition);
737 return mSavedPlaybackPosition;
738 }
739
740 public boolean isPlaying() {
741 if (mPrepared) {
742 mAutoplay = mVideoPreview.isPlaying();
743 }
744 return mAutoplay;
745 }
746
747 }