1 package com.android.contacts.widget;
2 
3 import android.content.Context;
4 import android.graphics.drawable.BitmapDrawable;
5 import android.graphics.drawable.Drawable;
6 import android.util.AttributeSet;
7 import android.widget.ImageView;
8 
9 import com.android.contacts.R;
10 import com.android.contacts.lettertiles.LetterTileDrawable;
11 
12 /**
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.
21  */
22 public class QuickContactImageView extends ImageView {
23 
24     private Drawable mOriginalDrawable;
25     private BitmapDrawable mBitmapDrawable;
26     private int mTintColor;
27     private boolean mIsBusiness;
28 
QuickContactImageView(Context context)29     public QuickContactImageView(Context context) {
30         this(context, null);
31     }
32 
QuickContactImageView(Context context, AttributeSet attrs)33     public QuickContactImageView(Context context, AttributeSet attrs) {
34         this(context, attrs, 0);
35     }
36 
QuickContactImageView(Context context, AttributeSet attrs, int defStyleAttr)37     public QuickContactImageView(Context context, AttributeSet attrs, int defStyleAttr) {
38         super(context, attrs, defStyleAttr);
39     }
40 
setTint(int color)41     public void setTint(int color) {
42         if (mBitmapDrawable == null || mBitmapDrawable.getBitmap() == null
43                 || mBitmapDrawable.getBitmap().hasAlpha()) {
44             setBackgroundColor(color);
45         } else {
46             setBackground(null);
47         }
48         mTintColor = color;
49         postInvalidate();
50     }
51 
isBasedOffLetterTile()52     public boolean isBasedOffLetterTile() {
53         return mOriginalDrawable instanceof LetterTileDrawable;
54     }
55 
setIsBusiness(boolean isBusiness)56     public void setIsBusiness(boolean isBusiness) {
57         mIsBusiness = isBusiness;
58     }
59 
60     @Override
setImageDrawable(Drawable drawable)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.
64         final BitmapDrawable bitmapDrawable;
65         if (drawable == null || drawable instanceof BitmapDrawable) {
66             bitmapDrawable = (BitmapDrawable) drawable;
67         } else if (drawable instanceof LetterTileDrawable) {
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             }
75         } else {
76             throw new IllegalArgumentException("Does not support this type of drawable");
77         }
78 
79         mOriginalDrawable = drawable;
80         mBitmapDrawable = bitmapDrawable;
81         setTint(mTintColor);
82         super.setImageDrawable(bitmapDrawable);
83     }
84 
85     @Override
getDrawable()86     public Drawable getDrawable() {
87         return mOriginalDrawable;
88     }
89 }
90