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