add tab switch animations

    Bug: 3446585
    Bug: 3281140
    Cross fade between the current and the new tab

Change-Id: I403e40b02e3f77806dc5ed3ebecd01afc508f68f
diff --git a/src/com/android/browser/BaseUi.java b/src/com/android/browser/BaseUi.java
index bca999d..93b0ec8 100644
--- a/src/com/android/browser/BaseUi.java
+++ b/src/com/android/browser/BaseUi.java
@@ -18,6 +18,8 @@
 
 import com.android.browser.Tab.LockIcon;
 
+import android.animation.Animator;
+import android.animation.Animator.AnimatorListener;
 import android.animation.ObjectAnimator;
 import android.app.Activity;
 import android.content.res.Configuration;
@@ -67,7 +69,7 @@
     Activity mActivity;
     UiController mUiController;
     TabControl mTabControl;
-    private Tab mActiveTab;
+    protected Tab mActiveTab;
     private InputMethodManager mInputManager;
 
     private Drawable mSecLockIcon;
@@ -220,12 +222,18 @@
     }
 
     @Override
-    public void setActiveTab(Tab tab) {
+    public void setActiveTab(final Tab tab) {
+        setActiveTab(tab, true);
+    }
+
+    void setActiveTab(Tab tab, boolean needsAttaching) {
         if ((tab != mActiveTab) && (mActiveTab != null)) {
             removeTabFromContentView(mActiveTab);
         }
         mActiveTab = tab;
-        attachTabToContentView(tab);
+        if (needsAttaching) {
+            attachTabToContentView(tab);
+        }
         setShouldShowErrorConsole(tab, mUiController.shouldShowErrorConsole());
         onTabDataChanged(tab);
         onProgressChanged(tab);
@@ -259,7 +267,7 @@
         attachTabToContentView(tab);
     }
 
-    private void attachTabToContentView(Tab tab) {
+    protected void attachTabToContentView(Tab tab) {
         if ((tab == null) || (tab.getWebView() == null)) {
             return;
         }
diff --git a/src/com/android/browser/ScrollWebView.java b/src/com/android/browser/ScrollWebView.java
index 2bf07e1..d1dc25b 100644
--- a/src/com/android/browser/ScrollWebView.java
+++ b/src/com/android/browser/ScrollWebView.java
@@ -1,22 +1,24 @@
 /*
  * Copyright (C) 2010 The Android Open Source Project
  *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
  *
- *      http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
  */
 
 package com.android.browser;
 
 import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.Paint;
 import android.util.AttributeSet;
 import android.view.MotionEvent;
 import android.view.View;
@@ -25,7 +27,7 @@
 import java.util.Map;
 
 /**
- *  Manage WebView scroll events
+ * Manage WebView scroll events
  */
 public class ScrollWebView extends WebView implements Runnable {
 
@@ -34,6 +36,9 @@
     private boolean mBackgroundRemoved = false;
     private boolean mUserInitiated = false;
     private TitleBarBase mTitleBar;
+    private boolean mDrawCached = false;
+    private Bitmap mBitmap;
+    private Paint mCachePaint = new Paint();
 
     /**
      * @param context
@@ -51,8 +56,8 @@
      * @param attrs
      * @param defStyle
      */
-    public ScrollWebView(Context context, AttributeSet attrs, int defStyle,
-            boolean privateBrowsing) {
+    public ScrollWebView(
+            Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
         super(context, attrs, defStyle, privateBrowsing);
     }
 
@@ -97,6 +102,10 @@
         }
     }
 
+    public boolean hasTitleBar() {
+        return (mTitleBar != null);
+    }
+
     @Override
     public boolean onTouchEvent(MotionEvent evt) {
         if (MotionEvent.ACTION_DOWN == evt.getActionMasked()) {
@@ -128,22 +137,46 @@
         mScrollListener = l;
     }
 
+    @Override
+    public void invalidate() {
+        if (!mDrawCached) {
+            super.invalidate();
+        }
+    }
+
     // callback for scroll events
 
     interface ScrollListener {
         public void onScroll(int visibleTitleHeight, boolean userInitiated);
     }
 
-    @Override
-    protected void onDraw(android.graphics.Canvas c) {
-        super.onDraw(c);
-        if (!mBackgroundRemoved && getRootView().getBackground() != null) {
-            mBackgroundRemoved = true;
-            post(new Runnable() {
-                public void run() {
-                    getRootView().setBackgroundDrawable(null);
-                }
-            });
+    void setDrawCached(boolean cached) {
+        if (cached) {
+            buildDrawingCache();
+            mBitmap = getDrawingCache(false);
+            mDrawCached = (mBitmap != null);
+        } else {
+            mBitmap = null;
+            destroyDrawingCache();
+            mDrawCached = false;
         }
     }
+
+    @Override
+    protected void onDraw(android.graphics.Canvas c) {
+        if (mDrawCached) {
+            c.drawBitmap(mBitmap, getScrollX(), getScrollY(), mCachePaint);
+        } else {
+            super.onDraw(c);
+            if (!mBackgroundRemoved && getRootView().getBackground() != null) {
+                mBackgroundRemoved = true;
+                post(new Runnable() {
+                    public void run() {
+                        getRootView().setBackgroundDrawable(null);
+                    }
+                });
+            }
+        }
+    }
+
 }
diff --git a/src/com/android/browser/XLargeUi.java b/src/com/android/browser/XLargeUi.java
index f914183..8c7756b 100644
--- a/src/com/android/browser/XLargeUi.java
+++ b/src/com/android/browser/XLargeUi.java
@@ -18,6 +18,9 @@
 
 import com.android.browser.ScrollWebView.ScrollListener;
 
+import android.animation.Animator;
+import android.animation.Animator.AnimatorListener;
+import android.animation.ObjectAnimator;
 import android.app.ActionBar;
 import android.app.Activity;
 import android.content.pm.PackageManager;
@@ -204,11 +207,57 @@
     }
 
     @Override
-    public void setActiveTab(Tab tab) {
-        if (mTitleBar.isEditingUrl()) {
-            mTitleBar.stopEditingUrl();
+    public void setActiveTab(final Tab tab) {
+        if ((tab != mActiveTab) && (mActiveTab != null)) {
+            // animate between the two
+            final ScrollWebView fromWV = (ScrollWebView) mActiveTab.getWebView();
+            fromWV.setDrawCached(true);
+            fromWV.setEmbeddedTitleBar(null);
+            final ScrollWebView toWV = (ScrollWebView) tab.getWebView();
+            if (!mUseQuickControls) {
+                if (mTitleBar.getParent() == null) {
+                    toWV.setEmbeddedTitleBar(mTitleBar);
+                }
+            }
+            toWV.setDrawCached(true);
+            attachTabToContentView(tab);
+            super.setActiveTab(tab, false);
+            ObjectAnimator transition = ObjectAnimator.ofFloat(
+                    toWV, "alpha", 0f, 1f);
+            transition.setDuration(mActivity.getResources()
+                    .getInteger(R.integer.tabFadeDuration));
+            transition.addListener(new AnimatorListener() {
+                @Override
+                public void onAnimationCancel(Animator animation) {
+                    fromWV.setDrawCached(false);
+                    toWV.setDrawCached(false);
+                    setActiveTab(tab, false);
+                }
+
+                @Override
+                public void onAnimationEnd(Animator animation) {
+                    fromWV.setDrawCached(false);
+                    toWV.setDrawCached(false);
+                    setActiveTab(tab, false);
+                }
+
+                @Override
+                public void onAnimationRepeat(Animator animation) {
+                }
+
+                @Override
+                public void onAnimationStart(Animator animation) {
+                }
+            });
+            transition.start();
+        } else {
+            super.setActiveTab(tab, true);
+            setActiveTab(tab, true);
         }
-        super.setActiveTab(tab);
+    }
+
+    @Override
+    void setActiveTab(Tab tab, boolean needsAttaching) {
         ScrollWebView view = (ScrollWebView) tab.getWebView();
         // TabControl.setCurrentTab has been called before this,
         // so the tab is guaranteed to have a webview
@@ -222,7 +271,10 @@
             view.setScrollListener(null);
             mTabBar.showTitleBarIndicator(false);
         } else {
-            view.setEmbeddedTitleBar(mTitleBar);
+            // check if title bar is already attached by animation
+            if (mTitleBar.getParent() == null) {
+                view.setEmbeddedTitleBar(mTitleBar);
+            }
             view.setScrollListener(this);
         }
         mTabBar.onSetActiveTab(tab);