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