The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.camera; |
| 18 | |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 19 | import com.android.camera.gallery.IImage; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 20 | |
Ray Chen | bde544f | 2009-09-30 14:33:15 -0700 | [diff] [blame] | 21 | import android.content.ContentResolver; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 22 | import android.graphics.Bitmap; |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 23 | import android.os.Handler; |
Ray Chen | bde544f | 2009-09-30 14:33:15 -0700 | [diff] [blame] | 24 | import android.provider.MediaStore; |
Chih-Chung Chang | ff9922f | 2009-04-27 19:43:36 +0800 | [diff] [blame] | 25 | import android.util.Log; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 26 | |
Ray Chen | 254590b | 2009-04-02 03:21:24 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | |
Ray Chen | 993105a | 2009-04-10 02:11:35 -0700 | [diff] [blame] | 29 | /** |
| 30 | * A dedicated decoding thread used by ImageGallery. |
| 31 | */ |
Owen Lin | 101d528 | 2009-04-03 16:20:08 -0700 | [diff] [blame] | 32 | public class ImageLoader { |
Owen Lin | bbc560b | 2009-04-17 11:31:27 +0800 | [diff] [blame] | 33 | @SuppressWarnings("unused") |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 34 | private static final String TAG = "ImageLoader"; |
| 35 | |
Chih-Chung Chang | 341ad98 | 2009-05-12 14:44:49 +0800 | [diff] [blame] | 36 | // Queue of work to do in the worker thread. The work is done in order. |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 37 | private final ArrayList<WorkItem> mQueue = new ArrayList<WorkItem>(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 38 | |
| 39 | // the worker thread and a done flag so we know when to exit |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 40 | private boolean mDone; |
| 41 | private Thread mDecodeThread; |
Ray Chen | bde544f | 2009-09-30 14:33:15 -0700 | [diff] [blame] | 42 | private ContentResolver mCr; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 43 | |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 44 | public interface LoadedCallback { |
| 45 | public void run(Bitmap result); |
| 46 | } |
| 47 | |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 48 | public void getBitmap(IImage image, |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 49 | LoadedCallback imageLoadedRunnable, |
Chih-Chung Chang | 341ad98 | 2009-05-12 14:44:49 +0800 | [diff] [blame] | 50 | int tag) { |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 51 | if (mDecodeThread == null) { |
| 52 | start(); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 53 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 54 | synchronized (mQueue) { |
Chih-Chung Chang | 341ad98 | 2009-05-12 14:44:49 +0800 | [diff] [blame] | 55 | WorkItem w = new WorkItem(image, imageLoadedRunnable, tag); |
| 56 | mQueue.add(w); |
| 57 | mQueue.notifyAll(); |
| 58 | } |
| 59 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 60 | |
Chih-Chung Chang | 341ad98 | 2009-05-12 14:44:49 +0800 | [diff] [blame] | 61 | 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 Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 67 | } else { |
Chih-Chung Chang | 341ad98 | 2009-05-12 14:44:49 +0800 | [diff] [blame] | 68 | return false; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 69 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Chih-Chung Chang | 341ad98 | 2009-05-12 14:44:49 +0800 | [diff] [blame] | 73 | // 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 Chang | 3343507 | 2009-05-14 12:36:58 +0800 | [diff] [blame] | 96 | private static class WorkItem { |
Ray Chen | 254590b | 2009-04-02 03:21:24 -0700 | [diff] [blame] | 97 | IImage mImage; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 98 | LoadedCallback mOnLoadedRunnable; |
Chih-Chung Chang | 341ad98 | 2009-05-12 14:44:49 +0800 | [diff] [blame] | 99 | int mTag; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 100 | |
Chih-Chung Chang | 341ad98 | 2009-05-12 14:44:49 +0800 | [diff] [blame] | 101 | WorkItem(IImage image, LoadedCallback onLoadedRunnable, int tag) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 102 | mImage = image; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 103 | mOnLoadedRunnable = onLoadedRunnable; |
Chih-Chung Chang | 341ad98 | 2009-05-12 14:44:49 +0800 | [diff] [blame] | 104 | mTag = tag; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Ray Chen | bde544f | 2009-09-30 14:33:15 -0700 | [diff] [blame] | 108 | public ImageLoader(ContentResolver cr, Handler handler) { |
| 109 | mCr = cr; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 110 | start(); |
| 111 | } |
| 112 | |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 113 | private class WorkerThread implements Runnable { |
Chih-Chung Chang | b21c07c | 2009-06-30 14:37:57 +0800 | [diff] [blame] | 114 | |
Chih-Chung Chang | 341ad98 | 2009-05-12 14:44:49 +0800 | [diff] [blame] | 115 | // 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 Chang | 9c61b2c | 2009-05-12 12:32:54 +0800 | [diff] [blame] | 117 | // the given runnable so things can get updated appropriately. |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 118 | public void run() { |
Chih-Chung Chang | f6e6d2b | 2009-05-14 19:19:17 +0800 | [diff] [blame] | 119 | while (true) { |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 120 | WorkItem workItem = null; |
| 121 | synchronized (mQueue) { |
Chih-Chung Chang | f6e6d2b | 2009-05-14 19:19:17 +0800 | [diff] [blame] | 122 | if (mDone) { |
| 123 | break; |
| 124 | } |
Chih-Chung Chang | 341ad98 | 2009-05-12 14:44:49 +0800 | [diff] [blame] | 125 | if (!mQueue.isEmpty()) { |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 126 | workItem = mQueue.remove(0); |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 127 | } else { |
Ray Chen | 9f1480b | 2009-08-27 17:59:29 -0700 | [diff] [blame] | 128 | try { |
| 129 | mQueue.wait(); |
| 130 | } catch (InterruptedException ex) { |
| 131 | // ignore the exception |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 132 | } |
Ray Chen | 9f1480b | 2009-08-27 17:59:29 -0700 | [diff] [blame] | 133 | continue; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 134 | } |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | final Bitmap b = workItem.mImage.miniThumbBitmap(); |
| 138 | |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 139 | if (workItem.mOnLoadedRunnable != null) { |
Chih-Chung Chang | 9c61b2c | 2009-05-12 12:32:54 +0800 | [diff] [blame] | 140 | workItem.mOnLoadedRunnable.run(b); |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 141 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
Chih-Chung Chang | f6e6d2b | 2009-05-14 19:19:17 +0800 | [diff] [blame] | 146 | private void start() { |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 147 | if (mDecodeThread != null) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | mDone = false; |
| 152 | Thread t = new Thread(new WorkerThread()); |
| 153 | t.setName("image-loader"); |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 154 | mDecodeThread = t; |
| 155 | t.start(); |
| 156 | } |
| 157 | |
Chih-Chung Chang | f6e6d2b | 2009-05-14 19:19:17 +0800 | [diff] [blame] | 158 | public void stop() { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 159 | synchronized (mQueue) { |
Chih-Chung Chang | f6e6d2b | 2009-05-14 19:19:17 +0800 | [diff] [blame] | 160 | mDone = true; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 161 | mQueue.notifyAll(); |
| 162 | } |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 163 | if (mDecodeThread != null) { |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 164 | try { |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 165 | Thread t = mDecodeThread; |
Ray Chen | 28f3595 | 2009-10-05 14:34:24 -0700 | [diff] [blame] | 166 | BitmapManager.instance().cancelThreadDecoding(t, mCr); |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 167 | t.join(); |
Chih-Chung Chang | ebd325f | 2009-04-24 18:43:14 +0800 | [diff] [blame] | 168 | mDecodeThread = null; |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 169 | } catch (InterruptedException ex) { |
| 170 | // so now what? |
| 171 | } |
| 172 | } |
The Android Open Source Project | b64d345 | 2009-03-03 19:32:20 -0800 | [diff] [blame] | 173 | } |
| 174 | } |