Fixed bad replace tokens and removed unnecessary quote
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / preview / PreviewMediaFragment.java
index a1e5fa3..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;
@@ -90,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,6 +108,7 @@ public class PreviewMediaFragment extends SherlockFragment implements
     private MediaControlView mMediaController = null;
     private MediaServiceConnection mMediaServiceConnection = null;
     private VideoHelper mVideoHelper;
+    private boolean mAutoplay;
     
     private static final String TAG = PreviewMediaFragment.class.getSimpleName();
 
@@ -123,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;
     }
     
     
@@ -138,6 +142,7 @@ public class PreviewMediaFragment extends SherlockFragment implements
         mAccount = null;
         mSavedPlaybackPosition = 0;
         mStorageManager = null;
+        mAutoplay = true;
     }
     
     
@@ -163,8 +168,8 @@ 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);
         
@@ -195,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) {
@@ -225,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());
         }
     }
     
@@ -344,8 +355,11 @@ public class PreviewMediaFragment extends SherlockFragment implements
          */
         @Override
         public void onPrepared(MediaPlayer vp) {
+            Log.e(TAG, "onPrepared");
             mVideoPreview.seekTo(mSavedPlaybackPosition);
-            mVideoPreview.start();
+            if (mAutoplay) { 
+                mVideoPreview.start();
+            }
             mMediaController.setEnabled(true);
             mMediaController.updatePausePlay();
         }
@@ -360,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();
         }
         
         
@@ -373,8 +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 (mVideoPreview.getWindowToken() != null) {
                 String message = MediaService.getMessageForMediaError(getActivity(), what, extra);
                 new AlertDialog.Builder(getActivity())
@@ -396,20 +425,9 @@ public class PreviewMediaFragment extends SherlockFragment implements
 
     
     @Override
-    public void onResume() {
-        super.onResume();
-    }
-
-
-    @Override
-    public void onPause() {
-        super.onPause();
-    }
-
-
-    @Override
     public void onStop() {
         super.onStop();
+        
         if (mMediaServiceConnection != null) {
             Log.d(TAG, "Unbinding from MediaService ...");
             if (mMediaServiceBinder != null && mMediaController != null) {
@@ -422,46 +440,43 @@ public class PreviewMediaFragment extends SherlockFragment implements
     }
     
     @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();
                 mMediaController.updatePausePlay();
             }