Bug 975173 - (Part 2) Always call BitmapLoader.onBitmapFound on the UI thread. r=bnicholson

This commit is contained in:
Margaret Leibovic 2014-02-22 09:41:30 -08:00
Родитель ca4c8e57ca
Коммит a28f1875e4
3 изменённых файлов: 42 добавлений и 32 удалений

Просмотреть файл

@ -835,24 +835,19 @@ public abstract class GeckoApp
void showButtonToast(final String message, final String buttonText,
final String buttonIcon, final String buttonId) {
BitmapUtils.getDrawable(GeckoApp.this, buttonIcon, new BitmapUtils.BitmapLoader() {
@Override
public void onBitmapFound(final Drawable d) {
ThreadUtils.postToUiThread(new Runnable() {
getButtonToast().show(false, message, buttonText, d, new ButtonToast.ToastListener() {
@Override
public void run() {
getButtonToast().show(false, message, buttonText, d, new ButtonToast.ToastListener() {
@Override
public void onButtonClicked() {
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Toast:Click", buttonId));
}
public void onButtonClicked() {
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Toast:Click", buttonId));
}
@Override
public void onToastHidden(ButtonToast.ReasonHidden reason) {
if (reason == ButtonToast.ReasonHidden.TIMEOUT) {
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Toast:Hidden", buttonId));
}
}
});
@Override
public void onToastHidden(ButtonToast.ReasonHidden reason) {
if (reason == ButtonToast.ReasonHidden.TIMEOUT) {
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Toast:Hidden", buttonId));
}
}
});
}

Просмотреть файл

@ -41,15 +41,36 @@ public final class BitmapUtils {
public void onBitmapFound(Drawable d);
}
private static void runOnBitmapFoundOnUiThread(final BitmapLoader loader, final Drawable d) {
if (ThreadUtils.isOnUiThread()) {
loader.onBitmapFound(d);
return;
}
ThreadUtils.postToUiThread(new Runnable() {
@Override
public void run() {
loader.onBitmapFound(d);
}
});
}
/**
* Attempts to find a drawable associated with a given string, using its URI scheme to determine
* how to load the drawable. The BitmapLoader's `onBitmapFound` method is always called, and
* will be called with `null` if no drawable is found.
*
* The BitmapLoader `onBitmapFound` method always runs on the UI thread.
*/
public static void getDrawable(final Context context, final String data, final BitmapLoader loader) {
if (TextUtils.isEmpty(data)) {
loader.onBitmapFound(null);
runOnBitmapFoundOnUiThread(loader, null);
return;
}
if (data.startsWith("data")) {
BitmapDrawable d = new BitmapDrawable(context.getResources(), getBitmapFromDataURI(data));
loader.onBitmapFound(d);
runOnBitmapFoundOnUiThread(loader, d);
return;
}
@ -95,7 +116,7 @@ public final class BitmapUtils {
try {
Drawable d = context.getPackageManager().getApplicationIcon(resource);
loader.onBitmapFound(d);
runOnBitmapFoundOnUiThread(loader, d);
} catch(Exception ex) { }
return;
@ -106,11 +127,11 @@ public final class BitmapUtils {
int id = getResource(imageUri, R.drawable.ic_status_logo);
Drawable d = context.getResources().getDrawable(id);
loader.onBitmapFound(d);
runOnBitmapFoundOnUiThread(loader, d);
return;
}
loader.onBitmapFound(null);
runOnBitmapFoundOnUiThread(loader, null);
}
public static Bitmap decodeByteArray(byte[] bytes) {

Просмотреть файл

@ -141,18 +141,12 @@ public class HomeBanner extends LinearLayout
BitmapUtils.getDrawable(getContext(), iconURI, new BitmapUtils.BitmapLoader() {
@Override
public void onBitmapFound(final Drawable d) {
// Update the banner icon on the UI thread.
ThreadUtils.postToUiThread(new Runnable() {
@Override
public void run() {
// Hide the image view if we don't have an icon to show.
if (d == null) {
mIconView.setVisibility(View.GONE);
} else {
mIconView.setImageDrawable(d);
}
}
});
// Hide the image view if we don't have an icon to show.
if (d == null) {
mIconView.setVisibility(View.GONE);
} else {
mIconView.setImageDrawable(d);
}
}
});
}