blob: 9254e8c83f58e017a086ca3a6cbc4d64e24d9037 [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() {
76 Bitmap photo_bmp = loadContactPhoto(cr, cid);
alisiond295ec22013-05-17 10:12:13 -040077
alision84813a12013-05-27 17:40:39 -040078 if (photo_bmp == null) {
79 photo_bmp = BitmapFactory.decodeResource(view.getResources(), R.drawable.ic_contact_picture);
80 }
alisiond295ec22013-05-17 10:12:13 -040081
alision84813a12013-05-27 17:40:39 -040082 int w = photo_bmp.getWidth(), h = photo_bmp.getHeight();
83 if (w > h) {
84 w = h;
85 } else if (h > w) {
86 h = w;
87 }
alisiond295ec22013-05-17 10:12:13 -040088
alision84813a12013-05-27 17:40:39 -040089 final Bitmap externalBMP = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
alisiond295ec22013-05-17 10:12:13 -040090
alision84813a12013-05-27 17:40:39 -040091 int radius = externalBMP.getWidth() / 2;
92 Path path = new Path();
alisiond295ec22013-05-17 10:12:13 -040093
alision84813a12013-05-27 17:40:39 -040094 path.addCircle(radius, radius, radius, Path.Direction.CW);
95 Paint mPaintPath = new Paint(Paint.ANTI_ALIAS_FLAG);
96 mPaintPath.setStyle(Paint.Style.FILL);
97 mPaintPath.setAntiAlias(true);
98 Bitmap circle = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
99 Canvas circle_drawer = new Canvas(circle);
100 circle_drawer.drawOval(new RectF(0, 0, w, h), mPaintPath);
101 mPaintPath.setFilterBitmap(false);
alisiond295ec22013-05-17 10:12:13 -0400102
alision84813a12013-05-27 17:40:39 -0400103 Canvas internalCanvas = new Canvas(externalBMP);
104 internalCanvas.drawBitmap(photo_bmp, 0, 0, mPaintPath);
105 mPaintPath.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
106 internalCanvas.drawBitmap(circle, 0, 0, mPaintPath);
alisiond295ec22013-05-17 10:12:13 -0400107
alision84813a12013-05-27 17:40:39 -0400108 view.post(new Runnable() {
109 @Override
110 public void run() {
111 view.setImageBitmap(externalBMP);
112 view.invalidate();
113 }
114 });
115 }
alisiond295ec22013-05-17 10:12:13 -0400116}