Bug 1130694 - Remove DB code path from ThumbnailHelper.getAndProcessThumbnailFor and move to Tab class r=rnewman
authorMark Finkle <mfinkle@mozilla.com>
Thu, 12 Feb 2015 10:03:47 -0500
changeset 242542 8839dac4cffc1738eecdcc5944c06de382609444
parent 242541 983c6b50a4319a69f45b4f548a7ff26144e9608c
child 242543 fef1003fe34b3dc64085e5897ab180778d40ada4
push id660
push usermichael.l.comella@gmail.com
push dateThu, 12 Feb 2015 18:55:31 +0000
reviewersrnewman
bugs1130694
milestone38.0a1
Bug 1130694 - Remove DB code path from ThumbnailHelper.getAndProcessThumbnailFor and move to Tab class r=rnewman
mobile/android/base/Tab.java
mobile/android/base/ThumbnailHelper.java
mobile/android/base/gfx/BitmapUtils.java
--- a/mobile/android/base/Tab.java
+++ b/mobile/android/base/Tab.java
@@ -16,16 +16,17 @@ import java.util.regex.Pattern;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.URLMetadata;
 import org.mozilla.gecko.favicons.Favicons;
 import org.mozilla.gecko.favicons.LoadFaviconTask;
 import org.mozilla.gecko.favicons.OnFaviconLoadedListener;
 import org.mozilla.gecko.favicons.RemoteFavicon;
+import org.mozilla.gecko.gfx.BitmapUtils;
 import org.mozilla.gecko.gfx.Layer;
 import org.mozilla.gecko.toolbar.BrowserToolbar.TabEditingState;
 import org.mozilla.gecko.util.ThreadUtils;
 
 import android.content.ContentResolver;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.Color;
@@ -636,48 +637,70 @@ public class Tab {
 
         ThreadUtils.getBackgroundHandler().postDelayed(new Runnable() {
             @Override
             public void run() {
                 // tab.getURL() may return null
                 if (!TextUtils.equals(oldURL, getURL()))
                     return;
 
-                ThumbnailHelper.getInstance().getAndProcessThumbnailFor(tab, mDB);
+                ThumbnailHelper.getInstance().getAndProcessThumbnailFor(tab);
             }
         }, 500);
     }
 
     void handleContentLoaded() {
         setLoadProgressIfLoading(LOAD_PROGRESS_LOADED);
     }
 
     protected void saveThumbnailToDB(final BrowserDB db) {
         final BitmapDrawable thumbnail = mThumbnail;
         if (thumbnail == null) {
             return;
         }
 
         try {
-            String url = getURL();
+            final String url = getURL();
             if (url == null) {
                 return;
             }
 
             db.updateThumbnailForUrl(getContentResolver(), url, thumbnail);
         } catch (Exception e) {
             // ignore
         }
     }
 
+    public void loadThumbnailFromDB(final BrowserDB db) {
+        try {
+            final String url = getURL();
+            if (url == null) {
+                return;
+            }
+
+            byte[] thumbnail = db.getThumbnailForUrl(getContentResolver(), url);
+            if (thumbnail == null) {
+                return;
+            }
+
+            Bitmap bitmap = BitmapUtils.decodeByteArray(thumbnail);
+            mThumbnail = new BitmapDrawable(mAppContext.getResources(), bitmap);
+
+            Tabs.getInstance().notifyListeners(Tab.this, Tabs.TabEvents.THUMBNAIL);
+        } catch (Exception e) {
+            // ignore
+        }
+    }
+
     private void clearThumbnailFromDB(final BrowserDB db) {
         try {
-            String url = getURL();
-            if (url == null)
+            final String url = getURL();
+            if (url == null) {
                 return;
+            }
 
             // Passing in a null thumbnail will delete the stored thumbnail for this url
             db.updateThumbnailForUrl(getContentResolver(), url, null);
         } catch (Exception e) {
             // ignore
         }
     }
 
--- a/mobile/android/base/ThumbnailHelper.java
+++ b/mobile/android/base/ThumbnailHelper.java
@@ -1,16 +1,15 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
-import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.gfx.BitmapUtils;
 import org.mozilla.gecko.mozglue.DirectBufferAllocator;
 import org.mozilla.gecko.mozglue.generatorannotations.WrapElementForJNI;
 
 import android.graphics.Bitmap;
 import android.util.Log;
 import android.content.res.Resources;
 
@@ -63,39 +62,22 @@ public final class ThumbnailHelper {
         mPendingThumbnails = new LinkedList<Tab>();
         try {
             mPendingWidth = new AtomicInteger((int)GeckoAppShell.getContext().getResources().getDimension(R.dimen.tab_thumbnail_width));
         } catch (Resources.NotFoundException nfe) { mPendingWidth = new AtomicInteger(0); }
         mWidth = -1;
         mHeight = -1;
     }
 
-    public void getAndProcessThumbnailFor(Tab tab, final BrowserDB db) {
+    public void getAndProcessThumbnailFor(Tab tab) {
         if (AboutPages.isAboutHome(tab.getURL())) {
             tab.updateThumbnail(null, CachePolicy.NO_STORE);
             return;
         }
 
-        // If we don't have a database, there's nothing left to do.
-        if (db == null) {
-            return;
-        }
-
-        if (tab.getState() == Tab.STATE_DELAYED) {
-            String url = tab.getURL();
-            if (url != null) {
-                byte[] thumbnail = db.getThumbnailForUrl(GeckoAppShell.getContext().getContentResolver(), url);
-                if (thumbnail != null) {
-                    // Since this thumbnail is from the database, its ok to store it
-                    setTabThumbnail(tab, null, thumbnail, CachePolicy.STORE);
-                }
-            }
-            return;
-        }
-
         synchronized (mPendingThumbnails) {
             if (mPendingThumbnails.lastIndexOf(tab) > 0) {
                 // This tab is already in the queue, so don't add it again.
                 // Note that if this tab is only at the *head* of the queue,
                 // (i.e. mPendingThumbnails.lastIndexOf(tab) == 0) then we do
                 // add it again because it may have already been thumbnailed
                 // and now we need to do it again.
                 return;
--- a/mobile/android/base/gfx/BitmapUtils.java
+++ b/mobile/android/base/gfx/BitmapUtils.java
@@ -148,18 +148,17 @@ public final class BitmapUtils {
                  @Override
                  public void onTabChanged(Tab t, Tabs.TabEvents msg, Object data) {
                      if (tab == t && msg == Tabs.TabEvents.THUMBNAIL) {
                          Tabs.unregisterOnTabsChangedListener(this);
                          runOnBitmapFoundOnUiThread(loader, t.getThumbnail());
                      }
                  }
              });
-         final GeckoProfile profile = GeckoProfile.get(context);
-         ThumbnailHelper.getInstance().getAndProcessThumbnailFor(tab, profile.getDB());
+         ThumbnailHelper.getInstance().getAndProcessThumbnailFor(tab);
     }
 
     public static Bitmap decodeByteArray(byte[] bytes) {
         return decodeByteArray(bytes, null);
     }
 
     public static Bitmap decodeByteArray(byte[] bytes, BitmapFactory.Options options) {
         return decodeByteArray(bytes, 0, bytes.length, options);