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
.FileDataStorageManager
;
49 import com
.owncloud
.android
.datamodel
.OCFile
;
50 import com
.owncloud
.android
.files
.FileMenuFilter
;
51 import com
.owncloud
.android
.media
.MediaControlView
;
52 import com
.owncloud
.android
.media
.MediaService
;
53 import com
.owncloud
.android
.media
.MediaServiceBinder
;
54 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
55 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
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
71 ConfirmationDialogFragment
.ConfirmationDialogFragmentListener
{
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(OCFile fileToDetail
, Account ocAccount
, int startPlaybackPosition
, boolean autoplay
) {
104 mAccount
= ocAccount
;
105 mSavedPlaybackPosition
= startPlaybackPosition
;
106 mAutoplay
= autoplay
;
111 * Creates an empty fragment for previews.
113 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically (for instance, when the device is turned a aside).
115 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful construction
117 public PreviewMediaFragment() {
120 mSavedPlaybackPosition
= 0;
129 public void onCreate(Bundle savedInstanceState
) {
130 super.onCreate(savedInstanceState
);
131 setHasOptionsMenu(true
);
139 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
140 Bundle savedInstanceState
) {
141 super.onCreateView(inflater
, container
, savedInstanceState
);
142 Log_OC
.e(TAG
, "onCreateView");
145 mView
= inflater
.inflate(R
.layout
.file_preview
, container
, false
);
147 mImagePreview
= (ImageView
)mView
.findViewById(R
.id
.image_preview
);
148 mVideoPreview
= (VideoView
)mView
.findViewById(R
.id
.video_preview
);
149 mVideoPreview
.setOnTouchListener(this);
151 mMediaController
= (MediaControlView
)mView
.findViewById(R
.id
.media_controller
);
161 public void onActivityCreated(Bundle savedInstanceState
) {
162 super.onActivityCreated(savedInstanceState
);
163 Log_OC
.e(TAG
, "onActivityCreated");
165 if (savedInstanceState
!= null
) {
166 setFile((OCFile
)savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_FILE
));
167 mAccount
= savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
);
168 mSavedPlaybackPosition
= savedInstanceState
.getInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
);
169 mAutoplay
= savedInstanceState
.getBoolean(PreviewMediaFragment
.EXTRA_PLAYING
);
172 OCFile file
= getFile();
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");
182 if (file
.isVideo()) {
183 mVideoPreview
.setVisibility(View
.VISIBLE
);
184 mImagePreview
.setVisibility(View
.GONE
);
188 mVideoPreview
.setVisibility(View
.GONE
);
189 mImagePreview
.setVisibility(View
.VISIBLE
);
199 public void onSaveInstanceState(Bundle outState
) {
200 super.onSaveInstanceState(outState
);
201 Log_OC
.e(TAG
, "onSaveInstanceState");
203 outState
.putParcelable(PreviewMediaFragment
.EXTRA_FILE
, getFile());
204 outState
.putParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
, mAccount
);
206 if (getFile().isVideo()) {
207 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
208 mAutoplay
= mVideoPreview
.isPlaying();
209 outState
.putInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
, mSavedPlaybackPosition
);
210 outState
.putBoolean(PreviewMediaFragment
.EXTRA_PLAYING
, mAutoplay
);
212 outState
.putInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
, mMediaServiceBinder
.getCurrentPosition());
213 outState
.putBoolean(PreviewMediaFragment
.EXTRA_PLAYING
, mMediaServiceBinder
.isPlaying());
219 public void onStart() {
221 Log_OC
.e(TAG
, "onStart");
223 OCFile file
= getFile();
225 if (file
.isAudio()) {
228 } else if (file
.isVideo()) {
236 private void stopAudio() {
237 Intent i
= new Intent(getSherlockActivity(), MediaService
.class);
238 i
.setAction(MediaService
.ACTION_STOP_ALL
);
239 getSherlockActivity().startService(i
);
247 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
248 super.onCreateOptionsMenu(menu
, inflater
);
249 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
257 public void onPrepareOptionsMenu(Menu menu
) {
258 super.onPrepareOptionsMenu(menu
);
260 FileMenuFilter mf
= new FileMenuFilter(
262 mContainerActivity
.getStorageManager().getAccount(),
264 getSherlockActivity()
268 // additional restriction for this fragment
269 // TODO allow renaming in PreviewImageFragment
270 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
272 item
.setVisible(false
);
273 item
.setEnabled(false
);
282 public boolean onOptionsItemSelected(MenuItem item
) {
283 switch (item
.getItemId()) {
284 case R
.id
.action_share_file
: {
286 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
289 case R
.id
.action_unshare_file
: {
291 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
294 case R
.id
.action_open_file_with
: {
298 case R
.id
.action_remove_file
: {
302 case R
.id
.action_see_details
: {
306 case R
.id
.action_send_file
: {
309 case R
.id
.action_sync_file
: {
310 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
322 * Update the file of the fragment with file value
325 public void updateFile(OCFile file
){
329 private void sendFile() {
331 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
335 private void seeDetails() {
337 mContainerActivity
.showDetails(getFile());
341 private void prepareVideo() {
342 // create helper to get more control on the playback
343 mVideoHelper
= new VideoHelper();
344 mVideoPreview
.setOnPreparedListener(mVideoHelper
);
345 mVideoPreview
.setOnCompletionListener(mVideoHelper
);
346 mVideoPreview
.setOnErrorListener(mVideoHelper
);
349 private void playVideo() {
350 // create and prepare control panel for the user
351 mMediaController
.setMediaPlayer(mVideoPreview
);
353 // load the video file in the video player ; when done, VideoHelper#onPrepared() will be called
354 mVideoPreview
.setVideoPath(getFile().getStoragePath());
358 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
361 * Called when the file is ready to be played.
363 * Just starts the playback.
365 * @param mp {@link MediaPlayer} instance performing the playback.
368 public void onPrepared(MediaPlayer vp
) {
369 Log_OC
.e(TAG
, "onPrepared");
370 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
372 mVideoPreview
.start();
374 mMediaController
.setEnabled(true
);
375 mMediaController
.updatePausePlay();
381 * Called when the file is finished playing.
383 * Finishes the activity.
385 * @param mp {@link MediaPlayer} instance performing the playback.
388 public void onCompletion(MediaPlayer mp
) {
389 Log_OC
.e(TAG
, "completed");
391 mVideoPreview
.seekTo(0);
392 // next lines are necessary to work around undesired video loops
393 if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD
) {
394 mVideoPreview
.pause();
396 } else if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
397 // mVideePreview.pause() is not enough
399 mMediaController
.setEnabled(false
);
400 mVideoPreview
.stopPlayback();
402 mSavedPlaybackPosition
= 0;
403 mVideoPreview
.setVideoPath(getFile().getStoragePath());
405 } // else : called from onError()
406 mMediaController
.updatePausePlay();
411 * Called when an error in playback occurs.
413 * @param mp {@link MediaPlayer} instance performing the playback.
414 * @param what Type of error
415 * @param extra Extra code specific to the error
418 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
419 if (mVideoPreview
.getWindowToken() != null
) {
420 String message
= MediaService
.getMessageForMediaError(getSherlockActivity(), what
, extra
);
421 new AlertDialog
.Builder(getSherlockActivity())
423 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
424 new DialogInterface
.OnClickListener() {
425 public void onClick(DialogInterface dialog
, int whichButton
) {
427 VideoHelper
.this.onCompletion(null
);
430 .setCancelable(false
)
440 public void onPause() {
442 Log_OC
.e(TAG
, "onPause");
446 public void onResume() {
448 Log_OC
.e(TAG
, "onResume");
452 public void onDestroy() {
454 Log_OC
.e(TAG
, "onDestroy");
458 public void onStop() {
459 Log_OC
.e(TAG
, "onStop");
463 if (mMediaServiceConnection
!= null
) {
464 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
465 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
466 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
468 getSherlockActivity().unbindService(mMediaServiceConnection
);
469 mMediaServiceConnection
= null
;
470 mMediaServiceBinder
= null
;
475 public boolean onTouch(View v
, MotionEvent event
) {
476 if (event
.getAction() == MotionEvent
.ACTION_DOWN
&& v
== mVideoPreview
) {
477 startFullScreenVideo();
484 private void startFullScreenVideo() {
485 Intent i
= new Intent(getSherlockActivity(), PreviewVideoActivity
.class);
486 i
.putExtra(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
487 i
.putExtra(FileActivity
.EXTRA_FILE
, getFile());
488 i
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPreview
.isPlaying());
489 mVideoPreview
.pause();
490 i
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPreview
.getCurrentPosition());
491 startActivityForResult(i
, 0);
495 public void onConfigurationChanged (Configuration newConfig
) {
496 Log_OC
.e(TAG
, "onConfigurationChanged " + this);
500 public void onActivityResult (int requestCode
, int resultCode
, Intent data
) {
501 Log_OC
.e(TAG
, "onActivityResult " + this);
502 super.onActivityResult(requestCode
, resultCode
, data
);
503 if (resultCode
== Activity
.RESULT_OK
) {
504 mSavedPlaybackPosition
= data
.getExtras().getInt(PreviewVideoActivity
.EXTRA_START_POSITION
);
505 mAutoplay
= data
.getExtras().getBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
);
510 private void playAudio() {
511 OCFile file
= getFile();
512 if (!mMediaServiceBinder
.isPlaying(file
)) {
513 Log_OC
.d(TAG
, "starting playback of " + file
.getStoragePath());
514 mMediaServiceBinder
.start(mAccount
, file
, mAutoplay
, mSavedPlaybackPosition
);
517 if (!mMediaServiceBinder
.isPlaying() && mAutoplay
) {
518 mMediaServiceBinder
.start();
519 mMediaController
.updatePausePlay();
525 private void bindMediaService() {
526 Log_OC
.d(TAG
, "Binding to MediaService...");
527 if (mMediaServiceConnection
== null
) {
528 mMediaServiceConnection
= new MediaServiceConnection();
530 getSherlockActivity().bindService( new Intent(getSherlockActivity(),
532 mMediaServiceConnection
,
533 Context
.BIND_AUTO_CREATE
);
534 // follow the flow in MediaServiceConnection#onServiceConnected(...)
537 /** Defines callbacks for service binding, passed to bindService() */
538 private class MediaServiceConnection
implements ServiceConnection
{
541 public void onServiceConnected(ComponentName component
, IBinder service
) {
542 if (getSherlockActivity() != null
) {
543 if (component
.equals(new ComponentName(getSherlockActivity(), MediaService
.class))) {
544 Log_OC
.d(TAG
, "Media service connected");
545 mMediaServiceBinder
= (MediaServiceBinder
) service
;
546 if (mMediaServiceBinder
!= null
) {
547 prepareMediaController();
548 playAudio(); // do not wait for the touch of nobody to play audio
550 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
553 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
559 private void prepareMediaController() {
560 mMediaServiceBinder
.registerMediaController(mMediaController
);
561 if (mMediaController
!= null
) {
562 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
563 mMediaController
.setEnabled(true
);
564 mMediaController
.updatePausePlay();
569 public void onServiceDisconnected(ComponentName component
) {
570 if (component
.equals(new ComponentName(getSherlockActivity(), MediaService
.class))) {
571 Log_OC
.e(TAG
, "Media service suddenly disconnected");
572 if (mMediaController
!= null
) {
573 mMediaController
.setMediaPlayer(null
);
575 Toast
.makeText(getSherlockActivity(), "No media controller to release when disconnected from media service", Toast
.LENGTH_SHORT
).show();
577 mMediaServiceBinder
= null
;
578 mMediaServiceConnection
= null
;
586 * Opens the previewed file with an external application.
588 private void openFile() {
590 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
595 * Starts a the removal of the previewed file.
597 * Shows a confirmation dialog. The action continues in {@link #onConfirmation(String)} , {@link #onNeutral(String)} or {@link #onCancel(String)},
598 * depending upon the user selection in the dialog.
600 private void removeFile() {
601 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
602 R
.string
.confirmation_remove_alert
,
603 new String
[]{getFile().getFileName()},
604 R
.string
.confirmation_remove_remote_and_local
,
605 R
.string
.confirmation_remove_local
,
606 R
.string
.common_cancel
);
607 confDialog
.setOnConfirmationListener(this);
608 confDialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
613 * Performs the removal of the previewed file, both locally and in the server.
616 public void onConfirmation(String callerTag
) {
617 OCFile file
= getFile();
618 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
619 if (storageManager
.getFileById(file
.getFileId()) != null
) { // check that the file is still there;
621 mContainerActivity
.getFileOperationsHelper().removeFile(file
, true
);
627 * Removes the file from local storage
630 public void onNeutral(String callerTag
) {
631 OCFile file
= getFile();
633 mContainerActivity
.getStorageManager().removeFile(file
, false
, true
); // TODO perform in background task / new thread
638 * User cancelled the removal action.
641 public void onCancel(String callerTag
) {
642 // nothing to do here
647 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment} to be previewed.
649 * @param file File to test if can be previewed.
650 * @return 'True' if the file can be handled by the fragment.
652 public static boolean canBePreviewed(OCFile file
) {
653 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
657 private void stopPreview(boolean stopAudio
) {
658 OCFile file
= getFile();
659 if (file
.isAudio() && stopAudio
) {
660 mMediaServiceBinder
.pause();
662 } else if (file
.isVideo()) {
663 mVideoPreview
.stopPlayback();
670 * Finishes the preview
672 private void finish() {
673 getSherlockActivity().onBackPressed();
677 public int getPosition() {
679 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
681 Log_OC
.e(TAG
, "getting position: " + mSavedPlaybackPosition
);
682 return mSavedPlaybackPosition
;
685 public boolean isPlaying() {
687 mAutoplay
= mVideoPreview
.isPlaying();