Don't embed titlebar in WebView

    Bug: 5032345

The titlebar gets attached to an overlay and tracks the scrolling
of the WebView at the top of the page.

Change-Id: I60b2163bb7a3642813823995278722455f566f36
diff --git a/src/com/android/browser/BaseUi.java b/src/com/android/browser/BaseUi.java
index ffb2928..f8a9414 100644
--- a/src/com/android/browser/BaseUi.java
+++ b/src/com/android/browser/BaseUi.java
@@ -250,6 +250,7 @@
         WebView web = mActiveTab.getWebView();
         updateUrlBarAutoShowManagerTarget();
         attachTabToContentView(tab);
+        mTitleBar.bringToFront();
         setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
         onTabDataChanged(tab);
         onProgressChanged(tab);
@@ -345,7 +346,6 @@
         // Remove the container from the content and then remove the
         // WebView from the container. This will trigger a focus change
         // needed by WebView.
-        WebViewClassic.fromWebView(mainView).setEmbeddedTitleBar(null);
         FrameLayout wrapper =
                 (FrameLayout) container.findViewById(R.id.webview_wrapper);
         wrapper.removeView(mainView);
@@ -475,13 +475,6 @@
         return mTitleBar;
     }
 
-    protected void setTitleGravity(int gravity) {
-        WebView web = getWebView();
-        if (web != null) {
-            WebViewClassic.fromWebView(web).setTitleBarGravity(gravity);
-        }
-    }
-
     @Override
     public void showVoiceTitleBar(String title, List<String> results) {
         mNavigationBar.setInVoiceMode(true, results);
diff --git a/src/com/android/browser/BrowserWebView.java b/src/com/android/browser/BrowserWebView.java
index 49d1a2e..12d511f 100644
--- a/src/com/android/browser/BrowserWebView.java
+++ b/src/com/android/browser/BrowserWebView.java
@@ -74,6 +74,10 @@
         super(context);
     }
 
