/
ShadowAssetManager.java
802 lines (687 loc) · 30.7 KB
/
ShadowAssetManager.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
package org.robolectric.shadows;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.XmlResourceParser;
import android.os.ParcelFileDescriptor;
import android.util.AttributeSet;
import android.util.TypedValue;
import org.jetbrains.annotations.NotNull;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.HiddenApi;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.annotation.Resetter;
import org.robolectric.res.*;
import org.robolectric.res.builder.ResourceParser;
import org.robolectric.res.builder.XmlBlock;
import org.robolectric.util.Logger;
import org.robolectric.util.ReflectionHelpers;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import static android.os.Build.VERSION_CODES.KITKAT_WATCH;
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static org.robolectric.RuntimeEnvironment.castNativePtr;
import static org.robolectric.Shadows.shadowOf;
/**
* Shadow for {@link android.content.res.AssetManager}.
*/
@Implements(AssetManager.class)
public final class ShadowAssetManager {
public static final int STYLE_NUM_ENTRIES = 6;
public static final int STYLE_TYPE = 0;
public static final int STYLE_DATA = 1;
public static final int STYLE_ASSET_COOKIE = 2;
public static final int STYLE_RESOURCE_ID = 3;
public static final int STYLE_CHANGING_CONFIGURATIONS = 4;
public static final int STYLE_DENSITY = 5;
boolean strictErrors = false;
private static long nextInternalThemeId = 1000;
private static final Map<Long, NativeTheme> nativeThemes = new HashMap<>();
private ResourceLoader resourceLoader;
class NativeTheme {
private ThemeStyleSet themeStyleSet;
public NativeTheme(ThemeStyleSet themeStyleSet) {
this.themeStyleSet = themeStyleSet;
}
public ShadowAssetManager getShadowAssetManager() {
return ShadowAssetManager.this;
}
}
@RealObject
AssetManager realObject;
private void convertAndFill(AttributeResource attribute, TypedValue outValue, String qualifiers, boolean resolveRefs) {
if (attribute.isNull()) {
outValue.type = TypedValue.TYPE_NULL;
outValue.data = TypedValue.DATA_NULL_UNDEFINED;
return;
} else if (attribute.isEmpty()) {
outValue.type = TypedValue.TYPE_NULL;
outValue.data = TypedValue.DATA_NULL_EMPTY;
return;
}
// short-circuit Android caching of loaded resources cuz our string positions don't remain stable...
outValue.assetCookie = Converter.getNextStringCookie();
// TODO: Handle resource and style references
if (attribute.isStyleReference()) {
return;
}
while (attribute.isResourceReference()) {
ResName resName = attribute.getResourceReference();
Integer resourceId = getResourceLoader(resName).getResourceIndex().getResourceId(resName);
if (resourceId == null) {
throw new Resources.NotFoundException("unknown resource " + resName);
}
outValue.type = TypedValue.TYPE_REFERENCE;
outValue.resourceId = resourceId;
TypedResource dereferencedRef = getResourceLoader(resName).getValue(resName, qualifiers);
if (dereferencedRef == null) {
Logger.strict("couldn't resolve %s from %s", resName.getFullyQualifiedName(), attribute);
if (resName.type.equals("id")) {
return;
} else if (resName.type.equals("layout")) {
return; // resourceId is good enough, right?
} else if (resName.type.equals("dimen")) {
return;
} else if (resName.type.equals("transition")) {
return;
} else if (resName.type.equals("interpolator")) {
return;
} else if (resName.type.equals("menu")) {
return;
} else if (resName.type.equals("raw")) {
return;
} else if (DrawableResourceLoader.isStillHandledHere(resName.type)) {
// wtf. color and drawable references reference are all kinds of stupid.
TypedResource drawableResource = getResourceLoader(resName).getValue(resName, qualifiers);
if (drawableResource == null) {
throw new Resources.NotFoundException("can't find file for " + resName);
} else {
outValue.type = TypedValue.TYPE_STRING;
outValue.data = 0;
outValue.assetCookie = Converter.getNextStringCookie();
outValue.string = (CharSequence) drawableResource.getData();
return;
}
} else {
throw new RuntimeException("huh? " + resName);
}
} else {
if (dereferencedRef.isFile()) {
outValue.type = TypedValue.TYPE_STRING;
outValue.data = 0;
outValue.assetCookie = Converter.getNextStringCookie();
outValue.string = dereferencedRef.asString();
return;
} else if (dereferencedRef.getData() instanceof String) {
attribute = new AttributeResource(attribute.resName, dereferencedRef.asString(), resName.packageName);
if (attribute.isResourceReference()) {
continue;
}
if (resolveRefs) {
Converter.getConverter(dereferencedRef.getResType()).fillTypedValue(attribute.value, outValue);
return;
}
}
}
break;
}
if (attribute.isNull()) {
outValue.type = TypedValue.TYPE_NULL;
return;
}
TypedResource attrTypeData = getResourceLoader(attribute.resName).getValue(attribute.resName, qualifiers);
if (attrTypeData != null) {
AttrData attrData = (AttrData) attrTypeData.getData();
String format = attrData.getFormat();
String[] types = format.split("\\|");
for (String type : types) {
if ("reference".equals(type)) continue; // already handled above
Converter converter = Converter.getConverterFor(attrData, type);
if (converter != null) {
if (converter.fillTypedValue(attribute.value, outValue)) {
return;
}
}
}
} else {
/**
* In cases where the runtime framework doesn't know this attribute, e.g: viewportHeight (added in 21) on a
* KitKat runtine, then infer the attribute type from the value.
*
* TODO: When we are able to pass the SDK resources from the build environment then we can remove this
* and replace the NullResourceLoader with simple ResourceLoader that only parses attribute type information.
*/
ResType resType = ResType.inferFromValue(attribute.value);
Converter.getConverter(resType).fillTypedValue(attribute.value, outValue);
}
}
public void __constructor__() {
resourceLoader = RuntimeEnvironment.getAppResourceLoader();
}
public void __constructor__(boolean isSystem) {
resourceLoader = isSystem ? RuntimeEnvironment.getSystemResourceLoader() : RuntimeEnvironment.getAppResourceLoader();
}
public ResourceLoader getResourceLoader() {
return resourceLoader;
}
@HiddenApi @Implementation
public CharSequence getResourceText(int ident) {
TypedResource value = getAndResolve(ident, RuntimeEnvironment.getQualifiers(), true);
if (value == null) return null;
return (CharSequence) value.getData();
}
@HiddenApi @Implementation
public CharSequence getResourceBagText(int ident, int bagEntryId) {
throw new UnsupportedOperationException(); // todo
}
@HiddenApi @Implementation
public String[] getResourceStringArray(final int id) {
CharSequence[] resourceTextArray = getResourceTextArray(id);
if (resourceTextArray == null) return null;
String[] strings = new String[resourceTextArray.length];
for (int i = 0; i < strings.length; i++) {
strings[i] = resourceTextArray[i].toString();
}
return strings;
}
@HiddenApi @Implementation
public int getResourceIdentifier(String name, String defType, String defPackage) {
ResName resName = ResName.qualifyResName(name, defPackage, defType);
// If the resource does not exist then return 0, otherwise ResourceIndex.getResourceId() will generate a placeholder.
if (!ResName.ID_TYPE.equals(resName.type)
&& !getResourceLoader(resName).hasValue(resName, RuntimeEnvironment.getQualifiers())) {
return 0;
}
Integer resourceId = getResourceLoader(resName).getResourceIndex().getResourceId(resName);
return resourceId == null ? 0 : resourceId;
}
@HiddenApi @Implementation
public boolean getResourceValue(int ident, int density, TypedValue outValue, boolean resolveRefs) {
TypedResource value = getAndResolve(ident, RuntimeEnvironment.getQualifiers(), resolveRefs);
if (value == null) return false;
getConverter(value).fillTypedValue(value.getData(), outValue);
return true;
}
private Converter getConverter(TypedResource value) {
if (value instanceof FileTypedResource.Image
|| (value instanceof FileTypedResource
&& ((FileTypedResource) value).getFsFile().getName().endsWith(".xml"))) {
return new Converter.FromFilePath();
}
return Converter.getConverter(value.getResType());
}
@HiddenApi @Implementation
public CharSequence[] getResourceTextArray(int resId) {
TypedResource value = getAndResolve(resId, RuntimeEnvironment.getQualifiers(), true);
if (value == null) return null;
TypedResource[] items = getConverter(value).getItems(value);
CharSequence[] charSequences = new CharSequence[items.length];
for (int i = 0; i < items.length; i++) {
TypedResource typedResource = resolve(items[i], RuntimeEnvironment.getQualifiers(), resId);
charSequences[i] = getConverter(typedResource).asCharSequence(typedResource);
}
return charSequences;
}
@HiddenApi @Implementation(maxSdk = KITKAT_WATCH)
public boolean getThemeValue(int themePtr, int ident, TypedValue outValue, boolean resolveRefs) {
return getThemeValue((long) themePtr, ident, outValue, resolveRefs);
}
@HiddenApi @Implementation(minSdk = LOLLIPOP)
public boolean getThemeValue(long themePtr, int ident, TypedValue outValue, boolean resolveRefs) {
ResourceIndex resourceIndex = getResourceLoader(ident).getResourceIndex();
ResName resName = resourceIndex.getResName(ident);
ThemeStyleSet themeStyleSet = getNativeTheme(themePtr).themeStyleSet;
AttributeResource attrValue = themeStyleSet.getAttrValue(resName);
while(resolveRefs && attrValue != null && attrValue.isStyleReference()) {
ResName attrResName = new ResName(attrValue.contextPackageName, "attr", attrValue.value.substring(1));
attrValue = themeStyleSet.getAttrValue(attrResName);
}
if (attrValue != null) {
convertAndFill(attrValue, outValue, RuntimeEnvironment.getQualifiers(), resolveRefs);
return true;
}
return false;
}
@HiddenApi @Implementation
public void ensureStringBlocks() {
}
@Implementation
public final InputStream open(String fileName) throws IOException {
return ShadowApplication.getInstance().getAppManifest().getAssetsDirectory().join(fileName).getInputStream();
}
@Implementation
public final InputStream open(String fileName, int accessMode) throws IOException {
return ShadowApplication.getInstance().getAppManifest().getAssetsDirectory().join(fileName).getInputStream();
}
@Implementation
public final AssetFileDescriptor openFd(String fileName) throws IOException {
File file = new File(ShadowApplication.getInstance().getAppManifest().getAssetsDirectory().join(fileName).getPath());
ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
return new AssetFileDescriptor(parcelFileDescriptor, 0, file.length());
}
@Implementation
public final String[] list(String path) throws IOException {
FsFile file = ShadowApplication.getInstance().getAppManifest().getAssetsDirectory().join(path);
if (file.isDirectory()) {
return file.listFileNames();
}
return new String[0];
}
@HiddenApi @Implementation
public final InputStream openNonAsset(int cookie, String fileName, int accessMode) throws IOException {
final ResName resName = qualifyFromNonAssetFileName(fileName);
ResourceLoader resourceLoader = getResourceLoader(resName);
final FileTypedResource typedResource =
(FileTypedResource) resourceLoader.getValue(resName, RuntimeEnvironment.getQualifiers());
if (typedResource == null) {
throw new IOException("Unable to find resource for " + fileName);
}
if (accessMode == AssetManager.ACCESS_STREAMING) {
return typedResource.getFsFile().getInputStream();
} else {
return new ByteArrayInputStream(typedResource.getFsFile().getBytes());
}
}
private ResName qualifyFromNonAssetFileName(String fileName) {
if (fileName.startsWith("jar:")) {
// Must remove "jar:" prefix, or else qualifyFromFilePath fails on Windows
return ResName.qualifyFromFilePath("android", fileName.replaceFirst("jar:", ""));
} else {
return ResName.qualifyFromFilePath(ShadowApplication.getInstance().getAppManifest().getPackageName(), fileName);
}
}
@HiddenApi @Implementation
public final AssetFileDescriptor openNonAssetFd(int cookie, String fileName) throws IOException {
throw new UnsupportedOperationException();
}
@Implementation
public final XmlResourceParser openXmlResourceParser(int cookie, String fileName) throws IOException {
return ResourceParser.create(fileName, "fixme", "fixme", null);
}
public XmlResourceParser loadXmlResourceParser(int resId, String type) throws Resources.NotFoundException {
ResName resName = getResName(resId);
ResName resolvedResName = resolveResName(resName, RuntimeEnvironment.getQualifiers());
if (resolvedResName == null) {
throw new RuntimeException("couldn't resolve " + resName.getFullyQualifiedName());
}
resName = resolvedResName;
ResourceLoader resourceLoader = getResourceLoader(resId);
XmlBlock block = resourceLoader.getXml(resName, RuntimeEnvironment.getQualifiers());
if (block == null) {
throw new Resources.NotFoundException(resName.getFullyQualifiedName());
}
return ResourceParser.from(block, resName.packageName, resourceLoader);
}
@HiddenApi @Implementation
public int addAssetPath(String path) {
return 1;
}
@HiddenApi @Implementation
public boolean isUpToDate() {
return true;
}
@HiddenApi @Implementation
public void setLocale(String locale) {
}
@Implementation
public String[] getLocales() {
return new String[0]; // todo
}
@HiddenApi @Implementation
public void setConfiguration(int mcc, int mnc, String locale,
int orientation, int touchscreen, int density, int keyboard,
int keyboardHidden, int navigation, int screenWidth, int screenHeight,
int smallestScreenWidthDp, int screenWidthDp, int screenHeightDp,
int screenLayout, int uiMode, int majorVersion) {
}
@HiddenApi @Implementation
public int[] getArrayIntResource(int resId) {
TypedResource value = getAndResolve(resId, RuntimeEnvironment.getQualifiers(), true);
if (value == null) return null;
TypedResource[] items = getConverter(value).getItems(value);
int[] ints = new int[items.length];
for (int i = 0; i < items.length; i++) {
TypedResource typedResource = resolve(items[i], RuntimeEnvironment.getQualifiers(), resId);
ints[i] = getConverter(typedResource).asInt(typedResource);
}
return ints;
}
@HiddenApi @Implementation
public Number createTheme() {
synchronized (nativeThemes) {
long nativePtr = nextInternalThemeId++;
nativeThemes.put(nativePtr, new NativeTheme(new ThemeStyleSet()));
return castNativePtr(nativePtr);
}
}
private static NativeTheme getNativeTheme(Resources.Theme theme) {
return getNativeTheme(shadowOf(theme).getNativePtr());
}
private static NativeTheme getNativeTheme(long themePtr) {
NativeTheme nativeTheme;
synchronized (nativeThemes) {
nativeTheme = nativeThemes.get(themePtr);
}
if (nativeTheme == null) {
throw new RuntimeException("no theme " + themePtr + " found in AssetManager");
}
return nativeTheme;
}
@HiddenApi @Implementation(maxSdk = KITKAT_WATCH)
public void releaseTheme(int themePtr) {
releaseTheme((long) themePtr);
}
@HiddenApi @Implementation(minSdk = LOLLIPOP)
public void releaseTheme(long themePtr) {
synchronized (nativeThemes) {
nativeThemes.remove(themePtr);
}
}
@HiddenApi @Implementation(maxSdk = KITKAT_WATCH)
public static void applyThemeStyle(int themePtr, int styleRes, boolean force) {
applyThemeStyle((long) themePtr, styleRes, force);
}
@HiddenApi @Implementation(minSdk = LOLLIPOP)
public static void applyThemeStyle(long themePtr, int styleRes, boolean force) {
NativeTheme nativeTheme = getNativeTheme(themePtr);
Style style = nativeTheme.getShadowAssetManager().resolveStyle(styleRes, null);
nativeTheme.themeStyleSet.apply(style, force);
}
@HiddenApi @Implementation(maxSdk = KITKAT_WATCH)
public static void copyTheme(int destPtr, int sourcePtr) {
copyTheme((long) destPtr, (long) sourcePtr);
}
@HiddenApi @Implementation(minSdk = LOLLIPOP)
public static void copyTheme(long destPtr, long sourcePtr) {
NativeTheme destNativeTheme = getNativeTheme(destPtr);
NativeTheme sourceNativeTheme = getNativeTheme(sourcePtr);
destNativeTheme.themeStyleSet = sourceNativeTheme.themeStyleSet.copy();
}
/////////////////////////
Style resolveStyle(int resId, Style themeStyleSet) {
ShadowAssetManager shadowAssetManager = ResourceIds.isFrameworkResource(resId) ? shadowOf(AssetManager.getSystem()) : this;
return shadowAssetManager.resolveStyle(getResName(resId), themeStyleSet);
}
private Style resolveStyle(@NotNull ResName themeStyleName, Style themeStyleSet) {
TypedResource themeStyleResource = getResourceLoader(themeStyleName).getValue(themeStyleName, RuntimeEnvironment.getQualifiers());
if (themeStyleResource == null) return null;
StyleData themeStyleData = (StyleData) themeStyleResource.getData();
if (themeStyleSet == null) {
themeStyleSet = new ThemeStyleSet();
}
return new StyleResolver(resourceLoader, shadowOf(AssetManager.getSystem()).getResourceLoader(), themeStyleData, themeStyleSet, themeStyleName, RuntimeEnvironment.getQualifiers());
}
private TypedResource getAndResolve(int resId, String qualifiers, boolean resolveRefs) {
ResourceLoader resourceLoader = getResourceLoader(resId);
TypedResource value = resourceLoader.getValue(resId, qualifiers);
if (resolveRefs) {
value = resolve(value, qualifiers, resId);
}
// todo: make the drawable loader put stuff into the normal spot...
String resourceTypeName = getResourceTypeName(resId);
if (value == null && DrawableResourceLoader.isStillHandledHere(resourceTypeName)) {
FileTypedResource typedResource = (FileTypedResource) resourceLoader.getValue(resId, qualifiers);
return new TypedResource<>(typedResource.getFsFile(), ResType.FILE, typedResource.getXmlContext());
}
// todo: gross. this is so resources.getString(R.layout.foo) works for ABS.
if (value == null && "layout".equals(resourceTypeName)) {
throw new UnsupportedOperationException("ugh, this doesn't work still?");
}
return value;
}
/**
* Returns either the Application's ResourceLoader or the Framework's ResourceLoader based on the package type of the resId
*/
ResourceLoader getResourceLoader(int resId) {
return ResourceIds.isFrameworkResource(resId) ? shadowOf(AssetManager.getSystem()).getResourceLoader() : this.resourceLoader;
}
/**
* Returns either the Application's ResourceLoader or the Framework's ResourceLoader based on the package of the ResName
*/
private ResourceLoader getResourceLoader(ResName resName) {
return "android".equals(resName.packageName) ? shadowOf(AssetManager.getSystem()).getResourceLoader() : this.resourceLoader;
}
TypedResource resolve(TypedResource value, String qualifiers, int resId) {
return resolveResourceValue(value, qualifiers, resId);
}
public ResName resolveResName(ResName resName, String qualifiers) {
TypedResource value = getResourceLoader(resName).getValue(resName, qualifiers);
return resolveResource(value, qualifiers, resName);
}
// todo: DRY up #resolveResource vs #resolveResourceValue
private ResName resolveResource(TypedResource value, String qualifiers, ResName resName) {
while (value != null && value.isReference()) {
String s = value.asString();
if (AttributeResource.isNull(s) || AttributeResource.isEmpty(s)) {
value = null;
} else {
String refStr = s.substring(1).replace("+", "");
resName = ResName.qualifyResName(refStr, resName);
value = getResourceLoader(resName).getValue(resName, qualifiers);
}
}
return resName;
}
private TypedResource resolveResourceValue(TypedResource value, String qualifiers, ResName resName) {
while (value != null && value.isReference()) {
String s = value.asString();
if (AttributeResource.isNull(s) || AttributeResource.isEmpty(s)) {
value = null;
} else {
String refStr = s.substring(1).replace("+", "");
resName = ResName.qualifyResName(refStr, resName);
value = getResourceLoader(resName).getValue(resName, qualifiers);
}
}
return value;
}
public TypedResource resolveResourceValue(TypedResource value, String qualifiers, int resId) {
ResName resName = getResName(resId);
return resolveResourceValue(value, qualifiers, resName);
}
private TypedValue buildTypedValue(AttributeSet set, int resId, int defStyleAttr, Style themeStyleSet, int defStyleRes) {
/*
* When determining the final value of a particular attribute, there are four inputs that come into play:
*
* 1. Any attribute values in the given AttributeSet.
* 2. The style resource specified in the AttributeSet (named "style").
* 3. The default style specified by defStyleAttr and defStyleRes
* 4. The base values in this theme.
*/
Style defStyleFromAttr = null;
Style defStyleFromRes = null;
Style styleAttrStyle = null;
if (defStyleAttr != 0) {
// Load the theme attribute for the default style attributes. E.g., attr/buttonStyle
ResName defStyleName = getResName(defStyleAttr);
// Load the style for the default style attribute. E.g. "@style/Widget.Robolectric.Button";
AttributeResource defStyleAttribute = themeStyleSet.getAttrValue(defStyleName);
if (defStyleAttribute != null) {
while (defStyleAttribute.isStyleReference()) {
AttributeResource other = themeStyleSet.getAttrValue(defStyleAttribute.getStyleReference());
if (other == null) {
throw new RuntimeException("couldn't dereference " + defStyleAttribute);
}
defStyleAttribute = other;
}
if (defStyleAttribute.isResourceReference()) {
ResName defStyleResName = defStyleAttribute.getResourceReference();
defStyleFromAttr = resolveStyle(defStyleResName, themeStyleSet);
}
}
}
if (set != null && set.getStyleAttribute() != 0) {
ResName styleAttributeResName = getResName(set.getStyleAttribute());
while (styleAttributeResName.type.equals("attr")) {
AttributeResource attrValue = themeStyleSet.getAttrValue(styleAttributeResName);
if (attrValue == null) {
throw new RuntimeException(
"no value for " + styleAttributeResName.getFullyQualifiedName()
+ " in " + themeStyleSet);
}
if (attrValue.isResourceReference()) {
styleAttributeResName = attrValue.getResourceReference();
} else if (attrValue.isStyleReference()) {
styleAttributeResName = attrValue.getStyleReference();
}
}
styleAttrStyle = resolveStyle(styleAttributeResName, themeStyleSet);
}
if (defStyleRes != 0) {
ResName resName = getResName(defStyleRes);
if (resName.type.equals("attr")) {
AttributeResource attributeValue = findAttributeValue(defStyleRes, set, styleAttrStyle, defStyleFromAttr, defStyleFromAttr, themeStyleSet);
if (attributeValue != null) {
if (attributeValue.isStyleReference()) {
resName = themeStyleSet.getAttrValue(attributeValue.getStyleReference()).getResourceReference();
} else if (attributeValue.isResourceReference()) {
resName = attributeValue.getResourceReference();
}
}
}
defStyleFromRes = resolveStyle(resName, themeStyleSet);
}
AttributeResource attribute = findAttributeValue(resId, set, styleAttrStyle, defStyleFromAttr, defStyleFromRes, themeStyleSet);
while (attribute != null && attribute.isStyleReference()) {
ResName otherAttrName = attribute.getStyleReference();
if (attribute.resName.equals(otherAttrName)) {
Logger.info("huh... circular reference for %s?", attribute.resName.getFullyQualifiedName());
return null;
}
ResName resName = getResourceLoader(resId).getResourceIndex().getResName(resId);
AttributeResource otherAttr = themeStyleSet.getAttrValue(otherAttrName);
if (otherAttr == null) {
strictError("no such attr %s in %s while resolving value for %s", attribute.value, themeStyleSet, resName.getFullyQualifiedName());
attribute = null;
} else {
attribute = new AttributeResource(resName, otherAttr.value, otherAttr.contextPackageName);
}
}
if (attribute == null || attribute.isNull()) {
return null;
} else {
TypedValue typedValue = new TypedValue();
convertAndFill(attribute, typedValue, RuntimeEnvironment.getQualifiers(), true);
return typedValue;
}
}
private void strictError(String message, Object... args) {
if (strictErrors) {
throw new RuntimeException(String.format(message, args));
} else {
Logger.strict(message, args);
}
}
TypedArray attrsToTypedArray(Resources resources, AttributeSet set, int[] attrs, int defStyleAttr, long nativeTheme, int defStyleRes) {
CharSequence[] stringData = new CharSequence[attrs.length];
int[] data = new int[attrs.length * ShadowAssetManager.STYLE_NUM_ENTRIES];
int[] indices = new int[attrs.length + 1];
int nextIndex = 0;
Style themeStyleSet = nativeTheme == 0
? new EmptyStyle()
: getNativeTheme(nativeTheme).themeStyleSet;
for (int i = 0; i < attrs.length; i++) {
int offset = i * ShadowAssetManager.STYLE_NUM_ENTRIES;
TypedValue typedValue = buildTypedValue(set, attrs[i], defStyleAttr, themeStyleSet, defStyleRes);
if (typedValue != null) {
//noinspection PointlessArithmeticExpression
data[offset + ShadowAssetManager.STYLE_TYPE] = typedValue.type;
data[offset + ShadowAssetManager.STYLE_DATA] = typedValue.type == TypedValue.TYPE_STRING ? i : typedValue.data;
data[offset + ShadowAssetManager.STYLE_ASSET_COOKIE] = typedValue.assetCookie;
data[offset + ShadowAssetManager.STYLE_RESOURCE_ID] = typedValue.resourceId;
data[offset + ShadowAssetManager.STYLE_CHANGING_CONFIGURATIONS] = typedValue.changingConfigurations;
data[offset + ShadowAssetManager.STYLE_DENSITY] = typedValue.density;
stringData[i] = typedValue.string;
indices[nextIndex + 1] = i;
nextIndex++;
}
}
indices[0] = nextIndex;
TypedArray typedArray = ShadowTypedArray.create(resources, attrs, data, indices, nextIndex, stringData);
if (set != null) {
shadowOf(typedArray).positionDescription = set.getPositionDescription();
}
return typedArray;
}
private AttributeResource findAttributeValue(int resId, AttributeSet attributeSet, Style styleAttrStyle, Style defStyleFromAttr, Style defStyleFromRes, @NotNull Style themeStyleSet) {
if (attributeSet != null) {
for (int i = 0; i < attributeSet.getAttributeCount(); i++) {
if (attributeSet.getAttributeNameResource(i) == resId && attributeSet.getAttributeValue(i) != null) {
// TODO: If there is a match in the XML then we should get the resource name from the XML rather than look it up from the app resource loader.
ResName resName = resourceLoader.getResourceIndex().getResName(resId);
if (resName == null) {
// We're looking for an attribute that doesn't yet exist at this SDK level just pass it back blindly and
// assume the user knows what they're doing with it.
resName = ResName.qualifyResName(attributeSet.getAttributeName(i), null, "attr");
}
return new AttributeResource(resName, attributeSet.getAttributeValue(i), "fixme!!!");
}
}
}
ResourceLoader resourceLoader = getResourceLoader(resId);
ResName attrName = resourceLoader.getResourceIndex().getResName(resId);
if (attrName == null) return null;
if (styleAttrStyle != null) {
AttributeResource attribute = styleAttrStyle.getAttrValue(attrName);
if (attribute != null) {
return attribute;
}
}
// else if attr in defStyleFromAttr, use its value
if (defStyleFromAttr != null) {
AttributeResource attribute = defStyleFromAttr.getAttrValue(attrName);
if (attribute != null) {
return attribute;
}
}
if (defStyleFromRes != null) {
AttributeResource attribute = defStyleFromRes.getAttrValue(attrName);
if (attribute != null) {
return attribute;
}
}
// else if attr in theme, use its value
return themeStyleSet.getAttrValue(attrName);
}
@NotNull private ResName getResName(int id) {
ResourceLoader resourceLoader = getResourceLoader(id);
ResName resName = resourceLoader.getResourceIndex().getResName(id);
if (resName == null) {
List<String> packages = new ArrayList<>(resourceLoader.getResourceIndex().getPackages());
Collections.sort(packages);
throw new Resources.NotFoundException("Unable to find resource ID #0x" + Integer.toHexString(id)
+ " in packages " + packages);
}
return resName;
}
@Implementation
public String getResourceName(int resid) {
return getResName(resid).getFullyQualifiedName();
}
@Implementation
public String getResourcePackageName(int resid) {
return getResName(resid).packageName;
}
@Implementation
public String getResourceTypeName(int resid) {
return getResName(resid).type;
}
@Implementation
public String getResourceEntryName(int resid) {
return getResName(resid).name;
}
@Resetter
public static void reset() {
ReflectionHelpers.setStaticField(AssetManager.class, "sSystem", null);
}
}