Skip to content

Commit a60b189

Browse files
author
Mahram Z. Foadi
committed
refactored the contacts photo retrieval to allow actual photo uris as well. The previous implementeation only worked with contact and lookup uris.
1 parent 77a8184 commit a60b189

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

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

Lines changed: 46 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

@@ -32,6 +34,7 @@
3234

3335
class ContactsPhotoBitmapHunter extends BitmapHunter {
3436
final Context context;
37+
static final UriMatcher matcher;
3538

3639
ContactsPhotoBitmapHunter(Context context, Picasso picasso, Dispatcher dispatcher, Cache cache,
3740
Stats stats, Action action) {
@@ -57,16 +60,23 @@ class ContactsPhotoBitmapHunter extends BitmapHunter {
5760
private InputStream getInputStream() throws IOException {
5861
ContentResolver contentResolver = context.getContentResolver();
5962
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);
63+
switch (matcher.match(uri)) {
64+
case ID_LOOKUP:
65+
uri = ContactsContract.Contacts.lookupContact(contentResolver, uri);
66+
if (null == uri) {
67+
return null;
68+
}
69+
case ID_CONTACT:
70+
if (SDK_INT < ICE_CREAM_SANDWICH) {
71+
return openContactPhotoInputStream(contentResolver, uri);
72+
} else {
73+
return ContactPhotoStreamIcs.get(contentResolver, uri);
74+
}
75+
case ID_THUMBNAIL:
76+
case ID_DISPLAY_PHOTO:
77+
return contentResolver.openInputStream(uri);
78+
default:
79+
return null;
7080
}
7181
}
7282

@@ -95,4 +105,30 @@ static InputStream get(ContentResolver contentResolver, Uri uri) {
95105
return openContactPhotoInputStream(contentResolver, uri, true);
96106
}
97107
}
108+
109+
/**
110+
a lookup uri (e.g. content://com.android.contacts/contacts/lookup/3570i61d948d30808e537 )
111+
*/
112+
private static final int ID_LOOKUP = 1;
113+
/**
114+
a contact thumbnail uri (e.g. content://com.android.contacts/contacts/38/photo)
115+
*/
116+
private static final int ID_THUMBNAIL = 2;
117+
/**
118+
a contact uri (e.g. content://com.android.contacts/contacts/38)
119+
*/
120+
private static final int ID_CONTACT = 3;
121+
/**
122+
a contact display photo (high resolution) uri
123+
(e.g. content://com.android.contacts/display_photo/5)
124+
*/
125+
private static final int ID_DISPLAY_PHOTO = 4;
126+
127+
static {
128+
matcher = new UriMatcher(UriMatcher.NO_MATCH);
129+
matcher.addURI(ContactsContract.AUTHORITY, "contacts/lookup/*", ID_LOOKUP);
130+
matcher.addURI(ContactsContract.AUTHORITY, "contacts/#/photo", ID_THUMBNAIL);
131+
matcher.addURI(ContactsContract.AUTHORITY, "contacts/#", ID_CONTACT);
132+
matcher.addURI(ContactsContract.AUTHORITY, "display_photo/#", ID_DISPLAY_PHOTO);
133+
}
98134
}

0 commit comments

Comments
 (0)