Fix portrait's fixed title bar

 Bug: 6468013

Change-Id: Ia6ee9645df9ebb8aa98734efef45b57db94651f7
diff --git a/src/com/android/browser/BaseUi.java b/src/com/android/browser/BaseUi.java
index bb44336..e3f5986 100644
--- a/src/com/android/browser/BaseUi.java
+++ b/src/com/android/browser/BaseUi.java
@@ -91,6 +91,7 @@
     protected FrameLayout mContentView;
     protected FrameLayout mCustomViewContainer;
     protected FrameLayout mFullscreenContainer;
+    private FrameLayout mFixedTitlebarContainer;
 
     private View mCustomView;
     private WebChromeClient.CustomViewCallback mCustomViewCallback;
@@ -126,6 +127,8 @@
                 .getDecorView().findViewById(android.R.id.content);
         LayoutInflater.from(mActivity)
                 .inflate(R.layout.custom_screen, frameLayout);
+        mFixedTitlebarContainer = (FrameLayout) frameLayout.findViewById(
+                R.id.fixed_titlebar_container);
         mContentView = (FrameLayout) frameLayout.findViewById(
                 R.id.main_content);
         mCustomViewContainer = (FrameLayout) frameLayout.findViewById(
@@ -844,4 +847,17 @@
         }
 
     }
+
+    public void addFixedTitleBar(View view) {
+        mFixedTitlebarContainer.addView(view);
+    }
+
+    public void setContentViewMarginTop(int margin) {
+        LinearLayout.LayoutParams params =
+                (LinearLayout.LayoutParams) mContentView.getLayoutParams();
+        if (params.topMargin != margin) {
+            params.topMargin = margin;
+            mContentView.setLayoutParams(params);
+        }
+    }
 }
diff --git a/src/com/android/browser/TitleBar.java b/src/com/android/browser/TitleBar.java
index 479b62e..9b2a947 100644
--- a/src/com/android/browser/TitleBar.java
+++ b/src/com/android/browser/TitleBar.java
@@ -45,7 +45,7 @@
 
     private UiController mUiController;
     private BaseUi mBaseUi;
-    private FrameLayout mParent;
+    private FrameLayout mContentView;
     private PageProgressView mProgress;
 
     private AutologinBar mAutoLogin;
@@ -58,15 +58,16 @@
     private boolean mInLoad;
     private boolean mSkipTitleBarAnimations;
     private Animator mTitleBarAnimator;
