|
1 /* |
|
2 * Copyright (C) 2013 Square, Inc. |
|
3 * |
|
4 * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 * you may not use this file except in compliance with the License. |
|
6 * You may obtain a copy of the License at |
|
7 * |
|
8 * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 * |
|
10 * Unless required by applicable law or agreed to in writing, software |
|
11 * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 * See the License for the specific language governing permissions and |
|
14 * limitations under the License. |
|
15 */ |
|
16 package com.squareup.picasso; |
|
17 |
|
18 import android.content.Context; |
|
19 import android.graphics.Bitmap; |
|
20 import android.graphics.drawable.Drawable; |
|
21 import android.widget.ImageView; |
|
22 |
|
23 class ImageViewAction extends Action<ImageView> { |
|
24 |
|
25 Callback callback; |
|
26 |
|
27 ImageViewAction(Picasso picasso, ImageView imageView, Request data, boolean skipCache, |
|
28 boolean noFade, int errorResId, Drawable errorDrawable, String key, Callback callback) { |
|
29 super(picasso, imageView, data, skipCache, noFade, errorResId, errorDrawable, key); |
|
30 this.callback = callback; |
|
31 } |
|
32 |
|
33 @Override public void complete(Bitmap result, Picasso.LoadedFrom from) { |
|
34 if (result == null) { |
|
35 throw new AssertionError( |
|
36 String.format("Attempted to complete action with no result!\n%s", this)); |
|
37 } |
|
38 |
|
39 ImageView target = this.target.get(); |
|
40 if (target == null) { |
|
41 return; |
|
42 } |
|
43 |
|
44 Context context = picasso.context; |
|
45 boolean debugging = picasso.debugging; |
|
46 PicassoDrawable.setBitmap(target, context, result, from, noFade, debugging); |
|
47 |
|
48 if (callback != null) { |
|
49 callback.onSuccess(); |
|
50 } |
|
51 } |
|
52 |
|
53 @Override public void error() { |
|
54 ImageView target = this.target.get(); |
|
55 if (target == null) { |
|
56 return; |
|
57 } |
|
58 if (errorResId != 0) { |
|
59 target.setImageResource(errorResId); |
|
60 } else if (errorDrawable != null) { |
|
61 target.setImageDrawable(errorDrawable); |
|
62 } |
|
63 |
|
64 if (callback != null) { |
|
65 callback.onError(); |
|
66 } |
|
67 } |
|
68 |
|
69 @Override void cancel() { |
|
70 super.cancel(); |
|
71 if (callback != null) { |
|
72 callback = null; |
|
73 } |
|
74 } |
|
75 } |