1 package com.android.contacts.widget; 2 3 import com.android.contacts.common.lettertiles.LetterTileDrawable; 4 5 import android.content.Context; 6 import android.graphics.drawable.BitmapDrawable; 7 import android.graphics.drawable.Drawable; 8 import android.util.AttributeSet; 9 import android.widget.ImageView; 10 11 import com.android.contacts.R; 12 13 /** 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. 22 */ 23 public class QuickContactImageView extends ImageView { 24 25 private Drawable mOriginalDrawable; 26 private BitmapDrawable mBitmapDrawable; 27 private int mTintColor; 28 private boolean mIsBusiness; 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) { 39 this(context, attrs, defStyleAttr, 0); 40 } 41 42 public QuickContactImageView(Context context, AttributeSet attrs, int defStyleAttr, 43 int defStyleRes) { 44 super(context, attrs, defStyleAttr, defStyleRes); 45 } 46 47 public void setTint(int color) { 48 if (mBitmapDrawable == null || mBitmapDrawable.getBitmap() == null 49 || mBitmapDrawable.getBitmap().hasAlpha()) { 50 setBackgroundColor(color); 51 } else { 52 setBackground(null); 53 } 54 mTintColor = color; 55 postInvalidate(); 56 } 57 58 public boolean isBasedOffLetterTile() { 59 return mOriginalDrawable instanceof LetterTileDrawable; 60 } 61 62 public void setIsBusiness(boolean isBusiness) { 63 mIsBusiness = isBusiness; 64 } 65 66 @Override 67 public void setImageDrawable(Drawable drawable) { 68 // There is no way to avoid all this casting. Blending modes aren't equally 69 // supported for all drawable types. 70 final BitmapDrawable bitmapDrawable; 71 if (drawable == null || drawable instanceof BitmapDrawable) { 72 bitmapDrawable = (BitmapDrawable) drawable; 73 } else if (drawable instanceof LetterTileDrawable) { 74 if (!mIsBusiness) { 75 bitmapDrawable = (BitmapDrawable) getResources().getDrawable( 76 R.drawable.person_white_540dp); 77 } else { 78 bitmapDrawable = (BitmapDrawable) getResources().getDrawable( 79 R.drawable.generic_business_white_540dp); 80 } 81 } else { 82 throw new IllegalArgumentException("Does not support this type of drawable"); 83 } 84 85 mOriginalDrawable = drawable; 86 mBitmapDrawable = bitmapDrawable; 87 setTint(mTintColor); 88 super.setImageDrawable(bitmapDrawable); 89 } 90 91 @Override 92 public Drawable getDrawable() { 93 return mOriginalDrawable; 94 } 95 } 96