aboutsummaryrefslogtreecommitdiff
path: root/v1/src/main/java/com/xtremelabs/robolectric/shadows/ShadowStateListDrawable.java
blob: 8922af8f6680383d0cf63bbc1e77381c8f1086bc (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
package com.xtremelabs.robolectric.shadows;

import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.StateSet;
import com.xtremelabs.robolectric.internal.Implementation;
import com.xtremelabs.robolectric.internal.Implements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Implements(StateListDrawable.class)
public class ShadowStateListDrawable extends ShadowDrawable {

    private Map<Integer, Integer> stateToResource;
    private Map<List<Integer>, Drawable> stateToDrawable;

    public void __constructor__() {
        stateToResource = new HashMap<Integer, Integer>();
        stateToDrawable = new HashMap<List<Integer>, Drawable>();
    }

    public void addState(int stateId, int resId) {
        stateToResource.put(stateId, resId);
    }

    public int getResourceIdForState(int stateId) {
        return stateToResource.get(stateId);
    }

    @Implementation
    public void addState(int[] stateSet, Drawable drawable) {
        stateToDrawable.put(createStateList(stateSet), drawable);
    }

    /**
     * Non Android accessor to retrieve drawable added for a specific state.
     *
     * @param stateSet Int array describing the state
     * @return Drawable added via {@link #addState(int[], android.graphics.drawable.Drawable)}
     */
    public Drawable getDrawableForState(int[] stateSet) {
        return stateToDrawable.get(createStateList(stateSet));
    }

    private List<Integer> createStateList(int[] stateSet) {
        List<Integer> stateList = new ArrayList<Integer>();
        if (stateSet == StateSet.WILD_CARD) {
            stateList.add(-1);
        } else {
            for (int state : stateSet) {
                stateList.add(state);
            }
        }

        return stateList;
    }
}