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