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
.net
.Uri
;
31 import android
.os
.AsyncTask
;
32 import android
.support
.v7
.app
.AlertDialog
;
33 import android
.content
.ComponentName
;
34 import android
.content
.Context
;
35 import android
.content
.DialogInterface
;
36 import android
.content
.Intent
;
37 import android
.content
.ServiceConnection
;
38 import android
.content
.res
.Configuration
;
39 import android
.content
.res
.Resources
;
40 import android
.media
.MediaPlayer
;
41 import android
.media
.MediaPlayer
.OnCompletionListener
;
42 import android
.media
.MediaPlayer
.OnErrorListener
;
43 import android
.media
.MediaPlayer
.OnPreparedListener
;
44 import android
.os
.Bundle
;
45 import android
.os
.IBinder
;
46 import android
.view
.LayoutInflater
;
47 import android
.view
.Menu
;
48 import android
.view
.MenuInflater
;
49 import android
.view
.MenuItem
;
50 import android
.view
.MotionEvent
;
51 import android
.view
.View
;
52 import android
.view
.View
.OnTouchListener
;
53 import android
.view
.ViewGroup
;
54 import android
.widget
.ImageView
;
55 import android
.widget
.Toast
;
56 import android
.widget
.VideoView
;
58 import com
.owncloud
.android
.MainApp
;
59 import com
.owncloud
.android
.R
;
60 import com
.owncloud
.android
.datamodel
.OCFile
;
61 import com
.owncloud
.android
.datamodel
.ThumbnailsCacheManager
;
62 import com
.owncloud
.android
.files
.FileMenuFilter
;
63 import com
.owncloud
.android
.lib
.common
.OwnCloudAccount
;
64 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
65 import com
.owncloud
.android
.lib
.common
.OwnCloudClientManagerFactory
;
66 import com
.owncloud
.android
.lib
.common
.OwnCloudCredentials
;
67 import com
.owncloud
.android
.lib
.common
.accounts
.AccountUtils
;
68 import com
.owncloud
.android
.lib
.common
.utils
.Log_OC
;
69 import com
.owncloud
.android
.media
.MediaControlView
;
70 import com
.owncloud
.android
.media
.MediaService
;
71 import com
.owncloud
.android
.media
.MediaServiceBinder
;
72 import com
.owncloud
.android
.ui
.activity
.FileActivity
;
73 import com
.owncloud
.android
.ui
.dialog
.ConfirmationDialogFragment
;
74 import com
.owncloud
.android
.ui
.dialog
.RemoveFileDialogFragment
;
75 import com
.owncloud
.android
.ui
.fragment
.FileFragment
;
77 import java
.io
.IOException
;
78 import java
.util
.concurrent
.ExecutionException
;
82 * This fragment shows a preview of a downloaded media file (audio or video).
84 * Trying to get an instance with NULL {@link OCFile} or ownCloud {@link Account} values will
85 * produce an {@link IllegalStateException}.
87 * By now, if the {@link OCFile} passed is not downloaded, an {@link IllegalStateException} is
88 * generated on instantiation too.
90 public class PreviewMediaFragment
extends FileFragment
implements
93 public static final String EXTRA_FILE
= "FILE";
94 public static final String EXTRA_ACCOUNT
= "ACCOUNT";
95 private static final String EXTRA_PLAY_POSITION
= "PLAY_POSITION";
96 private static final String EXTRA_PLAYING
= "PLAYING";
99 private Account mAccount
;
100 private ImageView mImagePreview
;
101 private VideoView mVideoPreview
;
102 private int mSavedPlaybackPosition
;
105 private MediaServiceBinder mMediaServiceBinder
= null
;
106 private MediaControlView mMediaController
= null
;
107 private MediaServiceConnection mMediaServiceConnection
= null
;
108 private VideoHelper mVideoHelper
;
109 private boolean mAutoplay
;
110 public boolean mPrepared
;
112 private static final String TAG
= PreviewMediaFragment
.class.getSimpleName();
116 * Creates a fragment to preview a file.
118 * When 'fileToDetail' or 'ocAccount' are null
120 * @param fileToDetail An {@link OCFile} to preview in the fragment
121 * @param ocAccount An ownCloud account; needed to start downloads
123 public PreviewMediaFragment(
126 int startPlaybackPosition
,
130 mAccount
= ocAccount
;
131 mSavedPlaybackPosition
= startPlaybackPosition
;
132 mAutoplay
= autoplay
;
137 * Creates an empty fragment for previews.
139 * MUST BE KEPT: the system uses it when tries to reinstantiate a fragment automatically
140 * (for instance, when the device is turned a aside).
142 * DO NOT CALL IT: an {@link OCFile} and {@link Account} must be provided for a successful
145 public PreviewMediaFragment() {
148 mSavedPlaybackPosition
= 0;
157 public void onCreate(Bundle savedInstanceState
) {
158 super.onCreate(savedInstanceState
);
159 setHasOptionsMenu(true
);
167 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
168 Bundle savedInstanceState
) {
169 super.onCreateView(inflater
, container
, savedInstanceState
);
170 Log_OC
.v(TAG
, "onCreateView");
173 mView
= inflater
.inflate(R
.layout
.file_preview
, container
, false
);
175 mImagePreview
= (ImageView
) mView
.findViewById(R
.id
.image_preview
);
176 mVideoPreview
= (VideoView
) mView
.findViewById(R
.id
.video_preview
);
177 mVideoPreview
.setOnTouchListener(this);
179 mMediaController
= (MediaControlView
) mView
.findViewById(R
.id
.media_controller
);
189 public void onActivityCreated(Bundle savedInstanceState
) {
190 super.onActivityCreated(savedInstanceState
);
191 Log_OC
.v(TAG
, "onActivityCreated");
193 OCFile file
= getFile();
194 if (savedInstanceState
== null
) {
196 throw new IllegalStateException("Instanced with a NULL OCFile");
198 if (mAccount
== null
) {
199 throw new IllegalStateException("Instanced with a NULL ownCloud Account");
203 file
= (OCFile
) savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_FILE
);
205 mAccount
= savedInstanceState
.getParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
);
206 mSavedPlaybackPosition
=
207 savedInstanceState
.getInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
);
208 mAutoplay
= savedInstanceState
.getBoolean(PreviewMediaFragment
.EXTRA_PLAYING
);
212 if (file
.isVideo()) {
213 mVideoPreview
.setVisibility(View
.VISIBLE
);
214 mImagePreview
.setVisibility(View
.GONE
);
219 mVideoPreview
.setVisibility(View
.GONE
);
220 mImagePreview
.setVisibility(View
.VISIBLE
);
221 extractAndSetCoverArt(file
);
228 * tries to read the cover art from the audio file and sets it as cover art.
230 * @param file audio file with potential cover art
232 private void extractAndSetCoverArt(OCFile file
) {
233 if (file
.isAudio()) {
235 MediaMetadataRetriever mmr
= new MediaMetadataRetriever();
236 mmr
.setDataSource(file
.getStoragePath());
237 byte[] data
= mmr
.getEmbeddedPicture();
239 Bitmap bitmap
= BitmapFactory
.decodeByteArray(data
, 0, data
.length
);
240 mImagePreview
.setImageBitmap(bitmap
); //associated cover art in bitmap
242 mImagePreview
.setImageResource(R
.drawable
.logo
);
244 } catch (Throwable t
) {
245 mImagePreview
.setImageResource(R
.drawable
.logo
);
255 public void onSaveInstanceState(Bundle outState
) {
256 super.onSaveInstanceState(outState
);
257 Log_OC
.v(TAG
, "onSaveInstanceState");
259 outState
.putParcelable(PreviewMediaFragment
.EXTRA_FILE
, getFile());
260 outState
.putParcelable(PreviewMediaFragment
.EXTRA_ACCOUNT
, mAccount
);
262 if (getFile().isVideo()) {
263 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
264 mAutoplay
= mVideoPreview
.isPlaying();
265 outState
.putInt(PreviewMediaFragment
.EXTRA_PLAY_POSITION
, mSavedPlaybackPosition
);
266 outState
.putBoolean(PreviewMediaFragment
.EXTRA_PLAYING
, mAutoplay
);
270 PreviewMediaFragment
.EXTRA_PLAY_POSITION
,
271 mMediaServiceBinder
.getCurrentPosition());
273 PreviewMediaFragment
.EXTRA_PLAYING
, mMediaServiceBinder
.isPlaying());
279 public void onStart() {
281 Log_OC
.v(TAG
, "onStart");
283 OCFile file
= getFile();
285 if (file
.isAudio()) {
290 if (file
.isVideo()) {
299 private void stopAudio() {
300 Intent i
= new Intent(getActivity(), MediaService
.class);
301 i
.setAction(MediaService
.ACTION_STOP_ALL
);
302 getActivity().startService(i
);
310 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
) {
311 super.onCreateOptionsMenu(menu
, inflater
);
312 inflater
.inflate(R
.menu
.file_actions_menu
, menu
);
320 public void onPrepareOptionsMenu(Menu menu
) {
321 super.onPrepareOptionsMenu(menu
);
323 if (mContainerActivity
.getStorageManager() != null
) {
324 FileMenuFilter mf
= new FileMenuFilter(
326 mContainerActivity
.getStorageManager().getAccount(),
333 // additional restriction for this fragment
334 // TODO allow renaming in PreviewImageFragment
335 MenuItem item
= menu
.findItem(R
.id
.action_rename_file
);
337 item
.setVisible(false
);
338 item
.setEnabled(false
);
341 // additional restriction for this fragment
342 item
= menu
.findItem(R
.id
.action_move
);
344 item
.setVisible(false
);
345 item
.setEnabled(false
);
348 // additional restriction for this fragment
349 item
= menu
.findItem(R
.id
.action_copy
);
351 item
.setVisible(false
);
352 item
.setEnabled(false
);
361 public boolean onOptionsItemSelected(MenuItem item
) {
362 switch (item
.getItemId()) {
363 case R
.id
.action_share_file
: {
365 mContainerActivity
.getFileOperationsHelper().shareFileWithLink(getFile());
368 case R
.id
.action_share_with_users
: {
372 case R
.id
.action_unshare_file
: {
374 mContainerActivity
.getFileOperationsHelper().unshareFileWithLink(getFile());
377 case R
.id
.action_open_file_with
: {
381 case R
.id
.action_remove_file
: {
382 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
383 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
386 case R
.id
.action_see_details
: {
390 case R
.id
.action_send_file
: {
394 case R
.id
.action_sync_file
: {
395 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
398 case R
.id
.action_favorite_file
:{
399 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), true
);
402 case R
.id
.action_unfavorite_file
:{
403 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), false
);
413 * Update the file of the fragment with file value
415 * @param file Replaces the held file with a new one
417 public void updateFile(OCFile file
) {
421 private void sendFile() {
423 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
427 private void seeDetails() {
429 mContainerActivity
.showDetails(getFile());
432 private void seeShareFile() {
434 mContainerActivity
.getFileOperationsHelper().showShareFile(getFile());
437 private void prepareVideo() {
438 // create helper to get more control on the playback
439 mVideoHelper
= new VideoHelper();
440 mVideoPreview
.setOnPreparedListener(mVideoHelper
);
441 mVideoPreview
.setOnCompletionListener(mVideoHelper
);
442 mVideoPreview
.setOnErrorListener(mVideoHelper
);
445 @SuppressWarnings("static-access")
446 private void playVideo() {
447 // create and prepare control panel for the user
448 mMediaController
.setMediaPlayer(mVideoPreview
);
450 // load the video file in the video player ;
451 // when done, VideoHelper#onPrepared() will be called
452 if (getFile().isDown()) {
453 mUri
= getFile().getStoragePath();
455 Context context
= MainApp
.getAppContext();
456 Account account
= mContainerActivity
.getStorageManager().getAccount();
458 mUri
= generateUrlWithCredentials(account
, context
, getFile());
461 mVideoPreview
.setVideoURI(getFile().getStorageUri());
464 public static String
generateUrlWithCredentials(Account account
, Context context
, OCFile file
){
465 OwnCloudAccount ocAccount
= null
;
467 ocAccount
= new OwnCloudAccount(account
, context
);
469 final ClientGenerationTask task
= new ClientGenerationTask();
470 task
.execute(ocAccount
);
472 OwnCloudClient mClient
= task
.get();
473 String url
= AccountUtils
.constructFullURLForAccount(context
, account
) + Uri
.encode(file
.getRemotePath(), "/");
474 OwnCloudCredentials credentials
= mClient
.getCredentials();
476 return url
.replace("//", "//" + credentials
.getUsername() + ":" + credentials
.getAuthToken() + "@");
478 } catch (AccountUtils
.AccountNotFoundException e
) {
481 } catch (InterruptedException e
) {
483 } catch (ExecutionException e
) {
489 public static class ClientGenerationTask
extends AsyncTask
<Object
, Void
, OwnCloudClient
> {
491 protected OwnCloudClient
doInBackground(Object
... params
) {
492 Object account
= params
[0];
493 if (account
instanceof OwnCloudAccount
){
495 OwnCloudAccount ocAccount
= (OwnCloudAccount
) account
;
496 return OwnCloudClientManagerFactory
.getDefaultSingleton().
497 getClientFor(ocAccount
, MainApp
.getAppContext());
498 } catch (AccountUtils
.AccountNotFoundException e
) {
500 } catch (OperationCanceledException e
) {
502 } catch (AuthenticatorException e
) {
504 } catch (IOException e
) {
514 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
517 * Called when the file is ready to be played.
519 * Just starts the playback.
521 * @param vp {@link MediaPlayer} instance performing the playback.
524 public void onPrepared(MediaPlayer vp
) {
525 Log_OC
.v(TAG
, "onPrepared");
526 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
528 mVideoPreview
.start();
530 mMediaController
.setEnabled(true
);
531 mMediaController
.updatePausePlay();
537 * Called when the file is finished playing.
539 * Finishes the activity.
541 * @param mp {@link MediaPlayer} instance performing the playback.
544 public void onCompletion(MediaPlayer mp
) {
545 Log_OC
.v(TAG
, "completed");
547 mVideoPreview
.seekTo(0);
548 } // else : called from onError()
549 mMediaController
.updatePausePlay();
554 * Called when an error in playback occurs.
556 * @param mp {@link MediaPlayer} instance performing the playback.
557 * @param what Type of error
558 * @param extra Extra code specific to the error
561 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
562 MediaService
.streamWithExternalApp(mUri
, getActivity()).show();
568 public void onPause() {
569 Log_OC
.v(TAG
, "onPause");
574 public void onResume() {
576 Log_OC
.v(TAG
, "onResume");
580 public void onDestroy() {
581 Log_OC
.v(TAG
, "onDestroy");
586 public void onStop() {
587 Log_OC
.v(TAG
, "onStop");
590 if (mMediaServiceConnection
!= null
) {
591 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
592 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
593 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
595 getActivity().unbindService(mMediaServiceConnection
);
596 mMediaServiceConnection
= null
;
597 mMediaServiceBinder
= null
;
604 public boolean onTouch(View v
, MotionEvent event
) {
605 if (event
.getAction() == MotionEvent
.ACTION_DOWN
&& v
== mVideoPreview
) {
606 // added a margin on the left to avoid interfering with gesture to open navigation drawer
607 if (event
.getX() / Resources
.getSystem().getDisplayMetrics().density
> 24.0) {
608 startFullScreenVideo();
616 private void startFullScreenVideo() {
617 Intent i
= new Intent(getActivity(), PreviewVideoActivity
.class);
618 i
.putExtra(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
619 i
.putExtra(FileActivity
.EXTRA_FILE
, getFile());
620 i
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPreview
.isPlaying());
621 mVideoPreview
.pause();
622 i
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPreview
.getCurrentPosition());
623 startActivityForResult(i
, 0);
627 public void onConfigurationChanged(Configuration newConfig
) {
628 Log_OC
.v(TAG
, "onConfigurationChanged " + this);
632 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
633 Log_OC
.v(TAG
, "onActivityResult " + this);
634 super.onActivityResult(requestCode
, resultCode
, data
);
635 if (resultCode
== Activity
.RESULT_OK
) {
636 mSavedPlaybackPosition
= data
.getExtras().getInt(
637 PreviewVideoActivity
.EXTRA_START_POSITION
);
638 mAutoplay
= data
.getExtras().getBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
);
643 private void playAudio() {
644 OCFile file
= getFile();
645 if (!mMediaServiceBinder
.isPlaying(file
)) {
646 Log_OC
.d(TAG
, "starting playback of " + file
.getStoragePath());
647 mMediaServiceBinder
.start(mAccount
, file
, mAutoplay
, mSavedPlaybackPosition
);
651 if (!mMediaServiceBinder
.isPlaying() && mAutoplay
) {
652 mMediaServiceBinder
.start();
653 mMediaController
.updatePausePlay();
659 private void bindMediaService() {
660 Log_OC
.d(TAG
, "Binding to MediaService...");
661 if (mMediaServiceConnection
== null
) {
662 mMediaServiceConnection
= new MediaServiceConnection();
664 getActivity().bindService( new Intent(getActivity(),
666 mMediaServiceConnection
,
667 Context
.BIND_AUTO_CREATE
);
668 // follow the flow in MediaServiceConnection#onServiceConnected(...)
671 /** Defines callbacks for service binding, passed to bindService() */
672 private class MediaServiceConnection
implements ServiceConnection
{
675 public void onServiceConnected(ComponentName component
, IBinder service
) {
676 if (getActivity() != null
) {
677 if (component
.equals(
678 new ComponentName(getActivity(), MediaService
.class))) {
679 Log_OC
.d(TAG
, "Media service connected");
680 mMediaServiceBinder
= (MediaServiceBinder
) service
;
681 if (mMediaServiceBinder
!= null
) {
682 prepareMediaController();
683 playAudio(); // do not wait for the touch of nobody to play audio
685 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
689 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
695 private void prepareMediaController() {
696 mMediaServiceBinder
.registerMediaController(mMediaController
);
697 if (mMediaController
!= null
) {
698 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
699 mMediaController
.setEnabled(true
);
700 mMediaController
.updatePausePlay();
705 public void onServiceDisconnected(ComponentName component
) {
706 if (component
.equals(new ComponentName(getActivity(), MediaService
.class))) {
707 Log_OC
.w(TAG
, "Media service suddenly disconnected");
708 if (mMediaController
!= null
) {
709 mMediaController
.setMediaPlayer(null
);
714 "No media controller to release when disconnected from media service",
715 Toast
.LENGTH_SHORT
).show();
717 mMediaServiceBinder
= null
;
718 mMediaServiceConnection
= null
;
725 * Opens the previewed file with an external application.
727 private void openFile() {
729 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
734 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
737 * @param file File to test if can be previewed.
738 * @return 'True' if the file can be handled by the fragment.
740 public static boolean canBePreviewed(OCFile file
) {
741 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
745 public void stopPreview(boolean stopAudio
) {
746 OCFile file
= getFile();
747 if (file
.isAudio() && stopAudio
) {
748 mMediaServiceBinder
.pause();
752 if (file
.isVideo()) {
753 mVideoPreview
.stopPlayback();
760 * Finishes the preview
762 private void finish() {
763 getActivity().onBackPressed();
767 public int getPosition() {
769 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
771 Log_OC
.v(TAG
, "getting position: " + mSavedPlaybackPosition
);
772 return mSavedPlaybackPosition
;
775 public boolean isPlaying() {
777 mAutoplay
= mVideoPreview
.isPlaying();