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
.media
.MediaPlayer
;
32 import android
.media
.MediaPlayer
.OnCompletionListener
;
33 import android
.media
.MediaPlayer
.OnErrorListener
;
34 import android
.media
.MediaPlayer
.OnPreparedListener
;
35 import android
.net
.Uri
;
36 import android
.os
.Build
;
37 import android
.os
.Bundle
;
38 import android
.os
.IBinder
;
39 import android
.view
.LayoutInflater
;
40 import android
.view
.MotionEvent
;
41 import android
.view
.View
;
42 import android
.view
.View
.OnTouchListener
;
43 import android
.view
.ViewGroup
;
44 import android
.widget
.ImageView
;
45 import android
.widget
.Toast
;
46 import android
.widget
.VideoView
;
48 import com
.actionbarsherlock
.view
.Menu
;
49 import com
.actionbarsherlock
.view
.MenuInflater
;
50 import com
.actionbarsherlock
.view
.MenuItem
;
51 import com
.owncloud
.android
.R
;
52 import com
.owncloud
.android
.datamodel
.OCFile
;
53 import com
.owncloud
.android
.files
.FileMenuFilter
;
54 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
55 import com
.owncloud
.android
.media
.MediaControlView
;
56 import com
.owncloud
.android
.media
.MediaService
;
57 import com
.owncloud
.android
.media
.MediaServiceBinder
;
58 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
59 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
60 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
61 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
65 * This fragment shows a preview of a downloaded media file (audio or video).
67 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will produce an {@link IllegalStateException}.
69 * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too.
71 public class PreviewMediaFragment
extends FileFragment
implements
74 public static final String EXTRA_FILE
= "FILE";
75 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
76 private static final String EXTRA_PLAY_POSITION
= "PLAY_POSITION";
77 private static final String EXTRA_PLAYING
= "PLAYING";
80 private Account mAccount
;
81 private ImageView mImagePreview
;
82 private VideoView mVideoPreview
;
83 private int mSavedPlaybackPosition
;
85 private MediaServiceBinder mMediaServiceBinder
= null
;
86 private MediaControlView mMediaController
= null
;
87 private MediaServiceConnection mMediaServiceConnection
= null
;
88 private VideoHelper mVideoHelper
;
89 private boolean mAutoplay
;
90 public boolean mPrepared
;
92 private static final String TAG
= PreviewMediaFragment
.class.getSimpleName();
96 * Creates a fragment to preview a file.
98 * When 'fileToDetail' or 'ocAccount' are null
100 * @param fileToDetail An {@link OCFile} to preview in the fragment
101 * @param ocAccount An ownCloud account; needed to start downloads
103 public PreviewMediaFragment(
106 int startPlaybackPosition
,
110 mAccount
= ocAccount
;
111 mSavedPlaybackPosition
= startPlaybackPosition
;
112 mAutoplay
= autoplay
;
117 * Creates an empty fragment for previews.
119 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
120 * (for instance, when the device is turned a aside).
122 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
125 public PreviewMediaFragment() {
128 mSavedPlaybackPosition
= 0;
137 public void onCreate(Bundle savedInstanceState
) {
138 super.onCreate(savedInstanceState
);
139 setHasOptionsMenu(true
);
147 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
148 Bundle savedInstanceState
) {
149 super.onCreateView(inflater
, container
, savedInstanceState
);
150 Log_OC
.e(TAG
, "onCreateView");
153 mView
= inflater
.inflate(R
.layout
.file_preview
, container
, false
);
155 mImagePreview
= (ImageView
)mView
.findViewById(R
.id
.image_preview
);
156 mVideoPreview
= (VideoView
)mView
.findViewById(R
.id
.video_preview
);
157 mVideoPreview
.setOnTouchListener(this);
159 mMediaController
= (MediaControlView
)mView
.findViewById(R
.id
.media_controller
);
169 public void onActivityCreated(Bundle savedInstanceState
) {
170 super.onActivityCreated(savedInstanceState
);
171 Log_OC
.e(TAG
, "onActivityCreated");
173 OCFile file
= getFile();
174 if (savedInstanceState
== null
) {
176 throw new IllegalStateException("Instanced with a NULL OCFile");
178 if (mAccount
== null
) {
179 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
181 if (!file
.isDown()) {
182 throw new IllegalStateException("There is no local file to preview");
186 file
= (OCFile
)savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_FILE
);
188 mAccount
= savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
);
189 mSavedPlaybackPosition
=
190 savedInstanceState
.getInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
);
191 mAutoplay
= savedInstanceState
.getBoolean(PreviewMediaFragment
.EXTRA_PLAYING
);
194 if (file
!= null
&& file
.isDown()) {
195 if (file
.isVideo()) {
196 mVideoPreview
.setVisibility(View
.VISIBLE
);
197 mImagePreview
.setVisibility(View
.GONE
);
201 mVideoPreview
.setVisibility(View
.GONE
);
202 mImagePreview
.setVisibility(View
.VISIBLE
);
213 public void onSaveInstanceState(Bundle outState
) {
214 super.onSaveInstanceState(outState
);
215 Log_OC
.e(TAG
, "onSaveInstanceState");
217 outState
.putParcelable(PreviewMediaFragment
.EXTRA_FILE
, getFile());
218 outState
.putParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
, mAccount
);
220 if (getFile().isVideo()) {
221 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
222 mAutoplay
= mVideoPreview
.isPlaying();
223 outState
.putInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
, mSavedPlaybackPosition
);
224 outState
.putBoolean(PreviewMediaFragment
.EXTRA_PLAYING
, mAutoplay
);
227 PreviewMediaFragment
.EXTRA_PLAY_POSITION
,
228 mMediaServiceBinder
.getCurrentPosition());
230 PreviewMediaFragment
.EXTRA_PLAYING
, mMediaServiceBinder
.isPlaying());
236 public void onStart() {
238 Log_OC
.e(TAG
, "onStart");
240 OCFile file
= getFile();
241 if (file
!= null
&& file
.isDown()) {
242 if (file
.isAudio()) {
245 } else if (file
.isVideo()) {
253 private void stopAudio() {
254 Intent i
= new Intent(getSherlockActivity(), MediaService
.class);
255 i
.setAction(MediaService
.ACTION_STOP_ALL
);
256 getSherlockActivity().startService(i
);
264 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
265 super.onCreateOptionsMenu(menu
, inflater
);
266 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
274 public void onPrepareOptionsMenu(Menu menu
) {
275 super.onPrepareOptionsMenu(menu
);
277 if (mContainerActivity
.getStorageManager() != null
) {
278 FileMenuFilter mf
= new FileMenuFilter(
280 mContainerActivity
.getStorageManager().getAccount(),
282 getSherlockActivity()
287 // additional restriction for this fragment
288 // TODO allow renaming in PreviewImageFragment
289 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
291 item
.setVisible(false
);
292 item
.setEnabled(false
);
295 // additional restriction for this fragment
296 item
= menu
.findItem(R
.id
.action_move
);
298 item
.setVisible(false
);
299 item
.setEnabled(false
);
308 public boolean onOptionsItemSelected(MenuItem item
) {
309 switch (item
.getItemId()) {
310 case R
.id
.action_share_file
: {
312 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
315 case R
.id
.action_unshare_file
: {
317 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
320 case R
.id
.action_open_file_with
: {
324 case R
.id
.action_remove_file
: {
325 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
326 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
329 case R
.id
.action_see_details
: {
333 case R
.id
.action_send_file
: {
337 case R
.id
.action_sync_file
: {
338 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
350 * Update the file of the fragment with file value
353 public void updateFile(OCFile file
){
357 private void sendFile() {
359 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
363 private void seeDetails() {
365 mContainerActivity
.showDetails(getFile());
369 private void prepareVideo() {
370 // create helper to get more control on the playback
371 mVideoHelper
= new VideoHelper();
372 mVideoPreview
.setOnPreparedListener(mVideoHelper
);
373 mVideoPreview
.setOnCompletionListener(mVideoHelper
);
374 mVideoPreview
.setOnErrorListener(mVideoHelper
);
377 @SuppressWarnings("static-access")
378 private void playVideo() {
379 // create and prepare control panel for the user
380 mMediaController
.setMediaPlayer(mVideoPreview
);
382 // load the video file in the video player ;
383 // when done, VideoHelper#onPrepared() will be called
384 Uri uri
= Uri
.parse(getFile().getStoragePath());
385 mVideoPreview
.setVideoPath(uri
.encode(getFile().getStoragePath()));
389 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
392 * Called when the file is ready to be played.
394 * Just starts the playback.
396 * @param mp {@link MediaPlayer} instance performing the playback.
399 public void onPrepared(MediaPlayer vp
) {
400 Log_OC
.e(TAG
, "onPrepared");
401 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
403 mVideoPreview
.start();
405 mMediaController
.setEnabled(true
);
406 mMediaController
.updatePausePlay();
412 * Called when the file is finished playing.
414 * Finishes the activity.
416 * @param mp {@link MediaPlayer} instance performing the playback.
419 public void onCompletion(MediaPlayer mp
) {
420 Log_OC
.e(TAG
, "completed");
422 mVideoPreview
.seekTo(0);
423 // next lines are necessary to work around undesired video loops
424 if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD
) {
425 mVideoPreview
.pause();
427 } else if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
428 // mVideePreview.pause() is not enough
430 mMediaController
.setEnabled(false
);
431 mVideoPreview
.stopPlayback();
433 mSavedPlaybackPosition
= 0;
434 mVideoPreview
.setVideoPath(getFile().getStoragePath());
436 } // else : called from onError()
437 mMediaController
.updatePausePlay();
442 * Called when an error in playback occurs.
444 * @param mp {@link MediaPlayer} instance performing the playback.
445 * @param what Type of error
446 * @param extra Extra code specific to the error
449 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
450 if (mVideoPreview
.getWindowToken() != null
) {
451 String message
= MediaService
.getMessageForMediaError(
452 getSherlockActivity(), what
, extra
);
453 new AlertDialog
.Builder(getSherlockActivity())
455 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
456 new DialogInterface
.OnClickListener() {
457 public void onClick(DialogInterface dialog
, int whichButton
) {
459 VideoHelper
.this.onCompletion(null
);
462 .setCancelable(false
)
472 public void onPause() {
473 Log_OC
.e(TAG
, "onPause");
478 public void onResume() {
480 Log_OC
.e(TAG
, "onResume");
484 public void onDestroy() {
485 Log_OC
.e(TAG
, "onDestroy");
490 public void onStop() {
491 Log_OC
.e(TAG
, "onStop");
494 if (mMediaServiceConnection
!= null
) {
495 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
496 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
497 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
499 getSherlockActivity().unbindService(mMediaServiceConnection
);
500 mMediaServiceConnection
= null
;
501 mMediaServiceBinder
= null
;
508 public boolean onTouch(View v
, MotionEvent event
) {
509 if (event
.getAction() == MotionEvent
.ACTION_DOWN
&& v
== mVideoPreview
) {
510 startFullScreenVideo();
517 private void startFullScreenVideo() {
518 Intent i
= new Intent(getSherlockActivity(), PreviewVideoActivity
.class);
519 i
.putExtra(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
520 i
.putExtra(FileActivity
.EXTRA_FILE
, getFile());
521 i
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPreview
.isPlaying());
522 mVideoPreview
.pause();
523 i
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPreview
.getCurrentPosition());
524 startActivityForResult(i
, 0);
528 public void onConfigurationChanged (Configuration newConfig
) {
529 Log_OC
.e(TAG
, "onConfigurationChanged " + this);
533 public void onActivityResult (int requestCode
, int resultCode
, Intent data
) {
534 Log_OC
.e(TAG
, "onActivityResult " + this);
535 super.onActivityResult(requestCode
, resultCode
, data
);
536 if (resultCode
== Activity
.RESULT_OK
) {
537 mSavedPlaybackPosition
= data
.getExtras().getInt(
538 PreviewVideoActivity
.EXTRA_START_POSITION
);
539 mAutoplay
= data
.getExtras().getBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
);
544 private void playAudio() {
545 OCFile file
= getFile();
546 if (!mMediaServiceBinder
.isPlaying(file
)) {
547 Log_OC
.d(TAG
, "starting playback of " + file
.getStoragePath());
548 mMediaServiceBinder
.start(mAccount
, file
, mAutoplay
, mSavedPlaybackPosition
);
551 if (!mMediaServiceBinder
.isPlaying() && mAutoplay
) {
552 mMediaServiceBinder
.start();
553 mMediaController
.updatePausePlay();
559 private void bindMediaService() {
560 Log_OC
.d(TAG
, "Binding to MediaService...");
561 if (mMediaServiceConnection
== null
) {
562 mMediaServiceConnection
= new MediaServiceConnection();
564 getSherlockActivity().bindService( new Intent(getSherlockActivity(),
566 mMediaServiceConnection
,
567 Context
.BIND_AUTO_CREATE
);
568 // follow the flow in MediaServiceConnection#onServiceConnected(...)
571 /** Defines callbacks for service binding, passed to bindService() */
572 private class MediaServiceConnection
implements ServiceConnection
{
575 public void onServiceConnected(ComponentName component
, IBinder service
) {
576 if (getSherlockActivity() != null
) {
577 if (component
.equals(
578 new ComponentName(getSherlockActivity(), MediaService
.class))) {
579 Log_OC
.d(TAG
, "Media service connected");
580 mMediaServiceBinder
= (MediaServiceBinder
) service
;
581 if (mMediaServiceBinder
!= null
) {
582 prepareMediaController();
583 playAudio(); // do not wait for the touch of nobody to play audio
585 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
588 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
594 private void prepareMediaController() {
595 mMediaServiceBinder
.registerMediaController(mMediaController
);
596 if (mMediaController
!= null
) {
597 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
598 mMediaController
.setEnabled(true
);
599 mMediaController
.updatePausePlay();
604 public void onServiceDisconnected(ComponentName component
) {
605 if (component
.equals(new ComponentName(getSherlockActivity(), MediaService
.class))) {
606 Log_OC
.e(TAG
, "Media service suddenly disconnected");
607 if (mMediaController
!= null
) {
608 mMediaController
.setMediaPlayer(null
);
611 getSherlockActivity(),
612 "No media controller to release when disconnected from media service",
613 Toast
.LENGTH_SHORT
).show();
615 mMediaServiceBinder
= null
;
616 mMediaServiceConnection
= null
;
624 * Opens the previewed file with an external application.
626 private void openFile() {
628 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
633 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
636 * @param file File to test if can be previewed.
637 * @return 'True' if the file can be handled by the fragment.
639 public static boolean canBePreviewed(OCFile file
) {
640 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
644 public void stopPreview(boolean stopAudio
) {
645 OCFile file
= getFile();
646 if (file
.isAudio() && stopAudio
) {
647 mMediaServiceBinder
.pause();
649 } else if (file
.isVideo()) {
650 mVideoPreview
.stopPlayback();
657 * Finishes the preview
659 private void finish() {
660 getSherlockActivity().onBackPressed();
664 public int getPosition() {
666 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
668 Log_OC
.e(TAG
, "getting position: " + mSavedPlaybackPosition
);
669 return mSavedPlaybackPosition
;
672 public boolean isPlaying() {
674 mAutoplay
= mVideoPreview
.isPlaying();