X-Git-Url: http://git.linex4red.de/pub/Android/ownCloud.git/blobdiff_plain/d68b32465328dafb0cd30d8104ef8a3a05ef7bde..2a95f59adc2bdd8f8a90cab8fd8d28f90bf9fc68:/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java diff --git a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java index f9fd122e..85575d16 100644 --- a/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java +++ b/src/com/owncloud/android/ui/preview/PreviewMediaFragment.java @@ -2,9 +2,8 @@ * Copyright (C) 2012-2013 ownCloud Inc. * * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 2 of the License, or - * (at your option) any later version. + * it under the terms of the GNU General Public License version 2, + * as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -30,11 +29,13 @@ import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.ServiceConnection; +import android.content.res.Configuration; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; import android.media.MediaPlayer.OnPreparedListener; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; @@ -47,7 +48,6 @@ import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.webkit.MimeTypeMap; import android.widget.ImageView; -import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView; @@ -67,11 +67,11 @@ import com.owncloud.android.operations.RemoteOperationResult; import com.owncloud.android.operations.RemoveFileOperation; import com.owncloud.android.ui.activity.FileDetailActivity; import com.owncloud.android.ui.activity.FileDisplayActivity; -import com.owncloud.android.ui.activity.TransferServiceGetter; import com.owncloud.android.ui.fragment.ConfirmationDialogFragment; import com.owncloud.android.ui.fragment.FileDetailFragment; import com.owncloud.android.ui.fragment.FileFragment; +import com.owncloud.android.Log_OC; import com.owncloud.android.R; import eu.alefzero.webdav.WebdavClient; import eu.alefzero.webdav.WebdavUtils; @@ -92,6 +92,7 @@ public class PreviewMediaFragment extends SherlockFragment implements public static final String EXTRA_FILE = "FILE"; public static final String EXTRA_ACCOUNT = "ACCOUNT"; private static final String EXTRA_PLAY_POSITION = "PLAY_POSITION"; + private static final String EXTRA_PLAYING = "PLAYING"; private View mView; private OCFile mFile; @@ -105,10 +106,11 @@ public class PreviewMediaFragment extends SherlockFragment implements private RemoteOperation mLastRemoteOperation; private MediaServiceBinder mMediaServiceBinder = null; - //private MediaController mMediaController = null; private MediaControlView mMediaController = null; private MediaServiceConnection mMediaServiceConnection = null; private VideoHelper mVideoHelper; + private boolean mAutoplay; + public boolean mPrepared; private static final String TAG = PreviewMediaFragment.class.getSimpleName(); @@ -121,11 +123,12 @@ public class PreviewMediaFragment extends SherlockFragment implements * @param fileToDetail An {@link OCFile} to preview in the fragment * @param ocAccount An ownCloud account; needed to start downloads */ - public PreviewMediaFragment(OCFile fileToDetail, Account ocAccount) { + public PreviewMediaFragment(OCFile fileToDetail, Account ocAccount, int startPlaybackPosition, boolean autoplay) { mFile = fileToDetail; mAccount = ocAccount; - mSavedPlaybackPosition = 0; + mSavedPlaybackPosition = startPlaybackPosition; mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment + mAutoplay = autoplay; } @@ -141,6 +144,7 @@ public class PreviewMediaFragment extends SherlockFragment implements mAccount = null; mSavedPlaybackPosition = 0; mStorageManager = null; + mAutoplay = true; } @@ -162,16 +166,17 @@ public class PreviewMediaFragment extends SherlockFragment implements public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); + Log_OC.e(TAG, "onCreateView"); + mView = inflater.inflate(R.layout.file_preview, container, false); mImagePreview = (ImageView)mView.findViewById(R.id.image_preview); - mImagePreview.setOnTouchListener(this); mVideoPreview = (VideoView)mView.findViewById(R.id.video_preview); + mVideoPreview.setOnTouchListener(this); mMediaController = (MediaControlView)mView.findViewById(R.id.media_controller); - //updateFileDetails(false); return mView; } @@ -182,6 +187,8 @@ public class PreviewMediaFragment extends SherlockFragment implements @Override public void onAttach(Activity activity) { super.onAttach(activity); + Log_OC.e(TAG, "onAttach"); + if (!(activity instanceof FileFragment.ContainerActivity)) throw new ClassCastException(activity.toString() + " must implement " + FileFragment.ContainerActivity.class.getSimpleName()); } @@ -193,12 +200,14 @@ public class PreviewMediaFragment extends SherlockFragment implements @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); + Log_OC.e(TAG, "onActivityCreated"); mStorageManager = new FileDataStorageManager(mAccount, getActivity().getApplicationContext().getContentResolver()); if (savedInstanceState != null) { mFile = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_FILE); mAccount = savedInstanceState.getParcelable(PreviewMediaFragment.EXTRA_ACCOUNT); mSavedPlaybackPosition = savedInstanceState.getInt(PreviewMediaFragment.EXTRA_PLAY_POSITION); + mAutoplay = savedInstanceState.getBoolean(PreviewMediaFragment.EXTRA_PLAYING); } if (mFile == null) { @@ -229,12 +238,19 @@ public class PreviewMediaFragment extends SherlockFragment implements @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); + Log_OC.e(TAG, "onSaveInstanceState"); outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, mFile); outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount); - if (mVideoPreview.isPlaying()) { - outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mVideoPreview.getCurrentPosition()); + if (mFile.isVideo()) { + mSavedPlaybackPosition = mVideoPreview.getCurrentPosition(); + mAutoplay = mVideoPreview.isPlaying(); + outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mSavedPlaybackPosition); + outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mAutoplay); + } else { + outState.putInt(PreviewMediaFragment.EXTRA_PLAY_POSITION , mMediaServiceBinder.getCurrentPosition()); + outState.putBoolean(PreviewMediaFragment.EXTRA_PLAYING , mMediaServiceBinder.isPlaying()); } } @@ -242,6 +258,7 @@ public class PreviewMediaFragment extends SherlockFragment implements @Override public void onStart() { super.onStart(); + Log_OC.e(TAG, "onStart"); if (mFile != null) { if (mFile.isAudio()) { @@ -276,6 +293,7 @@ public class PreviewMediaFragment extends SherlockFragment implements toHide.add(R.id.action_cancel_download); toHide.add(R.id.action_cancel_upload); toHide.add(R.id.action_download_file); + toHide.add(R.id.action_sync_file); toHide.add(R.id.action_rename_file); // by now for (int i : toHide) { @@ -329,18 +347,11 @@ public class PreviewMediaFragment extends SherlockFragment implements } private void playVideo() { + // create and prepare control panel for the user + mMediaController.setMediaPlayer(mVideoPreview); + // load the video file in the video player ; when done, VideoHelper#onPrepared() will be called mVideoPreview.setVideoPath(mFile.getStoragePath()); - - // create and prepare control panel for the user - //mMediaController = new MediaController(getActivity()); - if (mMediaController != null) { - mMediaController.setMediaPlayer(mVideoPreview); - //mMediaController.setAnchorView(mVideoPreview); - //mVideoPreview.setMediaController(mMediaController); - } else { - Toast.makeText(getActivity(), "No media controller to play video", Toast.LENGTH_SHORT).show(); - } } @@ -355,9 +366,14 @@ public class PreviewMediaFragment extends SherlockFragment implements */ @Override public void onPrepared(MediaPlayer vp) { + Log.e(TAG, "onPrepared"); mVideoPreview.seekTo(mSavedPlaybackPosition); - mVideoPreview.start(); - //mMediaController.show(MediaService.MEDIA_CONTROL_SHORT_LIFE); + if (mAutoplay) { + mVideoPreview.start(); + } + mMediaController.setEnabled(true); + mMediaController.updatePausePlay(); + mPrepared = true; } @@ -370,7 +386,24 @@ public class PreviewMediaFragment extends SherlockFragment implements */ @Override public void onCompletion(MediaPlayer mp) { - // nothing, right now + Log.e(TAG, "completed"); + if (mp != null) { + mVideoPreview.seekTo(0); + // next lines are necessary to work around undesired video loops + if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD) { + mVideoPreview.pause(); + + } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD_MR1) { + // mVideePreview.pause() is not enough + + mMediaController.setEnabled(false); + mVideoPreview.stopPlayback(); + mAutoplay = false; + mSavedPlaybackPosition = 0; + mVideoPreview.setVideoPath(mFile.getStoragePath()); + } + } // else : called from onError() + mMediaController.updatePausePlay(); } @@ -383,14 +416,6 @@ public class PreviewMediaFragment extends SherlockFragment implements */ @Override public boolean onError(MediaPlayer mp, int what, int extra) { - Log.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra); - - /* - if (mMediaController != null) { - mMediaController.hide(); - } - */ - if (mVideoPreview.getWindowToken() != null) { String message = MediaService.getMessageForMediaError(getActivity(), what, extra); new AlertDialog.Builder(getActivity()) @@ -412,88 +437,85 @@ public class PreviewMediaFragment extends SherlockFragment implements @Override + public void onPause() { + super.onPause(); + Log_OC.e(TAG, "onPause"); + } + + @Override public void onResume() { super.onResume(); + Log_OC.e(TAG, "onResume"); } - - + @Override - public void onPause() { - super.onPause(); + public void onDestroy() { + super.onDestroy(); + Log_OC.e(TAG, "onDestroy"); } - - + @Override public void onStop() { + Log_OC.e(TAG, "onStop"); super.onStop(); + + mPrepared = false; if (mMediaServiceConnection != null) { Log.d(TAG, "Unbinding from MediaService ..."); if (mMediaServiceBinder != null && mMediaController != null) { - //mMediaServiceBinder.unregisterMediaController(mMediaController); + mMediaServiceBinder.unregisterMediaController(mMediaController); } getActivity().unbindService(mMediaServiceConnection); mMediaServiceConnection = null; mMediaServiceBinder = null; - /* - if (mMediaController != null) { - mMediaController.hide(); - mMediaController = null; - } - */ } } @Override - public void onDestroy() { - super.onDestroy(); - } - - - @Override public boolean onTouch(View v, MotionEvent event) { - if (event.getAction() == MotionEvent.ACTION_DOWN) { - if (v == mImagePreview && - mMediaServiceBinder != null && mFile.isAudio() && mMediaServiceBinder.isPlaying(mFile)) { - toggleMediaController(MediaService.MEDIA_CONTROL_PERMANENT); - return true; - - } else if (v == mVideoPreview) { - toggleMediaController(MediaService.MEDIA_CONTROL_SHORT_LIFE); - return true; - } + if (event.getAction() == MotionEvent.ACTION_DOWN && v == mVideoPreview) { + startFullScreenVideo(); + return true; } return false; } - private void toggleMediaController(int time) { - /* - if (mMediaController.isShowing()) { - mMediaController.hide(); - } else { - mMediaController.show(time); - } - */ + private void startFullScreenVideo() { + Intent i = new Intent(getActivity(), PreviewVideoActivity.class); + i.putExtra(PreviewVideoActivity.EXTRA_ACCOUNT, mAccount); + i.putExtra(PreviewVideoActivity.EXTRA_FILE, mFile); + i.putExtra(PreviewVideoActivity.EXTRA_AUTOPLAY, mVideoPreview.isPlaying()); + mVideoPreview.pause(); + i.putExtra(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPreview.getCurrentPosition()); + startActivityForResult(i, 0); } + @Override + public void onConfigurationChanged (Configuration newConfig) { + Log_OC.e(TAG, "onConfigurationChanged " + this); + } + + @Override + public void onActivityResult (int requestCode, int resultCode, Intent data) { + Log_OC.e(TAG, "onActivityResult " + this); + super.onActivityResult(requestCode, resultCode, data); + if (resultCode == Activity.RESULT_OK) { + mSavedPlaybackPosition = data.getExtras().getInt(PreviewVideoActivity.EXTRA_START_POSITION); + mAutoplay = data.getExtras().getBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY); + } + } + private void playAudio() { if (!mMediaServiceBinder.isPlaying(mFile)) { Log.d(TAG, "starting playback of " + mFile.getStoragePath()); - mMediaServiceBinder.start(mAccount, mFile); + mMediaServiceBinder.start(mAccount, mFile, mAutoplay, mSavedPlaybackPosition); } else { - if (!mMediaServiceBinder.isPlaying()) { + if (!mMediaServiceBinder.isPlaying() && mAutoplay) { mMediaServiceBinder.start(); - } - if (!mMediaController.isShowing() && isVisible()) { - //mMediaController.show(MediaService.MEDIA_CONTROL_PERMANENT); - // TODO - fix strange bug; steps to trigger : - // 1. remove the "isVisible()" control - // 2. start the app and preview an audio file - // 3. exit from the app (home button, for instance) while the audio file is still being played - // 4. go to notification bar and click on the "ownCloud music app" notification - // PUM! + mMediaController.updatePausePlay(); } } } @@ -520,11 +542,6 @@ public class PreviewMediaFragment extends SherlockFragment implements Log.d(TAG, "Media service connected"); mMediaServiceBinder = (MediaServiceBinder) service; if (mMediaServiceBinder != null) { - /* - if (mMediaController == null) { - mMediaController = new MediaController(getSherlockActivity()); - } - */ prepareMediaController(); playAudio(); // do not wait for the touch of nobody to play audio @@ -535,15 +552,13 @@ public class PreviewMediaFragment extends SherlockFragment implements } } } - + private void prepareMediaController() { - //mMediaServiceBinder.registerMediaController(mMediaController); + mMediaServiceBinder.registerMediaController(mMediaController); if (mMediaController != null) { mMediaController.setMediaPlayer(mMediaServiceBinder); - //mMediaController.setAnchorView(getView()); - mMediaController.setEnabled(mMediaServiceBinder.isInPlaybackState()); - } else { - Toast.makeText(getActivity(), "No media controller to prepare when connected to media service", Toast.LENGTH_SHORT).show(); + mMediaController.setEnabled(true); + mMediaController.updatePausePlay(); } } @@ -552,9 +567,7 @@ public class PreviewMediaFragment extends SherlockFragment implements if (component.equals(new ComponentName(getActivity(), MediaService.class))) { Log.e(TAG, "Media service suddenly disconnected"); if (mMediaController != null) { - //mMediaController.hide(); mMediaController.setMediaPlayer(null); - //mMediaController = null; } else { Toast.makeText(getActivity(), "No media controller to release when disconnected from media service", Toast.LENGTH_SHORT).show(); } @@ -751,9 +764,6 @@ public class PreviewMediaFragment extends SherlockFragment implements } private void stopPreview(boolean stopAudio) { - if (mMediaController != null) { - //mMediaController.hide(); - } if (mFile.isAudio() && stopAudio) { mMediaServiceBinder.pause(); @@ -779,5 +789,21 @@ public class PreviewMediaFragment extends SherlockFragment implements container.finish(); } } + + + public int getPosition() { + if (mPrepared) { + mSavedPlaybackPosition = mVideoPreview.getCurrentPosition(); + } + Log_OC.e(TAG, "getting position: " + mSavedPlaybackPosition); + return mSavedPlaybackPosition; + } + + public boolean isPlaying() { + if (mPrepared) { + mAutoplay = mVideoPreview.isPlaying(); + } + return mAutoplay; + } }