/
StyleResolver.java
173 lines (149 loc) · 5.21 KB
/
StyleResolver.java
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
package org.robolectric.res;
import org.robolectric.util.Strings;
import java.util.ArrayList;
import java.util.List;
public class StyleResolver implements Style {
private final ResourceLoader resourceLoader;
private final List<StyleData> styles = new ArrayList<>();
private final Style theme;
private final ResName myResName;
private final String qualifiers;
public StyleResolver(ResourceLoader resourceLoader, StyleData styleData,
Style theme, ResName myResName, String qualifiers) {
this.resourceLoader = resourceLoader;
this.theme = theme;
this.myResName = myResName;
this.qualifiers = qualifiers;
styles.add(styleData);
}
@Override public AttributeResource getAttrValue(ResName resName) {
for (StyleData style : styles) {
AttributeResource value = style.getAttrValue(resName);
if (value != null) return value;
}
int initialSize = styles.size();
while (hasParent(styles.get(styles.size() - 1))) {
StyleData parent = getParent(styles.get(styles.size() - 1));
if (parent != null) {
styles.add(parent);
} else {
break;
}
}
for (int i = initialSize; i < styles.size(); i++) {
StyleData style = styles.get(i);
AttributeResource value = style.getAttrValue(resName);
if (value != null) return value;
}
if (theme != null) {
AttributeResource value = theme.getAttrValue(resName);
if (value != null) return value;
}
return null;
}
private static String getParentStyleName(StyleData style) {
if (style == null) {
return null;
}
String parent = style.getParent();
if (parent == null || parent.isEmpty()) {
parent = null;
String name = style.getName();
if (name.contains(".")) {
parent = name.substring(0, name.lastIndexOf('.'));
if (parent.isEmpty()) {
return null;
}
}
}
return parent;
}
private static boolean hasParent(StyleData style) {
if (style == null) return false;
String parent = style.getParent();
return parent != null && !parent.isEmpty();
}
private StyleData getParent(StyleData style) {
String parent = getParentStyleName(style);
if (parent == null || "Null".equals(parent)) return null;
if (parent.startsWith("@")) parent = parent.substring(1);
ResName styleRef = ResName.qualifyResName(parent, style.getPackageName(), "style");
styleRef = dereferenceResName(styleRef);
TypedResource typedResource = resourceLoader.getValue(styleRef, qualifiers);
if (typedResource == null) {
StringBuilder builder = new StringBuilder("Could not find any resource ")
.append(" from reference ").append(styleRef)
.append(" from style ").append(style)
.append(" with theme ").append(theme);
throw new RuntimeException(builder.toString());
}
Object data = typedResource.getData();
if (data instanceof StyleData) {
return (StyleData) data;
} else {
StringBuilder builder = new StringBuilder(styleRef.toString())
.append(" does not resolve to a Style.")
.append(" got ").append(data).append(" instead. ")
.append(" from style ").append(style)
.append(" with theme ").append(theme);
throw new RuntimeException(builder.toString());
}
}
private ResName dereferenceResName(ResName res) {
ResName styleRef = res;
boolean dereferencing = true;
while ("attr".equals(styleRef.type) && dereferencing) {
dereferencing = false;
for (StyleData parentStyle : styles) {
AttributeResource value = parentStyle.getAttrValue(styleRef);
if (value != null) {
styleRef = dereferenceAttr(value);
dereferencing = true;
break;
}
}
if (!dereferencing && theme != null) {
AttributeResource value = theme.getAttrValue(styleRef);
if (value != null) {
styleRef = dereferenceAttr(value);
dereferencing = true;
}
}
}
return styleRef;
}
private ResName dereferenceAttr(AttributeResource attr) {
if (attr.isResourceReference()) {
return attr.getResourceReference();
} else if (attr.isStyleReference()) {
return attr.getStyleReference();
}
throw new RuntimeException("Found a " + attr + " but can't cast it :(");
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof StyleResolver)) {
return false;
}
StyleResolver other = (StyleResolver) obj;
return ((theme == null && other.theme == null) || (theme != null && theme.equals(other.theme)))
&& ((myResName == null && other.myResName == null)
|| (myResName != null && myResName.equals(other.myResName)))
&& Strings.equals(qualifiers, other.qualifiers);
}
@Override
public int hashCode() {
int hashCode = 0;
hashCode = 31 * hashCode + (theme != null ? theme.hashCode() : 0);
hashCode = 31 * hashCode + (myResName != null ? myResName.hashCode() : 0);
hashCode = 31 * hashCode + Strings.nullToEmpty(qualifiers).hashCode();
return hashCode;
}
@Override
public String toString() {
return "StyleResolver{"
+ "name=" + myResName
+ ", of=" + styles.get(0)
+ "}";
}
}