blob: f85fe167066ffa021269fe1d4a49853bea9ae94f [file] [log] [blame]
Brian Attwell9b889e62014-06-23 18:25:32 -07001package com.android.contacts.widget;
2
Brian Attwell9b889e62014-06-23 18:25:32 -07003import com.android.contacts.common.lettertiles.LetterTileDrawable;
4
5import android.content.Context;
Brian Attwell9b889e62014-06-23 18:25:32 -07006import android.graphics.drawable.BitmapDrawable;
7import android.graphics.drawable.Drawable;
8import android.util.AttributeSet;
9import android.widget.ImageView;
10
Brian Attwelld7980782014-06-24 18:52:26 -070011import com.android.contacts.R;
12
Brian Attwell9b889e62014-06-23 18:25:32 -070013/**
Brian Attwelld7980782014-06-24 18:52:26 -070014 * 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 Attwell9b889e62014-06-23 18:25:32 -070022 */
23public class QuickContactImageView extends ImageView {
24
Brian Attwell9b889e62014-06-23 18:25:32 -070025 private Drawable mOriginalDrawable;
Brian Attwell5c748642014-09-03 14:28:53 -070026 private BitmapDrawable mBitmapDrawable;
27 private int mTintColor;
Brian Attwell02eaf112014-08-27 21:03:42 -070028 private boolean mIsBusiness;
Brian Attwell9b889e62014-06-23 18:25:32 -070029
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 Wang334bfaf2015-12-22 11:08:27 -080039 super(context, attrs, defStyleAttr);
Brian Attwell9b889e62014-06-23 18:25:32 -070040 }
41
42 public void setTint(int color) {
Brian Attwell5c748642014-09-03 14:28:53 -070043 if (mBitmapDrawable == null || mBitmapDrawable.getBitmap() == null
44 || mBitmapDrawable.getBitmap().hasAlpha()) {
Brian Attwelld7980782014-06-24 18:52:26 -070045 setBackgroundColor(color);
46 } else {
47 setBackground(null);
48 }
Brian Attwell5c748642014-09-03 14:28:53 -070049 mTintColor = color;
Brian Attwell9b889e62014-06-23 18:25:32 -070050 postInvalidate();
51 }
52
53 public boolean isBasedOffLetterTile() {
54 return mOriginalDrawable instanceof LetterTileDrawable;
55 }
56
Brian Attwell02eaf112014-08-27 21:03:42 -070057 public void setIsBusiness(boolean isBusiness) {
58 mIsBusiness = isBusiness;
59 }
60
Brian Attwell9b889e62014-06-23 18:25:32 -070061 @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 Attwelld7980782014-06-24 18:52:26 -070065 final BitmapDrawable bitmapDrawable;
Brian Attwell9b889e62014-06-23 18:25:32 -070066 if (drawable == null || drawable instanceof BitmapDrawable) {
Brian Attwelld7980782014-06-24 18:52:26 -070067 bitmapDrawable = (BitmapDrawable) drawable;
Brian Attwell9b889e62014-06-23 18:25:32 -070068 } else if (drawable instanceof LetterTileDrawable) {
Brian Attwell02eaf112014-08-27 21:03:42 -070069 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 Attwell9b889e62014-06-23 18:25:32 -070076 } else {
77 throw new IllegalArgumentException("Does not support this type of drawable");
Brian Attwell9b889e62014-06-23 18:25:32 -070078 }
Brian Attwell5c748642014-09-03 14:28:53 -070079
Brian Attwell9b889e62014-06-23 18:25:32 -070080 mOriginalDrawable = drawable;
Brian Attwell5c748642014-09-03 14:28:53 -070081 mBitmapDrawable = bitmapDrawable;
82 setTint(mTintColor);
Brian Attwelld7980782014-06-24 18:52:26 -070083 super.setImageDrawable(bitmapDrawable);
Brian Attwell9b889e62014-06-23 18:25:32 -070084 }
85
86 @Override
87 public Drawable getDrawable() {
88 return mOriginalDrawable;
89 }
Brian Attwell9b889e62014-06-23 18:25:32 -070090}