michael@0: /* michael@0: * Copyright (C) 2013 Square, Inc. michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: package com.squareup.picasso; michael@0: michael@0: import android.content.Context; michael@0: import android.graphics.Bitmap; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.widget.ImageView; michael@0: michael@0: class ImageViewAction extends Action { michael@0: michael@0: Callback callback; michael@0: michael@0: ImageViewAction(Picasso picasso, ImageView imageView, Request data, boolean skipCache, michael@0: boolean noFade, int errorResId, Drawable errorDrawable, String key, Callback callback) { michael@0: super(picasso, imageView, data, skipCache, noFade, errorResId, errorDrawable, key); michael@0: this.callback = callback; michael@0: } michael@0: michael@0: @Override public void complete(Bitmap result, Picasso.LoadedFrom from) { michael@0: if (result == null) { michael@0: throw new AssertionError( michael@0: String.format("Attempted to complete action with no result!\n%s", this)); michael@0: } michael@0: michael@0: ImageView target = this.target.get(); michael@0: if (target == null) { michael@0: return; michael@0: } michael@0: michael@0: Context context = picasso.context; michael@0: boolean debugging = picasso.debugging; michael@0: PicassoDrawable.setBitmap(target, context, result, from, noFade, debugging); michael@0: michael@0: if (callback != null) { michael@0: callback.onSuccess(); michael@0: } michael@0: } michael@0: michael@0: @Override public void error() { michael@0: ImageView target = this.target.get(); michael@0: if (target == null) { michael@0: return; michael@0: } michael@0: if (errorResId != 0) { michael@0: target.setImageResource(errorResId); michael@0: } else if (errorDrawable != null) { michael@0: target.setImageDrawable(errorDrawable); michael@0: } michael@0: michael@0: if (callback != null) { michael@0: callback.onError(); michael@0: } michael@0: } michael@0: michael@0: @Override void cancel() { michael@0: super.cancel(); michael@0: if (callback != null) { michael@0: callback = null; michael@0: } michael@0: } michael@0: }