Update the orientation of the NavScreen on orientationChange.

Before, it was destroying all the UI objects and recreating them,
no matter if the tab switcher was visible or not. The new behavior
matches the old one visually, but happens in a fraction of the
time.

Also, this updates a bit the visibility of properties in the class.
This class should require more cleanup in the future.

Change-Id: I4e5be461aa954faf8fc6b8d50fa8dd865a72e564
diff --git a/src/com/android/browser/NavScreen.java b/src/com/android/browser/NavScreen.java
index 0962449..d7f389a 100644
--- a/src/com/android/browser/NavScreen.java
+++ b/src/com/android/browser/NavScreen.java
@@ -16,6 +16,7 @@
 
 package com.android.browser;
 
+import android.animation.ObjectAnimator;
 import android.app.Activity;
 import android.content.Context;
 import android.content.res.Configuration;
@@ -42,20 +43,19 @@
         implements OnClickListener, OnMenuItemClickListener, OnThumbnailUpdatedListener {
 
 
-    UiController mUiController;
-    PhoneUi mUi;
-    Activity mActivity;
+    private final UiController mUiController;
+    private final PhoneUi mUi;
+    private final Activity mActivity;
 
-    View mToolbarLayout;
-    ImageButton mMore;
-    ImageButton mNewTab;
-    ImageButton mNewIncognitoTab;
+    private View mToolbarLayout;
+    private ImageButton mMore;
+    private ImageButton mNewTab;
+    private ImageButton mNewIncognitoTab;
 
-    NavTabScroller mScroller;
-    TabAdapter mAdapter;
-    int mOrientation;
-    boolean mNeedsMenu;
-    HashMap<Tab, View> mTabViews;
+    private NavTabScroller mScroller;
+    private TabAdapter mAdapter;
+    private int mOrientation;
+    private HashMap<Tab, View> mTabViews;
 
     public NavScreen(Activity activity, UiController ctl, PhoneUi ui) {
         super(activity);
@@ -66,7 +66,7 @@
         init();
     }
 
-    protected void showMenu() {
+    protected void showPopupMenu() {
         if (mUiController instanceof Controller) {
             PopupMenu popup = new PopupMenu(getContext(), mMore);
             Menu menu = popup.getMenu();
@@ -76,6 +76,15 @@
         }
     }
 
+    public NavTabScroller getScroller() {
+        return mScroller;
+    }
+
+    public ObjectAnimator createToolbarInAnimator() {
+        return ObjectAnimator.ofFloat(mToolbarLayout, "translationY",
+                -getResources().getDimensionPixelSize(R.dimen.toolbar_height), 0f);
+    }
+
     @Override
     public boolean onMenuItemClick(MenuItem item) {
         return mUiController.onOptionsItemSelected(item);
@@ -84,12 +93,13 @@
     @Override
     protected void onConfigurationChanged(Configuration newconfig) {
         if (newconfig.orientation != mOrientation) {
-            int sv = mScroller.getScrollValue();
-            removeAllViews();
             mOrientation = newconfig.orientation;
-            init();
-            mScroller.setScrollValue(sv);
-            mAdapter.notifyDataSetChanged();
+
+            // the only thing we need to change is the orientation. see nav_screen.xml
+            //final int prevScroll = mScroller.getScrollValue();
+            mScroller.setOrientation(mOrientation == Configuration.ORIENTATION_LANDSCAPE
+                    ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
+            mScroller.setScrollOnNextLayout();
         }
     }
 
@@ -124,8 +134,8 @@
                 onCloseTab(tab);
             }
         });
-        mNeedsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
-        if (!mNeedsMenu) {
+        boolean needsMenu = !ViewConfiguration.get(getContext()).hasPermanentMenuKey();
+        if (!needsMenu) {
             mMore.setVisibility(View.GONE);
         }
     }
@@ -137,7 +147,7 @@
         } else if (mNewIncognitoTab == v) {
             openNewIncognitoTab();
         } else if (mMore == v) {
-            showMenu();
+            showPopupMenu();
         }
     }
 
