18
18
import android .annotation .TargetApi ;
19
19
import android .content .ContentResolver ;
20
20
import android .content .Context ;
21
+ import android .content .UriMatcher ;
21
22
import android .graphics .Bitmap ;
22
23
import android .graphics .BitmapFactory ;
23
24
import android .net .Uri ;
24
25
import android .provider .ContactsContract ;
26
+
25
27
import java .io .IOException ;
26
28
import java .io .InputStream ;
27
29
31
33
import static com .squareup .picasso .Picasso .LoadedFrom .DISK ;
32
34
33
35
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
+
34
64
final Context context ;
35
65
36
66
ContactsPhotoBitmapHunter (Context context , Picasso picasso , Dispatcher dispatcher , Cache cache ,
@@ -57,16 +87,24 @@ class ContactsPhotoBitmapHunter extends BitmapHunter {
57
87
private InputStream getInputStream () throws IOException {
58
88
ContentResolver contentResolver = context .getContentResolver ();
59
89
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 );
70
108
}
71
109
}
72
110
0 commit comments