alision | 11e8e16 | 2013-05-28 10:33:14 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2012 Savoir-Faire Linux Inc. |
| 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
| 31 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 32 | package com.savoirfairelinux.sflphone.adapters; |
| 33 | |
| 34 | import java.io.InputStream; |
| 35 | |
| 36 | import android.content.ContentResolver; |
| 37 | import android.content.ContentUris; |
| 38 | import android.content.Context; |
| 39 | import android.graphics.Bitmap; |
| 40 | import android.graphics.BitmapFactory; |
| 41 | import android.graphics.Canvas; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 42 | import android.graphics.Paint; |
| 43 | import android.graphics.Path; |
| 44 | import android.graphics.PorterDuff.Mode; |
| 45 | import android.graphics.PorterDuffXfermode; |
| 46 | import android.graphics.RectF; |
| 47 | import android.net.Uri; |
| 48 | import android.provider.ContactsContract; |
| 49 | import android.widget.ImageView; |
| 50 | |
| 51 | import com.savoirfairelinux.sflphone.R; |
| 52 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 53 | public class ContactPictureLoader implements Runnable { |
| 54 | private ImageView view; |
| 55 | private long cid; |
| 56 | private ContentResolver cr; |
| 57 | private final String TAG = ContactPictureLoader.class.getSimpleName(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 58 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 59 | public ContactPictureLoader(Context context, ImageView element, long contact_id) { |
| 60 | cid = contact_id; |
| 61 | cr = context.getContentResolver(); |
| 62 | view = element; |
| 63 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 64 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 65 | public static Bitmap loadContactPhoto(ContentResolver cr, long id) { |
| 66 | Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 67 | InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri, true); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 68 | if (input == null) { |
| 69 | return null; |
| 70 | } |
| 71 | return BitmapFactory.decodeStream(input); |
| 72 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 73 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 74 | @Override |
| 75 | public void run() { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 76 | Bitmap photo_bmp; |
| 77 | try{ |
| 78 | photo_bmp = loadContactPhoto(cr, cid); |
| 79 | }catch(IllegalArgumentException e){ |
| 80 | photo_bmp = null; |
| 81 | } |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 82 | if (photo_bmp == null) { |
| 83 | photo_bmp = BitmapFactory.decodeResource(view.getResources(), R.drawable.ic_contact_picture); |
| 84 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 85 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 86 | int w = photo_bmp.getWidth(), h = photo_bmp.getHeight(); |
| 87 | if (w > h) { |
| 88 | w = h; |
| 89 | } else if (h > w) { |
| 90 | h = w; |
| 91 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 92 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 93 | final Bitmap externalBMP = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 94 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 95 | int radius = externalBMP.getWidth() / 2; |
| 96 | Path path = new Path(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 97 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 98 | path.addCircle(radius, radius, radius, Path.Direction.CW); |
| 99 | Paint mPaintPath = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 100 | mPaintPath.setStyle(Paint.Style.FILL); |
| 101 | mPaintPath.setAntiAlias(true); |
| 102 | Bitmap circle = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
| 103 | Canvas circle_drawer = new Canvas(circle); |
| 104 | circle_drawer.drawOval(new RectF(0, 0, w, h), mPaintPath); |
| 105 | mPaintPath.setFilterBitmap(false); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 106 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 107 | Canvas internalCanvas = new Canvas(externalBMP); |
| 108 | internalCanvas.drawBitmap(photo_bmp, 0, 0, mPaintPath); |
| 109 | mPaintPath.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); |
| 110 | internalCanvas.drawBitmap(circle, 0, 0, mPaintPath); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 111 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 112 | view.post(new Runnable() { |
| 113 | @Override |
| 114 | public void run() { |
| 115 | view.setImageBitmap(externalBMP); |
| 116 | view.invalidate(); |
| 117 | } |
| 118 | }); |
| 119 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 120 | } |