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
);
250 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
252 List<Integer> toHide = new ArrayList<Integer>();
254 MenuItem item = null;
255 toHide.add(R.id.action_cancel_download);
256 toHide.add(R.id.action_cancel_upload);
257 toHide.add(R.id.action_download_file);
258 toHide.add(R.id.action_sync_file);
259 toHide.add(R.id.action_rename_file); // by now
262 if (!getFile().isShareByLink()) {
263 toHide.add(R.id.action_unshare_file);
267 boolean sendEnabled = getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on");
269 toHide.add(R.id.action_send_file);
272 for (int i : toHide) {
273 item = menu.findItem(i);
275 item.setVisible(false);
276 item.setEnabled(false);
288 public void onPrepareOptionsMenu(Menu menu
) {
289 super.onPrepareOptionsMenu(menu
);
291 FileMenuFilter mf
= new FileMenuFilter();
292 mf
.setFile(getFile());
293 mf
.setComponentGetter(mContainerActivity
);
294 mf
.setAccount(mContainerActivity
.getStorageManager().getAccount());
295 mf
.setContext(getSherlockActivity());
296 mf
.setFragment(this);
300 MenuItem item = menu.findItem(R.id.action_unshare_file);
302 if (!getFile().isShareByLink()) {
303 item.setVisible(false);
304 item.setEnabled(false);
306 item.setVisible(true);
307 item.setEnabled(true);
317 public boolean onOptionsItemSelected(MenuItem item
) {
318 switch (item
.getItemId()) {
319 case R
.id
.action_share_file
: {
323 case R
.id
.action_unshare_file
: {
324 unshareFileWithLink();
327 case R
.id
.action_open_file_with
: {
331 case R
.id
.action_remove_file
: {
335 case R
.id
.action_see_details
: {
339 case R
.id
.action_send_file
: {
351 * Update the file of the fragment with file value
354 public void updateFile(OCFile file
){
358 private void unshareFileWithLink() {
360 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
363 private void shareFileWithLink() {
365 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
369 private void sendFile() {
371 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
375 private void seeDetails() {
377 mContainerActivity
.showDetails(getFile());
381 private void prepareVideo() {
382 // create helper to get more control on the playback
383 mVideoHelper
= new VideoHelper();
384 mVideoPreview
.setOnPreparedListener(mVideoHelper
);
385 mVideoPreview
.setOnCompletionListener(mVideoHelper
);
386 mVideoPreview
.setOnErrorListener(mVideoHelper
);
389 private void playVideo() {
390 // create and prepare control panel for the user
391 mMediaController
.setMediaPlayer(mVideoPreview
);
393 // load the video file in the video player ; when done, VideoHelper#onPrepared() will be called
394 mVideoPreview
.setVideoPath(getFile().getStoragePath());
398 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
401 * Called when the file is ready to be played.
403 * Just starts the playback.
405 * @param mp {@link MediaPlayer} instance performing the playback.
408 public void onPrepared(MediaPlayer vp
) {
409 Log_OC
.e(TAG
, "onPrepared");
410 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
412 mVideoPreview
.start();
414 mMediaController
.setEnabled(true
);
415 mMediaController
.updatePausePlay();
421 * Called when the file is finished playing.
423 * Finishes the activity.
425 * @param mp {@link MediaPlayer} instance performing the playback.
428 public void onCompletion(MediaPlayer mp
) {
429 Log_OC
.e(TAG
, "completed");
431 mVideoPreview
.seekTo(0);
432 // next lines are necessary to work around undesired video loops
433 if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD
) {
434 mVideoPreview
.pause();
436 } else if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
437 // mVideePreview.pause() is not enough
439 mMediaController
.setEnabled(false
);
440 mVideoPreview
.stopPlayback();
442 mSavedPlaybackPosition
= 0;
443 mVideoPreview
.setVideoPath(getFile().getStoragePath());
445 } // else : called from onError()
446 mMediaController
.updatePausePlay();
451 * Called when an error in playback occurs.
453 * @param mp {@link MediaPlayer} instance performing the playback.
454 * @param what Type of error
455 * @param extra Extra code specific to the error
458 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
459 if (mVideoPreview
.getWindowToken() != null
) {
460 String message
= MediaService
.getMessageForMediaError(getSherlockActivity(), what
, extra
);
461 new AlertDialog
.Builder(getSherlockActivity())
463 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
464 new DialogInterface
.OnClickListener() {
465 public void onClick(DialogInterface dialog
, int whichButton
) {
467 VideoHelper
.this.onCompletion(null
);
470 .setCancelable(false
)
480 public void onPause() {
482 Log_OC
.e(TAG
, "onPause");
486 public void onResume() {
488 Log_OC
.e(TAG
, "onResume");
492 public void onDestroy() {
494 Log_OC
.e(TAG
, "onDestroy");
498 public void onStop() {
499 Log_OC
.e(TAG
, "onStop");
503 if (mMediaServiceConnection
!= null
) {
504 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
505 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
506 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
508 getSherlockActivity().unbindService(mMediaServiceConnection
);
509 mMediaServiceConnection
= null
;
510 mMediaServiceBinder
= null
;
515 public boolean onTouch(View v
, MotionEvent event
) {
516 if (event
.getAction() == MotionEvent
.ACTION_DOWN
&& v
== mVideoPreview
) {
517 startFullScreenVideo();
524 private void startFullScreenVideo() {
525 Intent i
= new Intent(getSherlockActivity(), PreviewVideoActivity
.class);
526 i
.putExtra(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
527 i
.putExtra(FileActivity
.EXTRA_FILE
, getFile());
528 i
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPreview
.isPlaying());
529 mVideoPreview
.pause();
530 i
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPreview
.getCurrentPosition());
531 startActivityForResult(i
, 0);
535 public void onConfigurationChanged (Configuration newConfig
) {
536 Log_OC
.e(TAG
, "onConfigurationChanged " + this);
540 public void onActivityResult (int requestCode
, int resultCode
, Intent data
) {
541 Log_OC
.e(TAG
, "onActivityResult " + this);
542 super.onActivityResult(requestCode
, resultCode
, data
);
543 if (resultCode
== Activity
.RESULT_OK
) {
544 mSavedPlaybackPosition
= data
.getExtras().getInt(PreviewVideoActivity
.EXTRA_START_POSITION
);
545 mAutoplay
= data
.getExtras().getBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
);
550 private void playAudio() {
551 OCFile file
= getFile();
552 if (!mMediaServiceBinder
.isPlaying(file
)) {
553 Log_OC
.d(TAG
, "starting playback of " + file
.getStoragePath());
554 mMediaServiceBinder
.start(mAccount
, file
, mAutoplay
, mSavedPlaybackPosition
);
557 if (!mMediaServiceBinder
.isPlaying() && mAutoplay
) {
558 mMediaServiceBinder
.start();
559 mMediaController
.updatePausePlay();
565 private void bindMediaService() {
566 Log_OC
.d(TAG
, "Binding to MediaService...");
567 if (mMediaServiceConnection
== null
) {
568 mMediaServiceConnection
= new MediaServiceConnection();
570 getSherlockActivity().bindService( new Intent(getSherlockActivity(),
572 mMediaServiceConnection
,
573 Context
.BIND_AUTO_CREATE
);
574 // follow the flow in MediaServiceConnection#onServiceConnected(...)
577 /** Defines callbacks for service binding, passed to bindService() */
578 private class MediaServiceConnection
implements ServiceConnection
{
581 public void onServiceConnected(ComponentName component
, IBinder service
) {
582 if (getSherlockActivity() != null
) {
583 if (component
.equals(new ComponentName(getSherlockActivity(), MediaService
.class))) {
584 Log_OC
.d(TAG
, "Media service connected");
585 mMediaServiceBinder
= (MediaServiceBinder
) service
;
586 if (mMediaServiceBinder
!= null
) {
587 prepareMediaController();
588 playAudio(); // do not wait for the touch of nobody to play audio
590 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
593 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
599 private void prepareMediaController() {
600 mMediaServiceBinder
.registerMediaController(mMediaController
);
601 if (mMediaController
!= null
) {
602 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
603 mMediaController
.setEnabled(true
);
604 mMediaController
.updatePausePlay();
609 public void onServiceDisconnected(ComponentName component
) {
610 if (component
.equals(new ComponentName(getSherlockActivity(), MediaService
.class))) {
611 Log_OC
.e(TAG
, "Media service suddenly disconnected");
612 if (mMediaController
!= null
) {
613 mMediaController
.setMediaPlayer(null
);
615 Toast
.makeText(getSherlockActivity(), "No media controller to release when disconnected from media service", Toast
.LENGTH_SHORT
).show();
617 mMediaServiceBinder
= null
;
618 mMediaServiceConnection
= null
;
626 * Opens the previewed file with an external application.
628 private void openFile() {
630 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
635 * Starts a the removal of the previewed file.
637 * Shows a confirmation dialog. The action continues in {@link #onConfirmation(String)} , {@link #onNeutral(String)} or {@link #onCancel(String)},
638 * depending upon the user selection in the dialog.
640 private void removeFile() {
641 ConfirmationDialogFragment confDialog
= ConfirmationDialogFragment
.newInstance(
642 R
.string
.confirmation_remove_alert
,
643 new String
[]{getFile().getFileName()},
644 R
.string
.confirmation_remove_remote_and_local
,
645 R
.string
.confirmation_remove_local
,
646 R
.string
.common_cancel
);
647 confDialog
.setOnConfirmationListener(this);
648 confDialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
653 * Performs the removal of the previewed file, both locally and in the server.
656 public void onConfirmation(String callerTag
) {
657 OCFile file
= getFile();
658 FileDataStorageManager storageManager
= mContainerActivity
.getStorageManager();
659 if (storageManager
.getFileById(file
.getFileId()) != null
) { // check that the file is still there;
661 mContainerActivity
.getFileOperationsHelper().removeFile(file
, true
);
667 * Removes the file from local storage
670 public void onNeutral(String callerTag
) {
671 OCFile file
= getFile();
673 mContainerActivity
.getStorageManager().removeFile(file
, false
, true
); // TODO perform in background task / new thread
678 * User cancelled the removal action.
681 public void onCancel(String callerTag
) {
682 // nothing to do here
687 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment} to be previewed.
689 * @param file File to test if can be previewed.
690 * @return 'True' if the file can be handled by the fragment.
692 public static boolean canBePreviewed(OCFile file
) {
693 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
697 private void stopPreview(boolean stopAudio
) {
698 OCFile file
= getFile();
699 if (file
.isAudio() && stopAudio
) {
700 mMediaServiceBinder
.pause();
702 } else if (file
.isVideo()) {
703 mVideoPreview
.stopPlayback();
710 * Finishes the preview
712 private void finish() {
713 getSherlockActivity().onBackPressed();
717 public int getPosition() {
719 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
721 Log_OC
.e(TAG
, "getting position: " + mSavedPlaybackPosition
);
722 return mSavedPlaybackPosition
;
725 public boolean isPlaying() {
727 mAutoplay
= mVideoPreview
.isPlaying();