diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java index 2e149748636b9..93fd005ebc057 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java @@ -52,7 +52,6 @@ import java.lang.ref.SoftReference; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.Arrays; -import java.util.Comparator; import java.util.EnumMap; import java.util.EnumSet; import java.util.HashMap; @@ -62,7 +61,6 @@ import java.util.concurrent.locks.ReentrantLock; import libcore.io.MemoryMappedFile_Delegate; import static com.android.ide.common.rendering.api.Result.Status.ERROR_UNKNOWN; -import static com.android.ide.common.rendering.api.Result.Status.SUCCESS; /** * Main entry point of the LayoutLib Bridge. @@ -90,19 +88,14 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { /** * Maps from id to resource type/name. This is for com.android.internal.R */ - private final static Map> sRMap = - new HashMap>(); + @SuppressWarnings("deprecation") + private final static Map> sRMap = new HashMap<>(); - /** - * Same as sRMap except for int[] instead of int resources. This is for android.R only. - */ - private final static Map sRArrayMap = new HashMap(384); /** * Reverse map compared to sRMap, resource type -> (resource name -> id). * This is for com.android.internal.R. */ - private final static Map> sRevRMap = - new EnumMap>(ResourceType.class); + private final static Map> sRevRMap = new EnumMap<>(ResourceType.class); // framework resources are defined as 0x01XX#### where XX is the resource type (layout, // drawable, etc...). Using FF as the type allows for 255 resource types before we get a @@ -111,55 +104,18 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { private final static DynamicIdMap sDynamicIds = new DynamicIdMap(DYNAMIC_ID_SEED_START); private final static Map>> sProjectBitmapCache = - new HashMap>>(); + new HashMap<>(); private final static Map>> sProject9PatchCache = - new HashMap>>(); - private final static Map> sFrameworkBitmapCache = - new HashMap>(); + new HashMap<>(); + + private final static Map> sFrameworkBitmapCache = new HashMap<>(); private final static Map> sFramework9PatchCache = - new HashMap>(); + new HashMap<>(); private static Map> sEnumValueMap; private static Map sPlatformProperties; - /** - * int[] wrapper to use as keys in maps. - */ - private final static class IntArray { - private int[] mArray; - - private IntArray() { - // do nothing - } - - private IntArray(int[] a) { - mArray = a; - } - - private void set(int[] a) { - mArray = a; - } - - @Override - public int hashCode() { - return Arrays.hashCode(mArray); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null) return false; - if (getClass() != obj.getClass()) return false; - - IntArray other = (IntArray) obj; - return Arrays.equals(mArray, other.mArray); - } - } - - /** Instance of IntArrayWrapper to be reused in {@link #resolveResourceId(int[])}. */ - private final static IntArray sIntArrayWrapper = new IntArray(); - /** * A default log than prints to stdout/stderr. */ @@ -192,6 +148,7 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { return com.android.ide.common.rendering.api.Bridge.API_CURRENT; } + @SuppressWarnings("deprecation") @Override @Deprecated public EnumSet getCapabilities() { @@ -272,11 +229,11 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { case STRING: case STYLE: // Slightly less than thousand entries in each. - fullMap = new HashMap(1280); + fullMap = new HashMap<>(1280); // no break. default: if (fullMap == null) { - fullMap = new HashMap(); + fullMap = new HashMap<>(); } sRevRMap.put(resType, fullMap); } @@ -288,13 +245,9 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { continue; } Class type = f.getType(); - if (type.isArray()) { - // if the object is an int[] we put it in sRArrayMap using an IntArray - // wrapper that properly implements equals and hashcode for the array - // objects, as required by the map contract. - sRArrayMap.put(new IntArray((int[]) f.get(null)), f.getName()); - } else { + if (!type.isArray()) { Integer value = (Integer) f.get(null); + //noinspection deprecation sRMap.put(value, Pair.of(resType, f.getName())); fullMap.put(f.getName(), value); } @@ -332,32 +285,29 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { // values, we try and find them from the styleables. // There were 1500 elements in this map at M timeframe. - Map revRAttrMap = new HashMap(2048); + Map revRAttrMap = new HashMap<>(2048); sRevRMap.put(ResourceType.ATTR, revRAttrMap); // There were 2000 elements in this map at M timeframe. - Map revRStyleableMap = new HashMap(3072); + Map revRStyleableMap = new HashMap<>(3072); sRevRMap.put(ResourceType.STYLEABLE, revRStyleableMap); Class c = com.android.internal.R.styleable.class; Field[] fields = c.getDeclaredFields(); // Sort the fields to bring all arrays to the beginning, so that indices into the array are // able to refer back to the arrays (i.e. no forward references). - Arrays.sort(fields, new Comparator() { - @Override - public int compare(Field o1, Field o2) { - if (o1 == o2) { - return 0; - } - Class t1 = o1.getType(); - Class t2 = o2.getType(); - if (t1.isArray() && !t2.isArray()) { - return -1; - } else if (t2.isArray() && !t1.isArray()) { - return 1; - } - return o1.getName().compareTo(o2.getName()); + Arrays.sort(fields, (o1, o2) -> { + if (o1 == o2) { + return 0; } + Class t1 = o1.getType(); + Class t2 = o2.getType(); + if (t1.isArray() && !t2.isArray()) { + return -1; + } else if (t2.isArray() && !t1.isArray()) { + return 1; + } + return o1.getName().compareTo(o2.getName()); }); - Map styleables = new HashMap(); + Map styleables = new HashMap<>(); for (Field field : fields) { if (!isValidRField(field)) { // Only consider public static fields that are int or int[]. @@ -367,7 +317,6 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { String name = field.getName(); if (field.getType().isArray()) { int[] styleableValue = (int[]) field.get(null); - sRArrayMap.put(new IntArray(styleableValue), name); styleables.put(name, styleableValue); continue; } @@ -389,9 +338,11 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { if (arrayValue != null) { String attrName = name.substring(arrayName.length() + 1); int attrValue = arrayValue[index]; + //noinspection deprecation sRMap.put(attrValue, Pair.of(ResourceType.ATTR, attrName)); revRAttrMap.put(attrName, attrValue); } + //noinspection deprecation sRMap.put(index, Pair.of(ResourceType.STYLEABLE, name)); revRStyleableMap.put(name, index); } @@ -422,7 +373,7 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { @Override public RenderSession createSession(SessionParams params) { try { - Result lastResult = SUCCESS.createResult(); + Result lastResult; RenderSessionImpl scene = new RenderSessionImpl(params); try { prepareThread(); @@ -456,7 +407,7 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { @Override public Result renderDrawable(DrawableParams params) { try { - Result lastResult = SUCCESS.createResult(); + Result lastResult; RenderDrawable action = new RenderDrawable(params); try { prepareThread(); @@ -581,25 +532,15 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { * @return a Pair containing the resource type and name, or null if the id * does not match any resource. */ + @SuppressWarnings("deprecation") public static Pair resolveResourceId(int value) { Pair pair = sRMap.get(value); if (pair == null) { pair = sDynamicIds.resolveId(value); - if (pair == null) { - //System.out.println(String.format("Missing id: %1$08X (%1$d)", value)); - } } return pair; } - /** - * Returns the name of a framework resource whose value is an int array. - */ - public static String resolveResourceId(int[] array) { - sIntArrayWrapper.set(array); - return sRArrayMap.get(sIntArrayWrapper); - } - /** * Returns the integer id of a framework resource, from a given resource type and resource name. *

