FileDocCategorySizeDatePackage
DefaultPhotoManager.javaAPI DocAndroid 5.1 API5783Thu Mar 12 22:22:50 GMT 2015com.android.ex.chips

DefaultPhotoManager

public class DefaultPhotoManager extends Object implements PhotoManager
Default implementation of {@link com.android.ex.chips.PhotoManager} that queries for photo bytes by using the {@link com.android.ex.chips.RecipientEntry}'s photoThumbnailUri.

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final int
BUFFER_SIZE
For reading photos for directory contacts, this is the chunk size for copying from the {@link InputStream} to the output stream.
private final android.content.ContentResolver
mContentResolver
private final android.support.v4.util.LruCache
mPhotoCacheMap
Constructors Summary
public DefaultPhotoManager(android.content.ContentResolver contentResolver)


       
        mContentResolver = contentResolver;
        mPhotoCacheMap = new LruCache<Uri, byte[]>(PHOTO_CACHE_SIZE);
    
Methods Summary
private voidfetchPhotoAsync(RecipientEntry entry, android.net.Uri photoThumbnailUri, PhotoManagerCallback callback)

        final AsyncTask<Void, Void, byte[]> photoLoadTask = new AsyncTask<Void, Void, byte[]>() {
            @Override
            protected byte[] doInBackground(Void... params) {
                // First try running a query. Images for local contacts are
                // loaded by sending a query to the ContactsProvider.
                final Cursor photoCursor = mContentResolver.query(
                        photoThumbnailUri, PhotoQuery.PROJECTION, null, null, null);
                if (photoCursor != null) {
                    try {
                        if (photoCursor.moveToFirst()) {
                            return photoCursor.getBlob(PhotoQuery.PHOTO);
                        }
                    } finally {
                        photoCursor.close();
                    }
                } else {
                    // If the query fails, try streaming the URI directly.
                    // For remote directory images, this URI resolves to the
                    // directory provider and the images are loaded by sending
                    // an openFile call to the provider.
                    try {
                        InputStream is = mContentResolver.openInputStream(
                                photoThumbnailUri);
                        if (is != null) {
                            byte[] buffer = new byte[BUFFER_SIZE];
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            try {
                                int size;
                                while ((size = is.read(buffer)) != -1) {
                                    baos.write(buffer, 0, size);
                                }
                            } finally {
                                is.close();
                            }
                            return baos.toByteArray();
                        }
                    } catch (IOException ex) {
                        // ignore
                    }
                }
                return null;
            }

            @Override
            protected void onPostExecute(final byte[] photoBytes) {
                entry.setPhotoBytes(photoBytes);
                if (photoBytes != null) {
                    mPhotoCacheMap.put(photoThumbnailUri, photoBytes);
                    if (callback != null) {
                        callback.onPhotoBytesAsynchronouslyPopulated();
                    }
                } else if (callback != null) {
                    callback.onPhotoBytesAsyncLoadFailed();
                }
            }
        };
        photoLoadTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
    
public voidpopulatePhotoBytesAsync(RecipientEntry entry, PhotoManagerCallback callback)

        final Uri photoThumbnailUri = entry.getPhotoThumbnailUri();
        if (photoThumbnailUri != null) {
            final byte[] photoBytes = mPhotoCacheMap.get(photoThumbnailUri);
            if (photoBytes != null) {
                entry.setPhotoBytes(photoBytes);
                if (callback != null) {
                    callback.onPhotoBytesPopulated();
                }
            } else {
                if (DEBUG) {
                    Log.d(TAG, "No photo cache for " + entry.getDisplayName()
                            + ". Fetch one asynchronously");
                }
                fetchPhotoAsync(entry, photoThumbnailUri, callback);
            }
        } else if (callback != null) {
            callback.onPhotoBytesAsyncLoadFailed();
        }