blob: 7b57a17ac3ba2893640212471513bc4006a63f76 [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
Alexandre Lision064e1e02013-10-01 16:18:42 -040032package org.sflphone.adapters;
alisiond295ec22013-05-17 10:12:13 -040033
34import java.io.InputStream;
35
Alexandre Lision5dcd0f42013-10-04 14:33:04 -040036import org.sflphone.R;
37
alisiond295ec22013-05-17 10:12:13 -040038import android.content.ContentResolver;
39import android.content.ContentUris;
40import android.content.Context;
Alexandre Lision9b53dc42013-10-04 10:13:46 -040041import android.content.res.Resources;
alisiond295ec22013-05-17 10:12:13 -040042import android.graphics.Bitmap;
43import android.graphics.BitmapFactory;
Alexandre Lision5dcd0f42013-10-04 14:33:04 -040044import android.graphics.BitmapShader;
alisiond295ec22013-05-17 10:12:13 -040045import android.graphics.Canvas;
alisiond295ec22013-05-17 10:12:13 -040046import android.graphics.Paint;
alisiond295ec22013-05-17 10:12:13 -040047import android.graphics.RectF;
Alexandre Lision5dcd0f42013-10-04 14:33:04 -040048import android.graphics.Shader;
alisiond295ec22013-05-17 10:12:13 -040049import android.net.Uri;
50import android.provider.ContactsContract;
51import android.widget.ImageView;
52
Alexandre Lision6e8931e2013-09-19 16:49:34 -040053public class ContactPictureTask implements Runnable {
alision84813a12013-05-27 17:40:39 -040054 private ImageView view;
55 private long cid;
56 private ContentResolver cr;
Alexandre Lision9b53dc42013-10-04 10:13:46 -040057
58 // private final String TAG = ContactPictureTask.class.getSimpleName();
alisiond295ec22013-05-17 10:12:13 -040059
Alexandre Lision6e8931e2013-09-19 16:49:34 -040060 public ContactPictureTask(Context context, ImageView element, long contact_id) {
alision84813a12013-05-27 17:40:39 -040061 cid = contact_id;
62 cr = context.getContentResolver();
63 view = element;
64 }
alisiond295ec22013-05-17 10:12:13 -040065
alision84813a12013-05-27 17:40:39 -040066 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
67 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040068 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri, true);
alision84813a12013-05-27 17:40:39 -040069 if (input == null) {
70 return null;
71 }
72 return BitmapFactory.decodeStream(input);
73 }
alisiond295ec22013-05-17 10:12:13 -040074
alision84813a12013-05-27 17:40:39 -040075 @Override
76 public void run() {
alision2ec64f92013-06-17 17:28:58 -040077 Bitmap photo_bmp;
Alexandre Lision9b53dc42013-10-04 10:13:46 -040078 try {
79 photo_bmp = loadContactPhoto(cr, cid);
80 } catch (IllegalArgumentException e) {
alision2ec64f92013-06-17 17:28:58 -040081 photo_bmp = null;
82 }
Alexandre Lisionc59685a2013-10-04 16:36:36 -040083
alision84813a12013-05-27 17:40:39 -040084 if (photo_bmp == null) {
Alexandre Lision9b53dc42013-10-04 10:13:46 -040085 photo_bmp = decodeSampledBitmapFromResource(view.getResources(), R.drawable.ic_contact_picture, view.getWidth(), view.getHeight());
alision84813a12013-05-27 17:40:39 -040086 }
alisiond295ec22013-05-17 10:12:13 -040087
alision84813a12013-05-27 17:40:39 -040088 int w = photo_bmp.getWidth(), h = photo_bmp.getHeight();
89 if (w > h) {
90 w = h;
91 } else if (h > w) {
92 h = w;
93 }
alisiond295ec22013-05-17 10:12:13 -040094
alision84813a12013-05-27 17:40:39 -040095 final Bitmap externalBMP = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Alexandre Lisionc59685a2013-10-04 16:36:36 -040096
Alexandre Lision5dcd0f42013-10-04 14:33:04 -040097 BitmapShader shader;
98 shader = new BitmapShader(photo_bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Alexandre Lisionc59685a2013-10-04 16:36:36 -040099
Alexandre Lision5dcd0f42013-10-04 14:33:04 -0400100 Paint paint = new Paint();
101 paint.setAntiAlias(true);
102 paint.setShader(shader);
alision84813a12013-05-27 17:40:39 -0400103 Canvas internalCanvas = new Canvas(externalBMP);
Alexandre Lision5dcd0f42013-10-04 14:33:04 -0400104 internalCanvas.drawOval(new RectF(0, 0, w, h), paint);
alisiond295ec22013-05-17 10:12:13 -0400105
alision84813a12013-05-27 17:40:39 -0400106 view.post(new Runnable() {
107 @Override
108 public void run() {
109 view.setImageBitmap(externalBMP);
110 view.invalidate();
111 }
112 });
113 }
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400114
115 public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
Alexandre Lision9b53dc42013-10-04 10:13:46 -0400116
117 // First decode with inJustDecodeBounds=true to check dimensions
118 final BitmapFactory.Options options = new BitmapFactory.Options();
119 options.inJustDecodeBounds = true;
120 BitmapFactory.decodeResource(res, resId, options);
121
122 // Calculate inSampleSize
123 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
124
125 // Decode bitmap with inSampleSize set
126 options.inJustDecodeBounds = false;
127 return BitmapFactory.decodeResource(res, resId, options);
128 }
129
130 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
131 // Raw height and width of image
132 final int height = options.outHeight;
133 final int width = options.outWidth;
134 int inSampleSize = 1;
135
136 if (height > reqHeight || width > reqWidth) {
137
138 // Calculate ratios of height and width to requested height and width
139 final int heightRatio = Math.round((float) height / (float) reqHeight);
140 final int widthRatio = Math.round((float) width / (float) reqWidth);
141
142 // Choose the smallest ratio as inSampleSize value, this will guarantee
143 // a final image with both dimensions larger than or equal to the
144 // requested height and width.
145 inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
146 }
147
148 return inSampleSize;
149 }
alisiond295ec22013-05-17 10:12:13 -0400150}