diff --git a/src/com/android/browser/NavTabScroller.java b/src/com/android/browser/NavTabScroller.java
index 39c73ac..220f44c 100644
--- a/src/com/android/browser/NavTabScroller.java
+++ b/src/com/android/browser/NavTabScroller.java
@@ -30,6 +30,7 @@
 import android.view.ViewGroup;
 import android.view.animation.DecelerateInterpolator;
 import android.widget.BaseAdapter;
+import android.widget.FrameLayout;
 import android.widget.LinearLayout;
 
 import com.android.browser.view.ScrollerView;
@@ -126,6 +127,17 @@
                     new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
         }
         super.setOrientation(orientation);
+
+        // update the layout parameters of existing views (to not destroy/recreate all)
+        final int childCount = mContentView.getChildCount();
+        for (int i = 0; i < childCount; i++) {
+            final View view = mContentView.getChildAt(i);
+            final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
+            lp.gravity = (mHorizontal ? Gravity.CENTER_VERTICAL : Gravity.CENTER_HORIZONTAL);
+            view.setLayoutParams(lp);
+            if (mGapPosition > INVALID_POSITION)
+                adjustViewGap(view, i);
+        }
     }
 
     @Override
@@ -190,6 +202,10 @@
         handleDataChanged(INVALID_POSITION);
     }
 
+    void setScrollOnNextLayout() {
+        mNeedsScroll = true;
+    }
+
     void handleDataChanged(int newscroll) {
         int scroll = getScrollValue();
         if (mGapAnimator != null) {
@@ -381,8 +397,10 @@
                 || (mGap > 0 && pos < mGapPosition)) {
             if (mHorizontal) {
                 view.setTranslationX(mGap);
+                view.setTranslationY(0);
             } else {
                 view.setTranslationY(mGap);
+                view.setTranslationX(0);
             }
         }
     }
diff --git a/src/com/android/browser/PhoneUi.java b/src/com/android/browser/PhoneUi.java
index 5dc87f3..a58cdac 100644
--- a/src/com/android/browser/PhoneUi.java
+++ b/src/com/android/browser/PhoneUi.java
@@ -290,8 +290,7 @@
         ObjectAnimator tr = ObjectAnimator.ofInt(mAnimScreen.mContent, "right", fromRight, toRight);
         ObjectAnimator tb = ObjectAnimator.ofInt(mAnimScreen.mContent, "bottom", fromBottom, toBottom);
         ObjectAnimator sx = ObjectAnimator.ofFloat(mAnimScreen, "scaleFactor", 1f, toScaleFactor);
-        ObjectAnimator navTabsIn = ObjectAnimator.ofFloat(mNavScreen.mToolbarLayout, "translationY",
-                -mNavScreen.getResources().getDimensionPixelSize(R.dimen.toolbar_height), 0f);
+        ObjectAnimator navTabsIn = mNavScreen.createToolbarInAnimator();
         mAnimScreen.mContent.layout(fromLeft, fromTop, fromRight, fromBottom);
         mAnimScreen.setScaleFactor(1f);
 
@@ -373,12 +372,12 @@
         }
         mAnimScreen.mMain.layout(0, 0, mContentView.getWidth(),
                 mContentView.getHeight());
-        mNavScreen.mScroller.finishScroller();
+        mNavScreen.getScroller().finishScroller();
         int toLeft = 0;
         int toTop = mTitleBar.calculateEmbeddedHeight();
         int toRight = mContentView.getWidth();
-        int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.mScroller.getScrollX();
-        int fromTop = tabview.getTop() + target.getTop() - mNavScreen.mScroller.getScrollY();
+        int fromLeft = tabview.getLeft() + target.getLeft() - mNavScreen.getScroller().getScrollX();
+        int fromTop = tabview.getTop() + target.getTop() - mNavScreen.getScroller().getScrollY();
         int fromRight = fromLeft + width;
         int fromBottom = fromTop + height;
         float scaleFactor = mContentView.getWidth() / (float) width;