summaryrefslogtreecommitdiffstats
path: root/src/org/lineageos/eleven/cache/BitmapWorkerTask.java
blob: c7ab8425ab0fdcf04e9721fb0e62a9738b8707c5 (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (C) 2014 The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cyanogenmod.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.cyanogenmod.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;

    protected boolean mScaleImgToView;

    /**
     * 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) {
        this(key, imageView, imageType, fromDrawable, context, false);
    }

    /**
     * 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
     * @param scaleImgToView flag to scale the bitmap to the image view bounds
     */
    public BitmapWorkerTask(final String key, final ImageView imageView, final ImageType imageType,
                            final Drawable fromDrawable, final Context context, final boolean scaleImgToView) {
        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;

        mScaleImgToView = scaleImgToView;
    }

    /**
     * @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);
    }
}