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