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
: {
367 case R
.id
.action_open_file_with
: {
371 case R
.id
.action_remove_file
: {
372 RemoveFileDialogFragment dialog
= RemoveFileDialogFragment
.newInstance(getFile());
373 dialog
.show(getFragmentManager(), ConfirmationDialogFragment
.FTAG_CONFIRMATION
);
376 case R
.id
.action_see_details
: {
380 case R
.id
.action_send_file
: {
384 case R
.id
.action_sync_file
: {
385 mContainerActivity
.getFileOperationsHelper().syncFile(getFile());
388 case R
.id
.action_favorite_file
:{
389 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), true
);
392 case R
.id
.action_unfavorite_file
:{
393 mContainerActivity
.getFileOperationsHelper().toggleFavorite(getFile(), false
);
403 * Update the file of the fragment with file value
405 * @param file Replaces the held file with a new one
407 public void updateFile(OCFile file
) {
411 private void sendFile() {
413 mContainerActivity
.getFileOperationsHelper().sendDownloadedFile(getFile());
417 private void seeDetails() {
419 mContainerActivity
.showDetails(getFile());
422 private void seeShareFile() {
424 mContainerActivity
.getFileOperationsHelper().showShareFile(getFile());
427 private void prepareVideo() {
428 // create helper to get more control on the playback
429 mVideoHelper
= new VideoHelper();
430 mVideoPreview
.setOnPreparedListener(mVideoHelper
);
431 mVideoPreview
.setOnCompletionListener(mVideoHelper
);
432 mVideoPreview
.setOnErrorListener(mVideoHelper
);
435 @SuppressWarnings("static-access")
436 private void playVideo() {
437 // create and prepare control panel for the user
438 mMediaController
.setMediaPlayer(mVideoPreview
);
440 // load the video file in the video player ;
441 // when done, VideoHelper#onPrepared() will be called
442 if (getFile().isDown()) {
443 mUri
= getFile().getStoragePath();
445 Context context
= MainApp
.getAppContext();
446 Account account
= mContainerActivity
.getStorageManager().getAccount();
448 mUri
= generateUrlWithCredentials(account
, context
, getFile());
451 mVideoPreview
.setVideoURI(getFile().getStorageUri());
454 public static String
generateUrlWithCredentials(Account account
, Context context
, OCFile file
){
455 OwnCloudAccount ocAccount
= null
;
457 ocAccount
= new OwnCloudAccount(account
, context
);
459 final ClientGenerationTask task
= new ClientGenerationTask();
460 task
.execute(ocAccount
);
462 OwnCloudClient mClient
= task
.get();
463 String url
= AccountUtils
.constructFullURLForAccount(context
, account
) + Uri
.encode(file
.getRemotePath(), "/");
464 OwnCloudCredentials credentials
= mClient
.getCredentials();
466 return url
.replace("//", "//" + credentials
.getUsername() + ":" + credentials
.getAuthToken() + "@");
468 } catch (AccountUtils
.AccountNotFoundException e
) {
471 } catch (InterruptedException e
) {
473 } catch (ExecutionException e
) {
479 public static class ClientGenerationTask
extends AsyncTask
<Object
, Void
, OwnCloudClient
> {
481 protected OwnCloudClient
doInBackground(Object
... params
) {
482 Object account
= params
[0];
483 if (account
instanceof OwnCloudAccount
){
485 OwnCloudAccount ocAccount
= (OwnCloudAccount
) account
;
486 return OwnCloudClientManagerFactory
.getDefaultSingleton().
487 getClientFor(ocAccount
, MainApp
.getAppContext());
488 } catch (AccountUtils
.AccountNotFoundException e
) {
490 } catch (OperationCanceledException e
) {
492 } catch (AuthenticatorException e
) {
494 } catch (IOException e
) {
504 private class VideoHelper
implements OnCompletionListener
, OnPreparedListener
, OnErrorListener
{
507 * Called when the file is ready to be played.
509 * Just starts the playback.
511 * @param vp {@link MediaPlayer} instance performing the playback.
514 public void onPrepared(MediaPlayer vp
) {
515 Log_OC
.v(TAG
, "onPrepared");
516 mVideoPreview
.seekTo(mSavedPlaybackPosition
);
518 mVideoPreview
.start();
520 mMediaController
.setEnabled(true
);
521 mMediaController
.updatePausePlay();
527 * Called when the file is finished playing.
529 * Finishes the activity.
531 * @param mp {@link MediaPlayer} instance performing the playback.
534 public void onCompletion(MediaPlayer mp
) {
535 Log_OC
.v(TAG
, "completed");
537 mVideoPreview
.seekTo(0);
538 } // else : called from onError()
539 mMediaController
.updatePausePlay();
544 * Called when an error in playback occurs.
546 * @param mp {@link MediaPlayer} instance performing the playback.
547 * @param what Type of error
548 * @param extra Extra code specific to the error
551 public boolean onError(MediaPlayer mp
, int what
, int extra
) {
552 MediaService
.streamWithExternalApp(mUri
, getActivity()).show();
558 public void onPause() {
559 Log_OC
.v(TAG
, "onPause");
564 public void onResume() {
566 Log_OC
.v(TAG
, "onResume");
570 public void onDestroy() {
571 Log_OC
.v(TAG
, "onDestroy");
576 public void onStop() {
577 Log_OC
.v(TAG
, "onStop");
580 if (mMediaServiceConnection
!= null
) {
581 Log_OC
.d(TAG
, "Unbinding from MediaService ...");
582 if (mMediaServiceBinder
!= null
&& mMediaController
!= null
) {
583 mMediaServiceBinder
.unregisterMediaController(mMediaController
);
585 getActivity().unbindService(mMediaServiceConnection
);
586 mMediaServiceConnection
= null
;
587 mMediaServiceBinder
= null
;
594 public boolean onTouch(View v
, MotionEvent event
) {
595 if (event
.getAction() == MotionEvent
.ACTION_DOWN
&& v
== mVideoPreview
) {
596 // added a margin on the left to avoid interfering with gesture to open navigation drawer
597 if (event
.getX() / Resources
.getSystem().getDisplayMetrics().density
> 24.0) {
598 startFullScreenVideo();
606 private void startFullScreenVideo() {
607 Intent i
= new Intent(getActivity(), PreviewVideoActivity
.class);
608 i
.putExtra(FileActivity
.EXTRA_ACCOUNT
, mAccount
);
609 i
.putExtra(FileActivity
.EXTRA_FILE
, getFile());
610 i
.putExtra(PreviewVideoActivity
.EXTRA_AUTOPLAY
, mVideoPreview
.isPlaying());
611 mVideoPreview
.pause();
612 i
.putExtra(PreviewVideoActivity
.EXTRA_START_POSITION
, mVideoPreview
.getCurrentPosition());
613 startActivityForResult(i
, 0);
617 public void onConfigurationChanged(Configuration newConfig
) {
618 Log_OC
.v(TAG
, "onConfigurationChanged " + this);
622 public void onActivityResult(int requestCode
, int resultCode
, Intent data
) {
623 Log_OC
.v(TAG
, "onActivityResult " + this);
624 super.onActivityResult(requestCode
, resultCode
, data
);
625 if (resultCode
== Activity
.RESULT_OK
) {
626 mSavedPlaybackPosition
= data
.getExtras().getInt(
627 PreviewVideoActivity
.EXTRA_START_POSITION
);
628 mAutoplay
= data
.getExtras().getBoolean(PreviewVideoActivity
.EXTRA_AUTOPLAY
);
633 private void playAudio() {
634 OCFile file
= getFile();
635 if (!mMediaServiceBinder
.isPlaying(file
)) {
636 Log_OC
.d(TAG
, "starting playback of " + file
.getStoragePath());
637 mMediaServiceBinder
.start(mAccount
, file
, mAutoplay
, mSavedPlaybackPosition
);
641 if (!mMediaServiceBinder
.isPlaying() && mAutoplay
) {
642 mMediaServiceBinder
.start();
643 mMediaController
.updatePausePlay();
649 private void bindMediaService() {
650 Log_OC
.d(TAG
, "Binding to MediaService...");
651 if (mMediaServiceConnection
== null
) {
652 mMediaServiceConnection
= new MediaServiceConnection();
654 getActivity().bindService( new Intent(getActivity(),
656 mMediaServiceConnection
,
657 Context
.BIND_AUTO_CREATE
);
658 // follow the flow in MediaServiceConnection#onServiceConnected(...)
661 /** Defines callbacks for service binding, passed to bindService() */
662 private class MediaServiceConnection
implements ServiceConnection
{
665 public void onServiceConnected(ComponentName component
, IBinder service
) {
666 if (getActivity() != null
) {
667 if (component
.equals(
668 new ComponentName(getActivity(), MediaService
.class))) {
669 Log_OC
.d(TAG
, "Media service connected");
670 mMediaServiceBinder
= (MediaServiceBinder
) service
;
671 if (mMediaServiceBinder
!= null
) {
672 prepareMediaController();
673 playAudio(); // do not wait for the touch of nobody to play audio
675 Log_OC
.d(TAG
, "Successfully bound to MediaService, MediaController ready");
679 Log_OC
.e(TAG
, "Unexpected response from MediaService while binding");
685 private void prepareMediaController() {
686 mMediaServiceBinder
.registerMediaController(mMediaController
);
687 if (mMediaController
!= null
) {
688 mMediaController
.setMediaPlayer(mMediaServiceBinder
);
689 mMediaController
.setEnabled(true
);
690 mMediaController
.updatePausePlay();
695 public void onServiceDisconnected(ComponentName component
) {
696 if (component
.equals(new ComponentName(getActivity(), MediaService
.class))) {
697 Log_OC
.w(TAG
, "Media service suddenly disconnected");
698 if (mMediaController
!= null
) {
699 mMediaController
.setMediaPlayer(null
);
704 "No media controller to release when disconnected from media service",
705 Toast
.LENGTH_SHORT
).show();
707 mMediaServiceBinder
= null
;
708 mMediaServiceConnection
= null
;
715 * Opens the previewed file with an external application.
717 private void openFile() {
719 mContainerActivity
.getFileOperationsHelper().openFile(getFile());
724 * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment}
727 * @param file File to test if can be previewed.
728 * @return 'True' if the file can be handled by the fragment.
730 public static boolean canBePreviewed(OCFile file
) {
731 return (file
!= null
&& (file
.isAudio() || file
.isVideo()));
735 public void stopPreview(boolean stopAudio
) {
736 OCFile file
= getFile();
737 if (file
.isAudio() && stopAudio
) {
738 mMediaServiceBinder
.pause();
742 if (file
.isVideo()) {
743 mVideoPreview
.stopPlayback();
750 * Finishes the preview
752 private void finish() {
753 getActivity().onBackPressed();
757 public int getPosition() {
759 mSavedPlaybackPosition
= mVideoPreview
.getCurrentPosition();
761 Log_OC
.v(TAG
, "getting position: " + mSavedPlaybackPosition
);
762 return mSavedPlaybackPosition
;
765 public boolean isPlaying() {
767 mAutoplay
= mVideoPreview
.isPlaying();