Restore overhaul

 Bug: 5069192
 Store thumbnails in a database restored async for each tab
 Fix restoring a tab not restoring its current state

Change-Id: I2c14e352638aac0ef766fb3bf4036ff220c53ecd
diff --git a/src/com/android/browser/Controller.java b/src/com/android/browser/Controller.java
index b3be618..92f448c 100644
--- a/src/com/android/browser/Controller.java
+++ b/src/com/android/browser/Controller.java
@@ -80,6 +80,7 @@
 import com.android.browser.UI.ComboViews;
 import com.android.browser.UI.DropdownChangeListener;
 import com.android.browser.provider.BrowserProvider;
+import com.android.browser.provider.BrowserProvider2.Thumbnails;
 import com.android.browser.provider.SnapshotProvider.Snapshots;
 import com.android.browser.search.SearchEngine;
 import com.android.common.Search;
@@ -87,6 +88,7 @@
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.net.URLEncoder;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.HashMap;
 import java.util.List;
@@ -309,6 +311,7 @@
     private void onPreloginFinished(Bundle icicle, Intent intent, long currentTabId,
             boolean restoreIncognitoTabs) {
         if (currentTabId == -1) {
+            BackgroundHandler.execute(new PruneThumbnails(mActivity, null));
             final Bundle extra = intent.getExtras();
             // Create an initial tab.
             // If the intent is ACTION_VIEW and data is not null, the Browser is
@@ -335,7 +338,13 @@
         } else {
             mTabControl.restoreState(icicle, currentTabId, restoreIncognitoTabs,
                     mUi.needsRestoreAllTabs());
-            mUi.updateTabs(mTabControl.getTabs());
+            List<Tab> tabs = mTabControl.getTabs();
+            ArrayList<Long> restoredTabs = new ArrayList<Long>(tabs.size());
+            for (Tab t : tabs) {
+                restoredTabs.add(t.getId());
+            }
+            BackgroundHandler.execute(new PruneThumbnails(mActivity, restoredTabs));
+            mUi.updateTabs(tabs);
             // TabControl.restoreState() will create a new tab even if
             // restoring the state fails.
             setActiveTab(mTabControl.getCurrentTab());
@@ -357,6 +366,38 @@
         }
     }
 
+    private static class PruneThumbnails implements Runnable {
+        private Context mContext;
+        private List<Long> mIds;
+
+        PruneThumbnails(Context context, List<Long> preserveIds) {
+            mContext = context.getApplicationContext();
+            mIds = preserveIds;
+        }
+
+        @Override
+        public void run() {
+            ContentResolver cr = mContext.getContentResolver();
+            if (mIds == null || mIds.size() == 0) {
+                cr.delete(Thumbnails.CONTENT_URI, null, null);
+            } else {
+                int length = mIds.size();
+                StringBuilder where = new StringBuilder();
+                where.append(Thumbnails._ID);
+                where.append(" not in (");
+                for (int i = 0; i < length; i++) {
+                    where.append(mIds.get(i));
+                    if (i < (length - 1)) {
+                        where.append(",");
+                    }
+                }
+                where.append(")");
+                cr.delete(Thumbnails.CONTENT_URI, where.toString(), null);
+            }
+        }
+
+    }
+
     @Override
     public WebViewFactory getWebViewFactory() {
         return mFactory;
@@ -612,7 +653,7 @@
 
     }
 
-    void onSaveInstanceState(Bundle outState, boolean saveImages) {
+    void onSaveInstanceState(Bundle outState) {
         // the default implementation requires each view to have an id. As the
         // browser handles the state itself and it doesn't use id for the views,
         // don't call the default implementation. Otherwise it will trigger the
@@ -620,7 +661,7 @@
         // focused view XXX has no id".
 
         // Save all the tabs
-        mTabControl.saveState(outState, false);
+        mTabControl.saveState(outState);
         if (!outState.isEmpty()) {
             // Save time so that we know how old incognito tabs (if any) are.
             outState.putSerializable("lastActiveDate", Calendar.getInstance());
@@ -1902,13 +1943,6 @@
                 R.dimen.bookmarkThumbnailHeight);
     }
 
-    static Bitmap createScreenshot(Tab tab, int width, int height) {
-        if ((tab != null) && (tab.getWebView() != null)) {
-            return createScreenshot(tab.getWebView(), width, height);
-        }
-        return null;
-    }
-
     static Bitmap createScreenshot(WebView view, int width, int height) {
         // We render to a bitmap 2x the desired size so that we can then
         // re-scale it with filtering since canvas.scale doesn't filter
@@ -2646,4 +2680,9 @@
         return true;
     }
 
+    @Override
+    public boolean shouldCaptureThumbnails() {
+        return mUi.shouldCaptureThumbnails();
+    }
+
 }