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 | |
Alexandre Lision | 064e1e0 | 2013-10-01 16:18:42 -0400 | [diff] [blame] | 32 | package org.sflphone.adapters; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 33 | |
| 34 | import java.io.InputStream; |
| 35 | |
Alexandre Lision | 5dcd0f4 | 2013-10-04 14:33:04 -0400 | [diff] [blame] | 36 | import org.sflphone.R; |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 37 | import org.sflphone.model.CallContact; |
Alexandre Lision | 5dcd0f4 | 2013-10-04 14:33:04 -0400 | [diff] [blame] | 38 | |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 39 | import android.content.ContentResolver; |
| 40 | import android.content.ContentUris; |
| 41 | import android.content.Context; |
Alexandre Lision | 9b53dc4 | 2013-10-04 10:13:46 -0400 | [diff] [blame] | 42 | import android.content.res.Resources; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 43 | import android.graphics.Bitmap; |
| 44 | import android.graphics.BitmapFactory; |
Alexandre Lision | 5dcd0f4 | 2013-10-04 14:33:04 -0400 | [diff] [blame] | 45 | import android.graphics.BitmapShader; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 46 | import android.graphics.Canvas; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 47 | import android.graphics.Paint; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 48 | import android.graphics.RectF; |
Alexandre Lision | 5dcd0f4 | 2013-10-04 14:33:04 -0400 | [diff] [blame] | 49 | import android.graphics.Shader; |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 50 | import android.net.Uri; |
| 51 | import android.provider.ContactsContract; |
| 52 | import android.widget.ImageView; |
| 53 | |
Alexandre Lision | 6e8931e | 2013-09-19 16:49:34 -0400 | [diff] [blame] | 54 | public class ContactPictureTask implements Runnable { |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 55 | private ImageView view; |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 56 | private CallContact contact; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 57 | private ContentResolver cr; |
Alexandre Lision | 9b53dc4 | 2013-10-04 10:13:46 -0400 | [diff] [blame] | 58 | |
| 59 | // private final String TAG = ContactPictureTask.class.getSimpleName(); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 60 | |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 61 | public ContactPictureTask(Context context, ImageView element, CallContact item) { |
| 62 | contact = item; |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 63 | cr = context.getContentResolver(); |
| 64 | view = element; |
| 65 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 66 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 67 | public static Bitmap loadContactPhoto(ContentResolver cr, long id) { |
| 68 | Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id); |
Alexandre Lision | c51ccb1 | 2013-09-11 16:00:30 -0400 | [diff] [blame] | 69 | InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(cr, uri, true); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 70 | if (input == null) { |
| 71 | return null; |
| 72 | } |
| 73 | return BitmapFactory.decodeStream(input); |
| 74 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 75 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 76 | @Override |
| 77 | public void run() { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 78 | Bitmap photo_bmp; |
Alexandre Lision | 9b53dc4 | 2013-10-04 10:13:46 -0400 | [diff] [blame] | 79 | try { |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 80 | photo_bmp = loadContactPhoto(cr, contact.getId()); |
Alexandre Lision | 9b53dc4 | 2013-10-04 10:13:46 -0400 | [diff] [blame] | 81 | } catch (IllegalArgumentException e) { |
alision | 2ec64f9 | 2013-06-17 17:28:58 -0400 | [diff] [blame] | 82 | photo_bmp = null; |
| 83 | } |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 84 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 85 | if (photo_bmp == null) { |
Alexandre Lision | 9b53dc4 | 2013-10-04 10:13:46 -0400 | [diff] [blame] | 86 | photo_bmp = decodeSampledBitmapFromResource(view.getResources(), R.drawable.ic_contact_picture, view.getWidth(), view.getHeight()); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 87 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 88 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 89 | int w = photo_bmp.getWidth(), h = photo_bmp.getHeight(); |
| 90 | if (w > h) { |
| 91 | w = h; |
| 92 | } else if (h > w) { |
| 93 | h = w; |
| 94 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 95 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 96 | final Bitmap externalBMP = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 97 | |
Alexandre Lision | 5dcd0f4 | 2013-10-04 14:33:04 -0400 | [diff] [blame] | 98 | BitmapShader shader; |
| 99 | shader = new BitmapShader(photo_bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 100 | |
Alexandre Lision | 5dcd0f4 | 2013-10-04 14:33:04 -0400 | [diff] [blame] | 101 | Paint paint = new Paint(); |
| 102 | paint.setAntiAlias(true); |
| 103 | paint.setShader(shader); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 104 | Canvas internalCanvas = new Canvas(externalBMP); |
Alexandre Lision | 5dcd0f4 | 2013-10-04 14:33:04 -0400 | [diff] [blame] | 105 | internalCanvas.drawOval(new RectF(0, 0, w, h), paint); |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 106 | |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 107 | view.post(new Runnable() { |
| 108 | @Override |
| 109 | public void run() { |
| 110 | view.setImageBitmap(externalBMP); |
Alexandre Lision | 35c3cbb | 2013-11-04 17:11:54 -0500 | [diff] [blame] | 111 | contact.setPhoto(externalBMP); |
alision | 84813a1 | 2013-05-27 17:40:39 -0400 | [diff] [blame] | 112 | view.invalidate(); |
| 113 | } |
| 114 | }); |
| 115 | } |
Alexandre Lision | c59685a | 2013-10-04 16:36:36 -0400 | [diff] [blame] | 116 | |
| 117 | public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { |
Alexandre Lision | 9b53dc4 | 2013-10-04 10:13:46 -0400 | [diff] [blame] | 118 | |
| 119 | // First decode with inJustDecodeBounds=true to check dimensions |
| 120 | final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 121 | options.inJustDecodeBounds = true; |
| 122 | BitmapFactory.decodeResource(res, resId, options); |
| 123 | |
| 124 | // Calculate inSampleSize |
| 125 | options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); |
| 126 | |
| 127 | // Decode bitmap with inSampleSize set |
| 128 | options.inJustDecodeBounds = false; |
| 129 | return BitmapFactory.decodeResource(res, resId, options); |
| 130 | } |
| 131 | |
| 132 | public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { |
| 133 | // Raw height and width of image |
| 134 | final int height = options.outHeight; |
| 135 | final int width = options.outWidth; |
| 136 | int inSampleSize = 1; |
| 137 | |
| 138 | if (height > reqHeight || width > reqWidth) { |
| 139 | |
| 140 | // Calculate ratios of height and width to requested height and width |
| 141 | final int heightRatio = Math.round((float) height / (float) reqHeight); |
| 142 | final int widthRatio = Math.round((float) width / (float) reqWidth); |
| 143 | |
| 144 | // Choose the smallest ratio as inSampleSize value, this will guarantee |
| 145 | // a final image with both dimensions larger than or equal to the |
| 146 | // requested height and width. |
| 147 | inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; |
| 148 | } |
| 149 | |
| 150 | return inSampleSize; |
| 151 | } |
alision | d295ec2 | 2013-05-17 10:12:13 -0400 | [diff] [blame] | 152 | } |