Skip to content

Commit 5cecac6

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 a60b189 commit 5cecac6

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

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

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,36 @@
3333
import static com.squareup.picasso.Picasso.LoadedFrom.DISK;
3434

3535
class ContactsPhotoBitmapHunter extends BitmapHunter {
36-
final Context context;
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+
3754
static final UriMatcher matcher;
3855

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+
64+
final Context context;
65+
3966
ContactsPhotoBitmapHunter(Context context, Picasso picasso, Dispatcher dispatcher, Cache cache,
4067
Stats stats, Action action) {
4168
super(picasso, dispatcher, cache, stats, action);
@@ -61,22 +88,23 @@ private InputStream getInputStream() throws IOException {
6188
ContentResolver contentResolver = context.getContentResolver();
6289
Uri uri = getData().uri;
6390
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:
91+
case ID_LOOKUP:
92+
uri = ContactsContract.Contacts.lookupContact(contentResolver, uri);
93+
if (null == uri) {
7994
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);
80108
}
81109
}
82110

@@ -105,30 +133,4 @@ static InputStream get(ContentResolver contentResolver, Uri uri) {
105133
return openContactPhotoInputStream(contentResolver, uri, true);
106134
}
107135
}
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-
}
134136
}

0 commit comments

Comments
 (0)