Fixed bad replace tokens and removed unnecessary quote
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewMediaFragment.java
index 512becd..35afe15 100644 (file)
@@ -35,6 +35,7 @@ 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;
 
@@ -57,6 +57,7 @@ import com.actionbarsherlock.view.MenuInflater;
 import com.actionbarsherlock.view.MenuItem;
 import com.owncloud.android.datamodel.FileDataStorageManager;
 import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.media.MediaControlView;
 import com.owncloud.android.media.MediaService;
 import com.owncloud.android.media.MediaServiceBinder;
 import com.owncloud.android.network.OwnCloudClientUtils;
@@ -66,12 +67,9 @@ 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.ui.fragment.ConfirmationDialogFragment.ConfirmationDialogFragmentListener;
-import com.owncloud.android.ui.fragment.FileFragment.ContainerActivity;
 
 import com.owncloud.android.R;
 import eu.alefzero.webdav.WebdavClient;
@@ -93,6 +91,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;
@@ -106,9 +105,10 @@ 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;
     
     private static final String TAG = PreviewMediaFragment.class.getSimpleName();
 
@@ -126,6 +126,7 @@ public class PreviewMediaFragment extends SherlockFragment implements
         mAccount = ocAccount;
         mSavedPlaybackPosition = 0;
         mStorageManager = null; // we need a context to init this; the container activity is not available yet at this moment 
+        mAutoplay = true;
     }
     
     
@@ -141,6 +142,7 @@ public class PreviewMediaFragment extends SherlockFragment implements
         mAccount = null;
         mSavedPlaybackPosition = 0;
         mStorageManager = null;
+        mAutoplay = true;
     }
     
     
@@ -166,10 +168,11 @@ public class PreviewMediaFragment extends SherlockFragment implements
         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;
     }
     
@@ -197,6 +200,7 @@ public class PreviewMediaFragment extends SherlockFragment implements
             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) {
@@ -227,12 +231,17 @@ public class PreviewMediaFragment extends SherlockFragment implements
     @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
-        
         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());
         }
     }
     
@@ -246,12 +255,20 @@ public class PreviewMediaFragment extends SherlockFragment implements
                bindMediaService();
                
            } else if (mFile.isVideo()) {
+               stopAudio();
                playVideo(); 
            }
         }
     }
     
     
+    private void stopAudio() {
+        Intent i = new Intent(getSherlockActivity(), MediaService.class);
+        i.setAction(MediaService.ACTION_STOP_ALL);
+        getSherlockActivity().startService(i);
+    }
+
+
     /**
      * {@inheritDoc}
      */
@@ -319,14 +336,11 @@ public class PreviewMediaFragment extends SherlockFragment implements
     }
     
     private void playVideo() {
-        // 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());
         mMediaController.setMediaPlayer(mVideoPreview);
-        mMediaController.setAnchorView(mVideoPreview);
-        mVideoPreview.setMediaController(mMediaController);
+        
+        // load the video file in the video player ; when done, VideoHelper#onPrepared() will be called
+        mVideoPreview.setVideoPath(mFile.getStoragePath()); 
     }
     
 
@@ -341,9 +355,13 @@ 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();
         }
         
         
@@ -356,7 +374,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();
         }
         
         
@@ -369,12 +404,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())
@@ -396,42 +425,9 @@ public class PreviewMediaFragment extends SherlockFragment implements
 
     
     @Override
-    public void onResume() {
-        super.onResume();
-        /*
-        mDownloadFinishReceiver = new DownloadFinishReceiver();
-        IntentFilter filter = new IntentFilter(
-                FileDownloader.DOWNLOAD_FINISH_MESSAGE);
-        getActivity().registerReceiver(mDownloadFinishReceiver, filter);
-        
-        mUploadFinishReceiver = new UploadFinishReceiver();
-        filter = new IntentFilter(FileUploader.UPLOAD_FINISH_MESSAGE);
-        getActivity().registerReceiver(mUploadFinishReceiver, filter);
-        */
-
-    }
-
-
-    @Override
-    public void onPause() {
-        super.onPause();
-        /*
-        if (mVideoPreview.getVisibility() == View.VISIBLE) {
-            mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
-        }*/
-        /*
-        getActivity().unregisterReceiver(mDownloadFinishReceiver);
-        mDownloadFinishReceiver = null;
-        
-        getActivity().unregisterReceiver(mUploadFinishReceiver);
-        mUploadFinishReceiver = null;
-        */
-    }
-
-
-    @Override
     public void onStop() {
         super.onStop();
+        
         if (mMediaServiceConnection != null) {
             Log.d(TAG, "Unbinding from MediaService ...");
             if (mMediaServiceBinder != null && mMediaController != null) {
@@ -440,62 +436,49 @@ public class PreviewMediaFragment extends SherlockFragment implements
             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 onActivityResult (int requestCode, int resultCode, Intent data) {
+        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();
             }
         }
     }
@@ -522,9 +505,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,12 +515,14 @@ public class PreviewMediaFragment extends SherlockFragment implements
                 }
             }
         }
-        
+
         private void prepareMediaController() {
             mMediaServiceBinder.registerMediaController(mMediaController);
-            mMediaController.setMediaPlayer(mMediaServiceBinder);
-            mMediaController.setAnchorView(getView());
-            mMediaController.setEnabled(mMediaServiceBinder.isInPlaybackState());
+            if (mMediaController != null) {
+                mMediaController.setMediaPlayer(mMediaServiceBinder);
+                mMediaController.setEnabled(true);
+                mMediaController.updatePausePlay();
+            }
         }
 
         @Override
@@ -548,9 +530,9 @@ 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();
                 }
                 mMediaServiceBinder = null;
                 mMediaServiceConnection = null;
@@ -705,29 +687,6 @@ public class PreviewMediaFragment extends SherlockFragment implements
     
 
     /**
-     * Interface to implement by any Activity that includes some instance of FileDetailFragment
-     * 
-     * @author David A. Velasco
-     */
-    public interface ContainerActivity extends TransferServiceGetter {
-
-        /**
-         * Callback method invoked when the detail fragment wants to notice its container 
-         * activity about a relevant state the file shown by the fragment.
-         * 
-         * Added to notify to FileDisplayActivity about the need of refresh the files list. 
-         * 
-         * Currently called when:
-         *  - a download is started;
-         *  - a rename is completed;
-         *  - a deletion is completed;
-         *  - the 'inSync' flag is changed;
-         */
-        public void onFileStateChanged();
-        
-    }
-
-    /**
      * Helper method to test if an {@link OCFile} can be passed to a {@link PreviewMediaFragment} to be previewed.
      * 
      * @param file      File to test if can be previewed.
@@ -768,9 +727,6 @@ public class PreviewMediaFragment extends SherlockFragment implements
     }
 
     private void stopPreview(boolean stopAudio) {
-        if (mMediaController != null) {
-            mMediaController.hide();
-        }
         if (mFile.isAudio() && stopAudio) {
             mMediaServiceBinder.pause();