blob: 2a4f373fd71e968b0791b952043db1f34f6e7090 [file] [log] [blame]
alision11e8e162013-05-28 10:33:14 -04001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
alision11e8e162013-05-28 10:33:14 -04003 *
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;
Alexandre Lision35c3cbb2013-11-04 17:11:54 -050037import org.sflphone.model.CallContact;
Alexandre Lision5dcd0f42013-10-04 14:33:04 -040038
alisiond295ec22013-05-17 10:12:13 -040039import android.content.ContentResolver;
40import android.content.ContentUris;
41import android.content.Context;
Alexandre Lision9b53dc42013-10-04 10:13:46 -040042import android.content.res.Resources;
alisiond295ec22013-05-17 10:12:13 -040043import android.graphics.Bitmap;
44import android.graphics.BitmapFactory;
Alexandre Lision5dcd0f42013-10-04 14:33:04 -040045import android.graphics.BitmapShader;
alisiond295ec22013-05-17 10:12:13 -040046import android.graphics.Canvas;
Alexandre Lision5f733bc2013-12-04 13:10:30 -050047import android.graphics.Color;
alisiond295ec22013-05-17 10:12:13 -040048import android.graphics.Paint;
Alexandre Lision5f733bc2013-12-04 13:10:30 -050049import android.graphics.Paint.Style;
alisiond295ec22013-05-17 10:12:13 -040050import android.graphics.RectF;
Alexandre Lision5dcd0f42013-10-04 14:33:04 -040051import android.graphics.Shader;
alisiond295ec22013-05-17 10:12:13 -040052import android.net.Uri;
53import android.provider.ContactsContract;
54import android.widget.ImageView;
55
Alexandre Lision6e8931e2013-09-19 16:49:34 -040056public class ContactPictureTask implements Runnable {
alision84813a12013-05-27 17:40:39 -040057 private ImageView view;
Alexandre Lision35c3cbb2013-11-04 17:11:54 -050058 private CallContact contact;
alision84813a12013-05-27 17:40:39 -040059 private ContentResolver cr;
Alexandre Lision5f733bc2013-12-04 13:10:30 -050060 private static int PADDING = 5;
Alexandre Lision9b53dc42013-10-04 10:13:46 -040061
62 // private final String TAG = ContactPictureTask.class.getSimpleName();
alisiond295ec22013-05-17 10:12:13 -040063
Alexandre Lision35c3cbb2013-11-04 17:11:54 -050064 public ContactPictureTask(Context context, ImageView element, CallContact item) {
65 contact = item;
alision84813a12013-05-27 17:40:39 -040066 cr = context.getContentResolver();
67 view = element;
68 }
alisiond295ec22013-05-17 10:12:13 -040069
alision84813a12013-05-27 17:40:39 -040070 public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Alexandre Lisionfed2a642014-01-10 12:05:47 -050071 if(id == -1)
72 return null;
alision84813a12013-05-27 17:40:39 -040073 Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id);
Alexandre Lisionc51ccb12013-09-11 16:00:30 -040074 InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri, true);
alision84813a12013-05-27 17:40:39 -040075 if (input == null) {
76 return null;
77 }
78 return BitmapFactory.decodeStream(input);
79 }
alisiond295ec22013-05-17 10:12:13 -040080
alision84813a12013-05-27 17:40:39 -040081 @Override
82 public void run() {
alision2ec64f92013-06-17 17:28:58 -040083 Bitmap photo_bmp;
Alexandre Lision9b53dc42013-10-04 10:13:46 -040084 try {
Alexandre Lision35c3cbb2013-11-04 17:11:54 -050085 photo_bmp = loadContactPhoto(cr, contact.getId());
Alexandre Lision9b53dc42013-10-04 10:13:46 -040086 } catch (IllegalArgumentException e) {
alision2ec64f92013-06-17 17:28:58 -040087 photo_bmp = null;
88 }
Alexandre Lisionc59685a2013-10-04 16:36:36 -040089
Alexandre Lision5f733bc2013-12-04 13:10:30 -050090 int dpiPadding = (int) (PADDING * view.getResources().getDisplayMetrics().density);
91
alision84813a12013-05-27 17:40:39 -040092 if (photo_bmp == null) {
Alexandre Lision9b53dc42013-10-04 10:13:46 -040093 photo_bmp = decodeSampledBitmapFromResource(view.getResources(), R.drawable.ic_contact_picture, view.getWidth(), view.getHeight());
alision84813a12013-05-27 17:40:39 -040094 }
alisiond295ec22013-05-17 10:12:13 -040095
alision84813a12013-05-27 17:40:39 -040096 int w = photo_bmp.getWidth(), h = photo_bmp.getHeight();
97 if (w > h) {
98 w = h;
99 } else if (h > w) {
100 h = w;
101 }
alisiond295ec22013-05-17 10:12:13 -0400102
alision84813a12013-05-27 17:40:39 -0400103 final Bitmap externalBMP = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400104
Alexandre Lision5dcd0f42013-10-04 14:33:04 -0400105 BitmapShader shader;
106 shader = new BitmapShader(photo_bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400107
Alexandre Lision5dcd0f42013-10-04 14:33:04 -0400108 Paint paint = new Paint();
109 paint.setAntiAlias(true);
110 paint.setShader(shader);
alision84813a12013-05-27 17:40:39 -0400111 Canvas internalCanvas = new Canvas(externalBMP);
Alexandre Lision5f733bc2013-12-04 13:10:30 -0500112
113 Paint paintLine = new Paint();
114 paintLine.setAntiAlias(true);
115 paintLine.setDither(true);
116 paintLine.setStyle(Style.STROKE);
117 paintLine.setColor(Color.WHITE);
Alexandre Lisionbaeeb212013-12-09 12:54:47 -0500118 // internalCanvas.drawCircle(externalBMP.getWidth() / 2, externalBMP.getHeight() / 2, externalBMP.getWidth() / 2 - dpiPadding / 2, paintLine);
119 // internalCanvas.drawOval(new RectF(PADDING, PADDING, externalBMP.getWidth() - dpiPadding, externalBMP.getHeight() - dpiPadding), paint);
120 internalCanvas.drawOval(new RectF(0, 0, externalBMP.getWidth(), externalBMP.getHeight()), paint);
alisiond295ec22013-05-17 10:12:13 -0400121
alision84813a12013-05-27 17:40:39 -0400122 view.post(new Runnable() {
123 @Override
124 public void run() {
125 view.setImageBitmap(externalBMP);
Alexandre Lision35c3cbb2013-11-04 17:11:54 -0500126 contact.setPhoto(externalBMP);
alision84813a12013-05-27 17:40:39 -0400127 view.invalidate();
128 }
129 });
130 }
Alexandre Lisionc59685a2013-10-04 16:36:36 -0400131
132 public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
Alexandre Lision9b53dc42013-10-04 10:13:46 -0400133
134 // First decode with inJustDecodeBounds=true to check dimensions
135 final BitmapFactory.Options options = new BitmapFactory.Options();
Alexandre Lision5f733bc2013-12-04 13:10:30 -0500136 // options.inJustDecodeBounds = true;
137 // BitmapFactory.decodeResource(res, resId, options);
Alexandre Lision9b53dc42013-10-04 10:13:46 -0400138
139 // Calculate inSampleSize
140 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
141
142 // Decode bitmap with inSampleSize set
143 options.inJustDecodeBounds = false;
144 return BitmapFactory.decodeResource(res, resId, options);
145 }
146
147 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
148 // Raw height and width of image
149 final int height = options.outHeight;
150 final int width = options.outWidth;
151 int inSampleSize = 1;
152
153 if (height > reqHeight || width > reqWidth) {
154
155 // Calculate ratios of height and width to requested height and width
156 final int heightRatio = Math.round((float) height / (float) reqHeight);
157 final int widthRatio = Math.round((float) width / (float) reqWidth);
158
159 // Choose the smallest ratio as inSampleSize value, this will guarantee
160 // a final image with both dimensions larger than or equal to the
161 // requested height and width.
162 inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
163 }
164
165 return inSampleSize;
166 }
alisiond295ec22013-05-17 10:12:13 -0400167}