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