Replace TitleBar sub views with a ViewStub.

Optimise startup performance by using a ViewStub for TitleBar views
that aren't needed until later.

Bug: 5179151
Change-Id: I87cf23cbd1e3b818a566c95dc6bb9cc1ff366587
diff --git a/res/layout-sw600dp/title_bar_autologin.xml b/res/layout-sw600dp/title_bar_autologin.xml
index 783b4c3..1065756 100644
--- a/res/layout-sw600dp/title_bar_autologin.xml
+++ b/res/layout-sw600dp/title_bar_autologin.xml
@@ -16,6 +16,8 @@
 -->
 <com.android.browser.AutologinBar
     xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
     android:background="#FBF0A0"
     android:gravity="center_vertical"
     android:visibility="gone">
diff --git a/res/layout-sw600dp/title_bar_snapshot.xml b/res/layout-sw600dp/title_bar_snapshot.xml
index e844c36..69e3864 100644
--- a/res/layout-sw600dp/title_bar_snapshot.xml
+++ b/res/layout-sw600dp/title_bar_snapshot.xml
@@ -16,6 +16,8 @@
 -->
 <com.android.browser.SnapshotBar
     xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:background="@drawable/bg_urlbar">
     <ImageButton
diff --git a/res/layout/title_bar.xml b/res/layout/title_bar.xml
index cf500c8..acc49e6 100644
--- a/res/layout/title_bar.xml
+++ b/res/layout/title_bar.xml
@@ -24,14 +24,14 @@
         android:id="@+id/taburlbar"
         android:layout_width="match_parent"
         android:layout_height="@dimen/toolbar_height" />
-    <include
-        layout="@layout/title_bar_snapshot"
-        android:id="@+id/snapshotbar"
+    <ViewStub
+        android:id="@+id/snapshotbar_stub"
+        android:layout="@layout/title_bar_snapshot"
         android:layout_width="match_parent"
         android:layout_height="@dimen/toolbar_height" />
-    <include
-        layout="@layout/title_bar_autologin"
-        android:id="@+id/autologin"
+    <ViewStub
+        android:id="@+id/autologin_stub"
+        android:layout="@layout/title_bar_autologin"
         android:paddingTop="3dip"
         android:layout_below="@id/taburlbar"
         android:layout_width="match_parent"
diff --git a/res/layout/title_bar_autologin.xml b/res/layout/title_bar_autologin.xml
index 839f875..22da630 100644
--- a/res/layout/title_bar_autologin.xml
+++ b/res/layout/title_bar_autologin.xml
@@ -16,6 +16,8 @@
 -->
 <com.android.browser.AutologinBar
     xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
     android:background="#FBF0A0"
     android:gravity="center_vertical"
     android:visibility="gone"
diff --git a/res/layout/title_bar_snapshot.xml b/res/layout/title_bar_snapshot.xml
index 9d9d7c9..4d6128c 100644
--- a/res/layout/title_bar_snapshot.xml
+++ b/res/layout/title_bar_snapshot.xml
@@ -16,6 +16,8 @@
 -->
 <com.android.browser.SnapshotBar
     xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:background="@drawable/bg_urlbar">
     <ImageView
diff --git a/src/com/android/browser/TitleBar.java b/src/com/android/browser/TitleBar.java
index 9848a39..8fa4d43 100644
--- a/src/com/android/browser/TitleBar.java
+++ b/src/com/android/browser/TitleBar.java
@@ -25,6 +25,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
+import android.view.ViewStub;
 import android.view.animation.Animation;
 import android.view.animation.Animation.AnimationListener;
 import android.view.animation.AnimationUtils;
@@ -72,11 +73,27 @@
         LayoutInflater factory = LayoutInflater.from(context);
         factory.inflate(R.layout.title_bar, this);
         mProgress = (PageProgressView) findViewById(R.id.progress);
-        mAutoLogin = (AutologinBar) findViewById(R.id.autologin);
-        mAutoLogin.setTitleBar(this);
         mNavBar = (NavigationBarBase) findViewById(R.id.taburlbar);
         mNavBar.setTitleBar(this);
-        mSnapshotBar = (SnapshotBar) findViewById(R.id.snapshotbar);
+    }
+
+    private void inflateAutoLoginBar() {
+        if (mAutoLogin != null) {
+            return;
+        }
+
+        ViewStub stub = (ViewStub) findViewById(R.id.autologin_stub);
+        mAutoLogin = (AutologinBar) stub.inflate();
+        mAutoLogin.setTitleBar(this);
+    }
+
+    private void inflateSnapshotBar() {
+        if (mSnapshotBar != null) {
+            return;
+        }
+
+        ViewStub stub = (ViewStub) findViewById(R.id.snapshotbar_stub);
+        mSnapshotBar = (SnapshotBar) stub.inflate();
         mSnapshotBar.setTitleBar(this);
     }
 
@@ -236,13 +253,19 @@
 
     public int getEmbeddedHeight() {
         int height = mNavBar.getHeight();
-        if (mAutoLogin.getVisibility() == View.VISIBLE) {
+        if (mAutoLogin != null && mAutoLogin.getVisibility() == View.VISIBLE) {
             height += mAutoLogin.getHeight();
         }
         return height;
     }
 
     public void updateAutoLogin(Tab tab, boolean animate) {
+        if (mAutoLogin == null) {
+            if  (tab.getDeviceAccountLogin() == null) {
+                return;
+            }
+            inflateAutoLoginBar();
+        }
         mAutoLogin.updateAutoLogin(tab, animate);
     }
 
@@ -250,6 +273,9 @@
         if (mUseQuickControls) {
             mBaseUi.showTitleBar();
         }
+        if (mAutoLogin == null) {
+            inflateAutoLoginBar();
+        }
         mAutoLogin.setVisibility(View.VISIBLE);
         if (animate) {
             mAutoLogin.startAnimation(AnimationUtils.loadAnimation(
@@ -291,12 +317,12 @@
 
     public boolean wantsToBeVisible() {
         return inAutoLogin()
-            || (mSnapshotBar.getVisibility() == View.VISIBLE
+            || (mSnapshotBar != null && mSnapshotBar.getVisibility() == View.VISIBLE
                     && mSnapshotBar.isAnimating());
     }
 
     private boolean inAutoLogin() {
-        return mAutoLogin.getVisibility() == View.VISIBLE;
+        return mAutoLogin != null && mAutoLogin.getVisibility() == View.VISIBLE;
     }
 
     public boolean isEditingUrl() {
@@ -348,12 +374,18 @@
     }
 
     public void onTabDataChanged(Tab tab) {
-        mSnapshotBar.onTabDataChanged(tab);
+        if (mSnapshotBar != null) {
+            mSnapshotBar.onTabDataChanged(tab);
+        }
+
         if (tab.isSnapshot()) {
+            inflateSnapshotBar();
             mSnapshotBar.setVisibility(VISIBLE);
             mNavBar.setVisibility(GONE);
         } else {
-            mSnapshotBar.setVisibility(GONE);
+            if (mSnapshotBar != null) {
+                mSnapshotBar.setVisibility(GONE);
+            }
             mNavBar.setVisibility(VISIBLE);
         }
     }