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(),
281 getSherlockActivity()
286 // additional restriction for this fragment
287 // TODO allow renaming in PreviewImageFragment
288 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
290 item
.setVisible(false
);
291 item
.setEnabled(false
);
294 // additional restriction for this fragment
295 item
= menu
.findItem(R
.id
.action_move
);
297 item
.setVisible(false
);
298 item
.setEnabled(false
);
307 public boolean onOptionsItemSelected(MenuItem item
) {
308 switch (item
.getItemId()) {
309 case R
.id
.action_share_file
: {
311 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
314 case R
.id
.action_unshare_file
: {
316 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
319 case R
.id
.action_open_file_with
: {
323 case R
.id
.action_remove_file
: {
324 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
325 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
328 case R
.id
.action_see_details
: {
332 case R
.id
.action_send_file
: {
336 case R
.id
.action_sync_file
: {
337 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
349 * Update the file of the fragment with file value
352 public void updateFile(OCFile file
){
356 private void sendFile() {
358 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
362 private void seeDetails() {
364 mContainerActivity
.showDetails(getFile());
368 private void prepareVideo() {
369 // create helper to get more control on the playback
370 mVideoHelper
= new VideoHelper();
371 mVideoPreview
.setOnPreparedListener(mVideoHelper
);
372 mVideoPreview
.setOnCompletionListener(mVideoHelper
);
373 mVideoPreview
.setOnErrorListener(mVideoHelper
);
376 @SuppressWarnings("static-access")
377 private void playVideo() {
378 // create and prepare control panel for the user
379 mMediaController
.setMediaPlayer(mVideoPreview
);
381 // load the video file in the video player ;
382 // when done, VideoHelper#onPrepared() will be called
383 Uri uri
= Uri
.parse(getFile().getStoragePath());
384 mVideoPreview
.setVideoPath(uri
.encode(getFile().getStoragePath()));
388 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
391 * Called when the file is ready to be played.
393 * Just starts the playback.
395 * @param mp {@link MediaPlayer} instance performing the playback.
398 public void onPrepared(MediaPlayer vp
) {
399 Log_OC
.e(TAG
, "onPrepared");
400 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
402 mVideoPreview
.start();
404 mMediaController
.setEnabled(true
);
405 mMediaController
.updatePausePlay();
411 * Called when the file is finished playing.
413 * Finishes the activity.
415 * @param mp {@link MediaPlayer} instance performing the playback.
418 public void onCompletion(MediaPlayer mp
) {
419 Log_OC
.e(TAG
, "completed");
421 mVideoPreview
.seekTo(0);
422 // next lines are necessary to work around undesired video loops
423 if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD
) {
424 mVideoPreview
.pause();
426 } else if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
427 // mVideePreview.pause() is not enough
429 mMediaController
.setEnabled(false
);
430 mVideoPreview
.stopPlayback();
432 mSavedPlaybackPosition
= 0;
433 mVideoPreview
.setVideoPath(getFile().getStoragePath());
435 } // else : called from onError()
436 mMediaController
.updatePausePlay();
441 * Called when an error in playback occurs.
443 * @param mp {@link MediaPlayer} instance performing the playback.
444 * @param what Type of error
445 * @param extra Extra code specific to the error
448 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
449 if (mVideoPreview
.getWindowToken() != null
) {
450 String message
= MediaService
.getMessageForMediaError(
451 getSherlockActivity(), what
, extra
);
452 new AlertDialog
.Builder(getSherlockActivity())
454 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
455 new DialogInterface
.OnClickListener() {
456 public void onClick(DialogInterface dialog
, int whichButton
) {
458 VideoHelper
.this.onCompletion(null
);
461 .setCancelable(false
)
471 public void onPause() {
472 Log_OC
.e(TAG
, "onPause");
477 public void onResume() {
479 Log_OC
.e(TAG
, "onResume");
483 public void onDestroy() {
484 Log_OC
.e(TAG
, "onDestroy");
489 public void onStop() {
490 Log_OC
.e(TAG
, "onStop");
493 if (mMediaServiceConnection
!= null
) {
494 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
495 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
496 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
498 getSherlockActivity().unbindService(mMediaServiceConnection
);
499 mMediaServiceConnection
= null
;
500 mMediaServiceBinder
= null
;
507 public boolean onTouch(View v
, MotionEvent event
) {
508 if (event
.getAction() == MotionEvent
.ACTION_DOWN
&& v
== mVideoPreview
) {
509 startFullScreenVideo();
516 private void startFullScreenVideo() {
517 Intent i
= new Intent(getSherlockActivity(), PreviewVideoActivity
.class);
518 i
.putExtra(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
519 i
.putExtra(FileActivity
.EXTRA_FILE
, getFile());
520 i
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPreview
.isPlaying());
521 mVideoPreview
.pause();
522 i
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPreview
.getCurrentPosition());
523 startActivityForResult(i
, 0);
527 public void onConfigurationChanged (Configuration newConfig
) {
528 Log_OC
.e(TAG
, "onConfigurationChanged " + this);
532 public void onActivityResult (int requestCode
, int resultCode
, Intent data
) {
533 Log_OC
.e(TAG
, "onActivityResult " + this);
534 super.onActivityResult(requestCode
, resultCode
, data
);
535 if (resultCode
== Activity
.RESULT_OK
) {
536 mSavedPlaybackPosition
= data
.getExtras().getInt(
537 PreviewVideoActivity
.EXTRA_START_POSITION
);
538 mAutoplay
= data
.getExtras().getBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
);
543 private void playAudio() {
544 OCFile file
= getFile();
545 if (!mMediaServiceBinder
.isPlaying(file
)) {
546 Log_OC
.d(TAG
, "starting playback of " + file
.getStoragePath());
547 mMediaServiceBinder
.start(mAccount
, file
, mAutoplay
, mSavedPlaybackPosition
);
550 if (!mMediaServiceBinder
.isPlaying() && mAutoplay
) {
551 mMediaServiceBinder
.start();
552 mMediaController
.updatePausePlay();
558 private void bindMediaService() {
559 Log_OC
.d(TAG
, "Binding to MediaService...");
560 if (mMediaServiceConnection
== null
) {
561 mMediaServiceConnection
= new MediaServiceConnection();
563 getSherlockActivity().bindService( new Intent(getSherlockActivity(),
565 mMediaServiceConnection
,
566 Context
.BIND_AUTO_CREATE
);
567 // follow the flow in MediaServiceConnection#onServiceConnected(...)
570 /** Defines callbacks for service binding, passed to bindService() */
571 private class MediaServiceConnection
implements ServiceConnection
{
574 public void onServiceConnected(ComponentName component
, IBinder service
) {
575 if (getSherlockActivity() != null
) {
576 if (component
.equals(
577 new ComponentName(getSherlockActivity(), MediaService
.class))) {
578 Log_OC
.d(TAG
, "Media service connected");
579 mMediaServiceBinder
= (MediaServiceBinder
) service
;
580 if (mMediaServiceBinder
!= null
) {
581 prepareMediaController();
582 playAudio(); // do not wait for the touch of nobody to play audio
584 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
587 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
593 private void prepareMediaController() {
594 mMediaServiceBinder
.registerMediaController(mMediaController
);
595 if (mMediaController
!= null
) {
596 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
597 mMediaController
.setEnabled(true
);
598 mMediaController
.updatePausePlay();
603 public void onServiceDisconnected(ComponentName component
) {
604 if (component
.equals(new ComponentName(getSherlockActivity(), MediaService
.class))) {
605 Log_OC
.e(TAG
, "Media service suddenly disconnected");
606 if (mMediaController
!= null
) {
607 mMediaController
.setMediaPlayer(null
);
610 getSherlockActivity(),
611 "No media controller to release when disconnected from media service",
612 Toast
.LENGTH_SHORT
).show();
614 mMediaServiceBinder
= null
;
615 mMediaServiceConnection
= null
;
623 * Opens the previewed file with an external application.
625 private void openFile() {
627 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
632 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
635 * @param file File to test if can be previewed.
636 * @return 'True' if the file can be handled by the fragment.
638 public static boolean canBePreviewed(OCFile file
) {
639 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
643 public void stopPreview(boolean stopAudio
) {
644 OCFile file
= getFile();
645 if (file
.isAudio() && stopAudio
) {
646 mMediaServiceBinder
.pause();
648 } else if (file
.isVideo()) {
649 mVideoPreview
.stopPlayback();
656 * Finishes the preview
658 private void finish() {
659 getSherlockActivity().onBackPressed();
663 public int getPosition() {
665 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
667 Log_OC
.e(TAG
, "getting position: " + mSavedPlaybackPosition
);
668 return mSavedPlaybackPosition
;
671 public boolean isPlaying() {
673 mAutoplay
= mVideoPreview
.isPlaying();