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