updated readme
[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_share_with_users: {
370 seeShareFile();
371 return true;
372 }
373 case R.id.action_unshare_file: {
374 stopPreview(false);
375 mContainerActivity.getFileOperationsHelper().unshareFileWithLink(getFile());
376 return true;
377 }
378 case R.id.action_open_file_with: {
379 openFile();
380 return true;
381 }
382 case R.id.action_remove_file: {
383 RemoveFileDialogFragment dialog = RemoveFileDialogFragment.newInstance(getFile());
384 dialog.show(getFragmentManager(), ConfirmationDialogFragment.FTAG_CONFIRMATION);
385 return true;
386 }
387 case R.id.action_see_details: {
388 seeDetails();
389 return true;
390 }
391 case R.id.action_send_file: {
392 sendFile();
393 return true;
394 }
395 case R.id.action_sync_file: {
396 mContainerActivity.getFileOperationsHelper().syncFile(getFile());
397 return true;
398 }
399 case R.id.action_favorite_file:{
400 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), true);
401 return true;
402 }
403 case R.id.action_unfavorite_file:{
404 mContainerActivity.getFileOperationsHelper().toggleFavorite(getFile(), false);
405 return true;
406 }
407 default:
408 return false;
409 }
410 }
411
412
413 /**
414 * Update the file of the fragment with file value
415 *
416 * @param file
417 */
418 public void updateFile(OCFile file) {
419 setFile(file);
420 }
421
422 private void sendFile() {
423 stopPreview(false);
424 mContainerActivity.getFileOperationsHelper().sendDownloadedFile(getFile());
425
426 }
427
428 private void seeDetails() {
429 stopPreview(false);
430 mContainerActivity.showDetails(getFile());
431 }
432
433 private void seeShareFile() {
434 stopPreview(false);
435 mContainerActivity.getFileOperationsHelper().showShareFile(getFile());
436 }
437
438 private void prepareVideo() {
439 // create helper to get more control on the playback
440 mVideoHelper = new VideoHelper();
441 mVideoPreview.setOnPreparedListener(mVideoHelper);
442 mVideoPreview.setOnCompletionListener(mVideoHelper);
443 mVideoPreview.setOnErrorListener(mVideoHelper);
444 }
445
446 @SuppressWarnings("static-access")
447 private void playVideo() {
448 // create and prepare control panel for the user
449 mMediaController.setMediaPlayer(mVideoPreview);
450
451 // load the video file in the video player ;
452 // when done, VideoHelper#onPrepared() will be called
453 if (getFile().isDown()) {
454 mUri = getFile().getStoragePath();
455 } else {
456 Context context = MainApp.getAppContext();
457 Account account = mContainerActivity.getStorageManager().getAccount();
458
459 mUri = generateUrlWithCredentials(account, context, getFile());
460 }
461
462 mVideoPreview.setVideoPath(mUri);
463 }
464
465 public static String generateUrlWithCredentials(Account account, Context context, OCFile file){
466 OwnCloudAccount ocAccount = null;
467 try {
468 ocAccount = new OwnCloudAccount(account, context);
469
470 final ClientGenerationTask task = new ClientGenerationTask();
471 task.execute(ocAccount);
472
473 OwnCloudClient mClient = task.get();
474 String url = AccountUtils.constructFullURLForAccount(context, account) + Uri.encode(file.getRemotePath(), "/");
475 OwnCloudCredentials credentials = mClient.getCredentials();
476
477 return url.replace("//", "//" + credentials.getUsername() + ":" + credentials.getAuthToken() + "@");
478
479 } catch (AccountUtils.AccountNotFoundException e) {
480 e.printStackTrace();
481
482 } catch (InterruptedException e) {
483 e.printStackTrace();
484 } catch (ExecutionException e) {
485 e.printStackTrace();
486 }
487 return "";
488 }
489
490 public static class ClientGenerationTask extends AsyncTask<Object, Void, OwnCloudClient> {
491 @Override
492 protected OwnCloudClient doInBackground(Object... params) {
493 Object account = params[0];
494 if (account instanceof OwnCloudAccount){
495 try {
496 OwnCloudAccount ocAccount = (OwnCloudAccount) account;
497 return OwnCloudClientManagerFactory.getDefaultSingleton().
498 getClientFor(ocAccount, MainApp.getAppContext());
499 } catch (AccountUtils.AccountNotFoundException e) {
500 e.printStackTrace();
501 } catch (OperationCanceledException e) {
502 e.printStackTrace();
503 } catch (AuthenticatorException e) {
504 e.printStackTrace();
505 } catch (IOException e) {
506 e.printStackTrace();
507 }
508 }
509
510 return null;
511 }
512 }
513
514
515 private class VideoHelper implements OnCompletionListener, OnPreparedListener, OnErrorListener {
516
517 /**
518 * Called when the file is ready to be played.
519 * <p/>
520 * Just starts the playback.
521 *
522 * @param vp {@link MediaPlayer} instance performing the playback.
523 */
524 @Override
525 public void onPrepared(MediaPlayer vp) {
526 Log_OC.e(TAG, "onPrepared");
527 mVideoPreview.seekTo(mSavedPlaybackPosition);
528 if (mAutoplay) {
529 mVideoPreview.start();
530 }
531 mMediaController.setEnabled(true);
532 mMediaController.updatePausePlay();
533 mPrepared = true;
534 }
535
536
537 /**
538 * Called when the file is finished playing.
539 * <p/>
540 * Finishes the activity.
541 *
542 * @param mp {@link MediaPlayer} instance performing the playback.
543 */
544 @Override
545 public void onCompletion(MediaPlayer mp) {
546 Log_OC.e(TAG, "completed");
547 if (mp != null) {
548 mVideoPreview.seekTo(0);
549 // next lines are necessary to work around undesired video loops
550 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD) {
551 mVideoPreview.pause();
552
553 }
554 else {
555 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD_MR1) {
556 // mVideePreview.pause() is not enough
557
558 mMediaController.setEnabled(false);
559 mVideoPreview.stopPlayback();
560 mAutoplay = false;
561 mSavedPlaybackPosition = 0;
562 mVideoPreview.setVideoPath(getFile().getStoragePath());
563 }
564 }
565 } // else : called from onError()
566 mMediaController.updatePausePlay();
567 }
568
569
570 /**
571 * Called when an error in playback occurs.
572 *
573 * @param mp {@link MediaPlayer} instance performing the playback.
574 * @param what Type of error
575 * @param extra Extra code specific to the error
576 */
577 @Override
578 public boolean onError(MediaPlayer mp, int what, int extra) {
579 MediaService.streamWithExternalApp(mUri, getActivity()).show();
580 return true;
581 }
582 }
583
584 @Override
585 public void onPause() {
586 Log_OC.e(TAG, "onPause");
587 super.onPause();
588 }
589
590 @Override
591 public void onResume() {
592 super.onResume();
593 Log_OC.e(TAG, "onResume");
594 }
595
596 @Override
597 public void onDestroy() {
598 Log_OC.e(TAG, "onDestroy");
599 super.onDestroy();
600 }
601
602 @Override
603 public void onStop() {
604 Log_OC.e(TAG, "onStop");
605
606 mPrepared = false;
607 if (mMediaServiceConnection != null) {
608 Log_OC.d(TAG, "Unbinding from MediaService ...");
609 if (mMediaServiceBinder != null && mMediaController != null) {
610 mMediaServiceBinder.unregisterMediaController(mMediaController);
611 }
612 getActivity().unbindService(mMediaServiceConnection);
613 mMediaServiceConnection = null;
614 mMediaServiceBinder = null;
615 }
616
617 super.onStop();
618 }
619
620 @Override
621 public boolean onTouch(View v, MotionEvent event) {
622 if (event.getAction() == MotionEvent.ACTION_DOWN && v == mVideoPreview) {
623 // added a margin on the left to avoid interfering with gesture to open navigation drawer
624 if (event.getX() / Resources.getSystem().getDisplayMetrics().density > 24.0) {
625 startFullScreenVideo();
626 }
627 return true;
628 }
629 return false;
630 }
631
632
633 private void startFullScreenVideo() {
634 Intent i = new Intent(getActivity(), PreviewVideoActivity.class);
635 i.putExtra(FileActivity.EXTRA_ACCOUNT, mAccount);
636 i.putExtra(FileActivity.EXTRA_FILE, getFile());
637 i.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, mVideoPreview.isPlaying());
638 mVideoPreview.pause();
639 i.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPreview.getCurrentPosition());
640 startActivityForResult(i, 0);
641 }
642
643 @Override
644 public void onConfigurationChanged(Configuration newConfig) {
645 Log_OC.e(TAG, "onConfigurationChanged " + this);
646 }
647
648 @Override
649 public void onActivityResult(int requestCode, int resultCode, Intent data) {
650 Log_OC.e(TAG, "onActivityResult " + this);
651 super.onActivityResult(requestCode, resultCode, data);
652 if (resultCode == Activity.RESULT_OK) {
653 mSavedPlaybackPosition = data.getExtras().getInt(
654 PreviewVideoActivity.EXTRA_START_POSITION);
655 mAutoplay = data.getExtras().getBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY);
656 }
657 }
658
659
660 private void playAudio() {
661 OCFile file = getFile();
662 if (!mMediaServiceBinder.isPlaying(file)) {
663 Log_OC.d(TAG, "starting playback of " + file.getStoragePath());
664 mMediaServiceBinder.start(mAccount, file, mAutoplay, mSavedPlaybackPosition);
665
666 }
667 else {
668 if (!mMediaServiceBinder.isPlaying() && mAutoplay) {
669 mMediaServiceBinder.start();
670 mMediaController.updatePausePlay();
671 }
672 }
673 }
674
675
676 private void bindMediaService() {
677 Log_OC.d(TAG, "Binding to MediaService...");
678 if (mMediaServiceConnection == null) {
679 mMediaServiceConnection = new MediaServiceConnection();
680 }
681 getActivity().bindService( new Intent(getActivity(),
682 MediaService.class),
683 mMediaServiceConnection,
684 Context.BIND_AUTO_CREATE);
685 // follow the flow in MediaServiceConnection#onServiceConnected(...)
686 }
687
688 /** Defines callbacks for service binding, passed to bindService() */
689 private class MediaServiceConnection implements ServiceConnection {
690
691 @Override
692 public void onServiceConnected(ComponentName component, IBinder service) {
693 if (getActivity() != null) {
694 if (component.equals(
695 new ComponentName(getActivity(), MediaService.class))) {
696 Log_OC.d(TAG, "Media service connected");
697 mMediaServiceBinder = (MediaServiceBinder) service;
698 if (mMediaServiceBinder != null) {
699 prepareMediaController();
700 playAudio(); // do not wait for the touch of nobody to play audio
701
702 Log_OC.d(TAG, "Successfully bound to MediaService, MediaController ready");
703
704 }
705 else {
706 Log_OC.e(TAG, "Unexpected response from MediaService while binding");
707 }
708 }
709 }
710 }
711
712 private void prepareMediaController() {
713 mMediaServiceBinder.registerMediaController(mMediaController);
714 if (mMediaController != null) {
715 mMediaController.setMediaPlayer(mMediaServiceBinder);
716 mMediaController.setEnabled(true);
717 mMediaController.updatePausePlay();
718 }
719 }
720
721 @Override
722 public void onServiceDisconnected(ComponentName component) {
723 if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
724 Log_OC.e(TAG, "Media service suddenly disconnected");
725 if (mMediaController != null) {
726 mMediaController.setMediaPlayer(null);
727 }
728 else {
729 Toast.makeText(
730 getActivity(),
731 "No media controller to release when disconnected from media service",
732 Toast.LENGTH_SHORT).show();
733 }
734 mMediaServiceBinder = null;
735 mMediaServiceConnection = null;
736 }
737 }
738 }
739
740
741 /**
742 * Opens the previewed file with an external application.
743 */
744 private void openFile() {
745 stopPreview(true);
746 mContainerActivity.getFileOperationsHelper().openFile(getFile());
747 finish();
748 }
749
750 /**
751 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
752 * to be previewed.
753 *
754 * @param file File to test if can be previewed.
755 * @return 'True' if the file can be handled by the fragment.
756 */
757 public static boolean canBePreviewed(OCFile file) {
758 return (file != null && (file.isAudio() || file.isVideo()));
759 }
760
761
762 public void stopPreview(boolean stopAudio) {
763 OCFile file = getFile();
764 if (file.isAudio() && stopAudio) {
765 mMediaServiceBinder.pause();
766
767 }
768 else {
769 if (file.isVideo()) {
770 mVideoPreview.stopPlayback();
771 }
772 }
773 }
774
775
776 /**
777 * Finishes the preview
778 */
779 private void finish() {
780 getActivity().onBackPressed();
781 }
782
783
784 public int getPosition() {
785 if (mPrepared) {
786 mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
787 }
788 Log_OC.e(TAG, "getting position: " + mSavedPlaybackPosition);
789 return mSavedPlaybackPosition;
790 }
791
792 public boolean isPlaying() {
793 if (mPrepared) {
794 mAutoplay = mVideoPreview.isPlaying();
795 }
796 return mAutoplay;
797 }
798
799 }