blob: 2943912c47f10739de8bc0eb54daf23490a2101f [file] [log] [blame]
Brian Attwell9b889e62014-06-23 18:25:32 -07001package com.android.contacts.widget;
2
Brian Attwell9b889e62014-06-23 18:25:32 -07003import android.content.Context;
Brian Attwell9b889e62014-06-23 18:25:32 -07004import android.graphics.drawable.BitmapDrawable;
5import android.graphics.drawable.Drawable;
6import android.util.AttributeSet;
7import android.widget.ImageView;
8
Brian Attwelld7980782014-06-24 18:52:26 -07009import com.android.contacts.R;
Gary Mai0a49afa2016-12-05 15:53:58 -080010import com.android.contacts.lettertiles.LetterTileDrawable;
Brian Attwelld7980782014-06-24 18:52:26 -070011
Brian Attwell9b889e62014-06-23 18:25:32 -070012/**
Brian Attwelld7980782014-06-24 18:52:26 -070013 * 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 Attwell9b889e62014-06-23 18:25:32 -070021 */
22public class QuickContactImageView extends ImageView {
23
Brian Attwell9b889e62014-06-23 18:25:32 -070024 private Drawable mOriginalDrawable;
Brian Attwell5c748642014-09-03 14:28:53 -070025 private BitmapDrawable mBitmapDrawable;
26 private int mTintColor;
Brian Attwell02eaf112014-08-27 21:03:42 -070027 private boolean mIsBusiness;
Brian Attwell9b889e62014-06-23 18:25:32 -070028
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 Wang334bfaf2015-12-22 11:08:27 -080038 super(context, attrs, defStyleAttr);
Brian Attwell9b889e62014-06-23 18:25:32 -070039 }
40
41 public void setTint(int color) {
Brian Attwell5c748642014-09-03 14:28:53 -070042 if (mBitmapDrawable == null || mBitmapDrawable.getBitmap() == null
43 || mBitmapDrawable.getBitmap().hasAlpha()) {
Brian Attwelld7980782014-06-24 18:52:26 -070044 setBackgroundColor(color);
45 } else {
46 setBackground(null);
47 }
Brian Attwell5c748642014-09-03 14:28:53 -070048 mTintColor = color;
Brian Attwell9b889e62014-06-23 18:25:32 -070049 postInvalidate();
50 }
51
52 public boolean isBasedOffLetterTile() {
53 return mOriginalDrawable instanceof LetterTileDrawable;
54 }
55
Brian Attwell02eaf112014-08-27 21:03:42 -070056 public void setIsBusiness(boolean isBusiness) {
57 mIsBusiness = isBusiness;
58 }
59
Brian Attwell9b889e62014-06-23 18:25:32 -070060 @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 Attwelld7980782014-06-24 18:52:26 -070064 final BitmapDrawable bitmapDrawable;
Brian Attwell9b889e62014-06-23 18:25:32 -070065 if (drawable == null || drawable instanceof BitmapDrawable) {
Brian Attwelld7980782014-06-24 18:52:26 -070066 bitmapDrawable = (BitmapDrawable) drawable;
Brian Attwell9b889e62014-06-23 18:25:32 -070067 } else if (drawable instanceof LetterTileDrawable) {
Brian Attwell02eaf112014-08-27 21:03:42 -070068 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 Attwell9b889e62014-06-23 18:25:32 -070075 } else {
76 throw new IllegalArgumentException("Does not support this type of drawable");
Brian Attwell9b889e62014-06-23 18:25:32 -070077 }
Brian Attwell5c748642014-09-03 14:28:53 -070078
Brian Attwell9b889e62014-06-23 18:25:32 -070079 mOriginalDrawable = drawable;
Brian Attwell5c748642014-09-03 14:28:53 -070080 mBitmapDrawable = bitmapDrawable;
81 setTint(mTintColor);
Brian Attwelld7980782014-06-24 18:52:26 -070082 super.setImageDrawable(bitmapDrawable);
Brian Attwell9b889e62014-06-23 18:25:32 -070083 }
84
85 @Override
86 public Drawable getDrawable() {
87 return mOriginalDrawable;
88 }
Brian Attwell9b889e62014-06-23 18:25:32 -070089}