1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/com/squareup/picasso/ContactsPhotoBitmapHunter.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,130 @@ 1.4 +/* 1.5 + * Copyright (C) 2013 Square, Inc. 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 +package com.squareup.picasso; 1.20 + 1.21 +import android.annotation.TargetApi; 1.22 +import android.content.ContentResolver; 1.23 +import android.content.Context; 1.24 +import android.content.UriMatcher; 1.25 +import android.graphics.Bitmap; 1.26 +import android.graphics.BitmapFactory; 1.27 +import android.net.Uri; 1.28 +import android.provider.ContactsContract; 1.29 + 1.30 +import java.io.IOException; 1.31 +import java.io.InputStream; 1.32 + 1.33 +import static android.os.Build.VERSION.SDK_INT; 1.34 +import static android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH; 1.35 +import static android.provider.ContactsContract.Contacts.openContactPhotoInputStream; 1.36 +import static com.squareup.picasso.Picasso.LoadedFrom.DISK; 1.37 + 1.38 +class ContactsPhotoBitmapHunter extends BitmapHunter { 1.39 + /** A lookup uri (e.g. content://com.android.contacts/contacts/lookup/3570i61d948d30808e537) */ 1.40 + private static final int ID_LOOKUP = 1; 1.41 + /** A contact thumbnail uri (e.g. content://com.android.contacts/contacts/38/photo) */ 1.42 + private static final int ID_THUMBNAIL = 2; 1.43 + /** A contact uri (e.g. content://com.android.contacts/contacts/38) */ 1.44 + private static final int ID_CONTACT = 3; 1.45 + /** 1.46 + * A contact display photo (high resolution) uri 1.47 + * (e.g. content://com.android.contacts/display_photo/5) 1.48 + */ 1.49 + private static final int ID_DISPLAY_PHOTO = 4; 1.50 + 1.51 + private static final UriMatcher matcher; 1.52 + 1.53 + static { 1.54 + matcher = new UriMatcher(UriMatcher.NO_MATCH); 1.55 + matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*/#", ID_LOOKUP); 1.56 + matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*", ID_LOOKUP); 1.57 + matcher.addURI(ContactsContract.AUTHORITY, "contacts/#/photo", ID_THUMBNAIL); 1.58 + matcher.addURI(ContactsContract.AUTHORITY, "contacts/#", ID_CONTACT); 1.59 + matcher.addURI(ContactsContract.AUTHORITY, "display_photo/#", ID_DISPLAY_PHOTO); 1.60 + } 1.61 + 1.62 + final Context context; 1.63 + 1.64 + ContactsPhotoBitmapHunter(Context context, Picasso picasso, Dispatcher dispatcher, Cache cache, 1.65 + Stats stats, Action action) { 1.66 + super(picasso, dispatcher, cache, stats, action); 1.67 + this.context = context; 1.68 + } 1.69 + 1.70 + @Override Bitmap decode(Request data) throws IOException { 1.71 + InputStream is = null; 1.72 + try { 1.73 + is = getInputStream(); 1.74 + return decodeStream(is, data); 1.75 + } finally { 1.76 + Utils.closeQuietly(is); 1.77 + } 1.78 + } 1.79 + 1.80 + @Override Picasso.LoadedFrom getLoadedFrom() { 1.81 + return DISK; 1.82 + } 1.83 + 1.84 + private InputStream getInputStream() throws IOException { 1.85 + ContentResolver contentResolver = context.getContentResolver(); 1.86 + Uri uri = getData().uri; 1.87 + switch (matcher.match(uri)) { 1.88 + case ID_LOOKUP: 1.89 + uri = ContactsContract.Contacts.lookupContact(contentResolver, uri); 1.90 + if (uri == null) { 1.91 + return null; 1.92 + } 1.93 + // Resolved the uri to a contact uri, intentionally fall through to process the resolved uri 1.94 + case ID_CONTACT: 1.95 + if (SDK_INT < ICE_CREAM_SANDWICH) { 1.96 + return openContactPhotoInputStream(contentResolver, uri); 1.97 + } else { 1.98 + return ContactPhotoStreamIcs.get(contentResolver, uri); 1.99 + } 1.100 + case ID_THUMBNAIL: 1.101 + case ID_DISPLAY_PHOTO: 1.102 + return contentResolver.openInputStream(uri); 1.103 + default: 1.104 + throw new IllegalStateException("Invalid uri: " + uri); 1.105 + } 1.106 + } 1.107 + 1.108 + private Bitmap decodeStream(InputStream stream, Request data) throws IOException { 1.109 + if (stream == null) { 1.110 + return null; 1.111 + } 1.112 + BitmapFactory.Options options = null; 1.113 + if (data.hasSize()) { 1.114 + options = new BitmapFactory.Options(); 1.115 + options.inJustDecodeBounds = true; 1.116 + InputStream is = getInputStream(); 1.117 + try { 1.118 + BitmapFactory.decodeStream(is, null, options); 1.119 + } finally { 1.120 + Utils.closeQuietly(is); 1.121 + } 1.122 + calculateInSampleSize(data.targetWidth, data.targetHeight, options); 1.123 + } 1.124 + return BitmapFactory.decodeStream(stream, null, options); 1.125 + } 1.126 + 1.127 + @TargetApi(ICE_CREAM_SANDWICH) 1.128 + private static class ContactPhotoStreamIcs { 1.129 + static InputStream get(ContentResolver contentResolver, Uri uri) { 1.130 + return openContactPhotoInputStream(contentResolver, uri, true); 1.131 + } 1.132 + } 1.133 +}