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
.net
.Uri
;
33 import android
.os
.Build
;
34 import android
.os
.Bundle
;
35 import android
.os
.IBinder
;
36 import android
.view
.LayoutInflater
;
37 import android
.view
.MotionEvent
;
38 import android
.view
.View
;
39 import android
.view
.View
.OnTouchListener
;
40 import android
.view
.ViewGroup
;
41 import android
.widget
.ImageView
;
42 import android
.widget
.Toast
;
43 import android
.widget
.VideoView
;
45 import com
.actionbarsherlock
.view
.Menu
;
46 import com
.actionbarsherlock
.view
.MenuInflater
;
47 import com
.actionbarsherlock
.view
.MenuItem
;
48 import com
.owncloud
.android
.R
;
49 import com
.owncloud
.android
.datamodel
.OCFile
;
50 import com
.owncloud
.android
.files
.FileMenuFilter
;
51 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
52 import com
.owncloud
.android
.media
.MediaControlView
;
53 import com
.owncloud
.android
.media
.MediaService
;
54 import com
.owncloud
.android
.media
.MediaServiceBinder
;
55 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
56 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
57 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
58 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
62 * This fragment shows a preview of a downloaded media file (audio or video).
64 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will produce an {@link IllegalStateException}.
66 * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is generated on instantiation too.
68 * @author David A. Velasco
70 public class PreviewMediaFragment
extends FileFragment
implements
73 public static final String EXTRA_FILE
= "FILE";
74 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
75 private static final String EXTRA_PLAY_POSITION
= "PLAY_POSITION";
76 private static final String EXTRA_PLAYING
= "PLAYING";
79 private Account mAccount
;
80 private ImageView mImagePreview
;
81 private VideoView mVideoPreview
;
82 private int mSavedPlaybackPosition
;
84 private MediaServiceBinder mMediaServiceBinder
= null
;
85 private MediaControlView mMediaController
= null
;
86 private MediaServiceConnection mMediaServiceConnection
= null
;
87 private VideoHelper mVideoHelper
;
88 private boolean mAutoplay
;
89 public boolean mPrepared
;
91 private static final String TAG
= PreviewMediaFragment
.class.getSimpleName();
95 * Creates a fragment to preview a file.
97 * When 'fileToDetail' or 'ocAccount' are null
99 * @param fileToDetail An {@link OCFile} to preview in the fragment
100 * @param ocAccount An ownCloud account; needed to start downloads
102 public PreviewMediaFragment(
105 int startPlaybackPosition
,
109 mAccount
= ocAccount
;
110 mSavedPlaybackPosition
= startPlaybackPosition
;
111 mAutoplay
= autoplay
;
116 * Creates an empty fragment for previews.
118 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
119 * (for instance, when the device is turned a aside).
121 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
124 public PreviewMediaFragment() {
127 mSavedPlaybackPosition
= 0;
136 public void onCreate(Bundle savedInstanceState
) {
137 super.onCreate(savedInstanceState
);
138 setHasOptionsMenu(true
);
146 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
147 Bundle savedInstanceState
) {
148 super.onCreateView(inflater
, container
, savedInstanceState
);
149 Log_OC
.e(TAG
, "onCreateView");
152 mView
= inflater
.inflate(R
.layout
.file_preview
, container
, false
);
154 mImagePreview
= (ImageView
)mView
.findViewById(R
.id
.image_preview
);
155 mVideoPreview
= (VideoView
)mView
.findViewById(R
.id
.video_preview
);
156 mVideoPreview
.setOnTouchListener(this);
158 mMediaController
= (MediaControlView
)mView
.findViewById(R
.id
.media_controller
);
168 public void onActivityCreated(Bundle savedInstanceState
) {
169 super.onActivityCreated(savedInstanceState
);
170 Log_OC
.e(TAG
, "onActivityCreated");
172 OCFile file
= getFile();
173 if (savedInstanceState
== null
) {
175 throw new IllegalStateException("Instanced with a NULL OCFile");
177 if (mAccount
== null
) {
178 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
180 if (!file
.isDown()) {
181 throw new IllegalStateException("There is no local file to preview");
185 file
= (OCFile
)savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_FILE
);
187 mAccount
= savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
);
188 mSavedPlaybackPosition
=
189 savedInstanceState
.getInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
);
190 mAutoplay
= savedInstanceState
.getBoolean(PreviewMediaFragment
.EXTRA_PLAYING
);
193 if (file
!= null
&& file
.isDown()) {
194 if (file
.isVideo()) {
195 mVideoPreview
.setVisibility(View
.VISIBLE
);
196 mImagePreview
.setVisibility(View
.GONE
);
200 mVideoPreview
.setVisibility(View
.GONE
);
201 mImagePreview
.setVisibility(View
.VISIBLE
);
212 public void onSaveInstanceState(Bundle outState
) {
213 super.onSaveInstanceState(outState
);
214 Log_OC
.e(TAG
, "onSaveInstanceState");
216 outState
.putParcelable(PreviewMediaFragment
.EXTRA_FILE
, getFile());
217 outState
.putParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
, mAccount
);
219 if (getFile().isVideo()) {
220 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
221 mAutoplay
= mVideoPreview
.isPlaying();
222 outState
.putInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
, mSavedPlaybackPosition
);
223 outState
.putBoolean(PreviewMediaFragment
.EXTRA_PLAYING
, mAutoplay
);
226 PreviewMediaFragment
.EXTRA_PLAY_POSITION
,
227 mMediaServiceBinder
.getCurrentPosition());
229 PreviewMediaFragment
.EXTRA_PLAYING
, mMediaServiceBinder
.isPlaying());
235 public void onStart() {
237 Log_OC
.e(TAG
, "onStart");
239 OCFile file
= getFile();
240 if (file
!= null
&& file
.isDown()) {
241 if (file
.isAudio()) {
244 } else if (file
.isVideo()) {
252 private void stopAudio() {
253 Intent i
= new Intent(getSherlockActivity(), MediaService
.class);
254 i
.setAction(MediaService
.ACTION_STOP_ALL
);
255 getSherlockActivity().startService(i
);
263 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
264 super.onCreateOptionsMenu(menu
, inflater
);
265 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
273 public void onPrepareOptionsMenu(Menu menu
) {
274 super.onPrepareOptionsMenu(menu
);
276 if (mContainerActivity
.getStorageManager() != null
) {
277 FileMenuFilter mf
= new FileMenuFilter(
279 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
);
293 // additional restriction for this fragment
294 item
= menu
.findItem(R
.id
.action_move
);
296 item
.setVisible(false
);
297 item
.setEnabled(false
);
306 public boolean onOptionsItemSelected(MenuItem item
) {
307 switch (item
.getItemId()) {
308 case R
.id
.action_share_file
: {
310 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
313 case R
.id
.action_unshare_file
: {
315 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
318 case R
.id
.action_open_file_with
: {
322 case R
.id
.action_remove_file
: {
323 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
324 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
327 case R
.id
.action_see_details
: {
331 case R
.id
.action_send_file
: {
335 case R
.id
.action_sync_file
: {
336 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
348 * Update the file of the fragment with file value
351 public void updateFile(OCFile file
){
355 private void sendFile() {
357 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
361 private void seeDetails() {
363 mContainerActivity
.showDetails(getFile());
367 private void prepareVideo() {
368 // create helper to get more control on the playback
369 mVideoHelper
= new VideoHelper();
370 mVideoPreview
.setOnPreparedListener(mVideoHelper
);
371 mVideoPreview
.setOnCompletionListener(mVideoHelper
);
372 mVideoPreview
.setOnErrorListener(mVideoHelper
);
375 @SuppressWarnings("static-access")
376 private void playVideo() {
377 // create and prepare control panel for the user
378 mMediaController
.setMediaPlayer(mVideoPreview
);
380 // load the video file in the video player ;
381 // when done, VideoHelper#onPrepared() will be called
382 Uri uri
= Uri
.parse(getFile().getStoragePath());
383 mVideoPreview
.setVideoPath(uri
.encode(getFile().getStoragePath()));
387 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
390 * Called when the file is ready to be played.
392 * Just starts the playback.
394 * @param mp {@link MediaPlayer} instance performing the playback.
397 public void onPrepared(MediaPlayer vp
) {
398 Log_OC
.e(TAG
, "onPrepared");
399 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
401 mVideoPreview
.start();
403 mMediaController
.setEnabled(true
);
404 mMediaController
.updatePausePlay();
410 * Called when the file is finished playing.
412 * Finishes the activity.
414 * @param mp {@link MediaPlayer} instance performing the playback.
417 public void onCompletion(MediaPlayer mp
) {
418 Log_OC
.e(TAG
, "completed");
420 mVideoPreview
.seekTo(0);
421 // next lines are necessary to work around undesired video loops
422 if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD
) {
423 mVideoPreview
.pause();
425 } else if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
426 // mVideePreview.pause() is not enough
428 mMediaController
.setEnabled(false
);
429 mVideoPreview
.stopPlayback();
431 mSavedPlaybackPosition
= 0;
432 mVideoPreview
.setVideoPath(getFile().getStoragePath());
434 } // else : called from onError()
435 mMediaController
.updatePausePlay();
440 * Called when an error in playback occurs.
442 * @param mp {@link MediaPlayer} instance performing the playback.
443 * @param what Type of error
444 * @param extra Extra code specific to the error
447 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
448 if (mVideoPreview
.getWindowToken() != null
) {
449 String message
= MediaService
.getMessageForMediaError(
450 getSherlockActivity(), what
, extra
);
451 new AlertDialog
.Builder(getSherlockActivity())
453 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
454 new DialogInterface
.OnClickListener() {
455 public void onClick(DialogInterface dialog
, int whichButton
) {
457 VideoHelper
.this.onCompletion(null
);
460 .setCancelable(false
)
470 public void onPause() {
471 Log_OC
.e(TAG
, "onPause");
476 public void onResume() {
478 Log_OC
.e(TAG
, "onResume");
482 public void onDestroy() {
483 Log_OC
.e(TAG
, "onDestroy");
488 public void onStop() {
489 Log_OC
.e(TAG
, "onStop");
492 if (mMediaServiceConnection
!= null
) {
493 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
494 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
495 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
497 getSherlockActivity().unbindService(mMediaServiceConnection
);
498 mMediaServiceConnection
= null
;
499 mMediaServiceBinder
= null
;
506 public boolean onTouch(View v
, MotionEvent event
) {
507 if (event
.getAction() == MotionEvent
.ACTION_DOWN
&& v
== mVideoPreview
) {
508 startFullScreenVideo();
515 private void startFullScreenVideo() {
516 Intent i
= new Intent(getSherlockActivity(), PreviewVideoActivity
.class);
517 i
.putExtra(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
518 i
.putExtra(FileActivity
.EXTRA_FILE
, getFile());
519 i
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPreview
.isPlaying());
520 mVideoPreview
.pause();
521 i
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPreview
.getCurrentPosition());
522 startActivityForResult(i
, 0);
526 public void onConfigurationChanged (Configuration newConfig
) {
527 Log_OC
.e(TAG
, "onConfigurationChanged " + this);
531 public void onActivityResult (int requestCode
, int resultCode
, Intent data
) {
532 Log_OC
.e(TAG
, "onActivityResult " + this);
533 super.onActivityResult(requestCode
, resultCode
, data
);
534 if (resultCode
== Activity
.RESULT_OK
) {
535 mSavedPlaybackPosition
= data
.getExtras().getInt(
536 PreviewVideoActivity
.EXTRA_START_POSITION
);
537 mAutoplay
= data
.getExtras().getBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
);
542 private void playAudio() {
543 OCFile file
= getFile();
544 if (!mMediaServiceBinder
.isPlaying(file
)) {
545 Log_OC
.d(TAG
, "starting playback of " + file
.getStoragePath());
546 mMediaServiceBinder
.start(mAccount
, file
, mAutoplay
, mSavedPlaybackPosition
);
549 if (!mMediaServiceBinder
.isPlaying() && mAutoplay
) {
550 mMediaServiceBinder
.start();
551 mMediaController
.updatePausePlay();
557 private void bindMediaService() {
558 Log_OC
.d(TAG
, "Binding to MediaService...");
559 if (mMediaServiceConnection
== null
) {
560 mMediaServiceConnection
= new MediaServiceConnection();
562 getSherlockActivity().bindService( new Intent(getSherlockActivity(),
564 mMediaServiceConnection
,
565 Context
.BIND_AUTO_CREATE
);
566 // follow the flow in MediaServiceConnection#onServiceConnected(...)
569 /** Defines callbacks for service binding, passed to bindService() */
570 private class MediaServiceConnection
implements ServiceConnection
{
573 public void onServiceConnected(ComponentName component
, IBinder service
) {
574 if (getSherlockActivity() != null
) {
575 if (component
.equals(
576 new ComponentName(getSherlockActivity(), MediaService
.class))) {
577 Log_OC
.d(TAG
, "Media service connected");
578 mMediaServiceBinder
= (MediaServiceBinder
) service
;
579 if (mMediaServiceBinder
!= null
) {
580 prepareMediaController();
581 playAudio(); // do not wait for the touch of nobody to play audio
583 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
586 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
592 private void prepareMediaController() {
593 mMediaServiceBinder
.registerMediaController(mMediaController
);
594 if (mMediaController
!= null
) {
595 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
596 mMediaController
.setEnabled(true
);
597 mMediaController
.updatePausePlay();
602 public void onServiceDisconnected(ComponentName component
) {
603 if (component
.equals(new ComponentName(getSherlockActivity(), MediaService
.class))) {
604 Log_OC
.e(TAG
, "Media service suddenly disconnected");
605 if (mMediaController
!= null
) {
606 mMediaController
.setMediaPlayer(null
);
609 getSherlockActivity(),
610 "No media controller to release when disconnected from media service",
611 Toast
.LENGTH_SHORT
).show();
613 mMediaServiceBinder
= null
;
614 mMediaServiceConnection
= null
;
622 * Opens the previewed file with an external application.
624 private void openFile() {
626 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
631 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
634 * @param file File to test if can be previewed.
635 * @return 'True' if the file can be handled by the fragment.
637 public static boolean canBePreviewed(OCFile file
) {
638 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
642 public void stopPreview(boolean stopAudio
) {
643 OCFile file
= getFile();
644 if (file
.isAudio() && stopAudio
) {
645 mMediaServiceBinder
.pause();
647 } else if (file
.isVideo()) {
648 mVideoPreview
.stopPlayback();
655 * Finishes the preview
657 private void finish() {
658 getSherlockActivity().onBackPressed();
662 public int getPosition() {
664 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
666 Log_OC
.e(TAG
, "getting position: " + mSavedPlaybackPosition
);
667 return mSavedPlaybackPosition
;
670 public boolean isPlaying() {
672 mAutoplay
= mVideoPreview
.isPlaying();