Skip to content

Commit 121d5b3

Browse files
committed
Merge pull request square#368 from copolii/master
Handle actual photo uris
2 parents 77a8184 + 734070e commit 121d5b3

File tree

1 file changed

+48
-10
lines changed

1 file changed

+48
-10
lines changed

picasso/src/main/java/com/squareup/picasso/ContactsPhotoBitmapHunter.java

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
import android.annotation.TargetApi;
1919
import android.content.ContentResolver;
2020
import android.content.Context;
21+
import android.content.UriMatcher;
2122
import android.graphics.Bitmap;
2223
import android.graphics.BitmapFactory;
2324
import android.net.Uri;
2425
import android.provider.ContactsContract;
26+
2527
import java.io.IOException;
2628
import java.io.InputStream;
2729

@@ -31,6 +33,34 @@
3133
import static com.squareup.picasso.Picasso.LoadedFrom.DISK;
3234

3335
class ContactsPhotoBitmapHunter extends BitmapHunter {
36+
/**
37+
a lookup uri (e.g. content://com.android.contacts/contacts/lookup/3570i61d948d30808e537 )
38+
*/
39+
private static final int ID_LOOKUP = 1;
40+
/**
41+
a contact thumbnail uri (e.g. content://com.android.contacts/contacts/38/photo)
42+
*/
43+
private static final int ID_THUMBNAIL = 2;
44+
/**
45+
a contact uri (e.g. content://com.android.contacts/contacts/38)
46+
*/
47+
private static final int ID_CONTACT = 3;
48+
/**
49+
a contact display photo (high resolution) uri
50+
(e.g. content://com.android.contacts/display_photo/5)
51+
*/
52+
private static final int ID_DISPLAY_PHOTO = 4;
53+
54+
static final UriMatcher matcher;
55+
56+
static {
57+
matcher = new UriMatcher(UriMatcher.NO_MATCH);
58+
matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*", ID_LOOKUP);
59+
matcher.addURI(ContactsContract.AUTHORITY, "contacts/#/photo", ID_THUMBNAIL);
60+
matcher.addURI(ContactsContract.AUTHORITY, "contacts/#", ID_CONTACT);
61+
matcher.addURI(ContactsContract.AUTHORITY, "display_photo/#", ID_DISPLAY_PHOTO);
62+
}
63+
3464
final Context context;
3565

3666
ContactsPhotoBitmapHunter(Context context, Picasso picasso, Dispatcher dispatcher, Cache cache,
@@ -57,16 +87,24 @@ class ContactsPhotoBitmapHunter extends BitmapHunter {
5787
private InputStream getInputStream() throws IOException {
5888
ContentResolver contentResolver = context.getContentResolver();
5989
Uri uri = getData().uri;
60-
if (uri.toString().startsWith(ContactsContract.Contacts.CONTENT_LOOKUP_URI.toString())) {
61-
uri = ContactsContract.Contacts.lookupContact(contentResolver, uri);
62-
if (uri == null) {
63-
return null;
64-
}
65-
}
66-
if (SDK_INT < ICE_CREAM_SANDWICH) {
67-
return openContactPhotoInputStream(contentResolver, uri);
68-
} else {
69-
return ContactPhotoStreamIcs.get(contentResolver, uri);
90+
switch (matcher.match(uri)) {
91+
case ID_LOOKUP:
92+
uri = ContactsContract.Contacts.lookupContact(contentResolver, uri);
93+
if (null == uri) {
94+
return null;
95+
}
96+
// Resolved the uri to a contact uri, intentionally fall through to process the resolved uri
97+
case ID_CONTACT:
98+
if (SDK_INT < ICE_CREAM_SANDWICH) {
99+
return openContactPhotoInputStream(contentResolver, uri);
100+
} else {
101+
return ContactPhotoStreamIcs.get(contentResolver, uri);
102+
}
103+
case ID_THUMBNAIL:
104+
case ID_DISPLAY_PHOTO:
105+
return contentResolver.openInputStream(uri);
106+
default:
107+
throw new IllegalStateException("Invalid uri: " + uri);
70108
}
71109
}
72110

0 commit comments

Comments
 (0)