OSDN Git Service

Correct some lint warnings
[android-x86/packages-apps-Eleven.git] / src / org / lineageos / eleven / cache / BitmapWorkerTask.java
1 /*
2 * Copyright (C) 2014 The CyanogenMod Project
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 org.lineageos.eleven.cache;
17
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;
25
26 import org.lineageos.eleven.cache.ImageWorker.ImageType;
27
28 import java.lang.ref.WeakReference;
29
30 /**
31  * The actual {@link android.os.AsyncTask} that will process the image.
32  */
33 public abstract class BitmapWorkerTask<Params, Progress, Result>
34         extends AsyncTask<Params, Progress, Result> {
35     /**
36      * The {@link android.widget.ImageView} used to set the result
37      */
38     protected final WeakReference<ImageView> mImageReference;
39
40     /**
41      * Type of URL to download
42      */
43     protected final ImageWorker.ImageType mImageType;
44
45     /**
46      * Layer drawable used to cross fade the result from the worker
47      */
48     protected Drawable mFromDrawable;
49
50     protected final Context mContext;
51
52     protected final ImageCache mImageCache;
53
54     protected final Resources mResources;
55
56     protected boolean mScaleImgToView;
57
58     /**
59      * The key used to store cached entries
60      */
61     public String mKey;
62
63     /**
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
69      */
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);
73     }
74
75     /**
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
82      */
83     public BitmapWorkerTask(final String key, final ImageView imageView, final ImageType imageType,
84                             final Drawable fromDrawable, final Context context, final boolean scaleImgToView) {
85         mKey = key;
86
87         mContext = context;
88         mImageCache = ImageCache.getInstance(mContext);
89         mResources = mContext.getResources();
90
91         mImageReference = new WeakReference<>(imageView);
92         mImageType = imageType;
93
94         // A transparent image (layer 0) and the new result (layer 1)
95         mFromDrawable = fromDrawable;
96
97         mScaleImgToView = scaleImgToView;
98     }
99
100     /**
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.
104      */
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) {
110                 return imageView;
111             }
112         }
113
114         return null;
115     }
116
117     /**
118      * Gets the bitmap given the input params
119      * @param params artistName, albumName, albumId
120      * @return Bitmap
121      */
122     protected Bitmap getBitmapInBackground(final String... params) {
123         return ImageWorker.getBitmapInBackground(mContext, mImageCache, mKey,
124                 params[1], params[0], Long.valueOf(params[2]), mImageType);
125     }
126
127     /**
128      * Creates a transition drawable with default parameters
129      * @param bitmap the bitmap to transition to
130      * @return the transition drawable
131      */
132     protected TransitionDrawable createImageTransitionDrawable(final Bitmap bitmap) {
133         return createImageTransitionDrawable(bitmap, ImageWorker.FADE_IN_TIME, false, false);
134     }
135
136     /**
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
143      */
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);
148     }
149 }