Merge "Fix the aspect ratio for full screen playing" into honeycomb-mr1
diff --git a/core/java/android/webkit/HTML5VideoFullScreen.java b/core/java/android/webkit/HTML5VideoFullScreen.java
index 993d694..6be988e 100644
--- a/core/java/android/webkit/HTML5VideoFullScreen.java
+++ b/core/java/android/webkit/HTML5VideoFullScreen.java
@@ -25,7 +25,30 @@
     implements MediaPlayerControl, MediaPlayer.OnPreparedListener,
     View.OnTouchListener {
 
-    private SurfaceView mSurfaceView;
+    // Add this sub-class to handle the resizing when rotating screen.
+    private class VideoSurfaceView extends SurfaceView {
+
+        public VideoSurfaceView(Context context) {
+            super(context);
+        }
+
+        @Override
+        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+            int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
+            int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
+            if (mVideoWidth > 0 && mVideoHeight > 0) {
+                if ( mVideoWidth * height  > width * mVideoHeight ) {
+                    height = width * mVideoHeight / mVideoWidth;
+                } else if ( mVideoWidth * height  < width * mVideoHeight ) {
+                    width = height * mVideoWidth / mVideoHeight;
+                }
+            }
+            setMeasuredDimension(width, height);
+        }
+    }
+
+    // This view will contain the video.
+    private VideoSurfaceView mVideoSurfaceView;
 
     // We need the full screen state to decide which surface to render to and
     // when to create the MediaPlayer accordingly.
@@ -51,6 +74,11 @@
     // The container for the progress view and video view
     private static FrameLayout mLayout;
 
+    // The video size will be ready when prepared. Used to make sure the aspect
+    // ratio is correct.
+    private int mVideoWidth;
+    private int mVideoHeight;
+
     SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback()
     {
         public void surfaceChanged(SurfaceHolder holder, int format,
@@ -82,14 +110,16 @@
         }
     };
 
-    public SurfaceView getSurfaceView() {
-        return mSurfaceView;
+    private SurfaceView getSurfaceView() {
+        return mVideoSurfaceView;
     }
 
     HTML5VideoFullScreen(Context context, int videoLayerId, int position,
             boolean autoStart) {
-        mSurfaceView = new SurfaceView(context);
+        mVideoSurfaceView = new VideoSurfaceView(context);
         mFullScreenMode = FULLSCREEN_OFF;
+        mVideoWidth = 0;
+        mVideoHeight = 0;
         init(videoLayerId, position, autoStart);
     }
 
@@ -101,7 +131,7 @@
     private void attachMediaController() {
         if (mPlayer != null && mMediaController != null) {
             mMediaController.setMediaPlayer(this);
-            mMediaController.setAnchorView(mSurfaceView);
+            mMediaController.setAnchorView(mVideoSurfaceView);
             //Will be enabled when prepared
             mMediaController.setEnabled(false);
         }
@@ -112,8 +142,7 @@
         mPlayer.setDisplay(mSurfaceHolder);
     }
 
-    @Override
-    public void prepareForFullScreen() {
+    private void prepareForFullScreen() {
         // So in full screen, we reset the MediaPlayer
         mPlayer.reset();
         setMediaController(new MediaController(mProxy.getContext()));
@@ -134,7 +163,7 @@
     public void onPrepared(MediaPlayer mp) {
         super.onPrepared(mp);
 
-        mSurfaceView.setOnTouchListener(this);
+        mVideoSurfaceView.setOnTouchListener(this);
         // Get the capabilities of the player for this stream
         Metadata data = mp.getMetadata(MediaPlayer.METADATA_ALL,
                 MediaPlayer.BYPASS_METADATA_FILTER);
@@ -165,6 +194,11 @@
             mLayout.removeView(mProgressView);
             mProgressView = null;
         }
+
+        mVideoWidth = mp.getVideoWidth();
+        mVideoHeight = mp.getVideoHeight();
+        // This will trigger the onMeasure to get the display size right.
+        mVideoSurfaceView.getHolder().setFixedSize(mVideoWidth, mVideoHeight);
     }
 
 
@@ -202,11 +236,11 @@
         mPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
         mProxy = proxy;
 
-        mSurfaceView.getHolder().addCallback(mSHCallback);
-        mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
-        mSurfaceView.setFocusable(true);
-        mSurfaceView.setFocusableInTouchMode(true);
-        mSurfaceView.requestFocus();
+        mVideoSurfaceView.getHolder().addCallback(mSHCallback);
+        mVideoSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
+        mVideoSurfaceView.setFocusable(true);
+        mVideoSurfaceView.setFocusableInTouchMode(true);
+        mVideoSurfaceView.requestFocus();
 
         // Create a FrameLayout that will contain the VideoView and the
         // progress view (if any).
diff --git a/core/java/android/webkit/HTML5VideoView.java b/core/java/android/webkit/HTML5VideoView.java
index ade7106..663497c 100644
--- a/core/java/android/webkit/HTML5VideoView.java
+++ b/core/java/android/webkit/HTML5VideoView.java
@@ -243,16 +243,9 @@
         return false;
     }
 
-    public SurfaceView getSurfaceView() {
-        return null;
-    }
-
     public void decideDisplayMode() {
     }
 
-    public void prepareForFullScreen() {
-    }
-
     public boolean getReadyToUseSurfTex() {
         return false;
     }