Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Dmitri Plotnikov | 022b62d | 2011-01-28 12:16:07 -0800 | [diff] [blame^] | 17 | package com.android.contacts; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 18 | |
| 19 | import com.google.android.collect.Lists; |
| 20 | |
| 21 | import android.content.ContentResolver; |
| 22 | import android.content.Context; |
| 23 | import android.database.Cursor; |
| 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapFactory; |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 27 | import android.os.Handler; |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 28 | import android.os.Handler.Callback; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 29 | import android.os.HandlerThread; |
| 30 | import android.os.Message; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 31 | import android.provider.ContactsContract.Contacts.Photo; |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 32 | import android.provider.ContactsContract.Data; |
| 33 | import android.util.Log; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 34 | import android.widget.ImageView; |
| 35 | |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 36 | import java.io.ByteArrayOutputStream; |
| 37 | import java.io.InputStream; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 38 | import java.lang.ref.SoftReference; |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.Iterator; |
| 41 | import java.util.concurrent.ConcurrentHashMap; |
| 42 | |
| 43 | /** |
| 44 | * Asynchronously loads contact photos and maintains cache of photos. The class is |
| 45 | * mostly single-threaded. The only two methods accessed by the loader thread are |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 46 | * {@link #cacheBitmap} and {@link #obtainPhotoIdsAndUrisToLoad}. Those methods access concurrent |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 47 | * hash maps shared with the main thread. |
| 48 | */ |
| 49 | public class ContactPhotoLoader implements Callback { |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 50 | private static final String TAG = "ContactPhotoLoader"; |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 51 | private static final String LOADER_THREAD_NAME = "ContactPhotoLoader"; |
| 52 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Type of message sent by the UI thread to itself to indicate that some photos |
| 55 | * need to be loaded. |
| 56 | */ |
| 57 | private static final int MESSAGE_REQUEST_LOADING = 1; |
| 58 | |
| 59 | /** |
| 60 | * Type of message sent by the loader thread to indicate that some photos have |
| 61 | * been loaded. |
| 62 | */ |
| 63 | private static final int MESSAGE_PHOTOS_LOADED = 2; |
| 64 | |
| 65 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 66 | |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 67 | private final String[] COLUMNS = new String[] { Photo._ID, Photo.PHOTO }; |
| 68 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 69 | /** |
| 70 | * The resource ID of the image to be used when the photo is unavailable or being |
| 71 | * loaded. |
| 72 | */ |
| 73 | private final int mDefaultResourceId; |
| 74 | |
| 75 | /** |
| 76 | * Maintains the state of a particular photo. |
| 77 | */ |
| 78 | private static class BitmapHolder { |
| 79 | private static final int NEEDED = 0; |
| 80 | private static final int LOADING = 1; |
| 81 | private static final int LOADED = 2; |
Dmitri Plotnikov | 437babb | 2010-11-23 18:29:50 -0800 | [diff] [blame] | 82 | private static final int LOADED_NEEDS_RELOAD = 3; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 83 | |
| 84 | int state; |
Dmitri Plotnikov | 37dc7cf | 2010-12-17 19:25:08 -0800 | [diff] [blame] | 85 | Bitmap bitmap; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 86 | SoftReference<Bitmap> bitmapRef; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * A soft cache for photos. |
| 91 | */ |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 92 | private final ConcurrentHashMap<Object, BitmapHolder> mBitmapCache = |
| 93 | new ConcurrentHashMap<Object, BitmapHolder>(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * A map from ImageView to the corresponding photo ID. Please note that this |
| 97 | * photo ID may change before the photo loading request is started. |
| 98 | */ |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 99 | private final ConcurrentHashMap<ImageView, Object> mPendingRequests = |
| 100 | new ConcurrentHashMap<ImageView, Object>(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Handler for messages sent to the UI thread. |
| 104 | */ |
| 105 | private final Handler mMainThreadHandler = new Handler(this); |
| 106 | |
| 107 | /** |
| 108 | * Thread responsible for loading photos from the database. Created upon |
| 109 | * the first request. |
| 110 | */ |
| 111 | private LoaderThread mLoaderThread; |
| 112 | |
| 113 | /** |
| 114 | * A gate to make sure we only send one instance of MESSAGE_PHOTOS_NEEDED at a time. |
| 115 | */ |
| 116 | private boolean mLoadingRequested; |
| 117 | |
| 118 | /** |
| 119 | * Flag indicating if the image loading is paused. |
| 120 | */ |
| 121 | private boolean mPaused; |
| 122 | |
| 123 | private final Context mContext; |
| 124 | |
| 125 | /** |
| 126 | * Constructor. |
| 127 | * |
| 128 | * @param context content context |
| 129 | * @param defaultResourceId the image resource ID to be used when there is |
| 130 | * no photo for a contact |
| 131 | */ |
| 132 | public ContactPhotoLoader(Context context, int defaultResourceId) { |
| 133 | mDefaultResourceId = defaultResourceId; |
| 134 | mContext = context; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Load photo into the supplied image view. If the photo is already cached, |
| 139 | * it is displayed immediately. Otherwise a request is sent to load the photo |
| 140 | * from the database. |
| 141 | */ |
| 142 | public void loadPhoto(ImageView view, long photoId) { |
| 143 | if (photoId == 0) { |
| 144 | // No photo is needed |
| 145 | view.setImageResource(mDefaultResourceId); |
| 146 | mPendingRequests.remove(view); |
| 147 | } else { |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 148 | loadPhotoByIdOrUri(view, photoId); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Load photo into the supplied image view. If the photo is already cached, |
| 154 | * it is displayed immediately. Otherwise a request is sent to load the photo |
| 155 | * from the location specified by the URI. |
| 156 | */ |
| 157 | public void loadPhoto(ImageView view, Uri photoUri) { |
| 158 | if (photoUri == null) { |
| 159 | // No photo is needed |
| 160 | view.setImageResource(mDefaultResourceId); |
| 161 | mPendingRequests.remove(view); |
| 162 | } else { |
| 163 | loadPhotoByIdOrUri(view, photoUri); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | private void loadPhotoByIdOrUri(ImageView view, Object key) { |
| 168 | boolean loaded = loadCachedPhoto(view, key); |
| 169 | if (loaded) { |
| 170 | mPendingRequests.remove(view); |
| 171 | } else { |
| 172 | mPendingRequests.put(view, key); |
| 173 | if (!mPaused) { |
| 174 | // Send a request to start loading photos |
| 175 | requestLoading(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
Dmitri Plotnikov | 718a250 | 2010-11-23 17:56:28 -0800 | [diff] [blame] | 181 | * Mark all cached photos for reloading. We can continue using cache but should |
| 182 | * also make sure the photos haven't changed in the background and notify the views |
| 183 | * if so. |
| 184 | */ |
| 185 | public void refreshCache() { |
| 186 | for (BitmapHolder holder : mBitmapCache.values()) { |
Dmitri Plotnikov | 437babb | 2010-11-23 18:29:50 -0800 | [diff] [blame] | 187 | if (holder.state == BitmapHolder.LOADED) { |
| 188 | holder.state = BitmapHolder.LOADED_NEEDS_RELOAD; |
Dmitri Plotnikov | 718a250 | 2010-11-23 17:56:28 -0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 194 | * Checks if the photo is present in cache. If so, sets the photo on the view, |
| 195 | * otherwise sets the state of the photo to {@link BitmapHolder#NEEDED} and |
| 196 | * temporarily set the image to the default resource ID. |
| 197 | */ |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 198 | private boolean loadCachedPhoto(ImageView view, Object key) { |
| 199 | BitmapHolder holder = mBitmapCache.get(key); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 200 | if (holder == null) { |
| 201 | holder = new BitmapHolder(); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 202 | mBitmapCache.put(key, holder); |
Dmitri Plotnikov | 718a250 | 2010-11-23 17:56:28 -0800 | [diff] [blame] | 203 | } else { |
Dmitri Plotnikov | 437babb | 2010-11-23 18:29:50 -0800 | [diff] [blame] | 204 | boolean loaded = (holder.state == BitmapHolder.LOADED); |
| 205 | boolean loadedNeedsReload = (holder.state == BitmapHolder.LOADED_NEEDS_RELOAD); |
| 206 | if (loadedNeedsReload) { |
Dmitri Plotnikov | 718a250 | 2010-11-23 17:56:28 -0800 | [diff] [blame] | 207 | holder.state = BitmapHolder.NEEDED; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Dmitri Plotnikov | 99f9d53 | 2010-12-17 14:58:51 -0800 | [diff] [blame] | 210 | // Null bitmap reference means that database contains no bytes for the photo |
| 211 | if ((loaded || loadedNeedsReload) && holder.bitmapRef == null) { |
| 212 | view.setImageResource(mDefaultResourceId); |
| 213 | return loaded; |
| 214 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 215 | |
Dmitri Plotnikov | 99f9d53 | 2010-12-17 14:58:51 -0800 | [diff] [blame] | 216 | if (holder.bitmapRef != null) { |
Dmitri Plotnikov | 718a250 | 2010-11-23 17:56:28 -0800 | [diff] [blame] | 217 | Bitmap bitmap = holder.bitmapRef.get(); |
| 218 | if (bitmap != null) { |
| 219 | view.setImageBitmap(bitmap); |
Dmitri Plotnikov | 437babb | 2010-11-23 18:29:50 -0800 | [diff] [blame] | 220 | return loaded; |
Dmitri Plotnikov | 718a250 | 2010-11-23 17:56:28 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Null bitmap means that the soft reference was released by the GC |
| 224 | // and we need to reload the photo. |
| 225 | holder.bitmapRef = null; |
| 226 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // The bitmap has not been loaded - should display the placeholder image. |
| 230 | view.setImageResource(mDefaultResourceId); |
| 231 | holder.state = BitmapHolder.NEEDED; |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Stops loading images, kills the image loader thread and clears all caches. |
| 237 | */ |
| 238 | public void stop() { |
| 239 | pause(); |
| 240 | |
| 241 | if (mLoaderThread != null) { |
| 242 | mLoaderThread.quit(); |
| 243 | mLoaderThread = null; |
| 244 | } |
| 245 | |
| 246 | mPendingRequests.clear(); |
| 247 | mBitmapCache.clear(); |
| 248 | } |
| 249 | |
Dmitri Plotnikov | b369c49 | 2010-03-24 18:11:24 -0700 | [diff] [blame] | 250 | public void clear() { |
| 251 | mPendingRequests.clear(); |
| 252 | mBitmapCache.clear(); |
| 253 | } |
| 254 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 255 | /** |
| 256 | * Temporarily stops loading photos from the database. |
| 257 | */ |
| 258 | public void pause() { |
| 259 | mPaused = true; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Resumes loading photos from the database. |
| 264 | */ |
| 265 | public void resume() { |
| 266 | mPaused = false; |
| 267 | if (!mPendingRequests.isEmpty()) { |
| 268 | requestLoading(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Sends a message to this thread itself to start loading images. If the current |
| 274 | * view contains multiple image views, all of those image views will get a chance |
| 275 | * to request their respective photos before any of those requests are executed. |
| 276 | * This allows us to load images in bulk. |
| 277 | */ |
| 278 | private void requestLoading() { |
| 279 | if (!mLoadingRequested) { |
| 280 | mLoadingRequested = true; |
| 281 | mMainThreadHandler.sendEmptyMessage(MESSAGE_REQUEST_LOADING); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Processes requests on the main thread. |
| 287 | */ |
| 288 | public boolean handleMessage(Message msg) { |
| 289 | switch (msg.what) { |
| 290 | case MESSAGE_REQUEST_LOADING: { |
| 291 | mLoadingRequested = false; |
| 292 | if (!mPaused) { |
| 293 | if (mLoaderThread == null) { |
| 294 | mLoaderThread = new LoaderThread(mContext.getContentResolver()); |
| 295 | mLoaderThread.start(); |
| 296 | } |
| 297 | |
| 298 | mLoaderThread.requestLoading(); |
| 299 | } |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | case MESSAGE_PHOTOS_LOADED: { |
| 304 | if (!mPaused) { |
| 305 | processLoadedImages(); |
| 306 | } |
| 307 | return true; |
| 308 | } |
| 309 | } |
| 310 | return false; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Goes over pending loading requests and displays loaded photos. If some of the |
| 315 | * photos still haven't been loaded, sends another request for image loading. |
| 316 | */ |
| 317 | private void processLoadedImages() { |
| 318 | Iterator<ImageView> iterator = mPendingRequests.keySet().iterator(); |
| 319 | while (iterator.hasNext()) { |
| 320 | ImageView view = iterator.next(); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 321 | Object key = mPendingRequests.get(view); |
| 322 | boolean loaded = loadCachedPhoto(view, key); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 323 | if (loaded) { |
| 324 | iterator.remove(); |
| 325 | } |
| 326 | } |
| 327 | |
Dmitri Plotnikov | 37dc7cf | 2010-12-17 19:25:08 -0800 | [diff] [blame] | 328 | softenCache(); |
| 329 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 330 | if (!mPendingRequests.isEmpty()) { |
| 331 | requestLoading(); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /** |
Dmitri Plotnikov | 37dc7cf | 2010-12-17 19:25:08 -0800 | [diff] [blame] | 336 | * Removes strong references to loaded bitmaps to allow them to be garbage collected |
| 337 | * if needed. |
| 338 | */ |
| 339 | private void softenCache() { |
| 340 | for (BitmapHolder holder : mBitmapCache.values()) { |
| 341 | holder.bitmap = null; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /** |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 346 | * Stores the supplied bitmap in cache. |
| 347 | */ |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 348 | private void cacheBitmap(Object key, byte[] bytes) { |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 349 | if (mPaused) { |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | BitmapHolder holder = new BitmapHolder(); |
| 354 | holder.state = BitmapHolder.LOADED; |
| 355 | if (bytes != null) { |
| 356 | try { |
| 357 | Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null); |
Dmitri Plotnikov | 37dc7cf | 2010-12-17 19:25:08 -0800 | [diff] [blame] | 358 | holder.bitmap = bitmap; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 359 | holder.bitmapRef = new SoftReference<Bitmap>(bitmap); |
| 360 | } catch (OutOfMemoryError e) { |
| 361 | // Do nothing - the photo will appear to be missing |
| 362 | } |
| 363 | } |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 364 | mBitmapCache.put(key, holder); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Populates an array of photo IDs that need to be loaded. |
| 369 | */ |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 370 | private void obtainPhotoIdsAndUrisToLoad(ArrayList<Long> photoIds, |
| 371 | ArrayList<String> photoIdsAsStrings, ArrayList<Uri> uris) { |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 372 | photoIds.clear(); |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 373 | photoIdsAsStrings.clear(); |
Dmitri Plotnikov | 0f7462c | 2010-10-20 14:41:18 -0700 | [diff] [blame] | 374 | uris.clear(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 375 | |
| 376 | /* |
| 377 | * Since the call is made from the loader thread, the map could be |
| 378 | * changing during the iteration. That's not really a problem: |
| 379 | * ConcurrentHashMap will allow those changes to happen without throwing |
| 380 | * exceptions. Since we may miss some requests in the situation of |
| 381 | * concurrent change, we will need to check the map again once loading |
| 382 | * is complete. |
| 383 | */ |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 384 | Iterator<Object> iterator = mPendingRequests.values().iterator(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 385 | while (iterator.hasNext()) { |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 386 | Object key = iterator.next(); |
| 387 | BitmapHolder holder = mBitmapCache.get(key); |
Dmitri Plotnikov | 92dfa11 | 2010-04-02 11:32:50 -0700 | [diff] [blame] | 388 | if (holder != null && holder.state == BitmapHolder.NEEDED) { |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 389 | // Assuming atomic behavior |
| 390 | holder.state = BitmapHolder.LOADING; |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 391 | if (key instanceof Long) { |
| 392 | photoIds.add((Long)key); |
| 393 | photoIdsAsStrings.add(key.toString()); |
| 394 | } else { |
| 395 | uris.add((Uri)key); |
| 396 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * The thread that performs loading of photos from the database. |
| 403 | */ |
| 404 | private class LoaderThread extends HandlerThread implements Callback { |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 405 | private static final int BUFFER_SIZE = 1024*16; |
| 406 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 407 | private final ContentResolver mResolver; |
| 408 | private final StringBuilder mStringBuilder = new StringBuilder(); |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 409 | private final ArrayList<Long> mPhotoIds = Lists.newArrayList(); |
| 410 | private final ArrayList<String> mPhotoIdsAsStrings = Lists.newArrayList(); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 411 | private final ArrayList<Uri> mPhotoUris = Lists.newArrayList(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 412 | private Handler mLoaderThreadHandler; |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 413 | private byte mBuffer[]; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 414 | |
| 415 | public LoaderThread(ContentResolver resolver) { |
| 416 | super(LOADER_THREAD_NAME); |
| 417 | mResolver = resolver; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * Sends a message to this thread to load requested photos. |
| 422 | */ |
| 423 | public void requestLoading() { |
| 424 | if (mLoaderThreadHandler == null) { |
| 425 | mLoaderThreadHandler = new Handler(getLooper(), this); |
| 426 | } |
| 427 | mLoaderThreadHandler.sendEmptyMessage(0); |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Receives the above message, loads photos and then sends a message |
| 432 | * to the main thread to process them. |
| 433 | */ |
| 434 | public boolean handleMessage(Message msg) { |
| 435 | loadPhotosFromDatabase(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 436 | return true; |
| 437 | } |
| 438 | |
| 439 | private void loadPhotosFromDatabase() { |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 440 | obtainPhotoIdsAndUrisToLoad(mPhotoIds, mPhotoIdsAsStrings, mPhotoUris); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 441 | |
| 442 | int count = mPhotoIds.size(); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 443 | if (count != 0) { |
| 444 | mStringBuilder.setLength(0); |
| 445 | mStringBuilder.append(Photo._ID + " IN("); |
| 446 | for (int i = 0; i < count; i++) { |
| 447 | if (i != 0) { |
| 448 | mStringBuilder.append(','); |
| 449 | } |
| 450 | mStringBuilder.append('?'); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 451 | } |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 452 | mStringBuilder.append(')'); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 453 | |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 454 | Cursor cursor = null; |
| 455 | try { |
| 456 | cursor = mResolver.query(Data.CONTENT_URI, |
| 457 | COLUMNS, |
| 458 | mStringBuilder.toString(), |
| 459 | mPhotoIdsAsStrings.toArray(EMPTY_STRING_ARRAY), |
| 460 | null); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 461 | |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 462 | if (cursor != null) { |
| 463 | while (cursor.moveToNext()) { |
| 464 | Long id = cursor.getLong(0); |
| 465 | byte[] bytes = cursor.getBlob(1); |
| 466 | cacheBitmap(id, bytes); |
| 467 | mPhotoIds.remove(id); |
| 468 | } |
| 469 | } |
| 470 | } finally { |
| 471 | if (cursor != null) { |
| 472 | cursor.close(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 473 | } |
| 474 | } |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 475 | |
| 476 | // Remaining photos were not found in the database - mark the cache accordingly. |
| 477 | count = mPhotoIds.size(); |
| 478 | for (int i = 0; i < count; i++) { |
| 479 | cacheBitmap(mPhotoIds.get(i), null); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 480 | } |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 481 | mMainThreadHandler.sendEmptyMessage(MESSAGE_PHOTOS_LOADED); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 482 | } |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 483 | |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 484 | count = mPhotoUris.size(); |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 485 | for (int i = 0; i < count; i++) { |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 486 | Uri uri = mPhotoUris.get(i); |
| 487 | if (mBuffer == null) { |
| 488 | mBuffer = new byte[BUFFER_SIZE]; |
| 489 | } |
| 490 | try { |
| 491 | InputStream is = mResolver.openInputStream(uri); |
| 492 | if (is != null) { |
| 493 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 494 | try { |
| 495 | int size; |
| 496 | while ((size = is.read(mBuffer)) != -1) { |
| 497 | baos.write(mBuffer, 0, size); |
| 498 | } |
| 499 | } finally { |
| 500 | is.close(); |
| 501 | } |
| 502 | cacheBitmap(uri, baos.toByteArray()); |
| 503 | mMainThreadHandler.sendEmptyMessage(MESSAGE_PHOTOS_LOADED); |
Dmitri Plotnikov | 0f7462c | 2010-10-20 14:41:18 -0700 | [diff] [blame] | 504 | } else { |
| 505 | Log.v(TAG, "Cannot load photo " + uri); |
| 506 | cacheBitmap(uri, null); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 507 | } |
| 508 | } catch (Exception ex) { |
| 509 | Log.v(TAG, "Cannot load photo " + uri, ex); |
| 510 | cacheBitmap(uri, null); |
| 511 | } |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 512 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | } |