1 package org.robolectric.shadows; 2 3 import android.content.res.Resources; 4 import android.util.LongSparseArray; 5 import java.lang.reflect.Field; 6 import java.lang.reflect.Modifier; 7 import java.util.ArrayList; 8 import java.util.List; 9 import org.robolectric.shadows.ShadowLegacyResourcesImpl.ShadowLegacyThemeImpl; 10 11 abstract public class ShadowResourcesImpl { 12 13 public static class Picker extends ResourceModeShadowPicker<ShadowResourcesImpl> { 14 Picker()15 public Picker() { 16 super(ShadowLegacyResourcesImpl.class, ShadowArscResourcesImpl.class, 17 ShadowArscResourcesImpl.class); 18 } 19 } 20 21 private static List<LongSparseArray<?>> resettableArrays; 22 reset()23 public static void reset() { 24 if (resettableArrays == null) { 25 resettableArrays = obtainResettableArrays(); 26 } 27 for (LongSparseArray<?> sparseArray : resettableArrays) { 28 sparseArray.clear(); 29 } 30 } 31 obtainResettableArrays()32 private static List<LongSparseArray<?>> obtainResettableArrays() { 33 List<LongSparseArray<?>> resettableArrays = new ArrayList<>(); 34 Field[] allFields = Resources.class.getDeclaredFields(); 35 for (Field field : allFields) { 36 if (Modifier.isStatic(field.getModifiers()) && field.getType().equals(LongSparseArray.class)) { 37 field.setAccessible(true); 38 try { 39 LongSparseArray<?> longSparseArray = (LongSparseArray<?>) field.get(null); 40 if (longSparseArray != null) { 41 resettableArrays.add(longSparseArray); 42 } 43 } catch (IllegalAccessException e) { 44 throw new RuntimeException(e); 45 } 46 } 47 } 48 return resettableArrays; 49 } 50 51 abstract public static class ShadowThemeImpl { 52 public static class Picker extends ResourceModeShadowPicker<ShadowThemeImpl> { 53 Picker()54 public Picker() { 55 super(ShadowLegacyThemeImpl.class, null, null); 56 } 57 } 58 } 59 } 60