/* Copyright (c) 2009-2011 Matthias Kaeppler
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.ignition.support.images.remote;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

public class RemoteImageLoaderHandler extends Handler {

    public static final int HANDLER_MESSAGE_ID = 0;
    public static final String BITMAP_EXTRA = "ign:extra_bitmap";
    public static final String IMAGE_URL_EXTRA = "ign:extra_image_url";

    private ImageView imageView;
    private String imageUrl;
    private Drawable errorDrawable;

    public RemoteImageLoaderHandler(ImageView imageView, String imageUrl, Drawable errorDrawable) {
        this.imageView = imageView;
        this.imageUrl = imageUrl;
        this.errorDrawable = errorDrawable;
    }

    @Override
    public final void handleMessage(Message msg) {
        if (msg.what == HANDLER_MESSAGE_ID) {
            handleImageLoadedMessage(msg);
        }
    }

    protected final void handleImageLoadedMessage(Message msg) {
        Bundle data = msg.getData();
        Bitmap bitmap = data.getParcelable(BITMAP_EXTRA);
        handleImageLoaded(bitmap, msg);
    }

    /**
     * Override this method if you need custom handler logic. Note that this method can actually be
     * called directly for performance reasons, in which case the message will be null
     * 
     * @param bitmap
     *            the bitmap returned from the image loader
     * @param msg
     *            the handler message; can be null
     * @return true if the view was updated with the new image, false if it was discarded
     */
    protected boolean handleImageLoaded(Bitmap bitmap, Message msg) {
        // If this handler is used for loading images in a ListAdapter,
        // the thread will set the image only if it's the right position,
        // otherwise it won't do anything.
        String forUrl = (String) imageView.getTag();
        if (imageUrl.equals(forUrl)) {
            Bitmap image = (bitmap == null ? ((BitmapDrawable) errorDrawable).getBitmap() : bitmap);
            imageView.setImageBitmap(image);

            // remove the image URL from the view's tag
            imageView.setTag(null);

            return true;
        }

        return false;
    }

    public String getImageUrl() {
        return imageUrl;
    }

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }

    public ImageView getImageView() {
        return imageView;
    }

    public void setImageView(ImageView imageView) {
        this.imageView = imageView;
    }
}

@@ -2616,135 +2616,184 @@
 if (
-imageUrl.equals(forUrl)) %7B%0A            Bitmap image = (bitmap == null ? ((BitmapDrawable) errorDrawable).getBitmap() : bitmap);
+bitmap == null && errorDrawable != null) %7B%0A            bitmap = ((BitmapDrawable) errorDrawable).getBitmap();%0A        %7D%0A        if (bitmap != null && imageUrl.equals(forUrl)) %7B
 %0A   
@@ -2826,21 +2826,22 @@
 eBitmap(
-image
+bitmap
 );%0A%0A    

Add a NULL check for errorDrawable; if both bitmap and error bitmap are NULL, make this a no-op.