Home | History | Annotate | Download | only in widget
      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         super(context, attrs, defStyleAttr);
     40     }
     41 
     42     public void setTint(int color) {
     43         if (mBitmapDrawable == null || mBitmapDrawable.getBitmap() == null
     44                 || mBitmapDrawable.getBitmap().hasAlpha()) {
     45             setBackgroundColor(color);
     46         } else {
     47             setBackground(null);
     48         }
     49         mTintColor = color;
     50         postInvalidate();
     51     }
     52 
     53     public boolean isBasedOffLetterTile() {
     54         return mOriginalDrawable instanceof LetterTileDrawable;
     55     }
     56 
     57     public void setIsBusiness(boolean isBusiness) {
     58         mIsBusiness = isBusiness;
     59     }
     60 
     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.
     65         final BitmapDrawable bitmapDrawable;
     66         if (drawable == null || drawable instanceof BitmapDrawable) {
     67             bitmapDrawable = (BitmapDrawable) drawable;
     68         } else if (drawable instanceof LetterTileDrawable) {
     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             }
     76         } else {
     77             throw new IllegalArgumentException("Does not support this type of drawable");
     78         }
     79 
     80         mOriginalDrawable = drawable;
     81         mBitmapDrawable = bitmapDrawable;
     82         setTint(mTintColor);
     83         super.setImageDrawable(bitmapDrawable);
     84     }
     85 
     86     @Override
     87     public Drawable getDrawable() {
     88         return mOriginalDrawable;
     89     }
     90 }
     91