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