Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 1 | package com.android.contacts.widget; |
| 2 | |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 3 | import com.android.contacts.common.lettertiles.LetterTileDrawable; |
| 4 | |
| 5 | import android.content.Context; |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 6 | import android.graphics.drawable.BitmapDrawable; |
| 7 | import android.graphics.drawable.Drawable; |
| 8 | import android.util.AttributeSet; |
| 9 | import android.widget.ImageView; |
| 10 | |
Brian Attwell | d798078 | 2014-06-24 18:52:26 -0700 | [diff] [blame] | 11 | import com.android.contacts.R; |
| 12 | |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 13 | /** |
Brian Attwell | d798078 | 2014-06-24 18:52:26 -0700 | [diff] [blame] | 14 | * An {@link ImageView} designed to display QuickContact's contact photo. When requested to draw |
| 15 | * {@link LetterTileDrawable}'s, this class instead draws a different default avatar drawable. |
| 16 | * |
| 17 | * In addition to supporting {@link ImageView#setColorFilter} this also supports a {@link #setTint} |
| 18 | * method. |
| 19 | * |
| 20 | * This entire class can be deleted once use of LetterTileDrawable is no longer used |
| 21 | * inside QuickContactsActivity at all. |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 22 | */ |
| 23 | public class QuickContactImageView extends ImageView { |
| 24 | |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 25 | private Drawable mOriginalDrawable; |
Brian Attwell | 5c74864 | 2014-09-03 14:28:53 -0700 | [diff] [blame] | 26 | private BitmapDrawable mBitmapDrawable; |
| 27 | private int mTintColor; |
Brian Attwell | 02eaf11 | 2014-08-27 21:03:42 -0700 | [diff] [blame] | 28 | private boolean mIsBusiness; |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 29 | |
| 30 | public QuickContactImageView(Context context) { |
| 31 | this(context, null); |
| 32 | } |
| 33 | |
| 34 | public QuickContactImageView(Context context, AttributeSet attrs) { |
| 35 | this(context, attrs, 0); |
| 36 | } |
| 37 | |
| 38 | public QuickContactImageView(Context context, AttributeSet attrs, int defStyleAttr) { |
Wenyi Wang | 334bfaf | 2015-12-22 11:08:27 -0800 | [diff] [blame] | 39 | super(context, attrs, defStyleAttr); |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | public void setTint(int color) { |
Brian Attwell | 5c74864 | 2014-09-03 14:28:53 -0700 | [diff] [blame] | 43 | if (mBitmapDrawable == null || mBitmapDrawable.getBitmap() == null |
| 44 | || mBitmapDrawable.getBitmap().hasAlpha()) { |
Brian Attwell | d798078 | 2014-06-24 18:52:26 -0700 | [diff] [blame] | 45 | setBackgroundColor(color); |
| 46 | } else { |
| 47 | setBackground(null); |
| 48 | } |
Brian Attwell | 5c74864 | 2014-09-03 14:28:53 -0700 | [diff] [blame] | 49 | mTintColor = color; |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 50 | postInvalidate(); |
| 51 | } |
| 52 | |
| 53 | public boolean isBasedOffLetterTile() { |
| 54 | return mOriginalDrawable instanceof LetterTileDrawable; |
| 55 | } |
| 56 | |
Brian Attwell | 02eaf11 | 2014-08-27 21:03:42 -0700 | [diff] [blame] | 57 | public void setIsBusiness(boolean isBusiness) { |
| 58 | mIsBusiness = isBusiness; |
| 59 | } |
| 60 | |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 61 | @Override |
| 62 | public void setImageDrawable(Drawable drawable) { |
| 63 | // There is no way to avoid all this casting. Blending modes aren't equally |
| 64 | // supported for all drawable types. |
Brian Attwell | d798078 | 2014-06-24 18:52:26 -0700 | [diff] [blame] | 65 | final BitmapDrawable bitmapDrawable; |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 66 | if (drawable == null || drawable instanceof BitmapDrawable) { |
Brian Attwell | d798078 | 2014-06-24 18:52:26 -0700 | [diff] [blame] | 67 | bitmapDrawable = (BitmapDrawable) drawable; |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 68 | } else if (drawable instanceof LetterTileDrawable) { |
Brian Attwell | 02eaf11 | 2014-08-27 21:03:42 -0700 | [diff] [blame] | 69 | if (!mIsBusiness) { |
| 70 | bitmapDrawable = (BitmapDrawable) getResources().getDrawable( |
| 71 | R.drawable.person_white_540dp); |
| 72 | } else { |
| 73 | bitmapDrawable = (BitmapDrawable) getResources().getDrawable( |
| 74 | R.drawable.generic_business_white_540dp); |
| 75 | } |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 76 | } else { |
| 77 | throw new IllegalArgumentException("Does not support this type of drawable"); |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 78 | } |
Brian Attwell | 5c74864 | 2014-09-03 14:28:53 -0700 | [diff] [blame] | 79 | |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 80 | mOriginalDrawable = drawable; |
Brian Attwell | 5c74864 | 2014-09-03 14:28:53 -0700 | [diff] [blame] | 81 | mBitmapDrawable = bitmapDrawable; |
| 82 | setTint(mTintColor); |
Brian Attwell | d798078 | 2014-06-24 18:52:26 -0700 | [diff] [blame] | 83 | super.setImageDrawable(bitmapDrawable); |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public Drawable getDrawable() { |
| 88 | return mOriginalDrawable; |
| 89 | } |
Brian Attwell | 9b889e6 | 2014-06-23 18:25:32 -0700 | [diff] [blame] | 90 | } |