Fixes to Edge Navigation for missing bitmaps

- Show prominent color from favicon when bitmap capture of a
  navigation entry is missing
- More active purging of bitmaps on navigation

Change-Id: I2c137385bd1b1179dbbb16c0c3e933783d499c34
diff --git a/src/com/android/browser/EdgeSwipeController.java b/src/com/android/browser/EdgeSwipeController.java
index 5d6edd5..b9c2568 100644
--- a/src/com/android/browser/EdgeSwipeController.java
+++ b/src/com/android/browser/EdgeSwipeController.java
@@ -159,7 +159,7 @@
         if (!mbCurrBMSynced) {
             Bitmap currBM = mModel.readSnapshot(mCurrIndex);
             if (currBM != null) {
-                mView.setStationaryViewBitmap(currBM);
+                mView.setStationaryViewBitmap(currBM, mModel.getColor(mCurrIndex));
                 mbCurrBMSynced = true;
             }
         }
@@ -168,7 +168,7 @@
     private void showCurrBMInSlidingView() {
         if (!mbCurrBMSynced) {
             Bitmap currBM = mModel.readSnapshot(mCurrIndex);
-            mView.setSlidingViewBitmap(currBM);
+            mView.setSlidingViewBitmap(currBM, mModel.getColor(mCurrIndex));
             if (currBM != null) {
                 mbCurrBMSynced = true;
             }
@@ -224,12 +224,14 @@
                         switch (mFromEdge) {
                             case ViewDragHelper.EDGE_LEFT:
                                 mView.setSlidingViewBitmap(
-                                        getGrayscale(getSnapshotOrFavicon(pageIndex)));
+                                        getGrayscale(getSnapshotOrFavicon(pageIndex)),
+                                        mModel.getColor(pageIndex));
                                 mGrayBM = true;
                                 break;
                             case ViewDragHelper.EDGE_RIGHT:
                                 mView.setStationaryViewBitmap(
-                                        getGrayscale(getSnapshotOrFavicon(pageIndex)));
+                                        getGrayscale(getSnapshotOrFavicon(pageIndex)),
+                                        mModel.getColor(pageIndex));
                                 mGrayBM = true;
                                 break;
                         }
@@ -238,7 +240,8 @@
                             return;
                         }
                         mView.setStationaryViewBitmap(
-                                getGrayscale(getSnapshotOrFavicon(pageIndex)));
+                                getGrayscale(getSnapshotOrFavicon(pageIndex)),
+                                mModel.getColor(pageIndex));
                         mGrayBM = true;
                     }
                 }
@@ -325,7 +328,8 @@
             mView.hideSlidingViews();
 
             if (mbNavigated) {
-                mView.setStationaryViewBitmap(getSnapshotOrFavicon(mCurrIndex));
+                mView.setStationaryViewBitmap(getSnapshotOrFavicon(mCurrIndex),
+                        mModel.getColor(mCurrIndex));
             } else {
                 swipeSessionCleanup();
             }
@@ -402,7 +406,8 @@
 
                     if (mPrevIndex >= 0) {
                         if (!mView.stationaryViewHasImage()) {
-                            mView.setStationaryViewBitmap(getSnapshotOrFavicon(mPrevIndex));
+                            mView.setStationaryViewBitmap(getSnapshotOrFavicon(mPrevIndex),
+                                    mModel.getColor(mPrevIndex));
                         }
                     }
                     break;
@@ -412,7 +417,8 @@
                         mView.moveShadowView(mView.getMeasuredWidth() + left);
 
                         if (!mView.slidingViewHasImage() && mNextIndex < mMaxIndex) {
-                            mView.setSlidingViewBitmap(getSnapshotOrFavicon(mNextIndex));
+                            mView.setSlidingViewBitmap(getSnapshotOrFavicon(mNextIndex),
+                                    mModel.getColor(mNextIndex));
                         }
 
                         showCurrBMInStationaryView();
@@ -451,13 +457,15 @@
                     mView.showSlidingViews();
                     mView.goDormant();
                     mPrevIndex = mCurrIndex - 1;
-                    mView.setStationaryViewBitmap(getSnapshotOrFavicon(mPrevIndex));
+                    mView.setStationaryViewBitmap(getSnapshotOrFavicon(mPrevIndex),
+                            mModel.getColor(mPrevIndex));
                     showCurrBMInSlidingView();
                     break;
                 case ViewDragHelper.EDGE_RIGHT:
                     mView.showSlidingViews();
                     mNextIndex = mCurrIndex + 1;
-                    mView.setSlidingViewBitmap(getSnapshotOrFavicon(mNextIndex));
+                    mView.setSlidingViewBitmap(getSnapshotOrFavicon(mNextIndex),
+                            mModel.getColor(mNextIndex));
                     showCurrBMInStationaryView();
                     if (mbCurrBMSynced)
                         mView.goDormant();
diff --git a/src/com/android/browser/EdgeSwipeModel.java b/src/com/android/browser/EdgeSwipeModel.java
index 51a5dc9..2d8517b 100644
--- a/src/com/android/browser/EdgeSwipeModel.java
+++ b/src/com/android/browser/EdgeSwipeModel.java
@@ -30,11 +30,15 @@
 package com.android.browser;
 
 import android.graphics.Bitmap;
+import android.graphics.Color;
 import android.util.SparseArray;
 import android.webkit.ValueCallback;
 
