add tab bar animations

    http://b/issue?id=3281119
    added in/out animations for tabs

Change-Id: Ia8a467b93e5bc14b8b84cfe7b7e25ffbfadc7209
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index 16c57c6..6113e54 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -2192,12 +2192,9 @@
         removeComboView();
         int currentIndex = mTabControl.getCurrentIndex();
         int removeIndex = mTabControl.getTabIndex(tab);
-        removeTab(tab);
-        if (currentIndex >= removeIndex && currentIndex != 0) {
-            currentIndex--;
-        }
         Tab newtab = mTabControl.getTab(currentIndex);
         setActiveTab(newtab);
+        removeTab(tab);
     }
 
     /**************** TODO: Url loading clean up *******************************/
diff --git a/src/com/android/browser/TabBar.java b/src/com/android/browser/TabBar.java
index 1ab02ed..301165a 100644
--- a/src/com/android/browser/TabBar.java
+++ b/src/com/android/browser/TabBar.java
@@ -18,6 +18,10 @@
 
 import com.android.browser.ScrollWebView.ScrollListener;
 
+import android.animation.Animator;
+import android.animation.Animator.AnimatorListener;
+import android.animation.AnimatorSet;
+import android.animation.ObjectAnimator;
 import android.app.Activity;
 import android.content.Context;
 import android.content.res.Resources;
@@ -151,6 +155,7 @@
         mTabMap.clear();
         for (Tab tab : tabs) {
             TabView tv = buildTabView(tab);
+            mTabs.addTab(tv);
         }
         mTabs.setSelectedTab(mTabControl.getCurrentIndex());
     }
@@ -286,7 +291,6 @@
         TabView tabview = new TabView(mActivity, tab);
         mTabMap.put(tab, tabview);
         tabview.setOnClickListener(this);
-        mTabs.addTab(tabview);
         return tabview;
     }
 
@@ -500,6 +504,62 @@
         return d;
     }
 
+    private void animateTabOut(final Tab tab, final TabView tv) {
+        ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 1.0f, 0.0f);
+        ObjectAnimator scaley = ObjectAnimator.ofFloat(tv, "scaleY", 1.0f, 0.0f);
+        AnimatorSet animator = new AnimatorSet();
+        animator.playTogether(scalex, scaley);
+        animator.setDuration(150);
+        animator.addListener(new AnimatorListener() {
+
+            @Override
+            public void onAnimationCancel(Animator animation) {
+            }
+
+            @Override
+            public void onAnimationEnd(Animator animation) {
+                mTabs.removeTab(tv);
+                mTabMap.remove(tab);
+            }
+
+            @Override
+            public void onAnimationRepeat(Animator animation) {
+            }
+
+            @Override
+            public void onAnimationStart(Animator animation) {
+            }
+
+        });
+        animator.start();
+    }
+
+    private void animateTabIn(final Tab tab, final TabView tv) {
+        ObjectAnimator scalex = ObjectAnimator.ofFloat(tv, "scaleX", 0f, 1.0f);
+        scalex.setDuration(150);
+        scalex.addListener(new AnimatorListener() {
+
+            @Override
+            public void onAnimationCancel(Animator animation) {
+            }
+
+            @Override
+            public void onAnimationEnd(Animator animation) {
+            }
+
+            @Override
+            public void onAnimationRepeat(Animator animation) {
+            }
+
+            @Override
+            public void onAnimationStart(Animator animation) {
+                mTabs.addTab(tv);
+            }
+
+        });
+        scalex.start();
+    }
+
     // TabChangeListener implementation
 
     public void onSetActiveTab(Tab tab) {
@@ -526,6 +586,7 @@
 
     public void onNewTab(Tab tab) {
         TabView tv = buildTabView(tab);
+        animateTabIn(tab, tv);
     }
 
     public void onProgress(Tab tab, int progress) {
@@ -538,9 +599,10 @@
     public void onRemoveTab(Tab tab) {
         TabView tv = mTabMap.get(tab);
         if (tv != null) {
-            mTabs.removeTab(tv);
+            animateTabOut(tab, tv);
+        } else {
+            mTabMap.remove(tab);
         }
-        mTabMap.remove(tab);
     }
 
     public void onUrlAndTitle(Tab tab, String url, String title) {