• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.xmladapters;
18 
19 import android.content.ContentUris;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.database.Cursor;
23 import android.graphics.Bitmap;
24 import android.graphics.BitmapFactory;
25 import android.graphics.drawable.BitmapDrawable;
26 import android.graphics.drawable.Drawable;
27 import android.net.Uri;
28 import android.provider.ContactsContract;
29 import android.view.View;
30 import android.widget.TextView;
31 
32 import java.io.InputStream;
33 import java.util.HashMap;
34 
35 /**
36  * This custom cursor binder is used by the adapter defined in res/xml to
37  * bind contacts photos to their respective list item. This binder simply
38  * queries a contact's photo based on the contact's id and sets the
39  * photo as a compound drawable on the TextView used to display the contact's
40  * name.
41  */
42 public class ContactPhotoBinder extends Adapters.CursorBinder {
43     private static final int PHOTO_SIZE_DIP = 54;
44 
45     private final Drawable mDefault;
46     private final HashMap<Long, Drawable> mCache;
47     private final Resources mResources;
48     private final int mPhotoSize;
49 
ContactPhotoBinder(Context context, Adapters.CursorTransformation transformation)50     public ContactPhotoBinder(Context context, Adapters.CursorTransformation transformation) {
51         super(context, transformation);
52 
53         mResources = mContext.getResources();
54         // Default picture used when a contact does not provide one
55         mDefault = mResources.getDrawable(R.drawable.ic_contact_picture);
56         // Cache used to avoid re-querying contacts photos every time
57         mCache = new HashMap<Long, Drawable>();
58         // Compute the size of the photo based on the display's density
59         mPhotoSize = (int) (PHOTO_SIZE_DIP * mResources.getDisplayMetrics().density + 0.5f);
60     }
61 
62     @Override
bind(View view, Cursor cursor, int columnIndex)63     public boolean bind(View view, Cursor cursor, int columnIndex) {
64         final long id = cursor.getLong(columnIndex);
65 
66         // First check whether we have already cached the contact's photo
67         Drawable d = mCache.get(id);
68 
69         if (d == null) {
70             // If the photo wasn't in the cache, ask the contacts provider for
71             // an input stream we can use to load the photo
72             Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
73             InputStream stream = ContactsContract.Contacts.openContactPhotoInputStream(
74                     mContext.getContentResolver(), uri);
75 
76             // Creates the drawable for the contact's photo or use our fallback drawable
77             if (stream != null) {
78                 // decoding the bitmap could be done in a worker thread too.
79                 Bitmap bitmap = BitmapFactory.decodeStream(stream);
80                 d = new BitmapDrawable(mResources, bitmap);
81             } else {
82                 d = mDefault;
83             }
84 
85             d.setBounds(0, 0, mPhotoSize, mPhotoSize);
86             ((TextView) view).setCompoundDrawables(d, null, null, null);
87 
88             // Remember the photo associated with this contact
89             mCache.put(id, d);
90         } else {
91             ((TextView) view).setCompoundDrawables(d, null, null, null);
92         }
93 
94         return true;
95     }
96 }
97