+import org.codeaurora.swe.WebHistoryItem;
+
 public class EdgeSwipeModel {
     private SparseArray<Bitmap> mBitmaps;
+    private SparseArray<Integer> mColors;
     private Tab mTab;
     private TitleBar mBar;
 
@@ -46,6 +50,7 @@
         mTab = tab;
         mBar = bar;
         mBitmaps = new SparseArray<>();
+        mColors = new SparseArray<>();
     }
 
     public void updateSnapshot(final int index) {
@@ -80,6 +85,17 @@
     }
 
     public void fetchSnapshot(final int index) {
+        if (mColors.get(index) == null && mTab.getWebView() != null) {
+            WebHistoryItem item = mTab.getWebView().copyBackForwardList().getItemAtIndex(index);
+            if (item != null) {
+                String url = item.getUrl();
+                int color = NavigationBarBase.getSiteIconColor(url);
+                if (color != 0) {
+                    mColors.put(index, color);
+                }
+            }
+        }
+
         if (mBitmaps.get(index) != null) {
             return;
         }
@@ -108,11 +124,29 @@
         return mBitmaps.get(index);
     }
 
+    public int getColor(int index) {
+        if (index < 0) {
+            return Color.DKGRAY;
+        }
+
+        if (index > (mTab.getWebView().copyBackForwardList().getSize() - 1)) {
+            return Color.DKGRAY;
+        }
+
+        Integer color = mColors.get(index);
+        if (color != null) {
+            return color.intValue();
+        }
+
+        return Color.DKGRAY;
+    }
+
     public void deleteSnapshot(int index) {
         mBitmaps.delete(index);
     }
 
     public void cleanup() {
         mBitmaps.clear();
+        mColors.clear();
     }
 }
diff --git a/src/com/android/browser/EdgeSwipeView.java b/src/com/android/browser/EdgeSwipeView.java
index a777c26..bce8aef 100644
--- a/src/com/android/browser/EdgeSwipeView.java
+++ b/src/com/android/browser/EdgeSwipeView.java
@@ -135,10 +135,10 @@
         return (mViewGroup.getHeight() < mViewGroup.getWidth());
     }
 
-    private void setBitmap(ImageView view, Bitmap bitmap) {
+    private void setBitmap(ImageView view, Bitmap bitmap, int color) {
         clampViewIfNeeded(view);
         if (bitmap == null) {
-            bitmap = getColorBitmap(Color.DKGRAY);
+            bitmap = getColorBitmap(color);
         }
 
         int offset = 0;
@@ -146,6 +146,12 @@
             offset = mTitleBar.getNavigationBar().getMeasuredHeight();
         }
 
+        if ((view.getMeasuredHeight() > view.getMeasuredWidth()) !=
+                (bitmap.getHeight() > bitmap.getWidth())) {
+            view.setImageBitmap(bitmap);
+            return;
+        }
+
         int bitmap_height = bitmap.getHeight();
 
         if (view.getMeasuredHeight() != 0) {
@@ -161,18 +167,18 @@
         }
     }
 
-    public void setStationaryViewBitmap(Bitmap bitmap) {
+    public void setStationaryViewBitmap(Bitmap bitmap, int color) {
         mbStationaryViewBMSet = null != bitmap;
-        setBitmap(mStationaryView, bitmap);
+        setBitmap(mStationaryView, bitmap, color);
     }
 
     public void setStationaryViewAlpha(float alpha) {
         mStationaryView.setAlpha(alpha);
     }
 
-    public void setSlidingViewBitmap(Bitmap bitmap) {
+    public void setSlidingViewBitmap(Bitmap bitmap, int color) {
         mbSlidingViewBMSet = null != bitmap;
-        setBitmap(mSlidingView, bitmap);
+        setBitmap(mSlidingView, bitmap, color);
     }
 
     public boolean slidingViewHasImage() {
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index f1444c8..ebeeadf 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -608,13 +608,16 @@
             mTabHistoryUpdateObservable.set(index);
             final int maxIdx = view.copyBackForwardList().getSize();
             final WebView wv = view;
+            final int currIdx = index;
+            final int currentTabIdx = (int) mWebViewController.getTabControl().
+                    getCurrentTab().getId();
             view.getSnapshotIds(new ValueCallback <List<Integer>>() {
                 @Override
                 public void onReceiveValue(List<Integer> ids) {
-                    int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
                     for (Integer id : ids) {
-                        if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
-                                getNavIdxFromCaptureIdx(id) >= maxIdx) {
+                        int tabIdx = getTabIdxFromCaptureIdx(id);
+                        int navIdx = getNavIdxFromCaptureIdx(id);
+                        if (tabIdx == currentTabIdx && (navIdx >= maxIdx || navIdx == currIdx)) {
                             wv.deleteSnapshot(id);
                         }
                     }
@@ -1363,13 +1366,13 @@
             final WebView webView = mMainView;
             setWebView(null);
             if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
+                final int destroyedTabIdx = (int) mId;
                 // Tabs can be reused with new instance of WebView so delete the snapshots
                 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
                     @Override
                     public void onReceiveValue(List<Integer> ids) {
-                        int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
                         for (Integer id : ids) {
-                            if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
+                            if (getTabIdxFromCaptureIdx(id) == destroyedTabIdx) {
                                 webView.deleteSnapshot(id);
                             }
                         }