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
.net
.Uri
;
44 import android
.os
.Build
;
45 import android
.os
.Bundle
;
46 import android
.os
.IBinder
;
47 import android
.view
.LayoutInflater
;
48 import android
.view
.Menu
;
49 import android
.view
.MenuInflater
;
50 import android
.view
.MenuItem
;
51 import android
.view
.MotionEvent
;
52 import android
.view
.View
;
53 import android
.view
.View
.OnTouchListener
;
54 import android
.view
.ViewGroup
;
55 import android
.widget
.ImageView
;
56 import android
.widget
.Toast
;
57 import android
.widget
.VideoView
;
59 import com
.owncloud
.android
.MainApp
;
60 import com
.owncloud
.android
.R
;
61 import com
.owncloud
.android
.datamodel
.OCFile
;
62 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
63 import com
.owncloud
.android
.files
.FileMenuFilter
;
64 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
65 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
66 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
67 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentials
;
68 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
;
69 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
70 import com
.owncloud
.android
.media
.MediaControlView
;
71 import com
.owncloud
.android
.media
.MediaService
;
72 import com
.owncloud
.android
.media
.MediaServiceBinder
;
73 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
74 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
75 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
76 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
78 import java
.io
.IOException
;
79 import java
.util
.concurrent
.ExecutionException
;
83 * This fragment shows a preview of a downloaded media file (audio or video).
85 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will
86 * produce an {@link IllegalStateException}.
88 * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is
89 * generated on instantiation too.
91 public class PreviewMediaFragment
extends FileFragment
implements
94 public static final String EXTRA_FILE
= "FILE";
95 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
96 private static final String EXTRA_PLAY_POSITION
= "PLAY_POSITION";
97 private static final String EXTRA_PLAYING
= "PLAYING";
100 private Account mAccount
;
101 private ImageView mImagePreview
;
102 private VideoView mVideoPreview
;
103 private int mSavedPlaybackPosition
;
106 private MediaServiceBinder mMediaServiceBinder
= null
;
107 private MediaControlView mMediaController
= null
;
108 private MediaServiceConnection mMediaServiceConnection
= null
;
109 private VideoHelper mVideoHelper
;
110 private boolean mAutoplay
;
111 public boolean mPrepared
;
113 private static final String TAG
= PreviewMediaFragment
.class.getSimpleName();
117 * Creates a fragment to preview a file.
119 * When 'fileToDetail' or 'ocAccount' are null
121 * @param fileToDetail An {@link OCFile} to preview in the fragment
122 * @param ocAccount An ownCloud account; needed to start downloads
124 public PreviewMediaFragment(
127 int startPlaybackPosition
,
131 mAccount
= ocAccount
;
132 mSavedPlaybackPosition
= startPlaybackPosition
;
133 mAutoplay
= autoplay
;
138 * Creates an empty fragment for previews.
140 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
141 * (for instance, when the device is turned a aside).
143 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
146 public PreviewMediaFragment() {
149 mSavedPlaybackPosition
= 0;
158 public void onCreate(Bundle savedInstanceState
) {
159 super.onCreate(savedInstanceState
);
160 setHasOptionsMenu(true
);
168 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
169 Bundle savedInstanceState
) {
170 super.onCreateView(inflater
, container
, savedInstanceState
);
171 Log_OC
.e(TAG
, "onCreateView");
174 mView
= inflater
.inflate(R
.layout
.file_preview
, container
, false
);
176 mImagePreview
= (ImageView
) mView
.findViewById(R
.id
.image_preview
);
177 mVideoPreview
= (VideoView
) mView
.findViewById(R
.id
.video_preview
);
178 mVideoPreview
.setOnTouchListener(this);
180 mMediaController
= (MediaControlView
) mView
.findViewById(R
.id
.media_controller
);
190 public void onActivityCreated(Bundle savedInstanceState
) {
191 super.onActivityCreated(savedInstanceState
);
192 Log_OC
.e(TAG
, "onActivityCreated");
194 OCFile file
= getFile();
195 if (savedInstanceState
== null
) {
197 throw new IllegalStateException("Instanced with a NULL OCFile");
199 if (mAccount
== null
) {
200 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
204 file
= (OCFile
) savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_FILE
);
206 mAccount
= savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
);
207 mSavedPlaybackPosition
=
208 savedInstanceState
.getInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
);
209 mAutoplay
= savedInstanceState
.getBoolean(PreviewMediaFragment
.EXTRA_PLAYING
);
213 if (file
.isVideo()) {
214 mVideoPreview
.setVisibility(View
.VISIBLE
);
215 mImagePreview
.setVisibility(View
.GONE
);
220 mVideoPreview
.setVisibility(View
.GONE
);
221 mImagePreview
.setVisibility(View
.VISIBLE
);
222 extractAndSetCoverArt(file
);
229 * tries to read the cover art from the audio file and sets it as cover art.
231 * @param file audio file with potential cover art
233 private void extractAndSetCoverArt(OCFile file
) {
234 if (file
.isAudio()) {
236 MediaMetadataRetriever mmr
= new MediaMetadataRetriever();
237 mmr
.setDataSource(file
.getStoragePath());
238 byte[] data
= mmr
.getEmbeddedPicture();
240 Bitmap bitmap
= BitmapFactory
.decodeByteArray(data
, 0, data
.length
);
241 mImagePreview
.setImageBitmap(bitmap
); //associated cover art in bitmap
243 mImagePreview
.setImageResource(R
.drawable
.logo
);
245 } catch (Throwable t
) {
246 mImagePreview
.setImageResource(R
.drawable
.logo
);
256 public void onSaveInstanceState(Bundle outState
) {
257 super.onSaveInstanceState(outState
);
258 Log_OC
.e(TAG
, "onSaveInstanceState");
260 outState
.putParcelable(PreviewMediaFragment
.EXTRA_FILE
, getFile());
261 outState
.putParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
, mAccount
);
263 if (getFile().isVideo()) {
264 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
265 mAutoplay
= mVideoPreview
.isPlaying();
266 outState
.putInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
, mSavedPlaybackPosition
);
267 outState
.putBoolean(PreviewMediaFragment
.EXTRA_PLAYING
, mAutoplay
);
271 PreviewMediaFragment
.EXTRA_PLAY_POSITION
,
272 mMediaServiceBinder
.getCurrentPosition());
274 PreviewMediaFragment
.EXTRA_PLAYING
, mMediaServiceBinder
.isPlaying());
280 public void onStart() {
282 Log_OC
.e(TAG
, "onStart");
284 OCFile file
= getFile();
286 if (file
.isAudio()) {
291 if (file
.isVideo()) {
300 private void stopAudio() {
301 Intent i
= new Intent(getActivity(), MediaService
.class);
302 i
.setAction(MediaService
.ACTION_STOP_ALL
);
303 getActivity().startService(i
);
311 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
312 super.onCreateOptionsMenu(menu
, inflater
);
313 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
321 public void onPrepareOptionsMenu(Menu menu
) {
322 super.onPrepareOptionsMenu(menu
);
324 if (mContainerActivity
.getStorageManager() != null
) {
325 FileMenuFilter mf
= new FileMenuFilter(
327 mContainerActivity
.getStorageManager().getAccount(),
334 // additional restriction for this fragment
335 // TODO allow renaming in PreviewImageFragment
336 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
338 item
.setVisible(false
);
339 item
.setEnabled(false
);
342 // additional restriction for this fragment
343 item
= menu
.findItem(R
.id
.action_move
);
345 item
.setVisible(false
);
346 item
.setEnabled(false
);
349 // additional restriction for this fragment
350 item
= menu
.findItem(R
.id
.action_copy
);
352 item
.setVisible(false
);
353 item
.setEnabled(false
);
362 public boolean onOptionsItemSelected(MenuItem item
) {
363 switch (item
.getItemId()) {
364 case R
.id
.action_share_file
: {
366 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
369 case R
.id
.action_unshare_file
: {
371 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
374 case R
.id
.action_open_file_with
: {
378 case R
.id
.action_remove_file
: {
379 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
380 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
383 case R
.id
.action_see_details
: {
387 case R
.id
.action_send_file
: {
391 case R
.id
.action_sync_file
: {
392 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
395 case R
.id
.action_favorite_file
:{
396 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), true
);
399 case R
.id
.action_unfavorite_file
:{
400 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), false
);
410 * Update the file of the fragment with file value
414 public void updateFile(OCFile file
) {
418 private void sendFile() {
420 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
424 private void seeDetails() {
426 mContainerActivity
.showDetails(getFile());
430 private void prepareVideo() {
431 // create helper to get more control on the playback
432 mVideoHelper
= new VideoHelper();
433 mVideoPreview
.setOnPreparedListener(mVideoHelper
);
434 mVideoPreview
.setOnCompletionListener(mVideoHelper
);
435 mVideoPreview
.setOnErrorListener(mVideoHelper
);
438 @SuppressWarnings("static-access")
439 private void playVideo() {
440 // create and prepare control panel for the user
441 mMediaController
.setMediaPlayer(mVideoPreview
);
443 // load the video file in the video player ;
444 // when done, VideoHelper#onPrepared() will be called
445 if (getFile().isDown()) {
446 mUri
= getFile().getStoragePath();
448 Context context
= MainApp
.getAppContext();
449 Account account
= mContainerActivity
.getStorageManager().getAccount();
451 mUri
= generateUrlWithCredentials(account
, context
, getFile());
454 mVideoPreview
.setVideoPath(mUri
);
457 public static String
generateUrlWithCredentials(Account account
, Context context
, OCFile file
){
458 OwnCloudAccount ocAccount
= null
;
460 ocAccount
= new OwnCloudAccount(account
, context
);
462 final ClientGenerationTask task
= new ClientGenerationTask();
463 task
.execute(ocAccount
);
465 OwnCloudClient mClient
= task
.get();
466 String url
= AccountUtils
.constructFullURLForAccount(context
, account
) + Uri
.encode(file
.getRemotePath(), "/");
467 OwnCloudCredentials credentials
= mClient
.getCredentials();
469 return url
.replace("//", "//" + credentials
.getUsername() + ":" + credentials
.getAuthToken() + "@");
471 } catch (AccountUtils
.AccountNotFoundException e
) {
474 } catch (InterruptedException e
) {
476 } catch (ExecutionException e
) {
482 public static class ClientGenerationTask
extends AsyncTask
<Object
, Void
, OwnCloudClient
> {
484 protected OwnCloudClient
doInBackground(Object
... params
) {
485 Object account
= params
[0];
486 if (account
instanceof OwnCloudAccount
){
488 OwnCloudAccount ocAccount
= (OwnCloudAccount
) account
;
489 return OwnCloudClientManagerFactory
.getDefaultSingleton().
490 getClientFor(ocAccount
, MainApp
.getAppContext());
491 } catch (AccountUtils
.AccountNotFoundException e
) {
493 } catch (OperationCanceledException e
) {
495 } catch (AuthenticatorException e
) {
497 } catch (IOException e
) {
507 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
510 * Called when the file is ready to be played.
512 * Just starts the playback.
514 * @param vp {@link MediaPlayer} instance performing the playback.
517 public void onPrepared(MediaPlayer vp
) {
518 Log_OC
.e(TAG
, "onPrepared");
519 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
521 mVideoPreview
.start();
523 mMediaController
.setEnabled(true
);
524 mMediaController
.updatePausePlay();
530 * Called when the file is finished playing.
532 * Finishes the activity.
534 * @param mp {@link MediaPlayer} instance performing the playback.
537 public void onCompletion(MediaPlayer mp
) {
538 Log_OC
.e(TAG
, "completed");
540 mVideoPreview
.seekTo(0);
541 // next lines are necessary to work around undesired video loops
542 if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD
) {
543 mVideoPreview
.pause();
547 if (Build
.VERSION
.SDK_INT
== Build
.VERSION_CODES
.GINGERBREAD_MR1
) {
548 // mVideePreview.pause() is not enough
550 mMediaController
.setEnabled(false
);
551 mVideoPreview
.stopPlayback();
553 mSavedPlaybackPosition
= 0;
554 mVideoPreview
.setVideoPath(getFile().getStoragePath());
557 } // else : called from onError()
558 mMediaController
.updatePausePlay();
563 * Called when an error in playback occurs.
565 * @param mp {@link MediaPlayer} instance performing the playback.
566 * @param what Type of error
567 * @param extra Extra code specific to the error
570 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
571 MediaService
.streamWithExternalApp(mUri
, getActivity()).show();
577 public void onPause() {
578 Log_OC
.e(TAG
, "onPause");
583 public void onResume() {
585 Log_OC
.e(TAG
, "onResume");
589 public void onDestroy() {
590 Log_OC
.e(TAG
, "onDestroy");
595 public void onStop() {
596 Log_OC
.e(TAG
, "onStop");
599 if (mMediaServiceConnection
!= null
) {
600 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
601 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
602 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
604 getActivity().unbindService(mMediaServiceConnection
);
605 mMediaServiceConnection
= null
;
606 mMediaServiceBinder
= null
;
613 public boolean onTouch(View v
, MotionEvent event
) {
614 if (event
.getAction() == MotionEvent
.ACTION_DOWN
&& v
== mVideoPreview
) {
615 // added a margin on the left to avoid interfering with gesture to open navigation drawer
616 if (event
.getX() / Resources
.getSystem().getDisplayMetrics().density
> 24.0) {
617 startFullScreenVideo();
625 private void startFullScreenVideo() {
626 Intent i
= new Intent(getActivity(), PreviewVideoActivity
.class);
627 i
.putExtra(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
628 i
.putExtra(FileActivity
.EXTRA_FILE
, getFile());
629 i
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPreview
.isPlaying());
630 mVideoPreview
.pause();
631 i
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPreview
.getCurrentPosition());
632 startActivityForResult(i
, 0);
636 public void onConfigurationChanged(Configuration newConfig
) {
637 Log_OC
.e(TAG
, "onConfigurationChanged " + this);
641 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
642 Log_OC
.e(TAG
, "onActivityResult " + this);
643 super.onActivityResult(requestCode
, resultCode
, data
);
644 if (resultCode
== Activity
.RESULT_OK
) {
645 mSavedPlaybackPosition
= data
.getExtras().getInt(
646 PreviewVideoActivity
.EXTRA_START_POSITION
);
647 mAutoplay
= data
.getExtras().getBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
);
652 private void playAudio() {
653 OCFile file
= getFile();
654 if (!mMediaServiceBinder
.isPlaying(file
)) {
655 Log_OC
.d(TAG
, "starting playback of " + file
.getStoragePath());
656 mMediaServiceBinder
.start(mAccount
, file
, mAutoplay
, mSavedPlaybackPosition
);
660 if (!mMediaServiceBinder
.isPlaying() && mAutoplay
) {
661 mMediaServiceBinder
.start();
662 mMediaController
.updatePausePlay();
668 private void bindMediaService() {
669 Log_OC
.d(TAG
, "Binding to MediaService...");
670 if (mMediaServiceConnection
== null
) {
671 mMediaServiceConnection
= new MediaServiceConnection();
673 getActivity().bindService( new Intent(getActivity(),
675 mMediaServiceConnection
,
676 Context
.BIND_AUTO_CREATE
);
677 // follow the flow in MediaServiceConnection#onServiceConnected(...)
680 /** Defines callbacks for service binding, passed to bindService() */
681 private class MediaServiceConnection
implements ServiceConnection
{
684 public void onServiceConnected(ComponentName component
, IBinder service
) {
685 if (getActivity() != null
) {
686 if (component
.equals(
687 new ComponentName(getActivity(), MediaService
.class))) {
688 Log_OC
.d(TAG
, "Media service connected");
689 mMediaServiceBinder
= (MediaServiceBinder
) service
;
690 if (mMediaServiceBinder
!= null
) {
691 prepareMediaController();
692 playAudio(); // do not wait for the touch of nobody to play audio
694 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
698 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
704 private void prepareMediaController() {
705 mMediaServiceBinder
.registerMediaController(mMediaController
);
706 if (mMediaController
!= null
) {
707 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
708 mMediaController
.setEnabled(true
);
709 mMediaController
.updatePausePlay();
714 public void onServiceDisconnected(ComponentName component
) {
715 if (component
.equals(new ComponentName(getActivity(), MediaService
.class))) {
716 Log_OC
.e(TAG
, "Media service suddenly disconnected");
717 if (mMediaController
!= null
) {
718 mMediaController
.setMediaPlayer(null
);
723 "No media controller to release when disconnected from media service",
724 Toast
.LENGTH_SHORT
).show();
726 mMediaServiceBinder
= null
;
727 mMediaServiceConnection
= null
;
734 * Opens the previewed file with an external application.
736 private void openFile() {
738 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
743 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
746 * @param file File to test if can be previewed.
747 * @return 'True' if the file can be handled by the fragment.
749 public static boolean canBePreviewed(OCFile file
) {
750 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
754 public void stopPreview(boolean stopAudio
) {
755 OCFile file
= getFile();
756 if (file
.isAudio() && stopAudio
) {
757 mMediaServiceBinder
.pause();
761 if (file
.isVideo()) {
762 mVideoPreview
.stopPlayback();
769 * Finishes the preview
771 private void finish() {
772 getActivity().onBackPressed();
776 public int getPosition() {
778 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
780 Log_OC
.e(TAG
, "getting position: " + mSavedPlaybackPosition
);
781 return mSavedPlaybackPosition
;
784 public boolean isPlaying() {
786 mAutoplay
= mVideoPreview
.isPlaying();