OSDN Git Service

027eb7f91b81aad510297e9753e27a65768797cc
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / cache / SimpleBitmapWorkerTask.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 com.cyanogenmod.eleven.cache;
17
18 import android.content.Context;
19 import android.graphics.Bitmap;
20 import android.graphics.drawable.Drawable;
21 import android.graphics.drawable.TransitionDrawable;
22 import android.widget.ImageView;
23 import com.cyanogenmod.eleven.cache.ImageWorker.ImageType;
24 import com.cyanogenmod.eleven.utils.ImageUtils;
25
26 /**
27  * The actual {@link android.os.AsyncTask} that will process the image.
28  */
29 public class SimpleBitmapWorkerTask extends BitmapWorkerTask<String, Void, TransitionDrawable> {
30
31     /**
32      * Constructor of <code>BitmapWorkerTask</code>
33      *
34      * @param key the key of the image to store to
35      * @param imageView The {@link ImageView} to use.
36      * @param imageType The type of image URL to fetch for.
37      * @param fromDrawable what drawable to transition from
38      */
39     public SimpleBitmapWorkerTask(final String key, final ImageView imageView, final ImageType imageType,
40                             final Drawable fromDrawable, final Context context) {
41         super(key, imageView, imageType, fromDrawable, context);
42     }
43
44     /**
45      * Constructor of <code>BitmapWorkerTask</code>
46      *
47      * @param key the key of the image to store to
48      * @param imageView The {@link ImageView} to use.
49      * @param imageType The type of image URL to fetch for.
50      * @param fromDrawable what drawable to transition from
51      * @param scaleImgToView flag to scale the bitmap to the image view bounds
52      */
53     public SimpleBitmapWorkerTask(final String key, final ImageView imageView, final ImageType imageType,
54                                   final Drawable fromDrawable, final Context context, final boolean scaleImgToView) {
55         super(key, imageView, imageType, fromDrawable, context, scaleImgToView);
56     }
57
58     /**
59      * {@inheritDoc}
60      */
61     @Override
62     protected TransitionDrawable doInBackground(final String... params) {
63         if (isCancelled()) {
64             return null;
65         }
66
67         final Bitmap bitmap = getBitmapInBackground(params);
68         if (mScaleImgToView) {
69             Bitmap scaledBitmap = ImageUtils.scaleBitmapForImageView(bitmap, getAttachedImageView());
70             return createImageTransitionDrawable(scaledBitmap);
71         }
72         else
73             return createImageTransitionDrawable(bitmap);
74     }
75
76     /**
77      * {@inheritDoc}
78      */
79     @Override
80     protected void onPostExecute(TransitionDrawable transitionDrawable) {
81         final ImageView imageView = getAttachedImageView();
82         if (transitionDrawable != null && imageView != null) {
83             imageView.setImageDrawable(transitionDrawable);
84         } else if (imageView != null) {
85             imageView.setImageDrawable(mFromDrawable);
86         }
87     }
88 }