+    private boolean mIsFixedTitleBar;
 
     public TitleBar(Context context, UiController controller, BaseUi ui,
-            FrameLayout parent) {
+            FrameLayout contentView) {
         super(context, null);
         mUiController = controller;
         mBaseUi = ui;
-        mParent = parent;
+        mContentView = contentView;
         initLayout(context);
-        mParent.addView(this, makeLayoutParams());
+        setFixedTitleBar(!mContext.getResources().getBoolean(R.bool.hide_title));
     }
 
     private void initLayout(Context context) {
@@ -100,10 +101,34 @@
     @Override
     protected void onConfigurationChanged(Configuration config) {
         super.onConfigurationChanged(config);
-        if (config.orientation == Configuration.ORIENTATION_PORTRAIT) {
-            if (!mContext.getResources().getBoolean(R.bool.hide_title)) {
-                show();
-            }
+        setFixedTitleBar(!mContext.getResources().getBoolean(R.bool.hide_title));
+    }
+
+    @Override
+    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+        if (mIsFixedTitleBar) {
+            int margin = getMeasuredHeight() - calculateEmbeddedHeight();
+            mBaseUi.setContentViewMarginTop(-margin);
+        }
+    }
+
+    private void setFixedTitleBar(boolean isFixed) {
+        // If getParent() returns null, we are initializing
+        ViewGroup parent = (ViewGroup)getParent();
+        if (mIsFixedTitleBar == isFixed && parent != null) return;
+        mIsFixedTitleBar = isFixed;
+        setSkipTitleBarAnimations(true);
+        show();
+        setSkipTitleBarAnimations(false);
+        if (parent != null) {
+            parent.removeView(this);
+        }
+        if (mIsFixedTitleBar) {
+            mBaseUi.addFixedTitleBar(this);
+        } else {
+            mContentView.addView(this, makeLayoutParams());
+            mBaseUi.setContentViewMarginTop(0);
         }
     }
 
@@ -145,23 +170,21 @@
     }
 
     void show() {
-        if (mUseQuickControls) {
+        cancelTitleBarAnimation(false);
+        if (mUseQuickControls || mSkipTitleBarAnimations) {
             this.setVisibility(View.VISIBLE);
             this.setTranslationY(0);
         } else {
-            if (!mSkipTitleBarAnimations) {
-                cancelTitleBarAnimation(false);
-                int visibleHeight = getVisibleTitleHeight();
-                float startPos = (-getEmbeddedHeight() + visibleHeight);
-                if (getTranslationY() != 0) {
-                    startPos = Math.max(startPos, getTranslationY());
-                }
-                mTitleBarAnimator = ObjectAnimator.ofFloat(this,
-                        "translationY",
-                        startPos, 0);
-                setupTitleBarAnimator(mTitleBarAnimator);
-                mTitleBarAnimator.start();
+            int visibleHeight = getVisibleTitleHeight();
+            float startPos = (-getEmbeddedHeight() + visibleHeight);
+            if (getTranslationY() != 0) {
+                startPos = Math.max(startPos, getTranslationY());
             }
+            mTitleBarAnimator = ObjectAnimator.ofFloat(this,
+                    "translationY",
+                    startPos, 0);
+            setupTitleBarAnimator(mTitleBarAnimator);
+            mTitleBarAnimator.start();
         }
         mShowing = true;
     }
@@ -170,7 +193,7 @@
         if (mUseQuickControls) {
             this.setVisibility(View.GONE);
         } else {
-            if (!mContext.getResources().getBoolean(R.bool.hide_title)) return;
+            if (mIsFixedTitleBar) return;
             if (!mSkipTitleBarAnimations) {
                 cancelTitleBarAnimation(false);
                 int visibleHeight = getVisibleTitleHeight();
@@ -263,7 +286,11 @@
     }
 
     public int getEmbeddedHeight() {
-        if (mUseQuickControls) return 0;
+        if (mUseQuickControls || mIsFixedTitleBar) return 0;
+        return calculateEmbeddedHeight();
+    }
+
+    private int calculateEmbeddedHeight() {
         int height = mNavBar.getHeight();
         if (mAutoLogin != null && mAutoLogin.getVisibility() == View.VISIBLE) {
             height += mAutoLogin.getHeight();
@@ -397,7 +424,7 @@
     }
 
     public void onScrollChanged() {
-        if (!mShowing) {
+        if (!mShowing && !mIsFixedTitleBar) {
             setTranslationY(getVisibleTitleHeight() - getEmbeddedHeight());
         }
     }
diff --git a/src/com/android/browser/view/CustomScreenLinearLayout.java b/src/com/android/browser/view/CustomScreenLinearLayout.java
new file mode 100644
index 0000000..f5341e8
--- /dev/null
+++ b/src/com/android/browser/view/CustomScreenLinearLayout.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package com.android.browser.view;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.widget.LinearLayout;
+
+
+public class CustomScreenLinearLayout extends LinearLayout {
+
+    public CustomScreenLinearLayout(Context context) {
+        super(context);
+        setChildrenDrawingOrderEnabled(true);
+    }
+
+    public CustomScreenLinearLayout(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        setChildrenDrawingOrderEnabled(true);
+    }
+
+    public CustomScreenLinearLayout(Context context, AttributeSet attrs,
+            int defStyle) {
+        super(context, attrs, defStyle);
+        setChildrenDrawingOrderEnabled(true);
+    }
+
+    @Override
+    protected int getChildDrawingOrder(int childCount, int i) {
+        return childCount - i - 1;
+    }
+
+}