Reduce bitmap captures on EdgeNavigation

CR-Fixed: SWE-6185

Change-Id: I2609eed98b11733cae9be077a29e78e4ac9dc268
diff --git a/src/com/android/browser/EdgeSwipeModel.java b/src/com/android/browser/EdgeSwipeModel.java
index 2d8517b..419e265 100644
--- a/src/com/android/browser/EdgeSwipeModel.java
+++ b/src/com/android/browser/EdgeSwipeModel.java
@@ -37,8 +37,14 @@
 import org.codeaurora.swe.WebHistoryItem;
 
 public class EdgeSwipeModel {
+    private static final int MS_TIME_BETWEEN_CAPTURES = 1000;
     private SparseArray<Bitmap> mBitmaps;
     private SparseArray<Integer> mColors;
+
+    private long mLastCaptureTime;
+    private int mLastCaptureIndex;
+    private Bitmap mLastBitmap;
+
     private Tab mTab;
     private TitleBar mBar;
 
@@ -49,6 +55,8 @@
     public EdgeSwipeModel(Tab tab, TitleBar bar) {
         mTab = tab;
         mBar = bar;
+        mLastCaptureIndex = -1;
+        mLastCaptureTime = 0;
         mBitmaps = new SparseArray<>();
         mColors = new SparseArray<>();
     }
@@ -58,7 +66,7 @@
             return;
         }
 
-        int captureIndex = mTab.getCaptureIndex(index);
+        final int captureIndex = mTab.getCaptureIndex(index);
 
         boolean bitmapExists = mTab.getWebView().hasSnapshot(captureIndex);
 
@@ -68,10 +76,14 @@
         }
 
         int progress = mBar.getProgressView().getProgressPercent();
+        long currentTime = System.currentTimeMillis();
 
-        if (bitmapExists && progress < mMinProgress) {
-            fetchSnapshot(index);
-            return;
+        if (bitmapExists) {
+            if (progress < mMinProgress || (captureIndex == mLastCaptureIndex &&
+                    (currentTime < (mLastCaptureTime + MS_TIME_BETWEEN_CAPTURES)))) {
+                fetchSnapshot(index);
+                return;
+            }
         }
 
         mTab.getWebView().captureSnapshot(captureIndex,
@@ -79,6 +91,9 @@
                     @Override
                     public void onReceiveValue(Bitmap value) {
                         mBitmaps.put(index, value);
+                        mLastCaptureTime = System.currentTimeMillis();
+                        mLastCaptureIndex = captureIndex;
+                        mLastBitmap = value;
                     }
                 }
         );
@@ -101,6 +116,12 @@
         }
 
         int captureIndex = mTab.getCaptureIndex(index);
+        long currentTime = System.currentTimeMillis();
+        if (captureIndex == mLastCaptureIndex &&
+                (currentTime < (mLastCaptureTime + MS_TIME_BETWEEN_CAPTURES))) {
+            mBitmaps.put(index, mLastBitmap);
+            return;
+        }
 
         mTab.getWebView().getSnapshot(captureIndex,
                 new ValueCallback<Bitmap>() {