@@ -674,16 +615,12 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { */ public static void setCachedBitmap(String value, Bitmap bmp, Object projectKey) { if (projectKey != null) { - Map> map = sProjectBitmapCache.get(projectKey); + Map> map = + sProjectBitmapCache.computeIfAbsent(projectKey, k -> new HashMap<>()); - if (map == null) { - map = new HashMap>(); - sProjectBitmapCache.put(projectKey, map); - } - - map.put(value, new SoftReference(bmp)); + map.put(value, new SoftReference<>(bmp)); } else { - sFrameworkBitmapCache.put(value, new SoftReference(bmp)); + sFrameworkBitmapCache.put(value, new SoftReference<>(bmp)); } } @@ -722,16 +659,12 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { */ public static void setCached9Patch(String value, NinePatchChunk ninePatch, Object projectKey) { if (projectKey != null) { - Map> map = sProject9PatchCache.get(projectKey); + Map> map = + sProject9PatchCache.computeIfAbsent(projectKey, k -> new HashMap<>()); - if (map == null) { - map = new HashMap>(); - sProject9PatchCache.put(projectKey, map); - } - - map.put(value, new SoftReference(ninePatch)); + map.put(value, new SoftReference<>(ninePatch)); } else { - sFramework9PatchCache.put(value, new SoftReference(ninePatch)); + sFramework9PatchCache.put(value, new SoftReference<>(ninePatch)); } } }