blob: a394508a79aeaf9b31e7b7e69c172f525bae1f74 [file] [log] [blame]
The Android Open Source Projectb64d3452009-03-03 19:32:20 -08001/*
2 * Copyright (C) 2007 The Android Open Source 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
17package com.android.camera;
18
Owen Lin101d5282009-04-03 16:20:08 -070019import com.android.camera.gallery.IImage;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080020
Ray Chenbde544f2009-09-30 14:33:15 -070021import android.content.ContentResolver;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080022import android.graphics.Bitmap;
Chih-Chung Changebd325f2009-04-24 18:43:14 +080023import android.os.Handler;
Ray Chenbde544f2009-09-30 14:33:15 -070024import android.provider.MediaStore;
Chih-Chung Changff9922f2009-04-27 19:43:36 +080025import android.util.Log;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080026
Ray Chen254590b2009-04-02 03:21:24 -070027import java.util.ArrayList;
28
Ray Chen993105a2009-04-10 02:11:35 -070029/**
30 * A dedicated decoding thread used by ImageGallery.
31 */
Owen Lin101d5282009-04-03 16:20:08 -070032public class ImageLoader {
Owen Linbbc560b2009-04-17 11:31:27 +080033 @SuppressWarnings("unused")
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080034 private static final String TAG = "ImageLoader";
35
Chih-Chung Chang341ad982009-05-12 14:44:49 +080036 // Queue of work to do in the worker thread. The work is done in order.
Chih-Chung Changebd325f2009-04-24 18:43:14 +080037 private final ArrayList<WorkItem> mQueue = new ArrayList<WorkItem>();
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080038
39 // the worker thread and a done flag so we know when to exit
Chih-Chung Changebd325f2009-04-24 18:43:14 +080040 private boolean mDone;
41 private Thread mDecodeThread;
Ray Chenbde544f2009-09-30 14:33:15 -070042 private ContentResolver mCr;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080043
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080044 public interface LoadedCallback {
45 public void run(Bitmap result);
46 }
47
Chih-Chung Changebd325f2009-04-24 18:43:14 +080048 public void getBitmap(IImage image,
Chih-Chung Changebd325f2009-04-24 18:43:14 +080049 LoadedCallback imageLoadedRunnable,
Chih-Chung Chang341ad982009-05-12 14:44:49 +080050 int tag) {
Chih-Chung Changebd325f2009-04-24 18:43:14 +080051 if (mDecodeThread == null) {
52 start();
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080053 }
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080054 synchronized (mQueue) {
Chih-Chung Chang341ad982009-05-12 14:44:49 +080055 WorkItem w = new WorkItem(image, imageLoadedRunnable, tag);
56 mQueue.add(w);
57 mQueue.notifyAll();
58 }
59 }
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080060
Chih-Chung Chang341ad982009-05-12 14:44:49 +080061 public boolean cancel(final IImage image) {
62 synchronized (mQueue) {
63 int index = findItem(image);
64 if (index >= 0) {
65 mQueue.remove(index);
66 return true;
Chih-Chung Changebd325f2009-04-24 18:43:14 +080067 } else {
Chih-Chung Chang341ad982009-05-12 14:44:49 +080068 return false;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080069 }
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080070 }
71 }
72
Chih-Chung Chang341ad982009-05-12 14:44:49 +080073 // The caller should hold mQueue lock.
74 private int findItem(IImage image) {
75 for (int i = 0; i < mQueue.size(); i++) {
76 if (mQueue.get(i).mImage == image) {
77 return i;
78 }
79 }
80 return -1;
81 }
82
83 // Clear the queue. Returns an array of tags that were in the queue.
84 public int[] clearQueue() {
85 synchronized (mQueue) {
86 int n = mQueue.size();
87 int[] tags = new int[n];
88 for (int i = 0; i < n; i++) {
89 tags[i] = mQueue.get(i).mTag;
90 }
91 mQueue.clear();
92 return tags;
93 }
94 }
95
Chih-Chung Chang33435072009-05-14 12:36:58 +080096 private static class WorkItem {
Ray Chen254590b2009-04-02 03:21:24 -070097 IImage mImage;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -080098 LoadedCallback mOnLoadedRunnable;
Chih-Chung Chang341ad982009-05-12 14:44:49 +080099 int mTag;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800100
Chih-Chung Chang341ad982009-05-12 14:44:49 +0800101 WorkItem(IImage image, LoadedCallback onLoadedRunnable, int tag) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800102 mImage = image;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800103 mOnLoadedRunnable = onLoadedRunnable;
Chih-Chung Chang341ad982009-05-12 14:44:49 +0800104 mTag = tag;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800105 }
106 }
107
Ray Chenbde544f2009-09-30 14:33:15 -0700108 public ImageLoader(ContentResolver cr, Handler handler) {
109 mCr = cr;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800110 start();
111 }
112
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800113 private class WorkerThread implements Runnable {
Chih-Chung Changb21c07c2009-06-30 14:37:57 +0800114
Chih-Chung Chang341ad982009-05-12 14:44:49 +0800115 // Pick off items on the queue, one by one, and compute their bitmap.
116 // Place the resulting bitmap in the cache, then call back by executing
Chih-Chung Chang9c61b2c2009-05-12 12:32:54 +0800117 // the given runnable so things can get updated appropriately.
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800118 public void run() {
Chih-Chung Changf6e6d2b2009-05-14 19:19:17 +0800119 while (true) {
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800120 WorkItem workItem = null;
121 synchronized (mQueue) {
Chih-Chung Changf6e6d2b2009-05-14 19:19:17 +0800122 if (mDone) {
123 break;
124 }
Chih-Chung Chang341ad982009-05-12 14:44:49 +0800125 if (!mQueue.isEmpty()) {
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800126 workItem = mQueue.remove(0);
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800127 } else {
Ray Chen9f1480b2009-08-27 17:59:29 -0700128 try {
129 mQueue.wait();
130 } catch (InterruptedException ex) {
131 // ignore the exception
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800132 }
Ray Chen9f1480b2009-08-27 17:59:29 -0700133 continue;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800134 }
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800135 }
136
137 final Bitmap b = workItem.mImage.miniThumbBitmap();
138
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800139 if (workItem.mOnLoadedRunnable != null) {
Chih-Chung Chang9c61b2c2009-05-12 12:32:54 +0800140 workItem.mOnLoadedRunnable.run(b);
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800141 }
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800142 }
143 }
144 }
145
Chih-Chung Changf6e6d2b2009-05-14 19:19:17 +0800146 private void start() {
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800147 if (mDecodeThread != null) {
148 return;
149 }
150
151 mDone = false;
152 Thread t = new Thread(new WorkerThread());
153 t.setName("image-loader");
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800154 mDecodeThread = t;
155 t.start();
156 }
157
Chih-Chung Changf6e6d2b2009-05-14 19:19:17 +0800158 public void stop() {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800159 synchronized (mQueue) {
Chih-Chung Changf6e6d2b2009-05-14 19:19:17 +0800160 mDone = true;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800161 mQueue.notifyAll();
162 }
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800163 if (mDecodeThread != null) {
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800164 try {
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800165 Thread t = mDecodeThread;
Ray Chen28f35952009-10-05 14:34:24 -0700166 BitmapManager.instance().cancelThreadDecoding(t, mCr);
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800167 t.join();
Chih-Chung Changebd325f2009-04-24 18:43:14 +0800168 mDecodeThread = null;
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800169 } catch (InterruptedException ex) {
170 // so now what?
171 }
172 }
The Android Open Source Projectb64d3452009-03-03 19:32:20 -0800173 }
174}