From bef8ca1b49c92660410c6a68cda326af86cfa749 Mon Sep 17 00:00:00 2001 From: Material Design Team Date: Fri, 20 May 2022 04:08:07 -0400 Subject: [PATCH] [M3][Color] Fix Resources Loader bug for color harmonization PiperOrigin-RevId: 449926388 (cherry picked from commit 78d2c1f3db44c07fc84e6878fcd56bc2a5db310e) --- .../color/ColorResourcesTableCreator.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/java/com/google/android/material/color/ColorResourcesTableCreator.java b/lib/java/com/google/android/material/color/ColorResourcesTableCreator.java index 3ace2fe5fe8..cabc900189b 100644 --- a/lib/java/com/google/android/material/color/ColorResourcesTableCreator.java +++ b/lib/java/com/google/android/material/color/ColorResourcesTableCreator.java @@ -46,11 +46,13 @@ private ColorResourcesTableCreator() {} private static final short HEADER_TYPE_TYPE = 0x0201; private static final short HEADER_TYPE_TYPE_SPEC = 0x0202; - private static final byte TYPE_ID_COLOR = 0x06; - private static final byte ANDROID_PACKAGE_ID = 0x01; private static final byte APPLICATION_PACKAGE_ID = 0x7F; + private static final String RESOURCE_TYPE_NAME_COLOR = "color"; + + private static byte typeIdColor; + private static final PackageInfo ANDROID_PACKAGE_INFO = new PackageInfo(ANDROID_PACKAGE_ID, "android"); @@ -63,17 +65,24 @@ public int compare(ColorResource res1, ColorResource res2) { }; static byte[] create(Context context, Map colorMapping) throws IOException { + if (colorMapping.entrySet().isEmpty()) { + throw new IllegalArgumentException("No color resources provided for harmonization."); + } PackageInfo applicationPackageInfo = new PackageInfo(APPLICATION_PACKAGE_ID, context.getPackageName()); Map> colorResourceMap = new HashMap<>(); + ColorResource colorResource = null; for (Map.Entry entry : colorMapping.entrySet()) { - ColorResource colorResource = + colorResource = new ColorResource( entry.getKey(), context.getResources().getResourceName(entry.getKey()), entry.getValue()); - if (colorResource.typeId != TYPE_ID_COLOR) { + if (!context + .getResources() + .getResourceTypeName(entry.getKey()) + .equals(RESOURCE_TYPE_NAME_COLOR)) { throw new IllegalArgumentException( "Non color resource found: name=" + colorResource.name @@ -94,6 +103,13 @@ static byte[] create(Context context, Map colorMapping) throws } colorResourceMap.get(packageInfo).add(colorResource); } + // Resource Type Ids are assigned by aapt arbitrarily, for each new type the next available + // number is assigned and used. The type id will be the same for resources that are the same + // type. + typeIdColor = colorResource.typeId; + if (typeIdColor == 0) { + throw new IllegalArgumentException("No color resources found for harmonization."); + } ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); new ResTable(colorResourceMap).writeTo(outputStream); return outputStream.toByteArray(); @@ -403,7 +419,7 @@ private static class TypeSpecChunk { void writeTo(ByteArrayOutputStream outputStream) throws IOException { header.writeTo(outputStream); - outputStream.write(new byte[] {TYPE_ID_COLOR, 0x00, 0x00, 0x00}); + outputStream.write(new byte[] {typeIdColor, 0x00, 0x00, 0x00}); outputStream.write(intToByteArray(entryCount)); for (int entryFlag : entryFlags) { outputStream.write(intToByteArray(entryFlag)); @@ -465,7 +481,7 @@ private static class TypeChunk { void writeTo(ByteArrayOutputStream outputStream) throws IOException { header.writeTo(outputStream); - outputStream.write(new byte[] {TYPE_ID_COLOR, 0x00, 0x00, 0x00}); + outputStream.write(new byte[] {typeIdColor, 0x00, 0x00, 0x00}); outputStream.write(intToByteArray(entryCount)); outputStream.write(intToByteArray(getEntryStart())); outputStream.write(config);