Skip to content

Commit

Permalink
Bug 1258470 - 2. Move thumbnail code out of BitmapUtils; r=nalexander
Browse files Browse the repository at this point in the history
Move the "thumbnail:" handler out of BitmapUtils and into
ThumbnailHelper and PromptListItem.

The patch adds two overloads of the getAndProcessThumbnailFor method in
ThumbnailHelper, which handle the tasks of getting a thumbnail for a
specific tab and calling a given BitmapLoader.

Because only PromptListItem makes use of the "thumbnail:" convention,
the actual handling of "thumbnail:" is moved to PromptListItem, which
calls ThumbnailHelper to get the thumbnail.
  • Loading branch information
Jim Chen committed Sep 14, 2016
1 parent bdb3591 commit 3013200
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
23 changes: 23 additions & 0 deletions mobile/android/base/java/org/mozilla/gecko/ThumbnailHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,29 @@ private ThumbnailHelper() {
mHeight = -1;
}

public void getAndProcessThumbnailFor(final int tabId, final BitmapUtils.BitmapLoader loader) {
final Tab tab = Tabs.getInstance().getTab(tabId);
if (tab != null) {
getAndProcessThumbnailFor(tab, loader);
}
}

public void getAndProcessThumbnailFor(final Tab tab, final BitmapUtils.BitmapLoader loader) {
BitmapUtils.runOnBitmapFoundOnUiThread(loader, tab.getThumbnail());

Tabs.registerOnTabsChangedListener(new Tabs.OnTabsChangedListener() {
@Override
public void onTabChanged(final Tab t, final Tabs.TabEvents msg, final String data) {
if (tab != t || msg != Tabs.TabEvents.THUMBNAIL) {
return;
}
Tabs.unregisterOnTabsChangedListener(this);
BitmapUtils.runOnBitmapFoundOnUiThread(loader, t.getThumbnail());
}
});
getAndProcessThumbnailFor(tab);
}

public void getAndProcessThumbnailFor(Tab tab) {
if (AboutPages.isAboutHome(tab.getURL()) || AboutPages.isAboutPrivateBrowsing(tab.getURL())) {
tab.updateThumbnail(null, CachePolicy.NO_STORE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.mozilla.gecko.IntentHelper;
import org.mozilla.gecko.gfx.BitmapUtils;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.ThumbnailHelper;
import org.mozilla.gecko.widget.GeckoActionProvider;

import org.json.JSONArray;
Expand Down Expand Up @@ -57,12 +58,19 @@ public class PromptListItem {

final String iconStr = aObject.optString("icon");
if (iconStr != null) {
BitmapUtils.getDrawable(context, iconStr, new BitmapUtils.BitmapLoader() {
final BitmapUtils.BitmapLoader loader = new BitmapUtils.BitmapLoader() {
@Override
public void onBitmapFound(Drawable d) {
mIcon = d;
}
});
};

if (iconStr.startsWith("thumbnail:")) {
final int id = Integer.parseInt(iconStr.substring(10), 10);
ThumbnailHelper.getInstance().getAndProcessThumbnailFor(id, loader);
} else {
BitmapUtils.getDrawable(context, iconStr, loader);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
import org.mozilla.gecko.util.GeckoJarReader;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.util.UIAsyncTask;
import org.mozilla.gecko.Tab;
import org.mozilla.gecko.Tabs;
import org.mozilla.gecko.ThumbnailHelper;

import android.content.Context;
import android.content.res.Resources;
Expand Down Expand Up @@ -47,7 +44,7 @@ public interface BitmapLoader {
public void onBitmapFound(Drawable d);
}

private static void runOnBitmapFoundOnUiThread(final BitmapLoader loader, final Drawable d) {
public static void runOnBitmapFoundOnUiThread(final BitmapLoader loader, final Drawable d) {
if (ThreadUtils.isOnUiThread()) {
loader.onBitmapFound(d);
return;
Expand Down Expand Up @@ -80,11 +77,6 @@ public static void getDrawable(final Context context, final String data, final B
return;
}

if (data.startsWith("thumbnail:")) {
getThumbnailDrawable(context, data, loader);
return;
}

if (data.startsWith("jar:") || data.startsWith("file://")) {
(new UIAsyncTask.WithoutParams<Drawable>(ThreadUtils.getBackgroundHandler()) {
@Override
Expand Down Expand Up @@ -147,22 +139,6 @@ public void onPostExecute(Drawable drawable) {
runOnBitmapFoundOnUiThread(loader, null);
}

public static void getThumbnailDrawable(final Context context, final String data, final BitmapLoader loader) {
int id = Integer.parseInt(data.substring(10), 10);
final Tab tab = Tabs.getInstance().getTab(id);
runOnBitmapFoundOnUiThread(loader, tab.getThumbnail());
Tabs.registerOnTabsChangedListener(new Tabs.OnTabsChangedListener() {
@Override
public void onTabChanged(Tab t, Tabs.TabEvents msg, String data) {
if (tab == t && msg == Tabs.TabEvents.THUMBNAIL) {
Tabs.unregisterOnTabsChangedListener(this);
runOnBitmapFoundOnUiThread(loader, t.getThumbnail());
}
}
});
ThumbnailHelper.getInstance().getAndProcessThumbnailFor(tab);
}

public static Bitmap decodeByteArray(byte[] bytes) {
return decodeByteArray(bytes, null);
}
Expand Down

0 comments on commit 3013200

Please sign in to comment.