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
.accounts
.AuthenticatorException
;
24 import android
.accounts
.OperationCanceledException
;
25 import android
.app
.Activity
;
26 import android
.content
.ActivityNotFoundException
;
27 import android
.graphics
.Bitmap
;
28 import android
.graphics
.BitmapFactory
;
29 import android
.media
.MediaMetadataRetriever
;
30 import android
.os
.AsyncTask
;
31 import android
.support
.v7
.app
.AlertDialog
;
32 import android
.content
.ComponentName
;
33 import android
.content
.Context
;
34 import android
.content
.DialogInterface
;
35 import android
.content
.Intent
;
36 import android
.content
.ServiceConnection
;
37 import android
.content
.res
.Configuration
;
38 import android
.content
.res
.Resources
;
39 import android
.media
.MediaPlayer
;
40 import android
.media
.MediaPlayer
.OnCompletionListener
;
41 import android
.media
.MediaPlayer
.OnErrorListener
;
42 import android
.media
.MediaPlayer
.OnPreparedListener
;
43 import android
.os
.Bundle
;
44 import android
.os
.IBinder
;
45 import android
.view
.LayoutInflater
;
46 import android
.view
.Menu
;
47 import android
.view
.MenuInflater
;
48 import android
.view
.MenuItem
;
49 import android
.view
.MotionEvent
;
50 import android
.view
.View
;
51 import android
.view
.View
.OnTouchListener
;
52 import android
.view
.ViewGroup
;
53 import android
.widget
.ImageView
;
54 import android
.widget
.Toast
;
55 import android
.widget
.VideoView
;
57 import com
.owncloud
.android
.MainApp
;
58 import com
.owncloud
.android
.R
;
59 import com
.owncloud
.android
.datamodel
.OCFile
;
60 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
61 import com
.owncloud
.android
.files
.FileMenuFilter
;
62 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
63 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
64 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
65 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentials
;
66 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
;
67 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
68 import com
.owncloud
.android
.media
.MediaControlView
;
69 import com
.owncloud
.android
.media
.MediaService
;
70 import com
.owncloud
.android
.media
.MediaServiceBinder
;
71 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
72 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
73 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
74 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
76 import java
.io
.IOException
;
77 import java
.util
.concurrent
.ExecutionException
;
81 * This fragment shows a preview of a downloaded media file (audio or video).
83 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will
84 * produce an {@link IllegalStateException}.
86 * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is
87 * generated on instantiation too.
89 public class PreviewMediaFragment
extends FileFragment
implements
92 public static final String EXTRA_FILE
= "FILE";
93 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
94 private static final String EXTRA_PLAY_POSITION
= "PLAY_POSITION";
95 private static final String EXTRA_PLAYING
= "PLAYING";
98 private Account mAccount
;
99 private ImageView mImagePreview
;
100 private VideoView mVideoPreview
;
101 private int mSavedPlaybackPosition
;
104 private MediaServiceBinder mMediaServiceBinder
= null
;
105 private MediaControlView mMediaController
= null
;
106 private MediaServiceConnection mMediaServiceConnection
= null
;
107 private VideoHelper mVideoHelper
;
108 private boolean mAutoplay
;
109 public boolean mPrepared
;
111 private static final String TAG
= PreviewMediaFragment
.class.getSimpleName();
115 * Creates a fragment to preview a file.
117 * When 'fileToDetail' or 'ocAccount' are null
119 * @param fileToDetail An {@link OCFile} to preview in the fragment
120 * @param ocAccount An ownCloud account; needed to start downloads
122 public PreviewMediaFragment(
125 int startPlaybackPosition
,
129 mAccount
= ocAccount
;
130 mSavedPlaybackPosition
= startPlaybackPosition
;
131 mAutoplay
= autoplay
;
136 * Creates an empty fragment for previews.
138 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
139 * (for instance, when the device is turned a aside).
141 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
144 public PreviewMediaFragment() {
147 mSavedPlaybackPosition
= 0;
156 public void onCreate(Bundle savedInstanceState
) {
157 super.onCreate(savedInstanceState
);
158 setHasOptionsMenu(true
);
166 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
167 Bundle savedInstanceState
) {
168 super.onCreateView(inflater
, container
, savedInstanceState
);
169 Log_OC
.v(TAG
, "onCreateView");
172 mView
= inflater
.inflate(R
.layout
.file_preview
, container
, false
);
174 mImagePreview
= (ImageView
) mView
.findViewById(R
.id
.image_preview
);
175 mVideoPreview
= (VideoView
) mView
.findViewById(R
.id
.video_preview
);
176 mVideoPreview
.setOnTouchListener(this);
178 mMediaController
= (MediaControlView
) mView
.findViewById(R
.id
.media_controller
);
188 public void onActivityCreated(Bundle savedInstanceState
) {
189 super.onActivityCreated(savedInstanceState
);
190 Log_OC
.v(TAG
, "onActivityCreated");
192 OCFile file
= getFile();
193 if (savedInstanceState
== null
) {
195 throw new IllegalStateException("Instanced with a NULL OCFile");
197 if (mAccount
== null
) {
198 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
202 file
= (OCFile
) savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_FILE
);
204 mAccount
= savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
);
205 mSavedPlaybackPosition
=
206 savedInstanceState
.getInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
);
207 mAutoplay
= savedInstanceState
.getBoolean(PreviewMediaFragment
.EXTRA_PLAYING
);
211 if (file
.isVideo()) {
212 mVideoPreview
.setVisibility(View
.VISIBLE
);
213 mImagePreview
.setVisibility(View
.GONE
);
218 mVideoPreview
.setVisibility(View
.GONE
);
219 mImagePreview
.setVisibility(View
.VISIBLE
);
220 extractAndSetCoverArt(file
);
227 * tries to read the cover art from the audio file and sets it as cover art.
229 * @param file audio file with potential cover art
231 private void extractAndSetCoverArt(OCFile file
) {
232 if (file
.isAudio()) {
234 MediaMetadataRetriever mmr
= new MediaMetadataRetriever();
235 mmr
.setDataSource(file
.getStoragePath());
236 byte[] data
= mmr
.getEmbeddedPicture();
238 Bitmap bitmap
= BitmapFactory
.decodeByteArray(data
, 0, data
.length
);
239 mImagePreview
.setImageBitmap(bitmap
); //associated cover art in bitmap
241 mImagePreview
.setImageResource(R
.drawable
.logo
);
243 } catch (Throwable t
) {
244 mImagePreview
.setImageResource(R
.drawable
.logo
);
254 public void onSaveInstanceState(Bundle outState
) {
255 super.onSaveInstanceState(outState
);
256 Log_OC
.v(TAG
, "onSaveInstanceState");
258 outState
.putParcelable(PreviewMediaFragment
.EXTRA_FILE
, getFile());
259 outState
.putParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
, mAccount
);
261 if (getFile().isVideo()) {
262 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
263 mAutoplay
= mVideoPreview
.isPlaying();
264 outState
.putInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
, mSavedPlaybackPosition
);
265 outState
.putBoolean(PreviewMediaFragment
.EXTRA_PLAYING
, mAutoplay
);
269 PreviewMediaFragment
.EXTRA_PLAY_POSITION
,
270 mMediaServiceBinder
.getCurrentPosition());
272 PreviewMediaFragment
.EXTRA_PLAYING
, mMediaServiceBinder
.isPlaying());
278 public void onStart() {
280 Log_OC
.v(TAG
, "onStart");
282 OCFile file
= getFile();
284 if (file
.isAudio()) {
289 if (file
.isVideo()) {
298 private void stopAudio() {
299 Intent i
= new Intent(getActivity(), MediaService
.class);
300 i
.setAction(MediaService
.ACTION_STOP_ALL
);
301 getActivity().startService(i
);
309 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
310 super.onCreateOptionsMenu(menu
, inflater
);
311 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
319 public void onPrepareOptionsMenu(Menu menu
) {
320 super.onPrepareOptionsMenu(menu
);
322 if (mContainerActivity
.getStorageManager() != null
) {
323 FileMenuFilter mf
= new FileMenuFilter(
325 mContainerActivity
.getStorageManager().getAccount(),
332 // additional restriction for this fragment
333 // TODO allow renaming in PreviewImageFragment
334 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
336 item
.setVisible(false
);
337 item
.setEnabled(false
);
340 // additional restriction for this fragment
341 item
= menu
.findItem(R
.id
.action_move
);
343 item
.setVisible(false
);
344 item
.setEnabled(false
);
347 // additional restriction for this fragment
348 item
= menu
.findItem(R
.id
.action_copy
);
350 item
.setVisible(false
);
351 item
.setEnabled(false
);
360 public boolean onOptionsItemSelected(MenuItem item
) {
361 switch (item
.getItemId()) {
362 case R
.id
.action_share_file
: {
364 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
367 case R
.id
.action_share_with_users
: {
371 case R
.id
.action_unshare_file
: {
373 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
376 case R
.id
.action_open_file_with
: {
380 case R
.id
.action_remove_file
: {
381 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
382 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
385 case R
.id
.action_see_details
: {
389 case R
.id
.action_send_file
: {
393 case R
.id
.action_sync_file
: {
394 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
397 case R
.id
.action_favorite_file
:{
398 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), true
);
401 case R
.id
.action_unfavorite_file
:{
402 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), false
);
412 * Update the file of the fragment with file value
414 * @param file Replaces the held file with a new one
416 public void updateFile(OCFile file
) {
420 private void sendFile() {
422 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
426 private void seeDetails() {
428 mContainerActivity
.showDetails(getFile());
431 private void seeShareFile() {
433 mContainerActivity
.getFileOperationsHelper().showShareFile(getFile());
436 private void prepareVideo() {
437 // create helper to get more control on the playback
438 mVideoHelper
= new VideoHelper();
439 mVideoPreview
.setOnPreparedListener(mVideoHelper
);
440 mVideoPreview
.setOnCompletionListener(mVideoHelper
);
441 mVideoPreview
.setOnErrorListener(mVideoHelper
);
444 @SuppressWarnings("static-access")
445 private void playVideo() {
446 // create and prepare control panel for the user
447 mMediaController
.setMediaPlayer(mVideoPreview
);
449 // load the video file in the video player ;
450 // when done, VideoHelper#onPrepared() will be called
451 if (getFile().isDown()) {
452 mUri
= getFile().getStoragePath();
454 Context context
= MainApp
.getAppContext();
455 Account account
= mContainerActivity
.getStorageManager().getAccount();
457 mUri
= generateUrlWithCredentials(account
, context
, getFile());
460 mVideoPreview
.setVideoURI(getFile().getStorageUri());
463 public static String
generateUrlWithCredentials(Account account
, Context context
, OCFile file
){
464 OwnCloudAccount ocAccount
= null
;
466 ocAccount
= new OwnCloudAccount(account
, context
);
468 final ClientGenerationTask task
= new ClientGenerationTask();
469 task
.execute(ocAccount
);
471 OwnCloudClient mClient
= task
.get();
472 String url
= AccountUtils
.constructFullURLForAccount(context
, account
) + Uri
.encode(file
.getRemotePath(), "/");
473 OwnCloudCredentials credentials
= mClient
.getCredentials();
475 return url
.replace("//", "//" + credentials
.getUsername() + ":" + credentials
.getAuthToken() + "@");
477 } catch (AccountUtils
.AccountNotFoundException e
) {
480 } catch (InterruptedException e
) {
482 } catch (ExecutionException e
) {
488 public static class ClientGenerationTask
extends AsyncTask
<Object
, Void
, OwnCloudClient
> {
490 protected OwnCloudClient
doInBackground(Object
... params
) {
491 Object account
= params
[0];
492 if (account
instanceof OwnCloudAccount
){
494 OwnCloudAccount ocAccount
= (OwnCloudAccount
) account
;
495 return OwnCloudClientManagerFactory
.getDefaultSingleton().
496 getClientFor(ocAccount
, MainApp
.getAppContext());
497 } catch (AccountUtils
.AccountNotFoundException e
) {
499 } catch (OperationCanceledException e
) {
501 } catch (AuthenticatorException e
) {
503 } catch (IOException e
) {
513 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
516 * Called when the file is ready to be played.
518 * Just starts the playback.
520 * @param vp {@link MediaPlayer} instance performing the playback.
523 public void onPrepared(MediaPlayer vp
) {
524 Log_OC
.v(TAG
, "onPrepared");
525 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
527 mVideoPreview
.start();
529 mMediaController
.setEnabled(true
);
530 mMediaController
.updatePausePlay();
536 * Called when the file is finished playing.
538 * Finishes the activity.
540 * @param mp {@link MediaPlayer} instance performing the playback.
543 public void onCompletion(MediaPlayer mp
) {
544 Log_OC
.v(TAG
, "completed");
546 mVideoPreview
.seekTo(0);
547 } // else : called from onError()
548 mMediaController
.updatePausePlay();
553 * Called when an error in playback occurs.
555 * @param mp {@link MediaPlayer} instance performing the playback.
556 * @param what Type of error
557 * @param extra Extra code specific to the error
560 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
561 MediaService
.streamWithExternalApp(mUri
, getActivity()).show();
567 public void onPause() {
568 Log_OC
.v(TAG
, "onPause");
573 public void onResume() {
575 Log_OC
.v(TAG
, "onResume");
579 public void onDestroy() {
580 Log_OC
.v(TAG
, "onDestroy");
585 public void onStop() {
586 Log_OC
.v(TAG
, "onStop");
589 if (mMediaServiceConnection
!= null
) {
590 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
591 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
592 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
594 getActivity().unbindService(mMediaServiceConnection
);
595 mMediaServiceConnection
= null
;
596 mMediaServiceBinder
= null
;
603 public boolean onTouch(View v
, MotionEvent event
) {
604 if (event
.getAction() == MotionEvent
.ACTION_DOWN
&& v
== mVideoPreview
) {
605 // added a margin on the left to avoid interfering with gesture to open navigation drawer
606 if (event
.getX() / Resources
.getSystem().getDisplayMetrics().density
> 24.0) {
607 startFullScreenVideo();
615 private void startFullScreenVideo() {
616 Intent i
= new Intent(getActivity(), PreviewVideoActivity
.class);
617 i
.putExtra(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
618 i
.putExtra(FileActivity
.EXTRA_FILE
, getFile());
619 i
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPreview
.isPlaying());
620 mVideoPreview
.pause();
621 i
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPreview
.getCurrentPosition());
622 startActivityForResult(i
, 0);
626 public void onConfigurationChanged(Configuration newConfig
) {
627 Log_OC
.v(TAG
, "onConfigurationChanged " + this);
631 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
632 Log_OC
.v(TAG
, "onActivityResult " + this);
633 super.onActivityResult(requestCode
, resultCode
, data
);
634 if (resultCode
== Activity
.RESULT_OK
) {
635 mSavedPlaybackPosition
= data
.getExtras().getInt(
636 PreviewVideoActivity
.EXTRA_START_POSITION
);
637 mAutoplay
= data
.getExtras().getBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
);
642 private void playAudio() {
643 OCFile file
= getFile();
644 if (!mMediaServiceBinder
.isPlaying(file
)) {
645 Log_OC
.d(TAG
, "starting playback of " + file
.getStoragePath());
646 mMediaServiceBinder
.start(mAccount
, file
, mAutoplay
, mSavedPlaybackPosition
);
650 if (!mMediaServiceBinder
.isPlaying() && mAutoplay
) {
651 mMediaServiceBinder
.start();
652 mMediaController
.updatePausePlay();
658 private void bindMediaService() {
659 Log_OC
.d(TAG
, "Binding to MediaService...");
660 if (mMediaServiceConnection
== null
) {
661 mMediaServiceConnection
= new MediaServiceConnection();
663 getActivity().bindService( new Intent(getActivity(),
665 mMediaServiceConnection
,
666 Context
.BIND_AUTO_CREATE
);
667 // follow the flow in MediaServiceConnection#onServiceConnected(...)
670 /** Defines callbacks for service binding, passed to bindService() */
671 private class MediaServiceConnection
implements ServiceConnection
{
674 public void onServiceConnected(ComponentName component
, IBinder service
) {
675 if (getActivity() != null
) {
676 if (component
.equals(
677 new ComponentName(getActivity(), MediaService
.class))) {
678 Log_OC
.d(TAG
, "Media service connected");
679 mMediaServiceBinder
= (MediaServiceBinder
) service
;
680 if (mMediaServiceBinder
!= null
) {
681 prepareMediaController();
682 playAudio(); // do not wait for the touch of nobody to play audio
684 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
688 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
694 private void prepareMediaController() {
695 mMediaServiceBinder
.registerMediaController(mMediaController
);
696 if (mMediaController
!= null
) {
697 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
698 mMediaController
.setEnabled(true
);
699 mMediaController
.updatePausePlay();
704 public void onServiceDisconnected(ComponentName component
) {
705 if (component
.equals(new ComponentName(getActivity(), MediaService
.class))) {
706 Log_OC
.w(TAG
, "Media service suddenly disconnected");
707 if (mMediaController
!= null
) {
708 mMediaController
.setMediaPlayer(null
);
713 "No media controller to release when disconnected from media service",
714 Toast
.LENGTH_SHORT
).show();
716 mMediaServiceBinder
= null
;
717 mMediaServiceConnection
= null
;
724 * Opens the previewed file with an external application.
726 private void openFile() {
728 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
733 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
736 * @param file File to test if can be previewed.
737 * @return 'True' if the file can be handled by the fragment.
739 public static boolean canBePreviewed(OCFile file
) {
740 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
744 public void stopPreview(boolean stopAudio
) {
745 OCFile file
= getFile();
746 if (file
.isAudio() && stopAudio
) {
747 mMediaServiceBinder
.pause();
751 if (file
.isVideo()) {
752 mVideoPreview
.stopPlayback();
759 * Finishes the preview
761 private void finish() {
762 getActivity().onBackPressed();
766 public int getPosition() {
768 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
770 Log_OC
.v(TAG
, "getting position: " + mSavedPlaybackPosition
);
771 return mSavedPlaybackPosition
;
774 public boolean isPlaying() {
776 mAutoplay
= mVideoPreview
.isPlaying();