2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
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.
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.
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/>.
20 package com
.owncloud
.android
.ui
.preview
;
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
;
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
;
66 * This fragment shows a preview of a downloaded media file (audio or video).
68 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will
69 * produce an {@link IllegalStateException}.
71 * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is
72 * generated on instantiation too.
74 public class PreviewMediaFragment
extends FileFragment
implements
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";
83 private Account mAccount
;
84 private ImageView mImagePreview
;
85 private VideoView mVideoPreview
;
86 private int mSavedPlaybackPosition
;
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
;
95 private static final String TAG
= PreviewMediaFragment
.class.getSimpleName();
99 * Creates a fragment to preview a file.
101 * When 'fileToDetail' or 'ocAccount' are null
103 * @param fileToDetail An {@link OCFile} to preview in the fragment
104 * @param ocAccount An ownCloud account; needed to start downloads
106 public PreviewMediaFragment(
109 int startPlaybackPosition
,
113 mAccount
= ocAccount
;
114 mSavedPlaybackPosition
= startPlaybackPosition
;
115 mAutoplay
= autoplay
;
120 * Creates an empty fragment for previews.
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).
125 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
128 public PreviewMediaFragment() {
131 mSavedPlaybackPosition
= 0;
140 public void onCreate(Bundle savedInstanceState
) {
141 super.onCreate(savedInstanceState
);
142 setHasOptionsMenu(true
);
150 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
151 Bundle savedInstanceState
) {
152 super.onCreateView(inflater
, container
, savedInstanceState
);
153 Log_OC
.e(TAG
, "onCreateView");
156 mView
= inflater
.inflate(R
.layout
.file_preview
, container
, false
);
158 mImagePreview
= (ImageView
) mView
.findViewById(R
.id
.image_preview
);
159 mVideoPreview
= (VideoView
) mView
.findViewById(R
.id
.video_preview
);
160 mVideoPreview
.setOnTouchListener(this);
162 mMediaController
= (MediaControlView
) mView
.findViewById(R
.id
.media_controller
);
172 public void onActivityCreated(Bundle savedInstanceState
) {
173 super.onActivityCreated(savedInstanceState
);
174 Log_OC
.e(TAG
, "onActivityCreated");
176 OCFile file
= getFile();
177 if (savedInstanceState
== null
) {
179 throw new IllegalStateException("Instanced with a NULL OCFile");
181 if (mAccount
== null
) {
182 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
184 if (!file
.isDown()) {
185 throw new IllegalStateException("There is no local file to preview");
190 file
= (OCFile
) savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_FILE
);
192 mAccount
= savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
);
193 mSavedPlaybackPosition
=
194 savedInstanceState
.getInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
);
195 mAutoplay
= savedInstanceState
.getBoolean(PreviewMediaFragment
.EXTRA_PLAYING
);
198 if (file
!= null
&& file
.isDown()) {
199 if (file
.isVideo()) {
200 mVideoPreview
.setVisibility(View
.VISIBLE
);
201 mImagePreview
.setVisibility(View
.GONE
);
206 mVideoPreview
.setVisibility(View
.GONE
);
207 mImagePreview
.setVisibility(View
.VISIBLE
);
218 public void onSaveInstanceState(Bundle outState
) {
219 super.onSaveInstanceState(outState
);
220 Log_OC
.e(TAG
, "onSaveInstanceState");
222 outState
.putParcelable(PreviewMediaFragment
.EXTRA_FILE
, getFile());
223 outState
.putParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
, mAccount
);
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
);
233 PreviewMediaFragment
.EXTRA_PLAY_POSITION
,
234 mMediaServiceBinder
.getCurrentPosition());
236 PreviewMediaFragment
.EXTRA_PLAYING
, mMediaServiceBinder
.isPlaying());
242 public void onStart() {
244 Log_OC
.e(TAG
, "onStart");
246 OCFile file
= getFile();
247 if (file
!= null
&& file
.isDown()) {
248 if (file
.isAudio()) {
253 if (file
.isVideo()) {
262 private void stopAudio() {
263 Intent i
= new Intent(getActivity(), MediaService
.class);
264 i
.setAction(MediaService
.ACTION_STOP_ALL
);
265 getActivity().startService(i
);
273 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
274 super.onCreateOptionsMenu(menu
, inflater
);
275 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
283 public void onPrepareOptionsMenu(Menu menu
) {
284 super.onPrepareOptionsMenu(menu
);
286 if (mContainerActivity
.getStorageManager() != null
) {
287 FileMenuFilter mf
= new FileMenuFilter(
289 mContainerActivity
.getStorageManager().getAccount(),
296 // additional restriction for this fragment
297 // TODO allow renaming in PreviewImageFragment
298 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
300 item
.setVisible(false
);
301 item
.setEnabled(false
);
304 // additional restriction for this fragment
305 item
= menu
.findItem(R
.id
.action_move
);
307 item
.setVisible(false
);
308 item
.setEnabled(false
);
311 // additional restriction for this fragment
312 item
= menu
.findItem(R
.id
.action_copy
);
314 item
.setVisible(false
);
315 item
.setEnabled(false
);
324 public boolean onOptionsItemSelected(MenuItem item
) {
325 switch (item
.getItemId()) {
326 case R
.id
.action_share_file
: {
328 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
331 case R
.id
.action_unshare_file
: {
333 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
336 case R
.id
.action_open_file_with
: {
340 case R
.id
.action_remove_file
: {
341 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
342 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
345 case R
.id
.action_see_details
: {
349 case R
.id
.action_send_file
: {
353 case R
.id
.action_sync_file
: {
354 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
357 case R
.id
.action_favorite_file
:{
358 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), true
);
361 case R
.id
.action_unfavorite_file
:{
362 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), false
);
372 * Update the file of the fragment with file value
376 public void updateFile(OCFile file
) {
380 private void sendFile() {
382 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
386 private void seeDetails() {
388 mContainerActivity
.showDetails(getFile());
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
);
400 @SuppressWarnings("static-access")
401 private void playVideo() {
402 // create and prepare control panel for the user
403 mMediaController
.setMediaPlayer(mVideoPreview
);
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()));
412 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
415 * Called when the file is ready to be played.
417 * Just starts the playback.
419 * @param vp {@link MediaPlayer} instance performing the playback.
422 public void onPrepared(MediaPlayer vp
) {
423 Log_OC
.e(TAG
, "onPrepared");
424 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
426 mVideoPreview
.start();
428 mMediaController
.setEnabled(true
);
429 mMediaController
.updatePausePlay();
435 * Called when the file is finished playing.
437 * Finishes the activity.
439 * @param mp {@link MediaPlayer} instance performing the playback.
442 public void onCompletion(MediaPlayer mp
) {
443 Log_OC
.e(TAG
, "completed");
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();
452 if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
453 // mVideePreview.pause() is not enough
455 mMediaController
.setEnabled(false
);
456 mVideoPreview
.stopPlayback();
458 mSavedPlaybackPosition
= 0;
459 mVideoPreview
.setVideoPath(getFile().getStoragePath());
462 } // else : called from onError()
463 mMediaController
.updatePausePlay();
468 * Called when an error in playback occurs.
470 * @param mp {@link MediaPlayer} instance performing the playback.
471 * @param what Type of error
472 * @param extra Extra code specific to the error
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())
481 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
482 new DialogInterface
.OnClickListener() {
483 public void onClick(DialogInterface dialog
, int whichButton
) {
485 VideoHelper
.this.onCompletion(null
);
488 .setCancelable(false
)
498 public void onPause() {
499 Log_OC
.e(TAG
, "onPause");
504 public void onResume() {
506 Log_OC
.e(TAG
, "onResume");
510 public void onDestroy() {
511 Log_OC
.e(TAG
, "onDestroy");
516 public void onStop() {
517 Log_OC
.e(TAG
, "onStop");
520 if (mMediaServiceConnection
!= null
) {
521 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
522 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
523 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
525 getActivity().unbindService(mMediaServiceConnection
);
526 mMediaServiceConnection
= null
;
527 mMediaServiceBinder
= null
;
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();
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);
557 public void onConfigurationChanged(Configuration newConfig
) {
558 Log_OC
.e(TAG
, "onConfigurationChanged " + this);
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
);
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
);
581 if (!mMediaServiceBinder
.isPlaying() && mAutoplay
) {
582 mMediaServiceBinder
.start();
583 mMediaController
.updatePausePlay();
589 private void bindMediaService() {
590 Log_OC
.d(TAG
, "Binding to MediaService...");
591 if (mMediaServiceConnection
== null
) {
592 mMediaServiceConnection
= new MediaServiceConnection();
594 getActivity().bindService( new Intent(getActivity(),
596 mMediaServiceConnection
,
597 Context
.BIND_AUTO_CREATE
);
598 // follow the flow in MediaServiceConnection#onServiceConnected(...)
601 /** Defines callbacks for service binding, passed to bindService() */
602 private class MediaServiceConnection
implements ServiceConnection
{
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
615 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
619 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
625 private void prepareMediaController() {
626 mMediaServiceBinder
.registerMediaController(mMediaController
);
627 if (mMediaController
!= null
) {
628 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
629 mMediaController
.setEnabled(true
);
630 mMediaController
.updatePausePlay();
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
);
644 "No media controller to release when disconnected from media service",
645 Toast
.LENGTH_SHORT
).show();
647 mMediaServiceBinder
= null
;
648 mMediaServiceConnection
= null
;
655 * Opens the previewed file with an external application.
657 private void openFile() {
659 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
664 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
667 * @param file File to test if can be previewed.
668 * @return 'True' if the file can be handled by the fragment.
670 public static boolean canBePreviewed(OCFile file
) {
671 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
675 public void stopPreview(boolean stopAudio
) {
676 OCFile file
= getFile();
677 if (file
.isAudio() && stopAudio
) {
678 mMediaServiceBinder
.pause();
682 if (file
.isVideo()) {
683 mVideoPreview
.stopPlayback();
690 * Finishes the preview
692 private void finish() {
693 getActivity().onBackPressed();
697 public int getPosition() {
699 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
701 Log_OC
.e(TAG
, "getting position: " + mSavedPlaybackPosition
);
702 return mSavedPlaybackPosition
;
705 public boolean isPlaying() {
707 mAutoplay
= mVideoPreview
.isPlaying();