2 * ownCloud Android client application
4 * @author David A. Velasco
5 * Copyright (C) 2015 ownCloud Inc.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.ui
.preview
;
22 import android
.accounts
.Account
;
23 import android
.app
.Activity
;
24 import android
.app
.AlertDialog
;
25 import android
.content
.ComponentName
;
26 import android
.content
.Context
;
27 import android
.content
.DialogInterface
;
28 import android
.content
.Intent
;
29 import android
.content
.ServiceConnection
;
30 import android
.content
.res
.Configuration
;
31 import android
.content
.res
.Resources
;
32 import android
.media
.MediaPlayer
;
33 import android
.media
.MediaPlayer
.OnCompletionListener
;
34 import android
.media
.MediaPlayer
.OnErrorListener
;
35 import android
.media
.MediaPlayer
.OnPreparedListener
;
36 import android
.net
.Uri
;
37 import android
.os
.Build
;
38 import android
.os
.Bundle
;
39 import android
.os
.IBinder
;
40 import android
.view
.LayoutInflater
;
41 import android
.view
.Menu
;
42 import android
.view
.MenuInflater
;
43 import android
.view
.MenuItem
;
44 import android
.view
.MotionEvent
;
45 import android
.view
.View
;
46 import android
.view
.View
.OnTouchListener
;
47 import android
.view
.ViewGroup
;
48 import android
.widget
.ImageView
;
49 import android
.widget
.Toast
;
50 import android
.widget
.VideoView
;
52 import com
.owncloud
.android
.R
;
53 import com
.owncloud
.android
.datamodel
.OCFile
;
54 import com
.owncloud
.android
.files
.FileMenuFilter
;
55 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
56 import com
.owncloud
.android
.media
.MediaControlView
;
57 import com
.owncloud
.android
.media
.MediaService
;
58 import com
.owncloud
.android
.media
.MediaServiceBinder
;
59 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
60 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
61 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
62 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
66 * This fragment shows a preview of a downloaded media file (audio or video).
68 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will
69 * produce an {@link IllegalStateException}.
71 * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is
72 * generated on instantiation too.
74 public class PreviewMediaFragment
extends FileFragment
implements
77 public static final String EXTRA_FILE
= "FILE";
78 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
79 private static final String EXTRA_PLAY_POSITION
= "PLAY_POSITION";
80 private static final String EXTRA_PLAYING
= "PLAYING";
83 private Account mAccount
;
84 private ImageView mImagePreview
;
85 private VideoView mVideoPreview
;
86 private int mSavedPlaybackPosition
;
88 private MediaServiceBinder mMediaServiceBinder
= null
;
89 private MediaControlView mMediaController
= null
;
90 private MediaServiceConnection mMediaServiceConnection
= null
;
91 private VideoHelper mVideoHelper
;
92 private boolean mAutoplay
;
93 public boolean mPrepared
;
95 private static final String TAG
= PreviewMediaFragment
.class.getSimpleName();
99 * Creates a fragment to preview a file.
101 * When 'fileToDetail' or 'ocAccount' are null
103 * @param fileToDetail An {@link OCFile} to preview in the fragment
104 * @param ocAccount An ownCloud account; needed to start downloads
106 public PreviewMediaFragment(
109 int startPlaybackPosition
,
113 mAccount
= ocAccount
;
114 mSavedPlaybackPosition
= startPlaybackPosition
;
115 mAutoplay
= autoplay
;
120 * Creates an empty fragment for previews.
122 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
123 * (for instance, when the device is turned a aside).
125 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
128 public PreviewMediaFragment() {
131 mSavedPlaybackPosition
= 0;
140 public void onCreate(Bundle savedInstanceState
) {
141 super.onCreate(savedInstanceState
);
142 setHasOptionsMenu(true
);
150 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
151 Bundle savedInstanceState
) {
152 super.onCreateView(inflater
, container
, savedInstanceState
);
153 Log_OC
.e(TAG
, "onCreateView");
156 mView
= inflater
.inflate(R
.layout
.file_preview
, container
, false
);
158 mImagePreview
= (ImageView
)mView
.findViewById(R
.id
.image_preview
);
159 mVideoPreview
= (VideoView
)mView
.findViewById(R
.id
.video_preview
);
160 mVideoPreview
.setOnTouchListener(this);
162 mMediaController
= (MediaControlView
)mView
.findViewById(R
.id
.media_controller
);
172 public void onActivityCreated(Bundle savedInstanceState
) {
173 super.onActivityCreated(savedInstanceState
);
174 Log_OC
.e(TAG
, "onActivityCreated");
176 OCFile file
= getFile();
177 if (savedInstanceState
== null
) {
179 throw new IllegalStateException("Instanced with a NULL OCFile");
181 if (mAccount
== null
) {
182 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
184 if (!file
.isDown()) {
185 throw new IllegalStateException("There is no local file to preview");
189 file
= (OCFile
)savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_FILE
);
191 mAccount
= savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
);
192 mSavedPlaybackPosition
=
193 savedInstanceState
.getInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
);
194 mAutoplay
= savedInstanceState
.getBoolean(PreviewMediaFragment
.EXTRA_PLAYING
);
197 if (file
!= null
&& file
.isDown()) {
198 if (file
.isVideo()) {
199 mVideoPreview
.setVisibility(View
.VISIBLE
);
200 mImagePreview
.setVisibility(View
.GONE
);
204 mVideoPreview
.setVisibility(View
.GONE
);
205 mImagePreview
.setVisibility(View
.VISIBLE
);
216 public void onSaveInstanceState(Bundle outState
) {
217 super.onSaveInstanceState(outState
);
218 Log_OC
.e(TAG
, "onSaveInstanceState");
220 outState
.putParcelable(PreviewMediaFragment
.EXTRA_FILE
, getFile());
221 outState
.putParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
, mAccount
);
223 if (getFile().isVideo()) {
224 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
225 mAutoplay
= mVideoPreview
.isPlaying();
226 outState
.putInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
, mSavedPlaybackPosition
);
227 outState
.putBoolean(PreviewMediaFragment
.EXTRA_PLAYING
, mAutoplay
);
230 PreviewMediaFragment
.EXTRA_PLAY_POSITION
,
231 mMediaServiceBinder
.getCurrentPosition());
233 PreviewMediaFragment
.EXTRA_PLAYING
, mMediaServiceBinder
.isPlaying());
239 public void onStart() {
241 Log_OC
.e(TAG
, "onStart");
243 OCFile file
= getFile();
244 if (file
!= null
&& file
.isDown()) {
245 if (file
.isAudio()) {
248 } else if (file
.isVideo()) {
256 private void stopAudio() {
257 Intent i
= new Intent(getActivity(), MediaService
.class);
258 i
.setAction(MediaService
.ACTION_STOP_ALL
);
259 getActivity().startService(i
);
267 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
268 super.onCreateOptionsMenu(menu
, inflater
);
269 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
277 public void onPrepareOptionsMenu(Menu menu
) {
278 super.onPrepareOptionsMenu(menu
);
280 if (mContainerActivity
.getStorageManager() != null
) {
281 FileMenuFilter mf
= new FileMenuFilter(
283 mContainerActivity
.getStorageManager().getAccount(),
290 // additional restriction for this fragment
291 // TODO allow renaming in PreviewImageFragment
292 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
294 item
.setVisible(false
);
295 item
.setEnabled(false
);
298 // additional restriction for this fragment
299 item
= menu
.findItem(R
.id
.action_move
);
301 item
.setVisible(false
);
302 item
.setEnabled(false
);
311 public boolean onOptionsItemSelected(MenuItem item
) {
312 switch (item
.getItemId()) {
313 case R
.id
.action_share_file
: {
315 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
318 case R
.id
.action_unshare_file
: {
320 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
323 case R
.id
.action_open_file_with
: {
327 case R
.id
.action_remove_file
: {
328 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
329 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
332 case R
.id
.action_see_details
: {
336 case R
.id
.action_send_file
: {
340 case R
.id
.action_sync_file
: {
341 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
353 * Update the file of the fragment with file value
356 public void updateFile(OCFile file
){
360 private void sendFile() {
362 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
366 private void seeDetails() {
368 mContainerActivity
.showDetails(getFile());
372 private void prepareVideo() {
373 // create helper to get more control on the playback
374 mVideoHelper
= new VideoHelper();
375 mVideoPreview
.setOnPreparedListener(mVideoHelper
);
376 mVideoPreview
.setOnCompletionListener(mVideoHelper
);
377 mVideoPreview
.setOnErrorListener(mVideoHelper
);
380 @SuppressWarnings("static-access")
381 private void playVideo() {
382 // create and prepare control panel for the user
383 mMediaController
.setMediaPlayer(mVideoPreview
);
385 // load the video file in the video player ;
386 // when done, VideoHelper#onPrepared() will be called
387 Uri uri
= Uri
.parse(getFile().getStoragePath());
388 mVideoPreview
.setVideoPath(uri
.encode(getFile().getStoragePath()));
392 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
395 * Called when the file is ready to be played.
397 * Just starts the playback.
399 * @param vp {@link MediaPlayer} instance performing the playback.
402 public void onPrepared(MediaPlayer vp
) {
403 Log_OC
.e(TAG
, "onPrepared");
404 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
406 mVideoPreview
.start();
408 mMediaController
.setEnabled(true
);
409 mMediaController
.updatePausePlay();
415 * Called when the file is finished playing.
417 * Finishes the activity.
419 * @param mp {@link MediaPlayer} instance performing the playback.
422 public void onCompletion(MediaPlayer mp
) {
423 Log_OC
.e(TAG
, "completed");
425 mVideoPreview
.seekTo(0);
426 // next lines are necessary to work around undesired video loops
427 if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD
) {
428 mVideoPreview
.pause();
430 } else if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
431 // mVideePreview.pause() is not enough
433 mMediaController
.setEnabled(false
);
434 mVideoPreview
.stopPlayback();
436 mSavedPlaybackPosition
= 0;
437 mVideoPreview
.setVideoPath(getFile().getStoragePath());
439 } // else : called from onError()
440 mMediaController
.updatePausePlay();
445 * Called when an error in playback occurs.
447 * @param mp {@link MediaPlayer} instance performing the playback.
448 * @param what Type of error
449 * @param extra Extra code specific to the error
452 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
453 if (mVideoPreview
.getWindowToken() != null
) {
454 String message
= MediaService
.getMessageForMediaError(
455 getActivity(), what
, extra
);
456 new AlertDialog
.Builder(getActivity())
458 .setPositiveButton(android
.R
.string
.VideoView_error_button
,
459 new DialogInterface
.OnClickListener() {
460 public void onClick(DialogInterface dialog
, int whichButton
) {
462 VideoHelper
.this.onCompletion(null
);
465 .setCancelable(false
)
475 public void onPause() {
476 Log_OC
.e(TAG
, "onPause");
481 public void onResume() {
483 Log_OC
.e(TAG
, "onResume");
487 public void onDestroy() {
488 Log_OC
.e(TAG
, "onDestroy");
493 public void onStop() {
494 Log_OC
.e(TAG
, "onStop");
497 if (mMediaServiceConnection
!= null
) {
498 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
499 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
500 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
502 getActivity().unbindService(mMediaServiceConnection
);
503 mMediaServiceConnection
= null
;
504 mMediaServiceBinder
= null
;
511 public boolean onTouch(View v
, MotionEvent event
) {
512 if (event
.getAction() == MotionEvent
.ACTION_DOWN
&& v
== mVideoPreview
) {
513 // added a margin on the left to avoid interfering with gesture to open navigation drawer
514 if (event
.getX() / Resources
.getSystem().getDisplayMetrics().density
> 24.0) {
515 startFullScreenVideo();
523 private void startFullScreenVideo() {
524 Intent i
= new Intent(getActivity(), PreviewVideoActivity
.class);
525 i
.putExtra(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
526 i
.putExtra(FileActivity
.EXTRA_FILE
, getFile());
527 i
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPreview
.isPlaying());
528 mVideoPreview
.pause();
529 i
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPreview
.getCurrentPosition());
530 startActivityForResult(i
, 0);
534 public void onConfigurationChanged (Configuration newConfig
) {
535 Log_OC
.e(TAG
, "onConfigurationChanged " + this);
539 public void onActivityResult (int requestCode
, int resultCode
, Intent data
) {
540 Log_OC
.e(TAG
, "onActivityResult " + this);
541 super.onActivityResult(requestCode
, resultCode
, data
);
542 if (resultCode
== Activity
.RESULT_OK
) {
543 mSavedPlaybackPosition
= data
.getExtras().getInt(
544 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 getActivity().bindService( new Intent(getActivity(),
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 (getActivity() != null
) {
583 if (component
.equals(
584 new ComponentName(getActivity(), MediaService
.class))) {
585 Log_OC
.d(TAG
, "Media service connected");
586 mMediaServiceBinder
= (MediaServiceBinder
) service
;
587 if (mMediaServiceBinder
!= null
) {
588 prepareMediaController();
589 playAudio(); // do not wait for the touch of nobody to play audio
591 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
594 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
600 private void prepareMediaController() {
601 mMediaServiceBinder
.registerMediaController(mMediaController
);
602 if (mMediaController
!= null
) {
603 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
604 mMediaController
.setEnabled(true
);
605 mMediaController
.updatePausePlay();
610 public void onServiceDisconnected(ComponentName component
) {
611 if (component
.equals(new ComponentName(getActivity(), MediaService
.class))) {
612 Log_OC
.e(TAG
, "Media service suddenly disconnected");
613 if (mMediaController
!= null
) {
614 mMediaController
.setMediaPlayer(null
);
618 "No media controller to release when disconnected from media service",
619 Toast
.LENGTH_SHORT
).show();
621 mMediaServiceBinder
= null
;
622 mMediaServiceConnection
= null
;
630 * Opens the previewed file with an external application.
632 private void openFile() {
634 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
639 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
642 * @param file File to test if can be previewed.
643 * @return 'True' if the file can be handled by the fragment.
645 public static boolean canBePreviewed(OCFile file
) {
646 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
650 public void stopPreview(boolean stopAudio
) {
651 OCFile file
= getFile();
652 if (file
.isAudio() && stopAudio
) {
653 mMediaServiceBinder
.pause();
655 } else if (file
.isVideo()) {
656 mVideoPreview
.stopPlayback();
663 * Finishes the preview
665 private void finish() {
666 getActivity().onBackPressed();
670 public int getPosition() {
672 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
674 Log_OC
.e(TAG
, "getting position: " + mSavedPlaybackPosition
);
675 return mSavedPlaybackPosition
;
678 public boolean isPlaying() {
680 mAutoplay
= mVideoPreview
.isPlaying();