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
32
34
33
35
class ContactsPhotoBitmapHunter extends BitmapHunter {
34
36
final Context context ;
37
+ static final UriMatcher matcher ;
35
38
36
39
ContactsPhotoBitmapHunter (Context context , Picasso picasso , Dispatcher dispatcher , Cache cache ,
37
40
Stats stats , Action action ) {
@@ -57,16 +60,23 @@ class ContactsPhotoBitmapHunter extends BitmapHunter {
57
60
private InputStream getInputStream () throws IOException {
58
61
ContentResolver contentResolver = context .getContentResolver ();
59
62
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 ;
70
80
}
71
81
}
72
82
@@ -95,4 +105,30 @@ static InputStream get(ContentResolver contentResolver, Uri uri) {
95
105
return openContactPhotoInputStream (contentResolver , uri , true );
96
106
}
97
107
}
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
+ }
98
134
}
0 commit comments