blob: 70cae8edcdc81f99ae86461dbf0522a4268a6405 [file] [log] [blame]
alision11e8e162013-05-28 10:33:14 -04001/*
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
alisiond295ec22013-05-17 10:12:13 -040032package com.savoirfairelinux.sflphone.adapters;
33
34import java.io.InputStream;
35
36import android.content.ContentResolver;
37import android.content.ContentUris;
38import android.content.Context;
39import android.graphics.Bitmap;
40import android.graphics.BitmapFactory;
41import android.graphics.Canvas;
alisiond295ec22013-05-17 10:12:13 -040042import android.graphics.Paint;
43import android.graphics.Path;
44import android.graphics.PorterDuff.Mode;
45import android.graphics.PorterDuffXfermode;
46import android.graphics.RectF;
47import android.net.Uri;
48import android.provider.ContactsContract;
49import android.widget.ImageView;
50
51import com.savoirfairelinux.sflphone.R;
52
alision84813a12013-05-27 17:40:39 -040053public class ContactPictureLoader implements Runnable {
54 private ImageView view;
55 private long cid;
56 private ContentResolver cr;
57 private final String TAG = ContactPictureLoader.class.getSimpleName();
alisiond295ec22013-05-17 10:12:13 -040058
alision84813a12013-05-27 17:40:39 -040059 public ContactPictureLoader(Context context, ImageView element, long contact_id) {
60 cid = contact_id;
61 cr = context.getContentResolver();
62 view = element;
63 }
alisiond295ec22013-05-17 10:12:13 -040064
alision84813a12013-05-27 17:40:39 -040065 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
66 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
67 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri);
68 if (input == null) {
69 return null;
70 }
71 return BitmapFactory.decodeStream(input);
72 }
alisiond295ec22013-05-17 10:12:13 -040073
alision84813a12013-05-27 17:40:39 -040074 @Override
75 public void run() {
alision2ec64f92013-06-17 17:28:58 -040076 Bitmap photo_bmp;
77 try{
78 photo_bmp = loadContactPhoto(cr, cid);
79 }catch(IllegalArgumentException e){
80 photo_bmp = null;
81 }
alision84813a12013-05-27 17:40:39 -040082 if (photo_bmp == null) {
83 photo_bmp = BitmapFactory.decodeResource(view.getResources(), R.drawable.ic_contact_picture);
84 }
alisiond295ec22013-05-17 10:12:13 -040085
alision84813a12013-05-27 17:40:39 -040086 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 }
alisiond295ec22013-05-17 10:12:13 -040092
alision84813a12013-05-27 17:40:39 -040093 final Bitmap externalBMP = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
alisiond295ec22013-05-17 10:12:13 -040094
alision84813a12013-05-27 17:40:39 -040095 int radius = externalBMP.getWidth() / 2;
96 Path path = new Path();
alisiond295ec22013-05-17 10:12:13 -040097
alision84813a12013-05-27 17:40:39 -040098 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);
alisiond295ec22013-05-17 10:12:13 -0400106
alision84813a12013-05-27 17:40:39 -0400107 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);
alisiond295ec22013-05-17 10:12:13 -0400111
alision84813a12013-05-27 17:40:39 -0400112 view.post(new Runnable() {
113 @Override
114 public void run() {
115 view.setImageBitmap(externalBMP);
116 view.invalidate();
117 }
118 });
119 }
alisiond295ec22013-05-17 10:12:13 -0400120}