1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* Copyright (C) 2014 Cyanogen, Inc.
*/
package com.cyngn.eleven.cache;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.AsyncTask;
import android.widget.ImageView;
import com.cyngn.eleven.cache.ImageWorker.ImageType;
import java.lang.ref.WeakReference;
/**
* The actual {@link android.os.AsyncTask} that will process the image.
*/
public abstract class BitmapWorkerTask<Params, Progress, Result>
extends AsyncTask<Params, Progress, Result> {
/**
* The {@link android.widget.ImageView} used to set the result
*/
protected final WeakReference<ImageView> mImageReference;
/**
* Type of URL to download
*/
protected final ImageWorker.ImageType mImageType;
/**
* Layer drawable used to cross fade the result from the worker
*/
protected Drawable mFromDrawable;
protected final Context mContext;
protected final ImageCache mImageCache;
protected final Resources mResources;
/**
* The key used to store cached entries
*/
public String mKey;
/**
* Constructor of <code>BitmapWorkerTask</code>
* @param key used for caching the image
* @param imageView The {@link ImageView} to use.
* @param imageType The type of image URL to fetch for.
* @param fromDrawable what drawable to transition from
*/
public BitmapWorkerTask(final String key, final ImageView imageView, final ImageType imageType,
final Drawable fromDrawable, final Context context) {
mKey = key;
mContext = context;
mImageCache = ImageCache.getInstance(mContext);
mResources = mContext.getResources();
mImageReference = new WeakReference<ImageView>(imageView);
mImageType = imageType;
// A transparent image (layer 0) and the new result (layer 1)
mFromDrawable = fromDrawable;
}
/**
* @return The {@link ImageView} associated with this task as long as
* the ImageView's task still points to this task as well.
* Returns null otherwise.
*/
protected ImageView getAttachedImageView() {
final ImageView imageView = mImageReference.get();
if (imageView != null) {
final BitmapWorkerTask bitmapWorkerTask = ImageWorker.getBitmapWorkerTask(imageView);
if (this == bitmapWorkerTask) {
return imageView;
}
}
return null;
}
/**
* Gets the bitmap given the input params
* @param params artistName, albumName, albumId
* @return Bitmap
*/
protected Bitmap getBitmapInBackground(final String... params) {
return ImageWorker.getBitmapInBackground(mContext, mImageCache, mKey,
params[1], params[0], Long.valueOf(params[2]), mImageType);
}
/**
* Creates a transition drawable with default parameters
* @param bitmap the bitmap to transition to
* @return the transition drawable
*/
protected TransitionDrawable createImageTransitionDrawable(final Bitmap bitmap) {
return createImageTransitionDrawable(bitmap, ImageWorker.FADE_IN_TIME, false, false);
}
/**
* Creates a transition drawable
* @param bitmap to transition to
* @param fadeTime the time to fade in ms
* @param dither setting
* @param force force create a transition even if bitmap == null (fade to transparent)
* @return the transition drawable
*/
protected TransitionDrawable createImageTransitionDrawable(final Bitmap bitmap,
final int fadeTime, final boolean dither, final boolean force) {
return ImageWorker.createImageTransitionDrawable(mResources, mFromDrawable, bitmap,
fadeTime, dither, force);
}
}
|