aboutsummaryrefslogtreecommitdiff
path: root/v1/src/main/java/com/xtremelabs/robolectric/res/DrawableResourceLoader.java
blob: bac82e1645c034b7c028ec39b0390e819584fe8f (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.xtremelabs.robolectric.res;

import android.R;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.StateListDrawable;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.shadows.ShadowStateListDrawable;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * DrawableResourceLoader
 */
public class DrawableResourceLoader extends XmlLoader {

    // Put all the states for a StateListDrawable in the into a Map for looking up
    // http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
    private static final Map<String, Integer> stateMap = new HashMap<String, Integer>();
    static {
        stateMap.put("android:state_selected", R.attr.state_selected);
        stateMap.put("android:state_pressed", R.attr.state_pressed);
        stateMap.put("android:state_focused", R.attr.state_focused);
        stateMap.put("android:state_checkable", R.attr.state_checkable);
        stateMap.put("android:state_checked", R.attr.state_checked);
        stateMap.put("android:state_enabled", R.attr.state_enabled);
        stateMap.put("android:state_window_focused", R.attr.state_window_focused);
    }

    /** document */
    protected Map<String, Document> documents = new HashMap<String, Document>();

    /** resource directory */
    protected File resourceDirectory;

    /**
     * DrawableResourceLoader constructor.
     *
     * @param extractor         Extractor
     * @param resourceDirectory Resource directory
     */
    public DrawableResourceLoader(ResourceExtractor extractor, File resourceDirectory) {
        super(extractor);
        this.resourceDirectory = resourceDirectory;
    }

    /**
     * Check if resource is xml.
     *
     * @param resourceId Resource id
     * @return Boolean
     */
    public boolean isXml(int resourceId) {
        return documents.containsKey(resourceExtractor.getResourceName(resourceId));
    }

    public Drawable getXmlDrawable(int resId) {

        if (!isXml(resId)) {
            return null;
        }

        Document xmlDoc = documents.get(resourceExtractor.getResourceName(resId));
        NodeList nodes = xmlDoc.getElementsByTagName("selector");
        if (nodes != null && nodes.getLength() > 0) {
            return buildStateListDrawable(xmlDoc);
        }

        nodes = xmlDoc.getElementsByTagName("layer-list");
        if (nodes != null && nodes.getLength() > 0) {
            return new LayerDrawable(null);
        }

        nodes = xmlDoc.getElementsByTagName("animation-list");
        if (nodes != null && nodes.getLength() > 0) {
            return new AnimationDrawable();
        }

        return null;
    }

    /**
     * Store document locally keyed by resource name.
     *
     * @param xmlFile  Xml file
     * @param document Document
     * @param isSystem System resource
     * @throws Exception
     * @see com.xtremelabs.robolectric.res.XmlLoader#processResourceXml(java.io.File,
     *      org.w3c.dom.Document, boolean)
     */
    @Override
    protected void processResourceXml(File xmlFile, Document document, boolean isSystem) throws Exception {
        String name = toResourceName(xmlFile);
        if (!documents.containsKey(name)) {
            if (isSystem) {
                name = "android:" + name;
            }
            documents.put(name, document);
        }
    }

    /**
     * Convert file name to resource name.
     *
     * @param xmlFile Xml File
     * @return Resource name
     */
    private String toResourceName(File xmlFile) {
        try {
            return xmlFile.getCanonicalPath().replaceAll("[/\\\\\\\\]", "/")
                    .replaceAll("^.*?/res/", "").replaceAll("\\..+$", "");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * Get drawables by resource id.
     *
     * @param resourceId Resource id
     * @return Drawables
     */
    protected int[] getDrawableIds(int resourceId) {
        String resourceName = resourceExtractor.getResourceName(resourceId);
        Document document = documents.get(resourceName);

        NodeList items = document.getElementsByTagName("item");
        int[] drawableIds = new int[items.getLength()];

        for (int i = 0; i < items.getLength(); i++) {
            if (resourceName.startsWith("android:")) {
                drawableIds[i] = -1;
            } else {
                Node item = items.item(i);
                Node drawableName = item.getAttributes().getNamedItem("android:drawable");
                if (drawableName != null) {
                    drawableIds[i] = resourceExtractor.getResourceId(drawableName.getNodeValue());
                }
            }
        }

        return drawableIds;
    }

    public boolean isAnimationDrawable(int resourceId) {
        Document document = documents.get(resourceExtractor.getResourceName(resourceId));
        return "animation-list".equals(document.getDocumentElement().getLocalName());
    }

    private StateListDrawable buildStateListDrawable(Document d) {
        StateListDrawable drawable = new StateListDrawable();
        ShadowStateListDrawable shDrawable = Robolectric.shadowOf(drawable);
        NodeList items = d.getElementsByTagName("item");
        for (int i = 0; i < items.getLength(); i++) {
            Node node = items.item(i);
            Node drawableName = node.getAttributes().getNamedItem("android:drawable");
            if (drawableName != null) {
                int resId = resourceExtractor.getResourceId(drawableName.getNodeValue());
                int stateId = getStateId(node);
                shDrawable.addState(stateId, resId);
            }
        }
        return drawable;
    }

    private int getStateId(Node node) {
        NamedNodeMap attrs = node.getAttributes();
        for (String state : stateMap.keySet()) {
            Node attr = attrs.getNamedItem(state);
            if (attr != null) {
                return stateMap.get(state);
            }
        }

        // if a state wasn't specified, return the default state
        return R.attr.state_active;
    }
}