Use a margin to make the title bar stay the same size while loading.

Allows title_bar to use wrap_content, and therefore be able to show
multiple lines of text in voice search mode.

Fixes http://b/issue?id=2510376

Change-Id: If196f63577b6606be0129ee224842dc858f5931a
diff --git a/res/layout/title_bar.xml b/res/layout/title_bar.xml
index 22ab6e4..3765024 100644
--- a/res/layout/title_bar.xml
+++ b/res/layout/title_bar.xml
@@ -15,12 +15,9 @@
    limitations under the License.
 -->
 
-<!-- Manually set the height, to ensure that it matches the SearchDialog's
-        height.  It also ensures that when the stop button is showing it does
-        not change its height. -->
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
-    android:layout_height="57dip"
+    android:layout_height="wrap_content"
     android:orientation="vertical"
     android:paddingLeft="8dip"
     android:paddingRight="12dip"
diff --git a/src/com/android/browser/TitleBar.java b/src/com/android/browser/TitleBar.java
index 03d44a6..ee6aa9c 100644
--- a/src/com/android/browser/TitleBar.java
+++ b/src/com/android/browser/TitleBar.java
@@ -36,6 +36,7 @@
 import android.text.Spanned;
 import android.text.TextUtils;
 import android.text.style.ImageSpan;
+import android.util.DisplayMetrics;
 import android.util.TypedValue;
 import android.view.ContextMenu;
 import android.view.LayoutInflater;
@@ -43,6 +44,7 @@
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewConfiguration;
+import android.view.ViewGroup;
 import android.widget.ImageView;
 import android.widget.LinearLayout;
 import android.widget.ProgressBar;
@@ -77,6 +79,7 @@
     private Drawable        mNormalBackground;
     private Drawable        mLoadingBackground;
     private ImageSpan       mArcsSpan;
+    private int             mExtraMargin;
 
     private static int LONG_PRESS = 1;
 
@@ -99,9 +102,11 @@
         Resources resources = context.getResources();
         mCircularProgress = (Drawable) resources.getDrawable(
                 com.android.internal.R.drawable.search_spinner);
+        DisplayMetrics metrics = resources.getDisplayMetrics();
+        mExtraMargin = (int) TypedValue.applyDimension(
+                TypedValue.COMPLEX_UNIT_DIP, 6.5f, metrics);
         mIconDimension = (int) TypedValue.applyDimension(
-                TypedValue.COMPLEX_UNIT_DIP, 20f,
-                resources.getDisplayMetrics());
+                TypedValue.COMPLEX_UNIT_DIP, 20f, metrics);
         mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension);
         mHorizontalProgress = (ProgressBar) findViewById(
                 R.id.progress_horizontal);
@@ -261,22 +266,39 @@
         if (mInVoiceMode == inVoiceMode) return;
         mInVoiceMode = inVoiceMode && mVoiceSearchIntent != null;
         Drawable titleDrawable;
+        ViewGroup.MarginLayoutParams params
+                = (ViewGroup.MarginLayoutParams) mTitleBg.getLayoutParams();
         if (mInVoiceMode) {
             mRtButton.setImageDrawable(mVoiceDrawable);
             titleDrawable = mVoiceModeBackground;
             mTitle.setEllipsize(null);
             mRtButton.setVisibility(View.VISIBLE);
             mStopButton.setVisibility(View.GONE);
+            if (params != null) {
+                params.setMargins(0, 0, 0, 0);
+            }
         } else {
             if (mInLoad) {
                 titleDrawable = mLoadingBackground;
                 mRtButton.setVisibility(View.GONE);
                 mStopButton.setVisibility(View.VISIBLE);
+                ViewGroup.MarginLayoutParams stopParams
+                        = (ViewGroup.MarginLayoutParams)
+                        mStopButton.getLayoutParams();
+                if (stopParams != null) {
+                    stopParams.setMargins(0,0,0, mExtraMargin);
+                }
+                if (params != null) {
+                    params.setMargins(0, 0, 0, mExtraMargin);
+                }
             } else {
                 titleDrawable = mNormalBackground;
                 mRtButton.setVisibility(View.VISIBLE);
                 mStopButton.setVisibility(View.GONE);
                 mRtButton.setImageDrawable(mBookmarkDrawable);
+                if (params != null) {
+                    params.setMargins(0, 0, 0, 0);
+                }
             }
             mTitle.setEllipsize(TextUtils.TruncateAt.END);
         }
@@ -300,6 +322,8 @@
      * Update the progress, from 0 to 100.
      */
     /* package */ void setProgress(int newProgress) {
+        ViewGroup.MarginLayoutParams params
+                = (ViewGroup.MarginLayoutParams) mTitleBg.getLayoutParams();
         if (newProgress >= mHorizontalProgress.getMax()) {
             mTitle.setCompoundDrawables(null, null, null, null);
             ((Animatable) mCircularProgress).stop();
@@ -309,6 +333,11 @@
                 mRtButton.setVisibility(View.VISIBLE);
                 mStopButton.setVisibility(View.GONE);
                 mTitleBg.setBackgroundDrawable(mNormalBackground);
+                // Set the margin for the textfield to 0, which is appropriate
+                // for the normal background
+                if (params != null) {
+                    params.setMargins(0, 0, 0, 0);
+                }
             }
             mInLoad = false;
         } else {
@@ -326,6 +355,18 @@
                     mTitleBg.setBackgroundDrawable(mLoadingBackground);
                     mRtButton.setVisibility(View.GONE);
                     mStopButton.setVisibility(View.VISIBLE);
+                    // Set a margin for the bottom of the textfield and the stop
+                    // button so that the total height matches that of the
+                    // title bar when the normal background is showing.
+                    if (params != null) {
+                        params.setMargins(0,0,0, mExtraMargin);
+                    }
+                    ViewGroup.MarginLayoutParams stopParams
+                            = (ViewGroup.MarginLayoutParams)
+                            mStopButton.getLayoutParams();
+                    if (stopParams != null) {
+                        stopParams.setMargins(0,0,0, mExtraMargin);
+                    }
                 }
                 mInLoad = true;
             }