2 * Copyright (C) 2014 The CyanogenMod Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.lineageos.eleven.cache;
18 import android.content.Context;
19 import android.content.res.Resources;
20 import android.graphics.Bitmap;
21 import android.graphics.drawable.Drawable;
22 import android.graphics.drawable.TransitionDrawable;
23 import android.os.AsyncTask;
24 import android.widget.ImageView;
26 import org.lineageos.eleven.cache.ImageWorker.ImageType;
28 import java.lang.ref.WeakReference;
31 * The actual {@link android.os.AsyncTask} that will process the image.
33 public abstract class BitmapWorkerTask<Params, Progress, Result>
34 extends AsyncTask<Params, Progress, Result> {
36 * The {@link android.widget.ImageView} used to set the result
38 protected final WeakReference<ImageView> mImageReference;
41 * Type of URL to download
43 protected final ImageWorker.ImageType mImageType;
46 * Layer drawable used to cross fade the result from the worker
48 protected Drawable mFromDrawable;
50 protected final Context mContext;
52 protected final ImageCache mImageCache;
54 protected final Resources mResources;
56 protected boolean mScaleImgToView;
59 * The key used to store cached entries
64 * Constructor of <code>BitmapWorkerTask</code>
65 * @param key used for caching the image
66 * @param imageView The {@link ImageView} to use.
67 * @param imageType The type of image URL to fetch for.
68 * @param fromDrawable what drawable to transition from
70 public BitmapWorkerTask(final String key, final ImageView imageView, final ImageType imageType,
71 final Drawable fromDrawable, final Context context) {
72 this(key, imageView, imageType, fromDrawable, context, false);
76 * Constructor of <code>BitmapWorkerTask</code>
77 * @param key used for caching the image
78 * @param imageView The {@link ImageView} to use.
79 * @param imageType The type of image URL to fetch for.
80 * @param fromDrawable what drawable to transition from
81 * @param scaleImgToView flag to scale the bitmap to the image view bounds
83 public BitmapWorkerTask(final String key, final ImageView imageView, final ImageType imageType,
84 final Drawable fromDrawable, final Context context, final boolean scaleImgToView) {
88 mImageCache = ImageCache.getInstance(mContext);
89 mResources = mContext.getResources();
91 mImageReference = new WeakReference<>(imageView);
92 mImageType = imageType;
94 // A transparent image (layer 0) and the new result (layer 1)
95 mFromDrawable = fromDrawable;
97 mScaleImgToView = scaleImgToView;
101 * @return The {@link ImageView} associated with this task as long as
102 * the ImageView's task still points to this task as well.
103 * Returns null otherwise.
105 protected ImageView getAttachedImageView() {
106 final ImageView imageView = mImageReference.get();
107 if (imageView != null) {
108 final BitmapWorkerTask bitmapWorkerTask = ImageWorker.getBitmapWorkerTask(imageView);
109 if (this == bitmapWorkerTask) {
118 * Gets the bitmap given the input params
119 * @param params artistName, albumName, albumId
122 protected Bitmap getBitmapInBackground(final String... params) {
123 return ImageWorker.getBitmapInBackground(mContext, mImageCache, mKey,
124 params[1], params[0], Long.valueOf(params[2]), mImageType);
128 * Creates a transition drawable with default parameters
129 * @param bitmap the bitmap to transition to
130 * @return the transition drawable
132 protected TransitionDrawable createImageTransitionDrawable(final Bitmap bitmap) {
133 return createImageTransitionDrawable(bitmap, ImageWorker.FADE_IN_TIME, false, false);
137 * Creates a transition drawable
138 * @param bitmap to transition to
139 * @param fadeTime the time to fade in ms
140 * @param dither setting
141 * @param force force create a transition even if bitmap == null (fade to transparent)
142 * @return the transition drawable
144 protected TransitionDrawable createImageTransitionDrawable(final Bitmap bitmap,
145 final int fadeTime, final boolean dither, final boolean force) {
146 return ImageWorker.createImageTransitionDrawable(mResources, mFromDrawable, bitmap,
147 fadeTime, dither, force);