+    public void setTitleBar(TitleBar title) {
+        mTitleBar = title;
+    }
+
     // From TitleBarDelegate
     @Override
     public int getTitleHeight() {
@@ -83,7 +87,6 @@
     // From TitleBarDelegate
     @Override
     public void onSetEmbeddedTitleBar(final View title) {
-        mTitleBar = (TitleBar) title;
     }
 
     public boolean hasTitleBar() {
@@ -110,6 +113,9 @@
     @Override
     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
         super.onScrollChanged(l, t, oldl, oldt);
+        if (mTitleBar != null) {
+            mTitleBar.onScrollChanged();
+        }
         if (mOnScrollChangedListener != null) {
             mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
         }
diff --git a/src/com/android/browser/PhoneUi.java b/src/com/android/browser/PhoneUi.java
index 5afb9c4..d02a3d2 100644
--- a/src/com/android/browser/PhoneUi.java
+++ b/src/com/android/browser/PhoneUi.java
@@ -124,7 +124,6 @@
                 if (!mOptionsMenuOpen || mExtendedMenuOpen) {
                     if (mUseQuickControls && !mTitleBar.isEditingUrl()) {
                         mTitleBar.setShowProgressOnly(true);
-                        setTitleGravity(Gravity.TOP);
                     }
                     showTitleBar();
                 }
@@ -167,11 +166,9 @@
         // Request focus on the top window.
         if (mUseQuickControls) {
             mPieControl.forceToTop(mContentView);
+            view.setTitleBar(null);
         } else {
-            // check if title bar is already attached by animation
-            if (mTitleBar.getParent() == null) {
-                WebViewClassic.fromWebView(view).setEmbeddedTitleBar(mTitleBar);
-            }
+            view.setTitleBar(mTitleBar);
         }
         if (tab.isInVoiceSearchMode()) {
             showVoiceTitleBar(tab.getVoiceDisplayTitle(), tab.getVoiceSearchResults());
@@ -268,41 +265,16 @@
     }
 
     @Override
-    protected void setTitleGravity(int gravity) {
-        if (mUseQuickControls) {
-            FrameLayout.LayoutParams lp =
-                    (FrameLayout.LayoutParams) mTitleBar.getLayoutParams();
-            lp.gravity = gravity;
-            mTitleBar.setLayoutParams(lp);
-        } else {
-            super.setTitleGravity(gravity);
-        }
-    }
-
-    @Override
     public void setUseQuickControls(boolean useQuickControls) {
         mUseQuickControls = useQuickControls;
         mTitleBar.setUseQuickControls(mUseQuickControls);
         if (useQuickControls) {
             mPieControl = new PieControlPhone(mActivity, mUiController, this);
             mPieControl.attachToContainer(mContentView);
-            WebView web = getWebView();
-            if (web != null) {
-                WebViewClassic.fromWebView(web).setEmbeddedTitleBar(null);
-            }
         } else {
             if (mPieControl != null) {
                 mPieControl.removeFromContainer(mContentView);
             }
-            WebView web = getWebView();
-            if (web != null) {
-                // make sure we can re-parent titlebar
-                if ((mTitleBar != null) && (mTitleBar.getParent() != null)) {
-                    ((ViewGroup) mTitleBar.getParent()).removeView(mTitleBar);
-                }
-                WebViewClassic.fromWebView(web).setEmbeddedTitleBar(mTitleBar);
-            }
-            setTitleGravity(Gravity.NO_GRAVITY);
         }
         updateUrlBarAutoShowManagerTarget();
     }
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index acccb31..cfbd88a 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -1566,8 +1566,6 @@
     void destroy() {
         if (mMainView != null) {
             dismissSubWindow();
-            // Make sure the embedded title bar isn't still attached
-            getWebViewClassic().setEmbeddedTitleBar(null);
             // save the WebView to call destroy() after detach it from the tab
             WebView webView = mMainView;
             setWebView(null);
diff --git a/src/com/android/browser/TitleBar.java b/src/com/android/browser/TitleBar.java
index 8fa4d43..bb0eec5 100644
--- a/src/com/android/browser/TitleBar.java
+++ b/src/com/android/browser/TitleBar.java
@@ -21,7 +21,6 @@
 import android.animation.ObjectAnimator;
 import android.content.Context;
 import android.content.res.Resources;
-import android.view.Gravity;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -31,7 +30,6 @@
 import android.view.animation.AnimationUtils;
 import android.view.animation.DecelerateInterpolator;
 import android.webkit.WebView;
-import android.widget.AbsoluteLayout;
 import android.widget.FrameLayout;
 import android.widget.RelativeLayout;
 
@@ -67,6 +65,7 @@
         mBaseUi = ui;
         mParent = parent;
         initLayout(context);
+        mParent.addView(this, makeLayoutParams());
     }
 
     private void initLayout(Context context) {
@@ -107,7 +106,11 @@
 
     public void setUseQuickControls(boolean use) {
         mUseQuickControls = use;
-        setLayoutParams(makeLayoutParams());
+        if (use) {
+            this.setVisibility(View.GONE);
+        } else {
+            this.setVisibility(View.VISIBLE);
+        }
     }
 
     void setShowProgressOnly(boolean progress) {
@@ -132,7 +135,8 @@
 
     void show() {
         if (mUseQuickControls) {
-            mParent.addView(this);
+            this.setVisibility(View.VISIBLE);
+            this.setTranslationY(0);
         } else {
             if (!mSkipTitleBarAnimations) {
                 cancelTitleBarAnimation(false);
@@ -147,14 +151,13 @@
                 setupTitleBarAnimator(mTitleBarAnimator);
                 mTitleBarAnimator.start();
             }
-            mBaseUi.setTitleGravity(Gravity.TOP);
         }
         mShowing = true;
     }
 
     void hide() {
         if (mUseQuickControls) {
-            mParent.removeView(this);
+            this.setVisibility(View.GONE);
         } else {
             if (!mSkipTitleBarAnimations) {
                 cancelTitleBarAnimation(false);
@@ -166,7 +169,7 @@
                 setupTitleBarAnimator(mTitleBarAnimator);
                 mTitleBarAnimator.start();
             } else {
-                mBaseUi.setTitleGravity(Gravity.NO_GRAVITY);
+                onScrollChanged();
             }
         }
         mShowing = false;
@@ -188,10 +191,8 @@
 
     private AnimatorListener mHideTileBarAnimatorListener = new AnimatorListener() {
 
-        boolean mWasCanceled;
         @Override
         public void onAnimationStart(Animator animation) {
-            mWasCanceled = false;
         }
 
         @Override
@@ -200,15 +201,12 @@
 
         @Override
         public void onAnimationEnd(Animator animation) {
-            if (!mWasCanceled) {
-                setTranslationY(0);
-            }
-            mBaseUi.setTitleGravity(Gravity.NO_GRAVITY);
+            // update position
+            onScrollChanged();
         }
 
         @Override
         public void onAnimationCancel(Animator animation) {
-            mWasCanceled = true;
         }
     };
 
@@ -252,6 +250,7 @@
     }
 
     public int getEmbeddedHeight() {
+        if (mUseQuickControls) return 0;
         int height = mNavBar.getHeight();
         if (mAutoLogin != null && mAutoLogin.getVisibility() == View.VISIBLE) {
             height += mAutoLogin.getHeight();
@@ -355,14 +354,8 @@
     }
 
     private ViewGroup.LayoutParams makeLayoutParams() {
-        if (mUseQuickControls) {
-            return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
-                    LayoutParams.WRAP_CONTENT);
-        } else {
-            return new AbsoluteLayout.LayoutParams(
-                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
-                    0, 0);
-        }
+        return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
+                LayoutParams.WRAP_CONTENT);
     }
 
     @Override
@@ -390,4 +383,10 @@
         }
     }
 
+    public void onScrollChanged() {
+        if (!mShowing) {
+            setTranslationY(getVisibleTitleHeight() - getEmbeddedHeight());
+        }
+    }
+
 }
diff --git a/src/com/android/browser/XLargeUi.java b/src/com/android/browser/XLargeUi.java
index 46149aa..6e37c15 100644
--- a/src/com/android/browser/XLargeUi.java
+++ b/src/com/android/browser/XLargeUi.java
@@ -28,13 +28,9 @@
 import android.os.Handler;
 import android.util.Log;
 import android.view.ActionMode;
-import android.view.Gravity;
 import android.view.KeyEvent;
 import android.view.Menu;
 import android.view.MenuItem;
-import android.view.View;
-import android.view.ViewGroup;
-import android.webkit.WebChromeClient.CustomViewCallback;
 import android.webkit.WebView;
 import android.webkit.WebViewClassic;
 
@@ -92,25 +88,11 @@
             checkTabCount();
             mPieControl = new PieControlXLarge(mActivity, mUiController, this);
             mPieControl.attachToContainer(mContentView);
-            WebView web = getWebView();
-            if (web != null) {
-                WebViewClassic.fromWebView(web).setEmbeddedTitleBar(null);
-
-            }
         } else {
             mActivity.getActionBar().show();
             if (mPieControl != null) {
                 mPieControl.removeFromContainer(mContentView);
             }
-            WebView web = getWebView();
-            if (web != null) {
-                if (mTitleBar.getParent() != null) {
-                    ViewGroup p = (ViewGroup) mTitleBar.getParent();
-                    p.removeView(mTitleBar);
-                }
-                WebViewClassic.fromWebView(web).setEmbeddedTitleBar(mTitleBar);
-            }
-            setTitleGravity(Gravity.NO_GRAVITY);
         }
         mTabBar.setUseQuickControls(mUseQuickControls);
         // We need to update the tabs with this change
@@ -200,11 +182,9 @@
         // Request focus on the top window.
         if (mUseQuickControls) {
             mPieControl.forceToTop(mContentView);
+            view.setTitleBar(null);
         } else {
-            // check if title bar is already attached by animation
-            if (mTitleBar.getParent() == null) {
-                WebViewClassic.fromWebView(view).setEmbeddedTitleBar(mTitleBar);
-            }
+            view.setTitleBar(mTitleBar);
         }
         mTabBar.onSetActiveTab(tab);
         if (tab.isInVoiceSearchMode()) {
@@ -269,13 +249,6 @@
         }
     }
 
-    @Override
-    protected void setTitleGravity(int gravity) {
-        if (!mUseQuickControls) {
-            super.setTitleGravity(gravity);
-        }
-    }
-
     // action mode callbacks
 
     @Override