summaryrefslogtreecommitdiffstats
path: root/src/com/android/contacts/common/widget/CheckableQuickContactBadge.java
blob: 85160569dba5fb742c0fe4f5d8ecfa4ffaee6183 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.android.contacts.common.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.QuickContactBadge;

import com.android.contacts.common.R;

public class CheckableQuickContactBadge extends QuickContactBadge implements Checkable {
    private boolean mChecked = false;
    private int mCheckMarkBackgroundColor;
    private CheckableFlipDrawable mDrawable;

    public CheckableQuickContactBadge(Context context) {
        super(context);
        init(context);
    }

    public CheckableQuickContactBadge(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CheckableQuickContactBadge(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public CheckableQuickContactBadge(Context context, AttributeSet attrs,
            int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context) {
        TypedArray a = context.obtainStyledAttributes(android.R.styleable.Theme);
        setCheckMarkBackgroundColor(a.getColor(android.R.styleable.Theme_colorPrimary,
                context.getResources().getColor(R.color.people_app_theme_color)));
        a.recycle();
    }

    public void setCheckMarkBackgroundColor(int color) {
        mCheckMarkBackgroundColor = color;
        if (mDrawable != null) {
            mDrawable.setCheckMarkBackgroundColor(color);
        }
    }

    public void toggle() {
        setChecked(!mChecked);
    }

    @Override
    public boolean isChecked() {
        return mChecked;
    }

    @Override
    public void setChecked(boolean checked) {
        setChecked(checked, true);
    }

    public void setChecked(boolean checked, boolean animate) {
        if (mChecked == checked) {
            return;
        }

        mChecked = checked;

        Drawable d = getDrawable();
        if (d instanceof CheckableFlipDrawable) {
            CheckableFlipDrawable cfd = (CheckableFlipDrawable) d;
            cfd.flipTo(!mChecked);
            if (!animate) {
                cfd.reset();
            }
        }
    }

    @Override
    public void setImageDrawable(Drawable d) {
        if (d != null) {
            if (mDrawable == null) {
                mDrawable = new CheckableFlipDrawable(d, getResources(),
                        mCheckMarkBackgroundColor, 150);
            } else {
                int oldWidth = mDrawable.getIntrinsicWidth();
                int oldHeight = mDrawable.getIntrinsicHeight();
                mDrawable.setFront(d);
                if (oldWidth != mDrawable.getIntrinsicWidth()
                        || oldHeight != mDrawable.getIntrinsicHeight()) {
                    // enforce drawable size update + layout
                    super.setImageDrawable(null);
                }
            }
            d = mDrawable;
        }
        super.setImageDrawable(d);
    }
}