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; |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 20 | import com.android.contacts.util.MemoryUtils; |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 21 | import com.android.contacts.util.UriUtils; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 22 | import com.google.android.collect.Lists; |
Flavio Lerda | d33b18c | 2011-07-17 22:03:15 +0100 | [diff] [blame] | 23 | import com.google.android.collect.Sets; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 24 | |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 25 | import android.content.ComponentCallbacks2; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 26 | import android.content.ContentResolver; |
Dave Santoro | 84cac44 | 2011-08-24 15:23:10 -0700 | [diff] [blame] | 27 | import android.content.ContentUris; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 28 | import android.content.Context; |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 29 | import android.content.res.Configuration; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 30 | import android.database.Cursor; |
| 31 | import android.graphics.Bitmap; |
| 32 | import android.graphics.BitmapFactory; |
Daniel Lehmann | 7a46cde | 2012-02-01 17:53:44 -0800 | [diff] [blame] | 33 | import android.graphics.drawable.BitmapDrawable; |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 34 | import android.graphics.drawable.ColorDrawable; |
| 35 | import android.graphics.drawable.Drawable; |
Daniel Lehmann | 7a46cde | 2012-02-01 17:53:44 -0800 | [diff] [blame] | 36 | import android.graphics.drawable.TransitionDrawable; |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 37 | import android.net.Uri; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 38 | import android.os.Handler; |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 39 | import android.os.Handler.Callback; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 40 | import android.os.HandlerThread; |
| 41 | import android.os.Message; |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 42 | import android.provider.ContactsContract; |
| 43 | import android.provider.ContactsContract.Contacts; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 44 | import android.provider.ContactsContract.Contacts.Photo; |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 45 | import android.provider.ContactsContract.Data; |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 46 | import android.provider.ContactsContract.Directory; |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 47 | import android.text.TextUtils; |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 48 | import android.util.Log; |
Jesse Wilson | fb231aa | 2011-02-07 15:15:56 -0800 | [diff] [blame] | 49 | import android.util.LruCache; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 50 | import android.widget.ImageView; |
| 51 | |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 52 | import java.io.ByteArrayOutputStream; |
| 53 | import java.io.InputStream; |
Makoto Onuki | 173f281 | 2011-09-06 14:49:27 -0700 | [diff] [blame] | 54 | import java.lang.ref.Reference; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 55 | import java.lang.ref.SoftReference; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 56 | import java.util.Iterator; |
Flavio Lerda | d33b18c | 2011-07-17 22:03:15 +0100 | [diff] [blame] | 57 | import java.util.List; |
| 58 | import java.util.Set; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 59 | import java.util.concurrent.ConcurrentHashMap; |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 60 | import java.util.concurrent.atomic.AtomicInteger; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 61 | |
| 62 | /** |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 63 | * Asynchronously loads contact photos and maintains a cache of photos. |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 64 | */ |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 65 | public abstract class ContactPhotoManager implements ComponentCallbacks2 { |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 66 | static final String TAG = "ContactPhotoManager"; |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 67 | static final boolean DEBUG = false; // Don't submit with true |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 68 | |
| 69 | public static final String CONTACT_PHOTO_SERVICE = "contactPhotos"; |
| 70 | |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 71 | public static int getDefaultAvatarResId(boolean hires, boolean darkTheme) { |
| 72 | if (hires && darkTheme) return R.drawable.ic_contact_picture_180_holo_dark; |
| 73 | if (hires) return R.drawable.ic_contact_picture_180_holo_light; |
| 74 | if (darkTheme) return R.drawable.ic_contact_picture_holo_dark; |
| 75 | return R.drawable.ic_contact_picture_holo_light; |
| 76 | } |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 77 | |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 78 | public static abstract class DefaultImageProvider { |
| 79 | public abstract void applyDefaultImage(ImageView view, boolean hires, boolean darkTheme); |
| 80 | } |
| 81 | |
| 82 | private static class AvatarDefaultImageProvider extends DefaultImageProvider { |
| 83 | @Override |
| 84 | public void applyDefaultImage(ImageView view, boolean hires, boolean darkTheme) { |
| 85 | view.setImageResource(getDefaultAvatarResId(hires, darkTheme)); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private static class BlankDefaultImageProvider extends DefaultImageProvider { |
| 90 | private static Drawable sDrawable; |
| 91 | |
| 92 | @Override |
| 93 | public void applyDefaultImage(ImageView view, boolean hires, boolean darkTheme) { |
| 94 | if (sDrawable == null) { |
| 95 | Context context = view.getContext(); |
| 96 | sDrawable = new ColorDrawable(context.getResources().getColor( |
| 97 | R.color.image_placeholder)); |
| 98 | } |
| 99 | view.setImageDrawable(sDrawable); |
| 100 | } |
| 101 | } |
| 102 | |
Daniel Lehmann | 2d73de0 | 2012-02-21 19:51:38 -0800 | [diff] [blame] | 103 | public static final DefaultImageProvider DEFAULT_AVATAR = new AvatarDefaultImageProvider(); |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 104 | |
| 105 | public static final DefaultImageProvider DEFAULT_BLANK = new BlankDefaultImageProvider(); |
| 106 | |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 107 | /** |
| 108 | * Requests the singleton instance of {@link AccountTypeManager} with data bound from |
| 109 | * the available authenticators. This method can safely be called from the UI thread. |
| 110 | */ |
| 111 | public static ContactPhotoManager getInstance(Context context) { |
Flavio Lerda | 82e4a56 | 2011-07-08 17:05:31 +0100 | [diff] [blame] | 112 | Context applicationContext = context.getApplicationContext(); |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 113 | ContactPhotoManager service = |
Flavio Lerda | 82e4a56 | 2011-07-08 17:05:31 +0100 | [diff] [blame] | 114 | (ContactPhotoManager) applicationContext.getSystemService(CONTACT_PHOTO_SERVICE); |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 115 | if (service == null) { |
Flavio Lerda | 82e4a56 | 2011-07-08 17:05:31 +0100 | [diff] [blame] | 116 | service = createContactPhotoManager(applicationContext); |
| 117 | Log.e(TAG, "No contact photo service in context: " + applicationContext); |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 118 | } |
| 119 | return service; |
| 120 | } |
| 121 | |
| 122 | public static synchronized ContactPhotoManager createContactPhotoManager(Context context) { |
| 123 | return new ContactPhotoManagerImpl(context); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Load photo into the supplied image view. If the photo is already cached, |
| 128 | * it is displayed immediately. Otherwise a request is sent to load the photo |
| 129 | * from the database. |
| 130 | */ |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 131 | public abstract void loadPhoto(ImageView view, long photoId, boolean hires, boolean darkTheme, |
| 132 | DefaultImageProvider defaultProvider); |
| 133 | |
| 134 | /** |
| 135 | * Calls {@link #loadPhoto(ImageView, long, boolean, boolean, DefaultImageProvider)} with |
Daniel Lehmann | 2d73de0 | 2012-02-21 19:51:38 -0800 | [diff] [blame] | 136 | * {@link #DEFAULT_AVATAR}. |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 137 | */ |
| 138 | public final void loadPhoto(ImageView view, long photoId, boolean hires, boolean darkTheme) { |
Daniel Lehmann | 2d73de0 | 2012-02-21 19:51:38 -0800 | [diff] [blame] | 139 | loadPhoto(view, photoId, hires, darkTheme, DEFAULT_AVATAR); |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 140 | } |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Load photo into the supplied image view. If the photo is already cached, |
| 144 | * it is displayed immediately. Otherwise a request is sent to load the photo |
| 145 | * from the location specified by the URI. |
| 146 | */ |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 147 | public abstract void loadPhoto(ImageView view, Uri photoUri, boolean hires, boolean darkTheme, |
| 148 | DefaultImageProvider defaultProvider); |
| 149 | |
| 150 | /** |
| 151 | * Calls {@link #loadPhoto(ImageView, Uri, boolean, boolean, DefaultImageProvider)} with |
Daniel Lehmann | 2d73de0 | 2012-02-21 19:51:38 -0800 | [diff] [blame] | 152 | * {@link #DEFAULT_AVATAR}. |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 153 | */ |
| 154 | public final void loadPhoto(ImageView view, Uri photoUri, boolean hires, boolean darkTheme) { |
Daniel Lehmann | 2d73de0 | 2012-02-21 19:51:38 -0800 | [diff] [blame] | 155 | loadPhoto(view, photoUri, hires, darkTheme, DEFAULT_AVATAR); |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 156 | } |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 157 | |
| 158 | /** |
Daisuke Miyakawa | f5be9ba | 2011-08-05 09:17:07 -0700 | [diff] [blame] | 159 | * Remove photo from the supplied image view. This also cancels current pending load request |
| 160 | * inside this photo manager. |
| 161 | */ |
| 162 | public abstract void removePhoto(ImageView view); |
| 163 | |
| 164 | /** |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 165 | * Temporarily stops loading photos from the database. |
| 166 | */ |
| 167 | public abstract void pause(); |
| 168 | |
| 169 | /** |
| 170 | * Resumes loading photos from the database. |
| 171 | */ |
| 172 | public abstract void resume(); |
| 173 | |
| 174 | /** |
| 175 | * Marks all cached photos for reloading. We can continue using cache but should |
| 176 | * also make sure the photos haven't changed in the background and notify the views |
| 177 | * if so. |
| 178 | */ |
| 179 | public abstract void refreshCache(); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 180 | |
| 181 | /** |
Dave Santoro | 14d2083 | 2011-12-02 18:14:52 -0800 | [diff] [blame] | 182 | * Stores the given bitmap directly in the LRU bitmap cache. |
| 183 | * @param photoUri The URI of the photo (for future requests). |
| 184 | * @param bitmap The bitmap. |
| 185 | * @param photoBytes The bytes that were parsed to create the bitmap. |
| 186 | */ |
| 187 | public abstract void cacheBitmap(Uri photoUri, Bitmap bitmap, byte[] photoBytes); |
| 188 | |
| 189 | /** |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 190 | * Initiates a background process that over time will fill up cache with |
| 191 | * preload photos. |
| 192 | */ |
| 193 | public abstract void preloadPhotosInBackground(); |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 194 | |
| 195 | // ComponentCallbacks2 |
| 196 | @Override |
| 197 | public void onConfigurationChanged(Configuration newConfig) { |
| 198 | } |
| 199 | |
| 200 | // ComponentCallbacks2 |
| 201 | @Override |
| 202 | public void onLowMemory() { |
| 203 | } |
| 204 | |
| 205 | // ComponentCallbacks2 |
| 206 | @Override |
| 207 | public void onTrimMemory(int level) { |
| 208 | } |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | class ContactPhotoManagerImpl extends ContactPhotoManager implements Callback { |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 212 | private static final String LOADER_THREAD_NAME = "ContactPhotoLoader"; |
| 213 | |
Daniel Lehmann | 7a46cde | 2012-02-01 17:53:44 -0800 | [diff] [blame] | 214 | private static final int FADE_TRANSITION_DURATION = 200; |
| 215 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 216 | /** |
| 217 | * Type of message sent by the UI thread to itself to indicate that some photos |
| 218 | * need to be loaded. |
| 219 | */ |
| 220 | private static final int MESSAGE_REQUEST_LOADING = 1; |
| 221 | |
| 222 | /** |
| 223 | * Type of message sent by the loader thread to indicate that some photos have |
| 224 | * been loaded. |
| 225 | */ |
| 226 | private static final int MESSAGE_PHOTOS_LOADED = 2; |
| 227 | |
| 228 | private static final String[] EMPTY_STRING_ARRAY = new String[0]; |
| 229 | |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 230 | private static final String[] COLUMNS = new String[] { Photo._ID, Photo.PHOTO }; |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 231 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 232 | /** |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 233 | * Maintains the state of a particular photo. |
| 234 | */ |
| 235 | private static class BitmapHolder { |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 236 | final byte[] bytes; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 237 | |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 238 | volatile boolean fresh; |
Dmitri Plotnikov | 37dc7cf | 2010-12-17 19:25:08 -0800 | [diff] [blame] | 239 | Bitmap bitmap; |
Makoto Onuki | 173f281 | 2011-09-06 14:49:27 -0700 | [diff] [blame] | 240 | Reference<Bitmap> bitmapRef; |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 241 | |
| 242 | public BitmapHolder(byte[] bytes) { |
| 243 | this.bytes = bytes; |
| 244 | this.fresh = true; |
| 245 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 248 | private final Context mContext; |
| 249 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 250 | /** |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 251 | * An LRU cache for bitmap holders. The cache contains bytes for photos just |
Jesse Wilson | fb231aa | 2011-02-07 15:15:56 -0800 | [diff] [blame] | 252 | * as they come from the database. Each holder has a soft reference to the |
| 253 | * actual bitmap. |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 254 | */ |
Jesse Wilson | fb231aa | 2011-02-07 15:15:56 -0800 | [diff] [blame] | 255 | private final LruCache<Object, BitmapHolder> mBitmapHolderCache; |
| 256 | |
| 257 | /** |
| 258 | * Cache size threshold at which bitmaps will not be preloaded. |
| 259 | */ |
| 260 | private final int mBitmapHolderCacheRedZoneBytes; |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 261 | |
| 262 | /** |
| 263 | * Level 2 LRU cache for bitmaps. This is a smaller cache that holds |
| 264 | * the most recently used bitmaps to save time on decoding |
| 265 | * them from bytes (the bytes are stored in {@link #mBitmapHolderCache}. |
| 266 | */ |
Jesse Wilson | fb231aa | 2011-02-07 15:15:56 -0800 | [diff] [blame] | 267 | private final LruCache<Object, Bitmap> mBitmapCache; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 268 | |
| 269 | /** |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 270 | * A map from ImageView to the corresponding photo ID or uri, encapsulated in a request. |
| 271 | * The request may swapped out before the photo loading request is started. |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 272 | */ |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 273 | private final ConcurrentHashMap<ImageView, Request> mPendingRequests = |
| 274 | new ConcurrentHashMap<ImageView, Request>(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 275 | |
| 276 | /** |
| 277 | * Handler for messages sent to the UI thread. |
| 278 | */ |
| 279 | private final Handler mMainThreadHandler = new Handler(this); |
| 280 | |
| 281 | /** |
| 282 | * Thread responsible for loading photos from the database. Created upon |
| 283 | * the first request. |
| 284 | */ |
| 285 | private LoaderThread mLoaderThread; |
| 286 | |
| 287 | /** |
| 288 | * A gate to make sure we only send one instance of MESSAGE_PHOTOS_NEEDED at a time. |
| 289 | */ |
| 290 | private boolean mLoadingRequested; |
| 291 | |
| 292 | /** |
| 293 | * Flag indicating if the image loading is paused. |
| 294 | */ |
| 295 | private boolean mPaused; |
| 296 | |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 297 | /** Cache size for {@link #mBitmapHolderCache} for devices with "large" RAM. */ |
| 298 | private static final int HOLDER_CACHE_SIZE = 2000000; |
| 299 | |
| 300 | /** Cache size for {@link #mBitmapCache} for devices with "large" RAM. */ |
| 301 | private static final int BITMAP_CACHE_SIZE = 36864 * 48; // 1728K |
| 302 | |
| 303 | private static final int LARGE_RAM_THRESHOLD = 640 * 1024 * 1024; |
| 304 | |
| 305 | /** For debug: How many times we had to reload cached photo for a stale entry */ |
| 306 | private final AtomicInteger mStaleCacheOverwrite = new AtomicInteger(); |
| 307 | |
| 308 | /** For debug: How many times we had to reload cached photo for a fresh entry. Should be 0. */ |
| 309 | private final AtomicInteger mFreshCacheOverwrite = new AtomicInteger(); |
| 310 | |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 311 | public ContactPhotoManagerImpl(Context context) { |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 312 | mContext = context; |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 313 | |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 314 | final float cacheSizeAdjustment = |
| 315 | (MemoryUtils.getTotalMemorySize() >= LARGE_RAM_THRESHOLD) ? 1.0f : 0.5f; |
| 316 | final int bitmapCacheSize = (int) (cacheSizeAdjustment * BITMAP_CACHE_SIZE); |
| 317 | mBitmapCache = new LruCache<Object, Bitmap>(bitmapCacheSize) { |
| 318 | @Override protected int sizeOf(Object key, Bitmap value) { |
| 319 | return value.getByteCount(); |
| 320 | } |
| 321 | |
| 322 | @Override protected void entryRemoved( |
| 323 | boolean evicted, Object key, Bitmap oldValue, Bitmap newValue) { |
| 324 | if (DEBUG) dumpStats(); |
| 325 | } |
| 326 | }; |
| 327 | final int holderCacheSize = (int) (cacheSizeAdjustment * HOLDER_CACHE_SIZE); |
| 328 | mBitmapHolderCache = new LruCache<Object, BitmapHolder>(holderCacheSize) { |
Jesse Wilson | fb231aa | 2011-02-07 15:15:56 -0800 | [diff] [blame] | 329 | @Override protected int sizeOf(Object key, BitmapHolder value) { |
Jesse Wilson | ab79a26 | 2011-02-09 15:34:31 -0800 | [diff] [blame] | 330 | return value.bytes != null ? value.bytes.length : 0; |
Jesse Wilson | fb231aa | 2011-02-07 15:15:56 -0800 | [diff] [blame] | 331 | } |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 332 | |
| 333 | @Override protected void entryRemoved( |
| 334 | boolean evicted, Object key, BitmapHolder oldValue, BitmapHolder newValue) { |
| 335 | if (DEBUG) dumpStats(); |
| 336 | } |
Jesse Wilson | fb231aa | 2011-02-07 15:15:56 -0800 | [diff] [blame] | 337 | }; |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 338 | mBitmapHolderCacheRedZoneBytes = (int) (holderCacheSize * 0.75); |
| 339 | Log.i(TAG, "Cache adj: " + cacheSizeAdjustment); |
| 340 | if (DEBUG) { |
| 341 | Log.d(TAG, "Cache size: " + btk(mBitmapHolderCache.maxSize()) |
| 342 | + " + " + btk(mBitmapCache.maxSize())); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | /** Converts bytes to K bytes, rounding up. Used only for debug log. */ |
| 347 | private static String btk(int bytes) { |
| 348 | return ((bytes + 1023) / 1024) + "K"; |
| 349 | } |
| 350 | |
| 351 | private static final int safeDiv(int dividend, int divisor) { |
| 352 | return (divisor == 0) ? 0 : (dividend / divisor); |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Dump cache stats on logcat. |
| 357 | */ |
| 358 | private void dumpStats() { |
| 359 | if (!DEBUG) return; |
| 360 | { |
| 361 | int numHolders = 0; |
| 362 | int rawBytes = 0; |
| 363 | int bitmapBytes = 0; |
| 364 | int numBitmaps = 0; |
| 365 | for (BitmapHolder h : mBitmapHolderCache.snapshot().values()) { |
| 366 | numHolders++; |
| 367 | if (h.bytes != null) { |
| 368 | rawBytes += h.bytes.length; |
| 369 | } |
| 370 | Bitmap b = h.bitmapRef != null ? h.bitmapRef.get() : null; |
| 371 | if (b != null) { |
| 372 | numBitmaps++; |
| 373 | bitmapBytes += b.getByteCount(); |
| 374 | } |
| 375 | } |
| 376 | Log.d(TAG, "L1: " + btk(rawBytes) + " + " + btk(bitmapBytes) + " = " |
| 377 | + btk(rawBytes + bitmapBytes) + ", " + numHolders + " holders, " |
| 378 | + numBitmaps + " bitmaps, avg: " |
| 379 | + btk(safeDiv(rawBytes, numHolders)) |
| 380 | + "," + btk(safeDiv(bitmapBytes,numBitmaps))); |
| 381 | Log.d(TAG, "L1 Stats: " + mBitmapHolderCache.toString() |
| 382 | + ", overwrite: fresh=" + mFreshCacheOverwrite.get() |
| 383 | + " stale=" + mStaleCacheOverwrite.get()); |
| 384 | } |
| 385 | |
| 386 | { |
| 387 | int numBitmaps = 0; |
| 388 | int bitmapBytes = 0; |
| 389 | for (Bitmap b : mBitmapCache.snapshot().values()) { |
| 390 | numBitmaps++; |
| 391 | bitmapBytes += b.getByteCount(); |
| 392 | } |
| 393 | Log.d(TAG, "L2: " + btk(bitmapBytes) + ", " + numBitmaps + " bitmaps" |
| 394 | + ", avg: " + btk(safeDiv(bitmapBytes, numBitmaps))); |
| 395 | // We don't get from L2 cache, so L2 stats is meaningless. |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | @Override |
| 400 | public void onTrimMemory(int level) { |
| 401 | if (DEBUG) Log.d(TAG, "onTrimMemory: " + level); |
| 402 | if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) { |
| 403 | // Clear the caches. Note all pending requests will be removed too. |
| 404 | clear(); |
| 405 | } |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public void preloadPhotosInBackground() { |
| 410 | ensureLoaderThread(); |
| 411 | mLoaderThread.requestPreloading(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 414 | @Override |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 415 | public void loadPhoto(ImageView view, long photoId, boolean hires, boolean darkTheme, |
| 416 | DefaultImageProvider defaultProvider) { |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 417 | if (photoId == 0) { |
| 418 | // No photo is needed |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 419 | defaultProvider.applyDefaultImage(view, hires, darkTheme); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 420 | mPendingRequests.remove(view); |
| 421 | } else { |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 422 | if (DEBUG) Log.d(TAG, "loadPhoto request: " + photoId); |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 423 | loadPhotoByIdOrUri(view, Request.createFromId(photoId, hires, darkTheme, |
| 424 | defaultProvider)); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 428 | @Override |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 429 | public void loadPhoto(ImageView view, Uri photoUri, boolean hires, boolean darkTheme, |
| 430 | DefaultImageProvider defaultProvider) { |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 431 | if (photoUri == null) { |
| 432 | // No photo is needed |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 433 | defaultProvider.applyDefaultImage(view, hires, darkTheme); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 434 | mPendingRequests.remove(view); |
| 435 | } else { |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 436 | if (DEBUG) Log.d(TAG, "loadPhoto request: " + photoUri); |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 437 | loadPhotoByIdOrUri(view, Request.createFromUri(photoUri, hires, darkTheme, |
| 438 | defaultProvider)); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 442 | private void loadPhotoByIdOrUri(ImageView view, Request request) { |
Daniel Lehmann | 7a46cde | 2012-02-01 17:53:44 -0800 | [diff] [blame] | 443 | boolean loaded = loadCachedPhoto(view, request, false); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 444 | if (loaded) { |
| 445 | mPendingRequests.remove(view); |
| 446 | } else { |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 447 | mPendingRequests.put(view, request); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 448 | if (!mPaused) { |
| 449 | // Send a request to start loading photos |
| 450 | requestLoading(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 455 | @Override |
Daisuke Miyakawa | f5be9ba | 2011-08-05 09:17:07 -0700 | [diff] [blame] | 456 | public void removePhoto(ImageView view) { |
| 457 | view.setImageDrawable(null); |
| 458 | mPendingRequests.remove(view); |
| 459 | } |
| 460 | |
| 461 | @Override |
Dmitri Plotnikov | 718a250 | 2010-11-23 17:56:28 -0800 | [diff] [blame] | 462 | public void refreshCache() { |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 463 | if (DEBUG) Log.d(TAG, "refreshCache"); |
Jesse Wilson | fb231aa | 2011-02-07 15:15:56 -0800 | [diff] [blame] | 464 | for (BitmapHolder holder : mBitmapHolderCache.snapshot().values()) { |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 465 | holder.fresh = false; |
Dmitri Plotnikov | 718a250 | 2010-11-23 17:56:28 -0800 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | /** |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 470 | * Checks if the photo is present in cache. If so, sets the photo on the view. |
| 471 | * |
| 472 | * @return false if the photo needs to be (re)loaded from the provider. |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 473 | */ |
Daniel Lehmann | 7a46cde | 2012-02-01 17:53:44 -0800 | [diff] [blame] | 474 | private boolean loadCachedPhoto(ImageView view, Request request, boolean fadeIn) { |
| 475 | Bitmap bitmap = mBitmapCache.get(request.getKey()); |
| 476 | if (bitmap != null) { |
| 477 | view.setImageBitmap(bitmap); |
| 478 | return true; |
| 479 | } |
| 480 | |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 481 | BitmapHolder holder = mBitmapHolderCache.get(request.getKey()); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 482 | if (holder == null) { |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 483 | // The bitmap has not been loaded - should display the placeholder image. |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 484 | request.applyDefaultImage(view); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 485 | return false; |
| 486 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 487 | |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 488 | if (holder.bytes == null) { |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 489 | request.applyDefaultImage(view); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 490 | return holder.fresh; |
| 491 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 492 | |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 493 | // Optionally decode bytes into a bitmap |
| 494 | inflateBitmap(holder); |
Dmitri Plotnikov | 718a250 | 2010-11-23 17:56:28 -0800 | [diff] [blame] | 495 | |
Daniel Lehmann | 7a46cde | 2012-02-01 17:53:44 -0800 | [diff] [blame] | 496 | if (fadeIn) { |
| 497 | Drawable[] layers = new Drawable[2]; |
| 498 | layers[0] = mContext.getResources().getDrawable( |
| 499 | getDefaultAvatarResId(request.mHires, request.mDarkTheme)); |
| 500 | layers[1] = new BitmapDrawable(mContext.getResources(), holder.bitmap); |
| 501 | TransitionDrawable drawable = new TransitionDrawable(layers); |
| 502 | view.setImageDrawable(drawable); |
| 503 | drawable.startTransition(FADE_TRANSITION_DURATION); |
| 504 | } else { |
| 505 | view.setImageBitmap(holder.bitmap); |
| 506 | } |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 507 | |
Daisuke Miyakawa | bf9b213 | 2011-10-11 19:03:52 -0700 | [diff] [blame] | 508 | if (holder.bitmap != null) { |
| 509 | // Put the bitmap in the LRU cache |
Daniel Lehmann | 7a46cde | 2012-02-01 17:53:44 -0800 | [diff] [blame] | 510 | mBitmapCache.put(request.getKey(), holder.bitmap); |
Daisuke Miyakawa | bf9b213 | 2011-10-11 19:03:52 -0700 | [diff] [blame] | 511 | } |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 512 | |
| 513 | // Soften the reference |
| 514 | holder.bitmap = null; |
| 515 | |
| 516 | return holder.fresh; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * If necessary, decodes bytes stored in the holder to Bitmap. As long as the |
| 521 | * bitmap is held either by {@link #mBitmapCache} or by a soft reference in |
| 522 | * the holder, it will not be necessary to decode the bitmap. |
| 523 | */ |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 524 | private static void inflateBitmap(BitmapHolder holder) { |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 525 | byte[] bytes = holder.bytes; |
| 526 | if (bytes == null || bytes.length == 0) { |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | // Check the soft reference. If will be retained if the bitmap is also |
| 531 | // in the LRU cache, so we don't need to check the LRU cache explicitly. |
| 532 | if (holder.bitmapRef != null) { |
| 533 | holder.bitmap = holder.bitmapRef.get(); |
| 534 | if (holder.bitmap != null) { |
| 535 | return; |
Dmitri Plotnikov | 718a250 | 2010-11-23 17:56:28 -0800 | [diff] [blame] | 536 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 539 | try { |
| 540 | Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null); |
| 541 | holder.bitmap = bitmap; |
| 542 | holder.bitmapRef = new SoftReference<Bitmap>(bitmap); |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 543 | if (DEBUG) { |
| 544 | Log.d(TAG, "inflateBitmap " + btk(bytes.length) + " -> " |
| 545 | + bitmap.getWidth() + "x" + bitmap.getHeight() |
| 546 | + ", " + btk(bitmap.getByteCount())); |
| 547 | } |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 548 | } catch (OutOfMemoryError e) { |
| 549 | // Do nothing - the photo will appear to be missing |
| 550 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Dmitri Plotnikov | b369c49 | 2010-03-24 18:11:24 -0700 | [diff] [blame] | 553 | public void clear() { |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 554 | if (DEBUG) Log.d(TAG, "clear"); |
Dmitri Plotnikov | b369c49 | 2010-03-24 18:11:24 -0700 | [diff] [blame] | 555 | mPendingRequests.clear(); |
Jesse Wilson | fb231aa | 2011-02-07 15:15:56 -0800 | [diff] [blame] | 556 | mBitmapHolderCache.evictAll(); |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 557 | mBitmapCache.evictAll(); |
Dmitri Plotnikov | b369c49 | 2010-03-24 18:11:24 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 560 | @Override |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 561 | public void pause() { |
| 562 | mPaused = true; |
| 563 | } |
| 564 | |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 565 | @Override |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 566 | public void resume() { |
| 567 | mPaused = false; |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 568 | if (DEBUG) dumpStats(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 569 | if (!mPendingRequests.isEmpty()) { |
| 570 | requestLoading(); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Sends a message to this thread itself to start loading images. If the current |
| 576 | * view contains multiple image views, all of those image views will get a chance |
| 577 | * to request their respective photos before any of those requests are executed. |
| 578 | * This allows us to load images in bulk. |
| 579 | */ |
| 580 | private void requestLoading() { |
| 581 | if (!mLoadingRequested) { |
| 582 | mLoadingRequested = true; |
| 583 | mMainThreadHandler.sendEmptyMessage(MESSAGE_REQUEST_LOADING); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * Processes requests on the main thread. |
| 589 | */ |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 590 | @Override |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 591 | public boolean handleMessage(Message msg) { |
| 592 | switch (msg.what) { |
| 593 | case MESSAGE_REQUEST_LOADING: { |
| 594 | mLoadingRequested = false; |
| 595 | if (!mPaused) { |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 596 | ensureLoaderThread(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 597 | mLoaderThread.requestLoading(); |
| 598 | } |
| 599 | return true; |
| 600 | } |
| 601 | |
| 602 | case MESSAGE_PHOTOS_LOADED: { |
| 603 | if (!mPaused) { |
| 604 | processLoadedImages(); |
| 605 | } |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 606 | if (DEBUG) dumpStats(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 607 | return true; |
| 608 | } |
| 609 | } |
| 610 | return false; |
| 611 | } |
| 612 | |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 613 | public void ensureLoaderThread() { |
| 614 | if (mLoaderThread == null) { |
| 615 | mLoaderThread = new LoaderThread(mContext.getContentResolver()); |
| 616 | mLoaderThread.start(); |
| 617 | } |
| 618 | } |
| 619 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 620 | /** |
| 621 | * Goes over pending loading requests and displays loaded photos. If some of the |
| 622 | * photos still haven't been loaded, sends another request for image loading. |
| 623 | */ |
| 624 | private void processLoadedImages() { |
| 625 | Iterator<ImageView> iterator = mPendingRequests.keySet().iterator(); |
| 626 | while (iterator.hasNext()) { |
| 627 | ImageView view = iterator.next(); |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 628 | Request key = mPendingRequests.get(view); |
Daniel Lehmann | 7a46cde | 2012-02-01 17:53:44 -0800 | [diff] [blame] | 629 | boolean loaded = loadCachedPhoto(view, key, true); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 630 | if (loaded) { |
| 631 | iterator.remove(); |
| 632 | } |
| 633 | } |
| 634 | |
Dmitri Plotnikov | 37dc7cf | 2010-12-17 19:25:08 -0800 | [diff] [blame] | 635 | softenCache(); |
| 636 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 637 | if (!mPendingRequests.isEmpty()) { |
| 638 | requestLoading(); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /** |
Dmitri Plotnikov | 37dc7cf | 2010-12-17 19:25:08 -0800 | [diff] [blame] | 643 | * Removes strong references to loaded bitmaps to allow them to be garbage collected |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 644 | * if needed. Some of the bitmaps will still be retained by {@link #mBitmapCache}. |
Dmitri Plotnikov | 37dc7cf | 2010-12-17 19:25:08 -0800 | [diff] [blame] | 645 | */ |
| 646 | private void softenCache() { |
Jesse Wilson | fb231aa | 2011-02-07 15:15:56 -0800 | [diff] [blame] | 647 | for (BitmapHolder holder : mBitmapHolderCache.snapshot().values()) { |
Dmitri Plotnikov | 37dc7cf | 2010-12-17 19:25:08 -0800 | [diff] [blame] | 648 | holder.bitmap = null; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /** |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 653 | * Stores the supplied bitmap in cache. |
| 654 | */ |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 655 | private void cacheBitmap(Object key, byte[] bytes, boolean preloading) { |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 656 | if (DEBUG) { |
| 657 | BitmapHolder prev = mBitmapHolderCache.get(key); |
| 658 | if (prev != null && prev.bytes != null) { |
| 659 | Log.d(TAG, "Overwriting cache: key=" + key + (prev.fresh ? " FRESH" : " stale")); |
| 660 | if (prev.fresh) { |
| 661 | mFreshCacheOverwrite.incrementAndGet(); |
| 662 | } else { |
| 663 | mStaleCacheOverwrite.incrementAndGet(); |
| 664 | } |
| 665 | } |
| 666 | Log.d(TAG, "Caching data: key=" + key + ", " + btk(bytes.length)); |
| 667 | } |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 668 | BitmapHolder holder = new BitmapHolder(bytes); |
| 669 | holder.fresh = true; |
| 670 | |
| 671 | // Unless this image is being preloaded, decode it right away while |
| 672 | // we are still on the background thread. |
| 673 | if (!preloading) { |
| 674 | inflateBitmap(holder); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 675 | } |
| 676 | |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 677 | mBitmapHolderCache.put(key, holder); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 678 | } |
| 679 | |
Dave Santoro | 14d2083 | 2011-12-02 18:14:52 -0800 | [diff] [blame] | 680 | @Override |
| 681 | public void cacheBitmap(Uri photoUri, Bitmap bitmap, byte[] photoBytes) { |
Daniel Lehmann | 2d73de0 | 2012-02-21 19:51:38 -0800 | [diff] [blame] | 682 | Request request = Request.createFromUri(photoUri, true, false, DEFAULT_AVATAR); |
Dave Santoro | 14d2083 | 2011-12-02 18:14:52 -0800 | [diff] [blame] | 683 | BitmapHolder holder = new BitmapHolder(photoBytes); |
| 684 | mBitmapHolderCache.put(request.getKey(), holder); |
Daniel Lehmann | 7a46cde | 2012-02-01 17:53:44 -0800 | [diff] [blame] | 685 | mBitmapCache.put(request.getKey(), bitmap); |
Dave Santoro | 14d2083 | 2011-12-02 18:14:52 -0800 | [diff] [blame] | 686 | } |
| 687 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 688 | /** |
| 689 | * Populates an array of photo IDs that need to be loaded. |
| 690 | */ |
Flavio Lerda | d33b18c | 2011-07-17 22:03:15 +0100 | [diff] [blame] | 691 | private void obtainPhotoIdsAndUrisToLoad(Set<Long> photoIds, |
| 692 | Set<String> photoIdsAsStrings, Set<Uri> uris) { |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 693 | photoIds.clear(); |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 694 | photoIdsAsStrings.clear(); |
Dmitri Plotnikov | 0f7462c | 2010-10-20 14:41:18 -0700 | [diff] [blame] | 695 | uris.clear(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 696 | |
| 697 | /* |
| 698 | * Since the call is made from the loader thread, the map could be |
| 699 | * changing during the iteration. That's not really a problem: |
| 700 | * ConcurrentHashMap will allow those changes to happen without throwing |
| 701 | * exceptions. Since we may miss some requests in the situation of |
| 702 | * concurrent change, we will need to check the map again once loading |
| 703 | * is complete. |
| 704 | */ |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 705 | Iterator<Request> iterator = mPendingRequests.values().iterator(); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 706 | while (iterator.hasNext()) { |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 707 | Request request = iterator.next(); |
| 708 | BitmapHolder holder = mBitmapHolderCache.get(request); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 709 | if (holder == null || !holder.fresh) { |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 710 | if (request.isUriRequest()) { |
| 711 | uris.add(request.mUri); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 712 | } else { |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 713 | photoIds.add(request.mId); |
| 714 | photoIdsAsStrings.add(String.valueOf(request.mId)); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 715 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * The thread that performs loading of photos from the database. |
| 722 | */ |
| 723 | private class LoaderThread extends HandlerThread implements Callback { |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 724 | private static final int BUFFER_SIZE = 1024*16; |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 725 | private static final int MESSAGE_PRELOAD_PHOTOS = 0; |
| 726 | private static final int MESSAGE_LOAD_PHOTOS = 1; |
| 727 | |
| 728 | /** |
| 729 | * A pause between preload batches that yields to the UI thread. |
| 730 | */ |
Makoto Onuki | 173f281 | 2011-09-06 14:49:27 -0700 | [diff] [blame] | 731 | private static final int PHOTO_PRELOAD_DELAY = 1000; |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 732 | |
| 733 | /** |
| 734 | * Number of photos to preload per batch. |
| 735 | */ |
| 736 | private static final int PRELOAD_BATCH = 25; |
| 737 | |
| 738 | /** |
| 739 | * Maximum number of photos to preload. If the cache size is 2Mb and |
| 740 | * the expected average size of a photo is 4kb, then this number should be 2Mb/4kb = 500. |
| 741 | */ |
Makoto Onuki | 173f281 | 2011-09-06 14:49:27 -0700 | [diff] [blame] | 742 | private static final int MAX_PHOTOS_TO_PRELOAD = 100; |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 743 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 744 | private final ContentResolver mResolver; |
| 745 | private final StringBuilder mStringBuilder = new StringBuilder(); |
Flavio Lerda | d33b18c | 2011-07-17 22:03:15 +0100 | [diff] [blame] | 746 | private final Set<Long> mPhotoIds = Sets.newHashSet(); |
| 747 | private final Set<String> mPhotoIdsAsStrings = Sets.newHashSet(); |
| 748 | private final Set<Uri> mPhotoUris = Sets.newHashSet(); |
| 749 | private final List<Long> mPreloadPhotoIds = Lists.newArrayList(); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 750 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 751 | private Handler mLoaderThreadHandler; |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 752 | private byte mBuffer[]; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 753 | |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 754 | private static final int PRELOAD_STATUS_NOT_STARTED = 0; |
| 755 | private static final int PRELOAD_STATUS_IN_PROGRESS = 1; |
| 756 | private static final int PRELOAD_STATUS_DONE = 2; |
| 757 | |
| 758 | private int mPreloadStatus = PRELOAD_STATUS_NOT_STARTED; |
| 759 | |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 760 | public LoaderThread(ContentResolver resolver) { |
| 761 | super(LOADER_THREAD_NAME); |
| 762 | mResolver = resolver; |
| 763 | } |
| 764 | |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 765 | public void ensureHandler() { |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 766 | if (mLoaderThreadHandler == null) { |
| 767 | mLoaderThreadHandler = new Handler(getLooper(), this); |
| 768 | } |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | /** |
| 772 | * Kicks off preloading of the next batch of photos on the background thread. |
| 773 | * Preloading will happen after a delay: we want to yield to the UI thread |
| 774 | * as much as possible. |
| 775 | * <p> |
| 776 | * If preloading is already complete, does nothing. |
| 777 | */ |
| 778 | public void requestPreloading() { |
| 779 | if (mPreloadStatus == PRELOAD_STATUS_DONE) { |
| 780 | return; |
| 781 | } |
| 782 | |
| 783 | ensureHandler(); |
| 784 | if (mLoaderThreadHandler.hasMessages(MESSAGE_LOAD_PHOTOS)) { |
| 785 | return; |
| 786 | } |
| 787 | |
| 788 | mLoaderThreadHandler.sendEmptyMessageDelayed( |
| 789 | MESSAGE_PRELOAD_PHOTOS, PHOTO_PRELOAD_DELAY); |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * Sends a message to this thread to load requested photos. Cancels a preloading |
| 794 | * request, if any: we don't want preloading to impede loading of the photos |
| 795 | * we need to display now. |
| 796 | */ |
| 797 | public void requestLoading() { |
| 798 | ensureHandler(); |
| 799 | mLoaderThreadHandler.removeMessages(MESSAGE_PRELOAD_PHOTOS); |
| 800 | mLoaderThreadHandler.sendEmptyMessage(MESSAGE_LOAD_PHOTOS); |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /** |
| 804 | * Receives the above message, loads photos and then sends a message |
| 805 | * to the main thread to process them. |
| 806 | */ |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 807 | @Override |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 808 | public boolean handleMessage(Message msg) { |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 809 | switch (msg.what) { |
| 810 | case MESSAGE_PRELOAD_PHOTOS: |
| 811 | preloadPhotosInBackground(); |
| 812 | break; |
| 813 | case MESSAGE_LOAD_PHOTOS: |
| 814 | loadPhotosInBackground(); |
| 815 | break; |
| 816 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 817 | return true; |
| 818 | } |
| 819 | |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 820 | /** |
| 821 | * The first time it is called, figures out which photos need to be preloaded. |
| 822 | * Each subsequent call preloads the next batch of photos and requests |
| 823 | * another cycle of preloading after a delay. The whole process ends when |
| 824 | * we either run out of photos to preload or fill up cache. |
| 825 | */ |
| 826 | private void preloadPhotosInBackground() { |
| 827 | if (mPreloadStatus == PRELOAD_STATUS_DONE) { |
| 828 | return; |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 829 | } |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 830 | |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 831 | if (mPreloadStatus == PRELOAD_STATUS_NOT_STARTED) { |
| 832 | queryPhotosForPreload(); |
| 833 | if (mPreloadPhotoIds.isEmpty()) { |
| 834 | mPreloadStatus = PRELOAD_STATUS_DONE; |
| 835 | } else { |
| 836 | mPreloadStatus = PRELOAD_STATUS_IN_PROGRESS; |
| 837 | } |
| 838 | requestPreloading(); |
| 839 | return; |
| 840 | } |
| 841 | |
Jesse Wilson | fb231aa | 2011-02-07 15:15:56 -0800 | [diff] [blame] | 842 | if (mBitmapHolderCache.size() > mBitmapHolderCacheRedZoneBytes) { |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 843 | mPreloadStatus = PRELOAD_STATUS_DONE; |
| 844 | return; |
| 845 | } |
| 846 | |
| 847 | mPhotoIds.clear(); |
| 848 | mPhotoIdsAsStrings.clear(); |
| 849 | |
| 850 | int count = 0; |
| 851 | int preloadSize = mPreloadPhotoIds.size(); |
| 852 | while(preloadSize > 0 && mPhotoIds.size() < PRELOAD_BATCH) { |
| 853 | preloadSize--; |
| 854 | count++; |
| 855 | Long photoId = mPreloadPhotoIds.get(preloadSize); |
| 856 | mPhotoIds.add(photoId); |
| 857 | mPhotoIdsAsStrings.add(photoId.toString()); |
| 858 | mPreloadPhotoIds.remove(preloadSize); |
| 859 | } |
| 860 | |
Daniel Lehmann | 23b1b54 | 2011-06-10 20:01:38 -0700 | [diff] [blame] | 861 | loadPhotosFromDatabase(true); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 862 | |
| 863 | if (preloadSize == 0) { |
| 864 | mPreloadStatus = PRELOAD_STATUS_DONE; |
| 865 | } |
| 866 | |
Makoto Onuki | 173f281 | 2011-09-06 14:49:27 -0700 | [diff] [blame] | 867 | Log.v(TAG, "Preloaded " + count + " photos. Cached bytes: " |
| 868 | + mBitmapHolderCache.size()); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 869 | |
| 870 | requestPreloading(); |
| 871 | } |
| 872 | |
| 873 | private void queryPhotosForPreload() { |
| 874 | Cursor cursor = null; |
| 875 | try { |
| 876 | Uri uri = Contacts.CONTENT_URI.buildUpon().appendQueryParameter( |
| 877 | ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT)) |
Daniel Lehmann | 4ccae56 | 2011-05-02 16:39:01 -0700 | [diff] [blame] | 878 | .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY, |
| 879 | String.valueOf(MAX_PHOTOS_TO_PRELOAD)) |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 880 | .build(); |
| 881 | cursor = mResolver.query(uri, new String[] { Contacts.PHOTO_ID }, |
| 882 | Contacts.PHOTO_ID + " NOT NULL AND " + Contacts.PHOTO_ID + "!=0", |
| 883 | null, |
Daniel Lehmann | 4ccae56 | 2011-05-02 16:39:01 -0700 | [diff] [blame] | 884 | Contacts.STARRED + " DESC, " + Contacts.LAST_TIME_CONTACTED + " DESC"); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 885 | |
| 886 | if (cursor != null) { |
| 887 | while (cursor.moveToNext()) { |
| 888 | // Insert them in reverse order, because we will be taking |
| 889 | // them from the end of the list for loading. |
| 890 | mPreloadPhotoIds.add(0, cursor.getLong(0)); |
| 891 | } |
| 892 | } |
| 893 | } finally { |
| 894 | if (cursor != null) { |
| 895 | cursor.close(); |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | private void loadPhotosInBackground() { |
| 901 | obtainPhotoIdsAndUrisToLoad(mPhotoIds, mPhotoIdsAsStrings, mPhotoUris); |
Daniel Lehmann | 23b1b54 | 2011-06-10 20:01:38 -0700 | [diff] [blame] | 902 | loadPhotosFromDatabase(false); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 903 | loadRemotePhotos(); |
| 904 | requestPreloading(); |
| 905 | } |
| 906 | |
| 907 | private void loadPhotosFromDatabase(boolean preloading) { |
Flavio Lerda | d33b18c | 2011-07-17 22:03:15 +0100 | [diff] [blame] | 908 | if (mPhotoIds.isEmpty()) { |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 909 | return; |
| 910 | } |
| 911 | |
| 912 | // Remove loaded photos from the preload queue: we don't want |
| 913 | // the preloading process to load them again. |
| 914 | if (!preloading && mPreloadStatus == PRELOAD_STATUS_IN_PROGRESS) { |
Flavio Lerda | d33b18c | 2011-07-17 22:03:15 +0100 | [diff] [blame] | 915 | for (Long id : mPhotoIds) { |
| 916 | mPreloadPhotoIds.remove(id); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 917 | } |
| 918 | if (mPreloadPhotoIds.isEmpty()) { |
| 919 | mPreloadStatus = PRELOAD_STATUS_DONE; |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | mStringBuilder.setLength(0); |
| 924 | mStringBuilder.append(Photo._ID + " IN("); |
Flavio Lerda | d33b18c | 2011-07-17 22:03:15 +0100 | [diff] [blame] | 925 | for (int i = 0; i < mPhotoIds.size(); i++) { |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 926 | if (i != 0) { |
| 927 | mStringBuilder.append(','); |
| 928 | } |
| 929 | mStringBuilder.append('?'); |
| 930 | } |
| 931 | mStringBuilder.append(')'); |
| 932 | |
| 933 | Cursor cursor = null; |
| 934 | try { |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 935 | if (DEBUG) Log.d(TAG, "Loading " + TextUtils.join(",", mPhotoIdsAsStrings)); |
Dave Santoro | 84cac44 | 2011-08-24 15:23:10 -0700 | [diff] [blame] | 936 | cursor = mResolver.query(Data.CONTENT_URI, |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 937 | COLUMNS, |
| 938 | mStringBuilder.toString(), |
| 939 | mPhotoIdsAsStrings.toArray(EMPTY_STRING_ARRAY), |
| 940 | null); |
| 941 | |
| 942 | if (cursor != null) { |
| 943 | while (cursor.moveToNext()) { |
| 944 | Long id = cursor.getLong(0); |
| 945 | byte[] bytes = cursor.getBlob(1); |
| 946 | cacheBitmap(id, bytes, preloading); |
| 947 | mPhotoIds.remove(id); |
| 948 | } |
| 949 | } |
| 950 | } finally { |
| 951 | if (cursor != null) { |
| 952 | cursor.close(); |
| 953 | } |
| 954 | } |
| 955 | |
Dave Santoro | 84cac44 | 2011-08-24 15:23:10 -0700 | [diff] [blame] | 956 | // Remaining photos were not found in the contacts database (but might be in profile). |
Flavio Lerda | d33b18c | 2011-07-17 22:03:15 +0100 | [diff] [blame] | 957 | for (Long id : mPhotoIds) { |
Dave Santoro | 84cac44 | 2011-08-24 15:23:10 -0700 | [diff] [blame] | 958 | if (ContactsContract.isProfileId(id)) { |
| 959 | Cursor profileCursor = null; |
| 960 | try { |
| 961 | profileCursor = mResolver.query( |
| 962 | ContentUris.withAppendedId(Data.CONTENT_URI, id), |
| 963 | COLUMNS, null, null, null); |
| 964 | if (profileCursor != null && profileCursor.moveToFirst()) { |
| 965 | cacheBitmap(profileCursor.getLong(0), profileCursor.getBlob(1), |
| 966 | preloading); |
| 967 | } else { |
| 968 | // Couldn't load a photo this way either. |
| 969 | cacheBitmap(id, null, preloading); |
| 970 | } |
| 971 | } finally { |
| 972 | if (profileCursor != null) { |
| 973 | profileCursor.close(); |
| 974 | } |
| 975 | } |
| 976 | } else { |
| 977 | // Not a profile photo and not found - mark the cache accordingly |
| 978 | cacheBitmap(id, null, preloading); |
| 979 | } |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | mMainThreadHandler.sendEmptyMessage(MESSAGE_PHOTOS_LOADED); |
| 983 | } |
| 984 | |
| 985 | private void loadRemotePhotos() { |
Flavio Lerda | d33b18c | 2011-07-17 22:03:15 +0100 | [diff] [blame] | 986 | for (Uri uri : mPhotoUris) { |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 987 | if (mBuffer == null) { |
| 988 | mBuffer = new byte[BUFFER_SIZE]; |
| 989 | } |
| 990 | try { |
Makoto Onuki | 8f8bd6d | 2011-11-08 14:09:01 -0800 | [diff] [blame] | 991 | if (DEBUG) Log.d(TAG, "Loading " + uri); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 992 | InputStream is = mResolver.openInputStream(uri); |
| 993 | if (is != null) { |
| 994 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 995 | try { |
| 996 | int size; |
| 997 | while ((size = is.read(mBuffer)) != -1) { |
| 998 | baos.write(mBuffer, 0, size); |
| 999 | } |
| 1000 | } finally { |
| 1001 | is.close(); |
| 1002 | } |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 1003 | cacheBitmap(uri, baos.toByteArray(), false); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 1004 | mMainThreadHandler.sendEmptyMessage(MESSAGE_PHOTOS_LOADED); |
Dmitri Plotnikov | 0f7462c | 2010-10-20 14:41:18 -0700 | [diff] [blame] | 1005 | } else { |
| 1006 | Log.v(TAG, "Cannot load photo " + uri); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 1007 | cacheBitmap(uri, null, false); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 1008 | } |
| 1009 | } catch (Exception ex) { |
| 1010 | Log.v(TAG, "Cannot load photo " + uri, ex); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 1011 | cacheBitmap(uri, null, false); |
Dmitri Plotnikov | eb68943 | 2010-09-24 10:10:57 -0700 | [diff] [blame] | 1012 | } |
Dmitri Plotnikov | 3340985 | 2010-02-20 15:02:32 -0800 | [diff] [blame] | 1013 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 1014 | } |
| 1015 | } |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 1016 | |
| 1017 | /** |
| 1018 | * A holder for either a Uri or an id and a flag whether this was requested for the dark or |
| 1019 | * light theme |
| 1020 | */ |
| 1021 | private static final class Request { |
| 1022 | private final long mId; |
| 1023 | private final Uri mUri; |
| 1024 | private final boolean mDarkTheme; |
| 1025 | private final boolean mHires; |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 1026 | private final DefaultImageProvider mDefaultProvider; |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 1027 | |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 1028 | private Request(long id, Uri uri, boolean hires, boolean darkTheme, |
| 1029 | DefaultImageProvider defaultProvider) { |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 1030 | mId = id; |
| 1031 | mUri = uri; |
| 1032 | mDarkTheme = darkTheme; |
| 1033 | mHires = hires; |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 1034 | mDefaultProvider = defaultProvider; |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 1037 | public static Request createFromId(long id, boolean hires, boolean darkTheme, |
| 1038 | DefaultImageProvider defaultProvider) { |
| 1039 | return new Request(id, null /* no URI */, hires, darkTheme, defaultProvider); |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 1042 | public static Request createFromUri(Uri uri, boolean hires, boolean darkTheme, |
| 1043 | DefaultImageProvider defaultProvider) { |
| 1044 | return new Request(0 /* no ID */, uri, hires, darkTheme, defaultProvider); |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | public boolean isDarkTheme() { |
| 1048 | return mDarkTheme; |
| 1049 | } |
| 1050 | |
| 1051 | public boolean isHires() { |
| 1052 | return mHires; |
| 1053 | } |
| 1054 | |
| 1055 | public boolean isUriRequest() { |
| 1056 | return mUri != null; |
| 1057 | } |
| 1058 | |
| 1059 | @Override |
| 1060 | public int hashCode() { |
| 1061 | if (mUri != null) return mUri.hashCode(); |
| 1062 | |
| 1063 | // copied over from Long.hashCode() |
| 1064 | return (int) (mId ^ (mId >>> 32)); |
| 1065 | } |
| 1066 | |
| 1067 | @Override |
| 1068 | public boolean equals(Object o) { |
| 1069 | if (!(o instanceof Request)) return false; |
| 1070 | final Request that = (Request) o; |
| 1071 | // Don't compare equality of mHires and mDarkTheme fields because these are only used |
| 1072 | // in the default contact photo case. When the contact does have a photo, the contact |
| 1073 | // photo is the same regardless of mHires and mDarkTheme, so we shouldn't need to put |
| 1074 | // the photo request on the queue twice. |
| 1075 | return mId == that.mId && UriUtils.areEqual(mUri, that.mUri); |
| 1076 | } |
| 1077 | |
| 1078 | public Object getKey() { |
| 1079 | return mUri == null ? mId : mUri; |
| 1080 | } |
Makoto Onuki | 3d3a15c | 2011-09-22 10:55:08 -0700 | [diff] [blame] | 1081 | |
| 1082 | public void applyDefaultImage(ImageView view) { |
| 1083 | mDefaultProvider.applyDefaultImage(view, mHires, mDarkTheme); |
| 1084 | } |
Daniel Lehmann | ecfc26c | 2011-09-12 17:44:35 -0700 | [diff] [blame] | 1085 | } |
Dmitri Plotnikov | e864385 | 2010-02-17 10:49:05 -0800 | [diff] [blame] | 1086 | } |