|
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.graphics.Bitmap; |
|
19 import android.graphics.drawable.Drawable; |
|
20 import java.lang.ref.ReferenceQueue; |
|
21 import java.lang.ref.WeakReference; |
|
22 |
|
23 abstract class Action<T> { |
|
24 static class RequestWeakReference<T> extends WeakReference<T> { |
|
25 final Action action; |
|
26 |
|
27 public RequestWeakReference(Action action, T referent, ReferenceQueue<? super T> q) { |
|
28 super(referent, q); |
|
29 this.action = action; |
|
30 } |
|
31 } |
|
32 |
|
33 final Picasso picasso; |
|
34 final Request data; |
|
35 final WeakReference<T> target; |
|
36 final boolean skipCache; |
|
37 final boolean noFade; |
|
38 final int errorResId; |
|
39 final Drawable errorDrawable; |
|
40 final String key; |
|
41 |
|
42 boolean cancelled; |
|
43 |
|
44 Action(Picasso picasso, T target, Request data, boolean skipCache, boolean noFade, |
|
45 int errorResId, Drawable errorDrawable, String key) { |
|
46 this.picasso = picasso; |
|
47 this.data = data; |
|
48 this.target = new RequestWeakReference<T>(this, target, picasso.referenceQueue); |
|
49 this.skipCache = skipCache; |
|
50 this.noFade = noFade; |
|
51 this.errorResId = errorResId; |
|
52 this.errorDrawable = errorDrawable; |
|
53 this.key = key; |
|
54 } |
|
55 |
|
56 abstract void complete(Bitmap result, Picasso.LoadedFrom from); |
|
57 |
|
58 abstract void error(); |
|
59 |
|
60 void cancel() { |
|
61 cancelled = true; |
|
62 } |
|
63 |
|
64 Request getData() { |
|
65 return data; |
|
66 } |
|
67 |
|
68 T getTarget() { |
|
69 return target.get(); |
|
70 } |
|
71 |
|
72 String getKey() { |
|
73 return key; |
|
74 } |
|
75 |
|
76 boolean isCancelled() { |
|
77 return cancelled; |
|
78 } |
|
79 |
|
80 Picasso getPicasso() { |
|
81 return picasso; |
|
82 } |
|
83 } |