summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/browse/StarView.java
blob: 37df73342a0afc24e16f44c2dc3a860850e582f4 (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
package com.android.mail.browse;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.android.mail.R;

/**
 * An image view that respects a custom drawable state (state_starred)
 * that enables a src or background drawable to use to automatically
 * switch between the starred and unstarred state.
 */
public class StarView extends ImageView {

    private static final int[] STATE_STARRED = {R.attr.state_starred};

    private boolean mIsStarred;

    public StarView(Context context) {
        super(context);
    }

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

    public StarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * Set the starred state of the view.
     */
    public void setStarred(boolean isStarred) {
        mIsStarred = isStarred;
        setContentDescription(
                getResources().getString(mIsStarred ? R.string.remove_star : R.string.add_star));
        refreshDrawableState();
    }

    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (mIsStarred) {
            mergeDrawableStates(drawableState, STATE_STARRED);
        }
        return drawableState;
    }
}