1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package com
.owncloud
.android
.ui
.preview
;
19 import android
.accounts
.Account
;
20 import android
.app
.Activity
;
21 import android
.app
.AlertDialog
;
22 import android
.content
.ComponentName
;
23 import android
.content
.Context
;
24 import android
.content
.DialogInterface
;
25 import android
.content
.Intent
;
26 import android
.content
.ServiceConnection
;
27 import android
.content
.res
.Configuration
;
28 import android
.media
.MediaPlayer
;
29 import android
.media
.MediaPlayer
.OnCompletionListener
;
30 import android
.media
.MediaPlayer
.OnErrorListener
;
31 import android
.media
.MediaPlayer
.OnPreparedListener
;
32 import android
.os
.Build
;
33 import android
.os
.Bundle
;
34 import android
.os
.IBinder
;
35 import android
.view
.LayoutInflater
;
36 import android
.view
.MotionEvent
;
37 import android
.view
.View
;
38 import android
.view
.View
.OnTouchListener
;
39 import android
.view
.ViewGroup
;
40 import android
.widget
.ImageView
;
41 import android
.widget
.Toast
;
42 import android
.widget
.VideoView
;
44 import com
.actionbarsherlock
.view
.Menu
;
45 import com
.actionbarsherlock
.view
.MenuInflater
;
46 import com
.actionbarsherlock
.view
.MenuItem
;
47 import com
.owncloud
.android
.R
;
48 import com
.owncloud
.android
.datamodel
.OCFile
;
49 import com
.owncloud
.android
.files
.FileMenuFilter
;
50 import com
.owncloud
.android
.media
.MediaControlView
;
51 import com
.owncloud
.android
.media
.MediaService
;
52 import com
.owncloud
.android
.media
.MediaServiceBinder
;
53 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
54 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
55 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
56 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
57 import com
.owncloud
.android
.utils
.Log_OC
;
61 * This fragment shows a preview of a downloaded media file (audio or video).
63 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will produce an {@link IllegalStateException}.
65 * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too.
67 * @author David A. Velasco
69 public class PreviewMediaFragment
extends FileFragment
implements
72 public static final String EXTRA_FILE
= "FILE";
73 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
74 private static final String EXTRA_PLAY_POSITION
= "PLAY_POSITION";
75 private static final String EXTRA_PLAYING
= "PLAYING";
78 private Account mAccount
;
79 private ImageView mImagePreview
;
80 private VideoView mVideoPreview
;
81 private int mSavedPlaybackPosition
;
83 private MediaServiceBinder mMediaServiceBinder
= null
;
84 private MediaControlView mMediaController
= null
;
85 private MediaServiceConnection mMediaServiceConnection
= null
;
86 private VideoHelper mVideoHelper
;
87 private boolean mAutoplay
;
88 public boolean mPrepared
;
90 private static final String TAG
= PreviewMediaFragment
.class.getSimpleName();
94 * Creates a fragment to preview a file.
96 * When 'fileToDetail' or 'ocAccount' are null
98 * @param fileToDetail An {@link OCFile} to preview in the fragment
99 * @param ocAccount An ownCloud account; needed to start downloads
101 public PreviewMediaFragment(
104 int startPlaybackPosition
,
108 mAccount
= ocAccount
;
109 mSavedPlaybackPosition
= startPlaybackPosition
;
110 mAutoplay
= autoplay
;
115 * Creates an empty fragment for previews.
117 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
118 * (for instance, when the device is turned a aside).
120 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
123 public PreviewMediaFragment() {
126 mSavedPlaybackPosition
= 0;
135 public void onCreate(Bundle savedInstanceState
) {
136 super.onCreate(savedInstanceState
);
137 setHasOptionsMenu(true
);
145 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
146 Bundle savedInstanceState
) {
147 super.onCreateView(inflater
, container
, savedInstanceState
);
148 Log_OC
.e(TAG
, "onCreateView");
151 mView
= inflater
.inflate(R
.layout
.file_preview
, container
, false
);
153 mImagePreview
= (ImageView
)mView
.findViewById(R
.id
.image_preview
);
154 mVideoPreview
= (VideoView
)mView
.findViewById(R
.id
.video_preview
);
155 mVideoPreview
.setOnTouchListener(this);
157 mMediaController
= (MediaControlView
)mView
.findViewById(R
.id
.media_controller
);
167 public void onActivityCreated(Bundle savedInstanceState
) {
168 super.onActivityCreated(savedInstanceState
);
169 Log_OC
.e(TAG
, "onActivityCreated");
171 OCFile file
= getFile();
172 if (savedInstanceState
== null
) {
174 throw new IllegalStateException("Instanced with a NULL OCFile");
176 if (mAccount
== null
) {
177 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
179 if (!file
.isDown()) {
180 throw new IllegalStateException("There is no local file to preview");
184 file
= (OCFile
)savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_FILE
);
186 mAccount
= savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
);
187 mSavedPlaybackPosition
=
188 savedInstanceState
.getInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
);
189 mAutoplay
= savedInstanceState
.getBoolean(PreviewMediaFragment
.EXTRA_PLAYING
);
192 if (file
!= null
&& file
.isDown()) {
193 if (file
.isVideo()) {
194 mVideoPreview
.setVisibility(View
.VISIBLE
);
195 mImagePreview
.setVisibility(View
.GONE
);
199 mVideoPreview
.setVisibility(View
.GONE
);
200 mImagePreview
.setVisibility(View
.VISIBLE
);
211 public void onSaveInstanceState(Bundle outState
) {
212 super.onSaveInstanceState(outState
);
213 Log_OC
.e(TAG
, "onSaveInstanceState");
215 outState
.putParcelable(PreviewMediaFragment
.EXTRA_FILE
, getFile());
216 outState
.putParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
, mAccount
);
218 if (getFile().isVideo()) {
219 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
220 mAutoplay
= mVideoPreview
.isPlaying();
221 outState
.putInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
, mSavedPlaybackPosition
);
222 outState
.putBoolean(PreviewMediaFragment
.EXTRA_PLAYING
, mAutoplay
);
225 PreviewMediaFragment
.EXTRA_PLAY_POSITION
,
226 mMediaServiceBinder
.getCurrentPosition());
228 PreviewMediaFragment
.EXTRA_PLAYING
, mMediaServiceBinder
.isPlaying());
234 public void onStart() {
236 Log_OC
.e(TAG
, "onStart");
238 OCFile file
= getFile();
239 if (file
!= null
&& file
.isDown()) {
240 if (file
.isAudio()) {
243 } else if (file
.isVideo()) {
251 private void stopAudio() {
252 Intent i
= new Intent(getSherlockActivity(), MediaService
.class);
253 i
.setAction(MediaService
.ACTION_STOP_ALL
);
254 getSherlockActivity().startService(i
);
262 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
263 super.onCreateOptionsMenu(menu
, inflater
);
264 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
272 public void onPrepareOptionsMenu(Menu menu
) {
273 super.onPrepareOptionsMenu(menu
);
275 if (mContainerActivity
.getStorageManager() != null
) {
276 FileMenuFilter mf
= new FileMenuFilter(
278 mContainerActivity
.getStorageManager().getAccount(),
280 getSherlockActivity()
285 // additional restriction for this fragment
286 // TODO allow renaming in PreviewImageFragment
287 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
289 item
.setVisible(false
);
290 item
.setEnabled(false
);
299 public boolean onOptionsItemSelected(MenuItem item
) {
300 switch (item
.getItemId()) {
301 case R
.id
.action_share_file
: {
303 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
306 case R
.id
.action_unshare_file
: {
308 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
311 case R
.id
.action_open_file_with
: {
315 case R
.id
.action_remove_file
: {
316 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
317 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
320 case R
.id
.action_see_details
: {
324 case R
.id
.action_send_file
: {
328 case R
.id
.action_sync_file
: {
329 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
341 * Update the file of the fragment with file value
344 public void updateFile(OCFile file
){
348 private void sendFile() {
350 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
354 private void seeDetails() {
356 mContainerActivity
.showDetails(getFile());
360 private void prepareVideo() {
361 // create helper to get more control on the playback
362 mVideoHelper
= new VideoHelper();
363 mVideoPreview
.setOnPreparedListener(mVideoHelper
);
364 mVideoPreview
.setOnCompletionListener(mVideoHelper
);
365 mVideoPreview
.setOnErrorListener(mVideoHelper
);
368 private void playVideo() {
369 // create and prepare control panel for the user
370 mMediaController
.setMediaPlayer(mVideoPreview
);
372 // load the video file in the video player ;
373 // when done, VideoHelper#onPrepared() will be called
374 mVideoPreview
.setVideoPath(getFile().getStoragePath());
378 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
381 * Called when the file is ready to be played.
383 * Just starts the playback.
385 * @param mp {@link MediaPlayer} instance performing the playback.
388 public void onPrepared(MediaPlayer vp
) {
389 Log_OC
.e(TAG
, "onPrepared");
390 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
392 mVideoPreview
.start();
394 mMediaController
.setEnabled(true
);
395 mMediaController
.updatePausePlay();
401 * Called when the file is finished playing.
403 * Finishes the activity.
405 * @param mp {@link MediaPlayer} instance performing the playback.
408 public void onCompletion(MediaPlayer mp
) {
409 Log_OC
.e(TAG
, "completed");
411 mVideoPreview
.seekTo(0);
412 // next lines are necessary to work around undesired video loops
413 if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD
) {
414 mVideoPreview
.pause();
416 } else if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
417 // mVideePreview.pause() is not enough
419 mMediaController
.setEnabled(false
);
420 mVideoPreview
.stopPlayback();
422 mSavedPlaybackPosition
= 0;
423 mVideoPreview
.setVideoPath(getFile().getStoragePath());
425 } // else : called from onError()
426 mMediaController
.updatePausePlay();
431 * Called when an error in playback occurs.
433 * @param mp {@link MediaPlayer} instance performing the playback.
434 * @param what Type of error
435 * @param extra Extra code specific to the error
438 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
439 if (mVideoPreview
.getWindowToken() != null
) {
440 String message
= MediaService
.getMessageForMediaError(
441 getSherlockActivity(), what
, extra
);
442 new AlertDialog
.Builder(getSherlockActivity())
444 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
445 new DialogInterface
.OnClickListener() {
446 public void onClick(DialogInterface dialog
, int whichButton
) {
448 VideoHelper
.this.onCompletion(null
);
451 .setCancelable(false
)
461 public void onPause() {
462 Log_OC
.e(TAG
, "onPause");
467 public void onResume() {
469 Log_OC
.e(TAG
, "onResume");
473 public void onDestroy() {
474 Log_OC
.e(TAG
, "onDestroy");
479 public void onStop() {
480 Log_OC
.e(TAG
, "onStop");
483 if (mMediaServiceConnection
!= null
) {
484 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
485 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
486 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
488 getSherlockActivity().unbindService(mMediaServiceConnection
);
489 mMediaServiceConnection
= null
;
490 mMediaServiceBinder
= null
;
497 public boolean onTouch(View v
, MotionEvent event
) {
498 if (event
.getAction() == MotionEvent
.ACTION_DOWN
&& v
== mVideoPreview
) {
499 startFullScreenVideo();
506 private void startFullScreenVideo() {
507 Intent i
= new Intent(getSherlockActivity(), PreviewVideoActivity
.class);
508 i
.putExtra(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
509 i
.putExtra(FileActivity
.EXTRA_FILE
, getFile());
510 i
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPreview
.isPlaying());
511 mVideoPreview
.pause();
512 i
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPreview
.getCurrentPosition());
513 startActivityForResult(i
, 0);
517 public void onConfigurationChanged (Configuration newConfig
) {
518 Log_OC
.e(TAG
, "onConfigurationChanged " + this);
522 public void onActivityResult (int requestCode
, int resultCode
, Intent data
) {
523 Log_OC
.e(TAG
, "onActivityResult " + this);
524 super.onActivityResult(requestCode
, resultCode
, data
);
525 if (resultCode
== Activity
.RESULT_OK
) {
526 mSavedPlaybackPosition
= data
.getExtras().getInt(
527 PreviewVideoActivity
.EXTRA_START_POSITION
);
528 mAutoplay
= data
.getExtras().getBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
);
533 private void playAudio() {
534 OCFile file
= getFile();
535 if (!mMediaServiceBinder
.isPlaying(file
)) {
536 Log_OC
.d(TAG
, "starting playback of " + file
.getStoragePath());
537 mMediaServiceBinder
.start(mAccount
, file
, mAutoplay
, mSavedPlaybackPosition
);
540 if (!mMediaServiceBinder
.isPlaying() && mAutoplay
) {
541 mMediaServiceBinder
.start();
542 mMediaController
.updatePausePlay();
548 private void bindMediaService() {
549 Log_OC
.d(TAG
, "Binding to MediaService...");
550 if (mMediaServiceConnection
== null
) {
551 mMediaServiceConnection
= new MediaServiceConnection();
553 getSherlockActivity().bindService( new Intent(getSherlockActivity(),
555 mMediaServiceConnection
,
556 Context
.BIND_AUTO_CREATE
);
557 // follow the flow in MediaServiceConnection#onServiceConnected(...)
560 /** Defines callbacks for service binding, passed to bindService() */
561 private class MediaServiceConnection
implements ServiceConnection
{
564 public void onServiceConnected(ComponentName component
, IBinder service
) {
565 if (getSherlockActivity() != null
) {
566 if (component
.equals(
567 new ComponentName(getSherlockActivity(), MediaService
.class))) {
568 Log_OC
.d(TAG
, "Media service connected");
569 mMediaServiceBinder
= (MediaServiceBinder
) service
;
570 if (mMediaServiceBinder
!= null
) {
571 prepareMediaController();
572 playAudio(); // do not wait for the touch of nobody to play audio
574 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
577 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
583 private void prepareMediaController() {
584 mMediaServiceBinder
.registerMediaController(mMediaController
);
585 if (mMediaController
!= null
) {
586 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
587 mMediaController
.setEnabled(true
);
588 mMediaController
.updatePausePlay();
593 public void onServiceDisconnected(ComponentName component
) {
594 if (component
.equals(new ComponentName(getSherlockActivity(), MediaService
.class))) {
595 Log_OC
.e(TAG
, "Media service suddenly disconnected");
596 if (mMediaController
!= null
) {
597 mMediaController
.setMediaPlayer(null
);
600 getSherlockActivity(),
601 "No media controller to release when disconnected from media service",
602 Toast
.LENGTH_SHORT
).show();
604 mMediaServiceBinder
= null
;
605 mMediaServiceConnection
= null
;
613 * Opens the previewed file with an external application.
615 private void openFile() {
617 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
622 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
625 * @param file File to test if can be previewed.
626 * @return 'True' if the file can be handled by the fragment.
628 public static boolean canBePreviewed(OCFile file
) {
629 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
633 public void stopPreview(boolean stopAudio
) {
634 OCFile file
= getFile();
635 if (file
.isAudio() && stopAudio
) {
636 mMediaServiceBinder
.pause();
638 } else if (file
.isVideo()) {
639 mVideoPreview
.stopPlayback();
646 * Finishes the preview
648 private void finish() {
649 getSherlockActivity().onBackPressed();
653 public int getPosition() {
655 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
657 Log_OC
.e(TAG
, "getting position: " + mSavedPlaybackPosition
);
658 return mSavedPlaybackPosition
;
661 public boolean isPlaying() {
663 mAutoplay
= mVideoPreview
.isPlaying();